X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?a=blobdiff_plain;ds=inline;f=zipper-lists-continuations.mdwn;h=9ef23c250e4d3c71affbb3a0d40ce72ad156e51c;hb=07e66be3b3fa7b8b843894bdcbcb181bd1757349;hp=f0a252ee46d4e5f2141de51c700112c6e12faf16;hpb=25c4a39a52fd98f0df15e3e928795a1972db8f09;p=lambda.git diff --git a/zipper-lists-continuations.mdwn b/zipper-lists-continuations.mdwn index f0a252ee..9ef23c25 100644 --- a/zipper-lists-continuations.mdwn +++ b/zipper-lists-continuations.mdwn @@ -22,78 +22,77 @@ and monads). For instance, take the **Reader Monad**. Once we decide that the type constructor is - type 'a reader = fun e:env -> 'a + type 'a reader = env -> 'a -then we can deduce the unit and the bind: +then the choice of unit and bind is natural: - r_unit x:'a -> 'a reader = fun (e:env) -> x + let r_unit (a : 'a) : 'a reader = fun (e : env) -> a -Since the type of an `'a reader` is `fun e:env -> 'a` (by definition), -the type of the `r_unit` function is `'a -> e:env -> 'a`, which is a -specific case of the type of the *K* combinator. So it makes sense +Since the type of an `'a reader` is `env -> 'a` (by definition), +the type of the `r_unit` function is `'a -> env -> 'a`, which is a +specific case of the type of the *K* combinator. It makes sense that *K* is the unit for the reader monad. Since the type of the `bind` operator is required to be - r_bind:('a reader) -> ('a -> 'b reader) -> ('b reader) + r_bind : ('a reader) -> ('a -> 'b reader) -> ('b reader) -We can deduce the correct `bind` function as follows: +We can reason our way to the correct `bind` function as follows. We start by declaring the type: - r_bind (u:'a reader) (f:'a -> 'b reader):('b reader) = + let r_bind (u : 'a reader) (f : 'a -> 'b reader) : ('b reader) = -We have to open up the `u` box and get out the `'a` object in order to +Now we have to open up the `u` box and get out the `'a` object in order to feed it to `f`. Since `u` is a function from environments to objects of type `'a`, the way we open a box in this monad is by applying it to an environment: - .... f (u e) ... + ... f (u e) ... This subexpression types to `'b reader`, which is good. The only problem is that we invented an environment `e` that we didn't already have , so we have to abstract over that variable to balance the books: - fun e -> f (u e) ... + fun e -> f (u e) ... This types to `env -> 'b reader`, but we want to end up with `env -> -'b`. Once again, the easiest way to turn a `'b reader` into a `'b` is to apply it to -an environment. So we end up as follows: +'b`. Once again, the easiest way to turn a `'b reader` into a `'b` is to apply it to an environment. So we end up as follows: - r_bind (u:'a reader) (f:'a -> 'b reader):('b reader) = f (u e) e + r_bind (u : 'a reader) (f : 'a -> 'b reader) : ('b reader) = + f (u e) e -And we're done. +And we're done. This gives us a bind function of the right type. We can then check whether, in combination with the unit function we chose, it satisfies the monad laws, and behaves in the way we intend. And it does. -[This bind is a simplified version of the careful `let a = u e in ...` -constructions we provided in earlier lectures. We use the simplified -versions here in order to emphasize similarities of structure across -monads; the official bind is still the one with the plethora of `let`'s.] +[The bind we cite here is a condensed version of the careful `let a = u e in ...` +constructions we provided in earlier lectures. We use the condensed +version here in order to emphasize similarities of structure across +monads.] -The **State Monad** is similar. We somehow intuit that we want to use -the following type constructor: +The **State Monad** is similar. Once we've decided to use the following type constructor: - type 'a state = 'store -> ('a, 'store) + type 'a state = store -> ('a, store) -So our unit is naturally +Then our unit is naturally: - let s_unit (x:'a):('a state) = fun (s:'store) -> (x, s) + let s_unit (a : 'a) : ('a state) = fun (s : store) -> (a, s) -And we deduce the bind in a way similar to the reasoning given above. -First, we need to apply `f` to the contents of the `u` box: +And we can reason our way to the bind function in a way similar to the reasoning given above. First, we need to apply `f` to the contents of the `u` box: - let s_bind (u:'a state) (f:'a -> ('b state)):('b state) = + let s_bind (u : 'a state) (f : 'a -> 'b state) : 'b state = + ... f (...) ... But unlocking the `u` box is a little more complicated. As before, we need to posit a state `s` that we can apply `u` to. Once we do so, however, we won't have an `'a`, we'll have a pair whose first element is an `'a`. So we have to unpack the pair: - ... let (a, s') = u s in ... (f a) ... + ... let (a, s') = u s in ... (f a) ... Abstracting over the `s` and adjusting the types gives the result: - let s_bind (u:'a state) (f:'a -> ('b state)):('b state) = - fun (s:state) -> let (a, s') = u s in f a s' + let s_bind (u : 'a state) (f : 'a -> 'b state) : 'b state = + fun (s : store) -> let (a, s') = u s in f a s' -The **Option Monad** doesn't follow the same pattern so closely, so we +The **Option/Maybe Monad** doesn't follow the same pattern so closely, so we won't pause to explore it here, though conceptually its unit and bind follow just as naturally from its type constructor. @@ -101,7 +100,7 @@ Our other familiar monad is the **List Monad**, which we were told looks like this: type 'a list = ['a];; - l_unit (x:'a) = [x];; + l_unit (a : 'a) = [a];; l_bind u f = List.concat (List.map f u);; Recall that `List.map` take a function and a list and returns the @@ -125,8 +124,8 @@ So let's indulge ourselves in a completely useless digression and see if we can gain some insight into the details of the List monad. Let's choose type constructor that we can peer into, using some of the technology we built up so laboriously during the first half of the -course. We're going to use type 3 lists, partly because I know -they'll give the result I want, but also because they're the coolest. +course. We're going to use type 3 lists, partly because we know +they'll give the result we want, but also because they're the coolest. These were the lists that made lists look like Church numerals with extra bits embdded in them: @@ -135,29 +134,27 @@ extra bits embdded in them: list with two elements: fun f z -> f 2 (f 1 z) list with three elements: fun f z -> f 3 (f 2 (f 1 z)) -and so on. To save time, we'll let the Ocaml interpreter infer the +and so on. To save time, we'll let the OCaml interpreter infer the principle types of these functions (rather than deducing what the types should be): -
-# fun f z -> z;; -- : 'a -> 'b -> 'b =- -Finally, we're getting consistent principle types, so we can stop. -These types should remind you of the simply-typed lambda calculus -types for Church numerals (`(o -> o) -> o -> o`) with one extra bit -thrown in (in this case, an int). + # fun f z -> z;; + - : 'a -> 'b -> 'b =-# fun f z -> f 1 z;; -- : (int -> 'a -> 'b) -> 'a -> 'b = -# fun f z -> f 2 (f 1 z);; -- : (int -> 'a -> 'a) -> 'a -> 'a = -# fun f z -> f 3 (f 2 (f 1 z)) -- : (int -> 'a -> 'a) -> 'a -> 'a = -
-# let cons h t = h :: t;; (* Ocaml is stupid about :: *) -# l'_bind (fun f z -> f 1 (f 2 z)) - (fun i -> fun f z -> f i (f (i+1) z)) cons [];; -- : int list = [1; 2; 2; 3] -+ # let cons h t = h :: t;; (* OCaml is stupid about :: *) + # l'_bind (fun f z -> f 1 (f 2 z)) + (fun i -> fun f z -> f i (f (i+1) z)) cons [];; + - : int list = [1; 2; 2; 3] Ta da! To bad this digression, though it ties together various elements of the course, has *no relevance whatsoever* to the topic of -continuations. +continuations... Montague's PTQ treatment of DPs as generalized quantifiers ---------------------------------------------------------- @@ -263,7 +299,7 @@ generalized quantifier `fun pred -> pred j` of type `(e -> t) -> t`. Let's write a general function that will map individuals into their corresponding generalized quantifier: - gqize (x:e) = fun (p:e->t) -> p x + gqize (a : e) = fun (p : e -> t) -> p a This function wraps up an individual in a fancy box. That is to say, we are in the presence of a monad. The type constructor, the unit and @@ -271,17 +307,17 @@ the bind follow naturally. We've done this enough times that we won't belabor the construction of the bind function, the derivation is similar to the List monad just given: - type 'a continuation = ('a -> 'b) -> 'b - c_unit (x:'a) = fun (p:'a -> 'b) -> p x - c_bind (u:('a -> 'b) -> 'b) (f: 'a -> ('c -> 'd) -> 'd): ('c -> 'd) -> 'd = - fun (k:'a -> 'b) -> u (fun (x:'a) -> f x k) + type 'a continuation = ('a -> 'b) -> 'b + c_unit (a : 'a) = fun (p : 'a -> 'b) -> p a + c_bind (u : ('a -> 'b) -> 'b) (f : 'a -> ('c -> 'd) -> 'd) : ('c -> 'd) -> 'd = + fun (k : 'a -> 'b) -> u (fun (a : 'a) -> f a k) How similar is it to the List monad? Let's examine the type constructor and the terms from the list monad derived above: type ('a, 'b) list' = ('a -> 'b -> 'b) -> 'b -> 'b - l'_unit x = fun f -> f x - l'_bind u f = fun k -> u (fun x -> f x k) + l'_unit a = fun f -> f a + l'_bind u f = fun k -> u (fun a -> f a k) (We performed a sneaky but valid eta reduction in the unit term.) @@ -289,19 +325,18 @@ The unit and the bind for the Montague continuation monad and the homemade List monad are the same terms! In other words, the behavior of the List monad and the behavior of the continuations monad are parallel in a deep sense. To emphasize the parallel, we can -instantiate the type of the list' monad using the Ocaml list type: +instantiate the type of the list' monad using the OCaml list type: type 'a c_list = ('a -> 'a list) -> 'a list - let c_list_unit x = fun f -> f x;; - let c_list_bind u f = fun k -> u (fun x -> f x k);; -Have we really discovered that lists are secretly continuations? -Or have we merely found a way of simulating lists using list +Have we really discovered that lists are secretly continuations? Or +have we merely found a way of simulating lists using list continuations? Both perspectives are valid, and we can use our intuitions about the list monad to understand continuations, and vice -versa. The connections will be expecially relevant when we consider -indefinites and Hamblin semantics on the linguistic side, and -non-determinism on the list monad side. +versa (not to mention our intuitions about primitive recursion in +Church numerals too). The connections will be expecially relevant +when we consider indefinites and Hamblin semantics on the linguistic +side, and non-determinism on the list monad side. Refunctionalizing zippers -------------------------