-
Notifications
You must be signed in to change notification settings - Fork 7
/
Lab4.v
486 lines (406 loc) · 15.6 KB
/
Lab4.v
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
(** * 6.887 Formal Reasoning About Programs - Lab 4
* Operational Semantics *)
Require Import Frap.
(* Authors: Peng Wang ([email protected]), Adam Chlipala ([email protected]) *)
(** Challenge 1: Nondeterminism *)
Module Challenge1.
(* Here's the same old arithmetic-expression language. *)
Inductive arith : Set :=
| Const (n : nat)
| Var (x : var)
| Plus (e1 e2 : arith)
| Minus (e1 e2 : arith)
| Times (e1 e2 : arith).
(* And here's the same old command language, with one twist. *)
Inductive cmd :=
| Skip
| Assign (x : var) (e : arith)
| Sequence (c1 c2 : cmd)
| If (e : arith) (then_ else_ : cmd)
| While (e : arith) (body : cmd)
| Choice (c1 c2 : cmd).
(* The last construct [Choice c1 c2] stands for a *nondeterministic choice* at
* runtime. Every time we reach this command, we may run either [c1] or [c2]
* (but not both). *)
(* Same old expression evaluation *)
Definition valuation := fmap var nat.
Fixpoint interp (e : arith) (v : valuation) : nat :=
match e with
| Const n => n
| Var x =>
match v $? x with
| None => 0
| Some n => n
end
| Plus e1 e2 => interp e1 v + interp e2 v
| Minus e1 e2 => interp e1 v - interp e2 v
| Times e1 e2 => interp e1 v * interp e2 v
end.
(* THE CHALLENGE IS: update each semantics with appropriate rules for
* [Choice] and update the proofs below. *)
(** Big-step semantics *)
Inductive eval : valuation -> cmd -> valuation -> Prop :=
| EvalSkip : forall v,
eval v Skip v
| EvalAssign : forall v x e,
eval v (Assign x e) (v $+ (x, interp e v))
| EvalSeq : forall v c1 v1 c2 v2,
eval v c1 v1
-> eval v1 c2 v2
-> eval v (Sequence c1 c2) v2
| EvalIfTrue : forall v e then_ else_ v',
interp e v <> 0
-> eval v then_ v'
-> eval v (If e then_ else_) v'
| EvalIfFalse : forall v e then_ else_ v',
interp e v = 0
-> eval v else_ v'
-> eval v (If e then_ else_) v'
| EvalWhileTrue : forall v e body v' v'',
interp e v <> 0
-> eval v body v'
-> eval v' (While e body) v''
-> eval v (While e body) v''
| EvalWhileFalse : forall v e body,
interp e v = 0
-> eval v (While e body) v.
(** Small-step semantics *)
Inductive step : valuation * cmd -> valuation * cmd -> Prop :=
| StepAssign : forall v x e,
step (v, Assign x e) (v $+ (x, interp e v), Skip)
| StepSeq1 : forall v c1 c2 v' c1',
step (v, c1) (v', c1')
-> step (v, Sequence c1 c2) (v', Sequence c1' c2)
| StepSeq2 : forall v c2,
step (v, Sequence Skip c2) (v, c2)
| StepIfTrue : forall v e then_ else_,
interp e v <> 0
-> step (v, If e then_ else_) (v, then_)
| StepIfFalse : forall v e then_ else_,
interp e v = 0
-> step (v, If e then_ else_) (v, else_)
| StepWhileTrue : forall v e body,
interp e v <> 0
-> step (v, While e body) (v, Sequence body (While e body))
| StepWhileFalse : forall v e body,
interp e v = 0
-> step (v, While e body) (v, Skip).
Hint Constructors trc step eval.
Lemma step_star_Seq : forall v c1 c2 v' c1',
step^* (v, c1) (v', c1')
-> step^* (v, Sequence c1 c2) (v', Sequence c1' c2).
Proof.
induct 1; eauto.
cases y; eauto.
Qed.
Hint Resolve step_star_Seq.
Theorem big_small : forall v c v', eval v c v'
-> step^* (v, c) (v', Skip).
Proof.
induct 1; eauto 6 using trc_trans.
Qed.
Lemma small_big'' : forall v c v' c',
step (v, c) (v', c')
-> forall v'', eval v' c' v''
-> eval v c v''.
Proof.
induct 1; simplify;
repeat match goal with
| [ H : eval _ _ _ |- _ ] => invert1 H
end; eauto.
Qed.
Hint Resolve small_big''.
Lemma small_big' : forall v c v' c',
step^* (v, c) (v', c')
-> forall v'', eval v' c' v''
-> eval v c v''.
Proof.
induct 1; eauto.
cases y; eauto.
Qed.
Hint Resolve small_big'.
Theorem small_big : forall v c v',
step^* (v, c) (v', Skip)
-> eval v c v'.
Proof.
eauto.
Qed.
(** Contextual small-step semantics *)
Inductive context :=
| Hole
| CSeq (C : context) (c : cmd).
Inductive plug : context -> cmd -> cmd -> Prop :=
| PlugHole : forall c, plug Hole c c
| PlugSeq : forall c C c' c2,
plug C c c'
-> plug (CSeq C c2) c (Sequence c' c2).
Inductive step0 : valuation * cmd -> valuation * cmd -> Prop :=
| Step0Assign : forall v x e,
step0 (v, Assign x e) (v $+ (x, interp e v), Skip)
| Step0Seq : forall v c2,
step0 (v, Sequence Skip c2) (v, c2)
| Step0IfTrue : forall v e then_ else_,
interp e v <> 0
-> step0 (v, If e then_ else_) (v, then_)
| Step0IfFalse : forall v e then_ else_,
interp e v = 0
-> step0 (v, If e then_ else_) (v, else_)
| Step0WhileTrue : forall v e body,
interp e v <> 0
-> step0 (v, While e body) (v, Sequence body (While e body))
| Step0WhileFalse : forall v e body,
interp e v = 0
-> step0 (v, While e body) (v, Skip).
Inductive cstep : valuation * cmd -> valuation * cmd -> Prop :=
| CStep : forall C v c v' c' c1 c2,
plug C c c1
-> step0 (v, c) (v', c')
-> plug C c' c2
-> cstep (v, c1) (v', c2).
Hint Constructors plug step0 cstep.
Theorem step_cstep : forall v c v' c',
step (v, c) (v', c')
-> cstep (v, c) (v', c').
Proof.
induct 1; repeat match goal with
| [ H : cstep _ _ |- _ ] => invert H
end; eauto.
Qed.
Hint Resolve step_cstep.
Lemma step0_step : forall v c v' c',
step0 (v, c) (v', c')
-> step (v, c) (v', c').
Proof.
induct 1; eauto.
Qed.
Hint Resolve step0_step.
Lemma cstep_step' : forall C c0 c,
plug C c0 c
-> forall v' c'0 v c', step0 (v, c0) (v', c'0)
-> plug C c'0 c'
-> step (v, c) (v', c').
Proof.
induct 1; simplify; repeat match goal with
| [ H : plug _ _ _ |- _ ] => invert1 H
end; eauto.
Qed.
Hint Resolve cstep_step'.
Theorem cstep_step : forall v c v' c',
cstep (v, c) (v', c')
-> step (v, c) (v', c').
Proof.
induct 1; eauto.
Qed.
End Challenge1.
(** Challenge 2: Functions and Control Stack *)
(* This time, we give you the new semantics and ask you to fill in the
* proofs. *)
Module Challenge2.
Inductive arith : Set :=
| Const (n : nat)
| Var (x : var)
| Plus (e1 e2 : arith)
| Minus (e1 e2 : arith)
| Times (e1 e2 : arith).
Definition valuation := fmap var nat.
(* The new twist this time: function calls! *)
Inductive cmd :=
| Skip
| Assign (x : var) (e : arith)
| Sequence (c1 c2 : cmd)
| If (e : arith) (then_ else_ : cmd)
| While (e : arith) (body : cmd)
| Call (lhs f : var) (arg : arith)
(* Calling the function named [f] with the argument [arg], assigning the
* return value to [lhs] *)
| InCall (v : valuation) (lhs ret : var) (c : cmd).
(* This other command form only arises in the course of small-step evaluation.
* It means that a call to a function was made from a context with valuation
* [v], waiting to assign the return value to variable [lhs]. In the called
* function, variable [ret] is assigned to hold the return value, and we have
* executed that function up to command [c]. *)
Fixpoint interp (e : arith) (v : valuation) : nat :=
match e with
| Const n => n
| Var x =>
match v $? x with
| None => 0
| Some n => n
end
| Plus e1 e2 => interp e1 v + interp e2 v
| Minus e1 e2 => interp e1 v - interp e2 v
| Times e1 e2 => interp e1 v * interp e2 v
end.
(* Every function in the program is mapped to one of these. *)
Record fun_spec := {
Arg : var; (* Name of formal parameter *)
Ret : var; (* Variable where return value must be stored *)
Body : cmd (* Body to execute upon call *)
}.
(** An environment is a mapping from function names to function specs. *)
Definition environment := fmap var fun_spec.
Section env.
Variable env : environment.
(** Big-step semantics *)
Inductive eval : valuation -> cmd -> valuation -> Prop :=
| EvalSkip : forall v,
eval v Skip v
| EvalAssign : forall v x e,
eval v (Assign x e) (v $+ (x, interp e v))
| EvalSeq : forall v c1 v1 c2 v2,
eval v c1 v1
-> eval v1 c2 v2
-> eval v (Sequence c1 c2) v2
| EvalIfTrue : forall v e then_ else_ v',
interp e v <> 0
-> eval v then_ v'
-> eval v (If e then_ else_) v'
| EvalIfFalse : forall v e then_ else_ v',
interp e v = 0
-> eval v else_ v'
-> eval v (If e then_ else_) v'
| EvalWhileTrue : forall v e body v' v'',
interp e v <> 0
-> eval v body v'
-> eval v' (While e body) v''
-> eval v (While e body) v''
| EvalWhileFalse : forall v e body,
interp e v = 0
-> eval v (While e body) v
| EvalCall : forall v lhs f arg spec v',
env $? f = Some spec
(* When making a function call, look up the function spec by name. *)
-> eval ($0 $+ (spec.(Arg), interp arg v)) spec.(Body) v'
(* Then evaluate the callee's body, in a valuation with just the actual
* parameter value. *)
-> eval v (Call lhs f arg) (v $+ (lhs, interp (Var spec.(Ret)) v'))
(* Extend the original valuation with the function return value. *)
| EvalInCall : forall v0 lhs ret v c v',
eval v c v'
-> eval v (InCall v0 lhs ret c) (v0 $+ (lhs, interp (Var ret) v')).
(* This [InCall] case is just to make the proofs below go through nicely.
* We don't actually need [InCall] for big-step evaluation. *)
(** Small-step semantics *)
Inductive step : valuation * cmd -> valuation * cmd -> Prop :=
| StepAssign : forall v x e,
step (v, Assign x e) (v $+ (x, interp e v), Skip)
| StepSeq1 : forall v c1 c2 v' c1',
step (v, c1) (v', c1')
-> step (v, Sequence c1 c2) (v', Sequence c1' c2)
| StepSeq2 : forall v c2,
step (v, Sequence Skip c2) (v, c2)
| StepIfTrue : forall v e then_ else_,
interp e v <> 0
-> step (v, If e then_ else_) (v, then_)
| StepIfFalse : forall v e then_ else_,
interp e v = 0
-> step (v, If e then_ else_) (v, else_)
| StepWhileTrue : forall v e body,
interp e v <> 0
-> step (v, While e body) (v, Sequence body (While e body))
| StepWhileFalse : forall v e body,
interp e v = 0
-> step (v, While e body) (v, Skip)
| StepStartCall : forall v lhs f arg spec,
env $? f = Some spec
-> step (v, Call lhs f arg) ($0 $+ (spec.(Arg), interp arg v), InCall v lhs spec.(Ret) spec.(Body))
(* To begin with a function call, step to an [InCall], in a fresh
* valuation containing only the actual parameter value. Remember the
* current valuation as an argument to [InCall]. *)
| StepInCall : forall v c v' c' v0 lhs ret,
step (v, c) (v', c')
-> step (v, InCall v0 lhs ret c) (v', InCall v0 lhs ret c')
(* Basic congruence rule, for stepping with an [InCall], meaning that
* we take a step in the function that was called, leaving alone the
* "call stack" around it. *)
| StepEndCall : forall v0 lhs ret v,
step (v, InCall v0 lhs ret Skip) (v0, Assign lhs (Const (interp (Var ret) v))).
(* When does our [InCall] detour end? When the callee's body has
* stepped to [Skip]. Then we back up and assign to the caller's
* variable that has been waiting to receive the return value. *)
Theorem big_small : forall v c v',
eval v c v'
-> step^* (v, c) (v', Skip).
Proof.
Admitted.
Theorem small_big : forall v c v',
step^* (v, c) (v', Skip)
-> eval v c v'.
Proof.
Admitted.
(** Small-step semantics with control stack *)
(* As an alternative to evaluation contexts, we can also use an explicit
* representation of a call stack. First, a basic step relation that
* doesn't need a call stack. *)
Inductive step0 : valuation * cmd -> valuation * cmd -> Prop :=
| Step0Assign : forall v x e,
step0 (v, Assign x e) (v $+ (x, interp e v), Skip)
| Step0IfTrue : forall v e then_ else_,
interp e v <> 0
-> step0 (v, If e then_ else_) (v, then_)
| Step0IfFalse : forall v e then_ else_,
interp e v = 0
-> step0 (v, If e then_ else_) (v, else_)
| Step0WhileTrue : forall v e body,
interp e v <> 0
-> step0 (v, While e body) (v, Sequence body (While e body))
| Step0WhileFalse : forall v e body,
interp e v = 0
-> step0 (v, While e body) (v, Skip).
(* Our call stack will be a sequence of frames like these. *)
Record frame := {
Val : valuation; (* Local variable values *)
Cont : cmd; (* Command waiting to run after called function
* returns *)
LHS : var; (* Variable waiting to receive return value of called
* function *)
RetVar : var (* Place to store return value of this function *)
}.
Definition stack := list frame.
(* What are the four components of states in this semantics?
* #1: call stack
* #2: local variables of topmost call
* #3: command to run next
* #4: additional command to run afterward, called the *continuation* *)
Inductive stepcs : stack * valuation * cmd * cmd -> stack * valuation * cmd * cmd -> Prop :=
(* Easy case: just step the main command as usual *)
| StepcsStep0 : forall v c v' c' s k,
step0 (v, c) (v', c') ->
stepcs (s, v, c, k) (s, v', c', k)
(* Fancier case: when encountering a sequence, pop off its second step and
* push it onto the front of the continuation. *)
| StepcsSeq : forall s v c1 c2 k,
stepcs (s, v, Sequence c1 c2, k) (s, v, c1, Sequence c2 k)
(* Natural companion to last rule: when the main command is empty, start
* running the next command of the continuation, if one is available. *)
| StepcsCont : forall s v k1 k2,
stepcs (s, v, Skip, Sequence k1 k2) (s, v, k1, k2)
(* When both the main command and the continuation are [Skip], we're done
* running this function. Time to pop the call stack and return to the
* caller. *)
| StepcsReturn : forall fr s v,
stepcs (fr :: s, v, Skip, Skip) (s, fr.(Val) $+ (fr.(LHS), interp (Var fr.(RetVar)) v), Skip, fr.(Cont))
(* To make a cal, push a new frame onto the call stack. *)
| StepcsCall : forall s v lhs f arg k spec,
env $? f = Some spec
-> stepcs (s, v, Call lhs f arg, k) ({| Val := v; Cont := k; LHS := lhs; RetVar := spec.(Ret)|} :: s, $0 $+ (spec.(Arg), interp arg v), spec.(Body), Skip)
(* We don't actually need [InCall] in this semantics, but here's a single
* step to get rid of it. *)
| StepcsInCall : forall s v0 lhs ret v c k,
stepcs (s, v, InCall v0 lhs ret c, k) ({| Val := v0; Cont := k; LHS := lhs; RetVar := ret|} :: s, v, c, Skip).
Theorem eval_stepcs : forall v c v',
eval v c v'
-> stepcs^* ([], v, c, Skip) ([], v', Skip, Skip).
Proof.
Admitted.
Theorem stepcs_eval : forall v c v',
stepcs^* ([], v, c, Skip) ([], v', Skip, Skip)
-> eval v c v'.
Proof.
Admitted.
End env.
End Challenge2.
(* CHALLENGE #3: Extend the language with an explicit "return" command and add
* one rule to the control-stack semantics to explain its effect.
* The other semantics styles would need bigger modifications to handle
* "return," so here we see an advantage of the control-stack formulation. *)