-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hoare.html
2855 lines (2331 loc) · 243 KB
/
Hoare.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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>Hoare: Hoare Logic, Part I</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">Hoare<span class="subtitle">Hoare Logic, Part I</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <span class="id" type="var">Imp</span>.<br/>
<br/>
</div>
<div class="doc">
In the past couple of chapters, we've begun applying the
mathematical tools developed in the first part of the course to
studying the theory of a small programming language, Imp.
<div class="paragraph"> </div>
<ul class="doclist">
<li> We defined a type of <i>abstract syntax trees</i> for Imp, together
with an <i>evaluation relation</i> (a partial function on states)
that specifies the <i>operational semantics</i> of programs.
<div class="paragraph"> </div>
The language we defined, though small, captures some of the key
features of full-blown languages like C, C++, and Java,
including the fundamental notion of mutable state and some
common control structures.
<div class="paragraph"> </div>
</li>
<li> We proved a number of <i>metatheoretic properties</i> — "meta" in
the sense that they are properties of the language as a whole,
rather than properties of particular programs in the language.
These included:
<div class="paragraph"> </div>
<ul class="doclist">
<li> determinism of evaluation
<div class="paragraph"> </div>
</li>
<li> equivalence of some different ways of writing down the
definitions (e.g. functional and relational definitions of
arithmetic expression evaluation)
<div class="paragraph"> </div>
</li>
<li> guaranteed termination of certain classes of programs
<div class="paragraph"> </div>
</li>
<li> correctness (in the sense of preserving meaning) of a number
of useful program transformations
<div class="paragraph"> </div>
</li>
<li> behavioral equivalence of programs (in the <span class="inlinecode"><span class="id" type="var">Equiv</span></span> chapter).
</li>
</ul>
</li>
</ul>
If we stopped here, we would already have something useful: a set
of tools for defining and discussing programming languages and
language features that are mathematically precise, flexible, and
easy to work with, applied to a set of key properties. All of
these properties are things that language designers, compiler
writers, and users might care about knowing. Indeed, many of them
are so fundamental to our understanding of the programming
languages we deal with that we might not consciously recognize
them as "theorems." But properties that seem intuitively obvious
can sometimes be quite subtle (in some cases, even subtly wrong!).
<div class="paragraph"> </div>
We'll return to the theme of metatheoretic properties of whole
languages later in the course when we discuss <i>types</i> and <i>type
soundness</i>. In this chapter, though, we'll turn to a different
set of issues.
<div class="paragraph"> </div>
Our goal is to see how to carry out some simple examples of
<i>program verification</i> — i.e., using the precise definition of
Imp to prove formally that particular programs satisfy particular
specifications of their behavior. We'll develop a reasoning system
called <i>Floyd-Hoare Logic</i> — often shortened to just <i>Hoare
Logic</i> — in which each of the syntactic constructs of Imp is
equipped with a single, generic "proof rule" that can be used to
reason compositionally about the correctness of programs involving
this construct.
<div class="paragraph"> </div>
Hoare Logic originates in the 1960s, and it continues to be the
subject of intensive research right up to the present day. It
lies at the core of a multitude of tools that are being used in
academia and industry to specify and verify real software
systems.
</div>
<div class="code code-tight">
<br/>
<br/>
</div>
<div class="doc">
<a name="lab514"></a><h1 class="section">Hoare Logic</h1>
<div class="paragraph"> </div>
Hoare Logic combines two beautiful ideas: a natural way of
writing down <i>specifications</i> of programs, and a <i>compositional
proof technique</i> for proving that programs are correct with
respect to such specifications — where by "compositional" we mean
that the structure of proofs directly mirrors the structure of the
programs that they are about.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab515"></a><h2 class="section">Assertions</h2>
<div class="paragraph"> </div>
To talk about specifications of programs, the first thing we
need is a way of making <i>assertions</i> about properties that hold at
particular points during a program's execution — i.e., claims
about the current state of the memory when program execution
reaches that point. Formally, an assertion is just a family of
propositions indexed by a <span class="inlinecode"><span class="id" type="var">state</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">Assertion</span> := <span class="id" type="var">state</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab516"></a><h4 class="section">Exercise: 1 star, optional (assertions)</h4>
</div>
<div class="code code-space">
<br/>
</div>
<div class="doc">
Paraphrase the following assertions in English.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">as1</span> : <span class="id" type="var">Assertion</span> := <span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">st</span> <span class="id" type="var">X</span> = 3.<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">as2</span> : <span class="id" type="var">Assertion</span> := <span class="id" type="keyword">fun</span> <span class="id" type="var">st</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>.<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">as3</span> : <span class="id" type="var">Assertion</span> :=<br/>
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">st</span> <span class="id" type="var">X</span> = 3 <span style="font-family: arial;">∨</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>.<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">as4</span> : <span class="id" type="var">Assertion</span> :=<br/>
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">st</span> <span class="id" type="var">Z</span> × <span class="id" type="var">st</span> <span class="id" type="var">Z</span> ≤ <span class="id" type="var">st</span> <span class="id" type="var">X</span> <span style="font-family: arial;">∧</span><br/>
¬ (((<span class="id" type="var">S</span> (<span class="id" type="var">st</span> <span class="id" type="var">Z</span>)) × (<span class="id" type="var">S</span> (<span class="id" type="var">st</span> <span class="id" type="var">Z</span>))) ≤ <span class="id" type="var">st</span> <span class="id" type="var">X</span>).<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">as5</span> : <span class="id" type="var">Assertion</span> := <span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">True</span>.<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">as6</span> : <span class="id" type="var">Assertion</span> := <span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">False</span>.<br/>
<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="lab517"></a><h2 class="section">Notation for Assertions</h2>
<div class="paragraph"> </div>
This way of writing assertions can be a little bit heavy,
for two reasons: (1) every single assertion that we ever write is
going to begin with <span class="inlinecode"><span class="id" type="keyword">fun</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode">⇒</span> <span class="inlinecode"></span>; and (2) this state <span class="inlinecode"><span class="id" type="var">st</span></span> is the
only one that we ever use to look up variables (we will never need
to talk about two different memory states at the same time). For
discussing examples informally, we'll adopt some simplifying
conventions: we'll drop the initial <span class="inlinecode"><span class="id" type="keyword">fun</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode">⇒</span>, and we'll write
just <span class="inlinecode"><span class="id" type="var">X</span></span> to mean <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span>. Thus, instead of writing
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ (<span class="id" type="var">st</span> <span class="id" type="var">Z</span>) × (<span class="id" type="var">st</span> <span class="id" type="var">Z</span>) ≤ <span class="id" type="var">m</span> <span style="font-family: arial;">∧</span><br/>
¬ ((<span class="id" type="var">S</span> (<span class="id" type="var">st</span> <span class="id" type="var">Z</span>)) × (<span class="id" type="var">S</span> (<span class="id" type="var">st</span> <span class="id" type="var">Z</span>)) ≤ <span class="id" type="var">m</span>)
<div class="paragraph"> </div>
</div>
we'll write just
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">Z</span> × <span class="id" type="var">Z</span> ≤ <span class="id" type="var">m</span> <span style="font-family: arial;">∧</span> ~((<span class="id" type="var">S</span> <span class="id" type="var">Z</span>) × (<span class="id" type="var">S</span> <span class="id" type="var">Z</span>) ≤ <span class="id" type="var">m</span>).
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
Given two assertions <span class="inlinecode"><span class="id" type="var">P</span></span> and <span class="inlinecode"><span class="id" type="var">Q</span></span>, we say that <span class="inlinecode"><span class="id" type="var">P</span></span> <i>implies</i> <span class="inlinecode"><span class="id" type="var">Q</span></span>,
written <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span style="font-family: arial;">⇾</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> (in ASCII, <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode">-</span><span class="inlinecode">></span><span class="inlinecode">></span> <span class="inlinecode"><span class="id" type="var">Q</span></span>), if, whenever <span class="inlinecode"><span class="id" type="var">P</span></span>
holds in some state <span class="inlinecode"><span class="id" type="var">st</span></span>, <span class="inlinecode"><span class="id" type="var">Q</span></span> also holds.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">assert_implies</span> (<span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="var">Assertion</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">st</span>, <span class="id" type="var">P</span> <span class="id" type="var">st</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span> <span class="id" type="var">st</span>.<br/>
<br/>
<span class="id" type="keyword">Notation</span> "P <span style="font-family: arial;">⇾</span> Q" :=<br/>
(<span class="id" type="var">assert_implies</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span>) (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 80) : <span class="id" type="var">hoare_spec_scope</span>.<br/>
<span class="id" type="keyword">Open</span> <span class="id" type="keyword">Scope</span> <span class="id" type="var">hoare_spec_scope</span>.<br/>
<br/>
</div>
<div class="doc">
We'll also have occasion to use the "iff" variant of implication
between assertions:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "P <span style="font-family: arial;">⇿</span> Q" :=<br/>
(<span class="id" type="var">P</span> <span style="font-family: arial;">⇾</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">⇾</span> <span class="id" type="var">P</span>) (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 80) : <span class="id" type="var">hoare_spec_scope</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab518"></a><h2 class="section">Hoare Triples</h2>
<div class="paragraph"> </div>
Next, we need a way of making formal claims about the
behavior of commands.
<div class="paragraph"> </div>
Since the behavior of a command is to transform one state to
another, it is natural to express claims about commands in terms
of assertions that are true before and after the command executes:
<div class="paragraph"> </div>
<ul class="doclist">
<li> "If command <span class="inlinecode"><span class="id" type="var">c</span></span> is started in a state satisfying assertion
<span class="inlinecode"><span class="id" type="var">P</span></span>, and if <span class="inlinecode"><span class="id" type="var">c</span></span> eventually terminates in some final state,
then this final state will satisfy the assertion <span class="inlinecode"><span class="id" type="var">Q</span></span>."
</li>
</ul>
<div class="paragraph"> </div>
Such a claim is called a <i>Hoare Triple</i>. The property <span class="inlinecode"><span class="id" type="var">P</span></span> is
called the <i>precondition</i> of <span class="inlinecode"><span class="id" type="var">c</span></span>, while <span class="inlinecode"><span class="id" type="var">Q</span></span> is the
<i>postcondition</i>. Formally:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">hoare_triple</span><br/>
(<span class="id" type="var">P</span>:<span class="id" type="var">Assertion</span>) (<span class="id" type="var">c</span>:<span class="id" type="var">com</span>) (<span class="id" type="var">Q</span>:<span class="id" type="var">Assertion</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
<span style="font-family: arial;">∀</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 class="id" type="var">P</span> <span class="id" type="var">st</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">Q</span> <span class="id" type="var">st'</span>.<br/>
<br/>
</div>
<div class="doc">
Since we'll be working a lot with Hoare triples, it's useful to
have a compact notation:
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>}.
<div class="paragraph"> </div>
</div>
(The traditional notation is <span class="inlinecode">{<span class="id" type="var">P</span>}</span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode">{<span class="id" type="var">Q</span>}</span>, but single braces
are already used for other things in Coq.)
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "<span style="letter-spacing:-.4em;">{</span>{ P <span style="letter-spacing:-.4em;">}</span>} c <span style="letter-spacing:-.4em;">{</span>{ Q <span style="letter-spacing:-.4em;">}</span>}" :=<br/>
(<span class="id" type="var">hoare_triple</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span>) (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 90, <span class="id" type="var">c</span> <span class="id" type="tactic">at</span> <span class="id" type="var">next</span> <span class="id" type="var">level</span>)<br/>
: <span class="id" type="var">hoare_spec_scope</span>.<br/>
<br/>
</div>
<div class="doc">
(The <span class="inlinecode"><span class="id" type="var">hoare_spec_scope</span></span> annotation here tells Coq that this
notation is not global but is intended to be used in particular
contexts. The <span class="inlinecode"><span class="id" type="keyword">Open</span></span> <span class="inlinecode"><span class="id" type="keyword">Scope</span></span> tells Coq that this file is one such
context.)
<div class="paragraph"> </div>
<a name="lab519"></a><h4 class="section">Exercise: 1 star, optional (triples)</h4>
Paraphrase the following Hoare triples in English.
<div class="paragraph"> </div>
<div class="code code-tight">
1) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 5<span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
2) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = <span class="id" type="var">m</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = <span class="id" type="var">m</span> + 5)<span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
3) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> ≤ <span class="id" type="var">Y</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Y</span> ≤ <span class="id" type="var">X</span><span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
4) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">False</span><span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
5) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = <span class="id" type="var">m</span><span style="letter-spacing:-.4em;">}</span>} <br/>
<span class="id" type="var">c</span><br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Y</span> = <span class="id" type="var">real_fact</span> <span class="id" type="var">m</span><span style="letter-spacing:-.4em;">}</span>}.<br/>
<br/>
6) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>} <br/>
<span class="id" type="var">c</span> <br/>
<span style="letter-spacing:-.4em;">{</span>{(<span class="id" type="var">Z</span> × <span class="id" type="var">Z</span>) ≤ <span class="id" type="var">m</span> <span style="font-family: arial;">∧</span> ¬ (((<span class="id" type="var">S</span> <span class="id" type="var">Z</span>) × (<span class="id" type="var">S</span> <span class="id" type="var">Z</span>)) ≤ <span class="id" type="var">m</span>)<span style="letter-spacing:-.4em;">}</span>}
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab520"></a><h4 class="section">Exercise: 1 star, optional (valid_triples)</h4>
Which of the following Hoare triples are <i>valid</i> — i.e., the
claimed relation between <span class="inlinecode"><span class="id" type="var">P</span></span>, <span class="inlinecode"><span class="id" type="var">c</span></span>, and <span class="inlinecode"><span class="id" type="var">Q</span></span> is true?
<div class="paragraph"> </div>
<div class="code code-tight">
1) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= 5 <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 5<span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
2) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 2<span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1 <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 3<span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
3) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= 5; <span class="id" type="var">Y</span> ::= 0 <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 5<span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
4) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 2 <span style="font-family: arial;">∧</span> <span class="id" type="var">X</span> = 3<span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= 5 <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 0<span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
5) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">SKIP</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">False</span><span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
6) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">False</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">SKIP</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
7) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">WHILE</span> <span class="id" type="var">True</span> <span class="id" type="var">DO</span> <span class="id" type="var">SKIP</span> <span class="id" type="var">END</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">False</span><span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
8) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 0<span style="letter-spacing:-.4em;">}</span>}<br/>
<span class="id" type="var">WHILE</span> <span class="id" type="var">X</span> == 0 <span class="id" type="var">DO</span> <span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1 <span class="id" type="var">END</span><br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 1<span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
9) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 1<span style="letter-spacing:-.4em;">}</span>}<br/>
<span class="id" type="var">WHILE</span> <span class="id" type="var">X</span> ≠ 0 <span class="id" type="var">DO</span> <span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1 <span class="id" type="var">END</span><br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 100<span style="letter-spacing:-.4em;">}</span>}
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
</div>
<div class="code code-tight">
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
(Note that we're using informal mathematical notations for
expressions inside of commands, for readability, rather than their
formal <span class="inlinecode"><span class="id" type="var">aexp</span></span> and <span class="inlinecode"><span class="id" type="var">bexp</span></span> encodings. We'll continue doing so
throughout the chapter.)
<div class="paragraph"> </div>
To get us warmed up for what's coming, here are two simple
facts about Hoare triples.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">hoare_post_true</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="var">Assertion</span>) <span class="id" type="var">c</span>,<br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">st</span>, <span class="id" type="var">Q</span> <span class="id" type="var">st</span>) <span style="font-family: arial;">→</span><br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>}.<br/>
<div class="togglescript" id="proofcontrol1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')"><span class="show"></span></div>
<div class="proofscript" id="proof1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">c</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">hoare_triple</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">Heval</span> <span class="id" type="var">HP</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">hoare_pre_false</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="var">Assertion</span>) <span class="id" type="var">c</span>,<br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">st</span>, ~(<span class="id" type="var">P</span> <span class="id" type="var">st</span>)) <span style="font-family: arial;">→</span><br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>}.<br/>
<div class="togglescript" id="proofcontrol2" onclick="toggleDisplay('proof2');toggleDisplay('proofcontrol2')"><span class="show"></span></div>
<div class="proofscript" id="proof2" onclick="toggleDisplay('proof2');toggleDisplay('proofcontrol2')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">c</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">hoare_triple</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">Heval</span> <span class="id" type="var">HP</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">not</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span> <span class="id" type="keyword">in</span> <span class="id" type="var">HP</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">HP</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<a name="lab521"></a><h2 class="section">Proof Rules</h2>
<div class="paragraph"> </div>
The goal of Hoare logic is to provide a <i>compositional</i>
method for proving the validity of Hoare triples. That is, the
structure of a program's correctness proof should mirror the
structure of the program itself. To this end, in the sections
below, we'll introduce one rule for reasoning about each of the
different syntactic forms of commands in Imp — one for
assignment, one for sequencing, one for conditionals, etc. — plus
a couple of "structural" rules that are useful for gluing things
together. We will prove programs correct using these proof rules,
without ever unfolding the definition of <span class="inlinecode"><span class="id" type="var">hoare_triple</span></span>.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab522"></a><h3 class="section">Assignment</h3>
<div class="paragraph"> </div>
The rule for assignment is the most fundamental of the Hoare logic
proof rules. Here's how it works.
<div class="paragraph"> </div>
Consider this (valid) Hoare triple:
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">Y</span> = 1 <span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= <span class="id" type="var">Y</span> <span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">X</span> = 1 <span style="letter-spacing:-.4em;">}</span>}
<div class="paragraph"> </div>
</div>
In English: if we start out in a state where the value of <span class="inlinecode"><span class="id" type="var">Y</span></span>
is <span class="inlinecode">1</span> and we assign <span class="inlinecode"><span class="id" type="var">Y</span></span> to <span class="inlinecode"><span class="id" type="var">X</span></span>, then we'll finish in a
state where <span class="inlinecode"><span class="id" type="var">X</span></span> is <span class="inlinecode">1</span>. That is, the property of being equal
to <span class="inlinecode">1</span> gets transferred from <span class="inlinecode"><span class="id" type="var">Y</span></span> to <span class="inlinecode"><span class="id" type="var">X</span></span>.
<div class="paragraph"> </div>
Similarly, in
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">Y</span> + <span class="id" type="var">Z</span> = 1 <span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= <span class="id" type="var">Y</span> + <span class="id" type="var">Z</span> <span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">X</span> = 1 <span style="letter-spacing:-.4em;">}</span>}
<div class="paragraph"> </div>
</div>
the same property (being equal to one) gets transferred to
<span class="inlinecode"><span class="id" type="var">X</span></span> from the expression <span class="inlinecode"><span class="id" type="var">Y</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">Z</span></span> on the right-hand side of
the assignment.
<div class="paragraph"> </div>
More generally, if <span class="inlinecode"><span class="id" type="var">a</span></span> is <i>any</i> arithmetic expression, then
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">a</span> = 1 <span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= <span class="id" type="var">a</span> <span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">X</span> = 1 <span style="letter-spacing:-.4em;">}</span>}
<div class="paragraph"> </div>
</div>
is a valid Hoare triple.
<div class="paragraph"> </div>
This can be made even more general. To conclude that an
<i>arbitrary</i> property <span class="inlinecode"><span class="id" type="var">Q</span></span> holds after <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode">::=</span> <span class="inlinecode"><span class="id" type="var">a</span></span>, we need to assume
that <span class="inlinecode"><span class="id" type="var">Q</span></span> holds before <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode">::=</span> <span class="inlinecode"><span class="id" type="var">a</span></span>, but <i>with all occurrences of</i> <span class="inlinecode"><span class="id" type="var">X</span></span>
replaced by <span class="inlinecode"><span class="id" type="var">a</span></span> in <span class="inlinecode"><span class="id" type="var">Q</span></span>. This leads to the Hoare rule for
assignment
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">Q</span> [<span class="id" type="var">X</span> <span style="font-family: arial;">↦</span> <span class="id" type="var">a</span>] <span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= <span class="id" type="var">a</span> <span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">Q</span> <span style="letter-spacing:-.4em;">}</span>}
<div class="paragraph"> </div>
</div>
where "<span class="inlinecode"><span class="id" type="var">Q</span></span> <span class="inlinecode">[<span class="id" type="var">X</span></span> <span class="inlinecode"><span style="font-family: arial;">↦</span></span> <span class="inlinecode"><span class="id" type="var">a</span>]</span>" is pronounced "<span class="inlinecode"><span class="id" type="var">Q</span></span> where <span class="inlinecode"><span class="id" type="var">a</span></span> is substituted
for <span class="inlinecode"><span class="id" type="var">X</span></span>".
<div class="paragraph"> </div>
For example, these are valid applications of the assignment
rule:
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="letter-spacing:-.4em;">{</span>{ (<span class="id" type="var">X</span> ≤ 5) [<span class="id" type="var">X</span> <span style="font-family: arial;">↦</span> <span class="id" type="var">X</span> + 1]<br/>
<span class="id" type="var">i.e</span>., <span class="id" type="var">X</span> + 1 ≤ 5 <span style="letter-spacing:-.4em;">}</span>} <br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1 <br/>
<span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">X</span> ≤ 5 <span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
<span style="letter-spacing:-.4em;">{</span>{ (<span class="id" type="var">X</span> = 3) [<span class="id" type="var">X</span> <span style="font-family: arial;">↦</span> 3]<br/>
<span class="id" type="var">i.e</span>., 3 = 3<span style="letter-spacing:-.4em;">}</span>} <br/>
<span class="id" type="var">X</span> ::= 3 <br/>
<span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">X</span> = 3 <span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
<span style="letter-spacing:-.4em;">{</span>{ (0 ≤ <span class="id" type="var">X</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">X</span> ≤ 5) [<span class="id" type="var">X</span> <span style="font-family: arial;">↦</span> 3]<br/>
<span class="id" type="var">i.e</span>., (0 ≤ 3 <span style="font-family: arial;">∧</span> 3 ≤ 5)<span style="letter-spacing:-.4em;">}</span>} <br/>
<span class="id" type="var">X</span> ::= 3 <br/>
<span style="letter-spacing:-.4em;">{</span>{ 0 ≤ <span class="id" type="var">X</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">X</span> ≤ 5 <span style="letter-spacing:-.4em;">}</span>}
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
To formalize the rule, we must first formalize the idea of
"substituting an expression for an Imp variable in an assertion."
That is, given a proposition <span class="inlinecode"><span class="id" type="var">P</span></span>, a variable <span class="inlinecode"><span class="id" type="var">X</span></span>, and an
arithmetic expression <span class="inlinecode"><span class="id" type="var">a</span></span>, we want to derive another proposition
<span class="inlinecode"><span class="id" type="var">P'</span></span> that is just the same as <span class="inlinecode"><span class="id" type="var">P</span></span> except that, wherever <span class="inlinecode"><span class="id" type="var">P</span></span>
mentions <span class="inlinecode"><span class="id" type="var">X</span></span>, <span class="inlinecode"><span class="id" type="var">P'</span></span> should instead mention <span class="inlinecode"><span class="id" type="var">a</span></span>.
<div class="paragraph"> </div>
Since <span class="inlinecode"><span class="id" type="var">P</span></span> is an arbitrary Coq proposition, we can't directly
"edit" its text. Instead, we can achieve the effect we want by
evaluating <span class="inlinecode"><span class="id" type="var">P</span></span> in an updated state:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">assn_sub</span> <span class="id" type="var">X</span> <span class="id" type="var">a</span> <span class="id" type="var">P</span> : <span class="id" type="var">Assertion</span> :=<br/>
<span class="id" type="keyword">fun</span> (<span class="id" type="var">st</span> : <span class="id" type="var">state</span>) ⇒<br/>
<span class="id" type="var">P</span> (<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> (<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> <span class="id" type="var">a</span>)).<br/>
<br/>
<span class="id" type="keyword">Notation</span> "P [ X |-> a ]" := (<span class="id" type="var">assn_sub</span> <span class="id" type="var">X</span> <span class="id" type="var">a</span> <span class="id" type="var">P</span>) (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 10).<br/>
<br/>
</div>
<div class="doc">
That is, <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode">[<span class="id" type="var">X</span></span> <span class="inlinecode"><span style="font-family: arial;">↦</span></span> <span class="inlinecode"><span class="id" type="var">a</span>]</span> is an assertion <span class="inlinecode"><span class="id" type="var">P'</span></span> that is just like <span class="inlinecode"><span class="id" type="var">P</span></span>
except that, wherever <span class="inlinecode"><span class="id" type="var">P</span></span> looks up the variable <span class="inlinecode"><span class="id" type="var">X</span></span> in the current
state, <span class="inlinecode"><span class="id" type="var">P'</span></span> instead uses the value of the expression <span class="inlinecode"><span class="id" type="var">a</span></span>.
<div class="paragraph"> </div>
To see how this works, let's calculate what happens with a couple
of examples. First, suppose <span class="inlinecode"><span class="id" type="var">P'</span></span> is <span class="inlinecode">(<span class="id" type="var">X</span></span> <span class="inlinecode">≤</span> <span class="inlinecode">5)</span> <span class="inlinecode">[<span class="id" type="var">X</span></span> <span class="inlinecode"><span style="font-family: arial;">↦</span></span> <span class="inlinecode">3]</span> — that
is, more formally, <span class="inlinecode"><span class="id" type="var">P'</span></span> is the Coq expression
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <br/>
(<span class="id" type="keyword">fun</span> <span class="id" type="var">st'</span> ⇒ <span class="id" type="var">st'</span> <span class="id" type="var">X</span> ≤ 5) <br/>
(<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> (<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> (<span class="id" type="var">ANum</span> 3))),
<div class="paragraph"> </div>
</div>
which simplifies to
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <br/>
(<span class="id" type="keyword">fun</span> <span class="id" type="var">st'</span> ⇒ <span class="id" type="var">st'</span> <span class="id" type="var">X</span> ≤ 5) <br/>
(<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> 3)
<div class="paragraph"> </div>
</div>
and further simplifies to
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <br/>
((<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> 3) <span class="id" type="var">X</span>) ≤ 5)
<div class="paragraph"> </div>
</div>
and by further simplification to
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <br/>
(3 ≤ 5).
<div class="paragraph"> </div>
</div>
That is, <span class="inlinecode"><span class="id" type="var">P'</span></span> is the assertion that <span class="inlinecode">3</span> is less than or equal to
<span class="inlinecode">5</span> (as expected).
<div class="paragraph"> </div>
For a more interesting example, suppose <span class="inlinecode"><span class="id" type="var">P'</span></span> is <span class="inlinecode">(<span class="id" type="var">X</span></span> <span class="inlinecode">≤</span> <span class="inlinecode">5)</span> <span class="inlinecode">[<span class="id" type="var">X</span></span> <span class="inlinecode"><span style="font-family: arial;">↦</span></span>
<span class="inlinecode"><span class="id" type="var">X</span>+1]</span>. Formally, <span class="inlinecode"><span class="id" type="var">P'</span></span> is the Coq expression
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <br/>
(<span class="id" type="keyword">fun</span> <span class="id" type="var">st'</span> ⇒ <span class="id" type="var">st'</span> <span class="id" type="var">X</span> ≤ 5) <br/>
(<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> (<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> (<span class="id" type="var">APlus</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) (<span class="id" type="var">ANum</span> 1)))),
<div class="paragraph"> </div>
</div>
which simplifies to
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <br/>
(((<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> (<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> (<span class="id" type="var">APlus</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) (<span class="id" type="var">ANum</span> 1))))) <span class="id" type="var">X</span>) ≤ 5
<div class="paragraph"> </div>
</div>
and further simplifies to
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <br/>
(<span class="id" type="var">aeval</span> <span class="id" type="var">st</span> (<span class="id" type="var">APlus</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) (<span class="id" type="var">ANum</span> 1))) ≤ 5.
<div class="paragraph"> </div>
</div>
That is, <span class="inlinecode"><span class="id" type="var">P'</span></span> is the assertion that <span class="inlinecode"><span class="id" type="var">X</span>+1</span> is at most <span class="inlinecode">5</span>.
<div class="paragraph"> </div>
<div class="paragraph"> </div>
Now we can give the precise proof rule for assignment:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(hoare_asgn)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="letter-spacing:-.4em;">{</span>{Q [X <span style="font-family: arial;">↦</span> a]<span style="letter-spacing:-.4em;">}</span>} X ::= a <span style="letter-spacing:-.4em;">{</span>{Q<span style="letter-spacing:-.4em;">}</span>}</td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
We can prove formally that this rule is indeed valid.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">hoare_asgn</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">Q</span> <span class="id" type="var">X</span> <span class="id" type="var">a</span>,<br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span> [<span class="id" type="var">X</span> <span style="font-family: arial;">↦</span> <span class="id" type="var">a</span>]<span style="letter-spacing:-.4em;">}</span>} (<span class="id" type="var">X</span> ::= <span class="id" type="var">a</span>) <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>}.<br/>
<div class="togglescript" id="proofcontrol3" onclick="toggleDisplay('proof3');toggleDisplay('proofcontrol3')"><span class="show"></span></div>
<div class="proofscript" id="proof3" onclick="toggleDisplay('proof3');toggleDisplay('proofcontrol3')">
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">hoare_triple</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">Q</span> <span class="id" type="var">X</span> <span class="id" type="var">a</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">HE</span> <span class="id" type="var">HQ</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">HE</span>. <span class="id" type="tactic">subst</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">assn_sub</span> <span class="id" type="keyword">in</span> <span class="id" type="var">HQ</span>. <span class="id" type="tactic">assumption</span>. <span class="id" type="keyword">Qed</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
Here's a first formal proof using this rule.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">assn_sub_example</span> :<br/>
<span style="letter-spacing:-.4em;">{</span>{(<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">st</span> <span class="id" type="var">X</span> = 3) [<span class="id" type="var">X</span> <span style="font-family: arial;">↦</span> <span class="id" type="var">ANum</span> 3]<span style="letter-spacing:-.4em;">}</span>}<br/>
(<span class="id" type="var">X</span> ::= (<span class="id" type="var">ANum</span> 3))<br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">st</span> <span class="id" type="var">X</span> = 3<span style="letter-spacing:-.4em;">}</span>}.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">hoare_asgn</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab523"></a><h4 class="section">Exercise: 2 stars (hoare_asgn_examples)</h4>
Translate these informal Hoare triples...
<div class="paragraph"> </div>
<div class="code code-tight">
1) <span style="letter-spacing:-.4em;">{</span>{ (<span class="id" type="var">X</span> ≤ 5) [<span class="id" type="var">X</span> <span style="font-family: arial;">↦</span> <span class="id" type="var">X</span> + 1] <span style="letter-spacing:-.4em;">}</span>}<br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">X</span> + 1<br/>
<span style="letter-spacing:-.4em;">{</span>{ <span class="id" type="var">X</span> ≤ 5 <span style="letter-spacing:-.4em;">}</span>}<br/>
<br/>
2) <span style="letter-spacing:-.4em;">{</span>{ (0 ≤ <span class="id" type="var">X</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">X</span> ≤ 5) [<span class="id" type="var">X</span> <span style="font-family: arial;">↦</span> 3] <span style="letter-spacing:-.4em;">}</span>}<br/>
<span class="id" type="var">X</span> ::= 3<br/>
<span style="letter-spacing:-.4em;">{</span>{ 0 ≤ <span class="id" type="var">X</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">X</span> ≤ 5 <span style="letter-spacing:-.4em;">}</span>}
<div class="paragraph"> </div>
</div>
...into formal statements <span class="inlinecode"><span class="id" type="var">assn_sub_ex1</span>,</span> <span class="inlinecode"><span class="id" type="var">assn_sub_ex2</span></span>
and use <span class="inlinecode"><span class="id" type="var">hoare_asgn</span></span> to prove them.
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab524"></a><h4 class="section">Exercise: 2 stars (hoare_asgn_wrong)</h4>
The assignment rule looks backward to almost everyone the first
time they see it. If it still seems backward to you, it may help
to think a little about alternative "forward" rules. Here is a
seemingly natural one:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(hoare_asgn_wrong)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="letter-spacing:-.4em;">{</span>{ True <span style="letter-spacing:-.4em;">}</span>} X ::= a <span style="letter-spacing:-.4em;">{</span>{ X = a <span style="letter-spacing:-.4em;">}</span>}</td>
<td></td>
</td>
</table></center> Give a counterexample showing that this rule is incorrect
(informally). Hint: The rule universally quantifies over the
arithmetic expression <span class="inlinecode"><span class="id" type="var">a</span></span>, and your counterexample needs to
exhibit an <span class="inlinecode"><span class="id" type="var">a</span></span> for which the rule doesn't work.
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab525"></a><h4 class="section">Exercise: 3 stars, advanced (hoare_asgn_fwd)</h4>
However, using an auxiliary variable <span class="inlinecode"><span class="id" type="var">m</span></span> to remember the original
value of <span class="inlinecode"><span class="id" type="var">X</span></span> we can define a Hoare rule for assignment that does,
intuitively, "work forwards" rather than backwards.
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(hoare_asgn_fwd)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="letter-spacing:-.4em;">{</span>{fun st ⇒ P st <span style="font-family: arial;">∧</span> st X = m<span style="letter-spacing:-.4em;">}</span>}</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">X ::= a</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="letter-spacing:-.4em;">{</span>{fun st ⇒ P st' <span style="font-family: arial;">∧</span> st X = aeval st' a <span style="letter-spacing:-.4em;">}</span>}</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">(where st' = update st X m)</td>
<td></td>
</td>
</table></center> Note that we use the original value of <span class="inlinecode"><span class="id" type="var">X</span></span> to reconstruct the
state <span class="inlinecode"><span class="id" type="var">st'</span></span> before the assignment took place. Prove that this rule
is correct (the first hypothesis is the functional extensionality
axiom, which you will need at some point). Also note that this
rule is more complicated than <span class="inlinecode"><span class="id" type="var">hoare_asgn</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">hoare_asgn_fwd</span> :<br/>
(<span style="font-family: arial;">∀</span>{<span class="id" type="var">X</span> <span class="id" type="var">Y</span>: <span class="id" type="keyword">Type</span>} {<span class="id" type="var">f</span> <span class="id" type="var">g</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Y</span>},<br/>
(<span style="font-family: arial;">∀</span>(<span class="id" type="var">x</span>: <span class="id" type="var">X</span>), <span class="id" type="var">f</span> <span class="id" type="var">x</span> = <span class="id" type="var">g</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">f</span> = <span class="id" type="var">g</span>) <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">m</span> <span class="id" type="var">a</span> <span class="id" type="var">P</span>,<br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">P</span> <span class="id" type="var">st</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> = <span class="id" type="var">m</span><span style="letter-spacing:-.4em;">}</span>}<br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">a</span><br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">P</span> (<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> <span class="id" type="var">m</span>) <span style="font-family: arial;">∧</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> = <span class="id" type="var">aeval</span> (<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> <span class="id" type="var">m</span>) <span class="id" type="var">a</span> <span style="letter-spacing:-.4em;">}</span>}.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">functional_extensionality</span> <span class="id" type="var">m</span> <span class="id" type="var">a</span> <span class="id" type="var">P</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 class="paragraph"> </div>
<a name="lab526"></a><h4 class="section">Exercise: 2 stars, advanced (hoare_asgn_fwd_exists)</h4>
Another way to define a forward rule for assignment is to
existentially quantify over the previous value of the assigned
variable.
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(hoare_asgn_fwd_exists)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="letter-spacing:-.4em;">{</span>{fun st ⇒ P st<span style="letter-spacing:-.4em;">}</span>}</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">X ::= a</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule"><span style="letter-spacing:-.4em;">{</span>{fun st ⇒ <span style="font-family: arial;">∃</span>m, P (update st X m) <span style="font-family: arial;">∧</span></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">st X = aeval (update st X m) a <span style="letter-spacing:-.4em;">}</span>}</td>
<td></td>
</td>
</table></center>
</div>
<div class="code code-tight">
<span class="comment">(* This rule was proposed by Nick Giannarakis and Zoe Paraskevopoulou. *)</span><br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">hoare_asgn_fwd_exists</span> :<br/>
(<span style="font-family: arial;">∀</span>{<span class="id" type="var">X</span> <span class="id" type="var">Y</span>: <span class="id" type="keyword">Type</span>} {<span class="id" type="var">f</span> <span class="id" type="var">g</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Y</span>},<br/>
(<span style="font-family: arial;">∀</span>(<span class="id" type="var">x</span>: <span class="id" type="var">X</span>), <span class="id" type="var">f</span> <span class="id" type="var">x</span> = <span class="id" type="var">g</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">f</span> = <span class="id" type="var">g</span>) <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">a</span> <span class="id" type="var">P</span>,<br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">P</span> <span class="id" type="var">st</span><span style="letter-spacing:-.4em;">}</span>}<br/>
<span class="id" type="var">X</span> ::= <span class="id" type="var">a</span><br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span style="font-family: arial;">∃</span><span class="id" type="var">m</span>, <span class="id" type="var">P</span> (<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> <span class="id" type="var">m</span>) <span style="font-family: arial;">∧</span><br/>
<span class="id" type="var">st</span> <span class="id" type="var">X</span> = <span class="id" type="var">aeval</span> (<span class="id" type="var">update</span> <span class="id" type="var">st</span> <span class="id" type="var">X</span> <span class="id" type="var">m</span>) <span class="id" type="var">a</span> <span style="letter-spacing:-.4em;">}</span>}.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">functional_extensionality</span> <span class="id" type="var">a</span> <span class="id" type="var">P</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/>
</div>
<div class="doc">
<a name="lab527"></a><h3 class="section">Consequence</h3>
<div class="paragraph"> </div>
Sometimes the preconditions and postconditions we get from the
Hoare rules won't quite be the ones we want in the particular
situation at hand — they may be logically equivalent but have a
different syntactic form that fails to unify with the goal we are
trying to prove, or they actually may be logically weaker (for
preconditions) or stronger (for postconditions) than what we need.
<div class="paragraph"> </div>
For instance, while
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="letter-spacing:-.4em;">{</span>{(<span class="id" type="var">X</span> = 3) [<span class="id" type="var">X</span> <span style="font-family: arial;">↦</span> 3]<span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= 3 <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 3<span style="letter-spacing:-.4em;">}</span>},
<div class="paragraph"> </div>
</div>
follows directly from the assignment rule,
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span> ::= 3 <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span> = 3<span style="letter-spacing:-.4em;">}</span>}.
<div class="paragraph"> </div>
</div>
does not. This triple is valid, but it is not an instance of
<span class="inlinecode"><span class="id" type="var">hoare_asgn</span></span> because <span class="inlinecode"><span class="id" type="var">True</span></span> and <span class="inlinecode">(<span class="id" type="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode">3)</span> <span class="inlinecode">[<span class="id" type="var">X</span></span> <span class="inlinecode"><span style="font-family: arial;">↦</span></span> <span class="inlinecode">3]</span> are not
syntactically equal assertions. However, they are logically
equivalent, so if one triple is valid, then the other must
certainly be as well. We might capture this observation with the
following rule:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"><span style="letter-spacing:-.4em;">{</span>{P'<span style="letter-spacing:-.4em;">}</span>} c <span style="letter-spacing:-.4em;">{</span>{Q<span style="letter-spacing:-.4em;">}</span>}</td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">P <span style="font-family: arial;">⇿</span> P'</td>
<td class="infrulenamecol" rowspan="3">
(hoare_consequence_pre_equiv)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style="letter-spacing:-.4em;">{</span>{P<span style="letter-spacing:-.4em;">}</span>} c <span style="letter-spacing:-.4em;">{</span>{Q<span style="letter-spacing:-.4em;">}</span>}</td>
<td></td>
</td>
</table></center> Taking this line of thought a bit further, we can see that
strengthening the precondition or weakening the postcondition of a
valid triple always produces another valid triple. This