【发布时间】:2013-07-21 04:07:09
【问题描述】:
在 GHC 中编译此程序时:
import Control.Monad
f x = let
g y = let
h z = liftM not x
in h 0
in g 0
我收到一个错误:
test.hs:5:21:
Could not deduce (m ~ m1)
from the context (Monad m)
bound by the inferred type of f :: Monad m => m Bool -> m Bool
at test.hs:(3,1)-(7,8)
or from (m Bool ~ m1 Bool, Monad m1)
bound by the inferred type of
h :: (m Bool ~ m1 Bool, Monad m1) => t1 -> m1 Bool
at test.hs:5:5-21
`m' is a rigid type variable bound by
the inferred type of f :: Monad m => m Bool -> m Bool
at test.hs:3:1
`m1' is a rigid type variable bound by
the inferred type of
h :: (m Bool ~ m1 Bool, Monad m1) => t1 -> m1 Bool
at test.hs:5:5
Expected type: m1 Bool
Actual type: m Bool
In the second argument of `liftM', namely `x'
In the expression: liftM not x
In an equation for `h': h z = liftM not x
为什么?此外,为f (f :: Monad m => m Bool -> m Bool) 提供显式类型签名会使错误消失。但这与 Haskell 自动为 f 推断的类型完全相同,根据错误信息!
【问题讨论】:
-
单态限制?
-
单态限制只适用于简单的模式绑定,这里没有。反正加
-XNoMonomorphismRestriction是没有效果的。 -
我认为它与 let-generalization 有关,因为错误随着
-XMonoLocalBinds而消失
标签: haskell