[英]Could functions be instances of classes in Haskell
class A a
instance A (Int -> Float)
doesn't works and 不起作用,并且
class B b
instance B Int
works If functions in Haskell are taken as the first classes and (Int -> Float)is undoubtly a type although it is not a (*) concrete type, why can't functions be instance of classes 如果Haskell中的函数被视为第一类并且(Int-> Float)无疑是一种类型,尽管它不是(*)具体类型,为什么函数不能成为类的实例
Yes, the function type is not very special in Haskell: 是的,函数类型在Haskell中不是很特殊:
module Test where
instance Show (a -> b) where
show _ = "(function)"
As pointed out by shk in his answer, if you want to fix the type of the domain or the range, you will need an extension like FlexibleInstances – but that is not related to the function type and will be required for other type constructor applications like Maybe Int
as well. 正如shk在他的回答中所指出的那样,如果您想修复域或范围的类型,则将需要像FlexibleInstances这样的扩展名-但这与函数类型无关,并且其他类型构造函数应用程序(例如, Maybe Int
还有Maybe Int
。
With FlexibleInstances extension you can do that: 使用FlexibleInstances扩展,您可以执行以下操作:
{-# LANGUAGE FlexibleInstances #-}
module TestFlexibleInstances where
class A a
instance A (Int -> Float)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.