link to hint
[lambda.git] / topics / week2_encodings.mdwn
index ac92202..091fb4c 100644 (file)
@@ -5,7 +5,7 @@ The Lambda Calculus can represent any computable function?
 We need to do some work to show how to represent some of the functions
 we've become acquainted with.
 
-
+<a id=booleans></a>
 ## Booleans ##
 
 We'll start with the `if ... then ... else ...` construction we saw last week:
@@ -135,21 +135,22 @@ There's also a (slow, bare-bones, but perfectly adequate) version of Scheme avai
 You should also be experimenting with this site's [[lambda evaluator|code/lambda evaluator]].
 
 
+<a id=tuples></a>
 ## Tuples ##
 
 In class, we also showed you how to encode a tuple in the Lambda Calculus. We did it with an ordered triple, but the strategy generalizes in a straightforward way. (Some authors just use this strategy to define *pairs*, then define triples as pairs whose second member is another pair, and so on. Yech. If you keep a firm grip on your wits, that can also be made to work, but it's extremely likely that people who code in that way are going to lose their grip at some point and get themselves in a corner where they'll regret having made that decision about how to encode triples. And they will be forced to add further complexities at later points, that they're probably not anticipating now. The strategy presented here is as elegant as it first looks, and will help you program more hygienically even when your attention lapses.)
 
 Our proposal was to define the triple `(a, b, c)` as:
 
-    \f. f a b c
+    \h. h a b c
 
 To extract the first element of this, you'd write:
 
-    (\f. f a b c) fst_of_three
+    (\h. h a b c) fst_of_three
 
 where `fst_of_three` is the function `\x y z. x`:
 
-    (\f. f a b c) (\x y z. x) ~~>
+    (\h. h a b c) (\x y z. x) ~~>
     (\x y z. x) a b c ~~>
     (\y z. a) b c ~~>
     (\z. a) c ~~>
@@ -158,7 +159,7 @@ where `fst_of_three` is the function `\x y z. x`:
 Here are the corresponding definitions in Scheme (Racket):
 
     (define make-triple (lambda (a) (lambda (b) (lambda (c)
-                          (lambda (f) (((f a) b) c))))))
+                          (lambda (h) (((h a) b) c))))))
 
     (define fst_of_three (lambda (x) (lambda (y) (lambda (z) x))))
     (define snd_of_three (lambda (x) (lambda (y) (lambda (z) y))))
@@ -176,21 +177,38 @@ If you're puzzled by having the triple to the left and the function that "uses"
 
 If you really want to, you can disguise what's going on like this:
 
-    (define lifted-fst_of_three (lambda (p) (p fst_of_three)))
+    (define lifted-fst_of_three (lambda (trip) (trip fst_of_three)))
 
 Then you could say:
 
-    (lifted-fst_of_three p)
+    (lifted-fst_of_three t)
 
 instead of:
 
-    (p fst_of_three)
+    (t fst_of_three)
 
 Of course, the last is still what's happening under the hood.
 
 (Remark: `(lifted-f (((make-triple 10) 20) 30))` stands to `((((make-triple 10) 20) 30) f)` as
 `((((make-triple 10) 20) 30) f)` stands to `(((f 10) 20) 30)`.)
 
+
+### Curried and Uncurried functions in the Lambda Calculus ###
+
+As we've explained before, an *uncurried* function is one that takes multiple arguments in a single bundle, as a tuple, like this:
+
+    f (x, y, z)
+
+Whereas a *curried* function is one that takes multiple arguments in sequence, like this:
+
+    g x y z
+
+That is, `g` is a function expecting one argument (here `x`), that evaluates to a second function, that itself expects another argument (here `y`), and so on. (So `g` is a *higher-order function*: the result of applying it to argument `x` returns another function.) In discussing Kapulet and Scheme and OCaml and Haskell, we sometimes worked with uncurried functions, and other times with curried ones. Now that you've seen how to build and work with tuples in the Lambda Calculus, you can write uncurried functions there too. That is, you can write functions `f` that will expect arguments of the form `\h. h x y z`. But in the end, `f` is going to have to apply that argument to some auxiliary handler function `g` anyway, where `g` takes *its* arguments in curried form. So if you can, in the Lambda Calculus it's easiest to just work with curried functions like `g` in the first place, rather than uncurried, tuple-expecting arguments like `f`.
+
+In some cases you can't do this, because you'll be partaking of some general pattern that only makes room for a single argument --- like the "starting value" `z` in the `fold_right` function discussed below; yet you're performing some task that really requires you to stuff a couple of values into that position. Tuples are ideal for that purpose. But in for run-of-the-mill functions you're defining in the Lambda Calculus, if multiple arguments need to be passed to a function, and it's up to you whether to pass them in curried or uncurried/tuple style, you should default to the curried style (as in, `g x y z`). That's the more idiomatic, native style for passing arguments in the Lambda Calculus.
+
+
+<a id=lists></a>
 ## Lists ##
 
 There are multiple ways to encode lists, and also multiple ways to encode numbers. We are going to start with what we think are the most natural and elegant encodings. Historically these were the first encodings of numbers but not of lists.
