-
Notifications
You must be signed in to change notification settings - Fork 10
/
rectypes.ml
80 lines (50 loc) · 1.74 KB
/
rectypes.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(* Definitions with equi-recursive types in OCaml *)
(* --- Defining natural numbers with recursive types ----- *)
type ('a, 'b) sum = Inl of 'a | Inr of 'b
type 'a natF = (unit, 'a) sum
(* recursive type! *)
type nat = nat natF
let (z : nat) = Inl ()
let s : nat -> nat = fun v -> Inr v
let natF_map : ('a -> 'b) -> 'a natF -> 'b natF =
fun f x -> match x with
| Inl () -> Inl ()
| Inr y -> Inr (f y)
let rec nat_fold : ('a natF -> 'a) -> nat -> 'a =
fun alg x ->
alg (natF_map (nat_fold alg) x)
let rec nat_gen : ('a -> 'a natF) -> 'a -> nat =
fun alg x ->
natF_map (nat_gen alg) (alg x)
let add (x:nat) (y:nat) =
nat_fold (fun z -> match z with
| Inl () -> x
| Inr y' -> s y') y
let pred (x:nat) : nat = match x with
| Inl () -> x
| Inr y -> y
let rec omega : nat = Inr omega
let pred_omega = pred omega
(* ---- Defining fix with recursive types ----- *)
let fix = fun f -> (fun x -> f (fun v -> x x v))
(fun x -> f (fun v -> x x v))
let add' (x : nat) : nat -> nat =
fix (fun f -> (fun y -> match y with
| Inl () -> x
| Inr y' -> s (f y')))
(* ---- Defining state with recursive types ----- *)
type signal = (bool * bool) (* Inputs r & s *)
type rsl = { q' : bool ; (* inverse of q *)
q : bool ; (* state of latch *)
n : signal -> rsl }
let hold = (false, false)
let set = (false, true)
let reset = (true, false)
let nor p q = not (p || q)
let rec rsl l (r,s) =
let rec this = { q' = nor l.q s ;
q = nor r l.q' ;
n = fun s -> rsl this s } in
this
let rec init : rsl =
{ q' = false; q = false; n = fun s -> rsl init s }