1476
|
1 |
(* Title: HOL/IMP/VC.thy
|
|
2 |
Author: Tobias Nipkow
|
1447
|
3 |
|
|
4 |
acom: annotated commands
|
|
5 |
vc: verification-conditions
|
2810
|
6 |
awp: weakest (liberal) precondition
|
1447
|
7 |
*)
|
|
8 |
|
12431
|
9 |
header "Verification Conditions"
|
|
10 |
|
35754
|
11 |
theory VC imports Hoare_Op begin
|
1447
|
12 |
|
|
13 |
datatype acom = Askip
|
|
14 |
| Aass loc aexp
|
|
15 |
| Asemi acom acom
|
|
16 |
| Aif bexp acom acom
|
|
17 |
| Awhile bexp assn acom
|
|
18 |
|
27362
|
19 |
primrec awp :: "acom => assn => assn"
|
|
20 |
where
|
2810
|
21 |
"awp Askip Q = Q"
|
27362
|
22 |
| "awp (Aass x a) Q = (\<lambda>s. Q(s[x\<mapsto>a s]))"
|
|
23 |
| "awp (Asemi c d) Q = awp c (awp d Q)"
|
|
24 |
| "awp (Aif b c d) Q = (\<lambda>s. (b s-->awp c Q s) & (~b s-->awp d Q s))"
|
|
25 |
| "awp (Awhile b I c) Q = I"
|
1447
|
26 |
|
27362
|
27 |
primrec vc :: "acom => assn => assn"
|
|
28 |
where
|
12431
|
29 |
"vc Askip Q = (\<lambda>s. True)"
|
27362
|
30 |
| "vc (Aass x a) Q = (\<lambda>s. True)"
|
|
31 |
| "vc (Asemi c d) Q = (\<lambda>s. vc c (awp d Q) s & vc d Q s)"
|
|
32 |
| "vc (Aif b c d) Q = (\<lambda>s. vc c Q s & vc d Q s)"
|
|
33 |
| "vc (Awhile b I c) Q = (\<lambda>s. (I s & ~b s --> Q s) &
|
2810
|
34 |
(I s & b s --> awp c I s) & vc c I s)"
|
1447
|
35 |
|
27362
|
36 |
primrec astrip :: "acom => com"
|
|
37 |
where
|
1900
|
38 |
"astrip Askip = SKIP"
|
27362
|
39 |
| "astrip (Aass x a) = (x:==a)"
|
|
40 |
| "astrip (Asemi c d) = (astrip c;astrip d)"
|
|
41 |
| "astrip (Aif b c d) = (\<IF> b \<THEN> astrip c \<ELSE> astrip d)"
|
|
42 |
| "astrip (Awhile b I c) = (\<WHILE> b \<DO> astrip c)"
|
1451
|
43 |
|
2810
|
44 |
(* simultaneous computation of vc and awp: *)
|
27362
|
45 |
primrec vcawp :: "acom => assn => assn \<times> assn"
|
|
46 |
where
|
12431
|
47 |
"vcawp Askip Q = (\<lambda>s. True, Q)"
|
27362
|
48 |
| "vcawp (Aass x a) Q = (\<lambda>s. True, \<lambda>s. Q(s[x\<mapsto>a s]))"
|
|
49 |
| "vcawp (Asemi c d) Q = (let (vcd,wpd) = vcawp d Q;
|
2810
|
50 |
(vcc,wpc) = vcawp c wpd
|
12431
|
51 |
in (\<lambda>s. vcc s & vcd s, wpc))"
|
27362
|
52 |
| "vcawp (Aif b c d) Q = (let (vcd,wpd) = vcawp d Q;
|
2810
|
53 |
(vcc,wpc) = vcawp c Q
|
12431
|
54 |
in (\<lambda>s. vcc s & vcd s,
|
|
55 |
\<lambda>s.(b s --> wpc s) & (~b s --> wpd s)))"
|
27362
|
56 |
| "vcawp (Awhile b I c) Q = (let (vcc,wpc) = vcawp c I
|
12431
|
57 |
in (\<lambda>s. (I s & ~b s --> Q s) &
|
2810
|
58 |
(I s & b s --> wpc s) & vcc s, I))"
|
1451
|
59 |
|
12431
|
60 |
(*
|
|
61 |
Soundness and completeness of vc
|
|
62 |
*)
|
|
63 |
|
35754
|
64 |
declare hoare.conseq [intro]
|
12431
|
65 |
|
|
66 |
|
35754
|
67 |
lemma vc_sound: "(ALL s. vc c Q s) \<Longrightarrow> |- {awp c Q} astrip c {Q}"
|
|
68 |
proof(induct c arbitrary: Q)
|
|
69 |
case (Awhile b I c)
|
|
70 |
show ?case
|
|
71 |
proof(simp, rule While')
|
|
72 |
from `\<forall>s. vc (Awhile b I c) Q s`
|
|
73 |
have vc: "ALL s. vc c I s" and IQ: "ALL s. I s \<and> \<not> b s \<longrightarrow> Q s" and
|
|
74 |
awp: "ALL s. I s & b s --> awp c I s" by simp_all
|
|
75 |
from vc have "|- {awp c I} astrip c {I}" using Awhile.hyps by blast
|
|
76 |
with awp show "|- {\<lambda>s. I s \<and> b s} astrip c {I}"
|
|
77 |
by(rule strengthen_pre)
|
|
78 |
show "\<forall>s. I s \<and> \<not> b s \<longrightarrow> Q s" by(rule IQ)
|
|
79 |
qed
|
|
80 |
qed auto
|
12431
|
81 |
|
|
82 |
|
35754
|
83 |
lemma awp_mono:
|
|
84 |
"(!s. P s --> Q s) ==> awp c P s ==> awp c Q s"
|
|
85 |
proof (induct c arbitrary: P Q s)
|
|
86 |
case Asemi thus ?case by simp metis
|
|
87 |
qed simp_all
|
|
88 |
|
|
89 |
lemma vc_mono:
|
|
90 |
"(!s. P s --> Q s) ==> vc c P s ==> vc c Q s"
|
|
91 |
proof(induct c arbitrary: P Q)
|
|
92 |
case Asemi thus ?case by simp (metis awp_mono)
|
|
93 |
qed simp_all
|
12431
|
94 |
|
13596
|
95 |
lemma vc_complete: assumes der: "|- {P}c{Q}"
|
18372
|
96 |
shows "(\<exists>ac. astrip ac = c & (\<forall>s. vc ac Q s) & (\<forall>s. P s --> awp ac Q s))"
|
13596
|
97 |
(is "? ac. ?Eq P c Q ac")
|
|
98 |
using der
|
|
99 |
proof induct
|
|
100 |
case skip
|
|
101 |
show ?case (is "? ac. ?C ac")
|
|
102 |
proof show "?C Askip" by simp qed
|
|
103 |
next
|
23746
|
104 |
case (ass P x a)
|
13596
|
105 |
show ?case (is "? ac. ?C ac")
|
|
106 |
proof show "?C(Aass x a)" by simp qed
|
|
107 |
next
|
23746
|
108 |
case (semi P c1 Q c2 R)
|
13596
|
109 |
from semi.hyps obtain ac1 where ih1: "?Eq P c1 Q ac1" by fast
|
|
110 |
from semi.hyps obtain ac2 where ih2: "?Eq Q c2 R ac2" by fast
|
|
111 |
show ?case (is "? ac. ?C ac")
|
|
112 |
proof
|
|
113 |
show "?C(Asemi ac1 ac2)"
|
|
114 |
using ih1 ih2 by simp (fast elim!: awp_mono vc_mono)
|
|
115 |
qed
|
|
116 |
next
|
23746
|
117 |
case (If P b c1 Q c2)
|
13596
|
118 |
from If.hyps obtain ac1 where ih1: "?Eq (%s. P s & b s) c1 Q ac1" by fast
|
|
119 |
from If.hyps obtain ac2 where ih2: "?Eq (%s. P s & ~b s) c2 Q ac2" by fast
|
|
120 |
show ?case (is "? ac. ?C ac")
|
|
121 |
proof
|
|
122 |
show "?C(Aif b ac1 ac2)"
|
|
123 |
using ih1 ih2 by simp
|
|
124 |
qed
|
|
125 |
next
|
|
126 |
case (While P b c)
|
|
127 |
from While.hyps obtain ac where ih: "?Eq (%s. P s & b s) c P ac" by fast
|
|
128 |
show ?case (is "? ac. ?C ac")
|
|
129 |
proof show "?C(Awhile b P ac)" using ih by simp qed
|
|
130 |
next
|
|
131 |
case conseq thus ?case by(fast elim!: awp_mono vc_mono)
|
|
132 |
qed
|
12431
|
133 |
|
18372
|
134 |
lemma vcawp_vc_awp: "vcawp c Q = (vc c Q, awp c Q)"
|
20503
|
135 |
by (induct c arbitrary: Q) (simp_all add: Let_def)
|
12431
|
136 |
|
1447
|
137 |
end
|