From: jim Date: Thu, 19 Mar 2015 14:37:16 +0000 (-0400) Subject: wip X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=d83310d4c89d4b3adf0b9a1893f1721aaf227cbb;ds=sidebyside wip --- diff --git a/topics/week7_introducing_monads.mdwn b/topics/week7_introducing_monads.mdwn index a8351398..f67271f4 100644 --- a/topics/week7_introducing_monads.mdwn +++ b/topics/week7_introducing_monads.mdwn @@ -15,13 +15,13 @@ sometimes sloganized as Without some intuitive guidance, this can also be unhelpful. We'll try to find a good balance. The closest we will come to metaphorical talk is to suggest that -monadic types place objects inside of *boxes*, and that monads wrap -and unwrap boxes to expose or enclose the objects inside of them. In +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 linguistics literature. -## Box types: type expressions with one free type variable +## Box types: type expressions with one free type variable ## Recall that we've been using lower-case Greek letters α, β, γ, ... as type variables. We'll @@ -44,46 +44,55 @@ to specify which one of them the box is capturing. But let's keep it simple.) So (α, R) tree (assuming R contains no free type variables) (α, α) tree -The idea is that whatever type the free type variable α might be, -the boxed type will be a box that "contains" an object of type `α`. -For instance, if `α list` is our box type, and `α` is the type -`int`, then in this context, `int list` is the type of a boxed integer. +The idea is that whatever type the free type variable `α` might be instantiated to, +we will be a "type box" of a certain sort that "contains" values of type `α`. For instance, +if `α list` is our box type, and `α` is the type `int`, then in this context, `int list` +is the type of a boxed integer. -Warning: although our initial motivating examples are naturally thought of as "containers" (lists, trees, and so on, with `α`s as their "elments"), with later examples we discuss it will less intuitive to describe the box types that way. For example, where `R` is some fixed type, `R -> α` is a box type. +Warning: although our initial motivating examples are readily thought of as "containers" (lists, trees, and so on, with `α`s as their "elements"), with later examples we discuss it will be less natural to describe the boxed types that way. For example, where `R` is some fixed type, `R -> α` is a box type. -The *box type* is the type `α list` (or as we might just say, `list`); the *boxed type* is some specific instantiantion of the free type variable `α`. We'll often write boxed types as a box containing the instance of the free -type variable. So if our box type is `α list`, and `α` is instantiated with the specific type `int`, we would write: +Also, for clarity: the *box type* is the type `α list` (or as we might just say, the `list` type operator); the *boxed type* is some specific instantiation of the free type variable `α`. We'll often write boxed types as a box containing what the free +type variable instantiates to. So if our box type is `α list`, and `α` instantiates to the specific type `int`, we would write: int -for the type of a boxed `int`. (We'll fool with the markup to make this a genuine box later; for now it will just display as underlined.) +for the type of a boxed `int`. (We'll fool with the markup to make this show a genuine box later; for now it will just display as underlined.) -## Kleisli arrows +## Kleisli arrows ## A lot of what we'll be doing concerns types that are called *Kleisli arrows*. Don't worry about why they're called that, or if you like go read some Category Theory. All we need to know is that these are functions whose type has the form: P -> Q -That is, they are functions from objects 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 type expressions `P` and `Q`. For instance, the following are Kleisli arrows: int -> bool int list -> int list -In the first, `P` has become `int` and `Q` has become `bool`. (The boxed type Q is bool). +In the first, `P` has become `int` and `Q` has become `bool`. (The boxed type Q is bool). Note that the left-hand schema `P` is permitted to itself be a boxed type. That is, where if `α list` is our box type, we can write the second arrow as int -> Q +<<<<<<< HEAD We'll need a number of classes of functions to help us maneuver in the presence of box types. We will want to define a different instance of each of these for whichever box type we're dealing with. (This will become clear shortly.) +======= +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 arrows. + + +## A family of functions for each box type ## + +We'll need a family of functions to help us work with box types. As will become clear, these have to be defined differently for each box type. +>>>>>>> ecff6bbae7c00556584b51913b934bdade0cff40 Here are the types of our crucial functions, together with our pronunciation, and some other names the functions go by. (Usually the type doesn't fix their behavior, which will be discussed below.) @@ -97,15 +106,25 @@ Here are the types of our crucial functions, together with our pronunciation, an <=< or mcomp : (Q -> R) -> (P -> Q) -> (P -> R) ->=> or mpmoc (m-flipcomp): (P -> Q) -> (Q -> R) -> (P -> R) +>=> or mpmoc (flip mcomp): (P -> Q) -> (Q -> R) -> (P -> R) >>= or mbind : (Q) -> (Q -> R) -> (R) -=<<mdnib (or m-flipbind) (Q) -> (Q -> R) -> (R) +=<< or mdnib (flip mbind) (Q) -> (Q -> R) -> (R) + +join: P -> P + + +Test1: P + +Test2: P + +Test3: XX + +Test4: YY -join: 2P -> P -The managerie isn't quite as bewildering as you might suppose. Many of these will +The menagerie isn't quite as bewildering as you might suppose. Many of these will be interdefinable. For example, here is how `mcomp` and `mbind` are related: k <=< j ≡ \a. (j a >>= k). @@ -139,7 +158,7 @@ are a definition of `mid`, on the one hand, and one of `mcomp`, `mbind`, or `joi Here are some interdefinitions: TODO. Names in Haskell TODO. -## Examples +## Examples ## To take a trivial (but, as we will see, still useful) example, consider the identity box type Id: `α`. So if `α` is type `bool`, @@ -208,8 +227,7 @@ Contrast that to `m$` (`mapply`, which operates not on two *box-producing functi As we illustrated in class, there are clear patterns shared between lists and option types and trees, so perhaps you can see why people want to identify the general structures. But it probably isn't obvious yet why it would be useful to do so. To a large extent, this will only emerge over the next few classes. But we'll begin to demonstrate the usefulness of these patterns by talking through a simple example, that uses the Monadic functions of the Option/Maybe box type. -Safe division -------------- +## Safe division ## Integer division presupposes that its second argument (the divisor) is not zero, upon pain of presupposition failure. @@ -369,7 +387,7 @@ Compare the new definitions of `safe_add3` and `safe_div3` closely: the definiti for `safe_add3` shows what it looks like to equip an ordinary operation to survive in dangerous presupposition-filled world. Note that the new definition of `safe_add3` does not need to test whether its arguments are -None objects or real numbers---those details are hidden inside of the +None values or real numbers---those details are hidden inside of the `bind` function. The definition of `safe_div3` shows exactly what extra needs to be said in