From a7f15a6a8100c10bde1cf7b97a02660a3d00b9d9 Mon Sep 17 00:00:00 2001 From: jim Date: Sat, 21 Mar 2015 12:03:21 -0400 Subject: [PATCH] warning about mu, some tweaks --- topics/week7_introducing_monads.mdwn | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/topics/week7_introducing_monads.mdwn b/topics/week7_introducing_monads.mdwn index 4488a598..b2f3a135 100644 --- a/topics/week7_introducing_monads.mdwn +++ b/topics/week7_introducing_monads.mdwn @@ -19,7 +19,7 @@ The closest we will come to metaphorical talk is to suggest that monadic types place values inside of *boxes*, and that monads wrap and unwrap boxes to expose or enclose the values inside of them. In any case, our emphasis will be on starting with the abstract structure -of monads, followed by instances of monads from the philosophical and +of monads, followed in coming weeks by instances of monads from the philosophical and linguistics literature. > After you've read this once and are coming back to re-read it to try to digest the details further, the "endofunctors" that slogan is talking about are a combination of our boxes and their associated maps. Their "monoidal" character is captured in the Monad Laws, where a "monoid"---don't confuse with a mon*ad*---is a simpler algebraic notion, meaning a universe with some associative operation that has an identity. For advanced study, here are some further links on the relation between monads as we're working with them and monads as they appear in Category Theory: @@ -74,7 +74,7 @@ A lot of what we'll be doing concerns types that are called *Kleisli arrows*. Do P -> Q -That is, they are functions from values of one type `P` to a boxed type `Q`, for some choice of type expressions `P` and `Q`. +That is, they are functions from values of one type `P` to a boxed type `Q`, for some choice of box and of type expressions `P` and `Q`. For instance, the following are Kleisli arrow types: int -> bool @@ -130,13 +130,13 @@ Here are the types of our crucial functions, together with our pronunciation, an > In Haskell, this is `Control.Monad.<=<`. ->=> (flip mcomp, should we call it mpmoc?): (P -> Q) -> (Q -> R) -> (P -> R) +>=> or flip mcomp : (P -> Q) -> (Q -> R) -> (P -> R) > In Haskell, this is `Control.Monad.>=>`. In the class handout, we gave the types for `>=>` twice, and once was correct but the other was a typo. The above is the correct typing. >>= or mbind : (Q) -> (Q -> R) -> (R) -=<< (flip mbind, should we call it mdnib?) (Q -> R) -> (Q) -> (R) +=<< or flip mbind : (Q -> R) -> (Q) -> (R) join: P -> P @@ -227,6 +227,7 @@ has to obey the following Map Laws: > The first of these says that if you have a triply-boxed type, and you first merge the inner two boxes (with `map join`), and then merge the resulting box with the outermost box, that's the same as if you had first merged the outer two boxes, and then merged the resulting box with the innermost box. The second law says that if you take a box type and wrap a second box around it (with `mid`) and then merge them, that's the same as if you had done nothing, or if you had instead wrapped a second box around each element of the original (with `map mid`, leaving the original box on the outside), and then merged them.

> The Category Theorist would state these Laws like this, where `M` is the endofunctor that takes us from type `α` to type α: >

