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