Haskell类型中tilde的含义(类型相等)
我一直在摆弄Haskell类型中tilde的含义(类型相等),haskell,types,tilde,Haskell,Types,Tilde,我一直在摆弄fix函数,我偶然发现: λ let fix f = let x = f x in x λ fix (+) <interactive>:15:5: Occurs check: cannot construct the infinite type: t ~ t -> t Expected type: t -> t Actual type: t -> t -> t Relevant bindings include
fix
函数,我偶然发现:
λ let fix f = let x = f x in x
λ fix (+)
<interactive>:15:5:
Occurs check: cannot construct the infinite type: t ~ t -> t
Expected type: t -> t
Actual type: t -> t -> t
Relevant bindings include it :: t (bound at <interactive>:15:1)
In the first argument of ‘fix’, namely ‘(+)’
In the expression: fix (+)
λlet fix f=let x=f x in x
λfix(+)
:15:5:
发生检查:无法构造无限类型:t~t->t
预期类型:t->t
实际类型:t->t->t
相关绑定包括it::t(绑定时间:15:1)
在“fix”的第一个参数中,即“(+)”
在表达式中:fix(+)
我很清楚为什么会发生这个错误,但我注意到上面有一个有趣的类型签名:
t~t->t
。这是什么意思?tilde在haskell中的类型签名中是什么意思?它们在哪里使用?波浪(~
)在该错误中表示类型相等。它告诉你它不能把t
推断为t->t
。该符号也用于,但这是一个完全不同的上下文。它将用于何处,而不包括类型错误?@AJFarmarTypeFamilies
扩展允许您在自己的类型签名中使用~
,以表示类型相等,与此错误消息中使用的方式相同。在使用类型同义词族时,您会发现这是您想要的,但它本身对于执行各种类型级别的技巧非常有用(我脑海中的一个例子是:您可以使用重叠实例和实例Foo X
以及实例(y~y)=>Foo y
获得类的“默认”实例)