4 Integer division presupposes that its second argument
5 (the divisor) is not zero, upon pain of presupposition failure.
6 Here's what my OCaml interpreter says:
9 Exception: Division_by_zero.
11 So we want to explicitly allow for the possibility that
12 division will return something other than a number.
13 We'll use OCaml's `option` type, which works like this:
15 # type 'a option = None | Some of 'a;;
19 - : int option = Some 3
21 So if a division is normal, we return some number, but if the divisor is
22 zero, we return `None`. As a mnemonic aid, we'll append a `'` to the end of our new divide function.
25 let div' (x:int) (y:int) =
31 val div' : int -> int -> int option = fun
33 - : int option = Some 4
36 # div' (div' 12 3) 2;;
40 Error: This expression has type int option
41 but an expression was expected of type int
45 This starts off well: dividing 12 by 3, no problem; dividing 12 by 0,
46 just the behavior we were hoping for. But we want to be able to use
47 the output of the safe-division function as input for further division
48 operations. So we have to jack up the types of the inputs:
51 let div' (u:int option) (v:int option) =
55 | Some y -> (match u with
57 | Some x -> Some (x / y));;
60 val div' : int option -> int option -> int option = <fun>
61 # div' (Some 12) (Some 4);;
62 - : int option = Some 3
63 # div' (Some 12) (Some 0);;
65 # div' (div' (Some 12) (Some 0)) (Some 4);;
70 Beautiful, just what we need: now we can try to divide by anything we
71 want, without fear that we're going to trigger any system errors.
73 I prefer to line up the `match` alternatives by using OCaml's
77 let div' (u:int option) (v:int option) =
82 | (Some x, Some y) -> Some (x / y);;
85 So far so good. But what if we want to combine division with
86 other arithmetic operations? We need to make those other operations
87 aware of the possibility that one of their arguments will trigger a
88 presupposition failure:
91 let add' (u:int option) (v:int option) =
95 | (Some x, Some y) -> Some (x + y);;
98 val add' : int option -> int option -> int option = <fun>
99 # add' (Some 12) (Some 4);;
100 - : int option = Some 16
101 # add' (div' (Some 12) (Some 0)) (Some 4);;
102 - : int option = None
106 This works, but is somewhat disappointing: the `add'` operation
107 doesn't trigger any presupposition of its own, so it is a shame that
108 it needs to be adjusted because someone else might make trouble.
110 But we can automate the adjustment. The standard way in OCaml,
111 Haskell, etc., is to define a `bind` operator (the name `bind` is not
112 well chosen to resonate with linguists, but what can you do). To continue our mnemonic association, we'll put a `'` after the name "bind" as well.
115 let bind' (u: int option) (f: int -> (int option)) =
120 let add' (u: int option) (v: int option) =
121 bind' u (fun x -> bind' v (fun y -> Some (x + y)));;
123 let div' (u: int option) (v: int option) =
124 bind' u (fun x -> bind' v (fun y -> if (0 = y) then None else Some (x / y)));;
127 # div' (div' (Some 12) (Some 2)) (Some 4);;
128 - : int option = Some 1
129 # div' (div' (Some 12) (Some 0)) (Some 4);;
130 - : int option = None
131 # add' (div' (Some 12) (Some 0)) (Some 4);;
132 - : int option = None
136 Compare the new definitions of `add'` and `div'` closely: the definition
137 for `add'` shows what it looks like to equip an ordinary operation to
138 survive in dangerous presupposition-filled world. Note that the new
139 definition of `add'` does not need to test whether its arguments are
140 None objects or real numbers---those details are hidden inside of the
143 The definition of `div'` shows exactly what extra needs to be said in
144 order to trigger the no-division-by-zero presupposition.
146 For linguists: this is a complete theory of a particularly simply form
147 of presupposition projection (every predicate is a hole).