-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstances.ml
59 lines (55 loc) · 1.13 KB
/
instances.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
50
51
52
53
54
55
56
57
58
59
open Imp.Any
open Classes
implicit module Pair {A: Any} {B: Any}
: Product
with type t = A.t * B.t
and type a = A.t
and type b = B.t
= struct
type t = A.t * B.t
type a = A.t
type b = B.t
let construct a b = .< .~a, .~b >.
let deconstruct p = .< fst .~p >., .< snd .~p >.
end
implicit module Option {A: Any}
: Sum
with type t = A.t option
and type a = A.t
and type b = unit
= struct
type t = A.t option
type a = A.t
type b = unit
let construct_a a = .< Some .~a >.
let construct_b _ = .< None >.
let match_ fa fb x = .<
match .~x with
| Some a -> .~(fa .< a >.)
| None -> .~(fb .< () >.)
>.
end
implicit module List {A: Any}
: Sum
with type t = A.t list
and type a = unit
and type b = A.t * A.t list
= struct
type t = A.t list
type a = unit
type b = A.t * A.t list
let construct_a _ = .< [] >.
let construct_b p = .< let (x, xs) = .~p in x :: xs >.
let match_ fa fb (x: A.t list code) = .<
match .~x with
| [] -> .~(fa .< () >.)
| x :: xs -> .~(fb .< (x, xs) >.)
>.
end
implicit module Unit
: Unit
with type t = unit
= struct
type t = unit
let unit = .< () >.
end