1 ## Syntax of Lambda Calculus ##
3 We often talk about "*the* Lambda Calculus", as if there were just
4 one; but in fact, there are many, many variations. The one we will
5 start with, and will explore in some detail, is often called "the pure"
6 or "the untyped" Lambda Calculus. Actually, there are many variations even under
7 that heading. But all of the variations share a strong family
8 resemblance, so what we learn now will apply to all of them.
10 > Fussy note: calling this/these the "pure" lambda calculus is entrenched terminology,
11 but it coheres imperfectly with other uses of "pure" we'll encounter. There are
12 three respects in which the lambda calculus we'll be presenting might claim to
13 deserve the name "pure": (1) it has no pre-defined constants and a very spare
14 syntax; (2) it has no types; (3) it has no side-effects, and is insensitive to
15 the order of evaluation.
17 > Sense (3) corresponds most closely to the other uses of "pure" you'll
18 see in the surrounding literature. With respect to this point, it may be true that
19 this lambda calculus has no side effects. (Let's revisit that assumption
20 at the end of term.) But as we'll see next week, it is *not* true that it's insensitive
21 to the order of evaluation. So if that's what we mean by "pure", this lambda
22 calculus isn't as pure as you might hope to get. Some *typed* lambda calculi will
23 turn out to be more pure in that respect.
25 > But this lambda calculus is at least "pure" in sense (2). At least, it
26 doesn't *explicitly talk about* any types. Some prefer to say that this
27 lambda calculus *does* have types implicitly, it's just that
28 there's only one type, so that every expression is a member of
29 that one type. If you say that, you have to say that functions from
30 this type to this type also belong to this type. Which is weird... In
31 fact, though, such types are studied, under the name "recursive
32 types." More about these later in the seminar.
34 > Well, at least this lambda calculus is "pure" in sense (1). As we'll
35 see next week, though, there are some computational formal systems
36 whose syntax is *even more* spare, in that they don't even have variables.
37 So if that's what mean by "pure", again this lambda calculus isn't as pure
38 as you might hope to get.
40 > Still, if you say you're talking about "the pure" Lambda Calculus,
41 or "the untyped" Lambda Calculus, or even just "the" Lambda Calculus, this
42 is the system that people will understand you to be referring to.
47 <strong>Variables</strong>: <code>x</code>, <code>y</code>, <code>z</code>...
50 Each variable is an expression. For any expressions M and N and variable a, the following are also expressions:
53 <strong>Abstract</strong>: <code>(λa M)</code>
56 We'll tend to write <code>(λa M)</code> as just `(\a M)`, so we
57 don't have to write out the markup code for the
58 <code>λ</code>. You can yourself write <code>(λa
59 M)</code> or `(\a M)` or `(lambda a M)`.
62 <strong>Application</strong>: <code>(M N)</code>
65 Expressions in the lambda calculus are called "terms". Here is the
66 syntax of the lambda calculus given in the form of a context-free
79 Sometimes the first two production rules are further distinguished, and those
80 are called more specifically "value terms". Whereas Applications (terms of the
81 form `(M N)`) are terms but not value terms.
92 ((\x (x x)) (\x (x x)))
97 The lambda calculus has an associated proof theory. For now, we can regard the
98 proof theory as having just one rule, called the rule of **beta-reduction** or
99 "beta-contraction". Suppose you have some expression of the form:
103 This is an application whose first element is an abstract. This
104 compound form is called a **redex**, meaning it's a "beta-reducible
105 expression." `(\a M)` is called the **head** of the redex; `N` is
106 called the **argument**, and `M` is called the **body**.
108 The rule of beta-reduction permits a transition from that expression to the following:
112 What this means is just `M`, with any *free occurrences* inside `M` of
113 the variable `a` replaced with the term `N` (--- "without capture", which
114 we'll explain in the [[advanced notes|FIXME]]).
116 What is a free occurrence?
118 > Any occurrence of a variable `a` is **bound** in T when T has the form `(\a N)`.
120 > An occurrence of a variable `a` is **bound** in T when T has the form `(\b
121 N)` --- that is, with a *different* variable `b` --- just in case that
122 occurrence of `a` is bound in the subexpression `N`.
124 > When T has the form `(M N)`, any occurrences of `a` that are bound in the
125 subexpression `M` are also bound in T, and so too any occurrences of `a` that
126 are bound in the subexpression `N`.
128 > An occurrence of a variable is **free** if it's not bound.
130 For instance consider the following term `T`:
132 (x (\x (\y (x (y z)))))
134 The first occurrence of `x` in T is free. The `\x` we won't regard as
135 containing an occurrence of `x`. The next occurrence of `x` occurs
136 within a form `(\x (\y ...))` that begins with `\x`, so it is bound as well. The
137 occurrence of `y` is bound; and the occurrence of `z` is free.
141 * [[!wikipedia Free variables and bound variables]]
143 Here's an example of beta-reduction:
151 We'll write that like this:
153 ((\x (y x)) z) ~~> (y z)
155 Different authors use different terminology and notation. Some authors use the term
156 "contraction" for a single reduction step, and reserve the term
157 "reduction" for the reflexive transitive closure of that, that is, for
158 zero or more reduction steps. Informally, it seems easiest to us to
159 say "reduction" for one or more reduction steps. So when we write:
163 we'll mean that you can get from M to N by one or more reduction
166 When `M` and `N` are such that there is some common term (perhaps just one
167 of those two, or perhaps a third term) that they both reduce to,
168 we'll say that `M` and `N` are **beta-convertible**. We'll write that
173 More details about the notation and metatheory of
174 the lambda calculus here:
176 * [[topics/week2_lambda_advanced]]
181 The grammar we gave for the lambda calculus leads to some
182 verbosity. There are several informal conventions in widespread use,
183 which enable the language to be written more compactly. (If you like,
184 you could instead articulate a formal grammar which incorporates these
185 additional conventions. Instead of showing it to you, we'll leave it
186 as an exercise for those so inclined.)
189 **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:
197 but you should include the parentheses in:
206 **Dot notation** Dot means "assume a left paren here, and the matching right
207 paren as far the right as possible without creating unbalanced
212 can be abbreviated as:
228 This on the other hand:
236 We didn't have to insert any parentheses around the inner body of `\y. (x y)` because they were already there.
239 **Merging lambdas** An expression of the form `(\x (\y M))`, or equivalently, `(\x. \y. M)`, can be abbreviated as:
243 Similarly, `(\x (\y (\z M)))` can be abbreviated as:
248 ## Lambda terms represent functions ##
250 Let's pause and consider the following fundamental question: what is a
251 function? One popular answer is that a function can be represented by
252 a set of ordered pairs. This set is called the **graph** of the
253 function. If the ordered pair `(a, b)` is a member of the graph of `f`,
254 that means that `f` maps the argument `a` to the value `b`. In
255 symbols, `f: a` ↦ `b`, or `f (a) == b`.
257 In order to count as a *function* (rather
258 than as merely a more general *relation*), we require that the graph not contain two
259 (distinct) ordered pairs with the same first element. That is, in
260 order to count as a proper function, each argument must correspond to
263 The lambda calculus seems to be wonderfully well-suited for
264 representing functions. In fact, the untyped
265 lambda calculus is Turing Complete (see [[!wikipedia Turing Completeness]]):
266 all (recursively computable) functions can be represented by lambda
267 terms. Which, by most people's lights, means that all functions we can "effectively decide" ---
268 that is, compute in a mechanical way without requiring any ingenuity or insight ---
269 can be represented by lambda terms. As we'll see, though, it will be fun
270 (that is, not straightforward) unpacking how these things can be so "represented."
272 For some lambda terms, it is easy to see what function they represent:
274 > `(\x x)` represents the identity function: given any argument `M`, this function
275 simply returns `M`: `((\x x) M) ~~> M`.
277 > `(\x (x x))` duplicates its argument (applies it to itself):
278 `((\x (x x)) M) ~~> (M M)` <!-- **M** or ω; W is \uv.uvv -->
280 > `(\x (\y (y x)))` reorders its two arguments:
281 `(((\x (\y (y x))) M) N) ~~> (N M)` <!-- **T**; C is \uvx.uxv -->
283 > `(\x (\y x))` throws away its second argument:
284 `(((\x (\y x)) M) N) ~~> M` <!-- **K** -->
286 and so on. In order to get an intuitive feel for the power of the
287 lambda calculus, note that duplicating, reordering, and deleting
288 elements is all that it takes to simulate the behavior of a general
289 word processing program. That means that if we had a big enough
290 lambda term, it could take a representation of *Emma* as input and
291 produce *Hamlet* as a result.
293 Some of these functions are so useful that we'll give them special
294 names. In particular, we'll call the identity function `(\x x)`
295 **I**, and the function `(\x (\y x))` **K** (for "konstant": <code><b>K</b> x</code> is
296 a "constant function" that accepts any second argument `y` and ignores
297 it, always returning `x`).
299 It is easy to see that distinct lambda expressions can represent the same
300 function, considered as a mapping from input to outputs. Obviously:
308 both represent the same function, the identity function. However, we said
309 (FIXME in the advanced notes) that we would be regarding these expressions as
310 synactically equivalent, so they aren't yet really examples of *distinct*
311 lambda expressions representing a single function. However, all three of these
312 are distinct lambda expressions:
320 yet when applied to any argument M, all of these will always return M. So they
321 have the same extension. It's also true, though you may not yet be in a
322 position to see, that no other function can differentiate between them when
323 they're supplied as an argument to it. However, these expressions are all
324 syntactically distinct.
326 The first two expressions are (beta-)*convertible*: in particular the first
327 reduces to the second via a single instance of beta reduction. So they
328 can be regarded as proof-theoretically equivalent even though they're
329 not syntactically identical. However, the proof theory we've given so
330 far doesn't permit you to reduce the second expression to the
331 third. So these lambda expressions are non-equivalent.
333 In other words, we have here different (non-equivalent) lambda terms with the
334 same extension. This introduces some tension between the popular way of
335 thinking of functions as represented by (identical to?) their graphs or
336 extensions, and the idea that lambda terms express functions. Well, perhaps the
337 lambda terms are just a finer-grained way of expressing functions than
340 As we'll see, though, there are even further sources of
341 tension between the idea of functions-as-extensions and the idea of functions
342 embodied in the lambda calculus.
345 1. One reason is that that general
346 mathematical conception permits many *uncomputable* functions, but the
347 lambda calculus can't express those.
349 2. More problematically, some lambda terms express "functions" that can take
350 themselves as arguments. If we wanted to represent that set-theoretically, and
351 identified functions with their extensions, then we'd have to have some
352 extension that contained (an ordered pair containing) itself as a member. Which
353 we're not allowed to do in mainstream set-theory. But in the lambda calculus
354 this is permitted and common --- and in fact will turn out to be indispensable.
356 Here are some simple examples. We can apply the identity function to itself:
360 This is a redex that reduces to the identity function (of course). We can
361 apply the **K** function to another argument and itself:
363 > <code><b>K</b> z <b>K</b></code>
367 (\x (\y x)) z (\x (\y x))
369 That reduces to just `z`.
371 3. As we'll see in coming weeks, some lambda terms will turn out to be
372 impossible to associate with any extension. This is related to the previous point.
375 In fact it *does* turn out to be possible to represent the Lambda Calculus
376 set-theoretically. But not in the straightforward way that identifies
377 functions with their graphs. For years, it wasn't known whether it would be possible to do this. But then
378 [[!wikipedia Dana Scott]] figured out how to do it in late 1969, that is,
379 he formulated the first "denotational semantics" for this lambda calculus.
380 Scott himself had expected that this wouldn't be possible to do. He argued
381 for its unlikelihood in a paper he wrote only a month before the discovery.
383 (You can find an exposition of Scott's semantics in Chapters 15 and 16 of the
384 Hindley & Seldin book we recommended, and an exposition of some simpler
385 models that have since been discovered in Chapter 5 of the Hankin book, and
386 Section 16F of Hindley & Seldin. But realistically, you really ought to
387 wait a while and get more familiar with these systems before you'll be at all
388 ready to engage with those ideas.)
390 We've characterized the rule of beta-reduction as a kind of "proof theory" for
391 the Lambda Calculus. In fact, the usual proof theory is expressed in terms of
392 *convertibility* rather than in terms of reduction; but it's natural to
393 understand reduction as being the conceptually more fundamental notion. And
394 there are some proof theories for this system that are expressed in terms of
397 To use a linguistic analogy, you can think of what we're calling
398 "proof theory" as a kind of syntactic transformation. The
399 sentences *John saw Mary* and *Mary was seen by John* are not
400 syntactically identical, yet (on some theories) one can be derived
401 from the other. The key element in the analogy is that this kind of
402 syntactic derivation is supposed to preserve meaning, so that the two
403 sentences mean (roughly) the same thing.
405 There's an extension of the proof-theory we've presented so far which
406 does permit a further move of just that sort. It would permit us to
407 also count these functions:
413 as equivalent. This additional move is called **eta-reduction**. It's
414 crucial to eta-reduction that the outermost variable binding in the
415 abstract we begin with (here, `\x`) be of a variable that occurs free
416 exactly once in the body of that abstract, and that it be in the
419 In the extended proof theory/theories we get be permitting eta-reduction/conversion
420 as well as beta-reduction, *all computable functions with the same
421 extension do turn out to be equivalent*, that is, convertible.
423 However, we still shouldn't assume we're working with functions
424 traditionally conceived as just sets of ordered pairs, for the other
425 reasons sketched above.
428 ## The analogy with `let` ##
430 In our basic functional programming language, we used `let`
431 expressions to assign values to variables. For instance,
436 evaluates to the ordered pair (2, 2). It may be helpful to think of
437 a redex in the lambda calculus as a particular sort of `let`
440 ((\x BODY) ARG) is analogous to
445 This analogy should be treated with caution. For one thing, our `letrec`
446 allowed us to define recursive functions in which the name `x` appeared within
447 `ARG`, but we wanted it there to be bound to the very same `ARG`. We're not
448 ready to deal with recursive functions in the lambda calculus yet.
450 For another, we defined `let` in terms of *values*: we said that the variable
451 `x` was bound to whatever *value* `ARG` *evaluated to*. At this point, it's
452 not clear what the values are in the lambda calculus. We only have expressions.
453 (Though there was a suggestive remark earlier about some of the expressions but
454 not others being called "value terms".)
456 So perhaps we should translate `((\x BODY) ARG)` in purely syntactic terms,
457 like: "let `x` be replaced by `ARG` in `BODY`"?
459 Most problematically, in the lambda
460 calculus, an abstract such as `(\x (x x))` is perfectly well-formed
461 and coherent, but it is not possible to write a `let` expression that
462 does not have an `ARG`. That would be like:
464 `let x match` *missing*
465 `in x x`
467 Nevertheless, the correspondence is close enough that it can guide our