From: Jim Date: Sat, 7 Feb 2015 14:55:55 +0000 (-0500) Subject: refine week2 notes part1 X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=dd47850ecb1e5575272831c69bf6fcfcb3f6be24;ds=sidebyside refine week2 notes part1 --- diff --git a/exercises/_assignment2.mdwn b/exercises/_assignment2.mdwn deleted file mode 100644 index 6510f1e1..00000000 --- a/exercises/_assignment2.mdwn +++ /dev/null @@ -1,152 +0,0 @@ -Reduction ---------- - -Find "normal forms" for the following---that is, reduce them until no more reductions are possible. We'll write λx as `\x`. - -1. `(\x \y. y x) z` -2. `(\x (x x)) z` -3. `(\x (\x x)) z` -4. `(\x (\z x)) z` -5. `(\x (x (\y y))) (\z (z z))` -6. `(\x (x x)) (\x (x x))` -7. `(\x (x x x)) (\x (x x x))` - - -Booleans --------- - -Recall our definitions of true and false. - -> **true** is defined to be `\t \f. t` -> **false** is defined to be `\t \f. f` - -In Racket, these can be defined like this: - - (define true (lambda (t) (lambda (f) t))) - (define false (lambda (t) (lambda (f) f))) - -
    -
  1. Define a `neg` operator that negates `true` and `false`. - -Expected behavior: - - (((neg true) 10) 20) - -evaluates to 20, and - - (((neg false) 10) 20) - -evaluates to 10. - -
  2. Define an `and` operator. - -
  3. Define an `xor` operator. If you haven't seen this term before, here's a truth table: - - true xor true == false - true xor false == true - false xor true == true - false xor false == false - - -
  4. Inspired by our definition of boolean values, propose a data structure -capable of representing one of the two values `black` or `white`. -If we have -one of those values, call it a "black-or-white value", we should be able to -write: - - the-value if-black if-white - -(where `if-black` and `if-white` are anything), and get back one of `if-black` or -`if-white`, depending on which of the black-or-white values we started with. Give -a definition for each of `black` and `white`. (Do it in both lambda calculus -and also in Racket.) - -
  5. Now propose a data structure capable of representing one of the three values -`red` `green` or `blue`, based on the same model. (Do it in both lambda -calculus and also in Racket.) -
- - - -Pairs ------ - -Recall our definitions of ordered pairs. - -> the pair **(**x**,**y**)** is defined to be `\f. f x y` - -To extract the first element of a pair p, you write: - - p (\fst \snd. fst) - -Here are some definitions in Racket: - - (define make-pair (lambda (fst) (lambda (snd) (lambda (f) ((f fst) snd))))) - (define get-first (lambda (fst) (lambda (snd) fst))) - (define get-second (lambda (fst) (lambda (snd) snd))) - -Now we can write: - - (define p ((make-pair 10) 20)) - (p get-first) ; will evaluate to 10 - (p get-second) ; will evaluate to 20 - -If you're puzzled by having the pair to the left and the function that -operates on it come second, think about why it's being done this way: the pair -is a package that takes a function for operating on its elements *as an -argument*, and returns *the result of* operating on its elements with that -function. In other words, the pair is a higher-order function. (Consider the similarities between this definition of a pair and a generalized quantifier.) - -If you like, you can disguise what's going on like this: - - (define lifted-get-first (lambda (p) (p get-first))) - (define lifted-get-second (lambda (p) (p get-second))) - -Now you can write: - - (lifted-get-first p) - -instead of: - - (p get-first) - -However, the latter is still what's going on under the hood. (Remark: `(lifted-f ((make-pair 10) 20))` stands to `(((make-pair 10) 20) f)` as `(((make-pair 10) 20) f)` stands to `((f 10) 20)`.) - - -
    -
  1. Define a `swap` function that reverses the elements of a pair. Expected behavior: - - (define p ((make-pair 10) 20)) - ((p swap) get-first) ; evaluates to 20 - ((p swap) get-second) ; evaluates to 10 - -Write out the definition of `swap` in Racket. - - -
  2. Define a `dup` function that duplicates its argument to form a pair -whose elements are the same. -Expected behavior: - - ((dup 10) get-first) ; evaluates to 10 - ((dup 10) get-second) ; evaluates to 10 - -
  3. Define a `sixteen` function that makes -sixteen copies of its argument (and stores them in a data structure of -your choice). - -
  4. Inspired by our definition of ordered pairs, propose a data structure capable of representing ordered triples. That is, - - (((make-triple M) N) P) - -should return an object that behaves in a reasonable way to serve as a triple. In addition to defining the `make-triple` function, you have to show how to extract elements of your triple. Write a `get-first-of-triple` function, that does for triples what `get-first` does for pairs. Also write `get-second-of-triple` and `get-third-of-triple` functions. - -
  5. Write a function `second-plus-third` that when given to your triple, returns the result of adding the second and third members of the triple. - -You can help yourself to the following definition: - - (define add (lambda (x) (lambda (y) (+ x y)))) - - - -
