{"id":610,"date":"2008-03-11T10:57:26","date_gmt":"2008-03-11T10:57:26","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2008\/03\/11\/meta-out-the-wazoo-monads-and-monoids\/"},"modified":"2008-03-11T10:57:26","modified_gmt":"2008-03-11T10:57:26","slug":"meta-out-the-wazoo-monads-and-monoids","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2008\/03\/11\/meta-out-the-wazoo-monads-and-monoids\/","title":{"rendered":"Meta out the wazoo: Monads and Monoids"},"content":{"rendered":"<p> Since I mentioned the idea of monoids as a formal models of computations, <a href=\"http:\/\/scientopia.org\/blogs\/goodmath\/2008\/02\/full-circle-the-categorical-monoid#comment-768378\">John Armstrong made the natural leap ahead<\/a>, to the connection between monoids and monads &#8211; which are a common feature in programming language semantics, and a prominent language feature in <a href=\"http:\/\/scienceblogs.com\/goodmath\/goodmath\/programming\/haskell\/\">Haskell<\/a>, one of my favorite programming languages.<\/p>\n<p><!--more--><\/p>\n<p> Monads are a category theoretic construction. If you take a monoid, and use some of the constructions we&#8217;ve seen in the last few posts, we can move build a meta-monoid; that is, a monoid that&#8217;s built from monoid-to-monoid mappings &#8211; essentially, the category of small categories. (Small categories are categories whose collection of objects are a set, not a proper class.)<\/p>\n<p> We&#8217;re going to look at constructs built using objects in that category. But first (as usual), we need to come up with a bit of notation. Suppose we have a category, C. In the category of categories, there&#8217;s an <em>identity morphism<\/em> (which is also a functor) from C to C. We&#8217;ll call that 1<sub>C<\/sub>. And given any functor from T:C&rarr;C (that is, from C to itself), we&#8217;ll say that <em>exponents<\/em> of that functor are formed by self-compositions of T: T<sup>2<\/sup>=T&ordm;T; T<sup>3<\/sup>=T&ordm;T&ordm;T, etc. Finally, given a functor T, there&#8217;s a natural transformation from T to T, which we&#8217;ll call 1<sub>T<\/sub>.<\/p>\n<p> So, now, as I said, a monad is a construct in this category of category &#8211; that is, a particular category with some additional structure built around it. Given a category, C, a monad on C consists of three parts:<\/p>\n<ul>\n<li> T:C&rarr;C, a functor from C to itself.<\/li>\n<li> A natural transformation, &eta;:1<sub>C<\/sub>&rarr;T<\/li>\n<li> A natural transformation &mu;:T<sup>2<\/sup>&rarr;T<\/li>\n<\/ul>\n<p> C, T, &eta;, and &mu; must satisfy some <em>coherence conditions<\/em>, which basically mean that they must make the following two diagrams commute. First, we show a requirement that in terms of natural transformations, &mu; is commutative in how it maps T<sup>2<\/sup> to T:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"monad-prop1.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_300.jpg?resize=157%2C117\" width=\"157\" height=\"117\" class=\"inset\" \/><\/p>\n<p> And then, a commutativity requirement on &mu; and &eta; with respect to T (basically making &mu; and &eta; into a meta-identity for this meta-monoid):<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"monad-prop2.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_301.jpg?resize=236%2C170\" width=\"236\" height=\"170\" class=\"inset\" \/><\/p>\n<p> &mu; and &eta; basically play the role of turning C into a meta-meta-monoid. A monoid is basically a category; then we play with it, and construct the category of categories &#8211; the first meta-monoid. Now we&#8217;re taking a self-functor of the meta-monoid, and and using natural transformations to build a new meta-meta-monoid around it.<\/p>\n<p> One of the key things to notice here is that we&#8217;re building a monoid whose objects are, basically, functions from monoids to monoids. We&#8217;ve gone meta out the wazoo &#8211; but it&#8217;s given us something really interesting.<\/p>\n<p> We start with the category. From the category, we get the functor &#8211; a structure preserving map from the category to itself. The monad focuses on the functor &#8211; the transition from C to C: using natural transformations, it defines an equivalence &#8211; not an equality, but an equivalence &#8211; between multiple applications of the functor and a single application.<\/p>\n<p> In terms of programming languages, we can think of C as a <em>state<\/em>. An application of the functor T is an <em>action<\/em> &#8211; that is, a mapping from state to state. What the monad does is provide a structure for composing actions. We don&#8217;t need to write the state &#8211; it&#8217;s implicit in the definition of the functor\/action. The monad says that if we have an action &#8220;X&#8221; and an action &#8220;Y&#8221;, we can compose them into an action &#8220;X followed by Y&#8221;. What the natural transformation says is that &#8220;X followed by Y&#8221; is an action &#8211; we can compose sequences of actions, and the result is always an action &#8211; which we can compose further, producing other compound actions. <\/p>\n<p> So at the bottom, we have functions that are state-to-state transformers. But we don&#8217;t really need to think much about the complexity of a state-to-state transition. What we can do instead is provide a collection of primitive actions &#8211; which are themselves written as state-to-state transitions &#8211; and then use those primitives to build imperative code &#8211; which remains completely functional under the covers, and yet has all of the properties that we would want from an imperative programming system &#8211; ordering, updatable state, etc.<\/p>\n<p> Below is a really simple piece of Haskell code using the IO monad. What the monad does is play with IO states. The category is the set of IO states. Each action is a transformation from state to state. The state is <em>invisible<\/em> &#8212; it&#8217;s created at the beginning of the &#8220;do&#8221;, and each subsequent statement is implicitly converted to a state transition function. <\/p>\n<pre>\nhello :: IO ()\nhello =\n do\n   print \"What is your name?\"\n   x &lt;- getLine\n   print (concat [\"Hello\", x])\n<\/pre>\n<p> So in the code above, &#8220;<code>print \"What is your name\"<\/code>&#8221;\tis an action from an IO state to an IO state. It&#8217;s composed with <code>x &lt;- getLine<\/code> &#8211; which is, implicitly, another transition from an IO state to an IO state, which includes an implicit variable definition; and that&#8217;s composed with the finat &#8220;<code>print<\/code>&#8220;. The actions are sequenced &#8211; they occur in the correct order, and each passes its result state to next action. The monad lets us program completely in terms of the actions, without worrying about how to pass the states.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I mentioned the idea of monoids as a formal models of computations, John Armstrong made the natural leap ahead, to the connection between monoids and monads &#8211; which are a common feature in programming language semantics, and a prominent language feature in Haskell, one of my favorite programming languages.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[69,76,89,54],"tags":[],"class_list":["post-610","post","type-post","status-publish","format-standard","hentry","category-abstract-algebra","category-category-theory","category-haskell","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-9Q","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=610"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/610\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}