continue fix [#1], discovered by tests
[deriving.git] / lib / eq.ml
blobc5acde70a1aae89ccb7d092c16fd762c19cb4a46
1 (*pp deriving *)
3 (* Copyright Jeremy Yallop 2007.
4 This file is free software, distributed under the MIT license.
5 See the file COPYING for details.
6 *)
8 module type Eq =
9 sig
10 type a
11 val eq : a -> a -> bool
12 end
14 module Defaults (E : Eq) = E
16 module Eq_immutable(S : sig type a end) :
17 Eq with type a = S.a =
18 struct
19 type a = S.a
20 let eq = (=)
21 end
23 module Eq_mutable(S : sig type a end) :
24 Eq with type a = S.a =
25 struct
26 type a = S.a
27 let eq = (==)
28 end
30 module Eq_int = Eq_immutable(struct type a = int end)
31 module Eq_bool = Eq_immutable(struct type a = bool end)
32 module Eq_float = Eq_immutable(struct type a = float end)
33 module Eq_unit = Eq_immutable(struct type a = unit end)
34 module Eq_char = Eq_immutable(struct type a = char end)
36 module Eq_string = Eq_mutable(struct type a = string end)
37 module Eq_ref (E : Eq) = Eq_mutable(struct type a = E.a ref end)
38 module Eq_array (E : Eq) = Eq_mutable(struct type a = E.a array end)
40 module Eq_option (E : Eq)
41 : Eq with type a = E.a option =
42 struct
43 type a = E.a option
44 let eq l r = match l, r with
45 | None, None -> true
46 | Some l, Some r -> E.eq l r
47 | _ -> false
48 end
50 module Eq_map_s_t (E : Eq) (M : Map.S)
51 : Eq with type a = E.a M.t =
52 struct
53 type a = E.a M.t
54 let eq = M.equal (E.eq)
55 end
57 module Eq_list (E : Eq) :
58 Eq with type a = E.a list =
59 struct
60 type a = E.a list
61 let rec eq l r = match l, r with
62 | [], [] -> true
63 | (lfst::lrst), (rfst::rrst) when E.eq lfst rfst -> eq lrst rrst
64 | _ -> false
65 end
67 module Eq_num
68 : Eq with type a = Num.num =
69 struct
70 type a = Num.num
71 let eq = Num.eq_num
72 end