[Haskell-cafe] Cannot understand liftM2

Daniel McAllansmith dm.maillists at gmail.com
Mon Dec 11 15:37:04 EST 2006


On Tuesday 12 December 2006 08:57, Nicola Paolucci wrote:
> - How do I know - or how does the interpreter know - that the "m" of
> this example is an instance of type ((->) e) ?
> - Is it always like that for liftM2 ? Or is it like that only because
> I used the function (-) ?

It's the snd that forces the interpreter to infer the ((->) e) monad.

You can guess from the type of liftM2 that the (-) won't supply any more 
information/constraints about m because m is is only mentioned in the snd and 
fst parts.

If you use different monadic values, instead of snd and fst, then the m will 
end up constrained to a different monad

Try these commands in GHCi to see what happens if you use something in the 
Maybe monad:

Prelude> :m + Control.Monad

Prelude Control.Monad> :t liftM2
liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r

Prelude Control.Monad> :t liftM2 (-)
liftM2 (-) :: (Num a1, Monad m) => m a1 -> m a1 -> m a1

Prelude Control.Monad> :t liftM2 (-) (Just 5)
liftM2 (-) (Just 5) :: (Num a1) => Maybe a1 -> Maybe a1

Prelude Control.Monad> :t liftM2 (-) (Just 5) Nothing
liftM2 (-) (Just 5) Nothing :: (Num a1) => Maybe a1

Prelude Control.Monad> liftM2 (-) (Just 5) Nothing
Nothing


Daniel


More information about the Haskell-Cafe mailing list