haskell - liftM in ghci: why is there such difference? -


this question has answer here:

when define such functions inside ghci:

> :m control.monad > let f n = n+1 > let g = liftm f 

they work well:

> g $ 2 > 3 > g $ [1,2] > [2,3] 

but when define same functions in file (probl.hs):

 import control.monad   f :: integer -> integer  f n = n + 2   g = liftm f 

and run file via ghci:

 ghci probl.hs 

i got such message:

probl.hs:6:5: error:     * ambiguous type variable `m0' arising use of `liftm'       prevents constraint `(monad m0)' being solved.       relevant bindings include         g :: m0 integer -> m0 integer (bound @ probl.hs:6:1) ... failed, modules loaded: none. 

why there such difference? , how solve problem second situation (i want same behavior in first one)?

you being hit dreaded monomorphism restriction! unintuitive rule turned off in ghci, on when compile. use type signature (recommended), or {-# language nomonomorphismrestriction #-} disable it. basically, makes expressions without type signatures less polymorphic expect.

further reading


Comments