Moreover, with `map2` in hand, `map3`, `map4`, ... `mapN` are easily definable.) These
have to obey the following MapN Laws:
- 1. <code>⇧ (id : P->P) : <u>P</u> -> <u>P</u></code> is a left identity for `¢`, that is: `(⇧ id) ¢ xs = xs`
- 2. `⇧ (f a) = (⇧ f) ¢ (⇧ a)`
- 3. The `map2`ing of composition onto boxes `fs` and `gs` of functions, when `¢`'d to a box `xs` of arguments == the `¢`ing of `fs` to the `¢`ing of `gs` to xs: `(⇧ (○) ¢ fs ¢ gs) ¢ xs = fs ¢ (gs ¢ xs)`.
- 4. When the arguments (the right-hand operand of `¢`) are an `⇧`'d value, the order of `¢`ing doesn't matter: `fs ¢ (⇧ x) = ⇧ ($x) ¢ fs`. (Though note that it's `⇧ ($x)`, or `⇧ (\f. f x)` that gets `¢`d onto `fs`, not the original `⇧ x`.) Here's an example where the order *does* matter: `[succ,pred] ¢ [1,2] == [2,3,0,1]`, but `[($1),($2)] ¢ [succ,pred] == [2,0,3,1]`. This Law states a class of cases where the order is guaranteed not to matter.
- 5. A consequence of the laws already stated is that when the _left_-hand operand of `¢` is a `⇧`'d value, the order of `¢`ing doesn't matter either: `⇧ f ¢ xs == map (flip ($)) xs ¢ ⇧ f`.
+ 1. <code>⇧(id : P->P) : <u>P</u> -> <u>P</u></code> is a left identity for `¢`, that is: `(⇧id) ¢ xs = xs`
+ 2. `⇧(f a) = (⇧f) ¢ (⇧a)`
+ 3. The `map2`ing of composition onto boxes `fs` and `gs` of functions, when `¢`'d to a box `xs` of arguments == the `¢`ing of `fs` to the `¢`ing of `gs` to xs: `(⇧(○) ¢ fs ¢ gs) ¢ xs = fs ¢ (gs ¢ xs)`.
+ 4. When the arguments (the right-hand operand of `¢`) are an `⇧`'d value, the order of `¢`ing doesn't matter: `fs ¢ (⇧x) = ⇧($x) ¢ fs`. (Though note that it's `⇧($x)`, or `⇧(\f. f x)` that gets `¢`d onto `fs`, not the original `⇧x`.) Here's an example where the order *does* matter: `[succ,pred] ¢ [1,2] == [2,3,0,1]`, but `[($1),($2)] ¢ [succ,pred] == [2,0,3,1]`. This Law states a class of cases where the order is guaranteed not to matter.
+ 5. A consequence of the laws already stated is that when the _left_-hand operand of `¢` is a `⇧`'d value, the order of `¢`ing doesn't matter either: `⇧f ¢ xs == map (flip ($)) xs ¢ ⇧f`.
<!-- Probably there's a shorter proof, but:
- ⇧ T ¢ xs ¢ ⇧ f
-== ⇧ T ¢ ((⇧ id) ¢ xs) ¢ ⇧ f, by 1
-== ⇧ (○) ¢ ⇧ T ¢ ⇧ id ¢ xs ¢ ⇧ f, by 3
-== ⇧ ($id) ¢ (⇧ (○) ¢ ⇧ T) ¢ xs ¢ ⇧ f, by 4
-== ⇧ (○) ¢ ⇧ ($id) ¢ ⇧ (○) ¢ ⇧ T ¢ xs ¢ ⇧ f, by 3
-== ⇧ ((○) ($id)) ¢ ⇧ (○) ¢ ⇧ T ¢ xs ¢ ⇧ f, by 2
-== ⇧ ((○) ($id) (○)) ¢ ⇧ T ¢ xs ¢ ⇧ f, by 2
-== ⇧ id ¢ ⇧ T ¢ xs ¢ ⇧ f, by definitions of ○ and $
-== ⇧ T ¢ xs ¢ ⇧ f, by 1
-== ⇧ ($f) ¢ (⇧ T ¢ xs), by 4
-== ⇧ (○) ¢ ⇧ ($f) ¢ ⇧ T ¢ xs, by 3
-== ⇧ ((○) ($f)) ¢ ⇧ T ¢ xs, by 2
-== ⇧ ((○) ($f) T) ¢ xs, by 2
-== ⇧ f ¢ xs, by definitions of ○ and $ and T == flip ($)
+ ⇧T ¢ xs ¢ ⇧f
+== ⇧T ¢ ((⇧id) ¢ xs) ¢ ⇧f, by 1
+== ⇧(○) ¢ ⇧T ¢ ⇧id ¢ xs ¢ ⇧f, by 3
+== ⇧($id) ¢ (⇧(○) ¢ ⇧T) ¢ xs ¢ ⇧f, by 4
+== ⇧(○) ¢ ⇧($id) ¢ ⇧(○) ¢ ⇧T ¢ xs ¢ ⇧f, by 3
+== ⇧((○) ($id)) ¢ ⇧(○) ¢ ⇧T ¢ xs ¢ ⇧f, by 2
+== ⇧((○) ($id) (○)) ¢ ⇧T ¢ xs ¢ ⇧f, by 2
+== ⇧id ¢ ⇧T ¢ xs ¢ ⇧f, by definitions of ○ and $
+== ⇧T ¢ xs ¢ ⇧f, by 1
+== ⇧($f) ¢ (⇧T ¢ xs), by 4
+== ⇧(○) ¢ ⇧($f) ¢ ⇧T ¢ xs, by 3
+== ⇧((○) ($f)) ¢ ⇧T ¢ xs, by 2
+== ⇧((○) ($f) T) ¢ xs, by 2
+== ⇧f ¢ xs, by definitions of ○ and $ and T == flip ($)
-->
* ***Monad*** (or "Composables") A MapNable box type is a *Monad* if there
u >>= (\a -> k a >>= j) == (u >>= k) >>= j
u >>= ⇧ == u
- ⇧ a >>= k == k a
+ ⇧a >>= k == k a
(Also, Haskell calls `⇧` `return` or `pure`, but we've stuck to our terminology in this context.) Some authors try to make the first of those Laws look more symmetrical by writing it as:
u >>= k == (id >=> k) u; or ((\(). u) >=> k) ()
u >>= k == join (map k u)
join w == w >>= id
-map2 f xs ys == xs >>= (\x. ys >>= (\y. ⇧ (f x y)))
+map2 f xs ys == xs >>= (\x. ys >>= (\y. ⇧(f x y)))
map2 f xs ys == (map f xs) ¢ ys, using ¢ as an infix operator
fs ¢ xs == fs >>= (\f. map f xs)
¢ == map2 id
-map f xs == ⇧ f ¢ xs
+map f xs == ⇧f ¢ xs
map f u == u >>= ⇧ ○ f
</pre>
let rec catmap (k : α -> β list) (xs : α list) : β list =
match xs with
| [] -> []
- | x' :: xs' -> List.append (k x') (catmap f xs')
+ | x' :: xs' -> List.append (k x') (catmap k 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 (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.
+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 xs)` 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.
But if on the other hand, your box type is `α -> R`, you'll find that there is no way to define a `map` operation that takes arbitrary functions of type `P -> Q` and values of the boxed type <code><u>P</u></code>, that is `P -> R`, and returns values of the boxed type <code><u>Q</u></code>.
-For the second failure, that is cases of Mappables that are not MapNables, we cited box types like `(R, α)`, for arbitrary fixed types `R`. The `map` operation for these is defined by `map f (r,a) = (r, f a)`. For certain choices of `R` these can be MapNables too. The easiest case is when `R` is the type of `()`. But when we look at the MapNable Laws, we'll see that they impose constraints we cannot satisfy for *every* choice of the fixed type `R`. Here's why. We'll need to define `⇧ a = (r0, a)` for some specific `r0` of type `R`. The choice can't depend on the value of `a`, because `⇧` needs to work for `a`s of _any_ type. Then the MapNable Laws will entail:
+For the second failure, that is cases of Mappables that are not MapNables, we cited box types like `(R, α)`, for arbitrary fixed types `R`. The `map` operation for these is defined by `map f (r,a) = (r, f a)`. For certain choices of `R` these can be MapNables too. The easiest case is when `R` is the type of `()`. But when we look at the MapNable Laws, we'll see that they impose constraints we cannot satisfy for *every* choice of the fixed type `R`. Here's why. We'll need to define `⇧a = (r0, a)` for some specific `r0` of type `R`. The choice can't depend on the value of `a`, because `⇧` needs to work for `a`s of _any_ type. Then the MapNable Laws will entail:
1. (r0,id) ¢ (r,x) == (r,x)
2. (r0,f x) == (r0,f) ¢ (r0,x)
* [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)
-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.
+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|/readings]] 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.