为单一同态限定导致类型歧义提供一个例子。
问题描述
在下面这段代码中,在单一同态限定开启的状况下,函数的类型无法自动推导:
my_liftA2 :: Applicative f => (c -> a -> b) -> f c -> f a -> f b
my_liftA2 f a b = f <$> a <*> b
-- my_ap :: Applicative f => f (a -> b) -> f a -> f b
my_ap = my_liftA2 id
报错如下:
Prelude> :l main
[1 of 1] Compiling Main ( main.hs, interpreted )
main.hs:5:9: error:
• Ambiguous type variable ‘f0’ arising from a use of ‘my_liftA2’
prevents the constraint ‘(Applicative f0)’ from being solved.
Relevant bindings include
my_ap :: f0 (a -> b) -> f0 a -> f0 b (bound at main.hs:5:1)
Probable fix: use a type annotation to specify what ‘f0’ should be.
These potential instances exist:
instance Applicative (Either e) -- Defined in ‘Data.Either’
instance Applicative IO -- Defined in ‘GHC.Base’
instance Applicative Maybe -- Defined in ‘GHC.Base’
...plus three others
(use -fprint-potential-instances to see them all)
• In the expression: my_liftA2 id
In an equation for ‘my_ap’: my_ap = my_liftA2 id
Failed, modules loaded: none.
解决方法
解决方法1:显式指定my_ap
的类型签名,避免type inference,只进行type check
解决方法2:关闭单一同态限定:{-# LANGUAGE NoMonomorphismRestriction #-}
或者Prelude> :set -XNoMonomorphismRestriction
或者ghc ... -XNoMonomorphismRestriction
错误的原因
什么是单一同态限定?单一同态限定的规则就是如下两条:
The usual Hindley-Milner restriction on polymorphism is that only type variables that do not occur free in the environment may be generalized. In addition, the constrained type variables of a restricted declaration group may not be generalized in the generalization step for that group. (Recall that a type variable is constrained if it must belong to some type class; see Section 4.5.2 .)
Any monomorphic type variables that remain when type inference for an entire module is complete, are considered ambiguous, and are resolved to particular types using the defaulting rules (Section 4.3.4 ).
这么做的好处是避免了一些地方的冗余计算,坏处是简单的default rule
的resolve在某些时候牺牲了原类型可以type inference出来的精细性质。
然后让我们来考虑原问题:
A pattern binding is a declaration of the form:
<pattern> = expr
所以my_ap
是一个pattern binding。
a given declaration group is unrestricted if and only if:
- every variable in the group is bound by a function binding (e.g.
f x = x
) or a simple pattern binding (e.g.plus = (+)
; Section 4.4.3.2 ), and- an explicit type signature is given for every variable in the group that is bound by simple pattern binding. (e.g.
plus :: Num a => a -> a -> a; plus = (+)
).
所以my_ap
是一个restricted declaration group,受到单一同态限定影响。
原问题错误的原因是单一同态限定所做的第二件事:
Any monomorphic type variables that remain when type inference for an entire module is complete, are considered ambiguous, and are resolved to particular types using the defaulting rules (Section 4.3.4 ).
也就是,因为my_ap
推导出来的类型my_ap :: f0 (a -> b) -> f0 a -> f0 b
包括类型变量f0
,所以系统不能容忍这件事的发生,会按照default rule
试图给f0
赋予一个真实类型,但是这件事没能完成,所以报错。
那么什么是default rule
?其实就是在一个列表里逐个试。但是对于Applicative类型类,我们没有允许这个列表,所以系统fails to do type inference,就报错,并建议我们明确my_ap
的类型,指定f0
究竟是哪个Applicative实例,这样就可以不需要对my_ap
做type inference,从而避免这个错误。而我们选择为my_ap
加上类型签名,这样my_ap
作为pattern binding就不会受到单一同态限定的影响。
最后说一下,如果上述操作是在ghci上交互地输入的,则不会出现Ambiguous type variable
这个错误。StackOverflow上是这么解释这件事的:
This is due to how ghci handles (and must handle) the interactive definitions. Basically every statement entered in ghci must be completely type checked before the following is considered; in other words it’s as if every statement was in a separate module.
本文只是根据本问题对StackOverflow上的答案进行了粗浅的总结,如果要详细了解还是要看SO上的原问题。what-is-the-monomorphism-restriction