-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsetoid_test.ml
49 lines (40 loc) · 1.17 KB
/
setoid_test.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
40
41
42
43
44
45
46
47
48
49
open Setoid;;
(* We can uniquify any List of setoids - all we need is an `equals` *)
module Uniq = functor(Setoid : Setoid) -> struct
(* Like List.mem, but using Setoid.equals *)
let rec member needle = function
| [] -> false
| head :: tail ->
if Setoid.equals needle head then true
else member needle tail
(* Our main function which uniquifies a list of setoids *)
let rec f = function
| [] -> []
| hd :: [] -> [hd]
| hd :: tail ->
if member hd tail then f tail
else hd :: f tail
end
(* A simple Person module *)
module Person = struct
type t = string * int
let make name age: t = (name, age)
let name (n, _) = n
let age (_, a) = a
(* Not ready for production ;) *)
let equals p1 p2 = (name p1 = name p2) && (age p1 = age p2)
end
(* Use our Uniq functor on our Person module *)
module PersonUniq = Uniq(Person)
let crowd = [
Person.make "Jordan" 25 ;
Person.make "Steve" 29 ;
Person.make "Jordan" 25 ;
Person.make "Tom" 22 ;
Person.make "Jake" 1 ;
Person.make "Steve" 29
];;
let unique_members = PersonUniq.f crowd;;
assert (unique_members = [
("Jordan", 25) ; ("Tom", 22) ; ("Jake", 1) ; ("Steve", 29)
])