-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemT_NG_D.ml
171 lines (140 loc) · 5.38 KB
/
SystemT_NG_D.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
open Format
(* types *)
type ty = Nat
| Arr of ty * ty
(* syntax *)
type 'a tm = Z
| S of ('a tm)
| Rec of ty * ('a tm) * ('a tm)
(* --------------- *)
| Var of 'a
| Lam of ('a -> 'a tm)
| App of ('a tm) * ('a tm)
let rec pp_tm gensym pp_a ppf (t : 'a tm) =
let pp_tm = (pp_tm gensym pp_a) in
(match t with
| Z -> fprintf ppf "Z"
| S u -> fprintf ppf "(S %a)" pp_tm u
| Rec (_,z,s) -> fprintf ppf "(Rec %a %a)" pp_tm z pp_tm s
| Var x -> fprintf ppf "%a" pp_a x
| Lam f -> (let x = gensym() in
fprintf ppf "@[<1>(λ%a. %a)@]" pp_a x pp_tm (f x))
| App (s,t) -> fprintf ppf "@[<1>(%a %a)@]" pp_tm s pp_tm t)
(* WHNF syntax *)
type 'a nf = Lam_ of ('a -> 'a nf)
| Neu of ('a ne)
and 'a ne = App_ of ('a ne) * ('a nf)
| Var_ of 'a
| Z_
| S_ of ('a nf)
| Rec_ of ty * ('a nf) * ('a nf)
let rec nf_tm (t : 'a nf) : 'a tm =
(match t with
| Lam_ f -> Lam (fun x -> (nf_tm (f x)))
| Neu n -> ne_tm n)
and ne_tm (t : 'a ne) : 'a tm =
(match t with
| App_ (t,u) -> App (ne_tm t, nf_tm u)
| Var_ x -> Var x
| Z_ -> Z
| S_ u -> S (nf_tm u)
| Rec_ (a,z,s) -> Rec (a,nf_tm z, nf_tm s))
(* semantics *)
type 'a vl = Num of int
| Fun of ('a vl -> 'a vl)
| Syn of 'a ne
let app (v : 'a vl) (u : 'a vl) : 'a vl =
(match v with
| Fun f -> (f u)
| _ -> failwith "Ill-typed value: 'Fun'!.")
let zero : 'a vl = Num 0
let rec succ (v : 'a vl) : 'a vl =
(match v with
| Num k -> Num (k + 1)
| Syn s -> Syn (S_ (Neu s))
| Fun _ -> failwith "Ill-typed value 'Fun'!")
let rec nat_recD (a : ty) (z : 'a vl) (f : 'a vl) : 'a vl =
Fun (fun v ->
(match v with
| Num k -> (if k = 0 then z
else (app f (app (nat_recD a z f) (Num (k - 1)))))
| _ -> reflect a (App_ (Rec_ (a, reify a z, reify (Arr(a,a)) f), reify a v))))
(****************************************************************)
(* reify and reflect: from intermediate to target *)
(****************************************************************)
(* takes semantic objects to normal terms *)
and reify (a : ty) (v : 'a vl) : 'a nf =
(match (a,v) with
| _, Syn n -> Neu n
| Nat, Num k -> (if k>0 then (Neu (S_ (reify Nat (Num (k - 1)))))
else (Neu Z_))
| Arr (a,b), Fun f -> Lam_ (fun v -> reify b (f (reflect a (Var_ v))))
| _ -> failwith "Ill-typed value!")
(* takes neutral terms to semantic objects *)
and reflect (a : ty) (t : 'a ne) : 'a vl =
(match a with
| Nat -> Syn t
| Arr (a,b) ->
Fun (fun n -> (reflect b (App_ (t, reify a n)))))
let rec eval (t : ('a vl) tm) : 'a vl =
(match t with
| Z -> zero
| S u -> succ (eval u)
| Rec (a,z,s) -> nat_recD a (eval z) (eval s)
| Var v -> v
| Lam f -> Fun (fun v -> (eval (f v)))
| App (t,u) -> app (eval t) (eval u))
let nbe (a : ty) (t : ('a vl) tm) : 'a nf =
reify a (eval t)
(****************************************************************)
(* Tests *)
(****************************************************************)
let gensym =
(let x = ref 0 in
fun () ->
incr x ;
"x" ^ string_of_int !x)
let pp_var ppf s = Format.fprintf ppf "%s" s
let pp_tm_str = pp_tm gensym pp_var
let _1 = S Z
let _2 = S _1
let _3 = S _2
let _4 = S _3
let _5 = S _4
let _6 = S _5
let _7 = S _6
let _8 = S _7
let _9 = S _8
let _succ = Lam (fun x -> S (Var x))
let _I = Lam (fun x -> Var x)
let _K = Lam (fun x -> Lam (fun y -> Var y))
let _S = Lam (fun x -> Lam (fun y -> Lam (fun z -> App(App(Var x, Var z),App(Var y, Var z)))))
let _add = Lam (fun x -> Lam (fun y -> App(Rec(Nat, Var y, _succ),Var x)))
let _mul = Lam (fun x -> Lam (fun y -> App(Rec(Nat, Z, App(_add, Var x)), Var y)))
let tests : ('a tm * ty) list
= [(Lam (fun x -> Var x), Arr (Nat,Nat));
(Lam (fun f -> Lam (fun x -> App (Var f,Var x))), Arr (Arr (Nat,Nat), Arr (Nat, Nat)));
(Lam (fun x -> App (Lam (fun y -> Var y), Var x)), Arr (Nat, Nat));
(App (Lam (fun x -> S (Var x)), S Z), Nat);
(Lam (fun x -> S (Var x)), Arr (Nat, Nat));
(Lam (fun x -> App (Lam (fun x -> S (Var x)), S (Var x))), Arr (Nat, Nat));
(Lam (fun x -> Lam (fun y -> App (Var x,Var y))), Arr(Arr (Nat, Nat), Arr (Nat, Nat)));
(Lam (fun x -> Lam (fun y -> App (App (Var x,Var y), S (Var y)))), Arr(Arr (Nat, Arr (Nat, Nat)), Arr(Nat,Nat)));
(Lam (fun x -> Lam (fun y -> App (App (App (Var x,Var y), S (Var y)), S (S (Var y))))), Arr(Arr (Nat, Arr (Nat, Arr (Nat, Nat))), Arr(Nat,Nat)));
(Lam (fun x -> Lam (fun y -> Var y)), Arr(Nat,Arr(Nat,Nat)));
(App(Lam (fun x -> Lam (fun y -> App (App (Var x,Var y), S (Var y)))),Lam (fun x -> Lam (fun y -> Var y))), Arr(Nat,Nat));
(App(Lam (fun x -> Lam (fun y -> App (App (App (Var x,Var y), S (Var y)), S (S (Var y))))),
Lam (fun x -> Lam (fun y -> Lam (fun z -> Var z)))), Arr(Nat,Nat));
(_add, Arr(Nat,Arr(Nat,Nat)));
(_mul, Arr(Nat,Arr(Nat,Nat)));
(App(App(_add, _5), _7), Nat);
(App(App(_mul, _3), _4), Nat);
(Lam (fun x -> App(App(_mul, _3), Var x)), Arr(Nat,Nat));
]
let _ =
for i=0 to (List.length tests) - 1 do
(let p = (List.nth tests i) in
let p' = (List.nth tests i) in
(printf "test %d :: %a@\n" i pp_tm_str (fst p));
(printf "> %a@\n" pp_tm_str (nf_tm (nbe (snd p') (fst p')))))
done