forked from thbrooks22/lab9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab9.ml
385 lines (299 loc) · 13.1 KB
/
lab9.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
(*
CS51 Lab 9
Substitution Semantics
Objective:
This lab practices concepts of substitution semantics.
*)
(*====================================================================
Part 1: Substitution semantics derivation
In this part, you'll work out the formal derivation of the
substitution semantics for the expression
let x = 3 + 5 in
(fun x -> x * x) (x - 2)
according to the semantic rules presented in Chapter 13.
Before beginning, what should this expression evaluate to? Test out
your prediction in the OCaml REPL. *)
(* The exercises will take you through the derivation stepwise, so
that you can use the results from earlier exercises in the later
exercises.
By way of example, we do the first couple of exercises for you to give
you the idea.
......................................................................
Exercise 1. Carry out the derivation for the semantics of the
expression 3 + 5.
....................................................................*)
(* ANSWER:
3 + 5 =>
| 3 => 3 (R_int)
| 5 => 5 (R_int)
=> 8 (R_+)
(This derivation was actually given in the reading in Section
13.1. We've annotated each line with the semantic rule that it
uses. You should do that too below.) *)
(*....................................................................
Exercise 2. What is the result of the following substitution according
to the definition in Figure 13.3?
(x + 5) [x |-> 3]
....................................................................*)
(* ANSWER: Carrying out each step in the derivation:
(x + 5) [x |-> 3]
= x [x |-> 3] + 5 [x |-> 3] (by Eq. 4)
= 3 + 5 [x |-> 3] (by Eq. 2)
= 3 + 5 (by Eq. 1)
Again, we've labeled each line with the number of the equation that
was used from the set of equations for substitution in Figure
13.3. You should do that too. *)
(*....................................................................
Exercise 3. Carry out the derivation for the semantics of the
expression let x = 3 in x + 5.
....................................................................*)
(* ANSWER:
let x = 3 in x + 5 =>
| 3 => 3 (R_int)
| 3 + 5 => 8 (Exercises 2 and 1)
=> 8 (R_let)
Note the labeling of one of the steps with the prior results from
previous exercises. *)
(* Now it's your turn. We recommend doing these exercises with
pencil on paper, rather than typing them in.
......................................................................
Exercise 4. Carry out the derivation for the semantics of the
expression 8 - 2.
....................................................................*)
(* 8 - 2 =>
| 8 => 8
| 2 => 2
=> 6 *)
(*....................................................................
Exercise 5. Carry out the derivation for the semantics of the
expression 6 * 6.
....................................................................*)
(* 6 * 6 =>
| 6 => 6
| 6 => 6
=> 36 *)
(*....................................................................
Exercise 6. What is the result of the following substitution according
to the definition in Figure 13.3?
(x * x) [x |-> 6]
....................................................................*)
(* (x * x) [x |-> 6]
= x [x |-> 6] * x [x |-> 6]
= 6 * 6 *)
(*....................................................................
Exercise 7. The set of 10 equations defining substitution in Figure 13.3
is missing an equation for function application. You'll need this
equation in some exercises below. What should such an equation look
like? (Below, we'll refer to this as Eq. 11.)
....................................................................*)
(* (P R)[x |-> Q] = P[x |-> Q] R[x |-> Q] *)
(*....................................................................
Exercise 8. What is the result of the following substitution according
to the definition in Figure 13.3?
((fun x -> x * x) (x - 2)) [x |-> 8]
....................................................................*)
(* ((fun x -> x * x) (x - 2)) [x |-> 8]
= ((fun x -> x * x) [x |-> 8]) ((x - 2) [x |-> 8])
= (fun x -> x * x) (x [x |-> 8] - 2 [x |-> 8])
= (fun x -> x * x) (8 - 2) *)
(*....................................................................
Exercise 9. Carry out the derivation for the semantics of the
expression
(fun x -> x * x) (8 - 2)
....................................................................*)
(* (fun x -> x * x) (8 - 2) =>
| fun x -> x + x => fun x -> x + x
| 8 - 2 => 6
| 6 * 6 => 36
=> 36 *)
(*....................................................................
Exercise 10. Finally, carry out the derivation for the semantics of the
expression
(* let x = 3 + 5 in (fun x -> x * x) (x - 2) *)
....................................................................*)
(* let x = 3 + 5 in (fun x -> x * x) (x - 2) =>
| 3 + 5 => 8
| (fun x -> x * x) (8 - 2) => 36
=> 36 *)
(*====================================================================
Part 2: Pen and paper exercises with the free variables and
substitution definitions
In this part, you'll get more practice using the definitions of FV and
substitution from the textbook (Figure 13.3). Feel free to jump ahead
to later problems if you "get it" and are finding the exercises
tedious. *)
(*....................................................................
Exercise 11: Use the definition of FV to derive the set of free
variables in the expressions below. Show all steps using pen and
paper.
1. let x = 3 in let y = x in f x y
2. let x = x in let y = x in f x y
3. let x = y in let y = x in f x y
4. let x = fun y -> x in x
....................................................................*)
(*....................................................................
Exercise 12: What expressions are specified by the following
substitutions? Show all the steps as per the definition of
substitution given in the textbook, Figure 13.3.
1. (x + 1)[x |-> 50]
2. (x + 1)[y |-> 50]
3. (x * x)[x |-> 2]
4. (let x = y * y in x + x)[x |-> 3]
5. (let x = y * y in x + x)[y |-> 3]
....................................................................*)
(*......................................................................
Exercise 13: For each of the following expressions, derive its final
value using the evaluation rules in the textbook. Show all steps using
pen and paper, and label them with the name of the evaluation rule
used. Where an expression makes use of the evaluation of an earlier
expression, you don't need to rederive the earlier expression's value;
just use it directly.
1. 2 * 25
2. let x = 2 * 25 in x + 1
3. let x = 2 in x * x
4. let x = 51 in let x = 124 in x
......................................................................*)
(*====================================================================
Part 3: Implementing a simple arithmetic language.
You will now implement a simple language for evaluating let bindings
and arithmetic expressions. Recall the following abstract syntax for
such a language from the textbook.
<binop> ::= + | - | * | /
<var> ::= x | y | z | ...
<expr> ::= <integer>
| <var>
| <expr1> <binop> <expr>
| <var> = <expr_def> in <expr_body>
......................................................................
Exercise 14: Augment the provided type definitions to allow for other
binary operations (at least Minus and Times) and for unary operations
(at least Negate). Hint: Don't forget to extend the type definition
of expr to support unary operations as well.
....................................................................*)
type varspec = string ;;
type binop =
| Plus
| Minus
| Times
| Divide ;;
type unop =
| Negate ;;
type expr =
| Int of int
| Var of varspec
| Binop of binop * expr * expr
| Unop of unop * expr
| Let of varspec * expr * expr ;;
(*....................................................................
Exercise 15: Write a function free_vars : expr -> varspec Set.t that
returns a set of varspecs corresponding to the free variables in the
expression.
The free variable rules in this simple language are a subset of those
found in Figure 13.3, but we encourage you to first try to determine
the rules on your own, consulting the textbook only as
necessary.
You'll need to use the Set module for this exercise to complete the
definition of the VarSet module by using the Set.Make functor. More
documentation on the Set module can be found at:
https://caml.inria.fr/pub/docs/manual-ocaml/libref/Set.html
You should get behavior such as this, in calculating the free
variables in the expression
let x = x + y in z * 3 :
# VarSet.elements
(free_vars (Let ("x",
Binop (Plus, Var "x", Var "y"),
Binop (Times, Var "z", Int 3)))) ;;
- : Lab9_soln.VarSet.elt list = ["x"; "y"; "z"]
....................................................................*)
module VarSet = Set.Make (struct
type t = varspec
let compare = String.compare
end) ;;
let rec free_vars (exp : expr) : VarSet.t =
match exp with
| Var x -> VarSet.singleton x
| Int _ -> VarSet.empty
| Unop(_, arg) -> free_vars arg
| Binop(_, arg1, arg2) ->
VarSet.union (free_vars arg1) (free_vars arg2)
| Let(x, def, body) ->
VarSet.union (free_vars def) (VarSet.remove x (free_vars body))
;;
(*......................................................................
Exercise 16: Write a function subst : expr -> varspec -> expr -> expr
that performs substitution, that is, subst p x q returns the
expression that is the result of substituting q for the variable x in
the expression p.
The necessary substitution rules for this simple language are as
follows:
m[x |-> P] = m (where m is some integer value)
x[x |-> P] = P
y[x |-> P] = y
(Q + R)[x |-> P] = Q[x |-> P] + R[x |-> P]
(and similarly for other binary ops)
(let x = Q in R)[x |-> P] = let x = Q[x |-> P] in R
(let y = Q in R)[x |-> P] = let y = Q[x |-> P] in R[x |-> P]
(where x does not equal y)
You should get the following behavior:
# let example = Let ("x", Binop (Plus, Var "x", Var "y"),
Binop (Times, Var "z", Var "x")) ;;
val example : Lab9_soln.expr =
Let ("x", Binop (Plus, Var "x", Var "y"), Binop (Times, Var "z", Var "x"))
# subst example "x" (Int 42) ;;
- : Lab9_soln.expr =
Let ("x", Binop (Plus, Int 42, Var "y"), Binop (Times, Var "z", Var "x"))
# subst example "y" (Int 42) ;;
- : Lab9_soln.expr =
Let ("x", Binop (Plus, Var "x", Int 42), Binop (Times, Var "z", Var "x"))
......................................................................*)
let subst (exp : expr) (var_name : varspec) (repl : expr) : expr =
let rec sub (exp : expr) : expr =
match exp with
| Var x ->
if x = var_name then repl else exp
| Int _ -> exp
| Unop(op, arg) -> Unop(op, sub arg)
| Binop(op, arg1, arg2) -> Binop(op, sub arg1, sub arg2)
| Let(x, def, body) ->
if x = var_name then Let(x, sub def, body)
else Let(x, sub def, sub body)
in
sub exp ;;
(*......................................................................
Exercise 17: Complete the eval function below. Try to implement these
functions from scratch. If you get stuck, however, a good (though
incomplete) start can be found in section 13.4.2 of the textbook.
......................................................................*)
(* Please use the provided exceptions as appropriate. *)
exception UnboundVariable of string ;;
exception IllFormed of string ;;
let binopeval (op : binop) (v1 : expr) (v2 : expr) : expr =
match op, v1, v2 with
| Plus, Int x1, Int x2 -> Int (x1 + x2)
| Plus, _, _ -> raise (IllFormed "can't add non-integers")
| Minus, Int x1, Int x2 -> Int (x1 - x2)
| Minus, _, _ -> raise (IllFormed "can't subtract non-integers")
| Times, Int x1, Int x2 -> Int (x1 * x2)
| Times, _, _ -> raise (IllFormed "can't multiply non-integers")
| Divide, Int x1, Int x2 -> Int (x1 / x2)
| Divide, _, _ -> raise (IllFormed "can't divide non-integers") ;;
let unopeval (op : unop) (e : expr) : expr =
match op, e with
| Negate, Int x -> Int (~- x)
| Negate, _ -> raise (IllFormed "can't negate non-integers")
let rec eval (e : expr) : expr =
match e with
| Int _ -> e
| Var x -> raise (UnboundVariable x)
| Unop (op, e1) -> unopeval op (eval e1)
| Binop (op, e1, e2) -> binopeval op (eval e1) (eval e2)
| Let (x, def, body) -> eval (subst body x (eval def)) ;;
(*......................................................................
Go ahead and test eval by evaluating some arithmetic expressions and
let bindings.
For instance, try the following:
# eval (Let ("x", Int 6,
Let ("y", Int 3,
Binop (Times, Var "x", Var "y")))) ;;
- : expr = Int 18
......................................................................*)