-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoare.html
3192 lines (2754 loc) · 558 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="common/css/sf.css" rel="stylesheet" type="text/css" />
<title>Hoare: Hoare Logic, Part I</title>
<link href="common/jquery-ui/jquery-ui.css" rel="stylesheet">
<script src="common/jquery-ui/external/jquery/jquery.js"></script>
<script src="common/jquery-ui/jquery-ui.js"></script>
<script src="common/toggleproofs.js"></script>
<link href="common/css/plf.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page">
<div id="header">
<div id='logoinheader'><a href='https://softwarefoundations.cis.upenn.edu'>
<img src='common/media/image/sf_logo_sm.png' alt='Software Foundations Logo'></a></div>
<div class='booktitleinheader'><a href='index.html'>Volume 2: Programming Language Foundations</a></div>
<ul id='menu'>
<li class='section_name'><a href='toc.html'>Table of Contents</a></li>
<li class='section_name'><a href='coqindex.html'>Index</a></li>
<li class='section_name'><a href='deps.html'>Roadmap</a></li>
</ul>
</div>
<div id="main">
<h1 class="libtitle">Hoare<span class="subtitle">Hoare Logic, Part I</span></h1>
<div class="code">
<span class="id" title="keyword">Set</span> <span class="id" title="var">Warnings</span> "-notation-overridden,-parsing,-deprecated-hint-without-locality".<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">PLF</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <span class="id" title="library">Maps</span>.<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">Coq</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Bool.Bool.html#"><span class="id" title="library">Bool.Bool</span></a>.<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">Coq</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Arith.Arith.html#"><span class="id" title="library">Arith.Arith</span></a>.<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">Coq</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Arith.EqNat.html#"><span class="id" title="library">Arith.EqNat</span></a>.<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">Coq</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Arith.PeanoNat.html#"><span class="id" title="library">Arith.PeanoNat</span></a>. <span class="id" title="keyword">Import</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Arith.PeanoNat.html#Nat"><span class="id" title="module">Nat</span></a>.<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">Coq</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Import</span> <a class="idref" href="http://coq.inria.fr/library//Coq.micromega.Lia.html#"><span class="id" title="library">Lia</span></a>.<br/>
<span class="id" title="keyword">From</span> <span class="id" title="var">PLF</span> <span class="id" title="keyword">Require</span> <span class="id" title="keyword">Export</span> <span class="id" title="library">Imp</span>.<br/>
<span class="id" title="keyword">Set</span> <span class="id" title="var">Default</span> <span class="id" title="keyword">Goal</span> <span class="id" title="var">Selector</span> "!".<br/>
</div>
<div class="doc">
In the final chaper of <i>Logical Foundations</i> (<i>Software
Foundations</i>, volume 1), we began 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 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 <a href="Equiv.html"><span class="inlineref">Equiv</span></a>
chapter).
</li>
</ul>
</li>
</ul>
<div class="paragraph"> </div>
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 (sometimes also subtly wrong!).
<div class="paragraph"> </div>
We'll return to the theme of metatheoretic properties of whole
languages later in this volume when we discuss <i>types</i> and <i>type
soundness</i>. In this chapter, though, we turn to a different set
of issues.
<div class="paragraph"> </div>
Our goal in this chapter is to carry out some simple examples of
<i>program verification</i> -- i.e., to use the precise definition of
Imp to prove formally that particular programs satisfy particular
specifications of their behavior.
<div class="paragraph"> </div>
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 generic "proof
rule" that can be used to reason compositionally about the
correctness of programs involving this construct.
<div class="paragraph"> </div>
Hoare Logic originated 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 class="paragraph"> </div>
Hoare Logic combines two beautiful ideas: a natural way of writing
down <i>specifications</i> of programs, and a <i>structured proof
technique</i> for proving that programs are correct with respect to
such specifications -- where by "structured" we mean that the
structure of proofs directly mirrors the structure of the programs
that they are about.
</div>
<div class="doc">
<a id="lab52"></a><h1 class="section">Assertions</h1>
<div class="paragraph"> </div>
An <i>assertion</i> is a logical claim about the state of a program's
memory -- formally, a property of <span class="inlinecode"><span class="id" title="var">state</span></span>s.
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="Assertion" class="idref" href="#Assertion"><span class="id" title="definition">Assertion</span></a> := <span class="id" title="definition">state</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <span class="id" title="keyword">Prop</span>.<br/>
</div>
<div class="doc">
For example,
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" title="keyword">fun</span></span> <span class="inlinecode"><span class="id" title="var">st</span></span> <span class="inlinecode">⇒</span> <span class="inlinecode"><span class="id" title="var">st</span></span> <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode">3</span> holds for states <span class="inlinecode"><span class="id" title="var">st</span></span> in which value of <span class="inlinecode"><span class="id" title="var">X</span></span>
is <span class="inlinecode">3</span>,
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode"><span class="id" title="keyword">fun</span></span> <span class="inlinecode"><span class="id" title="var">st</span></span> <span class="inlinecode">⇒</span> <span class="inlinecode"><span class="id" title="var">True</span></span> hold for all states, and
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode"><span class="id" title="keyword">fun</span></span> <span class="inlinecode"><span class="id" title="var">st</span></span> <span class="inlinecode">⇒</span> <span class="inlinecode"><span class="id" title="var">False</span></span> holds for no states.
</li>
</ul>
<div class="paragraph"> </div>
<a id="lab53"></a><h4 class="section">Exercise: 1 star, standard, optional (assertions)</h4>
Paraphrase the following assertions in English (or your favorite
natural language).
</div>
<div class="code">
<span class="id" title="keyword">Module</span> <a id="ExAssertions" class="idref" href="#ExAssertions"><span class="id" title="module">ExAssertions</span></a>.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExAssertions.assertion1" class="idref" href="#ExAssertions.assertion1"><span class="id" title="definition">assertion1</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> := <span class="id" title="keyword">fun</span> <a id="st:1" class="idref" href="#st:1"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#st:1"><span class="id" title="variable">st</span></a> <span class="id" title="definition">X</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#cb53cf0ee22c036a03b4a9281c68b5a<sub>3</sub>"><span class="id" title="notation">≤</span></a> <a class="idref" href="Hoare.html#st:1"><span class="id" title="variable">st</span></a> <span class="id" title="definition">Y</span>.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExAssertions.assertion2" class="idref" href="#ExAssertions.assertion2"><span class="id" title="definition">assertion2</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> :=<br/>
<span class="id" title="keyword">fun</span> <a id="st:2" class="idref" href="#st:2"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#st:2"><span class="id" title="variable">st</span></a> <span class="id" title="definition">X</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> 3 <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#f031fe1957c4a4a8e217aa46af2b4e<sub>25</sub>"><span class="id" title="notation">∨</span></a> <a class="idref" href="Hoare.html#st:2"><span class="id" title="variable">st</span></a> <span class="id" title="definition">X</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#cb53cf0ee22c036a03b4a9281c68b5a<sub>3</sub>"><span class="id" title="notation">≤</span></a> <a class="idref" href="Hoare.html#st:2"><span class="id" title="variable">st</span></a> <span class="id" title="definition">Y</span>.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExAssertions.assertion3" class="idref" href="#ExAssertions.assertion3"><span class="id" title="definition">assertion3</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> :=<br/>
<span class="id" title="keyword">fun</span> <a id="st:3" class="idref" href="#st:3"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#st:3"><span class="id" title="variable">st</span></a> <span class="id" title="definition">Z</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Nat.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">×</span></a> <a class="idref" href="Hoare.html#st:3"><span class="id" title="variable">st</span></a> <span class="id" title="definition">Z</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#cb53cf0ee22c036a03b4a9281c68b5a<sub>3</sub>"><span class="id" title="notation">≤</span></a> <a class="idref" href="Hoare.html#st:3"><span class="id" title="variable">st</span></a> <span class="id" title="definition">X</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#ba2b0e492d2b4675a0acf3ea92aabadd"><span class="id" title="notation">∧</span></a><br/>
<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#63a68285c81db8f9bc456233bb9ed181"><span class="id" title="notation">¬</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#63a68285c81db8f9bc456233bb9ed181"><span class="id" title="notation">(</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#cb53cf0ee22c036a03b4a9281c68b5a<sub>3</sub>"><span class="id" title="notation">(</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Nat.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">(</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="Hoare.html#st:3"><span class="id" title="variable">st</span></a> <span class="id" title="definition">Z</span>)<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Nat.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Nat.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">×</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Nat.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">(</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> (<a class="idref" href="Hoare.html#st:3"><span class="id" title="variable">st</span></a> <span class="id" title="definition">Z</span>)<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Nat.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">)</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#cb53cf0ee22c036a03b4a9281c68b5a<sub>3</sub>"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#cb53cf0ee22c036a03b4a9281c68b5a<sub>3</sub>"><span class="id" title="notation">≤</span></a> <a class="idref" href="Hoare.html#st:3"><span class="id" title="variable">st</span></a> <span class="id" title="definition">X</span><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#63a68285c81db8f9bc456233bb9ed181"><span class="id" title="notation">)</span></a>.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExAssertions.assertion4" class="idref" href="#ExAssertions.assertion4"><span class="id" title="definition">assertion4</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> :=<br/>
<span class="id" title="keyword">fun</span> <a id="st:4" class="idref" href="#st:4"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#st:4"><span class="id" title="variable">st</span></a> <span class="id" title="definition">Z</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Arith.PeanoNat.html#Nat.max"><span class="id" title="definition">max</span></a> (<a class="idref" href="Hoare.html#st:4"><span class="id" title="variable">st</span></a> <span class="id" title="definition">X</span>) (<a class="idref" href="Hoare.html#st:4"><span class="id" title="variable">st</span></a> <span class="id" title="definition">Y</span>).<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
<span class="id" title="keyword">End</span> <a class="idref" href="Hoare.html#ExAssertions"><span class="id" title="module">ExAssertions</span></a>.<br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<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" title="keyword">fun</span></span> <span class="inlinecode"><span class="id" title="var">st</span></span> <span class="inlinecode">⇒</span> <span class="inlinecode"></span>; and (2) this state <span class="inlinecode"><span class="id" title="var">st</span></span> is the
only one that we ever use to look up variables in assertions (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" title="keyword">fun</span></span> <span class="inlinecode"><span class="id" title="var">st</span></span> <span class="inlinecode">⇒</span>, and
we'll write just <span class="inlinecode"><span class="id" title="var">X</span></span> to mean <span class="inlinecode"><span class="id" title="var">st</span></span> <span class="inlinecode"><span class="id" title="var">X</span></span>. Thus, instead of writing
<br/>
<span class="inlinecode"> <span class="id" title="keyword">fun</span> <span class="id" title="var">st</span> ⇒ <span class="id" title="var">st</span> <span class="id" title="var">X</span> = <span class="id" title="var">m</span>
</span> we'll write just
<br/>
<span class="inlinecode"> <span class="id" title="var">X</span> = <span class="id" title="var">m</span>.
</span>
<div class="paragraph"> </div>
This example also illustrates a convention that we'll use
throughout the Hoare Logic chapters: in informal assertions,
capital letters like <span class="inlinecode"><span class="id" title="var">X</span></span>, <span class="inlinecode"><span class="id" title="var">Y</span></span>, and <span class="inlinecode"><span class="id" title="var">Z</span></span> are Imp variables, while
lowercase letters like <span class="inlinecode"><span class="id" title="var">x</span></span>, <span class="inlinecode"><span class="id" title="var">y</span></span>, <span class="inlinecode"><span class="id" title="var">m</span></span>, and <span class="inlinecode"><span class="id" title="var">n</span></span> are ordinary Coq
variables (of type <span class="inlinecode"><span class="id" title="var">nat</span></span>). This is why, when translating from
informal to formal, we replace <span class="inlinecode"><span class="id" title="var">X</span></span> with <span class="inlinecode"><span class="id" title="var">st</span></span> <span class="inlinecode"><span class="id" title="var">X</span></span> but leave <span class="inlinecode"><span class="id" title="var">m</span></span>
alone.
<div class="paragraph"> </div>
Given two assertions <span class="inlinecode"><span class="id" title="var">P</span></span> and <span class="inlinecode"><span class="id" title="var">Q</span></span>, we say that <span class="inlinecode"><span class="id" title="var">P</span></span> <i>implies</i> <span class="inlinecode"><span class="id" title="var">Q</span></span>,
written <span class="inlinecode"><span class="id" title="var">P</span></span> <span class="inlinecode"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span><span style='letter-spacing:-.2em;'>></span>></span></span></span> <span class="inlinecode"><span class="id" title="var">Q</span></span>, if, whenever <span class="inlinecode"><span class="id" title="var">P</span></span> holds in some state <span class="inlinecode"><span class="id" title="var">st</span></span>, <span class="inlinecode"><span class="id" title="var">Q</span></span>
also holds.
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="assert_implies" class="idref" href="#assert_implies"><span class="id" title="definition">assert_implies</span></a> (<a id="P:5" class="idref" href="#P:5"><span class="id" title="binder">P</span></a> <a id="Q:6" class="idref" href="#Q:6"><span class="id" title="binder">Q</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a>) : <span class="id" title="keyword">Prop</span> :=<br/>
<span class="id" title="keyword">∀</span> <a id="st:7" class="idref" href="#st:7"><span class="id" title="binder">st</span></a>, <a class="idref" href="Hoare.html#P:5"><span class="id" title="variable">P</span></a> <a class="idref" href="Hoare.html#st:7"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Hoare.html#Q:6"><span class="id" title="variable">Q</span></a> <a class="idref" href="Hoare.html#st:7"><span class="id" title="variable">st</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Declare Scope</span> <span class="id" title="var">hoare_spec_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="::hoare_spec_scope:x_'->>'_x" class="idref" href="#::hoare_spec_scope:x_'->>'_x"><span class="id" title="notation">"</span></a>P <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span><span style='letter-spacing:-.2em;'>></span>></span></span> Q" := (<a class="idref" href="Hoare.html#assert_implies"><span class="id" title="definition">assert_implies</span></a> <span class="id" title="var">P</span> <span class="id" title="var">Q</span>)<br/>
(<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 80) : <span class="id" title="var">hoare_spec_scope</span>.<br/>
<span class="id" title="keyword">Open</span> <span class="id" title="keyword">Scope</span> <span class="id" title="var">hoare_spec_scope</span>.<br/>
</div>
<div class="doc">
(The <span class="inlinecode"><span class="id" title="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" title="keyword">Open</span></span> <span class="inlinecode"><span class="id" title="keyword">Scope</span></span> tells Coq that this file is one such
context.)
<div class="paragraph"> </div>
We'll also want the "iff" variant of implication between
assertions:
</div>
<div class="code">
<span class="id" title="keyword">Notation</span> <a id="::hoare_spec_scope:x_'<<->>'_x" class="idref" href="#::hoare_spec_scope:x_'<<->>'_x"><span class="id" title="notation">"</span></a>P <span class="nowrap"><span style='font-size:85%;'><span style='letter-spacing:-.2em;'><<</span><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span><span style='letter-spacing:-.2em;'>></span>></span></span> Q" := (<span class="id" title="var">P</span> <a class="idref" href="Hoare.html#::hoare_spec_scope:x_'->>'_x"><span class="id" title="notation"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span><span style='letter-spacing:-.2em;'>></span>></span></span></span></a> <span class="id" title="var">Q</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#ba2b0e492d2b4675a0acf3ea92aabadd"><span class="id" title="notation">∧</span></a> <span class="id" title="var">Q</span> <a class="idref" href="Hoare.html#::hoare_spec_scope:x_'->>'_x"><span class="id" title="notation"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span><span style='letter-spacing:-.2em;'>></span>></span></span></span></a> <span class="id" title="var">P</span>)<br/>
(<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 80) : <span class="id" title="var">hoare_spec_scope</span>.<br/>
</div>
<div class="doc">
<a id="lab54"></a><h2 class="section">Notations for Assertions</h2>
<div class="paragraph"> </div>
The convention described above can be implemented in Coq with a
little syntax magic, using coercions and annotation scopes, much
as we did with <span class="inlinecode">%<span class="id" title="var">imp</span></span> in <a href="Imp.html"><span class="inlineref">Imp</span></a>, to automatically lift
<span class="inlinecode"><span class="id" title="var">aexp</span></span>s, numbers, and <span class="inlinecode"><span class="id" title="keyword">Prop</span></span>s into <span class="inlinecode"><span class="id" title="var">Assertion</span></span>s when they appear
in the <span class="inlinecode">%<span class="id" title="var">assertion</span></span> scope or when Coq knows that the type of an
expression is <span class="inlinecode"><span class="id" title="var">Assertion</span></span>.
<div class="paragraph"> </div>
There is no need to understand the details of how these notation
hacks work. (We barely understand some of it ourselves!)
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="Aexp" class="idref" href="#Aexp"><span class="id" title="definition">Aexp</span></a> : <span class="id" title="keyword">Type</span> := <span class="id" title="definition">state</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="assert_of_Prop" class="idref" href="#assert_of_Prop"><span class="id" title="definition">assert_of_Prop</span></a> (<a id="P:8" class="idref" href="#P:8"><span class="id" title="binder">P</span></a> : <span class="id" title="keyword">Prop</span>) : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> := <span class="id" title="keyword">fun</span> <span class="id" title="var">_</span> ⇒ <a class="idref" href="Hoare.html#P:8"><span class="id" title="variable">P</span></a>.<br/>
<span class="id" title="keyword">Definition</span> <a id="Aexp_of_nat" class="idref" href="#Aexp_of_nat"><span class="id" title="definition">Aexp_of_nat</span></a> (<a id="n:9" class="idref" href="#n:9"><span class="id" title="binder">n</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a>) : <a class="idref" href="Hoare.html#Aexp"><span class="id" title="definition">Aexp</span></a> := <span class="id" title="keyword">fun</span> <span class="id" title="var">_</span> ⇒ <a class="idref" href="Hoare.html#n:9"><span class="id" title="variable">n</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="Aexp_of_aexp" class="idref" href="#Aexp_of_aexp"><span class="id" title="definition">Aexp_of_aexp</span></a> (<a id="a:10" class="idref" href="#a:10"><span class="id" title="binder">a</span></a> : <span class="id" title="inductive">aexp</span>) : <a class="idref" href="Hoare.html#Aexp"><span class="id" title="definition">Aexp</span></a> := <span class="id" title="keyword">fun</span> <a id="st:11" class="idref" href="#st:11"><span class="id" title="binder">st</span></a> ⇒ <span class="id" title="definition">aeval</span> <a class="idref" href="Hoare.html#st:11"><span class="id" title="variable">st</span></a> <a class="idref" href="Hoare.html#a:10"><span class="id" title="variable">a</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Coercion</span> <a class="idref" href="Hoare.html#assert_of_Prop"><span class="id" title="definition">assert_of_Prop</span></a> <a class="idref" href="Hoare.html#assert_of_Prop"><span class="id" title="definition">:</span></a> <a class="idref" href="Hoare.html#assert_of_Prop"><span class="id" title="definition">Sortclass</span></a> <a class="idref" href="Hoare.html#assert_of_Prop"><span class="id" title="definition">><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span></span></a> <a class="idref" href="Hoare.html#assert_of_Prop"><span class="id" title="definition">Assertion</span></a>.<br/>
<span class="id" title="keyword">Coercion</span> <a class="idref" href="Hoare.html#Aexp_of_nat"><span class="id" title="definition">Aexp_of_nat</span></a> <a class="idref" href="Hoare.html#Aexp_of_nat"><span class="id" title="definition">:</span></a> <a class="idref" href="Hoare.html#Aexp_of_nat"><span class="id" title="definition">nat</span></a> <a class="idref" href="Hoare.html#Aexp_of_nat"><span class="id" title="definition">><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span></span></a> <a class="idref" href="Hoare.html#Aexp_of_nat"><span class="id" title="definition">Aexp</span></a>.<br/>
<span class="id" title="keyword">Coercion</span> <a class="idref" href="Hoare.html#Aexp_of_aexp"><span class="id" title="definition">Aexp_of_aexp</span></a> <a class="idref" href="Hoare.html#Aexp_of_aexp"><span class="id" title="definition">:</span></a> <a class="idref" href="Hoare.html#Aexp_of_aexp"><span class="id" title="definition">aexp</span></a> <a class="idref" href="Hoare.html#Aexp_of_aexp"><span class="id" title="definition">><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span></span></a> <a class="idref" href="Hoare.html#Aexp_of_aexp"><span class="id" title="definition">Aexp</span></a>.<br/>
<span class="id" title="keyword">Add</span> <span class="id" title="keyword">Printing</span> <span class="id" title="keyword">Coercion</span> <span class="id" title="var">Aexp_of_nat</span> <span class="id" title="var">Aexp_of_aexp</span> <span class="id" title="var">assert_of_Prop</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Arguments</span> <a class="idref" href="Hoare.html#assert_of_Prop"><span class="id" title="definition">assert_of_Prop</span></a> /.<br/>
<span class="id" title="keyword">Arguments</span> <a class="idref" href="Hoare.html#Aexp_of_nat"><span class="id" title="definition">Aexp_of_nat</span></a> /.<br/>
<span class="id" title="keyword">Arguments</span> <a class="idref" href="Hoare.html#Aexp_of_aexp"><span class="id" title="definition">Aexp_of_aexp</span></a> /.<br/>
<span class="id" title="keyword">Add</span> <span class="id" title="keyword">Printing</span> <span class="id" title="keyword">Coercion</span> <span class="id" title="var">Aexp_of_nat</span> <span class="id" title="var">Aexp_of_aexp</span> <span class="id" title="var">assert_of_Prop</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Declare Scope</span> <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Bind Scope</span> <span class="id" title="var">assertion_scope</span> <span class="id" title="keyword">with</span> <span class="id" title="var">Assertion</span>.<br/>
<span class="id" title="keyword">Bind Scope</span> <span class="id" title="var">assertion_scope</span> <span class="id" title="keyword">with</span> <span class="id" title="var">Aexp</span>.<br/>
<span class="id" title="keyword">Delimit</span> <span class="id" title="keyword">Scope</span> <span class="id" title="var">assertion_scope</span> <span class="id" title="keyword">with</span> <span class="id" title="var">assertion</span>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Notation</span> <a id="assert" class="idref" href="#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">P</span> := (<span class="id" title="var">P</span>%<span class="id" title="var">assertion</span> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a>).<br/>
<span class="id" title="keyword">Notation</span> <a id="mkAexp" class="idref" href="#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> := (<span class="id" title="var">a</span>%<span class="id" title="var">assertion</span> : <a class="idref" href="Hoare.html#Aexp"><span class="id" title="definition">Aexp</span></a>).<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Notation</span> <a id="90b84b70bae2a0cf52991d884e146143" class="idref" href="#90b84b70bae2a0cf52991d884e146143"><span class="id" title="notation">"</span></a>~ P" := (<span class="id" title="keyword">fun</span> <a id="st:12" class="idref" href="#st:12"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#63a68285c81db8f9bc456233bb9ed181"><span class="id" title="notation">¬</span></a> <a class="idref" href="Hoare.html#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">P</span> <a class="idref" href="Hoare.html#st:12"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="cce4ad113420d0c25c6ce4891bbe5028" class="idref" href="#cce4ad113420d0c25c6ce4891bbe5028"><span class="id" title="notation">"</span></a>P /\ Q" := (<span class="id" title="keyword">fun</span> <a id="st:13" class="idref" href="#st:13"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">P</span> <a class="idref" href="Hoare.html#st:13"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#ba2b0e492d2b4675a0acf3ea92aabadd"><span class="id" title="notation">∧</span></a> <a class="idref" href="Hoare.html#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">Q</span> <a class="idref" href="Hoare.html#st:13"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="71acda7c05013ec3b4ea9870c5591533" class="idref" href="#71acda7c05013ec3b4ea9870c5591533"><span class="id" title="notation">"</span></a>P \/ Q" := (<span class="id" title="keyword">fun</span> <a id="st:14" class="idref" href="#st:14"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">P</span> <a class="idref" href="Hoare.html#st:14"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#f031fe1957c4a4a8e217aa46af2b4e<sub>25</sub>"><span class="id" title="notation">∨</span></a> <a class="idref" href="Hoare.html#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">Q</span> <a class="idref" href="Hoare.html#st:14"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="::assertion_scope:x_'->'_x" class="idref" href="#::assertion_scope:x_'->'_x"><span class="id" title="notation">"</span></a>P <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> Q" := (<span class="id" title="keyword">fun</span> <a id="st:15" class="idref" href="#st:15"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">P</span> <a class="idref" href="Hoare.html#st:15"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Hoare.html#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">Q</span> <a class="idref" href="Hoare.html#st:15"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="::assertion_scope:x_'<->'_x" class="idref" href="#::assertion_scope:x_'<->'_x"><span class="id" title="notation">"</span></a>P <<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> Q" := (<span class="id" title="keyword">fun</span> <a id="st:16" class="idref" href="#st:16"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">P</span> <a class="idref" href="Hoare.html#st:16"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'<->'_x"><span class="id" title="notation">↔</span></a> <a class="idref" href="Hoare.html#assert"><span class="id" title="abbreviation">assert</span></a> <span class="id" title="var">Q</span> <a class="idref" href="Hoare.html#st:16"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="5ad5c240a08c2b567f422c2e12577e<sub>27</sub>" class="idref" href="#5ad5c240a08c2b567f422c2e12577e<sub>27</sub>"><span class="id" title="notation">"</span></a>a = b" := (<span class="id" title="keyword">fun</span> <a id="st:17" class="idref" href="#st:17"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> <a class="idref" href="Hoare.html#st:17"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">b</span> <a class="idref" href="Hoare.html#st:17"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="::assertion_scope:x_'<>'_x" class="idref" href="#::assertion_scope:x_'<>'_x"><span class="id" title="notation">"</span></a>a <> b" := (<span class="id" title="keyword">fun</span> <a id="st:18" class="idref" href="#st:18"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> <a class="idref" href="Hoare.html#st:18"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'<>'_x"><span class="id" title="notation">≠</span></a> <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">b</span> <a class="idref" href="Hoare.html#st:18"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="154e85acf24eb57aa8f8a0156c0ba632" class="idref" href="#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">"</span></a>a <= b" := (<span class="id" title="keyword">fun</span> <a id="st:19" class="idref" href="#st:19"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> <a class="idref" href="Hoare.html#st:19"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#cb53cf0ee22c036a03b4a9281c68b5a<sub>3</sub>"><span class="id" title="notation">≤</span></a> <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">b</span> <a class="idref" href="Hoare.html#st:19"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="::assertion_scope:x_'<'_x" class="idref" href="#::assertion_scope:x_'<'_x"><span class="id" title="notation">"</span></a>a < b" := (<span class="id" title="keyword">fun</span> <a id="st:20" class="idref" href="#st:20"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> <a class="idref" href="Hoare.html#st:20"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#::nat_scope:x_'<'_x"><span class="id" title="notation"><</span></a> <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">b</span> <a class="idref" href="Hoare.html#st:20"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="47f7df8f7d756435423005da8ec23e<sub>41</sub>" class="idref" href="#47f7df8f7d756435423005da8ec23e<sub>41</sub>"><span class="id" title="notation">"</span></a>a >= b" := (<span class="id" title="keyword">fun</span> <a id="st:21" class="idref" href="#st:21"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> <a class="idref" href="Hoare.html#st:21"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#73030c22bc0b1fa771c65aa5414c65f<sub>9</sub>"><span class="id" title="notation">≥</span></a> <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">b</span> <a class="idref" href="Hoare.html#st:21"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="::assertion_scope:x_'>'_x" class="idref" href="#::assertion_scope:x_'>'_x"><span class="id" title="notation">"</span></a>a > b" := (<span class="id" title="keyword">fun</span> <a id="st:22" class="idref" href="#st:22"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> <a class="idref" href="Hoare.html#st:22"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Peano.html#::nat_scope:x_'>'_x"><span class="id" title="notation">></span></a> <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">b</span> <a class="idref" href="Hoare.html#st:22"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="d7f322243320be10cf04dfecd9fa5876" class="idref" href="#d7f322243320be10cf04dfecd9fa5876"><span class="id" title="notation">"</span></a>a + b" := (<span class="id" title="keyword">fun</span> <a id="st:23" class="idref" href="#st:23"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> <a class="idref" href="Hoare.html#st:23"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Nat.html#0dacc1786c5ba797d47dd85006231633"><span class="id" title="notation">+</span></a> <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">b</span> <a class="idref" href="Hoare.html#st:23"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="::assertion_scope:x_'-'_x" class="idref" href="#::assertion_scope:x_'-'_x"><span class="id" title="notation">"</span></a>a - b" := (<span class="id" title="keyword">fun</span> <a id="st:24" class="idref" href="#st:24"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> <a class="idref" href="Hoare.html#st:24"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Nat.html#::nat_scope:x_'-'_x"><span class="id" title="notation">-</span></a> <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">b</span> <a class="idref" href="Hoare.html#st:24"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
<span class="id" title="keyword">Notation</span> <a id="416660177ea0e5c9120e6d5bca0fb24e" class="idref" href="#416660177ea0e5c9120e6d5bca0fb24e"><span class="id" title="notation">"</span></a>a * b" := (<span class="id" title="keyword">fun</span> <a id="st:25" class="idref" href="#st:25"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">a</span> <a class="idref" href="Hoare.html#st:25"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Nat.html#ea2ff3d561159081cea6fb2e8113cc<sub>54</sub>"><span class="id" title="notation">×</span></a> <a class="idref" href="Hoare.html#mkAexp"><span class="id" title="abbreviation">mkAexp</span></a> <span class="id" title="var">b</span> <a class="idref" href="Hoare.html#st:25"><span class="id" title="variable">st</span></a>) : <span class="id" title="var">assertion_scope</span>.<br/>
</div>
<div class="doc">
One small limitation of this approach is that we don't have
an automatic way to coerce function applications that appear
within an assertion to make appropriate use of the state.
Instead, we use an explicit <span class="inlinecode"><span class="id" title="var">ap</span></span> operator to lift the function.
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="ap" class="idref" href="#ap"><span class="id" title="definition">ap</span></a> {<a id="X:26" class="idref" href="#X:26"><span class="id" title="binder">X</span></a>} (<a id="f:27" class="idref" href="#f:27"><span class="id" title="binder">f</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Hoare.html#X:26"><span class="id" title="variable">X</span></a>) (<a id="x:28" class="idref" href="#x:28"><span class="id" title="binder">x</span></a> : <a class="idref" href="Hoare.html#Aexp"><span class="id" title="definition">Aexp</span></a>) :=<br/>
<span class="id" title="keyword">fun</span> <a id="st:29" class="idref" href="#st:29"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#f:27"><span class="id" title="variable">f</span></a> (<a class="idref" href="Hoare.html#x:28"><span class="id" title="variable">x</span></a> <a class="idref" href="Hoare.html#st:29"><span class="id" title="variable">st</span></a>).<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="ap<sub>2</sub>" class="idref" href="#ap<sub>2</sub>"><span class="id" title="definition">ap<sub>2</sub></span></a> {<a id="X:30" class="idref" href="#X:30"><span class="id" title="binder">X</span></a>} (<a id="f:31" class="idref" href="#f:31"><span class="id" title="binder">f</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#nat"><span class="id" title="inductive">nat</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a> <a class="idref" href="Hoare.html#X:30"><span class="id" title="variable">X</span></a>) (<a id="x:32" class="idref" href="#x:32"><span class="id" title="binder">x</span></a> : <a class="idref" href="Hoare.html#Aexp"><span class="id" title="definition">Aexp</span></a>) (<a id="y:33" class="idref" href="#y:33"><span class="id" title="binder">y</span></a> : <a class="idref" href="Hoare.html#Aexp"><span class="id" title="definition">Aexp</span></a>) (<a id="st:34" class="idref" href="#st:34"><span class="id" title="binder">st</span></a> : <span class="id" title="definition">state</span>) :=<br/>
<a class="idref" href="Hoare.html#f:31"><span class="id" title="variable">f</span></a> (<a class="idref" href="Hoare.html#x:32"><span class="id" title="variable">x</span></a> <a class="idref" href="Hoare.html#st:34"><span class="id" title="variable">st</span></a>) (<a class="idref" href="Hoare.html#y:33"><span class="id" title="variable">y</span></a> <a class="idref" href="Hoare.html#st:34"><span class="id" title="variable">st</span></a>).<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Module</span> <a id="ExamplePrettyAssertions" class="idref" href="#ExamplePrettyAssertions"><span class="id" title="module">ExamplePrettyAssertions</span></a>.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExamplePrettyAssertions.ex1" class="idref" href="#ExamplePrettyAssertions.ex1"><span class="id" title="definition">ex<sub>1</sub></span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> := <span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#5ad5c240a08c2b567f422c2e12577e<sub>27</sub>"><span class="id" title="notation">=</span></a> 3.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExamplePrettyAssertions.ex2" class="idref" href="#ExamplePrettyAssertions.ex2"><span class="id" title="definition">ex<sub>2</sub></span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> := <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#True"><span class="id" title="inductive">True</span></a>.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExamplePrettyAssertions.ex3" class="idref" href="#ExamplePrettyAssertions.ex3"><span class="id" title="definition">ex<sub>3</sub></span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> := <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#False"><span class="id" title="inductive">False</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Definition</span> <a id="ExamplePrettyAssertions.assertion1" class="idref" href="#ExamplePrettyAssertions.assertion1"><span class="id" title="definition">assertion1</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> := <span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">≤</span></a> <span class="id" title="definition">Y</span>.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExamplePrettyAssertions.assertion2" class="idref" href="#ExamplePrettyAssertions.assertion2"><span class="id" title="definition">assertion2</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> := <span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#5ad5c240a08c2b567f422c2e12577e<sub>27</sub>"><span class="id" title="notation">=</span></a> 3 <a class="idref" href="Hoare.html#71acda7c05013ec3b4ea9870c5591533"><span class="id" title="notation">∨</span></a> <span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">≤</span></a> <span class="id" title="definition">Y</span>.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExamplePrettyAssertions.assertion3" class="idref" href="#ExamplePrettyAssertions.assertion3"><span class="id" title="definition">assertion3</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> := <span class="id" title="definition">Z</span> <a class="idref" href="Hoare.html#5ad5c240a08c2b567f422c2e12577e<sub>27</sub>"><span class="id" title="notation">=</span></a> <a class="idref" href="Hoare.html#ap<sub>2</sub>"><span class="id" title="definition">ap<sub>2</sub></span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Arith.PeanoNat.html#Nat.max"><span class="id" title="definition">max</span></a> <span class="id" title="definition">X</span> <span class="id" title="definition">Y</span>.<br/>
<span class="id" title="keyword">Definition</span> <a id="ExamplePrettyAssertions.assertion4" class="idref" href="#ExamplePrettyAssertions.assertion4"><span class="id" title="definition">assertion4</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> := <span class="id" title="definition">Z</span> <a class="idref" href="Hoare.html#416660177ea0e5c9120e6d5bca0fb24e"><span class="id" title="notation">×</span></a> <span class="id" title="definition">Z</span> <a class="idref" href="Hoare.html#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">≤</span></a> <span class="id" title="definition">X</span><br/>
<a class="idref" href="Hoare.html#cce4ad113420d0c25c6ce4891bbe5028"><span class="id" title="notation">∧</span></a> <a class="idref" href="Hoare.html#90b84b70bae2a0cf52991d884e146143"><span class="id" title="notation">¬</span></a> <a class="idref" href="Hoare.html#90b84b70bae2a0cf52991d884e146143"><span class="id" title="notation">(</span></a><a class="idref" href="Hoare.html#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">(</span></a><a class="idref" href="Hoare.html#416660177ea0e5c9120e6d5bca0fb24e"><span class="id" title="notation">(</span></a><a class="idref" href="Hoare.html#ap"><span class="id" title="definition">ap</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="definition">Z</span><a class="idref" href="Hoare.html#416660177ea0e5c9120e6d5bca0fb24e"><span class="id" title="notation">)</span></a> <a class="idref" href="Hoare.html#416660177ea0e5c9120e6d5bca0fb24e"><span class="id" title="notation">×</span></a> <a class="idref" href="Hoare.html#416660177ea0e5c9120e6d5bca0fb24e"><span class="id" title="notation">(</span></a><a class="idref" href="Hoare.html#ap"><span class="id" title="definition">ap</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Datatypes.html#S"><span class="id" title="constructor">S</span></a> <span class="id" title="definition">Z</span><a class="idref" href="Hoare.html#416660177ea0e5c9120e6d5bca0fb24e"><span class="id" title="notation">)</span></a><a class="idref" href="Hoare.html#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">)</span></a> <a class="idref" href="Hoare.html#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">≤</span></a> <span class="id" title="definition">X</span><a class="idref" href="Hoare.html#90b84b70bae2a0cf52991d884e146143"><span class="id" title="notation">)</span></a>.<br/>
<span class="id" title="keyword">End</span> <a class="idref" href="Hoare.html#ExamplePrettyAssertions"><span class="id" title="module">ExamplePrettyAssertions</span></a>.<br/>
</div>
<div class="doc">
<a id="lab55"></a><h1 class="section">Hoare Triples, Informally</h1>
<div class="paragraph"> </div>
A <i>Hoare triple</i> is a claim about the state before and
after executing a command. The standard notation is
<br/>
<span class="inlinecode"> {<span class="id" title="var">P</span>} <span class="id" title="var">c</span> {<span class="id" title="var">Q</span>}
</span> meaning:
<div class="paragraph"> </div>
<ul class="doclist">
<li> If command <span class="inlinecode"><span class="id" title="var">c</span></span> begins execution in a state satisfying assertion <span class="inlinecode"><span class="id" title="var">P</span></span>,
</li>
<li> and if <span class="inlinecode"><span class="id" title="var">c</span></span> eventually terminates in some final state,
</li>
<li> then that final state will satisfy the assertion <span class="inlinecode"><span class="id" title="var">Q</span></span>.
</li>
</ul>
<div class="paragraph"> </div>
Assertion <span class="inlinecode"><span class="id" title="var">P</span></span> is called the <i>precondition</i> of the triple, and <span class="inlinecode"><span class="id" title="var">Q</span></span> is
the <i>postcondition</i>.
<div class="paragraph"> </div>
Because single braces are already used for other things in Coq, we'll write
Hoare triples with double braces:
<br/>
<span class="inlinecode"> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">P</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">c</span> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">Q</span><span style='letter-spacing:-.4em;'>}</span>}
</span> For example,
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode">0<span style='letter-spacing:-.4em;'>}</span>}</span> <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">:=</span> <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1</span> <span class="inlinecode"><span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode">1<span style='letter-spacing:-.4em;'>}</span>}</span> is a valid Hoare triple,
stating that command <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">:=</span> <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1</span> will transform a state in
which <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode">0</span> to a state in which <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode">1</span>.
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode"><span class="id" title="keyword">∀</span></span> <span class="inlinecode"><span class="id" title="var">m</span>,</span> <span class="inlinecode"><span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">m</span><span style='letter-spacing:-.4em;'>}</span>}</span> <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">:=</span> <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1</span> <span class="inlinecode"><span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">m</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1<span style='letter-spacing:-.4em;'>}</span>}</span> is a
<i>proposition</i> stating that the Hoare triple <span class="inlinecode"><span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">m</span><span style='letter-spacing:-.4em;'>}</span>}</span> <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">:=</span> <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">+</span>
<span class="inlinecode">1</span> <span class="inlinecode"><span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" title="var">m</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1<span style='letter-spacing:-.4em;'>}</span>}</span> is valid for any choice of <span class="inlinecode"><span class="id" title="var">m</span></span>. Note that <span class="inlinecode"><span class="id" title="var">m</span></span>
in the two assertions and the command in the middle is a
reference to the <i>Coq</i> variable <span class="inlinecode"><span class="id" title="var">m</span></span>, which is bound outside the
Hoare triple.
</li>
</ul>
<div class="paragraph"> </div>
<a id="lab56"></a><h4 class="section">Exercise: 1 star, standard, optional (triples)</h4>
Paraphrase the following in English.
<br/>
<span class="inlinecode"> 1) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">True</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">c</span> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 5<span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
2) <span class="id" title="keyword">∀</span> <span class="id" title="var">m</span>, <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = <span class="id" title="var">m</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">c</span> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = <span class="id" title="var">m</span> + 5)<span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
3) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> ≤ <span class="id" title="var">Y</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">c</span> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">Y</span> ≤ <span class="id" title="var">X</span><span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
4) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">True</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">c</span> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">False</span><span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
5) <span class="id" title="keyword">∀</span> <span class="id" title="var">m</span>,<br/>
<span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = <span class="id" title="var">m</span><span style='letter-spacing:-.4em;'>}</span>}<br/>
<span class="id" title="var">c</span><br/>
<span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">Y</span> = <span class="id" title="var">real_fact</span> <span class="id" title="var">m</span><span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
6) <span class="id" title="keyword">∀</span> <span class="id" title="var">m</span>,<br/>
<span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = <span class="id" title="var">m</span><span style='letter-spacing:-.4em;'>}</span>}<br/>
<span class="id" title="var">c</span><br/>
<span style='letter-spacing:-.4em;'>{</span>{(<span class="id" title="var">Z</span> × <span class="id" title="var">Z</span>) ≤ <span class="id" title="var">m</span> ∧ ¬(((<span class="id" title="var">S</span> <span class="id" title="var">Z</span>) × (<span class="id" title="var">S</span> <span class="id" title="var">Z</span>)) ≤ <span class="id" title="var">m</span>)<span style='letter-spacing:-.4em;'>}</span>}
</span>
</div>
<div class="code">
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<div class="paragraph"> </div>
<a id="lab57"></a><h4 class="section">Exercise: 1 star, standard, 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" title="var">P</span></span>, <span class="inlinecode"><span class="id" title="var">c</span></span>, and <span class="inlinecode"><span class="id" title="var">Q</span></span> is true?
<br/>
<span class="inlinecode"> 1) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">True</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := 5 <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 5<span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
2) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 2<span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := <span class="id" title="var">X</span> + 1 <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 3<span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
3) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">True</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := 5; <span class="id" title="var">Y</span> := 0 <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 5<span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
4) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 2 ∧ <span class="id" title="var">X</span> = 3<span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := 5 <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 0<span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
5) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">True</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">skip</span> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">False</span><span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
6) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">False</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">skip</span> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">True</span><span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
7) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">True</span><span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">while</span> <span class="id" title="var">true</span> <span class="id" title="tactic">do</span> <span class="id" title="var">skip</span> <span class="id" title="keyword">end</span> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">False</span><span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
8) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 0<span style='letter-spacing:-.4em;'>}</span>}<br/>
<span class="id" title="var">while</span> <span class="id" title="var">X</span> = 0 <span class="id" title="tactic">do</span> <span class="id" title="var">X</span> := <span class="id" title="var">X</span> + 1 <span class="id" title="keyword">end</span><br/>
<span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 1<span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
9) <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 1<span style='letter-spacing:-.4em;'>}</span>}<br/>
<span class="id" title="var">while</span> <span class="id" title="var">X</span> ≠ 0 <span class="id" title="tactic">do</span> <span class="id" title="var">X</span> := <span class="id" title="var">X</span> + 1 <span class="id" title="keyword">end</span><br/>
<span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> = 100<span style='letter-spacing:-.4em;'>}</span>}
</span>
</div>
<div class="code">
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<a id="lab58"></a><h1 class="section">Hoare Triples, Formally</h1>
<div class="paragraph"> </div>
We can formalize valid Hoare triples in Coq as follows:
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="valid_hoare_triple" class="idref" href="#valid_hoare_triple"><span class="id" title="definition">valid_hoare_triple</span></a><br/>
(<a id="P:35" class="idref" href="#P:35"><span class="id" title="binder">P</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a>) (<a id="c:36" class="idref" href="#c:36"><span class="id" title="binder">c</span></a> : <span class="id" title="inductive">com</span>) (<a id="Q:37" class="idref" href="#Q:37"><span class="id" title="binder">Q</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a>) : <span class="id" title="keyword">Prop</span> :=<br/>
<span class="id" title="keyword">∀</span> <a id="st:38" class="idref" href="#st:38"><span class="id" title="binder">st</span></a> <a id="st':39" class="idref" href="#st':39"><span class="id" title="binder">st'</span></a>,<br/>
<a class="idref" href="Hoare.html#st:38"><span class="id" title="variable">st</span></a> <span class="id" title="notation">=[</span> <a class="idref" href="Hoare.html#c:36"><span class="id" title="variable">c</span></a> <span class="id" title="notation">]=></span> <a class="idref" href="Hoare.html#st':39"><span class="id" title="variable">st'</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a><br/>
<a class="idref" href="Hoare.html#P:35"><span class="id" title="variable">P</span></a> <a class="idref" href="Hoare.html#st:38"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a><br/>
<a class="idref" href="Hoare.html#Q:37"><span class="id" title="variable">Q</span></a> <a class="idref" href="Hoare.html#st':39"><span class="id" title="variable">st'</span></a>.<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Notation</span> <a id="a18cbce9fe584a9130c3f34d4f14555f" class="idref" href="#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation">"</span></a><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/>
(<a class="idref" href="Hoare.html#valid_hoare_triple"><span class="id" title="definition">valid_hoare_triple</span></a> <span class="id" title="var">P</span> <span class="id" title="var">c</span> <span class="id" title="var">Q</span>)<br/>
(<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 90, <span class="id" title="var">c</span> <span class="id" title="var">custom</span> <span class="id" title="var">com</span> <span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 99)<br/>
: <span class="id" title="var">hoare_spec_scope</span>.<br/>
<span class="id" title="keyword">Check</span> (<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#True"><span class="id" title="inductive">True</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <span class="id" title="definition">X</span> <span class="id" title="notation">:=</span> 0 <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#True"><span class="id" title="inductive">True</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>).<br/>
</div>
<div class="doc">
<a id="lab59"></a><h4 class="section">Exercise: 1 star, standard (hoare_post_true)</h4>
<div class="paragraph"> </div>
Prove that if <span class="inlinecode"><span class="id" title="var">Q</span></span> holds in every state, then any triple with <span class="inlinecode"><span class="id" title="var">Q</span></span>
as its postcondition is valid.
</div>
<div class="code">
<span class="id" title="keyword">Theorem</span> <a id="hoare_post_true" class="idref" href="#hoare_post_true"><span class="id" title="lemma">hoare_post_true</span></a> : <span class="id" title="keyword">∀</span> (<a id="P:40" class="idref" href="#P:40"><span class="id" title="binder">P</span></a> <a id="Q:41" class="idref" href="#Q:41"><span class="id" title="binder">Q</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a>) <a id="c:42" class="idref" href="#c:42"><span class="id" title="binder">c</span></a>,<br/>
<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">(</span></a><span class="id" title="keyword">∀</span> <a id="st:43" class="idref" href="#st:43"><span class="id" title="binder">st</span></a>, <a class="idref" href="Hoare.html#Q:41"><span class="id" title="variable">Q</span></a> <a class="idref" href="Hoare.html#st:43"><span class="id" title="variable">st</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a><br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#P:40"><span class="id" title="variable">P</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <a class="idref" href="Hoare.html#c:42"><span class="id" title="variable">c</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#Q:41"><span class="id" title="variable">Q</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<div class="paragraph"> </div>
<a id="lab60"></a><h4 class="section">Exercise: 1 star, standard (hoare_pre_false)</h4>
<div class="paragraph"> </div>
Prove that if <span class="inlinecode"><span class="id" title="var">P</span></span> holds in no state, then any triple with <span class="inlinecode"><span class="id" title="var">P</span></span> as
its precondition is valid.
</div>
<div class="code">
<span class="id" title="keyword">Theorem</span> <a id="hoare_pre_false" class="idref" href="#hoare_pre_false"><span class="id" title="lemma">hoare_pre_false</span></a> : <span class="id" title="keyword">∀</span> (<a id="P:44" class="idref" href="#P:44"><span class="id" title="binder">P</span></a> <a id="Q:45" class="idref" href="#Q:45"><span class="id" title="binder">Q</span></a> : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a>) <a id="c:46" class="idref" href="#c:46"><span class="id" title="binder">c</span></a>,<br/>
<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">(</span></a><span class="id" title="keyword">∀</span> <a id="st:47" class="idref" href="#st:47"><span class="id" title="binder">st</span></a>, <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#63a68285c81db8f9bc456233bb9ed181"><span class="id" title="notation">¬</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#63a68285c81db8f9bc456233bb9ed181"><span class="id" title="notation">(</span></a><a class="idref" href="Hoare.html#P:44"><span class="id" title="variable">P</span></a> <a class="idref" href="Hoare.html#st:47"><span class="id" title="variable">st</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#63a68285c81db8f9bc456233bb9ed181"><span class="id" title="notation">)</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">)</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a><br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#P:44"><span class="id" title="variable">P</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <a class="idref" href="Hoare.html#c:46"><span class="id" title="variable">c</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#Q:45"><span class="id" title="variable">Q</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<a id="lab61"></a><h1 class="section">Proof Rules</h1>
<div class="paragraph"> </div>
The goal of Hoare logic is to provide a <i>compositional</i>
method for proving the validity of specific Hoare triples. That
is, we want the structure of a program's correctness proof to
mirror the structure of the program itself. To this end, in the
sections below, we'll introduce a 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 for gluing things together. We
will then be able to prove programs correct using these proof
rules, without ever unfolding the definition of <span class="inlinecode"><span class="id" title="var">valid_hoare_triple</span></span>.
</div>
<div class="doc">
<a id="lab62"></a><h2 class="section">Skip</h2>
<div class="paragraph"> </div>
Since <span class="inlinecode"><span class="id" title="var">skip</span></span> doesn't change the state, it preserves any
assertion <span class="inlinecode"><span class="id" title="var">P</span></span>:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(hoare_skip)
</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>} skip <span style='letter-spacing:-.4em;'>{</span>{ P <span style='letter-spacing:-.4em;'>}</span>}</td>
<td></td>
</tr>
</table></center>
</div>
<div class="code">
<span class="id" title="keyword">Theorem</span> <a id="hoare_skip" class="idref" href="#hoare_skip"><span class="id" title="lemma">hoare_skip</span></a> : <span class="id" title="keyword">∀</span> <a id="P:48" class="idref" href="#P:48"><span class="id" title="binder">P</span></a>,<br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#P:48"><span class="id" title="variable">P</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <span class="id" title="notation">skip</span> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#P:48"><span class="id" title="variable">P</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<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" title="keyword">Proof</span>.<br/>
<span class="id" title="tactic">intros</span> <span class="id" title="var">P</span> <span class="id" title="var">st</span> <span class="id" title="var">st'</span> <span class="id" title="var">H</span> <span class="id" title="var">HP</span>. <span class="id" title="tactic">inversion</span> <span class="id" title="var">H</span>; <span class="id" title="tactic">subst</span>. <span class="id" title="tactic">assumption</span>.<br/>
<span class="id" title="keyword">Qed</span>.<br/>
</div>
</div>
<div class="doc">
<a id="lab63"></a><h2 class="section">Sequencing</h2>
<div class="paragraph"> </div>
If command <span class="inlinecode"><span class="id" title="var">c<sub>1</sub></span></span> takes any state where <span class="inlinecode"><span class="id" title="var">P</span></span> holds to a state where
<span class="inlinecode"><span class="id" title="var">Q</span></span> holds, and if <span class="inlinecode"><span class="id" title="var">c<sub>2</sub></span></span> takes any state where <span class="inlinecode"><span class="id" title="var">Q</span></span> holds to one
where <span class="inlinecode"><span class="id" title="var">R</span></span> holds, then doing <span class="inlinecode"><span class="id" title="var">c<sub>1</sub></span></span> followed by <span class="inlinecode"><span class="id" title="var">c<sub>2</sub></span></span> will take any
state where <span class="inlinecode"><span class="id" title="var">P</span></span> holds to one where <span class="inlinecode"><span class="id" title="var">R</span></span> holds:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"><span style='letter-spacing:-.4em;'>{</span>{ P <span style='letter-spacing:-.4em;'>}</span>} c<sub>1</sub> <span style='letter-spacing:-.4em;'>{</span>{ Q <span style='letter-spacing:-.4em;'>}</span>}</td>
<td></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style='letter-spacing:-.4em;'>{</span>{ Q <span style='letter-spacing:-.4em;'>}</span>} c<sub>2</sub> <span style='letter-spacing:-.4em;'>{</span>{ R <span style='letter-spacing:-.4em;'>}</span>}</td>
<td class="infrulenamecol" rowspan="3">
(hoare_seq)
</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<sub>1</sub>;c<sub>2</sub> <span style='letter-spacing:-.4em;'>{</span>{ R <span style='letter-spacing:-.4em;'>}</span>}</td>
<td></td>
</tr>
</table></center>
</div>
<div class="code">
<span class="id" title="keyword">Theorem</span> <a id="hoare_seq" class="idref" href="#hoare_seq"><span class="id" title="lemma">hoare_seq</span></a> : <span class="id" title="keyword">∀</span> <a id="P:49" class="idref" href="#P:49"><span class="id" title="binder">P</span></a> <a id="Q:50" class="idref" href="#Q:50"><span class="id" title="binder">Q</span></a> <a id="R:51" class="idref" href="#R:51"><span class="id" title="binder">R</span></a> <a id="c<sub>1</sub>:52" class="idref" href="#c<sub>1</sub>:52"><span class="id" title="binder">c<sub>1</sub></span></a> <a id="c<sub>2</sub>:53" class="idref" href="#c<sub>2</sub>:53"><span class="id" title="binder">c<sub>2</sub></span></a>,<br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#Q:50"><span class="id" title="variable">Q</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <a class="idref" href="Hoare.html#c<sub>2</sub>:53"><span class="id" title="variable">c<sub>2</sub></span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#R:51"><span class="id" title="variable">R</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a><br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#P:49"><span class="id" title="variable">P</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <a class="idref" href="Hoare.html#c<sub>1</sub>:52"><span class="id" title="variable">c<sub>1</sub></span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#Q:50"><span class="id" title="variable">Q</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#::type_scope:x_'->'_x"><span class="id" title="notation">→</span></a><br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#P:49"><span class="id" title="variable">P</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <a class="idref" href="Hoare.html#c<sub>1</sub>:52"><span class="id" title="variable">c<sub>1</sub></span></a><span class="id" title="notation">;</span> <a class="idref" href="Hoare.html#c<sub>2</sub>:53"><span class="id" title="variable">c<sub>2</sub></span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#R:51"><span class="id" title="variable">R</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<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" title="keyword">Proof</span>.<br/>
<span class="id" title="tactic">unfold</span> <a class="idref" href="Hoare.html#valid_hoare_triple"><span class="id" title="definition">valid_hoare_triple</span></a>.<br/>
<span class="id" title="tactic">intros</span> <span class="id" title="var">P</span> <span class="id" title="var">Q</span> <span class="id" title="var">R</span> <span class="id" title="var">c<sub>1</sub></span> <span class="id" title="var">c<sub>2</sub></span> <span class="id" title="var">H<sub>1</sub></span> <span class="id" title="var">H<sub>2</sub></span> <span class="id" title="var">st</span> <span class="id" title="var">st'</span> <span class="id" title="var">H<sub>12</sub></span> <span class="id" title="var">Pre</span>.<br/>
<span class="id" title="tactic">inversion</span> <span class="id" title="var">H<sub>12</sub></span>; <span class="id" title="tactic">subst</span>.<br/>
<span class="id" title="tactic">eauto</span>.<br/>
<span class="id" title="keyword">Qed</span>.<br/>
</div>
</div>
<div class="doc">
Note that, in the formal rule <span class="inlinecode"><span class="id" title="var">hoare_seq</span></span>, the premises are
given in backwards order (<span class="inlinecode"><span class="id" title="var">c<sub>2</sub></span></span> before <span class="inlinecode"><span class="id" title="var">c<sub>1</sub></span></span>). This matches the
natural flow of information in many of the situations where we'll
use the rule, since the natural way to construct a Hoare-logic
proof is to begin at the end of the program (with the final
postcondition) and push postconditions backwards through commands
until we reach the beginning.
</div>
<div class="doc">
<a id="lab64"></a><h2 class="section">Assignment</h2>
<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 incomplete Hoare triple:
<br/>
<span class="inlinecode"> <span style='letter-spacing:-.4em;'>{</span>{ ??? <span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := <span class="id" title="var">Y</span> <span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">X</span> = 1 <span style='letter-spacing:-.4em;'>}</span>}
</span>
<div class="paragraph"> </div>
We want to assign <span class="inlinecode"><span class="id" title="var">Y</span></span> to <span class="inlinecode"><span class="id" title="var">X</span></span> and finish in a state where <span class="inlinecode"><span class="id" title="var">X</span></span> is <span class="inlinecode">1</span>.
What could the precondition be?
<div class="paragraph"> </div>
One possibility is <span class="inlinecode"><span class="id" title="var">Y</span></span> <span class="inlinecode">=</span> <span class="inlinecode">1</span>, because if <span class="inlinecode"><span class="id" title="var">Y</span></span> is already <span class="inlinecode">1</span> then
assigning it to <span class="inlinecode"><span class="id" title="var">X</span></span> causes <span class="inlinecode"><span class="id" title="var">X</span></span> to be <span class="inlinecode">1</span>. That leads to a valid
Hoare triple:
<br/>
<span class="inlinecode"> <span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">Y</span> = 1 <span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := <span class="id" title="var">Y</span> <span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">X</span> = 1 <span style='letter-spacing:-.4em;'>}</span>}
</span> It may seem as though coming up with that precondition must have
taken some clever thought. But there is a mechanical way we could
have done it: if we take the postcondition <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode">1</span> and in it
replace <span class="inlinecode"><span class="id" title="var">X</span></span> with <span class="inlinecode"><span class="id" title="var">Y</span></span>---that is, replace the left-hand side of the
assignment statement with the right-hand side---we get the
precondition, <span class="inlinecode"><span class="id" title="var">Y</span></span> <span class="inlinecode">=</span> <span class="inlinecode">1</span>.
<div class="paragraph"> </div>
That same idea works in more complicated cases. For
example:
<br/>
<span class="inlinecode"> <span style='letter-spacing:-.4em;'>{</span>{ ??? <span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := <span class="id" title="var">X</span> + <span class="id" title="var">Y</span> <span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">X</span> = 1 <span style='letter-spacing:-.4em;'>}</span>}
</span> If we replace the <span class="inlinecode"><span class="id" title="var">X</span></span> in <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">=</span> <span class="inlinecode">1</span> with <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">Y</span></span>, we get <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">Y</span></span> <span class="inlinecode">=</span> <span class="inlinecode">1</span>.
That again leads to a valid Hoare triple:
<br/>
<span class="inlinecode"> <span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">X</span> + <span class="id" title="var">Y</span> = 1 <span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := <span class="id" title="var">X</span> + <span class="id" title="var">Y</span> <span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">X</span> = 1 <span style='letter-spacing:-.4em;'>}</span>}
</span> Why does this technique work? The postcondition identifies some
property <span class="inlinecode"><span class="id" title="var">P</span></span> that we want to hold of the variable <span class="inlinecode"><span class="id" title="var">X</span></span> being
assigned. In this case, <span class="inlinecode"><span class="id" title="var">P</span></span> is "equals <span class="inlinecode">1</span>". To complete the
triple and make it valid, we need to identify a precondition that
guarantees that property will hold of <span class="inlinecode"><span class="id" title="var">X</span></span>. Such a precondition
must ensure that the same property holds of <i>whatever is being
assigned to</i> <span class="inlinecode"><span class="id" title="var">X</span></span>. So, in the example, we need "equals <span class="inlinecode">1</span>" to
hold of <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" title="var">Y</span></span>. That's exactly what the technique guarantees.
<div class="paragraph"> </div>
In general, the postcondition could be some arbitrary assertion
<span class="inlinecode"><span class="id" title="var">Q</span></span>, and the right-hand side of the assignment could be some
arbitrary arithmetic expression <span class="inlinecode"><span class="id" title="var">a</span></span>:
<br/>
<span class="inlinecode"> <span style='letter-spacing:-.4em;'>{</span>{ ??? <span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := <span class="id" title="var">a</span> <span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">Q</span> <span style='letter-spacing:-.4em;'>}</span>}
</span> The precondition would then be <span class="inlinecode"><span class="id" title="var">Q</span></span>, but with any occurrences of
<span class="inlinecode"><span class="id" title="var">X</span></span> in it replaced by <span class="inlinecode"><span class="id" title="var">a</span></span>. Let's introduce a notation for this idea of replacing occurrences:
Define <span class="inlinecode"><span class="id" title="var">Q</span></span> <span class="inlinecode">[<span class="id" title="var">X</span></span> <span class="inlinecode"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span> <span class="inlinecode"><span class="id" title="var">a</span>]</span> to mean "<span class="inlinecode"><span class="id" title="var">Q</span></span> where <span class="inlinecode"><span class="id" title="var">a</span></span> is substituted in
place of <span class="inlinecode"><span class="id" title="var">X</span></span>".
<div class="paragraph"> </div>
This yields the Hoare logic rule for assignment:
<br/>
<span class="inlinecode"> <span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">Q</span> [<span class="id" title="var">X</span> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span> <span class="id" title="var">a</span>] <span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := <span class="id" title="var">a</span> <span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">Q</span> <span style='letter-spacing:-.4em;'>}</span>}
</span> One way of reading this rule is: If you want statement <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">:=</span> <span class="inlinecode"><span class="id" title="var">a</span></span>
to terminate in a state that satisfies assertion <span class="inlinecode"><span class="id" title="var">Q</span></span>, then it
suffices to start in a state that also satisfies <span class="inlinecode"><span class="id" title="var">Q</span></span>, except
where <span class="inlinecode"><span class="id" title="var">a</span></span> is substituted for every occurrence of <span class="inlinecode"><span class="id" title="var">X</span></span>.
<div class="paragraph"> </div>
To many people, this rule seems "backwards" at first, because
it proceeds from the postcondition to the precondition. Actually
it makes good sense to go in this direction: the postcondition is
often what is more important, because it characterizes what we
can assume afer running the code.
<div class="paragraph"> </div>
Nonetheless, it's also possible to formulate a "forward" assignment
rule. We'll do that later in some exercises.
<div class="paragraph"> </div>
Here are some valid instances of the assignment rule:
<br/>
<span class="inlinecode"> <span style='letter-spacing:-.4em;'>{</span>{ (<span class="id" title="var">X</span> ≤ 5) [<span class="id" title="var">X</span> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span> <span class="id" title="var">X</span> + 1] <span style='letter-spacing:-.4em;'>}</span>} (<span class="id" title="var">that</span> <span class="id" title="keyword">is</span>, <span class="id" title="var">X</span> + 1 ≤ 5)<br/>
<span class="id" title="var">X</span> := <span class="id" title="var">X</span> + 1<br/>
<span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">X</span> ≤ 5 <span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
<span style='letter-spacing:-.4em;'>{</span>{ (<span class="id" title="var">X</span> = 3) [<span class="id" title="var">X</span> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span> 3] <span style='letter-spacing:-.4em;'>}</span>} (<span class="id" title="var">that</span> <span class="id" title="keyword">is</span>, 3 = 3)<br/>
<span class="id" title="var">X</span> := 3<br/>
<span style='letter-spacing:-.4em;'>{</span>{ <span class="id" title="var">X</span> = 3 <span style='letter-spacing:-.4em;'>}</span>}<br/>
<br/>
<span style='letter-spacing:-.4em;'>{</span>{ (0 ≤ <span class="id" title="var">X</span> ∧ <span class="id" title="var">X</span> ≤ 5) [<span class="id" title="var">X</span> <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span> 3] (<span class="id" title="var">that</span> <span class="id" title="keyword">is</span>, 0 ≤ 3 ∧ 3 ≤ 5)<br/>
<span class="id" title="var">X</span> := 3<br/>
<span style='letter-spacing:-.4em;'>{</span>{ 0 ≤ <span class="id" title="var">X</span> ∧ <span class="id" title="var">X</span> ≤ 5 <span style='letter-spacing:-.4em;'>}</span>}
</span>
<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",
which we refer to as assertion substitution, or <span class="inlinecode"><span class="id" title="var">assertion_sub</span></span>. That
is, intuitively, given a proposition <span class="inlinecode"><span class="id" title="var">P</span></span>, a variable <span class="inlinecode"><span class="id" title="var">X</span></span>, and an
arithmetic expression <span class="inlinecode"><span class="id" title="var">a</span></span>, we want to derive another proposition
<span class="inlinecode"><span class="id" title="var">P'</span></span> that is just the same as <span class="inlinecode"><span class="id" title="var">P</span></span> except that <span class="inlinecode"><span class="id" title="var">P'</span></span> should mention
<span class="inlinecode"><span class="id" title="var">a</span></span> wherever <span class="inlinecode"><span class="id" title="var">P</span></span> mentions <span class="inlinecode"><span class="id" title="var">X</span></span>.
<div class="paragraph"> </div>
This operation is related to the idea of substituting Imp
expressions for Imp variables that we saw in <a href="Equiv.html"><span class="inlineref">Equiv</span></a>
(<span class="inlinecode"><span class="id" title="var">subst_aexp</span></span> and friends). The difference is that, here,
<span class="inlinecode"><span class="id" title="var">P</span></span> is an arbitrary Coq assertion, so we can't directly
"edit" its text.
<div class="paragraph"> </div>
However, we can achieve the same effect by evaluating <span class="inlinecode"><span class="id" title="var">P</span></span> in an
updated state:
</div>
<div class="code">
<span class="id" title="keyword">Definition</span> <a id="assertion_sub" class="idref" href="#assertion_sub"><span class="id" title="definition">assertion_sub</span></a> <a id="X:54" class="idref" href="#X:54"><span class="id" title="binder">X</span></a> <a id="a:55" class="idref" href="#a:55"><span class="id" title="binder">a</span></a> (<a id="P:56" class="idref" href="#P:56"><span class="id" title="binder">P</span></a>:<a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a>) : <a class="idref" href="Hoare.html#Assertion"><span class="id" title="definition">Assertion</span></a> :=<br/>
<span class="id" title="keyword">fun</span> (<a id="st:57" class="idref" href="#st:57"><span class="id" title="binder">st</span></a> : <span class="id" title="definition">state</span>) ⇒<br/>
<a class="idref" href="Hoare.html#P:56"><span class="id" title="variable">P</span></a> (<a class="idref" href="Hoare.html#X:54"><span class="id" title="variable">X</span></a> <span class="id" title="notation">!<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span></span> <span class="id" title="definition">aeval</span> <a class="idref" href="Hoare.html#st:57"><span class="id" title="variable">st</span></a> <a class="idref" href="Hoare.html#a:55"><span class="id" title="variable">a</span></a> <span class="id" title="notation">;</span> <a class="idref" href="Hoare.html#st:57"><span class="id" title="variable">st</span></a>).<br/><hr class='doublespaceincode'/>
<span class="id" title="keyword">Notation</span> <a id="510fc37417e4aa55210e4cfe35fb96ae" class="idref" href="#510fc37417e4aa55210e4cfe35fb96ae"><span class="id" title="notation">"</span></a>P [ X <span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span> a ]" := (<a class="idref" href="Hoare.html#assertion_sub"><span class="id" title="definition">assertion_sub</span></a> <span class="id" title="var">X</span> <span class="id" title="var">a</span> <span class="id" title="var">P</span>)<br/>
(<span class="id" title="tactic">at</span> <span class="id" title="keyword">level</span> 10, <span class="id" title="var">X</span> <span class="id" title="tactic">at</span> <span class="id" title="var">next</span> <span class="id" title="keyword">level</span>, <span class="id" title="var">a</span> <span class="id" title="var">custom</span> <span class="id" title="var">com</span>) : <span class="id" title="var">hoare_spec_scope</span>.<br/>
</div>
<div class="doc">
That is, <span class="inlinecode"><span class="id" title="var">P</span></span> <span class="inlinecode">[<span class="id" title="var">X</span></span> <span class="inlinecode"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span> <span class="inlinecode"><span class="id" title="var">a</span>]</span> stands for an assertion -- let's call it
<span class="inlinecode"><span class="id" title="var">P'</span></span> -- that is just like <span class="inlinecode"><span class="id" title="var">P</span></span> except that, wherever <span class="inlinecode"><span class="id" title="var">P</span></span> looks up
the variable <span class="inlinecode"><span class="id" title="var">X</span></span> in the current state, <span class="inlinecode"><span class="id" title="var">P'</span></span> instead uses the value
of the expression <span class="inlinecode"><span class="id" title="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" title="var">P'</span></span> is <span class="inlinecode">(<span class="id" title="var">X</span></span> <span class="inlinecode">≤</span> <span class="inlinecode">5)</span> <span class="inlinecode">[<span class="id" title="var">X</span></span> <span class="inlinecode"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span> <span class="inlinecode">3]</span> -- that
is, more formally, <span class="inlinecode"><span class="id" title="var">P'</span></span> is the Coq expression
<br/>
<span class="inlinecode"> <span class="id" title="keyword">fun</span> <span class="id" title="var">st</span> ⇒<br/>
(<span class="id" title="keyword">fun</span> <span class="id" title="var">st'</span> ⇒ <span class="id" title="var">st'</span> <span class="id" title="var">X</span> ≤ 5)<br/>
(<span class="id" title="var">X</span> !<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> <span class="id" title="var">aeval</span> <span class="id" title="var">st</span> 3 ; <span class="id" title="var">st</span>),
</span> which simplifies to
<br/>
<span class="inlinecode"> <span class="id" title="keyword">fun</span> <span class="id" title="var">st</span> ⇒<br/>
(<span class="id" title="keyword">fun</span> <span class="id" title="var">st'</span> ⇒ <span class="id" title="var">st'</span> <span class="id" title="var">X</span> ≤ 5)<br/>
(<span class="id" title="var">X</span> !<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> 3 ; <span class="id" title="var">st</span>)
</span> and further simplifies to
<br/>
<span class="inlinecode"> <span class="id" title="keyword">fun</span> <span class="id" title="var">st</span> ⇒<br/>
((<span class="id" title="var">X</span> !<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> 3 ; <span class="id" title="var">st</span>) <span class="id" title="var">X</span>) ≤ 5
</span> and finally to
<br/>
<span class="inlinecode"> <span class="id" title="keyword">fun</span> <span class="id" title="var">st</span> ⇒<br/>
3 ≤ 5.
</span> That is, <span class="inlinecode"><span class="id" title="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" title="var">P'</span></span> is <span class="inlinecode">(<span class="id" title="var">X</span></span> <span class="inlinecode">≤</span> <span class="inlinecode">5)</span> <span class="inlinecode">[<span class="id" title="var">X</span></span> <span class="inlinecode"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span>
<span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1]</span>. Formally, <span class="inlinecode"><span class="id" title="var">P'</span></span> is the Coq expression
<br/>
<span class="inlinecode"> <span class="id" title="keyword">fun</span> <span class="id" title="var">st</span> ⇒<br/>
(<span class="id" title="keyword">fun</span> <span class="id" title="var">st'</span> ⇒ <span class="id" title="var">st'</span> <span class="id" title="var">X</span> ≤ 5)<br/>
(<span class="id" title="var">X</span> !<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> <span class="id" title="var">aeval</span> <span class="id" title="var">st</span> (<span class="id" title="var">X</span> + 1) ; <span class="id" title="var">st</span>),
</span> which simplifies to
<br/>
<span class="inlinecode"> <span class="id" title="keyword">fun</span> <span class="id" title="var">st</span> ⇒<br/>
(<span class="id" title="var">X</span> !<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> <span class="id" title="var">aeval</span> <span class="id" title="var">st</span> (<span class="id" title="var">X</span> + 1) ; <span class="id" title="var">st</span>) <span class="id" title="var">X</span> ≤ 5
</span> and further simplifies to
<br/>
<span class="inlinecode"> <span class="id" title="keyword">fun</span> <span class="id" title="var">st</span> ⇒<br/>
(<span class="id" title="var">aeval</span> <span class="id" title="var">st</span> (<span class="id" title="var">X</span> + 1)) ≤ 5.
</span> That is, <span class="inlinecode"><span class="id" title="var">P'</span></span> is the assertion that <span class="inlinecode"><span class="id" title="var">X</span></span> <span class="inlinecode">+</span> <span class="inlinecode">1</span> is at most <span class="inlinecode">5</span>.
<div class="paragraph"> </div>
Now, using the substitution operation we've just defined, 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 class="nowrap"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></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>
</tr>
</table></center>
<div class="paragraph"> </div>
We can prove formally that this rule is indeed valid.
</div>
<div class="code">
<span class="id" title="keyword">Theorem</span> <a id="hoare_asgn" class="idref" href="#hoare_asgn"><span class="id" title="lemma">hoare_asgn</span></a> : <span class="id" title="keyword">∀</span> <a id="Q:58" class="idref" href="#Q:58"><span class="id" title="binder">Q</span></a> <a id="X:59" class="idref" href="#X:59"><span class="id" title="binder">X</span></a> <a id="a:60" class="idref" href="#a:60"><span class="id" title="binder">a</span></a>,<br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#Q:58"><span class="id" title="variable">Q</span></a> <a class="idref" href="Hoare.html#510fc37417e4aa55210e4cfe35fb96ae"><span class="id" title="notation">[</span></a><a class="idref" href="Hoare.html#X:59"><span class="id" title="variable">X</span></a> <a class="idref" href="Hoare.html#510fc37417e4aa55210e4cfe35fb96ae"><span class="id" title="notation"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span></a> <a class="idref" href="Hoare.html#a:60"><span class="id" title="variable">a</span></a><a class="idref" href="Hoare.html#510fc37417e4aa55210e4cfe35fb96ae"><span class="id" title="notation">]</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <a class="idref" href="Hoare.html#X:59"><span class="id" title="variable">X</span></a> <span class="id" title="notation">:=</span> <a class="idref" href="Hoare.html#a:60"><span class="id" title="variable">a</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#Q:58"><span class="id" title="variable">Q</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<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" title="keyword">Proof</span>.<br/>
<span class="id" title="tactic">unfold</span> <a class="idref" href="Hoare.html#valid_hoare_triple"><span class="id" title="definition">valid_hoare_triple</span></a>.<br/>
<span class="id" title="tactic">intros</span> <span class="id" title="var">Q</span> <span class="id" title="var">X</span> <span class="id" title="var">a</span> <span class="id" title="var">st</span> <span class="id" title="var">st'</span> <span class="id" title="var">HE</span> <span class="id" title="var">HQ</span>.<br/>
<span class="id" title="tactic">inversion</span> <span class="id" title="var">HE</span>. <span class="id" title="tactic">subst</span>.<br/>
<span class="id" title="tactic">unfold</span> <a class="idref" href="Hoare.html#assertion_sub"><span class="id" title="definition">assertion_sub</span></a> <span class="id" title="keyword">in</span> <span class="id" title="var">HQ</span>. <span class="id" title="tactic">assumption</span>. <span class="id" title="keyword">Qed</span>.<br/>
</div>
</div>
<div class="doc">
Here's a first formal proof using this rule.
</div>
<div class="code">
<span class="id" title="keyword">Example</span> <a id="assertion_sub_example" class="idref" href="#assertion_sub_example"><span class="id" title="definition">assertion_sub_example</span></a> :<br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><a class="idref" href="Hoare.html#510fc37417e4aa55210e4cfe35fb96ae"><span class="id" title="notation">(</span></a><span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#::assertion_scope:x_'<'_x"><span class="id" title="notation"><</span></a> 5<a class="idref" href="Hoare.html#510fc37417e4aa55210e4cfe35fb96ae"><span class="id" title="notation">)</span></a> <a class="idref" href="Hoare.html#510fc37417e4aa55210e4cfe35fb96ae"><span class="id" title="notation">[</span></a><span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#510fc37417e4aa55210e4cfe35fb96ae"><span class="id" title="notation"><span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>⊢</span><span style='font-size:90%;'>></span></span></span></span></span></a> <span class="id" title="definition">X</span> <span class="id" title="notation">+</span> 1<a class="idref" href="Hoare.html#510fc37417e4aa55210e4cfe35fb96ae"><span class="id" title="notation">]</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a><br/>
<span class="id" title="definition">X</span> <span class="id" title="notation">:=</span> <span class="id" title="definition">X</span> <span class="id" title="notation">+</span> 1<br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#::assertion_scope:x_'<'_x"><span class="id" title="notation"><</span></a> 5<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
<span class="id" title="tactic">apply</span> <a class="idref" href="Hoare.html#hoare_asgn"><span class="id" title="lemma">hoare_asgn</span></a>. <span class="id" title="keyword">Qed</span>.<br/>
</div>
<div class="doc">
Of course, what we'd probably prefer is to prove this
simpler triple:
<br/>
<span class="inlinecode"> <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> < 4<span style='letter-spacing:-.4em;'>}</span>} <span class="id" title="var">X</span> := <span class="id" title="var">X</span> + 1 <span style='letter-spacing:-.4em;'>{</span>{<span class="id" title="var">X</span> < 5<span style='letter-spacing:-.4em;'>}</span>}
</span> We will see how to do so in the next section.
<div class="paragraph"> </div>
Complete these Hoare triples by providing an appropriate
precondition using <span class="inlinecode"><span class="id" title="tactic">∃</span></span>, then prove then with <span class="inlinecode"><span class="id" title="tactic">apply</span></span>
<span class="inlinecode"><span class="id" title="var">hoare_asgn</span></span>. If you find that tactic doesn't suffice, double check
that you have completed the triple properly. <a id="lab65"></a><h4 class="section">Exercise: 2 stars, standard, optional (hoare_asgn_examples1)</h4>
</div>
<div class="code">
<span class="id" title="keyword">Example</span> <a id="hoare_asgn_examples1" class="idref" href="#hoare_asgn_examples1"><span class="id" title="definition">hoare_asgn_examples1</span></a> :<br/>
<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#a883bdd010993579f99d60b3775bcf54"><span class="id" title="notation">∃</span></a> <a id="P:61" class="idref" href="#P:61"><span class="id" title="binder">P</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#a883bdd010993579f99d60b3775bcf54"><span class="id" title="notation">,</span></a><br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a> <a class="idref" href="Hoare.html#P:61"><span class="id" title="variable">P</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a><br/>
<span class="id" title="definition">X</span> <span class="id" title="notation">:=</span> 2 <span class="id" title="notation">×</span> <span class="id" title="definition">X</span><br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a> <span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">≤</span></a> 10 <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<div class="paragraph"> </div>
<a id="lab66"></a><h4 class="section">Exercise: 2 stars, standard, optional (hoare_asgn_examples2)</h4>
</div>
<div class="code">
<span class="id" title="keyword">Example</span> <a id="hoare_asgn_examples2" class="idref" href="#hoare_asgn_examples2"><span class="id" title="definition">hoare_asgn_examples2</span></a> :<br/>
<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#a883bdd010993579f99d60b3775bcf54"><span class="id" title="notation">∃</span></a> <a id="P:62" class="idref" href="#P:62"><span class="id" title="binder">P</span></a><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#a883bdd010993579f99d60b3775bcf54"><span class="id" title="notation">,</span></a><br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a> <a class="idref" href="Hoare.html#P:62"><span class="id" title="variable">P</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a><br/>
<span class="id" title="definition">X</span> <span class="id" title="notation">:=</span> 3<br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a> 0 <a class="idref" href="Hoare.html#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">≤</span></a> <span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#cce4ad113420d0c25c6ce4891bbe5028"><span class="id" title="notation">∧</span></a> <span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#154e85acf24eb57aa8f8a0156c0ba632"><span class="id" title="notation">≤</span></a> 5 <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<br/>
<span class="id" title="keyword">Proof</span>. <span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<div class="paragraph"> </div>
<a id="lab67"></a><h4 class="section">Exercise: 2 stars, standard, especially useful (hoare_asgn_wrong)</h4>
The assignment rule looks backward to almost everyone the first
time they see it. If it still seems puzzling 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>
</tr>
</table></center> Give a counterexample showing that this rule is incorrect and use
it to complete the proof below, showing that it is really a
counterexample. (Hint: The rule universally quantifies over the
arithmetic expression <span class="inlinecode"><span class="id" title="var">a</span></span>, and your counterexample needs to
exhibit an <span class="inlinecode"><span class="id" title="var">a</span></span> for which the rule doesn't work.)
</div>
<div class="code">
<span class="id" title="keyword">Theorem</span> <a id="hoare_asgn_wrong" class="idref" href="#hoare_asgn_wrong"><span class="id" title="lemma">hoare_asgn_wrong</span></a> : <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#a883bdd010993579f99d60b3775bcf54"><span class="id" title="notation">∃</span></a> <a id="a:63" class="idref" href="#a:63"><span class="id" title="binder">a</span></a>:<span class="id" title="inductive">aexp</span><a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#a883bdd010993579f99d60b3775bcf54"><span class="id" title="notation">,</span></a><br/>
<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#63a68285c81db8f9bc456233bb9ed181"><span class="id" title="notation">¬</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#True"><span class="id" title="inductive">True</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a> <span class="id" title="definition">X</span> <span class="id" title="notation">:=</span> <a class="idref" href="Hoare.html#a:63"><span class="id" title="variable">a</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a> <span class="id" title="definition">X</span> <a class="idref" href="Hoare.html#5ad5c240a08c2b567f422c2e12577e<sub>27</sub>"><span class="id" title="notation">=</span></a> <a class="idref" href="Hoare.html#a:63"><span class="id" title="variable">a</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
</div>
<div class="doc">
<div class="paragraph"> </div>
<a id="lab68"></a><h4 class="section">Exercise: 3 stars, advanced (hoare_asgn_fwd)</h4>
By using a <i>parameter</i> <span class="inlinecode"><span class="id" title="var">m</span></span> (a Coq number) to remember the
original value of <span class="inlinecode"><span class="id" title="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 /\ st X = m<span style='letter-spacing:-.4em;'>}</span>}</td>
<td></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">X := a</td>
<td></td>
</tr>
<tr class="infruleassumption">
<td class="infrule"><span style='letter-spacing:-.4em;'>{</span>{fun st => P (X !<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> m ; st) /\ st X = aeval (X !<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span> m ; st) a <span style='letter-spacing:-.4em;'>}</span>}</td>
<td></td>
</tr>
</table></center> Note that we need to write out the postcondition in "desugared"
form, because it needs to talk about two different states: we use
the original value of <span class="inlinecode"><span class="id" title="var">X</span></span> to reconstruct the state <span class="inlinecode"><span class="id" title="var">st'</span></span> before the
assignment took place. (Also note that this rule is more complicated
than <span class="inlinecode"><span class="id" title="var">hoare_asgn</span></span>!)
<div class="paragraph"> </div>
Prove that this rule is correct.
</div>
<div class="code">
<span class="id" title="keyword">Theorem</span> <a id="hoare_asgn_fwd" class="idref" href="#hoare_asgn_fwd"><span class="id" title="lemma">hoare_asgn_fwd</span></a> :<br/>
<span class="id" title="keyword">∀</span> <a id="m:64" class="idref" href="#m:64"><span class="id" title="binder">m</span></a> <a id="a:65" class="idref" href="#a:65"><span class="id" title="binder">a</span></a> <a id="P:66" class="idref" href="#P:66"><span class="id" title="binder">P</span></a>,<br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><span class="id" title="keyword">fun</span> <a id="st:67" class="idref" href="#st:67"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#P:66"><span class="id" title="variable">P</span></a> <a class="idref" href="Hoare.html#st:67"><span class="id" title="variable">st</span></a> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#ba2b0e492d2b4675a0acf3ea92aabadd"><span class="id" title="notation">∧</span></a> <a class="idref" href="Hoare.html#st:67"><span class="id" title="variable">st</span></a> <span class="id" title="definition">X</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <a class="idref" href="Hoare.html#m:64"><span class="id" title="variable">m</span></a><a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a><br/>
<span class="id" title="definition">X</span> <span class="id" title="notation">:=</span> <a class="idref" href="Hoare.html#a:65"><span class="id" title="variable">a</span></a><br/>
<a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>{</span>{</span></a><span class="id" title="keyword">fun</span> <a id="st:68" class="idref" href="#st:68"><span class="id" title="binder">st</span></a> ⇒ <a class="idref" href="Hoare.html#P:66"><span class="id" title="variable">P</span></a> (<span class="id" title="definition">X</span> <span class="id" title="notation">!<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span></span> <a class="idref" href="Hoare.html#m:64"><span class="id" title="variable">m</span></a> <span class="id" title="notation">;</span> <a class="idref" href="Hoare.html#st:68"><span class="id" title="variable">st</span></a>)<br/>
<a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#ba2b0e492d2b4675a0acf3ea92aabadd"><span class="id" title="notation">∧</span></a> <a class="idref" href="Hoare.html#st:68"><span class="id" title="variable">st</span></a> <span class="id" title="definition">X</span> <a class="idref" href="http://coq.inria.fr/library//Coq.Init.Logic.html#6cd0f7b28b6092304087c7049437bb1a"><span class="id" title="notation">=</span></a> <span class="id" title="definition">aeval</span> (<span class="id" title="definition">X</span> <span class="id" title="notation">!<span class="nowrap"><span style='font-size:85%;'><span style='vertical-align:5%;'><span style='letter-spacing:-.2em;'>-</span></span>></span></span></span> <a class="idref" href="Hoare.html#m:64"><span class="id" title="variable">m</span></a> <span class="id" title="notation">;</span> <a class="idref" href="Hoare.html#st:68"><span class="id" title="variable">st</span></a>) <a class="idref" href="Hoare.html#a:65"><span class="id" title="variable">a</span></a> <a class="idref" href="Hoare.html#a18cbce9fe584a9130c3f34d4f14555f"><span class="id" title="notation"><span style='letter-spacing:-.4em;'>}</span>}</span></a>.<br/>
<span class="id" title="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" title="var">Admitted</span>.<br/>
<font size=-2>☐</font>
</div>