[Haskell] A collection of related proposals regarding monads

Cale Gibbard cgibbard at gmail.com
Wed Jan 4 17:34:56 EST 2006

On 04/01/06, ajb at spamcop.net <ajb at spamcop.net> wrote:
> G'day Cale.
> Quoting Cale Gibbard <cgibbard at gmail.com>:
> > I personally feel that the inclusion of 'fail' in the Monad class is
> > an ugly solution to the problem of pattern matching, and gives the
> > incorrect impression that monads should have some builtin notion of
> > failure.
> So do I.
> > We ought to be using MonadZero when we want to express failure, but it's
> > gone!
> I agree!
> > What do people think of the following proposal?
> I don't like it.
> My feeling is that do { p <- xs; return e } should behave identically
> (modulo the precise error message if the pattern match fails) to
> map (\p -> e) xs.  Your proposal would make it into a map/filter
> hybrid.
> Speaking more practically, if a pattern match is technically refutable,
> but I, as the programmer, intend it never to fail, then I shouldn't need
> to use MonadZero.
> I don't like fail either, but I must admit that I like allowing refutable
> patterns.
Okay, so how about we separate these?

do {p <- e; stmts} = e >>= \p -> do {stmts}
do {p <-: e; stmts} = let ok p = do {stmts}; ok _ = mzero in e >>= ok
(note the new symbol for monadically failing pattern match)

This allows the two meanings to be clearly separated, and only the
latter of the two will create a dependency on MonadZero, and it will
do so consistently.

 - Cale

More information about the Haskell mailing list