[Haskell-cafe] What is MonadPlus good for?

David Roundy droundy at abridgegame.org
Sun Feb 13 08:58:29 EST 2005


On Sat, Feb 12, 2005 at 01:08:59PM -0500, Benjamin Pierce wrote:
> I have seen lots of examples that show how it's useful to make some type
> constructor into an instance of Monad.
> 
> Where can I find examples showing why it's good to take the trouble to show
> that something is also a MonadPlus?  (I know there are many examples of
> things that *are* MonadPluses; what I want to know is why this is
> interesting. :-)

I've been working on a typeclass that derives from MonadPlus which will
encapsulate certain kinds of IO.  With MonadPlus, you can write monadic
code with exceptions and everything that may not be executed in the IO
monad.  You just use fail to throw exceptions, and mplus to catch them.

class MonadPlus m => ReadableDirectory m where
    mInCurrentDirectory :: FilePath -> m a -> m a
    mGetDirectoryContents :: m [FilePath]
    mReadFilePS :: FilePath -> m PackedString
    mReadFilePSs :: FilePath -> m [PackedString]
    mReadFilePSs f = linesPS `liftM` mReadFilePS f

One instance of this class is IO, but I can also have instances for
in-memory data structures (outside the IO monad) or (or example) for
reading a tarball from disk--which would be a monad that acts within the IO
monad.
-- 
David Roundy
http://www.darcs.net


More information about the Haskell-Cafe mailing list