Haskell 函数和类之间的类型签名

Haskell 函数和类之间的类型签名,haskell,Haskell,这很有效 但是 data Maybe' a = Nothing' | Just' a deriving (Show) class From' t where from' :: t a -> a instance From' Maybe' where from' (Just' x) = x 我不知道为什么它不起作用。在这个例子中,t被定义为Maybe',所以from'必须有类型Maybe'a->a。如果你写: from2 :: t a -> a from2

这很有效

但是

data Maybe' a = Nothing' | Just' a deriving (Show)

class From' t 
    where from' :: t a -> a

instance From' Maybe' 
    where from' (Just' x) = x

我不知道为什么它不起作用。

在这个例子中,
t
被定义为
Maybe'
,所以
from'
必须有类型
Maybe'a->a
。如果你写:

from2 :: t a -> a 
from2 (Just' x)  = x

Couldn't match type `t' with `Maybe''
  `t' is a rigid type variable bound by
      the type signature for from2 :: t a -> a at test.hs:11:1
In the pattern: Just' x
In an equation for `from2': from2 (Just' x) = x
那就行了。错误消息是,您的类型签名声明,对于
t
的任何选择,
from2
可以将
ta
转换为
a
,但您的定义仅在
t
可能“
时有效


顺便说一句,这个函数已经为标准的
类型定义了,可能是
数据中的
类型。可能是
模块,但您可能不应该使用它,因为如果您传递
什么都没有
,它将给出一个没有帮助的错误消息。当然,如果这只是一个例子,那就好了。

在这个例子中,
t
被定义为
Maybe'
,所以
from'
必须有类型
Maybe'a->a
。如果你写:

from2 :: t a -> a 
from2 (Just' x)  = x

Couldn't match type `t' with `Maybe''
  `t' is a rigid type variable bound by
      the type signature for from2 :: t a -> a at test.hs:11:1
In the pattern: Just' x
In an equation for `from2': from2 (Just' x) = x
那就行了。错误消息是,您的类型签名声明,对于
t
的任何选择,
from2
可以将
ta
转换为
a
,但您的定义仅在
t
可能“
时有效


顺便说一句,这个函数已经为标准的
类型定义了,可能是
数据中的
类型。可能是
模块,但您可能不应该使用它,因为如果您传递
什么都没有
,它将给出一个没有帮助的错误消息。当然,如果这只是一个例子,那也没关系。

没问题:)如果我的答案对你有帮助,你应该单击旁边的复选标记,这样问题就被标记为已解决。@ehird:他使用的类型是
Maybe'
,而不是
Maybe
。您应该更正您的答案,添加缺少的
:)没问题:)如果我的答案对您有帮助,您应该单击它旁边的复选标记,以便将问题标记为已解决。@ehird:他使用的类型是
Maybe'
,而不是
Maybe
。您应该更正您的答案,添加缺少的
:)