move do-notation links
[lambda.git] / topics / week7_introducing_monads.mdwn
index 840521a..b1782d8 100644 (file)
@@ -1,5 +1,4 @@
 <!-- λ Λ ∀ ≡ α β γ ρ ω Ω ○ μ η δ ζ ξ ⋆ ★ • ∙ ● 𝟎 𝟏 𝟐 𝟘 𝟙 𝟚 𝟬 𝟭 𝟮 -->
-<!-- Loved this one: http://www.stephendiehl.com/posts/monads.html -->
 
 
 The [[tradition in the functional programming
@@ -98,7 +97,7 @@ Here are some examples of values of these Kleisli arrow types, where the box typ
 \x. prime_factors_of x
 \x. [0, 0, 0]</pre>
 
-As semanticists, you are no doubt familiar with the debates between those who insist that propositions are sets of worlds and those who insist they are context change potentials. We hope to show you, in coming weeks, that propositions are (certain sorts of) Kleisli arrows. But this doesn't really compete with the other proposals; it is a generalization of them. Both of the other proposed structures can be construed as specific Kleisli arrow types.
+As semanticists, you are no doubt familiar with the debates between those who insist that propositions are sets of worlds and those who insist they are context change potentials. We hope to show you, in coming weeks, that propositions are (certain sorts of) Kleisli arrows. But this [doesn't really compete](/images/faye_dunaway.jpg) with the other proposals; it is a generalization of them. Both of the other proposed structures can be construed as specific Kleisli arrow types.
 
 
 ## A family of functions for each box type ##
@@ -209,7 +208,7 @@ has to obey the following Map Laws:
         k >=> mid == k
         mid >=> k == k
 
-    If you studied algebra, you'll remember that a mon*oid* is a universe with some associative operation that has an identity. For example, the natural numbers form a monoid with multiplication as the operation and `1` as the identity, or with addition as the operation and `0` as the identity. Strings form a monoid with concatenation as the operation and the empty string as the identity. (This example shows that the operation need not be commutative.) Monads are a kind of generalization of this notion, and that's why they're named as they are. The key difference is that for monads, the values being operated on need not be of the same type. They *can* be, if they're all Kleisli arrows of a single type <code>P -> <u>P</u></code>. But they needn't be. Their types only need to "cohere" in certain ways.
+    If you studied algebra, you'll remember that a mon*oid* is a universe with some associative operation that has an identity. For example, the natural numbers form a monoid with multiplication as the operation and `1` as the identity, or with addition as the operation and `0` as the identity. Strings form a monoid with concatenation as the operation and the empty string as the identity. (This example shows that the operation need not be commutative.) Monads are a kind of generalization of this notion, and that's why they're named as they are. The key difference is that for monads, the values being operated on need not be of the same type. They *can* be, if they're all Kleisli arrows of a single type <code>P -> <u>P</u></code>. But they needn't be. Their types only need to "cohere" in the sense that the output type of the one arrow is a boxing of the input type of the next.
 
     In the Haskell manuals, they express the Monad Laws using `>>=` instead of the composition operators `>=>` or `<=<`. This looks similar, but doesn't have the same symmetry:
 
@@ -383,7 +382,7 @@ That can be helpful, but it only enables us to have _zero or one_ elements in th
       | [] -> []
       | x' :: xs' -> List.append (k x') (catmap f xs')
 
-Now we can have as many elements in the result for a given `α` as `k` cares to return. Another way to write `catmap k xs` is as `List.concat (map k xs)`. And this is just the definition of `mbind` or `>>=` for the List Monad. The definition of `mcomp` or `<=<`, that we gave above, differs only in that it's the way to compose two functions `j` and `k`, that you'd want to `catmap`, rather than the way to `catmap` one of those functions over a value that's already a list.
+Now we can have as many elements in the result for a given `α` as `k` cares to return. Another way to write `catmap k xs` is as (Haskell) `concat (map k cs)` or (OCaml) `List.flatten (List.map k xs)`. And this is just the definition of `mbind` or `>>=` for the List Monad. The definition of `mcomp` or `<=<`, that we gave above, differs only in that it's the way to compose two functions `j` and `k`, that you'd want to `catmap`, rather than the way to `catmap` one of those functions over a value that's already a list.
 
 This example is a good intuitive basis for thinking about the notions of `mbind` and `mcomp` more generally. Thus `mbind` for the option/Maybe type takes an option value, applies `k` to its element (if there is one), and returns the resulting option value. `mbind` for a tree with `α`-labeled leaves would apply `k` to each of the leaves, and return a tree containing arbitrarily large subtrees in place of all its former leaves, depending on what `k` returned.
 
@@ -392,6 +391,7 @@ This example is a good intuitive basis for thinking about the notions of `mbind`
 
       Some a  >>=<sub>α option</sub>  (\a -> Some 0) ==> Some 0
       None    >>=<sub>α option</sub>  (\a -> Some 0) ==> None
+      Some a  >>=<sub>α option</sub>  (\a -> None  ) ==> None
 
                                                          .
                                                         / \
@@ -437,7 +437,9 @@ As we mentioned above, the notions of Monads have their origin in Category Theor
 [1](http://en.wikipedia.org/wiki/Outline_of_category_theory)
 [2](http://lambda1.jimpryor.net/advanced_topics/monads_in_category_theory/)
 [3](http://en.wikibooks.org/wiki/Haskell/Category_theory)
-[4](https://wiki.haskell.org/Category_theory), where you should follow the further links discussing Functors, Natural Transformations, and Monads.
+[4](https://wiki.haskell.org/Category_theory) <small>(where you should follow the further links discussing Functors, Natural Transformations, and Monads)</small>
+[5](http://www.stephendiehl.com/posts/monads.html)
+
 
 Here are some papers that introduced Monads into functional programming:
 
@@ -463,7 +465,5 @@ Here is some other reading:
 *      [Haskell wikibook on Understanding Monads](http://en.wikibooks.org/wiki/Haskell/Understanding_monads)
 *      [Haskell wikibook on Advanced Monads](http://en.wikibooks.org/wiki/Haskell/Advanced_monads)
 *      [Haskell wiki on Monad Laws](http://www.haskell.org/haskellwiki/Monad_laws)
-*      [Haskell wikibook on do-notation](http://en.wikibooks.org/wiki/Haskell/do_Notation)
-*      [Yet Another Haskell Tutorial on do-notation](http://en.wikibooks.org/wiki/Haskell/YAHT/Monads#Do_Notation)
 
 There's a long list of monad tutorials linked at the [[Haskell wiki|https://wiki.haskell.org/Monad_tutorials_timeline]] (we linked to this at the top of the page), and on our own [[Offsite Reading]] page. (Skimming the titles is somewhat amusing.) If you are confused by monads, make use of these resources. Read around until you find a tutorial pitched at a level that's helpful for you.