Haskell单一同态限定导致类型歧义:Ambiguous type variable ‘’ arising from a use of ‘’ prevents the constraint ‘’ f

为单一同态限定导致类型歧义提供一个例子。

问题描述

在下面这段代码中,在单一同态限定开启的状况下,函数的类型无法自动推导:

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:

  1. 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
  2. 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值