add Prelude
[lambda.git] / topics / _week2.mdwn
1 *This page is not ready to go live; just roughly copying over some material from last year.*
2
3
4 Here's what we did in seminar on Monday 9/13,
5
6 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.
7
8 [Linguistic and Philosophical Applications of the Tools We'll be Studying](/applications)
9 ==========================================================================
10
11 [Explanation of the "Damn" example shown in class](/damn)
12
13 Basics of Lambda Calculus
14 =========================
15
16 The lambda calculus we'll be focusing on for the first part of the course has no types. (Some prefer to say it instead has a single type---but 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 type." More about these later in the seminar.)
17
18 Here is its syntax:
19
20 <blockquote>
21 <strong>Variables</strong>: <code>x</code>, <code>y</code>, <code>z</code>...
22 </blockquote>
23
24 Each variable is an expression. For any expressions M and N and variable a, the following are also expressions:
25
26 <blockquote>
27 <strong>Abstract</strong>: <code>(&lambda;a M)</code>
28 </blockquote>
29
30 We'll tend to write <code>(&lambda;a M)</code> as just `(\a M)`, so we don't have to write out the markup code for the <code>&lambda;</code>. You can yourself write <code>(&lambda;a M)</code> or `(\a M)` or `(lambda a M)`.
31
32 <blockquote>
33 <strong>Application</strong>: <code>(M N)</code>
34 </blockquote>
35
36
37 Examples of expressions:
38
39         x
40         (y x)
41         (x x)
42         (\x y)
43         (\x x)
44         (\x (\y x))
45         (x (\x x))
46         ((\x (x x)) (\x (x x)))
47
48 The lambda calculus has an associated proof theory. For now, we can regard the
49 proof theory as having just one rule, called the rule of **beta-reduction** or
50 "beta-contraction". Suppose you have some expression of the form:
51
52         ((\a M) N)
53
54 that is, an application of an abstract to some other expression. This compound form is called a **redex**, meaning it's a "beta-reducible expression." `(\a M)` is called the **head** of the redex; `N` is called the **argument**, and `M` is called the **body**.
55
56 The rule of beta-reduction permits a transition from that expression to the following:
57
58         M [a:=N]
59
60 What this means is just `M`, with any *free occurrences* inside `M` of the variable `a` replaced with the term `N`.
61
62 What is a free occurrence?
63
64 >       An occurrence of a variable `a` is **bound** in T if T has the form `(\a N)`.
65
66 >       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`.
67
68 >       An occurrence of a variable is **free** if it's not bound.
69
70 For instance:
71
72
73 >       T is defined to be `(x (\x (\y (x (y z)))))`
74
75 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 occurrence of `y` is bound; and the occurrence of `z` is free.
76
77 To read further:
78
79 *       [[!wikipedia Free variables and bound variables]]
80
81 Here's an example of beta-reduction:
82
83         ((\x (y x)) z)
84
85 beta-reduces to:
86
87         (y z)
88
89 We'll write that like this:
90
91         ((\x (y x)) z) ~~> (y z)
92
93 Different authors use different notations. 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:
94
95         M ~~> N
96
97 We'll mean that you can get from M to N by one or more reduction steps. Hankin uses the symbol <code><big><big>&rarr;</big></big></code> for one-step contraction, and the symbol <code><big><big>&#8608;</big></big></code> for zero-or-more step reduction. Hindley and Seldin use <code><big><big><big>&#8883;</big></big></big><sub>1</sub></code> and <code><big><big><big>&#8883;</big></big></big></code>.
98
99 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:
100
101         M <~~> N
102
103 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; 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.)
104
105 In the metatheory, it's also sometimes useful to talk about formulas that are syntactically equivalent *before any reductions take place*. Hankin uses the symbol <code>&equiv;</code> for this. So too do Hindley and Seldin. We'll use that too, and will avoid using `=` 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:
106
107 >       T is defined to be `(M N)`.
108
109 We'll regard the following two expressions:
110
111         (\x (x y))
112
113         (\z (z y))
114
115 as syntactically equivalent, since they only involve a typographic change of a bound variable. Read Hankin section 2.3 for discussion of different attitudes one can take about this.
116
117 Note that neither of those expressions are identical to:
118
119         (\x (x w))
120
121 because here it's a free variable that's been changed. Nor are they identical to:
122
123         (\y (y y))
124
125 because here the second occurrence of `y` is no longer free.
126
127 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 intuitive understanding of what to do already, though, even if you're not able to articulate it rigorously.
128
129 *       [More discussion in week 2 notes](/week2/#index1h1)
130
131
132 Shorthand
133 ---------
134
135 The grammar we gave for the lambda calculus leads to some verbosity. There are several informal conventions in widespread use, which enable the language to be written more compactly. (If you like, you could instead articulate a formal grammar which incorporates these additional conventions. Instead of showing it to you, we'll leave it as an exercise for those so inclined.)
136
137
138 **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:
139
140         (\x (x y))
141
142 as:
143
144         \x (x y)
145
146 but you should include the parentheses in:
147
148         (\x (x y)) z
149
150 and:
151
152         z (\x (x y))
153
154
155 **Dot notation** Dot means "put a left paren here, and put the right
156 paren as far the right as possible without creating unbalanced
157 parentheses". So:
158
159         \x (\y (x y))
160
161 can be abbreviated as:
162
163         \x (\y. x y)
164
165 and that as:
166
167         \x. \y. x y
168
169 This:
170
171         \x. \y. (x y) x
172
173 abbreviates:
174
175         \x (\y ((x y) x))
176
177 This on the other hand:
178
179         (\x. \y. (x y)) x
180
181 abbreviates:
182
183         ((\x (\y (x y))) x)
184
185
186 **Merging lambdas** An expression of the form `(\x (\y M))`, or equivalently, `(\x. \y. M)`, can be abbreviated as:
187
188         (\x y. M)
189
190 Similarly, `(\x (\y (\z M)))` can be abbreviated as:
191
192         (\x y z. M)
193
194
195 Lambda terms represent functions
196 --------------------------------
197
198 The untyped lambda calculus is Turing complete: all (recursively computable) functions can be represented by lambda terms. For some lambda terms, it is easy to see what function they represent:
199
200 >       `(\x x)` represents the identity function: given any argument `M`, this function
201 simply returns `M`: `((\x x) M) ~~> M`.
202
203 >       `(\x (x x))` duplicates its argument:
204 `((\x (x x)) M) ~~> (M M)`
205
206 >       `(\x (\y x))` throws away its second argument:
207 `(((\x (\y x)) M) N) ~~> M`
208
209 and so on.
210
211 It is easy to see that distinct lambda expressions can represent the same
212 function, considered as a mapping from input to outputs. Obviously:
213
214         (\x x)
215
216 and:
217
218         (\z z)
219
220 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:
221
222         (\y x. y x) (\z z)
223
224         (\x. (\z z) x)
225
226         (\z z)
227
228 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.
229
230 The first two expressions are *convertible*: in particular the first reduces to the second. 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.
231
232 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 won't be working with the traditional mathematical notion of a function as a set of ordered pairs. One reason is that the latter but not the former permits many uncomputable functions. A second reason is that the latter but not the former prohibits functions from applying to themselves. We discussed this some at the end of Monday's meeting (and further discussion is best pursued in person).
233
234
235
236 Booleans and pairs
237 ==================
238
239 Our definition of these is reviewed in [[Assignment1]].
240
241
242 It's possible to do the assignment without using a Scheme interpreter, however
243 you should take this opportunity to [get Scheme installed on your
244 computer](/how_to_get_the_programming_languages_running_on_your_computer), and
245 [get started learning Scheme](/learning_scheme). It will help you test out
246 proposed answers to the assignment.
247
248
249 There's also a (slow, bare-bones, but perfectly adequate) version of Scheme available for online use at <http://tryscheme.sourceforge.net/>.
250