@@ -213,6 +231,7 @@ That will evaluate to whatever this does:
 
     f (f (f (z, 10), 20), 30)
 
+<a id=flipped-cons></a>
 With a commutative operator like `(+)`, it makes no difference whether you say `fold_right ((+), z) xs` or `fold_left ((+), z) xs`. But with other operators it will make a difference. We can't say `fold_left ((&), []) [10, 20, 30]`, since that would start by trying to evaluate `[] & 10`, which would crash. But we could do this:
 
     let
@@ -258,7 +277,7 @@ Now, what should the `SOMETHING` be? Well, when we supply an `f` and a `z` we sh
 
     \f z. f a (f b (f c z))
 
-Here we work with curried functions, because that's how the Lambda Calculus does things. You wouldn't want to build up a tuple using the mechanisms described above, and then supply `f` as an argument to that tuple, and so on. That would be a lot of red tape for no benefit. In the Lambda Calculus, it's simpler to just work with curried functions as our natural idiom.
+Here we assume `f` to be a curried function, taking its arguments in the form `f c z` rather that `f (c, z)` (that is, `f (\h. h c z)`), because as we explained at the end of the section on Tuples, the curried form is the idiomatic and native style for passing multiple arguments in the Lambda Calculus.
 
 So if `[a, b, c]` should be the displayed higher-order function above, what should `[c]` be? Evidently:
 
@@ -338,6 +357,7 @@ That will evaluate to:
 which looks like what we want, a higher-order function that will take an `f` and a `z` as arguments and then return the right fold of those arguments over `[g a, g b, g c]`, which is `map g [a, b, c]`.
 
 
+<a id=numbers></a>
 ## Numbers ##
 
 Armed with the encoding we developed for lists above, Church's method for encoding numbers in the Lambda Calculus is very natural. But this is not the order that these ideas were historically developed.
@@ -378,6 +398,8 @@ In fact, there's a way of looking at this that makes it look incredibly natural.
 
     \x. f (g x)
 
+For example, the operation that maps a number `n` to <code>n<sup>2</sup>+1</code> is the composition of the successor function and the squaring function (first we square, then we take the successor).
+
 The composition of a function `f` with itself, namely:
 
     \x. f (f x)
@@ -396,13 +418,13 @@ we are proposing to encode it as:
 
 And indeed this is the Church encoding of the numbers:
 
-<code>0 &equiv; \f z. I z &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;  or \f z. f<sup>0</sup> z</code>  
-<code>1 &equiv; \f z. f z &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;  or \f z. f<sup>1</sup> z</code>  
-<code>2 &equiv; \f z. f (f z) &nbsp;&nbsp;&nbsp;&nbsp;;  or \f z. f<sup>2</sup> z</code>  
-<code>3 &equiv; \f z. f (f (f z)) ;  or \f z. f<sup>3</sup> z</code>  
+<code>0 &equiv; \f z. z &nbsp;&nbsp;;  <~~> \f z. I z, or \f z. f<sup>0</sup> z</code>  
+<code>1 &equiv; \f z. f z &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;  or \f z. f<sup>1</sup> z</code>  
+<code>2 &equiv; \f z. f (f z) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;  or \f z. f<sup>2</sup> z</code>  
+<code>3 &equiv; \f z. f (f (f z)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;  or \f z. f<sup>3</sup> z</code>  
 <code>...</code>
 
-The encoding for `0` is equivalent to `\f z. z`, which we've also proposed as the encoding for `[]` and for `false`. Don't read too much into this.
+The encoding for `0` is what we also proposed as the encoding for `[]` and for `false`. Don't read too much into this.
 
 Given the above, can you figure out how to define the `succ` function? We already worked through the definition of `cons`, and this is just a simplification of that, so you should be able to do it. We'll make it a homework.