LiftM in ghci: why is there such a difference?

When I define such functions inside ghci:

> :m Control.Monad
> let f n = n+1
> let g = liftM f

they work well:

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

But when I define the same functions in the file (probl.hs):

 import Control.Monad

 f :: Integer -> Integer
 f n = n + 2

 g = liftM f

and then run this file through ghci:

 ghci probl.hs

I received a message like this:

probl.hs:6:5: error:
    * Ambiguous type variable `m0' arising from a use of `liftM'
      prevents the constraint `(Monad m0)' from being solved.
      Relevant bindings include
        g :: m0 Integer -> m0 Integer (bound at probl.hs:6:1)
...
Failed, modules loaded: none.

Why is there such a difference? And how to solve the problem with the second situation (I want the same behavior as in the first)?

+4
source share
1 answer

! , GHCi, . () {-# LANGUAGE NoMonomorphismRestriction #-}, . , , .

+3

Source: https://habr.com/ru/post/1674202/


All Articles