[Haskell-cafe] Newbie Q: Monad 'fail' and 'error'

Mark T.B. Carroll mark at ixod.org
Wed Jun 6 07:06:11 EDT 2007


"Dmitri O.Kondratiev" <dokondr at gmail.com> writes:
(snip)
> 1) What value and type 'error' actually returns in:
> error "some message" ?

For the purpose of type checking, error returns whatever value is
expected for that expression by whatever is 'using' the value. In
practice, 'error' terminates programme execution so that it evades the
trouble of figuring out how to construct a return value of the correct
type.

> 2) How declaration
> String -> m a
> matches with
> String -> a ?

'a' can be any type, including 'm a'. (Those are different 'a's! So 
I should really rename 'm a' to 'm b' when I use them in the same
sentence.)

> 3) In Maybe monad:
> fail = Nothing
>
> When and how 'fail' is used in Maybe monad?

A common way to think of the Maybe monad is that it represents a thing
that works only if all the parts worked. If any of the parts (bound with
>>=) fail, then the Nothing is contagious across bind, as the >>=
doesn't have a value to feed in to the next function as its argument,
and the return value of the whole thing is Nothing.

-- Mark



More information about the Haskell-Cafe mailing list