6 OCaml has type inference: the system can often infer what the type of
7 an expression must be, based on the type of other known expressions.
9 For instance, if we type
13 The system replies with
15 val f : int -> int = <fun>
17 Since `+` is only defined on integers, it has type
20 - : int -> int -> int = <fun>
22 The parentheses are there to turn off the trick that allows the two
23 arguments of `+` to surround it in infix (for linguists, SOV) argument
29 In general, tuples with one element are identical to their one
35 though OCaml, like many systems, refuses to try to prove whether two
36 functional objects may be identical:
39 Exception: Invalid_argument "equal: functional value".
43 [Note: There is a limited way you can compare functions, using the
44 `==` operator instead of the `=` operator. Later when we discuss mutation,
45 we'll discuss the difference between these two equality operations.
46 Scheme has a similar pair, which they name `eq?` and `equal?`. In Python,
47 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 understand `(f) == f` even though it doesn't understand
48 `(f) = f`. However, don't expect it to figure out in general when two functions
49 are identical. (That question is not Turing computable.)
51 # (f) == (fun x -> x + 3);;
54 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.]
58 Booleans in OCaml, and simple pattern matching
59 ----------------------------------------------
61 Where we would write `true 1 2` in our pure lambda calculus and expect
62 it to evaluate to `1`, in OCaml boolean types are not functions
63 (equivalently, they're functions that take zero arguments). Instead, selection is
64 accomplished as follows:
66 # if true then 1 else 2;;
69 The types of the `then` clause and of the `else` clause must be the
72 The `if` construction can be re-expressed by means of the following
73 pattern-matching expression:
75 match <bool expression> with true -> <expression1> | false -> <expression2>
79 # match true with true -> 1 | false -> 2;;
84 # match 3 with 1 -> 1 | 2 -> 4 | 3 -> 9;;
90 All functions in OCaml take exactly one argument. Even this one:
96 Here's how to tell that `f` has been curry'd:
99 - : int -> int = <fun>
101 After we've given our `f` one argument, it returns a function that is
102 still waiting for another argument.
104 There is a special type in OCaml called `unit`. There is exactly one
105 object in this type, written `()`. So
110 Just as you can define functions that take constants for arguments
116 you can also define functions that take the unit as its argument, thus
119 val f : unit -> int = <fun>
121 Then the only argument you can possibly apply `f` to that is of the
122 correct type is the unit:
127 Now why would that be useful?
129 Let's have some fun: think of `rec` as our `Y` combinator. Then
131 # let rec f n = if (0 = n) then 1 else (n * (f (n - 1)));;
132 val f : int -> int = <fun>
136 We can't define a function that is exactly analogous to our ω.
137 We could try `let rec omega x = x x;;` what happens?
139 [Note: if you want to learn more OCaml, you might come back here someday and try:
142 val id : 'a -> 'a = <fun>
143 # let unwrap (`Wrap a) = a;;
144 val unwrap : [< `Wrap of 'a ] -> 'a = <fun>
145 # let omega ((`Wrap x) as y) = x y;;
146 val omega : [< `Wrap of [> `Wrap of 'a ] -> 'b as 'a ] -> 'b = <fun>
147 # unwrap (omega (`Wrap id)) == id;;
149 # unwrap (omega (`Wrap omega));;
150 <Infinite loop, need to control-c to interrupt>
152 But we won't try to explain this now.]
155 Even if we can't (easily) express omega in OCaml, we can do this:
157 # let rec blackhole x = blackhole x;;
159 By the way, what's the type of this function?
161 If you then apply this `blackhole` function to an argument,
165 the interpreter goes into an infinite loop, and you have to type control-c
168 Oh, one more thing: lambda expressions look like this:
172 # (fun x -> x) true;;
175 (But `(fun x -> x x)` still won't work.)
177 You may also see this:
179 # (function x -> x);;
182 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.
184 We can try our usual tricks:
186 # (fun x -> true) blackhole;;
189 OCaml declined to try to fully reduce the argument before applying the
190 lambda function. Question: Why is that? Didn't we say that OCaml is a call-by-value/eager language?
192 Remember that `blackhole` is a function too, so we can
193 reverse the order of the arguments:
195 # blackhole (fun x -> true);;
199 Now consider the following variations in behavior:
201 # let test = blackhole blackhole;;
202 <Infinite loop, need to control-c to interrupt>
204 # let test () = blackhole blackhole;;
205 val test : unit -> 'a = <fun>
208 - : unit -> 'a = <fun>
211 <Infinite loop, need to control-c to interrupt>
213 We can use functions that take arguments of type unit to control
214 execution. In Scheme parlance, functions on the unit type are called
215 *thunks* (which I've always assumed was a blend of "think" and "chunk").
217 Question: why do thunks work? We know that `blackhole ()` doesn't terminate, so why do expressions like:
219 let f = fun () -> blackhole ()
224 Bottom type, divergence
225 -----------------------
227 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:
229 type 'a option = None | Some of 'a;;
230 type 'a option = None | Some of 'a | bottom;;
232 Here are some exercises that may help better understand this. Figure out what is the type of each of the following:
240 let rec blackhole x = blackhole x in blackhole;;
242 let rec blackhole x = blackhole x in blackhole 1;;
244 let rec blackhole x = blackhole x in fun (y:int) -> blackhole y y y;;
246 let rec blackhole x = blackhole x in (blackhole 1) + 2;;
248 let rec blackhole x = blackhole x in (blackhole 1) || false;;
250 let rec blackhole x = blackhole x in 2 :: (blackhole 1);;
252 let rec blackhole (x:'a) : 'a = blackhole x in blackhole
255 Back to thunks: the reason you'd want to control evaluation with thunks is to
256 manipulate when "effects" happen. In a strongly normalizing system, like the
257 simply-typed lambda calculus or System F, there are no "effects." In Scheme and
258 OCaml, on the other hand, we can write programs that have effects. One sort of
259 effect is printing (think of the [[damn]] example at the start of term).
260 Another sort of effect is mutation, which we'll be looking at soon.
261 Continuations are yet another sort of effect. None of these are yet on the
262 table though. The only sort of effect we've got so far is *divergence* or
263 non-termination. So the only thing thunks are useful for yet is controlling
264 whether an expression that would diverge if we tried to fully evaluate it does
265 diverge. As we consider richer languages, thunks will become more useful.
271 This has now been moved to [its own page](/towards_monads).