[Haskell-cafe] What is MonadPlus good for?
k.schupke at imperial.ac.uk
Mon Feb 14 07:33:09 EST 2005
Technically this is a use of MonadError, not MonadPlus (see earlier
discussion about how IO is _not_ an instance of MonadPlus).
David Roundy wrote:
>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
>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
More information about the Haskell-Cafe