forked from sven--/Software-Foundations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HoareAsLogic.html
428 lines (354 loc) · 44.1 KB
/
HoareAsLogic.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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="coqdoc.css" rel="stylesheet" type="text/css"/>
<title>HoareAsLogic: Hoare Logic as a Logic</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="page">
<div id="header">
</div>
<div id="main">
<h1 class="libtitle">HoareAsLogic<span class="subtitle">Hoare Logic as a Logic</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* $Date: 2013-07-17 16:19:11 -0400 (Wed, 17 Jul 2013) $ *)</span><br/>
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <span class="id" type="var">Hoare</span>.<br/>
<br/>
</div>
<div class="doc">
The presentation of Hoare logic in chapter <span class="inlinecode"><span class="id" type="var">Hoare</span></span> could be
described as "model-theoretic": the proof rules for each of the
constructors were presented as <i>theorems</i> about the evaluation
behavior of programs, and proofs of program correctness (validity
of Hoare triples) were constructed by combining these theorems
directly in Coq.
<div class="paragraph"> </div>
Another way of presenting Hoare logic is to define a completely
separate proof system — a set of axioms and inference rules that
talk about commands, Hoare triples, etc. — and then say that a
proof of a Hoare triple is a valid derivation in <i>that</i> logic. We
can do this by giving an inductive definition of <i>valid
derivations</i> in this new logic.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">hoare_proof</span> : <span class="id" type="var">Assertion</span> <span style="font-family: arial;">→</span> <span class="id" type="var">com</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Assertion</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">H_Skip</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span>, <br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> (<span class="id" type="var">SKIP</span>) <span class="id" type="var">P</span><br/>
| <span class="id" type="var">H_Asgn</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">Q</span> <span class="id" type="var">V</span> <span class="id" type="var">a</span>, <br/>
<span class="id" type="var">hoare_proof</span> (<span class="id" type="var">assn_sub</span> <span class="id" type="var">V</span> <span class="id" type="var">a</span> <span class="id" type="var">Q</span>) (<span class="id" type="var">V</span> ::= <span class="id" type="var">a</span>) <span class="id" type="var">Q</span><br/>
| <span class="id" type="var">H_Seq</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span> <span class="id" type="var">d</span> <span class="id" type="var">R</span>, <br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> <span class="id" type="var">hoare_proof</span> <span class="id" type="var">Q</span> <span class="id" type="var">d</span> <span class="id" type="var">R</span> <span style="font-family: arial;">→</span> <span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> (<span class="id" type="var">c</span>;;<span class="id" type="var">d</span>) <span class="id" type="var">R</span><br/>
| <span class="id" type="var">H_If</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">b</span> <span class="id" type="var">c1</span> <span class="id" type="var">c2</span>,<br/>
<span class="id" type="var">hoare_proof</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">P</span> <span class="id" type="var">st</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">bassn</span> <span class="id" type="var">b</span> <span class="id" type="var">st</span>) <span class="id" type="var">c1</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">hoare_proof</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">P</span> <span class="id" type="var">st</span> <span style="font-family: arial;">∧</span> ~(<span class="id" type="var">bassn</span> <span class="id" type="var">b</span> <span class="id" type="var">st</span>)) <span class="id" type="var">c2</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> (<span class="id" type="var">IFB</span> <span class="id" type="var">b</span> <span class="id" type="var">THEN</span> <span class="id" type="var">c1</span> <span class="id" type="var">ELSE</span> <span class="id" type="var">c2</span> <span class="id" type="var">FI</span>) <span class="id" type="var">Q</span><br/>
| <span class="id" type="var">H_While</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">hoare_proof</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">P</span> <span class="id" type="var">st</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">bassn</span> <span class="id" type="var">b</span> <span class="id" type="var">st</span>) <span class="id" type="var">c</span> <span class="id" type="var">P</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> (<span class="id" type="var">WHILE</span> <span class="id" type="var">b</span> <span class="id" type="var">DO</span> <span class="id" type="var">c</span> <span class="id" type="var">END</span>) (<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">P</span> <span class="id" type="var">st</span> <span style="font-family: arial;">∧</span> ¬ (<span class="id" type="var">bassn</span> <span class="id" type="var">b</span> <span class="id" type="var">st</span>))<br/>
| <span class="id" type="var">H_Consequence</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">P'</span> <span class="id" type="var">Q'</span> : <span class="id" type="var">Assertion</span>) <span class="id" type="var">c</span>, <br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P'</span> <span class="id" type="var">c</span> <span class="id" type="var">Q'</span> <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">st</span>, <span class="id" type="var">P</span> <span class="id" type="var">st</span> <span style="font-family: arial;">→</span> <span class="id" type="var">P'</span> <span class="id" type="var">st</span>) <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">st</span>, <span class="id" type="var">Q'</span> <span class="id" type="var">st</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span> <span class="id" type="var">st</span>) <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span>.<br/>
<br/>
<span class="id" type="keyword">Tactic Notation</span> "hoare_proof_cases" <span class="id" type="var">tactic</span>(<span class="id" type="var">first</span>) <span class="id" type="var">ident</span>(<span class="id" type="var">c</span>) :=<br/>
<span class="id" type="var">first</span>;<br/>
[ <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "H_Skip" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "H_Asgn" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "H_Seq"<br/>
| <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "H_If" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "H_While" | <span class="id" type="var">Case_aux</span> <span class="id" type="var">c</span> "H_Consequence" ].<br/>
<br/>
</div>
<div class="doc">
We don't need to include axioms corresponding to <span class="inlinecode"><span class="id" type="var">hoare_consequence_pre</span></span>
or <span class="inlinecode"><span class="id" type="var">hoare_consequence_post</span></span>, because these can be proven easily
from <span class="inlinecode"><span class="id" type="var">H_Consequence</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">H_Consequence_pre</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">P'</span>: <span class="id" type="var">Assertion</span>) <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P'</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">st</span>, <span class="id" type="var">P</span> <span class="id" type="var">st</span> <span style="font-family: arial;">→</span> <span class="id" type="var">P'</span> <span class="id" type="var">st</span>) <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">H_Consequence_post</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">Q'</span> : <span class="id" type="var">Assertion</span>) <span class="id" type="var">c</span>, <br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q'</span> <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">st</span>, <span class="id" type="var">Q'</span> <span class="id" type="var">st</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span> <span class="id" type="var">st</span>) <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
Now, for example, let's construct a proof object representing a
derivation for the hoare triple
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">assn_sub</span> <span class="id" type="var">X</span> (<span class="id" type="var">X</span>+1) (<span class="id" type="var">assn_sub</span> <span class="id" type="var">X</span> (<span class="id" type="var">X</span>+2) (<span class="id" type="var">X</span>=3))<span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">X</span>::=<span class="id" type="var">X</span>+1;; <span class="id" type="var">X</span>::=<span class="id" type="var">X</span>+2 <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">X</span>=3<span style="letter-spacing:-.4em;">}</span>}.
<div class="paragraph"> </div>
</div>
We can use Coq's tactics to help us construct the proof object.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">sample_proof</span><br/>
: <span class="id" type="var">hoare_proof</span> <br/>
(<span class="id" type="var">assn_sub</span> <span class="id" type="var">X</span> (<span class="id" type="var">APlus</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) (<span class="id" type="var">ANum</span> 1))<br/>
(<span class="id" type="var">assn_sub</span> <span class="id" type="var">X</span> (<span class="id" type="var">APlus</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) (<span class="id" type="var">ANum</span> 2))<br/>
(<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">st</span> <span class="id" type="var">X</span> = 3) ))<br/>
(<span class="id" type="var">X</span> ::= <span class="id" type="var">APlus</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) (<span class="id" type="var">ANum</span> 1);; (<span class="id" type="var">X</span> ::= <span class="id" type="var">APlus</span> (<span class="id" type="var">AId</span> <span class="id" type="var">X</span>) (<span class="id" type="var">ANum</span> 2)))<br/>
(<span class="id" type="keyword">fun</span> <span class="id" type="var">st</span> ⇒ <span class="id" type="var">st</span> <span class="id" type="var">X</span> = 3).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Seq</span>; <span class="id" type="tactic">apply</span> <span class="id" type="var">H_Asgn</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="comment">(* <br/>
Print sample_proof.<br/>
====> <br/>
H_Seq<br/>
(assn_sub X (APlus (AId X) (ANum 1))<br/>
(assn_sub X (APlus (AId X) (ANum 2)) (fun st : state => st X = VNat 3)))<br/>
(X ::= APlus (AId X) (ANum 1))<br/>
(assn_sub X (APlus (AId X) (ANum 2)) (fun st : state => st X = VNat 3))<br/>
(X ::= APlus (AId X) (ANum 2)) (fun st : state => st X = VNat 3)<br/>
(H_Asgn<br/>
(assn_sub X (APlus (AId X) (ANum 2)) (fun st : state => st X = VNat 3))<br/>
X (APlus (AId X) (ANum 1)))<br/>
(H_Asgn (fun st : state => st X = VNat 3) X (APlus (AId X) (ANum 2)))<br/>
*)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab584"></a><h4 class="section">Exercise: 2 stars (hoare_proof_sound)</h4>
Prove that such proof objects represent true claims.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">hoare_proof_sound</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span>,<br/>
<span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>}.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
We can also use Coq's reasoning facilities to prove metatheorems
about Hoare Logic. For example, here are the analogs of two
theorems we saw in chapter <span class="inlinecode"><span class="id" type="var">Hoare</span></span> — this time expressed in terms
of the syntax of Hoare Logic derivations (provability) rather than
directly in terms of the semantics of Hoare triples.
<div class="paragraph"> </div>
The first one says that, for every <span class="inlinecode"><span class="id" type="var">P</span></span> and <span class="inlinecode"><span class="id" type="var">c</span></span>, the assertion
<span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P</span><span style="letter-spacing:-.4em;">}</span>}</span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>}</span> is <i>provable</i> in Hoare Logic. Note that the
proof is more complex than the semantic proof in <span class="inlinecode"><span class="id" type="var">Hoare</span></span>: we
actually need to perform an induction over the structure of the
command <span class="inlinecode"><span class="id" type="var">c</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">H_Post_True_deriv</span>:<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">P</span>, <span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">_</span> ⇒ <span class="id" type="var">True</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">c</span>.<br/>
<span class="id" type="var">com_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">c</span>) <span class="id" type="var">Case</span>; <span class="id" type="tactic">intro</span> <span class="id" type="var">P</span>.<br/>
<span class="id" type="var">Case</span> "SKIP".<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">H_Skip</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>.<br/>
<span class="comment">(* Proof of True *)</span><br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">I</span>.<br/>
<span class="id" type="var">Case</span> "::=".<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence_pre</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">H_Asgn</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">I</span>.<br/>
<span class="id" type="var">Case</span> ";;".<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence_pre</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Seq</span>.<br/>
<span class="id" type="tactic">apply</span> (<span class="id" type="var">IHc1</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">_</span> ⇒ <span class="id" type="var">True</span>)).<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHc2</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">I</span>.<br/>
<span class="id" type="var">Case</span> "IFB".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">H_Consequence_pre</span> <span class="id" type="keyword">with</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">_</span> ⇒ <span class="id" type="var">True</span>).<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">H_If</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHc1</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHc2</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">I</span>.<br/>
<span class="id" type="var">Case</span> "WHILE".<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_While</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">IHc</span>.<br/>
<span class="id" type="tactic">intros</span>; <span class="id" type="tactic">apply</span> <span class="id" type="var">I</span>.<br/>
<span class="id" type="tactic">intros</span>; <span class="id" type="tactic">apply</span> <span class="id" type="var">I</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Similarly, we can show that <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">False</span><span style="letter-spacing:-.4em;">}</span>}</span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>}</span> is provable for
any <span class="inlinecode"><span class="id" type="var">c</span></span> and <span class="inlinecode"><span class="id" type="var">Q</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">False_and_P_imp</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span>,<br/>
<span class="id" type="var">False</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">P</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> [<span class="id" type="var">CONTRA</span> <span class="id" type="var">HP</span>].<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">CONTRA</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Tactic Notation</span> "pre_false_helper" <span class="id" type="var">constr</span>(<span class="id" type="var">CONSTR</span>) :=<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence_pre</span>;<br/>
[<span class="id" type="tactic">eapply</span> <span class="id" type="var">CONSTR</span> | <span class="id" type="tactic">intros</span> ? <span class="id" type="var">CONTRA</span>; <span class="id" type="tactic">destruct</span> <span class="id" type="var">CONTRA</span>].<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">H_Pre_False_deriv</span>:<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">Q</span>, <span class="id" type="var">hoare_proof</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">_</span> ⇒ <span class="id" type="var">False</span>) <span class="id" type="var">c</span> <span class="id" type="var">Q</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">c</span>.<br/>
<span class="id" type="var">com_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">c</span>) <span class="id" type="var">Case</span>; <span class="id" type="tactic">intro</span> <span class="id" type="var">Q</span>.<br/>
<span class="id" type="var">Case</span> "SKIP". <span class="id" type="var">pre_false_helper</span> <span class="id" type="var">H_Skip</span>.<br/>
<span class="id" type="var">Case</span> "::=". <span class="id" type="var">pre_false_helper</span> <span class="id" type="var">H_Asgn</span>.<br/>
<span class="id" type="var">Case</span> ";;". <span class="id" type="var">pre_false_helper</span> <span class="id" type="var">H_Seq</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">IHc1</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">IHc2</span>.<br/>
<span class="id" type="var">Case</span> "IFB".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">H_If</span>; <span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence_pre</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHc1</span>. <span class="id" type="tactic">intro</span>. <span class="id" type="tactic">eapply</span> <span class="id" type="var">False_and_P_imp</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHc2</span>. <span class="id" type="tactic">intro</span>. <span class="id" type="tactic">eapply</span> <span class="id" type="var">False_and_P_imp</span>.<br/>
<span class="id" type="var">Case</span> "WHILE".<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence_post</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_While</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence_pre</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHc</span>.<br/>
<span class="id" type="tactic">intro</span>. <span class="id" type="tactic">eapply</span> <span class="id" type="var">False_and_P_imp</span>.<br/>
<span class="id" type="tactic">intro</span>. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">eapply</span> <span class="id" type="var">False_and_P_imp</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
As a last step, we can show that the set of <span class="inlinecode"><span class="id" type="var">hoare_proof</span></span> axioms is
sufficient to prove any true fact about (partial) correctness.
More precisely, any semantic Hoare triple that we can prove can
also be proved from these axioms. Such a set of axioms is said
to be <i>relatively complete</i>.
<div class="paragraph"> </div>
This proof is inspired by the one at
http://www.ps.uni-saarland.de/courses/sem-ws11/script/Hoare.html
<div class="paragraph"> </div>
To prove this fact, we'll need to invent some intermediate
assertions using a technical device known as <i>weakest preconditions</i>.
Given a command <span class="inlinecode"><span class="id" type="var">c</span></span> and a desired postcondition assertion <span class="inlinecode"><span class="id" type="var">Q</span></span>,
the weakest precondition <span class="inlinecode"><span class="id" type="var">wp</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> is an assertion <span class="inlinecode"><span class="id" type="var">P</span></span> such that
<span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P</span><span style="letter-spacing:-.4em;">}</span>}</span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>}</span> holds, and moreover, for any other assertion <span class="inlinecode"><span class="id" type="var">P'</span></span>,
if <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P'</span><span style="letter-spacing:-.4em;">}</span>}</span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>}</span> holds then <span class="inlinecode"><span class="id" type="var">P'</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span>. We can more directly
define this as follows:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">wp</span> (<span class="id" type="var">c</span>:<span class="id" type="var">com</span>) (<span class="id" type="var">Q</span>:<span class="id" type="var">Assertion</span>) : <span class="id" type="var">Assertion</span> := <br/>
<span class="id" type="keyword">fun</span> <span class="id" type="var">s</span> ⇒ <span style="font-family: arial;">∀</span><span class="id" type="var">s'</span>, <span class="id" type="var">c</span> / <span class="id" type="var">s</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">s'</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span> <span class="id" type="var">s'</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab585"></a><h4 class="section">Exercise: 1 star (wp_is_precondition)</h4>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">wp_is_precondition</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">Q</span>, <br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">wp</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>}.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab586"></a><h4 class="section">Exercise: 1 star (wp_is_weakest)</h4>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">wp_is_weakest</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">Q</span> <span class="id" type="var">P'</span>, <br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P'</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>} <span style="font-family: arial;">→</span> <span style="font-family: arial;">∀</span><span class="id" type="var">st</span>, <span class="id" type="var">P'</span> <span class="id" type="var">st</span> <span style="font-family: arial;">→</span> <span class="id" type="var">wp</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span> <span class="id" type="var">st</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
The following utility lemma will also be useful.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">bassn_eval_false</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">st</span>, ¬ <span class="id" type="var">bassn</span> <span class="id" type="var">b</span> <span class="id" type="var">st</span> <span style="font-family: arial;">→</span> <span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span> = <span class="id" type="var">false</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">b</span> <span class="id" type="var">st</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">bassn</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">destruct</span> (<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span>).<br/>
<span class="id" type="var">exfalso</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab587"></a><h4 class="section">Exercise: 4 stars (hoare_proof_complete)</h4>
Complete the proof of the theorem.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">hoare_proof_complete</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span>,<br/>
<span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P</span><span style="letter-spacing:-.4em;">}</span>} <span class="id" type="var">c</span> <span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">Q</span><span style="letter-spacing:-.4em;">}</span>} <span style="font-family: arial;">→</span> <span class="id" type="var">hoare_proof</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span> <span class="id" type="var">Q</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">c</span>. <span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">P</span>.<br/>
<span class="id" type="var">com_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">c</span>) <span class="id" type="var">Case</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">HT</span>.<br/>
<span class="id" type="var">Case</span> "SKIP".<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Skip</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="var">eassumption</span>.<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">st</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HT</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">E_Skip</span>.<br/>
<span class="id" type="var">Case</span> "::=".<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Consequence</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">H_Asgn</span>.<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">st</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HT</span>. <span class="id" type="var">econstructor</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="tactic">intros</span>; <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="var">Case</span> ";;".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">H_Seq</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">wp</span> <span class="id" type="var">c2</span> <span class="id" type="var">Q</span>).<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">IHc1</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">E1</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">wp</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">st''</span> <span class="id" type="var">E2</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">HT</span>. <span class="id" type="var">econstructor</span>; <span class="id" type="var">eassumption</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">IHc2</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">st</span> <span class="id" type="var">st'</span> <span class="id" type="var">E1</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>; <span class="id" type="tactic">assumption</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
Finally, we might hope that our axiomatic Hoare logic is <i>decidable</i>;
that is, that there is an (terminating) algorithm (a <i>decision procedure</i>)
that can determine whether or not a given Hoare triple is valid (derivable).
But such a decision procedure cannot exist!
<div class="paragraph"> </div>
Consider the triple <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span><span style="letter-spacing:-.4em;">}</span>}</span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">False</span><span style="letter-spacing:-.4em;">}</span>}</span>. This triple is valid
if and only if <span class="inlinecode"><span class="id" type="var">c</span></span> is non-terminating. So any algorithm that could
determine validity of arbitrary triples could solve the Halting Problem.
<div class="paragraph"> </div>
Similarly, the triple <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">True</span>}</span> <span class="inlinecode"><span class="id" type="var">SKIP</span></span> <span class="inlinecode"><span style="letter-spacing:-.4em;">{</span>{<span class="id" type="var">P</span><span style="letter-spacing:-.4em;">}</span>}</span> is valid if and only if
<span class="inlinecode"><span style="font-family: arial;">∀</span></span> <span class="inlinecode"><span class="id" type="var">s</span>,</span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">s</span></span> is valid, where <span class="inlinecode"><span class="id" type="var">P</span></span> is an arbitrary assertion of Coq's
logic. But it is known that there can be no decision procedure for
this logic.
<div class="paragraph"> </div>
<div class="paragraph"> </div>
Overall, this axiomatic style of presentation gives a clearer picture of what it
means to "give a proof in Hoare logic." However, it is not
entirely satisfactory from the point of view of writing down such
proofs in practice: it is quite verbose. The section of chapter
<span class="inlinecode"><span class="id" type="var">Hoare2</span></span> on formalizing decorated programs shows how we can do even
better.
</div>
<div class="code code-tight">
<br/>
</div>
</div>
<div id="footer">
<hr/><a href="coqindex.html">Index</a><hr/>This page has been generated by <a href="http://www.lix.polytechnique.fr/coq/">coqdoc</a>
</div>
</div>
</body>
</html>