add _lambda_encodings
authorJim <jim.pryor@nyu.edu>
Sat, 7 Feb 2015 19:47:09 +0000 (14:47 -0500)
committerJim <jim.pryor@nyu.edu>
Sat, 7 Feb 2015 19:47:09 +0000 (14:47 -0500)
topics/_lambda_encodings.mdwn [new file with mode: 0644]

diff --git a/topics/_lambda_encodings.mdwn b/topics/_lambda_encodings.mdwn
new file mode 100644 (file)
index 0000000..aebe2b0
--- /dev/null
@@ -0,0 +1,201 @@
+# Encoding Booleans, Tuples, Lists, and Numbers #
+
+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.
+
+
+## Booleans ##
+
+We'll start with the `if ... then
+... else...` construction we saw last week:
+
+    if M then N else L
+
+For a boolean-valued expression `M`, the displayed expression should evaluate to whatever `N` does
+if `M` evaluates to `'true`, and to whatever `L` does if `M` evaluates to `'false.
+In order to encode or simulate such an `if` clause in the Lambda Calculus, we
+need to settle on a way to represent `'true` and `'false`.
+
+We could simply add the constants `'true` and `'false` to the
+language, since it does make sense to extend the Lambda Calculus by adding constants.
+However, that would also require complicating the
+interpretation of the language; at the very least, we would need more
+than just beta-reduction as our engine of computation.  In the spirit
+of computational minimalism, let's see how far we can get with the
+"pure" lambda calculus, without any special constants.
+
+Let's get rid of the parts of the `if` statement that are
+just syntactic window-dressing.  That is, let's get rid of the
+`if`, the `then`, and the `else`:
+
+    M  N  L
+
+Recall that our convention is that values associate left to right, so
+this series of terms would be evaluated as:
+
+    ((M N) L)
+
+If this expression is supposed to have the meaning of `if M then N
+else L`, then we need to find a value for `'true` such that when it is
+substituted in place of `M`, the expression evaluates to `N`.  The
+function we're looking for should take two arguments: first `N`, then `L`,
+and it should throw away `L` and return `N`.  
+
+We've already seen such a function.  We called it **K**: `(\x y. x)`.
+Let's test:
+
+    ((K N) L) == (((\x y. x) N) L) ~~> ((\y. N) L) ~~> N
+
+Sucess! In the same spirit, `'false` could be **K I**, which reduces to `(\y x. x)`:
+
+    (((K I) N) L) == ((((\x y. x) (\x x)) N) L) 
+                     ~~> (((\y. (\x x)) N) L)
+                     ~~> ((\x x) L)
+                     ~~> L
+
+So have seen our first major encoding in the lambda calculus:
+"true" is represented by **K**, and "false" is represented by **K I**.
+We'll be building up a lot of representations in the weeks to come,
+and they will all maintain the discipline that if a expression is
+meant to be interpreted as a truth value (i.e. as a Boolean), it will
+be equivalent to (convertible with) **K** or **K I**.
+
+In class, we explained how "and" could be thought of as the function, here written
+in Kapulet syntax:
+
+    lambda p q. if p then q else false
+
+or:
+
+    lambda p q. if p then q else q
+
+Given that we know how to express `if ... then ...` in terms of our encoded Booleans,
+we can represent this easily in the Lambda Calculus as either:
+
+    \p q. p q (\y x. x)
+
+or:
+
+    \p q. p q p
+
+Typically there will be more than one way to encode a given function into the Lambda Calculus, even holding fixed
+all your other decisions about how to encode other functions. In this case, we can use either representation
+because we're assuming that the `p` only gets bound to arguments that are either `\x y. x` or `\y x. x`. We don't make any
+promises about what will happen if our encodings are abused, that is, supplied with arguments they weren't designed to accept.
+If `p` is bound to `\x y. x`, then the result of both of the above displayed expressions will be whatever `q` is bound to.
+If on the other hand, `p` is bound to `\y x. x`, then the two dislayed expressions will be the same. Despite this, they are
+two different lambda expressions, and are not convertible. It's only within the frame of our assumptions about the restricted
+arguments we're thinking of supplying them that they behave exactly the same.
+
+You can try out these expressions in the [[lambda evaluator|code/lambda evaluator]]:
+
+    let true = \x y. x in
+    let false = \y x. x in
+    let and = \p q. p q p in
+    and true false
+
+will reduce or "normalize" to `\y x. x`, or false, just as you'd predict. The `let true =` stuff isn't officially part of the Lambda Calculus language. It's just a convenient shorthand that lets you use the lambda evaluator more efficiently. Behind the scenes, the displayed expression above gets translated to:
+
+    (\true.
+      (\false.
+        (\and. and true false)))
+        (\x y. x) ; this gets bound to variable "true"
+        (\y x. x) ; this gets bound to variable "false"
+        (\p q. p q p) ; this gets bound to variable "and"
+
+We expect you'll agree that the first is easier to write and to read.
+
+In Scheme (Racket), all of these notions can be defined like this:
+
+    (define true  (lambda (x) (lambda (y) x)))
+    (define false (lambda (y) (lambda (x) x)))
+    (define lambda-and (lambda (p) (lambda (q) ((p q) p))))
+
+and then used like this:
+
+    ((lambda-and true) false)
+
+which will evaluate to a function, that happens to be the true function. Most Scheme interpreters like Racket will helpfully display the function with the name, if any, that was first defined to be bound to it. So we'll see the result as:
+
+    #<procedure:false>
+
+The funny calling pattern, where we write `((lambda-and true) false)` instead of just `(lambda-and true false)`, is because that's how you have to write curried functions in Scheme. Similarly for why we have `(lambda (x) (lambda (y) x))` instead of just `(lambda (x y) x)`.
+
+It's possible to do the next few weeks of assignment without using a Scheme interpreter, however
+we do recommend you [get Scheme installed on your
+computer](/how_to_get_the_programming_languages_running_on_your_computer), and
+[get started learning Scheme](/learning_scheme). It will help you understand the ideas better to experiment with it.
+
+There's also a (slow, bare-bones, but perfectly adequate) version of Scheme available for online use at <http://tryscheme.sourceforge.net/>.
+
+You should also be experimenting with this site's [[lambda evaluator|code/lambda evaluator]].
+
+
+## Tuples ##
+
+In class, we also showed you how to encode a tuple in the Lambda Calculator. 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. The strategy presented here is elegant 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
+
+To extract the first element of this, you'd write:
+
+    (\f. f 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) ~~>
+    (\x y z. x) a b c ~~>
+    (\y z. a) b c ~~>
+    (\z. a) c ~~>
+    a
+
+Here are the corresponding definitions in Scheme (Racket):
+
+    (define make-triple (lambda (a) (lambda (b) (lambda (c)
+                          (lambda (f) (((f 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))))
+    (define trd_of_three (lambda (x) (lambda (y) (lambda (z) z))))
+
+Then:
+
+    (define p (((make-triple 10) 20) 30))
+    (p fst_of_three)  ; will evaluate to 10
+    (p snd_of_three)  ; will evaluate to 20
+
+If you're puzzled by having the triple to the left and the function that "uses" or "consumes" or operates on it to the right, think about why it's being done this way: the triple is a package that will be used in coordination with some function for operating on its elements. We don't know in advance what that function will be. And it's not obvious how to make the triple be some structure that the function can "look inside" and extract the elements from. We're still trying to *figure out* how to define such structures! But what we can do is make *the triple take the function as an argument*, and return *the result of* operating on its elements with that function. In other words, the triple is a higher-order function: a function that expects another function as an argument.
+
+(Consider the similarities between this definition of a triple and a generalized quantifier. This is in fact our first taste of "continuations" in the course, which are a systematic pattern for inverting the naive order of who-is-the-argument? and who-is-the-operator?)
+
+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)))
+
+Then you could say:
+
+    (lifted-fst_of_three p)
+
+instead of:
+
+    (p 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)`.)
+
+## Lists ##
+
+Coming...
+
+
+## Numbers ##
+
+Coming...
+
+