- diff --git a/topics/_week2.mdwn b/topics/_week2.mdwn deleted file mode 100644 index 7706eca9..00000000 --- a/topics/_week2.mdwn +++ /dev/null @@ -1,12 +0,0 @@ -*This page is not ready to go live; just roughly copying over some material from last year.* - - -Here's what we did in seminar on Monday 9/13, - -Sometimes these notes will expand on things mentioned only briefly in class, or discuss useful tangents that didn't even make it into class. This present page expands on *a lot*, and some of this material will be reviewed next week. - -[Linguistic and Philosophical Applications of the Tools We'll be Studying](/applications) -========================================================================== - -[Explanation of the "Damn" example shown in class](/damn) - diff --git a/topics/_week2_lambda_calculus_fine_points.mdwn b/topics/_week2_lambda_calculus_fine_points.mdwn index 2176f359..1372e436 100644 --- a/topics/_week2_lambda_calculus_fine_points.mdwn +++ b/topics/_week2_lambda_calculus_fine_points.mdwn @@ -1,5 +1,4 @@ -Fine points concerning the lambda calculus -========================================== +## Fine points concerning the lambda calculus ## Hankin uses the symbol for one-step contraction, @@ -8,17 +7,18 @@ zero-or-more step reduction. Hindley and Seldin use 1 and . -When M and N are such that there's some P that M reduces to by zero or -more steps, and that N also reduces to by zero or more steps, then we -say that M and N are **beta-convertible**. We'll write that like this: +As we said in the main notes, when M and N are such that there's some P that M +reduces to by zero or more steps, and that N also reduces to by zero or more +steps, then we say that M and N are **beta-convertible**. We write that like +this: - M <~~> N + M <~~> N This is what plays the role of equality in the lambda calculus. Hankin uses the symbol `=` for this. So too do Hindley and -Seldin. Personally, I keep confusing that with the relation to be -described next, so let's use this notation instead. Note that `M <~~> -N` doesn't mean that each of `M` and `N` are reducible to each other; +Seldin. Personally, we keep confusing that with the relation to be +described next, so let's use the `<~~>` notation instead. Note that +`M <~~> N` doesn't mean that each of `M` and `N` are reducible to each other; that only holds when `M` and `N` are the same expression. (Or, with our convention of only saying "reducible" for one or more reduction steps, it never holds.) @@ -31,33 +31,147 @@ when discussing the metatheory. Instead we'll use `<~~>` as we said above. When we want to introduce a stipulative definition, we'll write it out longhand, as in: -> T is defined to be `(M N)`. +> T is defined to be `(M N)`. + +or: + +> Let T be `(M N)`. We'll regard the following two expressions: - (\x (x y)) + (\x (x y)) - (\z (z y)) + (\z (z y)) as syntactically equivalent, since they only involve a typographic -change of a bound variable. Read Hankin section 2.3 for discussion of +change of a bound variable. Read Hankin Section 2.3 for discussion of different attitudes one can take about this. -Note that neither of those expressions are identical to: +Note that neither of the above expressions are identical to: - (\x (x w)) + (\x (x w)) because here it's a free variable that's been changed. Nor are they identical to: - (\y (y y)) + (\y (y y)) because here the second occurrence of `y` is no longer free. There is plenty of discussion of this, and the fine points of how -substitution works, in Hankin and in various of the tutorials we've -linked to about the lambda calculus. We expect you have a good +substitution works, in Hankin and in various of the tutorials we'll +link to about the lambda calculus. We expect you have a good intuitive understanding of what to do already, though, even if you're not able to articulate it rigorously. -* [More discussion in week 2 notes](/week2/#index1h1) + +## Substitution and Alpha-Conversion ## + +Intuitively, (a) and (b) express the application of the same function to the argument `y`: + +
    +
  1. (\x. \z. z x) y +
  2. (\x. \y. y x) y +
+ +One can't just rename variables freely. (a) and (b) are different than what's expressed by: + +
    +
  1. (\z. (\z. z z) y +
+ + +Substituting `y` into the body of (a) `(\x. \z. z x)` is unproblematic: + + (\x. \z. z x) y ~~> \z. z y + +However, with (b) we have to be more careful. If we just substituted blindly, +then we might take the result to be `\y. y y`. But this is the self-application +function, not the function which accepts an arbitrary argument and applies that +argument to the free variable `y`. In fact, the self-application function is +what (c) reduces to. So if we took (b) to reduce to `\y. y y`, we'd wrongly be +counting (b) to be equivalent to (c), instead of (a). + +To reduce (b), then, we need to be careful to that no free variables in what +we're substituting in get captured by binding λs that they shouldn't be +captured by. + +In practical terms, you'd just replace (b) with (a) and do the unproblematic substitution into (a). + +How should we think about the explanation and justification for that practical procedure? + +One way to think about things here is to identify expressions of the lambda +calculus with *particular alphabetic sequences*. Then (a) and (b) would be +distinct expressions, and we'd have to have an explicit rule permitting us to +do the kind of variable-renaming that takes us from (a) to (b) (or vice versa). +This kind of renaming is called "alpha-conversion." Look in the standard +treatments of the lambda calculus for detailed discussion of this. + +Another way to think of it is to identify expressions not with particular +alphabetic sequences, but rather with *classes* of alphabetic sequences, which +stand to each other in the way that (a) and (b) do. That's the way we'll talk. +We say that (a) and (b) are just typographically different notations for a +*single* lambda formula. As we'll say, the lambda formula written with (a) and +the lambda formula written with (b) are literally syntactically identical. + +A third way to think is to identify the lambda formula not with classes of +alphabetic sequences, but rather with abstract structures that we might draw +like this: + +

+    (λ. λ. _ _) y
+     ^  ^  | |
+     |  |__| |
+     |_______|
+
+ +Here there are no bound variables, but *bound positions* remain. We can +regard formula like (a) and (b) as just helpfully readable ways to designate +these abstract structures. + +A version of this last approach is known as [de Bruijn notation](http://en.wikipedia.org/wiki/De_Bruijn_index) for the lambda calculus. + +It doesn't seem to matter which of these approaches one takes; the logical +properties of the systems are exactly the same. It just affects the particulars +of how one states the rules for substitution, and so on. And whether one talks +about expressions being literally "syntactically identical," or whether one +instead counts them as "equivalent modulu alpha-conversion." + +(Linguistic trivia: some linguistic discussions do suppose that +alphabetic variance has important linguistic consequences; see Ivan Sag's +dissertation.) + +Next week, we'll discuss other systems that lack variables. Those systems will +not just lack variables in the sense that de Bruijn notation does; they will +furthermore lack any notion of a bound position. + + +## Review: syntactic equality, reduction, convertibility ## + +Define N to be `(\x. x y) z`. Then N and `(\x. x y) z` are syntactically equal, +and we're counting them as syntactically equal to `(\z. z y) z` as well. We'll express +all these claims in our metalanguage as: + +
N ≡ (\x. x y) z ≡ (\z. z y) z
+
+ +This: + + N ~~> z y + +means that N beta-reduces to `z y`. This: + + M <~~> N + +means that M and N are beta-convertible, that is, that there's something they both reduce to in zero or more steps. + +The symbols `~~>` and `<~~>` aren't part of what we're calling "the Lambda +Calculus". In our mouths, they're just part of our metatheory for talking about it. In the uses of +the Lambda Calculus as a formal proof theory, one or the other of these +symbols (or some notational variant of them) is added to the object language. + +See Hankin Sections 2.2 and 2.4 for the proof theory using `<~~>` (which he +writes as `=`). He discusses the proof theory using `~~>` in his Chapter 3. +This material is covered in slightly different ways (different organization and +some differences in terminology and notation) in Chapters 1, 6, and 7 of the +Hindley & Seldin. diff --git a/topics/_week2_lambda_calculus_intro.mdwn b/topics/_week2_lambda_calculus_intro.mdwn index d393be1d..5f8df8dd 100644 --- a/topics/_week2_lambda_calculus_intro.mdwn +++ b/topics/_week2_lambda_calculus_intro.mdwn @@ -1,21 +1,46 @@ -Basics of Lambda Calculus -========================= +## Syntax of Lambda Calculus ## We often talk about "*the* Lambda Calculus", as if there were just one; but in fact, there are many, many variations. The one we will -start with, and that we will explore in some detail, is the "pure" -Lambda Calculus. And actually, there are many variations even in the -pure Lambda Calculus. But all of the variations share a strong family +start with, and will explore in some detail, is often called "the pure" +or "the untyped" Lambda Calculus. Actually, there are many variations even under +that heading. But all of the variations share a strong family resemblance, so what we learn now will apply to all of them. -The lambda calculus we'll be focusing on for the first part of the -course has no types. Some prefer to say it does have types, it's just -that there's only one type, so that every expression is a member of -that one type. If you say that, you have to say that functions from +> Fussy note: calling this/these the "pure" lambda calculus is entrenched terminology, +but it coheres imperfectly with other uses of "pure" we'll encounter. There are +three respects in which the lambda calculus we'll be presenting might claim to +deserve the name "pure": (1) it has no pre-defined constants and a very spare +syntax; (2) it has no types; (3) it has no side-effects, and is insensitive to +the order of evaluation. + +> Sense (3) corresponds most closely to the other uses of "pure" you'll +see in the surrounding literature. With respect to this point, it may be true that +this lambda calculus has no side effects. (Let's revisit that assumption +at the end of term.) But as we'll see next week, it is *not* true that it's insensitive +to the order of evaluation. So if that's what we mean by "pure", this lambda +calculus isn't as pure as you might hope to get. Some *typed* lambda calculi will +turn out to be more pure in that respect. + +> But this lambda calculus is at least "pure" in sense (2). At least, it +doesn't *explicitly talk about* any types. Some prefer to say that this +lambda calculus *does* have types implicitly, it's just that +there's only one type, so that every expression is a member of +that one type. If you say that, you have to say that functions from this type to this type also belong to this type. Which is weird... In fact, though, such types are studied, under the name "recursive types." More about these later in the seminar. +> Well, at least this lambda calculus is "pure" in sense (1). As we'll +see next week, though, there are some computational formal systems +whose syntax is *even more* spare, in that they don't even have variables. +So if that's what mean by "pure", again this lambda calculus isn't as pure +as you might hope to get. + +> Still, if you say you're talking about "the pure" Lambda Calculus, +or "the untyped" Lambda Calculus, or even just "the" Lambda Calculus, this +is the system that people will understand you to be referring to. + Here is its syntax:
@@ -41,8 +66,9 @@ Expressions in the lambda calculus are called "terms". Here is the syntax of the lambda calculus given in the form of a context-free grammar: - T --> ( T T ) + T --> Var T --> ( lambda Var T) + T --> ( T T ) Var --> x Var --> y Var --> z @@ -50,22 +76,29 @@ grammar: Very, very simple. +Sometimes the first two production rules are further distinguished, and those +are called more specifically "value terms". Whereas Applications (terms of the +form `(M N)`) are terms but not value terms. + Examples of terms: - x - (y x) - (x x) - (\x y) - (\x x) - (\x (\y x)) - (x (\x x)) - ((\x (x x)) (\x (x x))) + x + (y x) + (x x) + (\x y) + (\x x) + (\x (\y x)) + (x (\x x)) + ((\x (x x)) (\x (x x))) + + +## Beta-Reduction ## The lambda calculus has an associated proof theory. For now, we can regard the proof theory as having just one rule, called the rule of **beta-reduction** or "beta-contraction". Suppose you have some expression of the form: - ((\a M) N) + ((\a M) N) This is an application whose first element is an abstract. This compound form is called a **redex**, meaning it's a "beta-reducible @@ -74,67 +107,76 @@ called the **argument**, and `M` is called the **body**. The rule of beta-reduction permits a transition from that expression to the following: - M [a <-- N] + M [a <-- N] What this means is just `M`, with any *free occurrences* inside `M` of -the variable `a` replaced with the term `N`. +the variable `a` replaced with the term `N` (--- "without capture", which +we'll explain in the [[advanced notes|FIXME]]). What is a free occurrence? -> An occurrence of a variable `a` is **bound** in T if T has the form `(\a N)`. +> Any occurrence of a variable `a` is **bound** in T when T has the form `(\a N)`. -> If T has the form `(M N)`, any occurrences of `a` that are bound in `M` are also bound in T, and so too any occurrences of `a` that are bound in `N`. +> An occurrence of a variable `a` is **bound** in T when T has the form `(\b +N)` --- that is, with a *different* variable `b` --- just in case that +occurrence of `a` is bound in the subexpression `N`. -> An occurrence of a variable is **free** if it's not bound. +> When T has the form `(M N)`, any occurrences of `a` that are bound in the +subexpression `M` are also bound in T, and so too any occurrences of `a` that +are bound in the subexpression `N`. -For instance: +> An occurrence of a variable is **free** if it's not bound. -> T is defined to be `(x (\x (\y (x (y z)))))` +For instance consider the following term `T`: + + (x (\x (\y (x (y z))))) The first occurrence of `x` in T is free. The `\x` we won't regard as containing an occurrence of `x`. The next occurrence of `x` occurs -within a form that begins with `\x`, so it is bound as well. The +within a form `(\x (\y ...))` that begins with `\x`, so it is bound as well. The occurrence of `y` is bound; and the occurrence of `z` is free. -Note that the definition of *bound* is carefully crafted to guarantee -that in an expression like `(\a (x a))`, both occurrences of `a` count -as bound. - To read further: -* [[!wikipedia Free variables and bound variables]] +* [[!wikipedia Free variables and bound variables]] Here's an example of beta-reduction: - ((\x (y x)) z) + ((\x (y x)) z) beta-reduces to: - (y z) + (y z) We'll write that like this: - ((\x (y x)) z) ~~> (y z) + ((\x (y x)) z) ~~> (y z) -Different authors use different notations. Some authors use the term +Different authors use different terminology and notation. Some authors use the term "contraction" for a single reduction step, and reserve the term "reduction" for the reflexive transitive closure of that, that is, for zero or more reduction steps. Informally, it seems easiest to us to say "reduction" for one or more reduction steps. So when we write: - M ~~> N + M ~~> N + +we'll mean that you can get from M to N by one or more reduction +steps. -We'll mean that you can get from M to N by one or more reduction -steps. +When `M` and `N` are such that there is some common term (perhaps just one +of those two, or perhaps a third term) that they both reduce to, +we'll say that `M` and `N` are **beta-convertible**. We'll write that +like this: -There are many more details about the notation and the metatheory of + M <~~> N + +More details about the notation and metatheory of the lambda calculus here: * [[topics/_week2_lambda_calculus_fine_points.mdwn]] -Shorthand ---------- +## Shorthand ## The grammar we gave for the lambda calculus leads to some verbosity. There are several informal conventions in widespread use, @@ -146,97 +188,100 @@ as an exercise for those so inclined.) **Parentheses** Outermost parentheses around applications can be dropped. Moreover, applications will associate to the left, so `M N P` will be understood as `((M N) P)`. Finally, you can drop parentheses around abstracts, but not when they're part of an application. So you can abbreviate: - (\x (x y)) + (\x (x y)) as: - \x (x y) + \x (x y) but you should include the parentheses in: - (\x (x y)) z + (\x (x y)) z and: - z (\x (x y)) + z (\x (x y)) -**Dot notation** Dot means "put a left paren here, and put the right +**Dot notation** Dot means "assume a left paren here, and the matching right paren as far the right as possible without creating unbalanced parentheses". So: - \x (\y (x y)) + \x (\y (x y)) can be abbreviated as: - \x (\y. x y) + \x (\y. x y) and that as: - \x. \y. x y + \x. \y. x y This: - \x. \y. (x y) x + \x. \y. (x y) x abbreviates: - \x (\y ((x y) x)) + \x (\y ((x y) x)) This on the other hand: - (\x. \y. (x y)) x + (\x. \y. (x y)) x abbreviates: - ((\x (\y (x y))) x) + ((\x (\y (x y))) x) + +We didn't have to insert any parentheses around the inner body of `\y. (x y)` because they were already there. **Merging lambdas** An expression of the form `(\x (\y M))`, or equivalently, `(\x. \y. M)`, can be abbreviated as: - (\x y. M) + (\x y. M) Similarly, `(\x (\y (\z M)))` can be abbreviated as: - (\x y z. M) + (\x y z. M) -Lambda terms represent functions --------------------------------- +## Lambda terms represent functions ## Let's pause and consider the following fundamental question: what is a function? One popular answer is that a function can be represented by a set of ordered pairs. This set is called the **graph** of the -funtion. If the ordered pair `(a,b)` is a member of the graph if `f`, +function. If the ordered pair `(a, b)` is a member of the graph of `f`, that means that `f` maps the argument `a` to the value `b`. In -symbols, `f a == b`. +symbols, `f: a` ↦ `b`, or `f (a) == b`. -There is a requirement that in order to count as a function (rather -than as merely a more general relation), the graph cannot contain two +In order to count as a *function* (rather +than as merely a more general *relation*), we require that the graph not contain two (distinct) ordered pairs with the same first element. That is, in order to count as a proper function, each argument must correspond to a unique result. -The essential usefullness of the lambda calculus is that it is -wonderfully suited to representing functions. In fact, the untyped +The lambda calculus seems to be wonderfully well-suited for +representing functions. In fact, the untyped lambda calculus is Turing Complete (see [[!wikipedia Turing Completeness]]): all (recursively computable) functions can be represented by lambda -terms. (As we'll see, much of the fun will be in unpacking the word -"represented".) +terms. Which, by most people's lights, means that all functions we can "effectively decide" --- +that is, compute in a mechanical way without requiring any ingenuity or insight --- +can be represented by lambda terms. As we'll see, though, it will be fun +(that is, not straightforward) unpacking how these things can be so "represented." For some lambda terms, it is easy to see what function they represent: -> `(\x x)` represents the identity function: given any argument `M`, this function +> `(\x x)` represents the identity function: given any argument `M`, this function simply returns `M`: `((\x x) M) ~~> M`. -> `(\x (x x))` duplicates its argument: -`((\x (x x)) M) ~~> (M M)` +> `(\x (x x))` duplicates its argument (applies it to itself): +`((\x (x x)) M) ~~> (M M)` -> `(\x (\y (y x)))` reorders its two arguments: -`(((\x (\y (y x))) M) N) ~~> (N M)` +> `(\x (\y (y x)))` reorders its two arguments: +`(((\x (\y (y x))) M) N) ~~> (N M)` -> `(\x (\y x))` throws away its second argument: -`(((\x (\y x)) M) N) ~~> M` +> `(\x (\y x))` throws away its second argument: +`(((\x (\y x)) M) N) ~~> M` and so on. In order to get an intuitive feel for the power of the lambda calculus, note that duplicating, reordering, and deleting @@ -246,161 +291,179 @@ lambda term, it could take a representation of *Emma* as input and produce *Hamlet* as a result. Some of these functions are so useful that we'll give them special -names. In particular, we'll call the identity function `(\x.x)` -**I**, and the function `(\x\y.x)` **K**. +names. In particular, we'll call the identity function `(\x x)` +**I**, and the function `(\x (\y x))` **K** (for "konstant": K x is +a "constant function" that accepts any second argument `y` and ignores +it, always returning `x`). It is easy to see that distinct lambda expressions can represent the same function, considered as a mapping from input to outputs. Obviously: - (\x x) + (\x x) and: - (\z z) + (\z z) -both represent the same function, the identity function. However, we said above that we would be regarding these expressions as synactically equivalent, so they aren't yet really examples of *distinct* lambda expressions representing a single function. However, all three of these are distinct lambda expressions: +both represent the same function, the identity function. However, we said +(FIXME in the advanced notes) that we would be regarding these expressions as +synactically equivalent, so they aren't yet really examples of *distinct* +lambda expressions representing a single function. However, all three of these +are distinct lambda expressions: - (\y x. y x) (\z z) + (\y x. y x) (\z z) - (\x. (\z z) x) + (\x. (\z z) x) - (\z z) + (\z z) -yet when applied to any argument M, all of these will always return M. So they have the same extension. It's also true, though you may not yet be in a position to see, that no other function can differentiate between them when they're supplied as an argument to it. However, these expressions are all syntactically distinct. +yet when applied to any argument M, all of these will always return M. So they +have the same extension. It's also true, though you may not yet be in a +position to see, that no other function can differentiate between them when +they're supplied as an argument to it. However, these expressions are all +syntactically distinct. -The first two expressions are *convertible*: in particular the first +The first two expressions are (beta-)*convertible*: in particular the first reduces to the second via a single instance of beta reduction. So they can be regarded as proof-theoretically equivalent even though they're not syntactically identical. However, the proof theory we've given so far doesn't permit you to reduce the second expression to the third. So these lambda expressions are non-equivalent. -To use a linguistic analogy, you can think of what we're calling -"proof theory" as a kind of syntactic transformation. That is, the -sentences *John saw Mary* and *Mary was seen by John* are not -syntactically identical, yet (on some theories) one can be derived -from the other. The key element in the analogy is that this kind of -syntactic derivation is supposed to preserve meaning, so that the two -sentence mean (roughly) the same thing. +In other words, we have here different (non-equivalent) lambda terms with the +same extension. This introduces some tension between the popular way of +thinking of functions as represented by (identical to?) their graphs or +extensions, and the idea that lambda terms express functions. Well, perhaps the +lambda terms are just a finer-grained way of expressing functions than +extensions are? -There's an extension of the proof-theory we've presented so far which -does permit this further move. And in that extended proof theory, all -computable functions with the same extension do turn out to be -equivalent (convertible). However, at that point, we still wouldn't be -working with the traditional mathematical notion of a function as a -set of ordered pairs. One reason is that the fully general -mathematical notion permits many uncomputable functions, but the -lambda calculus is capable of expressing only computable functions. A -second reason is that the full mathematical notion prohibits -functions from applying to themselves, but in the lambda calculus, -it is permitted for a function to take itself as an argument (for -instance, there is nothing wrong with applying the identity function -to itself, thus: `(\x x)(\x x)`. This is a redex that reduces to the -identity function (of course). - - -The analogy with `let` ----------------------- +As we'll see, though, there are even further sources of +tension between the idea of functions-as-extensions and the idea of functions +embodied in the lambda calculus. -In our basic functional programming language, we used `let` -expressions to assign values to variables. For instance, - let x match 2 - in (x, x) +1. One reason is that that general +mathematical conception permits many *uncomputable* functions, but the +lambda calculus can't express those. -evaluates to the ordered pair (2, 2). It may be helpful to think of -a redex in the lambda calculus as a particular sort of `let` -construction. +2. More problematically, some lambda terms express "functions" that can take +themselves as arguments. If we wanted to represent that set-theoretically, and +identified functions with their extensions, then we'd have to have some +extension that contained (an ordered pair containing) itself as a member. Which +we're not allowed to do in mainstream set-theory. But in the lambda calculus +this is permitted and common --- and in fact will turn out to be indispensable. - ((\x M) N) is analogous to + Here are some simple examples. We can apply the identity function to itself: - let x match N - in M + (\x x) (\x x) -This analogy should be treated with caution. For one thing, our -`letrec` allowed us to define recursive functions in which the name of -the function appeared within the expression `N`; we're not ready to -deal with recursive functions in the lambda calculus yet. For -another, we defined `let` in terms of values, and at this point, the -lambda calculus doesn't have values, it only has other expressions. -So it might be better to translate `((\x M) N)` as `let x be replaced -by N in M`, or some such. Most problematically, in the lambda -calculus, an abstract such as `(\x (x x))` is perfectly well-formed -and coherent, but it is not possible to write a `let` expression that -does not have an argument (here, `N`) explicitly present. + This is a redex that reduces to the identity function (of course). We can +apply the **K** function to another argument and itself: -Nevertheless, the correspondence is close enough that it can spur -intuition. + > K z K + That is: -Booleans and pairs -================== + (\x (\y x)) z (\x (\y x)) -So if 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. We'll start with the `if ... then -... else...` construction. + That reduces to just `z`. - if M then N else L +3. As we'll see in coming weeks, some lambda terms will turn out to be +impossible to associate with any extension. This is related to the previous point. -For a boolean expression `M`, this complex expression evaluates to `N` -if `M` evaluates to `'true`, and to `L` if `M` evaluations to `'false. -So in order to simulate and `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 makes sense to add constants to the lambda -calculus. 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. +In fact it *does* turn out to be possible to represent the Lambda Calculus +set-theoretically. But not in the straightforward way that identifies +functions with their graphs. For years, it wasn't known whether it would be possible to do this. But then +[[!wikipedia Dana Scott]] figured out how to do it in late 1969, that is, +he formulated the first "denotational semantics" for this lambda calculus. +Scott himself had expected that this wouldn't be possible to do. He argued +for its unlikelihood in a paper he wrote only a month before the discovery. -So we'll need to get rid of the parts of the `if` statement that are -just syntactic window-dressing. That is, we'll need to get rid of the -`if`, the `then`, and the `else`: +(You can find an exposition of Scott's semantics in Chapters 15 and 16 of the +Hindley & Seldin book we recommended, and an exposition of some simpler +models that have since been discovered in Chapter 5 of the Hankin book, and +Section 16F of Hindley & Seldin. But realistically, you really ought to +wait a while and get more familiar with these systems before you'll be at all +ready to engage with those ideas.) - M N L +We've characterized the rule of beta-reduction as a kind of "proof theory" for +the Lambda Calculus. In fact, the usual proof theory is expressed in terms of +*convertibility* rather than in terms of reduction; but it's natural to +understand reduction as being the conceptually more fundamental notion. And +there are some proof theories for this system that are expressed in terms of +reduction. -Recall that our convention is that values associate left to right, so -this series of terms is evaluated as +To use a linguistic analogy, you can think of what we're calling +"proof theory" as a kind of syntactic transformation. The +sentences *John saw Mary* and *Mary was seen by John* are not +syntactically identical, yet (on some theories) one can be derived +from the other. The key element in the analogy is that this kind of +syntactic derivation is supposed to preserve meaning, so that the two +sentences mean (roughly) the same thing. - ((M N) L) +There's an extension of the proof-theory we've presented so far which +does permit a further move of just that sort. It would permit us to +also count these functions: -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 takes two arguments: first `N`, then `L`, -throws away `L`, and returns `N`. + (\x. (\z z) x) -We've already seen such a function. We called it **K**: `(\x\y.x)`. -Let's test: + (\z z) - ((K M) L) == (((\x\y.x) M) L) ~~> ((\y.M) L) ~~> M +as equivalent. This additional move is called **eta-reduction**. It's +crucial to eta-reduction that the outermost variable binding in the +abstract we begin with (here, `\x`) be of a variable that occurs free +exactly once in the body of that abstract, and that it be in the +rightmost position. -Sucess! In the same spirit, `'false` will be **KI**: +In the extended proof theory/theories we get be permitting eta-reduction/conversion +as well as beta-reduction, *all computable functions with the same +extension do turn out to be equivalent*, that is, convertible. - (((K I) M) L) == ((((\x\y.x)(\x.x)) M) L) - ~~> (((\y.(\x.x)) M) L) - ~~> ((\x.x) L) - ~~> L +However, we still shouldn't assume we're working with functions +traditionally conceived as just sets of ordered pairs, for the other +reasons sketched above. -So have seen our first major representation in the lambda calculus: -"true" is represented by **K**, and "false" is represented by **KI**. -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 -evaluate to (an expression that is alpha-beta-eta equivalent to) **K** -or **KI**. -Our definition of these is reviewed in [[Assignment 2|topics/assignment2]]. +## The analogy with `let` ## -It's possible to do the assignment without using a Scheme interpreter, however -you should take this opportunity to [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 test out -proposed answers to the assignment. +In our basic functional programming language, we used `let` +expressions to assign values to variables. For instance, + let x match 2 + in (x, x) -There's also a (slow, bare-bones, but perfectly adequate) version of Scheme available for online use at . +evaluates to the ordered pair (2, 2). It may be helpful to think of +a redex in the lambda calculus as a particular sort of `let` +construction. + + ((\x BODY) ARG) is analogous to + + let x match ARG + in BODY + +This analogy should be treated with caution. For one thing, our `letrec` +allowed us to define recursive functions in which the name `x` appeared within +`ARG`, but we wanted it there to be bound to the very same `ARG`. We're not +ready to deal with recursive functions in the lambda calculus yet. + +For another, we defined `let` in terms of *values*: we said that the variable +`x` was bound to whatever *value* `ARG` *evaluated to*. At this point, it's +not clear what the values are in the lambda calculus. We only have expressions. +(Though there was a suggestive remark earlier about some of the expressions but +not others being called "value terms".) + +So perhaps we should translate `((\x BODY) ARG)` in purely syntactic terms, +like: "let `x` be replaced by `ARG` in `BODY`"? + +Most problematically, in the lambda +calculus, an abstract such as `(\x (x x))` is perfectly well-formed +and coherent, but it is not possible to write a `let` expression that +does not have an `ARG`. That would be like: + +    `let x match` *missing* +    `in x x` + +Nevertheless, the correspondence is close enough that it can guide our +intuition.