-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalProject.ml
More file actions
332 lines (308 loc) · 11.4 KB
/
finalProject.ml
File metadata and controls
332 lines (308 loc) · 11.4 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
(* Name: Chris Milne & Nick Castle*)
(* Course: UVM CS 225 Spring 2018 - Darais *)
open Util
open StringSetMap
(* The Project:
* We will be implementing error handleing similar to error
* handleing commonly seen in languages such as Java. We will be
* adding this onto the simply typed lambda calculus which we have
* been working with thus far in class.
*)
exception NOT_FOUND
(* ℓ ∈ loc ≈ ℕ
*)
type loc = int
[@@deriving show {with_path = false}]
(* Expressions.
*
* e ∈ exp ⩴ true | false | if(e){e}{e}
* | ⟨e,e⟩ | projl(e) | projr(e)
* | ref(e) | !e | e ≔ e | e ; e
* | loc(ℓ)
*)
type exp =
| True
| False
| If of exp * exp * exp
| Pair of exp * exp
| Projl of exp
| Projr of exp
| Ref of exp
| Deref of exp
| Assign of exp * exp
| Sequence of exp * exp
| Loc of loc
| Error
| Try of exp * exp
| Raise of exp
[@@deriving show {with_path = false}]
(* Values.
* v ∈ value ⩴ true | false
* | ⟨v,v⟩
* | loc(ℓ)
*)
type value =
| VTrue
| VFalse
| VPair of value * value
| VLoc of loc
| VError
[@@deriving show {with_path = false}]
let rec exp_of_val (v : value) : exp = match v with
| VTrue -> True
| VFalse -> False
| VPair(v1,v2) -> Pair(exp_of_val v1,exp_of_val v2)
| VLoc(l) -> Loc(l)
| VError -> Error
type store = (loc * value) list
[@@deriving show {with_path = false}]
(* store_lookup l s = s(l) *)
let rec store_lookup (l : loc) (s : store) : value = match s with
| [] -> raise NOT_FOUND
| (l',v) :: s' -> if l = l' then v else store_lookup l s'
(* store_update l v s = s[l↦v] *)
let rec store_update (l : loc) (v : value) (s : store) : store = match s with
| [] -> (l,v) :: []
| (l',v') :: s' -> if l = l' then (l,v) :: s' else (l',v') :: store_update l v s'
(* store_max_loc s = ⨆{ℓ | ℓ ∈ dom(s)} *)
let rec store_max_loc (s : store) : int = match s with
| [] -> 0
| (l,_) :: s' -> max l (store_max_loc s')
(* store_fresh s = ℓ where ℓ ∉ dom(s) *)
let rec store_fresh (s : store) : int =
let l = store_max_loc s in
l + 1
(* r ∈ result ⩴ v | ⟨e,s⟩ | stuck
*)
type result =
| Val of value
| Step of exp * store
| Stuck
[@@deriving show {with_path = false}]
let rec step (e0 : exp) (s : store) : result = match e0 with
| True -> Val(VTrue)
| False -> Val(VFalse)
| If(e1,e2,e3) -> begin match step e1 s with
| Val(VTrue) -> Step(e2,s)
| Val(VFalse) -> Step(e3,s)
| Val(_) -> Stuck
| Step(e1',s') -> Step(If(e1',e2,e3),s')
| Stuck -> Stuck
end
| Pair(e1,e2) -> begin match step e1 s with
| Val(v1) -> begin match step e2 s with
| Val(v2) -> Val(VPair(v1,v2))
| Step(e2',s') -> Step(Pair(e1,e2'),s')
| Stuck -> Stuck
end
| Step(e1',s') -> Step(Pair(e1',e2),s')
| Stuck -> Stuck
end
| Projl(e1) -> begin match step e1 s with
| Val(VPair(v1,v2)) -> Step(exp_of_val(v1),s)
| Val(_) -> Stuck
| Step(e1',s') -> Step(Projl(e1'), s')
| Stuck -> Stuck
end
| Projr(e1) -> begin match step e1 s with
| Val(VPair(v1,v2)) -> Step(exp_of_val(v2),s)
| Val(_) -> Stuck
| Step(e1',s') -> Step(Projr(e1'),s')
| Stuck -> Stuck
end
| Ref(e1) -> begin match step e1 s with
| Val(v1) ->
let l = store_fresh s in
let l1 = store_update l v1 s in
Step(Loc(l), l1)
| Step(e1',s') -> Step(Ref(e1'),s')
| Stuck -> Stuck
end
| Deref(e1) -> begin match step e1 s with
| Val(VLoc(l)) -> Step(exp_of_val(store_lookup l s), s)
| Val(_) -> Stuck
| Step(e1',s') -> Step(Deref(e1'),s')
| Stuck -> Stuck
end
| Assign(e1,e2) -> begin match step e1 s with
| Val(VLoc(l1)) -> begin match step e2 s with
| Val(v2) -> Step(e2,store_update l1 v2 s)
| Step(e2',s') -> Step(Assign(e1,e2'),s')
| Stuck -> Stuck
end
| Val(_) -> Stuck
| Step(e1',s') -> Step(Assign(e1',e2),s')
| Stuck -> Stuck
end
| Sequence(e1,e2) -> begin match step e1 s with
| Val(_) -> Step(e2, s)
| Step(e1',s') -> Step(Sequence(e1',e2),s')
| Stuck -> Stuck
end
| Loc(l) -> Val(VLoc(l))
| Error -> Val(VError)
| Try(e1,e2) -> begin match step e1 s with
|Val(v1) -> Step(e1,s)
|Step(e1',s') -> Step(Try(e1',e2),s')
|Stuck -> Stuck
end
| Raise(e1) -> begin match step e1 s with
| Val(v1) -> Step(exp_of_val(v1),s)
| Step(e1',s') -> Step(Raise(e1'),s')
| Stuck -> Stuck
end
(* The reflexive transitive closure of the small-step semantics relation *)
let rec step_star (e : exp) (s : store) : exp * store = match step e s with
| Val(v) -> (exp_of_val v,s)
| Step(e',s') -> step_star e' s'
| Stuck -> (e,s)
(* Types.
*
* τ ∈ ty ⩴ bool
* | τ × τ
* | ref(τ)
*)
type ty =
| Bool
| Prod of ty * ty
| Ref of ty
| Error
[@@deriving show {with_path = false}]
type store_ty = (loc * ty) list
[@@deriving show {with_path = false}]
let rec store_ty_lookup (l : loc) (st : store_ty) : ty = match st with
| [] -> raise NOT_FOUND
| (l',t) :: st' -> if l = l' then t else store_ty_lookup l st'
exception TYPE_ERROR
let rec infer (e : exp) (st : store_ty) : ty = match e with
| True -> Bool
| False -> Bool
| If(e1,e2,e3) ->
let t1 = infer e1 st in
let t2 = infer e2 st in
let t3 = infer e3 st in
if not (t1 = Bool) then raise TYPE_ERROR else
if not (t2 = t3) then raise TYPE_ERROR else
t2
| Pair(e1,e2) ->
let t1 = infer e1 st in
let t2 = infer e2 st in
Prod(t1,t2)
| Projl(e1) ->
let t1 = infer e1 st in
begin match t1 with
| Prod(t11,_) -> t11
| _ -> raise TYPE_ERROR
end
| Projr(e1) ->
let t1 = infer e1 st in
begin match t1 with
| Prod(_,t12) -> t12
| _ -> raise TYPE_ERROR
end
| Ref(e1) -> Ref(infer e1 st)
| Deref(e1) ->
let t1 = infer e1 st in
begin match t1 with
|Ref(t1) -> t1
| _ -> raise TYPE_ERROR
end
| Assign(e1,e2) ->
let t1 = infer e1 st in
let t2 = infer e2 st in
if t1 = Ref(t2) then
begin match t1 with
|Ref(t1) -> Bool
| _ -> raise TYPE_ERROR
end
else raise TYPE_ERROR
| Sequence(e1,e2) ->
let t2 = infer e2 st in
t2
| Loc(l) -> Ref(store_ty_lookup l st)
| Error -> Error
| Try(e1,e2) -> Error
| Raise(e1) -> Error
let step_tests : test_block =
let s1 : store = [(1,VTrue);(2,VFalse)] in
let s2 : store = [(1,VTrue);(2,VTrue)] in
TestBlock
( "STEP"
, [ (True,s1) , Val(VTrue)
; (False,s1) , Val(VFalse)
; (If(True,False,True),s1) , Step(False,s1)
; (If(False,False,True),s1) , Step(True,s1)
; (If(Pair(True,False),True,False),s1) , Stuck
; (If(Assign(Loc(2),True),False,True),s1) , Step(If(True,False,True),s2)
; (If(Deref(True),True,False),s1) , Stuck
; (Pair(True,False),s1) , Val(VPair(VTrue,VFalse))
; (Pair(Assign(Loc(2),True),True),s1) , Step(Pair(True,True),s2)
; (Pair(True,Assign(Loc(2),True)),s1) , Step(Pair(True,True),s2)
; (Pair(Deref(True),True),s1) , Stuck
; (Pair(True,Deref(True)),s1) , Stuck
; (Projl(Pair(True,False)),s1) , Step(True,s1)
; (Projl(True),s1) , Stuck
; (Projl(If(True,Pair(True,False),Pair(False,True))),s1) , Step(Projl(Pair(True,False)),s1)
; (Projl(Deref(True)),s1) , Stuck
; (Projr(Pair(True,False)),s1) , Step(False,s1)
; (Projr(True),s1) , Stuck
; (Projr(If(True,Pair(True,False),Pair(False,True))),s1) , Step(Projr(Pair(True,False)),s1)
; (Projr(Deref(True)),s1) , Stuck
; (Ref(True),s1) , Step(Loc(3),s1 @ [(3,VTrue)])
; (Ref(Assign(Loc(2),True)),s1) , Step(Ref(True),s2)
; (Ref(Deref(True)),s1) , Stuck
; (Deref(Loc(2)),s1) , Step(False,s1)
; (Deref(True),s1) , Stuck
; (Deref(If(True,Loc(1),Loc(2))),s1) , Step(Deref(Loc(1)),s1)
; (Deref(Deref(True)),s1) , Stuck
; (Assign(Loc(2),True),s1) , Step(True,s2)
; (Assign(True,False),s1) , Stuck
; (Assign(If(False,Loc(1),Loc(2)),True),s1) , Step(Assign(Loc(2),True),s1)
; (Assign(Loc(1),Assign(Loc(2),True)),s1) , Step(Assign(Loc(1),True),s2)
; (Assign(Deref(False),True),s1) , Stuck
; (Assign(Loc(1),Deref(False)),s1) , Stuck
; (Sequence(True,False),s1) , Step(False,s1)
; (Sequence(Assign(Loc(2),True),Deref(Loc(2))),s1) , Step(Sequence(True,Deref(Loc(2))),s2)
; (Sequence(Deref(True),False),s1) , Stuck
; (Error,s1) , Val(VError)
; (Try(If(False,Loc(1),Loc(2)),True),s1) , Step(Try(Loc(2),True),s1)
; (Try(Deref(False),True),s1) , Stuck
; (Raise(If(True,Pair(True,False),Pair(False,True))),s1) , Step(Raise(Pair(True,False)),s1)
; (Raise(Deref(True)),s1) , Stuck
; (Raise(Deref(False)),s1) , Stuck
]
, (fun (e,s) -> step e s)
, [%show : exp * store]
, [%show : result]
)
let infer_tests =
let st : store_ty = [(1,Bool);(2,Prod(Bool,Bool));(3,Ref(Bool))] in
TestBlock
( "INFER"
, [ True , Bool
; False , Bool
; If(True,False,True) , Bool
; If(True,Pair(True,Ref(False)),Pair(False,Ref(True))) , Prod(Bool,Ref(Bool))
; Projl(Pair(True,Ref(False))) , Bool
; Projr(Pair(True,Ref(False))) , Ref(Bool)
; Ref(True) , Ref(Bool)
; Ref(Loc(1)) , Ref(Ref(Bool))
; Deref(Loc(1)) , Bool
; Deref(Loc(2)) , Prod(Bool,Bool)
; Deref(Deref(Loc(3))) , Bool
; Assign(Loc(1),False) , Bool
; Assign(Loc(2),Pair(True,False)) , Bool
; Sequence(Assign(Loc(1),False),Ref(True)) , Ref(Bool)
; Error , Error
; Try(True, Error) , Error
; Try(False, Error) , Error
; Raise(Error) , Error
]
, (fun e -> infer e st)
, (fun e -> [%show : exp * store_ty] (e,st))
, [%show : ty]
)
let _ =
_SHOW_PASSED_TESTS := false ;
run_tests [step_tests;infer_tests]
(* Name: Chris Milne & Nick Castle*)