{"id":931,"date":"2014-08-13T22:36:28","date_gmt":"2014-08-14T05:36:28","guid":{"rendered":"http:\/\/www.elbeno.com\/blog\/?p=931"},"modified":"2015-06-30T21:18:12","modified_gmt":"2015-07-01T04:18:12","slug":"do-notation-can-be-misleading","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/blog\/?p=931","title":{"rendered":"Do-notation can be misleading"},"content":{"rendered":"<p>Consider the following function:<\/p>\n<pre lang=\"haskell\">\r\noddness :: Maybe Int\r\noddness = do\r\n  let a = Just 1 :: Maybe Int\r\n  b <- a\r\n  return b\r\n<\/pre>\n<p>Perfectly fine, albeit contrived, redundant, etc. Bear with me. Now consider what happens if we change the value of <code>a<\/code>:<\/p>\n<pre lang=\"haskell\">\r\noddness :: Maybe Int\r\noddness = do\r\n  let a = Nothing :: Maybe Int\r\n  b <- a\r\n  return b\r\n<\/pre>\n<p>This looks odd, because it looks like we're extracting the value from <code>a<\/code> into <code>b<\/code>, and then passing it to <code>return<\/code> - it looks like there's some <code>Int<\/code> we extract from <code>Nothing<\/code>, and calling <code>return<\/code> converts it back to <code>Nothing<\/code>.<\/p>\n<p>But of course, we know that this do-notation desugars to something like:<\/p>\n<pre lang=\"haskell\">\r\noddness :: Maybe Int\r\noddness =\r\n  let a = Nothing :: Maybe Int\r\n  in a >>= \\b -> return b\r\n<\/pre>\n<p>And recall the definition of <code>(>>=)<\/code> for <code>Maybe<\/code>:<\/p>\n<pre lang=\"haskell\">\r\ninstance  Monad Maybe  where\r\n    (Just x) >>= k      = k x\r\n    Nothing  >>= _      = Nothing\r\n<\/pre>\n<p>So what's happening here is that what's on the right of <code>(>>=)<\/code> remains unevaluated, and <code>Nothing<\/code> is the result. Mystery solved.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider the following function: oddness :: Maybe Int oddness = do let a = Just 1 :: Maybe Int b >= \\b -> return b And recall the definition of (>>=) for Maybe: instance Monad Maybe where (Just x) >>= k = k x Nothing >>= _ = Nothing So&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8,1],"tags":[],"class_list":["post-931","post","type-post","status-publish","format-standard","hentry","category-haskell","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=931"}],"version-history":[{"count":5,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/931\/revisions"}],"predecessor-version":[{"id":936,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/931\/revisions\/936"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}