我正在尝试了解 RankNTypes。
为什么编译器在这种情况下抱怨a
不能[a]
?
> :{
> | tupleF2 :: (forall a . a -> b) -> (a1, a2) -> (b, b)
> | tupleF2 elemF2 (x, y) = (elemF2 x, elemF2 y)
> | :}
> :t tupleF2 tupleF2 :: (forall a. a -> b) -> (a1, a2) -> (b, b)
> :t tupleF2 length
<interactive>:1:9: error:
• Couldn't match type ‘a’ with ‘[a0]’
‘a’ is a rigid type variable bound by
a type expected by the context:
forall a. a -> Int
at <interactive>:1:1-14
Expected type: a -> Int
Actual type: [a0] -> Int
• In the first argument of ‘tupleF2’, namely ‘length’
In the expression: tupleF2 length
虽然以下类型检查正常?并且t
可以t a
。
> :t tupleF length
tupleF length :: Foldable t => (t a, t a) -> (Int, Int)
:t tupleF
tupleF :: (t -> b) -> (t, t) -> (b, b)
上述编译失败是否仅在启用时发生RankNTypes
。了解正在发生的事情的任何指示都会很棒。
谢谢。