Source file integer_mod2.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
type t = Zero | One

let zero = Zero

let one = One

let add (x : t) (y : t) =
  match (x, y) with
  | (Zero, Zero) -> Zero
  | (Zero, One) | (One, Zero) -> One
  | (One, One) -> Zero

let neg (x : t) = x

let sub x y = add x (neg y)

let mul (x : t) (y : t) =
  match (x, y) with (Zero, _) | (_, Zero) -> Zero | (One, One) -> One

let div (x : t) (y : t) =
  match y with Zero -> Stdlib.failwith "Z2: division by zero" | _ -> x

let hash (x : t) = match x with Zero -> 0 | One -> 1

let pp fmtr (x : t) =
  match x with
  | Zero -> Format.pp_print_string fmtr "zero"
  | One -> Format.pp_print_string fmtr "one"

let equal (x : t) (y : t) =
  match (x, y) with (Zero, Zero) -> true | (One, One) -> true | _ -> false

let compare (x : t) (y : t) =
  match (x, y) with
  | (Zero, Zero) | (One, One) -> 0
  | (Zero, One) -> -1
  | (One, Zero) -> 1

let of_int (i : int) = if i mod 2 = 0 then Zero else One