-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImpCEvalFun.html
513 lines (439 loc) · 54.6 KB
/
ImpCEvalFun.html
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="coqdoc.css" rel="stylesheet" type="text/css"/>
<title>ImpCEvalFun: Evaluation Function for Imp</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="page">
<div id="header">
</div>
<div id="main">
<h1 class="libtitle">ImpCEvalFun<span class="subtitle">Evaluation Function for Imp</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab442"></a><h1 class="section">Evaluation Function</h1>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <span class="id" type="var">Imp</span>.<br/>
<br/>
</div>
<div class="doc">
Here's a first try at an evaluation function for commands,
omitting <span class="inlinecode"><span class="id" type="var">WHILE</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">ceval_step1</span> (<span class="id" type="var">st</span> : <span class="id" type="var">state</span>) (<span class="id" type="var">c</span> : <span class="id" type="var">com</span>) : <span class="id" type="var">state</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">c</span> <span class="id" type="keyword">with</span> <br/>
| <span class="id" type="var">SKIP</span> ⇒ <br/>
<span class="id" type="var">st</span><br/>
| <span class="id" type="var">l</span> ::= <span class="id" type="var">a1</span> ⇒ <br/>
<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">l</span> (<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> <span class="id" type="var">a1</span>)<br/>
| <span class="id" type="var">c1</span> ;; <span class="id" type="var">c2</span> ⇒ <br/>
<span class="id" type="keyword">let</span> <span class="id" type="var">st'</span> := <span class="id" type="var">ceval_step1</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="keyword">in</span><br/>
<span class="id" type="var">ceval_step1</span> <span class="id" type="var">st'</span> <span class="id" type="var">c2</span><br/>
| <span class="id" type="var">IFB</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> <span class="id" type="var">c1</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">c2</span> <span class="id" type="var">FI</span> ⇒ <br/>
<span class="id" type="keyword">if</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span>) <br/>
<span class="id" type="keyword">then</span> <span class="id" type="var">ceval_step1</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <br/>
<span class="id" type="keyword">else</span> <span class="id" type="var">ceval_step1</span> <span class="id" type="var">st</span> <span class="id" type="var">c2</span><br/>
| <span class="id" type="var">WHILE</span> <span class="id" type="var">b1</span> <span class="id" type="var">DO</span> <span class="id" type="var">c1</span> <span class="id" type="var">END</span> ⇒ <br/>
<span class="id" type="var">st</span> <span class="comment">(* bogus *)</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
In a traditional functional programming language like ML or
Haskell we could write the WHILE case as follows:
<pre>
| WHILE b1 DO c1 END =>
if (beval st b1)
then ceval_step1 st (c1;; WHILE b1 DO c1 END)
else st
</pre>
Coq doesn't accept such a definition (<span class="inlinecode"><span class="id" type="var">Error</span>:</span> <span class="inlinecode"><span class="id" type="var">Cannot</span></span> <span class="inlinecode"><span class="id" type="var">guess</span></span>
<span class="inlinecode"><span class="id" type="var">decreasing</span></span> <span class="inlinecode"><span class="id" type="var">argument</span></span> <span class="inlinecode"><span class="id" type="var">of</span></span> <span class="inlinecode"><span class="id" type="var">fix</span></span>) because the function we want to
define is not guaranteed to terminate. Indeed, the changed
<span class="inlinecode"><span class="id" type="var">ceval_step1</span></span> function applied to the <span class="inlinecode"><span class="id" type="var">loop</span></span> program from <span class="inlinecode"><span class="id" type="var">Imp.v</span></span> would
never terminate. Since Coq is not just a functional programming
language, but also a consistent logic, any potentially
non-terminating function needs to be rejected. Here is an
invalid(!) Coq program showing what would go wrong if Coq allowed
non-terminating recursive functions:
<pre>
Fixpoint loop_false (n : nat) : False := loop_false n.
</pre>
That is, propositions like <span class="inlinecode"><span class="id" type="var">False</span></span> would become
provable (e.g. <span class="inlinecode"><span class="id" type="var">loop_false</span></span> <span class="inlinecode">0</span> would be a proof of <span class="inlinecode"><span class="id" type="var">False</span></span>), which
would be a disaster for Coq's logical consistency.
<div class="paragraph"> </div>
Thus, because it doesn't terminate on all inputs, the full version
of <span class="inlinecode"><span class="id" type="var">ceval_step1</span></span> cannot be written in Coq — at least not
without one additional trick...
<div class="paragraph"> </div>
Second try, using an extra numeric argument as a "step index" to
ensure that evaluation always terminates.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">ceval_step2</span> (<span class="id" type="var">st</span> : <span class="id" type="var">state</span>) (<span class="id" type="var">c</span> : <span class="id" type="var">com</span>) (<span class="id" type="var">i</span> : <span class="id" type="var">nat</span>) : <span class="id" type="var">state</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">i</span> <span class="id" type="keyword">with</span> <br/>
| <span class="id" type="var">O</span> ⇒ <span class="id" type="var">empty_state</span><br/>
| <span class="id" type="var">S</span> <span class="id" type="var">i'</span> ⇒<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">c</span> <span class="id" type="keyword">with</span> <br/>
| <span class="id" type="var">SKIP</span> ⇒ <br/>
<span class="id" type="var">st</span><br/>
| <span class="id" type="var">l</span> ::= <span class="id" type="var">a1</span> ⇒ <br/>
<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">l</span> (<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> <span class="id" type="var">a1</span>)<br/>
| <span class="id" type="var">c1</span> ;; <span class="id" type="var">c2</span> ⇒ <br/>
<span class="id" type="keyword">let</span> <span class="id" type="var">st'</span> := <span class="id" type="var">ceval_step2</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span> <span class="id" type="keyword">in</span><br/>
<span class="id" type="var">ceval_step2</span> <span class="id" type="var">st'</span> <span class="id" type="var">c2</span> <span class="id" type="var">i'</span> <br/>
| <span class="id" type="var">IFB</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> <span class="id" type="var">c1</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">c2</span> <span class="id" type="var">FI</span> ⇒ <br/>
<span class="id" type="keyword">if</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span>) <br/>
<span class="id" type="keyword">then</span> <span class="id" type="var">ceval_step2</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span> <br/>
<span class="id" type="keyword">else</span> <span class="id" type="var">ceval_step2</span> <span class="id" type="var">st</span> <span class="id" type="var">c2</span> <span class="id" type="var">i'</span><br/>
| <span class="id" type="var">WHILE</span> <span class="id" type="var">b1</span> <span class="id" type="var">DO</span> <span class="id" type="var">c1</span> <span class="id" type="var">END</span> ⇒ <br/>
<span class="id" type="keyword">if</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b1</span>) <br/>
<span class="id" type="keyword">then</span> <span class="id" type="keyword">let</span> <span class="id" type="var">st'</span> := <span class="id" type="var">ceval_step2</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span> <span class="id" type="keyword">in</span><br/>
<span class="id" type="var">ceval_step2</span> <span class="id" type="var">st'</span> <span class="id" type="var">c</span> <span class="id" type="var">i'</span><br/>
<span class="id" type="keyword">else</span> <span class="id" type="var">st</span><br/>
<span class="id" type="keyword">end</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
<i>Note</i>: It is tempting to think that the index <span class="inlinecode"><span class="id" type="var">i</span></span> here is
counting the "number of steps of evaluation." But if you look
closely you'll see that this is not the case: for example, in the
rule for sequencing, the same <span class="inlinecode"><span class="id" type="var">i</span></span> is passed to both recursive
calls. Understanding the exact way that <span class="inlinecode"><span class="id" type="var">i</span></span> is treated will be
important in the proof of <span class="inlinecode"><span class="id" type="var">ceval__ceval_step</span></span>, which is given as
an exercise below.
<div class="paragraph"> </div>
Third try, returning an <span class="inlinecode"><span class="id" type="var">option</span></span> <span class="inlinecode"><span class="id" type="var">state</span></span> instead of just a <span class="inlinecode"><span class="id" type="var">state</span></span>
so that we can distinguish between normal and abnormal
termination.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">ceval_step3</span> (<span class="id" type="var">st</span> : <span class="id" type="var">state</span>) (<span class="id" type="var">c</span> : <span class="id" type="var">com</span>) (<span class="id" type="var">i</span> : <span class="id" type="var">nat</span>) <br/>
: <span class="id" type="var">option</span> <span class="id" type="var">state</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">i</span> <span class="id" type="keyword">with</span> <br/>
| <span class="id" type="var">O</span> ⇒ <span class="id" type="var">None</span><br/>
| <span class="id" type="var">S</span> <span class="id" type="var">i'</span> ⇒<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">c</span> <span class="id" type="keyword">with</span> <br/>
| <span class="id" type="var">SKIP</span> ⇒ <br/>
<span class="id" type="var">Some</span> <span class="id" type="var">st</span><br/>
| <span class="id" type="var">l</span> ::= <span class="id" type="var">a1</span> ⇒ <br/>
<span class="id" type="var">Some</span> (<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">l</span> (<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> <span class="id" type="var">a1</span>))<br/>
| <span class="id" type="var">c1</span> ;; <span class="id" type="var">c2</span> ⇒ <br/>
<span class="id" type="keyword">match</span> (<span class="id" type="var">ceval_step3</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span>) <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">Some</span> <span class="id" type="var">st'</span> ⇒ <span class="id" type="var">ceval_step3</span> <span class="id" type="var">st'</span> <span class="id" type="var">c2</span> <span class="id" type="var">i'</span><br/>
| <span class="id" type="var">None</span> ⇒ <span class="id" type="var">None</span><br/>
<span class="id" type="keyword">end</span><br/>
| <span class="id" type="var">IFB</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> <span class="id" type="var">c1</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">c2</span> <span class="id" type="var">FI</span> ⇒ <br/>
<span class="id" type="keyword">if</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span>) <br/>
<span class="id" type="keyword">then</span> <span class="id" type="var">ceval_step3</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span> <br/>
<span class="id" type="keyword">else</span> <span class="id" type="var">ceval_step3</span> <span class="id" type="var">st</span> <span class="id" type="var">c2</span> <span class="id" type="var">i'</span><br/>
| <span class="id" type="var">WHILE</span> <span class="id" type="var">b1</span> <span class="id" type="var">DO</span> <span class="id" type="var">c1</span> <span class="id" type="var">END</span> ⇒ <br/>
<span class="id" type="keyword">if</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b1</span>) <br/>
<span class="id" type="keyword">then</span> <span class="id" type="keyword">match</span> (<span class="id" type="var">ceval_step3</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span>) <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">Some</span> <span class="id" type="var">st'</span> ⇒ <span class="id" type="var">ceval_step3</span> <span class="id" type="var">st'</span> <span class="id" type="var">c</span> <span class="id" type="var">i'</span><br/>
| <span class="id" type="var">None</span> ⇒ <span class="id" type="var">None</span><br/>
<span class="id" type="keyword">end</span><br/>
<span class="id" type="keyword">else</span> <span class="id" type="var">Some</span> <span class="id" type="var">st</span><br/>
<span class="id" type="keyword">end</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
</div>
<div class="doc">
We can improve the readability of this definition by introducing a
bit of auxiliary notation to hide the "plumbing" involved in
repeatedly matching against optional states.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "'LETOPT' x <== e1 'IN' e2" <br/>
:= (<span class="id" type="keyword">match</span> <span class="id" type="var">e1</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">Some</span> <span class="id" type="var">x</span> ⇒ <span class="id" type="var">e2</span><br/>
| <span class="id" type="var">None</span> ⇒ <span class="id" type="var">None</span><br/>
<span class="id" type="keyword">end</span>)<br/>
(<span class="id" type="var">right</span> <span class="id" type="var">associativity</span>, <span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 60).<br/>
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">ceval_step</span> (<span class="id" type="var">st</span> : <span class="id" type="var">state</span>) (<span class="id" type="var">c</span> : <span class="id" type="var">com</span>) (<span class="id" type="var">i</span> : <span class="id" type="var">nat</span>) <br/>
: <span class="id" type="var">option</span> <span class="id" type="var">state</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">i</span> <span class="id" type="keyword">with</span> <br/>
| <span class="id" type="var">O</span> ⇒ <span class="id" type="var">None</span><br/>
| <span class="id" type="var">S</span> <span class="id" type="var">i'</span> ⇒<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">c</span> <span class="id" type="keyword">with</span> <br/>
| <span class="id" type="var">SKIP</span> ⇒ <br/>
<span class="id" type="var">Some</span> <span class="id" type="var">st</span><br/>
| <span class="id" type="var">l</span> ::= <span class="id" type="var">a1</span> ⇒ <br/>
<span class="id" type="var">Some</span> (<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">l</span> (<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> <span class="id" type="var">a1</span>))<br/>
| <span class="id" type="var">c1</span> ;; <span class="id" type="var">c2</span> ⇒ <br/>
<span class="id" type="var">LETOPT</span> <span class="id" type="var">st'</span> <== <span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span> <span class="id" type="var">IN</span><br/>
<span class="id" type="var">ceval_step</span> <span class="id" type="var">st'</span> <span class="id" type="var">c2</span> <span class="id" type="var">i'</span><br/>
| <span class="id" type="var">IFB</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> <span class="id" type="var">c1</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">c2</span> <span class="id" type="var">FI</span> ⇒ <br/>
<span class="id" type="keyword">if</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span>) <br/>
<span class="id" type="keyword">then</span> <span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span> <br/>
<span class="id" type="keyword">else</span> <span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c2</span> <span class="id" type="var">i'</span><br/>
| <span class="id" type="var">WHILE</span> <span class="id" type="var">b1</span> <span class="id" type="var">DO</span> <span class="id" type="var">c1</span> <span class="id" type="var">END</span> ⇒ <br/>
<span class="id" type="keyword">if</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b1</span>) <br/>
<span class="id" type="keyword">then</span> <span class="id" type="var">LETOPT</span> <span class="id" type="var">st'</span> <== <span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span> <span class="id" type="var">IN</span><br/>
<span class="id" type="var">ceval_step</span> <span class="id" type="var">st'</span> <span class="id" type="var">c</span> <span class="id" type="var">i'</span><br/>
<span class="id" type="keyword">else</span> <span class="id" type="var">Some</span> <span class="id" type="var">st</span><br/>
<span class="id" type="keyword">end</span><br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">test_ceval</span> (<span class="id" type="var">st</span>:<span class="id" type="var">state</span>) (<span class="id" type="var">c</span>:<span class="id" type="var">com</span>) := <br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c</span> 500 <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">None</span> ⇒ <span class="id" type="var">None</span><br/>
| <span class="id" type="var">Some</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">Some</span> (<span class="id" type="var">st</span> <span class="id" type="var">X</span>, <span class="id" type="var">st</span> <span class="id" type="var">Y</span>, <span class="id" type="var">st</span> <span class="id" type="var">Z</span>)<br/>
<span class="id" type="keyword">end</span>.<br/>
<br/>
<span class="comment">(* Compute<br/>
(test_ceval empty_state <br/>
(X ::= ANum 2;;<br/>
IFB BLe (AId X) (ANum 1)<br/>
THEN Y ::= ANum 3 <br/>
ELSE Z ::= ANum 4<br/>
FI)).<br/>
====><br/>
Some (2, 0, 4) *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab443"></a><h4 class="section">Exercise: 2 stars (pup_to_n)</h4>
Write an Imp program that sums the numbers from <span class="inlinecode">1</span> to
<span class="inlinecode"><span class="id" type="var">X</span></span> (inclusive: <span class="inlinecode">1</span> <span class="inlinecode">+</span> <span class="inlinecode">2</span> <span class="inlinecode">+</span> <span class="inlinecode">...</span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">X</span></span>) in the variable <span class="inlinecode"><span class="id" type="var">Y</span></span>. Make sure
your solution satisfies the test that follows.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">pup_to_n</span> : <span class="id" type="var">com</span> := <br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="comment">(* <br/>
Example pup_to_n_1 : <br/>
test_ceval (update empty_state X 5) pup_to_n<br/>
= Some (0, 15, 0).<br/>
Proof. reflexivity. Qed.<br/>
*)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab444"></a><h4 class="section">Exercise: 2 stars, optional (peven)</h4>
Write a <span class="inlinecode"><span class="id" type="var">While</span></span> program that sets <span class="inlinecode"><span class="id" type="var">Z</span></span> to <span class="inlinecode">0</span> if <span class="inlinecode"><span class="id" type="var">X</span></span> is even and
sets <span class="inlinecode"><span class="id" type="var">Z</span></span> to <span class="inlinecode">1</span> otherwise. Use <span class="inlinecode"><span class="id" type="var">ceval_test</span></span> to test your
program.
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab445"></a><h1 class="section">Equivalence of Relational and Step-Indexed Evaluation</h1>
<div class="paragraph"> </div>
As with arithmetic and boolean expressions, we'd hope that
the two alternative definitions of evaluation actually boil down
to the same thing. This section shows that this is the case.
Make sure you understand the statements of the theorems and can
follow the structure of the proofs.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ceval_step__ceval</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span>,<br/>
(<span style="font-family: arial;">∃</span><span class="id" type="var">i</span>, <span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c</span> <span class="id" type="var">i</span> = <span class="id" type="var">Some</span> <span class="id" type="var">st'</span>) <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st'</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">i</span> <span class="id" type="var">E</span>].<br/>
<span class="id" type="tactic">clear</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">st'</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">st</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">c</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">i</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">i'</span> ].<br/>
<br/>
- <span class="comment">(* i = 0 -- contradictory *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>.<br/>
<br/>
- <span class="comment">(* i = S i' *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">c</span>; <br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">clear</span> <span class="id" type="var">H</span>.<br/>
+ <span class="comment">(* SKIP *)</span> <span class="id" type="tactic">apply</span> <span class="id" type="var">E_Skip</span>.<br/>
+ <span class="comment">(* ::= *)</span> <span class="id" type="tactic">apply</span> <span class="id" type="var">E_Ass</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<br/>
+ <span class="comment">(* ;; *)</span><br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i'</span>) <span class="id" type="var">eqn</span>:<span class="id" type="var">Heqr1</span>.<br/>
× <span class="comment">(* Evaluation of r1 terminates normally *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_Seq</span> <span class="id" type="keyword">with</span> <span class="id" type="var">s</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHi'</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">Heqr1</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHi'</span>. <span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H1</span>. <span class="id" type="tactic">assumption</span>.<br/>
× <span class="comment">(* Otherwise -- contradiction *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H1</span>.<br/>
<br/>
+ <span class="comment">(* IFB *)</span><br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span>) <span class="id" type="var">eqn</span>:<span class="id" type="var">Heqr</span>.<br/>
× <span class="comment">(* r = true *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_IfTrue</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">Heqr</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHi'</span>. <span class="id" type="tactic">assumption</span>.<br/>
× <span class="comment">(* r = false *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_IfFalse</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">Heqr</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHi'</span>. <span class="id" type="tactic">assumption</span>.<br/>
<br/>
+ <span class="comment">(* WHILE *)</span> <span class="id" type="tactic">destruct</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span>) <span class="id" type="var">eqn</span> :<span class="id" type="var">Heqr</span>.<br/>
× <span class="comment">(* r = true *)</span><br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c</span> <span class="id" type="var">i'</span>) <span class="id" type="var">eqn</span>:<span class="id" type="var">Heqr1</span>.<br/>
{ <span class="comment">(* r1 = Some s *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_WhileLoop</span> <span class="id" type="keyword">with</span> <span class="id" type="var">s</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">Heqr</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHi'</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">Heqr1</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHi'</span>. <span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H1</span>. <span class="id" type="tactic">assumption</span>. }<br/>
{ <span class="comment">(* r1 = None *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H1</span>. }<br/>
× <span class="comment">(* r = false *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H1</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E_WhileEnd</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">Heqr</span>. <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab446"></a><h4 class="section">Exercise: 4 stars (ceval_step__ceval_inf)</h4>
Write an informal proof of <span class="inlinecode"><span class="id" type="var">ceval_step__ceval</span></span>, following the
usual template. (The template for case analysis on an inductively
defined value should look the same as for induction, except that
there is no induction hypothesis.) Make your proof communicate
the main ideas to a human reader; do not simply transcribe the
steps of the formal proof.
<div class="paragraph"> </div>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ceval_step_more</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">i1</span> <span class="id" type="var">i2</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">i1</span> ≤ <span class="id" type="var">i2</span> <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c</span> <span class="id" type="var">i1</span> = <span class="id" type="var">Some</span> <span class="id" type="var">st'</span> <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c</span> <span class="id" type="var">i2</span> = <span class="id" type="var">Some</span> <span class="id" type="var">st'</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">i1</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">i1'</span>]; <span class="id" type="tactic">intros</span> <span class="id" type="var">i2</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">c</span> <span class="id" type="var">Hle</span> <span class="id" type="var">Hceval</span>.<br/>
- <span class="comment">(* i1 = 0 *)</span><br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hceval</span>.<br/>
- <span class="comment">(* i1 = S i1' *)</span><br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">i2</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">i2'</span>]. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hle</span>.<br/>
<span class="id" type="tactic">assert</span> (<span class="id" type="var">Hle'</span>: <span class="id" type="var">i1'</span> ≤ <span class="id" type="var">i2'</span>) <span class="id" type="tactic">by</span> <span class="id" type="tactic">omega</span>.<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">c</span>.<br/>
+ <span class="comment">(* SKIP *)</span><br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hceval</span>.<br/>
<span class="id" type="tactic">reflexivity</span>.<br/>
+ <span class="comment">(* ::= *)</span><br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hceval</span>.<br/>
<span class="id" type="tactic">reflexivity</span>.<br/>
+ <span class="comment">(* ;; *)</span><br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>. <span class="id" type="tactic">simpl</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c1</span> <span class="id" type="var">i1'</span>) <span class="id" type="var">eqn</span>:<span class="id" type="var">Heqst1'o</span>.<br/>
× <span class="comment">(* st1'o = Some *)</span><br/>
<span class="id" type="tactic">apply</span> (<span class="id" type="var">IHi1'</span> <span class="id" type="var">i2'</span>) <span class="id" type="keyword">in</span> <span class="id" type="var">Heqst1'o</span>; <span class="id" type="tactic">try</span> <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">Heqst1'o</span>. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>.<br/>
<span class="id" type="tactic">apply</span> (<span class="id" type="var">IHi1'</span> <span class="id" type="var">i2'</span>) <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>; <span class="id" type="tactic">try</span> <span class="id" type="tactic">assumption</span>.<br/>
× <span class="comment">(* st1'o = None *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">Hceval</span>.<br/>
<br/>
+ <span class="comment">(* IFB *)</span><br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>. <span class="id" type="tactic">simpl</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span>); <span class="id" type="tactic">apply</span> (<span class="id" type="var">IHi1'</span> <span class="id" type="var">i2'</span>) <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>; <span class="id" type="tactic">assumption</span>.<br/>
<br/>
+ <span class="comment">(* WHILE *)</span><br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>. <span class="id" type="tactic">simpl</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span>); <span class="id" type="tactic">try</span> <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c</span> <span class="id" type="var">i1'</span>) <span class="id" type="var">eqn</span>: <span class="id" type="var">Heqst1'o</span>.<br/>
× <span class="comment">(* st1'o = Some *)</span><br/>
<span class="id" type="tactic">apply</span> (<span class="id" type="var">IHi1'</span> <span class="id" type="var">i2'</span>) <span class="id" type="keyword">in</span> <span class="id" type="var">Heqst1'o</span>; <span class="id" type="tactic">try</span> <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Heqst1'o</span>. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>.<br/>
<span class="id" type="tactic">apply</span> (<span class="id" type="var">IHi1'</span> <span class="id" type="var">i2'</span>) <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>; <span class="id" type="tactic">try</span> <span class="id" type="tactic">assumption</span>.<br/>
× <span class="comment">(* i1'o = None *)</span><br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">Hceval</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">Hceval</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab447"></a><h4 class="section">Exercise: 3 stars (ceval__ceval_step)</h4>
Finish the following proof. You'll need <span class="inlinecode"><span class="id" type="var">ceval_step_more</span></span> in a
few places, as well as some basic facts about <span class="inlinecode">≤</span> and <span class="inlinecode"><span class="id" type="var">plus</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ceval__ceval_step</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span>,<br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st'</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∃</span><span class="id" type="var">i</span>, <span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c</span> <span class="id" type="var">i</span> = <span class="id" type="var">Some</span> <span class="id" type="var">st'</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">Hce</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">Hce</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ceval_and_ceval_step_coincide</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span>,<br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st'</span><br/>
<span style="font-family: arial;">↔</span> <span style="font-family: arial;">∃</span><span class="id" type="var">i</span>, <span class="id" type="var">ceval_step</span> <span class="id" type="var">st</span> <span class="id" type="var">c</span> <span class="id" type="var">i</span> = <span class="id" type="var">Some</span> <span class="id" type="var">st'</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span>.<br/>
<span class="id" type="tactic">split</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">ceval__ceval_step</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">ceval_step__ceval</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab448"></a><h1 class="section">Determinism of Evaluation (Simpler Proof)</h1>
<div class="paragraph"> </div>
Here's a slicker proof showing that the evaluation relation is
deterministic, using the fact that the relational and step-indexed
definition of evaluation are the same.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ceval_deterministic'</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st1</span> <span class="id" type="var">st2</span>,<br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st1</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st2</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">st1</span> = <span class="id" type="var">st2</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st1</span> <span class="id" type="var">st2</span> <span class="id" type="var">He1</span> <span class="id" type="var">He2</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ceval__ceval_step</span> <span class="id" type="keyword">in</span> <span class="id" type="var">He1</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ceval__ceval_step</span> <span class="id" type="keyword">in</span> <span class="id" type="var">He2</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">He1</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">i1</span> <span class="id" type="var">E1</span>].<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">He2</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">i2</span> <span class="id" type="var">E2</span>].<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ceval_step_more</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">i2</span> := <span class="id" type="var">i1</span> + <span class="id" type="var">i2</span>) <span class="id" type="keyword">in</span> <span class="id" type="var">E1</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ceval_step_more</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">i2</span> := <span class="id" type="var">i1</span> + <span class="id" type="var">i2</span>) <span class="id" type="keyword">in</span> <span class="id" type="var">E2</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">E1</span> <span class="id" type="keyword">in</span> <span class="id" type="var">E2</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">E2</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">omega</span>. <span class="id" type="tactic">omega</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
</div>
</div>
<div id="footer">
<hr/><a href="coqindex.html">Index</a></div>
</div>
</body>
</html>