2014-08-30 74 views
2

我正在尝试使用箭头,并面临恼人的问题 - 我必须为我实现的所有功能提供显式类型。 如果我不能提供它GHC输出一些错误,如在haskell和箭头中的类型推断

No instance for (Arrow a0) arising from a use of ‘...’ 
The type variable ‘a0’ is ambiguous 

我能提供明确的类型,但它是非常恼人的,因为每一次我改变了一些功能,那就是我必须手动更改类型的每个函数依赖的可能性改变。

是否可以强制ghc自动推断函数类型?

简单的情况

import Control.Arrow 

ss = arr 

导致

No instance for (Arrow a0) arising from a use of ‘arr’ 
    The type variable ‘a0’ is ambiguous 
    Relevant bindings include 
     ss :: (b -> c) -> a0 b c (bound at src/Main.hs:62:1) 
    Note: there are several potential instances: 
     instance Arrow Coroutine -- Defined at src/Main.hs:33:10 
     instance Arrow (->) -- Defined in ‘Control.Arrow’ 
     instance Monad m => Arrow (Kleisli m) -- Defined in ‘Control.Arrow’ 
    In the expression: arr 
    In an equation for ‘ss’: ss = arr 

,同时具有完全相同的语义

import Control.Arrow 

ss :: forall a b c. (Arrow a) => (b -> c) -> a b c 
ss = arr 

代码编译得很好。

+0

我觉得这肯定是重复的,但是我还没有发现任何其他问题,特别是来自单态限制的模糊类型错误。 – 2014-08-30 14:04:50

回答

3

最简单的办法是关闭的单态限制 - 把这个在你的源文件的顶部:

{-# LANGUAGE NoMonomorphismRestriction #-} 

原因你的错误是,虽然哈斯克尔可以推断出该类型ss精细,单态限制要求在值的顶级定义中,除非存在明确的类型签名,否则类型不是类型类型的多态(例如Arrow)。