week6 tweaks
[lambda.git] / week6.mdwn
1 [[!toc]]
2
3 Polymorphic Types and System F
4 ------------------------------
5
6 [Notes still to be added. Hope you paid attention during seminar.]
7
8
9 Types in OCaml
10 --------------
11
12 OCaml has type inference: the system can often infer what the type of
13 an expression must be, based on the type of other known expressions.
14
15 For instance, if we type
16
17     # let f x = x + 3;;
18
19 The system replies with
20
21     val f : int -> int = <fun>
22
23 Since `+` is only defined on integers, it has type
24
25      # (+);;
26      - : int -> int -> int = <fun>
27
28 The parentheses are there to turn off the trick that allows the two
29 arguments of `+` to surround it in infix (for linguists, SOV) argument
30 order. That is,
31
32     # 3 + 4 = (+) 3 4;;
33     - : bool = true
34
35 In general, tuples with one element are identical to their one
36 element:
37
38     # (3) = 3;;
39     - : bool = true
40
41 though OCaml, like many systems, refuses to try to prove whether two
42 functional objects may be identical:
43
44     # (f) = f;;
45     Exception: Invalid_argument "equal: functional value".
46
47 Oh well.
48
49 [Note: There is a limited way you can compare functions, using the
50 `==` operator instead of the `=` operator. Later when we discuss mutation,
51 we'll discuss the difference between these two equality operations.
52 Scheme has a similar pair, which they name `eq?` and `equal?`. In Python,
53 these are `is` and `==` respectively. It's unfortunate that OCaml uses `==` for the opposite operation that Python and many other languages use it for. In any case, OCaml will accept `(f) == f` even though it doesn't accept
54 `(f) = f`. However, don't expect it to figure out in general when two functions
55 are equivalent. (That question is not Turing computable.)
56
57         # (f) == (fun x -> x + 3);;
58         - : bool = false
59
60 Here OCaml says (correctly) that the two functions don't stand in the `==` relation, which basically means they're not represented in the same chunk of memory. However as the programmer can see, the functions are extensionally equivalent. The meaning of `==` is rather weird.]
61
62
63
64 Booleans in OCaml, and simple pattern matching
65 ----------------------------------------------
66
67 Where we would write `true 1 2` in our pure lambda calculus and expect
68 it to evaluate to `1`, in OCaml boolean types are not functions
69 (equivalently, they're functions that take zero arguments). Instead, selection is
70 accomplished as follows:
71
72     # if true then 1 else 2;;
73     - : int = 1
74
75 The types of the `then` clause and of the `else` clause must be the
76 same.
77
78 The `if` construction can be re-expressed by means of the following
79 pattern-matching expression:
80
81     match <bool expression> with true -> <expression1> | false -> <expression2>
82
83 That is,
84
85     # match true with true -> 1 | false -> 2;;
86     - : int = 1
87
88 Compare with
89
90     # match 3 with 1 -> 1 | 2 -> 4 | 3 -> 9;;
91     - : int = 9
92
93 Unit and thunks
94 ---------------
95
96 All functions in OCaml take exactly one argument.  Even this one:
97
98     # let f x y = x + y;;
99     # f 2 3;;
100     - : int = 5
101
102 Here's how to tell that `f` has been curry'd:
103
104     # f 2;;
105     - : int -> int = <fun>
106
107 After we've given our `f` one argument, it returns a function that is
108 still waiting for another argument.
109
110 There is a special type in OCaml called `unit`.  There is exactly one
111 object in this type, written `()`.  So
112
113     # ();;
114     - : unit = ()
115
116 Just as you can define functions that take constants for arguments
117
118     # let f 2 = 3;;
119     # f 2;;
120     - : int = 3;;
121
122 you can also define functions that take the unit as its argument, thus
123
124     # let f () = 3;;
125     val f : unit -> int = <fun>
126
127 Then the only argument you can possibly apply `f` to that is of the
128 correct type is the unit:
129
130     # f ();;
131     - : int = 3
132
133 Now why would that be useful?
134
135 Let's have some fun: think of `rec` as our `Y` combinator.  Then
136
137     # let rec f n = if (0 = n) then 1 else (n * (f (n - 1)));;
138     val f : int -> int = <fun>
139     # f 5;;
140     - : int = 120
141
142 We can't define a function that is exactly analogous to our &omega;.
143 We could try `let rec omega x = x x;;` what happens?
144
145 [Note: if you want to learn more OCaml, you might come back here someday and try:
146
147         # let id x = x;;
148         val id : 'a -> 'a = <fun>
149         # let unwrap (`Wrap a) = a;;
150         val unwrap : [< `Wrap of 'a ] -> 'a = <fun>
151         # let omega ((`Wrap x) as y) = x y;;
152         val omega : [< `Wrap of [> `Wrap of 'a ] -> 'b as 'a ] -> 'b = <fun>
153         # unwrap (omega (`Wrap id)) == id;;
154         - : bool = true
155         # unwrap (omega (`Wrap omega));;
156     <Infinite loop, need to control-c to interrupt>
157
158 But we won't try to explain this now.]
159
160
161 Even if we can't (easily) express omega in OCaml, we can do this:
162
163     # let rec blackhole x = blackhole x;;
164
165 By the way, what's the type of this function?
166
167 If you then apply this `blackhole` function to an argument,
168
169     # blackhole 3;;
170
171 the interpreter goes into an infinite loop, and you have to type control-c
172 to break the loop.
173
174 Oh, one more thing: lambda expressions look like this:
175
176     # (fun x -> x);;
177     - : 'a -> 'a = <fun>
178     # (fun x -> x) true;;
179     - : bool = true
180
181 (But `(fun x -> x x)` still won't work.)
182
183 You may also see this:
184
185         # (function x -> x);;
186         - : 'a -> 'a = <fun>
187
188 This works the same as `fun` in simple cases like this, and slightly differently in more complex cases. If you learn more OCaml, you'll read about the difference between them.
189
190 We can try our usual tricks:
191
192     # (fun x -> true) blackhole;;
193     - : bool = true
194
195 OCaml declined to try to fully reduce the argument before applying the
196 lambda function. Question: Why is that? Didn't we say that OCaml is a call-by-value/eager language?
197
198 Remember that `blackhole` is a function too, so we can
199 reverse the order of the arguments:
200
201     # blackhole (fun x -> true);;
202
203 Infinite loop.
204
205 Now consider the following variations in behavior:
206
207     # let test = blackhole blackhole;;
208     <Infinite loop, need to control-c to interrupt>
209
210     # let test () = blackhole blackhole;;
211     val test : unit -> 'a = <fun>
212
213     # test;;
214     - : unit -> 'a = <fun>
215
216     # test ();;
217     <Infinite loop, need to control-c to interrupt>
218
219 We can use functions that take arguments of type `unit` to control
220 execution.  In Scheme parlance, functions on the `unit` type are called
221 *thunks* (which I've always assumed was a blend of "think" and "chunk").
222
223 Question: why do thunks work? We know that `blackhole ()` doesn't terminate, so why do expressions like:
224
225         let f = fun () -> blackhole ()
226         in true
227
228 terminate?
229
230 Bottom type, divergence
231 -----------------------
232
233 Expressions that don't terminate all belong to the **bottom type**. This is a subtype of every other type. That is, anything of bottom type belongs to every other type as well. More advanced type systems have more examples of subtyping: for example, they might make `int` a subtype of `real`. But the core type system of OCaml doesn't have any general subtyping relations. (Neither does System F.) Just this one: that expressions of the bottom type also belong to every other type. It's as if every type definition in OCaml, even the built in ones, had an implicit extra clause:
234
235         type 'a option = None | Some of 'a;;
236         type 'a option = None | Some of 'a | bottom;;
237
238 Here are some exercises that may help better understand this. Figure out what is the type of each of the following:
239
240         fun x y -> y;;
241
242         fun x (y:int) -> y;;
243
244         fun x y : int -> y;;
245
246         let rec blackhole x = blackhole x in blackhole;;
247
248         let rec blackhole x = blackhole x in blackhole 1;;
249
250         let rec blackhole x = blackhole x in fun (y:int) -> blackhole y y y;;
251
252         let rec blackhole x = blackhole x in (blackhole 1) + 2;;
253
254         let rec blackhole x = blackhole x in (blackhole 1) || false;;
255
256         let rec blackhole x = blackhole x in 2 :: (blackhole 1);;
257
258 By the way, what's the type of this:
259
260         let rec blackhole (x:'a) : 'a = blackhole x in blackhole
261
262
263 Back to thunks: the reason you'd want to control evaluation with thunks is to
264 manipulate when "effects" happen. In a strongly normalizing system, like the
265 simply-typed lambda calculus or System F, there are no "effects." In Scheme and
266 OCaml, on the other hand, we can write programs that have effects. One sort of
267 effect is printing (think of the [[damn]] example at the start of term).
268 Another sort of effect is mutation, which we'll be looking at soon.
269 Continuations are yet another sort of effect. None of these are yet on the
270 table though. The only sort of effect we've got so far is *divergence* or
271 non-termination. So the only thing thunks are useful for yet is controlling
272 whether an expression that would diverge if we tried to fully evaluate it does
273 diverge. As we consider richer languages, thunks will become more useful.
274
275
276 Towards Monads
277 --------------
278
279 This has now been moved to [its own page](/towards_monads).
280