author | nipkow |
Sat, 01 Jun 2013 11:48:06 +0200 | |
changeset 52281 | 780b3870319f |
parent 52228 | ee8e3eaad24c |
permissions | -rw-r--r-- |
50421 | 1 |
(* Author: Tobias Nipkow *) |
43158 | 2 |
|
52228 | 3 |
theory HoareT imports Hoare_Sound_Complete Hoare_Examples begin |
43158 | 4 |
|
50421 | 5 |
subsection "Hoare Logic for Total Correctness" |
6 |
||
46203 | 7 |
text{* Note that this definition of total validity @{text"\<Turnstile>\<^sub>t"} only |
8 |
works if execution is deterministic (which it is in our case). *} |
|
43158 | 9 |
|
10 |
definition hoare_tvalid :: "assn \<Rightarrow> com \<Rightarrow> assn \<Rightarrow> bool" |
|
11 |
("\<Turnstile>\<^sub>t {(1_)}/ (_)/ {(1_)}" 50) where |
|
52281 | 12 |
"\<Turnstile>\<^sub>t {P}c{Q} \<longleftrightarrow> (\<forall>s. P s \<longrightarrow> (\<exists>t. (c,s) \<Rightarrow> t \<and> Q t))" |
43158 | 13 |
|
45114 | 14 |
text{* Provability of Hoare triples in the proof system for total |
43158 | 15 |
correctness is written @{text"\<turnstile>\<^sub>t {P}c{Q}"} and defined |
16 |
inductively. The rules for @{text"\<turnstile>\<^sub>t"} differ from those for |
|
17 |
@{text"\<turnstile>"} only in the one place where nontermination can arise: the |
|
18 |
@{term While}-rule. *} |
|
19 |
||
20 |
inductive |
|
21 |
hoaret :: "assn \<Rightarrow> com \<Rightarrow> assn \<Rightarrow> bool" ("\<turnstile>\<^sub>t ({(1_)}/ (_)/ {(1_)})" 50) |
|
22 |
where |
|
52281 | 23 |
|
24 |
Skip: "\<turnstile>\<^sub>t {P} SKIP {P}" | |
|
25 |
||
26 |
Assign: "\<turnstile>\<^sub>t {\<lambda>s. P(s[a/x])} x::=a {P}" | |
|
27 |
||
28 |
Seq: "\<lbrakk> \<turnstile>\<^sub>t {P\<^isub>1} c\<^isub>1 {P\<^isub>2}; \<turnstile>\<^sub>t {P\<^isub>2} c\<^isub>2 {P\<^isub>3} \<rbrakk> \<Longrightarrow> \<turnstile>\<^sub>t {P\<^isub>1} c\<^isub>1;;c\<^isub>2 {P\<^isub>3}" | |
|
29 |
||
43158 | 30 |
If: "\<lbrakk> \<turnstile>\<^sub>t {\<lambda>s. P s \<and> bval b s} c\<^isub>1 {Q}; \<turnstile>\<^sub>t {\<lambda>s. P s \<and> \<not> bval b s} c\<^isub>2 {Q} \<rbrakk> |
52281 | 31 |
\<Longrightarrow> \<turnstile>\<^sub>t {P} IF b THEN c\<^isub>1 ELSE c\<^isub>2 {Q}" | |
32 |
||
43158 | 33 |
While: |
52281 | 34 |
"(\<And>n::nat. |
35 |
\<turnstile>\<^sub>t {\<lambda>s. P s \<and> bval b s \<and> T s n} c {\<lambda>s. P s \<and> (\<exists>n'. T s n' \<and> n' < n)}) |
|
36 |
\<Longrightarrow> \<turnstile>\<^sub>t {\<lambda>s. P s \<and> (\<exists>n. T s n)} WHILE b DO c {\<lambda>s. P s \<and> \<not>bval b s}" | |
|
37 |
||
43158 | 38 |
conseq: "\<lbrakk> \<forall>s. P' s \<longrightarrow> P s; \<turnstile>\<^sub>t {P}c{Q}; \<forall>s. Q s \<longrightarrow> Q' s \<rbrakk> \<Longrightarrow> |
39 |
\<turnstile>\<^sub>t {P'}c{Q'}" |
|
40 |
||
41 |
text{* The @{term While}-rule is like the one for partial correctness but it |
|
42 |
requires additionally that with every execution of the loop body some measure |
|
52281 | 43 |
relation @{term[source]"T :: state \<Rightarrow> nat \<Rightarrow> bool"} decreases. |
44 |
The following functional version is more intuitive: *} |
|
45 |
||
46 |
lemma While_fun: |
|
47 |
"\<lbrakk> \<And>n::nat. \<turnstile>\<^sub>t {\<lambda>s. P s \<and> bval b s \<and> n = f s} c {\<lambda>s. P s \<and> f s < n}\<rbrakk> |
|
48 |
\<Longrightarrow> \<turnstile>\<^sub>t {P} WHILE b DO c {\<lambda>s. P s \<and> \<not>bval b s}" |
|
49 |
by (rule While [where T="\<lambda>s n. n = f s", simplified]) |
|
50 |
||
51 |
text{* Building in the consequence rule: *} |
|
43158 | 52 |
|
53 |
lemma strengthen_pre: |
|
54 |
"\<lbrakk> \<forall>s. P' s \<longrightarrow> P s; \<turnstile>\<^sub>t {P} c {Q} \<rbrakk> \<Longrightarrow> \<turnstile>\<^sub>t {P'} c {Q}" |
|
55 |
by (metis conseq) |
|
56 |
||
57 |
lemma weaken_post: |
|
58 |
"\<lbrakk> \<turnstile>\<^sub>t {P} c {Q}; \<forall>s. Q s \<longrightarrow> Q' s \<rbrakk> \<Longrightarrow> \<turnstile>\<^sub>t {P} c {Q'}" |
|
59 |
by (metis conseq) |
|
60 |
||
61 |
lemma Assign': "\<forall>s. P s \<longrightarrow> Q(s[a/x]) \<Longrightarrow> \<turnstile>\<^sub>t {P} x ::= a {Q}" |
|
62 |
by (simp add: strengthen_pre[OF _ Assign]) |
|
63 |
||
52281 | 64 |
lemma While_fun': |
65 |
assumes "\<And>n::nat. \<turnstile>\<^sub>t {\<lambda>s. P s \<and> bval b s \<and> n = f s} c {\<lambda>s. P s \<and> f s < n}" |
|
43158 | 66 |
and "\<forall>s. P s \<and> \<not> bval b s \<longrightarrow> Q s" |
52281 | 67 |
shows "\<turnstile>\<^sub>t {P} WHILE b DO c {Q}" |
68 |
by(blast intro: assms(1) weaken_post[OF While_fun assms(2)]) |
|
43158 | 69 |
|
52227 | 70 |
|
43158 | 71 |
text{* Our standard example: *} |
72 |
||
52228 | 73 |
lemma "\<turnstile>\<^sub>t {\<lambda>s. s ''x'' = i} ''y'' ::= N 0;; wsum {\<lambda>s. s ''y'' = sum i}" |
47818 | 74 |
apply(rule Seq) |
52228 | 75 |
prefer 2 |
52281 | 76 |
apply(rule While_fun' [where P = "\<lambda>s. (s ''y'' = sum i - sum(s ''x''))" |
77 |
and f = "\<lambda>s. nat(s ''x'')"]) |
|
52228 | 78 |
apply(rule Seq) |
79 |
prefer 2 |
|
80 |
apply(rule Assign) |
|
81 |
apply(rule Assign') |
|
82 |
apply simp |
|
83 |
apply(simp add: minus_numeral_simps(1)[symmetric] del: minus_numeral_simps) |
|
84 |
apply(simp) |
|
43158 | 85 |
apply(rule Assign') |
86 |
apply simp |
|
87 |
done |
|
88 |
||
89 |
||
90 |
text{* The soundness theorem: *} |
|
91 |
||
92 |
theorem hoaret_sound: "\<turnstile>\<^sub>t {P}c{Q} \<Longrightarrow> \<Turnstile>\<^sub>t {P}c{Q}" |
|
93 |
proof(unfold hoare_tvalid_def, induct rule: hoaret.induct) |
|
52227 | 94 |
case (While P b T c) |
95 |
{ |
|
96 |
fix s n |
|
52228 | 97 |
have "\<lbrakk> P s; T s n \<rbrakk> \<Longrightarrow> \<exists>t. (WHILE b DO c, s) \<Rightarrow> t \<and> P t \<and> \<not> bval b t" |
52227 | 98 |
proof(induction "n" arbitrary: s rule: less_induct) |
43158 | 99 |
case (less n) |
100 |
thus ?case by (metis While(2) WhileFalse WhileTrue) |
|
101 |
qed |
|
52227 | 102 |
} |
103 |
thus ?case by auto |
|
43158 | 104 |
next |
105 |
case If thus ?case by auto blast |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44177
diff
changeset
|
106 |
qed fastforce+ |
43158 | 107 |
|
108 |
||
109 |
text{* |
|
110 |
The completeness proof proceeds along the same lines as the one for partial |
|
111 |
correctness. First we have to strengthen our notion of weakest precondition |
|
112 |
to take termination into account: *} |
|
113 |
||
114 |
definition wpt :: "com \<Rightarrow> assn \<Rightarrow> assn" ("wp\<^sub>t") where |
|
115 |
"wp\<^sub>t c Q \<equiv> \<lambda>s. \<exists>t. (c,s) \<Rightarrow> t \<and> Q t" |
|
116 |
||
117 |
lemma [simp]: "wp\<^sub>t SKIP Q = Q" |
|
118 |
by(auto intro!: ext simp: wpt_def) |
|
119 |
||
120 |
lemma [simp]: "wp\<^sub>t (x ::= e) Q = (\<lambda>s. Q(s(x := aval e s)))" |
|
121 |
by(auto intro!: ext simp: wpt_def) |
|
122 |
||
52046
bc01725d7918
replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents:
50421
diff
changeset
|
123 |
lemma [simp]: "wp\<^sub>t (c\<^isub>1;;c\<^isub>2) Q = wp\<^sub>t c\<^isub>1 (wp\<^sub>t c\<^isub>2 Q)" |
43158 | 124 |
unfolding wpt_def |
125 |
apply(rule ext) |
|
126 |
apply auto |
|
127 |
done |
|
128 |
||
129 |
lemma [simp]: |
|
130 |
"wp\<^sub>t (IF b THEN c\<^isub>1 ELSE c\<^isub>2) Q = (\<lambda>s. wp\<^sub>t (if bval b s then c\<^isub>1 else c\<^isub>2) Q s)" |
|
131 |
apply(unfold wpt_def) |
|
132 |
apply(rule ext) |
|
133 |
apply auto |
|
134 |
done |
|
135 |
||
136 |
||
137 |
text{* Now we define the number of iterations @{term "WHILE b DO c"} needs to |
|
138 |
terminate when started in state @{text s}. Because this is a truly partial |
|
139 |
function, we define it as an (inductive) relation first: *} |
|
140 |
||
141 |
inductive Its :: "bexp \<Rightarrow> com \<Rightarrow> state \<Rightarrow> nat \<Rightarrow> bool" where |
|
142 |
Its_0: "\<not> bval b s \<Longrightarrow> Its b c s 0" | |
|
143 |
Its_Suc: "\<lbrakk> bval b s; (c,s) \<Rightarrow> s'; Its b c s' n \<rbrakk> \<Longrightarrow> Its b c s (Suc n)" |
|
144 |
||
145 |
text{* The relation is in fact a function: *} |
|
146 |
||
147 |
lemma Its_fun: "Its b c s n \<Longrightarrow> Its b c s n' \<Longrightarrow> n=n'" |
|
45015 | 148 |
proof(induction arbitrary: n' rule:Its.induct) |
43158 | 149 |
case Its_0 thus ?case by(metis Its.cases) |
150 |
next |
|
151 |
case Its_Suc thus ?case by(metis Its.cases big_step_determ) |
|
152 |
qed |
|
153 |
||
154 |
text{* For all terminating loops, @{const Its} yields a result: *} |
|
155 |
||
156 |
lemma WHILE_Its: "(WHILE b DO c,s) \<Rightarrow> t \<Longrightarrow> \<exists>n. Its b c s n" |
|
45015 | 157 |
proof(induction "WHILE b DO c" s t rule: big_step_induct) |
43158 | 158 |
case WhileFalse thus ?case by (metis Its_0) |
159 |
next |
|
160 |
case WhileTrue thus ?case by (metis Its_Suc) |
|
161 |
qed |
|
162 |
||
163 |
lemma wpt_is_pre: "\<turnstile>\<^sub>t {wp\<^sub>t c Q} c {Q}" |
|
45015 | 164 |
proof (induction c arbitrary: Q) |
43158 | 165 |
case SKIP show ?case by simp (blast intro:hoaret.Skip) |
166 |
next |
|
167 |
case Assign show ?case by simp (blast intro:hoaret.Assign) |
|
168 |
next |
|
47818 | 169 |
case Seq thus ?case by simp (blast intro:hoaret.Seq) |
43158 | 170 |
next |
171 |
case If thus ?case by simp (blast intro:hoaret.If hoaret.conseq) |
|
172 |
next |
|
173 |
case (While b c) |
|
174 |
let ?w = "WHILE b DO c" |
|
52228 | 175 |
let ?T = "Its b c" |
52227 | 176 |
have "\<forall>s. wp\<^sub>t (WHILE b DO c) Q s \<longrightarrow> wp\<^sub>t (WHILE b DO c) Q s \<and> (\<exists>n. Its b c s n)" |
177 |
unfolding wpt_def by (metis WHILE_Its) |
|
178 |
moreover |
|
43158 | 179 |
{ fix n |
52227 | 180 |
{ fix s t |
52228 | 181 |
assume "bval b s" "?T s n" "(?w, s) \<Rightarrow> t" "Q t" |
52227 | 182 |
from `bval b s` `(?w, s) \<Rightarrow> t` obtain s' where |
183 |
"(c,s) \<Rightarrow> s'" "(?w,s') \<Rightarrow> t" by auto |
|
52228 | 184 |
from `(?w, s') \<Rightarrow> t` obtain n'' where "?T s' n''" by (blast dest: WHILE_Its) |
52227 | 185 |
with `bval b s` `(c, s) \<Rightarrow> s'` |
52228 | 186 |
have "?T s (Suc n'')" by (rule Its_Suc) |
187 |
with `?T s n` have "n = Suc n''" by (rule Its_fun) |
|
188 |
with `(c,s) \<Rightarrow> s'` `(?w,s') \<Rightarrow> t` `Q t` `?T s' n''` |
|
189 |
have "wp\<^sub>t c (\<lambda>s'. wp\<^sub>t ?w Q s' \<and> (\<exists>n'. ?T s' n' \<and> n' < n)) s" |
|
52227 | 190 |
by (auto simp: wpt_def) |
191 |
} |
|
52228 | 192 |
hence "\<forall>s. wp\<^sub>t ?w Q s \<and> bval b s \<and> ?T s n \<longrightarrow> |
193 |
wp\<^sub>t c (\<lambda>s'. wp\<^sub>t ?w Q s' \<and> (\<exists>n'. ?T s' n' \<and> n' < n)) s" |
|
52227 | 194 |
unfolding wpt_def by auto |
195 |
(* by (metis WhileE Its_Suc Its_fun WHILE_Its lessI) *) |
|
43158 | 196 |
note strengthen_pre[OF this While] |
197 |
} note hoaret.While[OF this] |
|
198 |
moreover have "\<forall>s. wp\<^sub>t ?w Q s \<and> \<not> bval b s \<longrightarrow> Q s" by (auto simp add:wpt_def) |
|
52227 | 199 |
ultimately show ?case by (rule conseq) |
43158 | 200 |
qed |
201 |
||
202 |
||
52227 | 203 |
text{*\noindent In the @{term While}-case, @{const Its} provides the obvious |
43158 | 204 |
termination argument. |
205 |
||
206 |
The actual completeness theorem follows directly, in the same manner |
|
207 |
as for partial correctness: *} |
|
208 |
||
209 |
theorem hoaret_complete: "\<Turnstile>\<^sub>t {P}c{Q} \<Longrightarrow> \<turnstile>\<^sub>t {P}c{Q}" |
|
210 |
apply(rule strengthen_pre[OF _ wpt_is_pre]) |
|
211 |
apply(auto simp: hoare_tvalid_def hoare_valid_def wpt_def) |
|
212 |
done |
|
213 |
||
214 |
end |