[Haskell-cafe] What monad am I in?

Marc Weber marco-oweber at gmx.de
Tue Sep 2 20:00:34 EDT 2008


> Have I, like Monsier Jourdain, been running in the IO monad all my
> life, and didn't even know it?
Sure, just try
readFile "doesnotexist" within ghci :-)
That's an IO action.
on the other side
ghci > (3+7)
10
is no IO action. So I think ghci has two default behaviours differing.
Either its a monad, than use IO, else evaluate the result. In both cases
show it. The ghc manual sould tell you all about this (too lazy to look
it up)


But the ghci error message is another one:
Try this:
 :set -XNoMonomorphismRestriction
 let increment x = return (x+1)
 let a = increment 1

the line let a = requires to find out about the type of m (Maybe any
Monad such as Maybe, list etc) without XNoMonomorphismRestriction.
With XNoMonomorphismRestriction you can tell ghc that you don't care yet
about this and it should try to resolve m later. That's why ghci shows
this:

ghci> :t a
a :: (Num t, Monad m) => m t

The second way to get rid of the ghci error message is telling ghci
which monad you want:
increment 1 -- this works because it's run within IO
print $ increment 1 -- won't, because ghc does'n know about the m type
print $ fromJust $ increment 1 -- works again, because you tell ghc that m is Maybe here 
print $ (increment 1 :: [Int]) -- works as well, using list monad
...

Sincerly
Marc Weber


More information about the Haskell-Cafe mailing list