
Hello! I see from http://www.haskell.org/haskellwiki/Monads_as_computation#Do_notation that do { v <- x ; <stmts> } = x >>= \v -> do { <stmts> } However, look at this GHCi session: Prelude> let return' = return :: a -> Maybe a Prelude> do {1 <- return 1; return' "ok"} Just "ok" Prelude> return 1 >>= \1 -> return' "ok" Just "ok" Prelude> do {1 <- return 3; return' "ok"} Nothing Prelude> return 3 >>= \1 -> return' "ok" *** Exception: <interactive>:1:13-30: Non-exhaustive patterns in lambda Hmmm... let's try with IO: Prelude> let return' = return :: a -> IO a Prelude> do {1 <- return 1; return' "ok"} "ok" Prelude> return 1 >>= \1 -> return' "ok" "ok" Prelude> do {1 <- return 3; return' "ok"} *** Exception: user error (Pattern match failure in do expression at <interactive>:1:4) Prelude> return 3 >>= \1 -> return' "ok" *** Exception: <interactive>:1:13-30: Non-exhaustive patterns in lambda Oh! What about lists? Prelude> let return' = return :: a -> [a] Prelude> do {1 <- return 1; return' "ok"} ["ok"] Prelude> return 1 >>= \1 -> return' "ok" ["ok"] Prelude> do {1 <- return 3; return' "ok"} [] Prelude> return 3 >>= \1 -> return' "ok" *** Exception: <interactive>:1:13-30: Non-exhaustive patterns in lambda Something seems wrong to me here. What am I missing? Thanks! -- Felipe.

On Sat, Dec 08, 2007 at 02:59:16PM -0200, Felipe Lessa wrote:
Hello!
I see from http://www.haskell.org/haskellwiki/Monads_as_computation#Do_notation that
do { v <- x ; <stmts> } = x >>= \v -> do { <stmts> }
Something seems wrong to me here. What am I missing?
That rule only applies when v is an irrefutable pattern. The Haskell Report lists the full rule. Stefan

On Sat, Dec 08, 2007 at 02:59:16PM -0200, Felipe Lessa wrote:
Hello!
I see from http://www.haskell.org/haskellwiki/Monads_as_computation#Do_notation that
do { v <- x ; <stmts> } = x >>= \v -> do { <stmts> }
However, look at this GHCi session:
Prelude> let return' = return :: a -> Maybe a Prelude> do {1 <- return 1; return' "ok"} Just "ok" Prelude> return 1 >>= \1 -> return' "ok" Just "ok" Prelude> do {1 <- return 3; return' "ok"} Nothing Prelude> return 3 >>= \1 -> return' "ok" *** Exception: <interactive>:1:13-30: Non-exhaustive patterns in lambda What seems confusing to you?
\1 -> foo is the same as \x -> case x of {1 -> foo;} When this function is evaluated with parameter different from 1, Haskell fails to find matching pattern for x and exception occurs.

On Dec 8, 2007 3:12 PM, Ilya Tsindlekht
On Sat, Dec 08, 2007 at 02:59:16PM -0200, Felipe Lessa wrote:
Prelude> do {1 <- return 3; return' "ok"} Nothing Prelude> return 3 >>= \1 -> return' "ok" *** Exception: <interactive>:1:13-30: Non-exhaustive patterns in lambda What seems confusing to you?
\1 -> foo is the same as \x -> case x of {1 -> foo;}
When this function is evaluated with parameter different from 1, Haskell fails to find matching pattern for x and exception occurs.
The problem is that with the do notation it doesn't raise an exception. In the example you quoted, do {1 <- return 3; (return "ok" :: Maybe String)} == Nothing =/= _|_ The report says (http://www.haskell.org/onlinereport/exps.html#sect3.14) that the do above is translated to let ok 1 = return 3 >> (return "ok" :: Maybe String); ok _ = fail "..." in return 3 >>= ok --> let ok 1 = Just 3 >> Just "ok"; ok _ = Nothing in Just 3 >>= ok --> case Just 3 of {Nothing -> Nothing; Just x -> case x of {1 -> Just 3 >> Just "ok"; _ -> Nothing}} --> case 3 of {1 -> Just 3 >> Just "ok"; _ -> Nothing}} --> Nothing and not as return 3 >>= \1 -> (return "ok" :: Maybe String) --> _|_ as I initially thought. -- Felipe.

On Dec 8, 2007 3:12 PM, Ilya Tsindlekht
wrote: On Sat, Dec 08, 2007 at 02:59:16PM -0200, Felipe Lessa wrote:
Prelude> do {1 <- return 3; return' "ok"} Nothing Prelude> return 3 >>= \1 -> return' "ok" *** Exception: <interactive>:1:13-30: Non-exhaustive patterns in lambda What seems confusing to you?
\1 -> foo is the same as \x -> case x of {1 -> foo;}
When this function is evaluated with parameter different from 1, Haskell fails to find matching pattern for x and exception occurs.
The problem is that with the do notation it doesn't raise an exception. In the example you quoted, Yes, I have already understood it from your reply to the original
On Sat, Dec 08, 2007 at 03:28:58PM -0200, Felipe Lessa wrote: poster.
participants (3)
-
Felipe Lessa
-
Ilya Tsindlekht
-
Stefan O'Rear