add anchors
[lambda.git] / topics / week3_what_is_computation.mdwn
1 #What is computation?#
2
3 The folk notion of computation involves taking a complicated
4 expression and replacing it with an equivalent simpler expression.
5
6     3 + 4 == 7
7
8 This equation can be interpreted as expressing the thought that the
9 complex expression `3 + 4` evaluates to `7`.  In this case, the
10 evaluation of the expression involves computing a sum.  There is a
11 clear sense in which the expression `7` is simpler than the expression
12 `3 + 4`: `7` is syntactically simple, and `3 + 4` is syntactically
13 complex.
14
15 It's worth pausing a moment and wondering why we feel that replacing a
16 complex expression like `3 + 4` with a simplex expression like `7`
17 feels like we've accomplished something.  If they really are
18 equivalent, why shouldn't we consider them to be equally valuable, or
19 even to prefer the longer expression?  For instance, should we prefer
20 2^9, or 512?  Likewise, in the realm of logic, why shold we ever
21 prefer `B` to the conjunction of `A` with `A --> B`?
22
23 The question to ask here is whether our intuitions about what counts
24 as more evaluated always tracks simplicity of expression, or whether
25 it tracks what is more useful to us in a given larger situation.
26
27 But even deciding which expression ought to count as simpler is not
28 always so clear.
29
30 ##Church arithmetic##
31
32 In our system of computing with Church encodings, we have the
33 following representations:
34
35     3 :=  \f z . f (f (f z))
36     4 :=  \f z . f (f (f (f z)))
37     7 :=  \f z . f (f (f (f (f (f (f z))))))
38
39 [By the way, those of you who did the extra credit problem on homework
40 2, solving the reverse function with Oleg's holes---do you see the
41 resemblance?]
42
43 The addition of 3 and 4, then, clearly needs to just tack 4's string
44 of f's onto the end of 3's string of f's.  This suggests the following
45 implementation of the arithmetic addition:
46
47     + := \lrfz.lf(rfz)
48
49     + 3 4 == (\lrfz.lf(rfz)) 3 4
50           ~~> \fz.3f(4fz)
51           ==  \fz.(\fz.f(f(fz))) f (4fz)
52           ~~> \fz.f(f(f(4fz)))
53           ==  \fz.f(f(f((\fz.f(f(f(fz))) f z))))
54           ~~> \fz.f(f(f(f(f(f(fz))))))
55           ==  7
56
57 as desired.  Is there still a sense in which the encoded version of `3
58 + 4` is simpler than the encoded version of `7`?  Well, yes: once the
59 numerals `3` and `4` have been replaced with their encodings, the
60 encoding of `3 + 4` contains more symbols than the encoding of `7`.
61
62 But now consider multiplication:
63
64     * := \lrf.l(rf)
65     * 3 4 :=  (\lrf.l(rf)) 3 4
66           ~~> (\rf.3(rf)) 4
67           ~~> \f.3(4f)
68           ==  \f.(\fz.f(f(fz)))(4f)
69           ~~> \fz.(4f)((4f)((4f)z))
70           ==  \fz.((\fz.f(f(f(fz))))f) (((\fz.f(f(f(fz))))f) (((\fz.f(f(f(fz))))f) z))
71           ~~> \fz.((\z.f(f(f(fz)))) ((\z.f(f(f(fz)))) ((\z.f(f(f(fz))))z)))
72           ~~> \fz.((\z.f(f(f(fz)))) ((\z.f(f(f(fz)))) (f(f(f(fz))))))
73           ~~> \fz.((\z.f(f(f(fz)))) (f(f(f(f(f(f(f(fz)))))))))
74           ~~> \fz.f(f(f(f(f(f(f(f(f(f(f(fz)))))))))))
75           ==  12
76
77 Is the final result simpler?  This time, the answer is not so straightfoward.
78 Compare the starting expression with the final expression:
79
80         *           3             4 
81         (\lrf.l(rf))(\fz.f(f(fz)))(\fz.f(f(f(fz))))
82     ~~> 12
83         (\fz.f(f(f(f(f(f(f(f(f(f(f(fz))))))))))))
84
85 And if we choose different numbers, the result is even less clear:
86
87         *           3             6 
88         (\lrf.l(rf))(\fz.f(f(fz)))(\fz.f(f(f(f(f(fz))))))
89     ~~> 18
90         (\fz.f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(fz))))))))))))))))))
91
92 On the on hand, there are more symbols in the encoding of `18` than in
93 the encoding of `* 3 6`.  On the other hand, there is more complexity
94 (speaking pre-theoretically) in the encoding of `* 3 6`, since the
95 encoding of `18` is just a uniform sequence of nested `f`'s.
96
97 This example shows that computation can't be just simplicity as
98 measured by the number of symbols in the representation.  There is
99 still some sense in which the evaluated expression is simpler, but the
100 right way to characterize "simpler" is elusive.
101
102 One possibility is to define simpler in terms of irreversability.  The
103 reduction rules of the lambda calculus define an asymmetric relation
104 over terms: for any given redex, there is a unique reduced term (up to
105 alphabetic variance).  But for any given term, there are many redexes
106 that reduce to that term.
107
108     ((\x.xx)y) ~~> yy
109     ((\xx)yy) ~~> yy
110     (y((\xx)y)) ~~> yy
111     etc.
112
113 Likewise, in the arithmetic example, there is only one number that
114 corresponds to the sum of 3 and 4 (namely, 7).  But there are many
115 sums that add up to 7: 3+4, 4+3, 5+2, 2+5, 6+1, 1+6, etc.
116
117 So the unevaluated expression contains information that is missing
118 from the evaluated value: information about *how* that value was
119 arrived at.  So this suggests the following way of thinking about what
120 counts as evaluated:
121
122     Given two expressions such that one reduces to the other,
123     the more evaluated one is the one that contains less information.
124
125 This definition is problematic, though, if we try to define the amount
126 of information using, say, [[!wikipedia Komolgorov complexity]].
127
128 Ultimately, we have to decide that the reduction rules determine what
129 counts as evaluated.  If we're lucky, that will align well with our
130 intuitive desires about what should count as simpler.  But we're not
131 always lucky.  In particular, although beta reduction in general lines
132 up well with our intuitive sense of simplification, there are
133 pathological examples where the results do not align so well:
134
135     (\x.xx)(\x.xx) ~~> (\x.xx)(\x.xx) ~~> (\x.xx)(\x.xx)
136
137 In this example, reduction returns the exact same lambda term.  There
138 is no simplification at all.
139
140     (\x.xxx)(\x.xxx) ~~> ((\x.xxxx)(\x.xxxx)(\x.xxxx)) 
141
142 Even worse, in this case, the "reduced" form is longer and more
143 complex by any measure.
144
145 We may have to settle for the idea that a well-chosen reduction system
146 will characterize our intuitive notion of evaluation in most cases, or
147 in some useful class of non-pathological cases.  
148
149 These are some of the deeper issues to keep in mind as we discuss the
150 ins and outs of reduction strategies.