{"id":158,"date":"2008-02-05T23:42:18","date_gmt":"2008-02-05T22:42:18","guid":{"rendered":"http:\/\/www.nobugs.org\/blog\/archives\/2008\/02\/05\/form_-vs-form_\/"},"modified":"2009-01-11T01:03:31","modified_gmt":"2009-01-11T00:03:31","slug":"form_-vs-form_","status":"publish","type":"post","link":"https:\/\/www.nobugs.org\/blog\/archives\/2008\/02\/05\/form_-vs-form_\/","title":{"rendered":"Polymorphic function; a tale of two forM_&#8217;s"},"content":{"rendered":"<p>Tonight I spent over an hour learning that there are two functions called &#8220;forM_&#8221; in haskell.  <\/p>\n<p>The first, which I was already familiar with, lives in <a href=\"http:\/\/www.haskell.org\/ghc\/docs\/latest\/html\/libraries\/base\/Control-Monad.html\">Control.Monad<\/a>.  Its purpose is to take a list of values, apply some function to them all to produce a list of actions, and then run these actions one after the other.<\/p>\n<p>The other one, which I did not know about, lives in <a href=\"http:\/\/www.haskell.org\/ghc\/docs\/latest\/html\/libraries\/base\/Data-Foldable.html\">Data.Foldable<\/a>.  It does exactly the same job, but it works on any &#8220;Foldable&#8221; data type; ie. those which can be collapsed\/folded in some way to a single summary value.  Lists are foldable.  <a href=\"http:\/\/www.haskell.org\/ghc\/docs\/latest\/html\/libraries\/containers\/Data-Map.html\">Maps<\/a> are also foldable (across their elements, not their keys).<\/p>\n<p>And this was the cause of my perplexification this evening; I had been storing my data in a list, and using forM_ (the monadic one, the only which I knew about!) to process it.  At some point, I decided to change to storing my data in a map instead, and ran the compiler to see the errors which would point me to the bits of code I needed to fix up. <\/p>\n<p>Except the compiler happily compiled everything without errors.  *confusion*<\/p>\n<p>Much headscratching followed, until eventually I added &#8220;-ddump-tc&#8221; to get ghc to dump out the results of its typechecking pass.  This lead me to the root cause:  I had been picking up forM_ from Data.Foldable all along, not Control.Monad.  Duh!  Everything had been working fine up until then, since Foldable.forM_ and Monad.forM_ operate identically on lists.  But the former also works on Data.Maps whereas the latter does not.<\/p>\n<p>I&#8217;m sure there&#8217;s a moral to this story.  I&#8217;m just not sure what it is yet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tonight I spent over an hour learning that there are two functions called &#8220;forM_&#8221; in haskell. The first, which I was already familiar with, lives in Control.Monad. Its purpose is to take a list of values, apply some function to them all to produce a list of actions, and then run these actions one after [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[9],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-programming","tag-haskell"],"_links":{"self":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":1,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":252,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions\/252"}],"wp:attachment":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}