starting lecture on types
[lambda.git] / topics / week2_lambda_advanced.mdwn
1 ## Advanced notes on the Lambda Calculus ##
2
3 Hankin uses the symbol
4 <code><big><big>&rarr;</big></big></code> for one-step contraction,
5 and the symbol <code><big><big>&#8608;</big></big></code> for
6 zero-or-more step reduction. Hindley and Seldin use
7 <code><big><big><big>&#8883;</big></big></big><sub>1</sub></code> and
8 <code><big><big><big>&#8883;</big></big></big></code>.
9
10 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:
11
12     M <~~> N
13
14 This is what plays the role of equality in the lambda calculus. Hankin
15 uses the symbol `=` for this. So too do Hindley and
16 Seldin. Personally, we keep confusing that with the relation to be
17 described next, so let's use the `<~~>` notation instead. Note that
18 `M <~~> N` doesn't mean that each of `M` and `N` are reducible to each other;
19 that only holds when `M` and `N` are the same expression. (Or, with
20 our convention of only saying "reducible" for one or more reduction
21 steps, it never holds.)
22
23 In the metatheory, it's also sometimes useful to talk about formulas
24 that are syntactically equivalent *before any reductions take
25 place*. Hankin uses the symbol <code>&equiv;</code> for this. So too
26 do Hindley and Seldin. We'll use that too, and will avoid using `=`
27 when discussing the metatheory. Instead we'll use `<~~>` as we said
28 above. When we want to introduce a stipulative definition, we'll write
29 it out longhand, as in:
30
31 > `T` is defined to be `(M N)`.
32
33 or:
34
35 > Let `T` be `(M N)`.
36
37 We'll regard the following two expressions:
38
39     (\x (x y))
40
41     (\z (z y))
42
43 as syntactically equivalent, since they only involve a typographic
44 change of a bound variable. Read Hankin Section 2.3 for discussion of
45 different attitudes one can take about this.
46
47 Note that neither of the above expressions are identical to:
48
49     (\x (x w))
50
51 because here it's a free variable that's been changed. Nor are they identical to:
52
53     (\y (y y))
54
55 because here the second occurrence of `y` is no longer free.
56
57 There is plenty of discussion of this, and the fine points of how
58 substitution works, in Hankin and in various of the tutorials we'll
59 link to about the lambda calculus. We expect you have a good
60 intuitive understanding of what to do already, though, even if you're
61 not able to articulate it rigorously.
62
63
64 ## Substitution and Alpha-Conversion ##
65
66 Intuitively, (a) and (b) express the application of the same function to the argument `y`:
67
68 <OL type=a>
69 <LI><code>(\x. \z. z x) y</code>
70 <LI><code>(\x. \y. y x) y</code>
71 </OL>
72
73 One can't just rename variables freely. (a) and (b) are different than what's expressed by:
74
75 <OL type=a start=3>
76 <LI><code>(\z. (\z. z z) y</code>
77 </OL>
78
79
80 Substituting `y` into the body of (a) `(\x. \z. z x)` is unproblematic:
81
82     (\x. \z. z x) y ~~> \z. z y
83
84 However, with (b) we have to be more careful. If we just substituted blindly,
85 then we might take the result to be `\y. y y`. But this is the self-application
86 function, not the function which accepts an arbitrary argument and applies that
87 argument to the free variable `y`. In fact, the self-application function is
88 what (c) reduces to. So if we took (b) to reduce to `\y. y y`, we'd wrongly be
89 counting (b) to be equivalent to (c), instead of (a).
90
91 To reduce (b), then, we need to be careful to that no free variables in what
92 we're substituting in get "captured" by binding &lambda;s that they shouldn't be
93 captured by.
94
95 In practical terms, you'd just replace (b) with (a) and do the unproblematic substitution into (a).
96
97 How should we think about the explanation and justification for that practical procedure?
98
99 One way to think about things here is to identify expressions of the lambda
100 calculus with *particular alphabetic sequences*. Then (a) and (b) would be
101 distinct expressions, and we'd have to have an explicit rule permitting us to
102 do the kind of variable-renaming that takes us from (a) to (b) (or vice versa).
103 This kind of renaming is called "alpha-conversion." Look in the standard
104 treatments of the lambda calculus for detailed discussion of this.
105
106 Another way to think of it is to identify expressions not with particular
107 alphabetic sequences, but rather with *classes* of alphabetic sequences, which
108 stand to each other in the way that (a) and (b) do. That's the way we'll talk.
109 We say that (a) and (b) are just typographically different notations for a
110 *single* lambda term. As we'll say, the lambda term written with (a) and
111 the lambda term written with (b) are literally syntactically identical.
112
113 A third way to think is to identify the lambda term not with classes of
114 alphabetic sequences, but rather with abstract structures that we might draw
115 like this:
116
117 <pre><code>
118     (&lambda;. &lambda;. _ _) y
119      ^  ^  | |
120      |  |__| |
121      |_______|
122 </code></pre>
123
124 Here there are no bound variables, but *bound positions* remain. We can
125 regard formula like (a) and (b) as just helpfully readable ways to designate
126 these abstract structures.
127
128 A version of this last approach is known as [de Bruijn notation](http://en.wikipedia.org/wiki/De_Bruijn_index) for the lambda calculus.
129
130 It doesn't seem to matter which of these approaches one takes; the logical
131 properties of the systems are exactly the same. It just affects the particulars
132 of how one states the rules for substitution, and so on. And whether one talks
133 about expressions being literally "syntactically identical," or whether one
134 instead counts them as "equivalent modulu alpha-conversion."
135
136 (Linguistic trivia: some linguistic discussions do suppose that
137 alphabetic variance has important linguistic consequences; see Ivan Sag's
138 dissertation.)
139
140 Next week, we'll discuss other systems that lack variables. Those systems will
141 not just lack variables in the sense that de Bruijn notation does; they will
142 furthermore lack any notion of a bound position.
143
144
145 ## Review: syntactic equality, reduction, convertibility ##
146
147 Define `N` to be `(\x. x y) z`. Then `N` and `(\x. x y) z` are syntactically equal,
148 and we're counting them as syntactically equal to `(\z. z y) z` as well. We'll express
149 all these claims in our metalanguage as:
150
151 <pre><code>N &equiv; (\x. x y) z &equiv; (\z. z y) z
152 </code></pre>
153
154 This:
155
156     N ~~> z y
157
158 means that `N` beta-reduces to `z y`. This:
159
160     M <~~> N
161
162 means that `M` and `N` are beta-convertible, that is, that there's some common term they both reduce to in zero or more steps.
163
164 The symbols `~~>` and `<~~>` aren't part of what we're calling "the Lambda
165 Calculus". In our mouths, they're just part of our metatheory for talking about it. In the uses of
166 the Lambda Calculus as a formal proof theory, one or the other of these
167 symbols (or some notational variant of them) *is* added to the object language. But only in outermost contexts. It's like the "sequent" symbol (written `=>` or <code>&vdash;</code>) in [Gentzen-style proof systems](https://en.wikipedia.org/wiki/Sequent_calculus) for logic. You can't embed the `~~>` or `<~~>` symbol inside lambda terms.
168
169 See Hankin Sections 2.2 and 2.4 for the proof theory using `<~~>` (which he
170 writes as `=`).  He discusses the proof theory using `~~>` in his Chapter 3.
171 This material is covered in slightly different ways (different organization and
172 some differences in terminology and notation) in Chapters 1, 6, and 7 of the
173 Hindley &amp; Seldin.
174