2

Just trying to get my head round monads...

looking at this page at the moment: http://www.haskell.org/haskellwiki/Simple_monad_examples

at the bottom it asks what do these snippets resolve to:

Just 0 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) )

Why does this return Nothing? Because of the fail call?

Nothing >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) )

I understand this one.

asked May 6, 2012 at 19:38
1
  • Thanks all for confirming and explanation :) Commented May 6, 2012 at 20:02

3 Answers 3

8

As always in Haskell, you can usually understand some code by inlining and term rewriting:

We have:

Prelude> Just 0 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) )
Nothing

The most important thing we need is the definition of fail and >>= for the Maybe monad, given as:

instance Monad Maybe where
 (Just x) >>= k = k x
 Nothing >>= _ = Nothing
 (Just _) >> k = k
 Nothing >> _ = Nothing
 return = Just
 fail _ = Nothing

so we have:

Just 0 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) )
-- by definition of >>=
(\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) 0
-- by definition of fail
(\ x -> if (x == 0) then Nothing else Just (x + 1) ) 0
-- beta reduce
if 0 == 0 then Nothing else Just (0 + 1)
-- Integer math
if True then Nothing else Just 1
-- evaluate `if`
Nothing

and there you have it.

answered May 6, 2012 at 19:53
Sign up to request clarification or add additional context in comments.

Comments

4

The behavior of fail depends on the monad. In the Maybe monad, fail returns Nothing.

instance Monad Maybe where
 return = Just
 (Just x) >>= k = k x
 Nothing >>= _ = Nothing
 fail _ = Nothing

However, in many other monads fail translates to error, since that is the default implementation. The monads which provide their own fail are usually the ones in the MonadPlus class, where you can have fail return mzero, which is Nothing in the Maybe monad.

In practice, I don't recommend using fail since it's so unclear what it will do. Instead, use the appropriate failure mechanism of the monad you're in, whether that's mzero, throwError or something else.

answered May 6, 2012 at 19:48

Comments

2

Yes, because of the fail call. Look at how Maybe is an instance of the Monad typeclass:

http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/Data-Maybe.html#Maybe

fail _ = Nothing
answered May 6, 2012 at 19:44

1 Comment

n.b. you can find this by hoogling Maybe and following the correct hyperlink to the docs, and then clicking the "source" link on that page, usually in the upper-right hand corner.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.