μ ○ M(μ) == μ ○ μ
μ ○ η == id == μ ○ M(η)
+ > A word of advice: if you're doing any work in this conceptual neighborhood and need a Greek letter, don't use μ. In addition to the preceding usage, there's also a use in recursion theory (for the minimization operator), in type theory (as a fixed point operator for types), and in the λμ-calculus, which is a formal system that deals with _continuations_, which we will focus on later in the course. So μ already exhibits more ambiguity than it can handle. As hinted in last week's homework and explained in class, the operations available in a Mappable system exactly preserve the "structure" of the boxed type they're operating on, and moreover are only sensitive to what content is in the corresponding original position. If you say `map f [1,2,3]`, then what ends up in the first position of the result depends only on how `f` and `1` combine. @@ -345,7 +346,7 @@ For example: `j 7` produced `[49, 14]`, which after being fed through `k` gave us `[49, 50, 14, 15]`. -Contrast that to `m$` (`mapply`), which operates not on two *box-producing functions*, but instead on two *values of a boxed type*, one containing functions to be applied to the values in the other box, via some predefined scheme. Thus: +Contrast that to `m$` (`mapply`), which operates not on two *box-producing functions*, but instead on two *boxed type values*, one containing functions to be applied to the values in the other box, via some predefined scheme. Thus: let js = [(\a->a*a),(\a->a+a)] in let xs = [7, 5] in @@ -354,11 +355,11 @@ Contrast that to `m$` (`mapply`), which operates not on two *box-producing funct The question came up in class of when box types might fail to be Mappable, or Mappables might fail to be MapNables, or MapNables might fail to be Monads. -For the first failure, we noted that it's easy to define a `map` operation for the box type `R -> α`, for a fixed type `R`. You `map` a function of type `P -> Q` over a value of the boxed type P, that is `R -> P`, by just returning a function that takes some `R` as input, first supplies it to your `R -> P` value, and then supplies the result to your `map`ped function of type `P -> Q`. (We will be working with this Mappable extensively; in fact it's not just a Mappable but more specifically a Monad.) +For the first failure, we noted that it's easy to define a `map` operation for the box type `R -> α`, for a fixed type `R`. You `map` a function of type `P -> Q` over a value of the boxed type P, that is a value of type `R -> P`, by just returning a function that takes some `R` as input, first supplies it to your `R -> P` value, and then supplies the result to your `map`ped function of type `P -> Q`. (We will be working with this Mappable extensively; in fact it's not just a Mappable but more specifically a Monad.) 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 P, that is `P -> R`, and returns values of the boxed type Q. -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 `mid a = (r0, a)` for some specific `r0` of type `R`. 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 `mid a = (r0, a)` for some specific `r0` of type `R`. The choice can't depend on the value of `a`, because `mid` needs to work for `a`s of _any_ type. Then the MapNable Laws will entail: 1. (r0,id) m$ (r,x) == (r,x) 2. (r0,f x) == (r0,f) m$ (r0,x) @@ -366,7 +367,7 @@ For the second failure, that is cases of Mappables that are not MapNables, we ci 4. (r'',f) m$ (r0,x) == (r0,($x)) m$ (r'',f) 5. (r0,f) m$ (r,x) == (r,($x)) m$ (r0,f) -Now we are not going to be able to write a `m$` function that inspects the second element of its left-hand operand to check if it's the `id` function; the identity of functions is not decidable. So the only way to satisfy Law 1 will be to use the first element of the right-hand operand (`r`) at least in those cases when the first element of the left-hand operand is `r0`. But then that means that the result of the lhs of Law 5 will also have a first element of `r`; so, turning now to the rhs of Law 5, we see that `m$` must use the first element of its _left_-hand operand (here again `r`) at least in those cases when the first element of its right-hand operand is `r0`. If our `R` type has a natural *monoid* structure, we could just let `r0` be the monoid's identity, and have `m$` combine other `R`s using the monoid's operation. Alternatively, if the `R` type is one that we can safely apply the predicate `(r0==)` to, then we could define `m$` something like this: +Now we are not going to be able to write a `m$` function that inspects the second element of its left-hand operand to check if it's the `id` function; the identity of functions is not decidable. So the only way to satisfy Law 1 will be to have the first element of the result (`r`) be taken from the first element of the right-hand operand in _all_ the cases when the first element of the left-hand operand is `r0`. But then that means that the result of the lhs of Law 5 will also have a first element of `r`; so, turning now to the rhs of Law 5, we see that `m$` must use the first element of its _left_-hand operand (here again `r`) at least in those cases when the first element of its right-hand operand is `r0`. If our `R` type has a natural *monoid* structure, we could just let `r0` be the monoid's identity, and have `m$` combine other `R`s using the monoid's operation. Alternatively, if the `R` type is one that we can safely apply the predicate `(r0==)` to, then we could define `m$` something like this: let (m$) (r1,f) (r2,x) = ((if r0==r1 then r2 else if r0==r2 then r1 else ...), ...) -- 2.11.0