考虑下面这个类型同义词:
type Synonym a b = (a, b)
这个代码在GHCi中无法运行:
ghci> let myFirst (f, s) = f :: Synonym a b -> a
<interactive>:1:21:
Inferred type is less polymorphic than expected
Quantified type variable `b' is mentioned in the environment:
f :: Synonym a b -> a (bound at <interactive>:1:13)
Quantified type variable `a' is mentioned in the environment:
f :: Synonym a b -> a (bound at <interactive>:1:13)
In the expression: f :: Synonym a b -> a
In the definition of `myFirst':
myFirst (f, s) = f :: Synonym a b -> a
但是这项技术可以:
ghci> let myFirst = fst :: Synonym a b -> a
-- no problem --
当我直接在GHCi中输入时,只有第一个定义有效;但是当我将它们放在文件中并使用
:load
命令加载时,两个定义都有效。这里的问题是什么?我遇到过很多次这个问题,但从未理解过为什么会出现这种情况。
附:我尝试过
:set -XNoMonomorphismRestriction
,但没有任何变化。