简体   繁体   中英

Type inference in haskell and Arrows

I'm trying to use arrows and faced annoying problem - I have to provide explicit types for all functions I implemented. If I not provide it ghc outputs some error like

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

I can provide explicit types but it's VERY annoying, as every time I change some function it's a possibility that I have to manually alter types of every function depended on changed.

Is it possible to force ghc to infer function types automatically?

Trivial case

import Control.Arrow

ss = arr

causes

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

while code with exactly the same semantic

import Control.Arrow

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

compiles pretty well.

Easiest thing is to turn off the monomorphism restriction - put this at the top of your source file:

{-# LANGUAGE NoMonomorphismRestriction #-}

The reason for your error is that although Haskell can infer the type for ss fine, the monomorphism restriction requires that in a top-level definitions of a value, the type is not polymorphic over a type-class (eg Arrow ) unless there is an explicit type signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM