我尝试了一个小例子,来自于这个问题的答案:
liftTup :: (x -> f x) -> (a, b) -> (f a, f b)
liftTup liftFunc (t, v) = (liftFunc t, liftFunc v)
为了使这个工作正常,显然需要使用forall
量词,但我正在尝试理解错误消息的语法,以便在将来遇到类似问题时知道出了什么错。所以对于这个问题,我得到了以下错误信息:
monad.hs:112:28-37: Couldn't match type `x' with `b' …
`x' is a rigid type variable bound by
the type signature for
liftTup :: (x -> f x) -> (a, b) -> (f a, f b)
at /Users/user/monad.hs:111:12
`b' is a rigid type variable bound by
the type signature for
liftTup :: (x -> f x) -> (a, b) -> (f a, f b)
at /Users/user/monad.hs:111:12
Expected type: f a
Actual type: f x
In the return type of a call of `liftFunc'
In the expression: liftFunc t
In the expression: (liftFunc t, liftFunc v)
monad.hs:112:40-49: Couldn't match type `a' with `b' …
`a' is a rigid type variable bound by
the type signature for
liftTup :: (x -> f x) -> (a, b) -> (f a, f b)
at /Users/user/monad.hs:111:12
`b' is a rigid type variable bound by
the type signature for
liftTup :: (x -> f x) -> (a, b) -> (f a, f b)
at /Users/user/monad.hs:111:12
Expected type: f b
Actual type: f x
In the return type of a call of `liftFunc'
In the expression: liftFunc v
In the expression: (liftFunc t, liftFunc v)
据我所知,第一个错误与第一次将
liftFunc
应用于t
有关,因此它试图将f x
与f a
匹配。但是 b 与这有什么关系呢? b 只出现在元组的第二个元素中。我猜第二个错误来自于先前的
liftFunc
应用中将a
匹配到x
,而现在我们正在尝试在第二个应用中将其与b
匹配,但这不太对。这样说对吗?如何正确阅读这些错误消息,并且“无法匹配类型..与..”和“期望类型:..实际类型:..”消息中提到的变量之间的关系是什么?