-
Notifications
You must be signed in to change notification settings - Fork 10
/
fpc.ml
89 lines (55 loc) · 1.92 KB
/
fpc.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
75
76
77
78
79
80
81
82
83
(* 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 = Fold of nat natF
let unfold (Fold x) = x
let (z : nat) = Fold (Inl ())
let s : nat -> nat = fun v -> Fold (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) (unfold x))
let rec nat_gen : ('a -> 'a natF) -> 'a -> nat =
fun alg x ->
Fold (natF_map (nat_gen alg) (alg x))
let add (x:nat) (y:nat) : nat =
nat_fold (fun z -> match z with
| Inl () -> x
| Inr y' -> s y') y
let pred (x:nat) : nat = match (unfold x) with
| Inl () -> x
| Inr y -> y
let rec omega = Fold (Inr omega)
let pred_omega = pred omega
(* ---- Defining fix with recursive types ----- *)
type 'a self = Self of ('a self -> 'a)
let unroll (Self x) = x
let fix : ('a -> 'a) -> 'a
= fun f ->
let d : 'a self -> 'a = fun x -> f (fun v -> unroll x x v) in
d (Self d)
let add' (x : nat) : nat -> nat =
fix (fun f -> (fun y -> match (unfold 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 }