-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrivine.ml
executable file
·264 lines (223 loc) · 7.89 KB
/
krivine.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
let addcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], Const(aa1+aa2))
;;
let subcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], Const(aa1-aa2))
;;
let divcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], Const(aa1/aa2))
;;
let mulcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], Const(aa1*aa2))
;;
let modcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], Const(aa1 mod aa2))
;;
let abscl a1 =
match a1 with
CLOSURE(t1, Const aa1) ->
if aa1<0 then
CLOSURE([], Const (-aa1))
else
CLOSURE([], Const aa1)
;;
let notcl a1 =
match a1 with
CLOSURE(t1, True) -> CLOSURE([], False)
| CLOSURE(t1, False) -> CLOSURE([], True)
;;
let andcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, True), CLOSURE(t2, True)) ->
CLOSURE([], True)
| _ -> CLOSURE([], False)
;;
let orrcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, False), CLOSURE(t2, False)) ->
CLOSURE([], False)
| _ -> CLOSURE([], True)
;;
let implycl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, True), CLOSURE(t2, False)) ->
CLOSURE([], False)
| _ -> CLOSURE([], True)
;;
let greatcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], if (aa1>aa2) then True else False)
;;
let equalcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], if (aa1=aa2) then True else False)
;;
let lessscl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], if (aa1<aa2) then True else False)
;;
let grequcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], if (aa1>=aa2) then True else False)
;;
let leequcl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], if (aa1<=aa2) then True else False)
;;
let rec expocl (a1, a2) =
match (a1, a2) with
(CLOSURE(t1, Const aa1), CLOSURE(t2, Const aa2)) ->
CLOSURE([], Const (poww aa1 aa2))
;;
let rec krivine c s =
match (c, s) with
(CLOSURE(gamma, True), s) ->
CLOSURE(gamma, True)
| (CLOSURE(gamma, False), s) ->
CLOSURE(gamma, False)
| (CLOSURE(gamma, Const(n)), s) ->
CLOSURE(gamma, Const(n))
| (CLOSURE(gamma, Abs(n)), s) ->
CLOSURE(gamma, Abs(n))
| (CLOSURE(gamma, Not(n)), s) ->
CLOSURE(gamma, Not(n))
| (CLOSURE(gamma, Projj(i, List(ex))), s) ->
krivine (CLOSURE(gamma, ith(i, ex))) []
| (CLOSURE(gamma, List(l)), s) -> CLOSURE(gamma, List(l))
| (CLOSURE(gamma, Plus(n, m)), s) ->
krivine (addcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Minus(n, m)), s) ->
krivine (subcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Mul(n, m)), s) ->
krivine (mulcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Div(n, m)), s) ->
krivine (divcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Mod(n, m)), s) ->
krivine (modcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Pow(n, m)), s) ->
krivine (expocl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, And(n, m)), s) ->
krivine (andcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Orr(n, m)), s) ->
krivine (orrcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Imply(n, m)), s) ->
krivine (implycl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Equal(n, m)), s) ->
krivine (equalcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Great(n, m)), s) ->
krivine (greatcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Lesss(n, m)), s) ->
krivine (lessscl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Grequ(n, m)), s) ->
krivine (grequcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Leequ(n, m)), s) ->
krivine (leequcl (
(krivine (CLOSURE(gamma, n)) []),
(krivine (CLOSURE(gamma, m)) [])
)
) s
| (CLOSURE(gamma, Var(x)), s) ->
krivine (get_var (Var x) gamma) s
| (CLOSURE(gamma, Lambda(y, e)), cl::ss) ->
krivine (CLOSURE((y,cl)::gamma, e)) ss
| (CLOSURE(gamma, LetInEnd(y, e1, e2)), s) ->
(let temp = krivine (CLOSURE((y,(krivine (CLOSURE(gamma, e1)) []))::gamma, e2)) s in
match temp with CLOSURE(t, e) -> CLOSURE(remove y t, e))
| (CLOSURE(gamma, Apply(f, e)), s) ->
krivine (CLOSURE(gamma, f)) (CLOSURE(gamma, e)::s)
| (CLOSURE(gamma, Ite(e, et, ef)), s) ->
krivine (match (krivine (CLOSURE(gamma, e)) []) with
CLOSURE(_, True) -> CLOSURE(gamma, et)
| CLOSURE(_, False) -> CLOSURE(gamma, ef)
) s
;;
let rec map_kr l env =
match l with
[] -> []
| x::y ->
(krivine (CLOSURE(env, x)) [])::(map_kr y env)
;;
let rec map_cl l env =
match l with
[] -> []
| x::y ->
CLOSURE(env, x)::(map_cl y env)
;;
let rec give_me_answers cl =
match cl with
CLOSURE(_, True) -> Bool(true)
| CLOSURE(_, False) -> Bool(false)
| CLOSURE(_, Const(n)) -> Int(n)
| CLOSURE(e, List(l)) ->
Listans (map give_me_answers (map_cl l e))
;;
(* let b = Apply(Lambda(Var("x"), Plus(Var("x"), Const(9))), Const(5));;
let clo = CLOSURE([],b)
let k = krivine clo [];; *)