[Haskell-beginners] How would you run a monad within another monad?

Arthur Chan baguasquirrel at gmail.com
Tue Apr 14 17:05:22 EDT 2009

Here's my contrived example that threw the error.

If you go into ghci, and do a `:t (foo' "blah" myDoohickey)`, you will get
the type signature "IO ()".
Doing the same for myOtherDoohickey returns "IO True"

So you would think that you'd be able to uncomment the code that makes IO an
instance of Toplevel.  foo' is a function that allows IO to run monadic
values of type Doohickey.  But it doesn't work.


import IO
import Control.Monad.Reader

class (Monad n) => Doohickey n where
    putRecord :: String -> n ()

class (Monad m) => Toplevel m where
    foo :: (Doohickey n) => FilePath -> n a -> m a

newtype IOToplevelT a = IOToplevelT { runIOToplevelT :: ReaderT Handle IO a
} deriving (Monad, MonadReader Handle, MonadIO)

instance Doohickey IOToplevelT where
    putRecord = liftIO . putStrLn

foo' s k = do
  f <- liftIO $ openFile s AppendMode
  runReaderT (runIOToplevelT k) f

--instance Toplevel IO where
--  foo = foo'

myDoohickey = do
  putRecord "foo"
  putRecord "bar"

myOtherDoohickey = do
  putRecord "hello"
  putRecord "world"
  return True

On Mon, Apr 13, 2009 at 7:55 PM, Jason Dusek <jason.dusek at gmail.com> wrote:

>  Copypasting and loading your code doesn't throw an error. Please,
>  pastebin an example that demonstrates the error.
> --
> Jason Dusek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090414/0c3d6d92/attachment.htm

More information about the Beginners mailing list