| author | paulson <lp15@cam.ac.uk> | 
| Mon, 16 Sep 2019 17:03:13 +0100 | |
| changeset 70707 | 125705f5965f | 
| parent 69605 | a96320074298 | 
| child 72806 | 4fa08e083865 | 
| permissions | -rw-r--r-- | 
| 33026 | 1 | (* Title: HOL/Isar_Examples/Hoare.thy | 
| 61932 | 2 | Author: Makarius | 
| 10148 | 3 | |
| 4 | A formulation of Hoare logic suitable for Isar. | |
| 5 | *) | |
| 6 | ||
| 58882 | 7 | section \<open>Hoare Logic\<close> | 
| 10148 | 8 | |
| 31758 | 9 | theory Hoare | 
| 63585 | 10 | imports Main | 
| 31758 | 11 | begin | 
| 10148 | 12 | |
| 58614 | 13 | subsection \<open>Abstract syntax and semantics\<close> | 
| 10148 | 14 | |
| 61932 | 15 | text \<open> | 
| 16 | The following abstract syntax and semantics of Hoare Logic over \<^verbatim>\<open>WHILE\<close> | |
| 17 | programs closely follows the existing tradition in Isabelle/HOL of | |
| 18 |   formalizing the presentation given in @{cite \<open>\S6\<close> "Winskel:1993"}. See also
 | |
| 63680 | 19 |   \<^dir>\<open>~~/src/HOL/Hoare\<close> and @{cite "Nipkow:1998:Winskel"}.
 | 
| 61932 | 20 | \<close> | 
| 10148 | 21 | |
| 41818 | 22 | type_synonym 'a bexp = "'a set" | 
| 23 | type_synonym 'a assn = "'a set" | |
| 10148 | 24 | |
| 58310 | 25 | datatype 'a com = | 
| 55656 | 26 | Basic "'a \<Rightarrow> 'a" | 
| 10148 | 27 |   | Seq "'a com" "'a com"    ("(_;/ _)" [60, 61] 60)
 | 
| 28 | | Cond "'a bexp" "'a com" "'a com" | |
| 29 | | While "'a bexp" "'a assn" "'a com" | |
| 30 | ||
| 37671 | 31 | abbreviation Skip  ("SKIP")
 | 
| 55656 | 32 | where "SKIP \<equiv> Basic id" | 
| 10148 | 33 | |
| 55656 | 34 | type_synonym 'a sem = "'a \<Rightarrow> 'a \<Rightarrow> bool" | 
| 10148 | 35 | |
| 55656 | 36 | primrec iter :: "nat \<Rightarrow> 'a bexp \<Rightarrow> 'a sem \<Rightarrow> 'a sem" | 
| 63585 | 37 | where | 
| 38 | "iter 0 b S s s' \<longleftrightarrow> s \<notin> b \<and> s = s'" | |
| 39 | | "iter (Suc n) b S s s' \<longleftrightarrow> s \<in> b \<and> (\<exists>s''. S s s'' \<and> iter n b S s'' s')" | |
| 10148 | 40 | |
| 55656 | 41 | primrec Sem :: "'a com \<Rightarrow> 'a sem" | 
| 63585 | 42 | where | 
| 43 | "Sem (Basic f) s s' \<longleftrightarrow> s' = f s" | |
| 44 | | "Sem (c1; c2) s s' \<longleftrightarrow> (\<exists>s''. Sem c1 s s'' \<and> Sem c2 s'' s')" | |
| 45 | | "Sem (Cond b c1 c2) s s' \<longleftrightarrow> (if s \<in> b then Sem c1 s s' else Sem c2 s s')" | |
| 46 | | "Sem (While b x c) s s' \<longleftrightarrow> (\<exists>n. iter n b (Sem c) s s')" | |
| 10148 | 47 | |
| 63585 | 48 | definition Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool"  ("(3\<turnstile> _/ (2_)/ _)" [100, 55, 100] 50)
 | 
| 55656 | 49 | where "\<turnstile> P c Q \<longleftrightarrow> (\<forall>s s'. Sem c s s' \<longrightarrow> s \<in> P \<longrightarrow> s' \<in> Q)" | 
| 10148 | 50 | |
| 63585 | 51 | lemma ValidI [intro?]: "(\<And>s s'. Sem c s s' \<Longrightarrow> s \<in> P \<Longrightarrow> s' \<in> Q) \<Longrightarrow> \<turnstile> P c Q" | 
| 10148 | 52 | by (simp add: Valid_def) | 
| 53 | ||
| 63585 | 54 | lemma ValidD [dest?]: "\<turnstile> P c Q \<Longrightarrow> Sem c s s' \<Longrightarrow> s \<in> P \<Longrightarrow> s' \<in> Q" | 
| 10148 | 55 | by (simp add: Valid_def) | 
| 56 | ||
| 57 | ||
| 58614 | 58 | subsection \<open>Primitive Hoare rules\<close> | 
| 10148 | 59 | |
| 61932 | 60 | text \<open> | 
| 61 | From the semantics defined above, we derive the standard set of primitive | |
| 62 |   Hoare rules; e.g.\ see @{cite \<open>\S6\<close> "Winskel:1993"}. Usually, variant forms
 | |
| 63 |   of these rules are applied in actual proof, see also \S\ref{sec:hoare-isar}
 | |
| 64 |   and \S\ref{sec:hoare-vcg}.
 | |
| 10148 | 65 | |
| 61541 | 66 | \<^medskip> | 
| 67 | The \<open>basic\<close> rule represents any kind of atomic access to the state space. | |
| 68 | This subsumes the common rules of \<open>skip\<close> and \<open>assign\<close>, as formulated in | |
| 61932 | 69 |   \S\ref{sec:hoare-isar}.
 | 
| 70 | \<close> | |
| 10148 | 71 | |
| 55656 | 72 | theorem basic: "\<turnstile> {s. f s \<in> P} (Basic f) P"
 | 
| 10148 | 73 | proof | 
| 55656 | 74 | fix s s' | 
| 75 |   assume s: "s \<in> {s. f s \<in> P}"
 | |
| 10148 | 76 | assume "Sem (Basic f) s s'" | 
| 37671 | 77 | then have "s' = f s" by simp | 
| 55656 | 78 | with s show "s' \<in> P" by simp | 
| 10148 | 79 | qed | 
| 80 | ||
| 61932 | 81 | text \<open> | 
| 82 | The rules for sequential commands and semantic consequences are established | |
| 83 | in a straight forward manner as follows. | |
| 84 | \<close> | |
| 10148 | 85 | |
| 55656 | 86 | theorem seq: "\<turnstile> P c1 Q \<Longrightarrow> \<turnstile> Q c2 R \<Longrightarrow> \<turnstile> P (c1; c2) R" | 
| 10148 | 87 | proof | 
| 55656 | 88 | assume cmd1: "\<turnstile> P c1 Q" and cmd2: "\<turnstile> Q c2 R" | 
| 89 | fix s s' | |
| 90 | assume s: "s \<in> P" | |
| 10148 | 91 | assume "Sem (c1; c2) s s'" | 
| 92 | then obtain s'' where sem1: "Sem c1 s s''" and sem2: "Sem c2 s'' s'" | |
| 93 | by auto | |
| 55656 | 94 | from cmd1 sem1 s have "s'' \<in> Q" .. | 
| 95 | with cmd2 sem2 show "s' \<in> R" .. | |
| 10148 | 96 | qed | 
| 97 | ||
| 55656 | 98 | theorem conseq: "P' \<subseteq> P \<Longrightarrow> \<turnstile> P c Q \<Longrightarrow> Q \<subseteq> Q' \<Longrightarrow> \<turnstile> P' c Q'" | 
| 10148 | 99 | proof | 
| 55656 | 100 | assume P'P: "P' \<subseteq> P" and QQ': "Q \<subseteq> Q'" | 
| 101 | assume cmd: "\<turnstile> P c Q" | |
| 10148 | 102 | fix s s' :: 'a | 
| 103 | assume sem: "Sem c s s'" | |
| 67613 | 104 | assume "s \<in> P'" with P'P have "s \<in> P" .. | 
| 55656 | 105 | with cmd sem have "s' \<in> Q" .. | 
| 106 | with QQ' show "s' \<in> Q'" .. | |
| 10148 | 107 | qed | 
| 108 | ||
| 61932 | 109 | text \<open> | 
| 110 | The rule for conditional commands is directly reflected by the corresponding | |
| 111 | semantics; in the proof we just have to look closely which cases apply. | |
| 112 | \<close> | |
| 10148 | 113 | |
| 114 | theorem cond: | |
| 55656 | 115 | assumes case_b: "\<turnstile> (P \<inter> b) c1 Q" | 
| 116 | and case_nb: "\<turnstile> (P \<inter> -b) c2 Q" | |
| 117 | shows "\<turnstile> P (Cond b c1 c2) Q" | |
| 10148 | 118 | proof | 
| 55656 | 119 | fix s s' | 
| 120 | assume s: "s \<in> P" | |
| 10148 | 121 | assume sem: "Sem (Cond b c1 c2) s s'" | 
| 55656 | 122 | show "s' \<in> Q" | 
| 10148 | 123 | proof cases | 
| 55656 | 124 | assume b: "s \<in> b" | 
| 10148 | 125 | from case_b show ?thesis | 
| 126 | proof | |
| 127 | from sem b show "Sem c1 s s'" by simp | |
| 55656 | 128 | from s b show "s \<in> P \<inter> b" by simp | 
| 10148 | 129 | qed | 
| 130 | next | |
| 55656 | 131 | assume nb: "s \<notin> b" | 
| 10148 | 132 | from case_nb show ?thesis | 
| 133 | proof | |
| 134 | from sem nb show "Sem c2 s s'" by simp | |
| 67613 | 135 | from s nb show "s \<in> P \<inter> -b" by simp | 
| 10148 | 136 | qed | 
| 137 | qed | |
| 138 | qed | |
| 139 | ||
| 61932 | 140 | text \<open> | 
| 141 | The \<open>while\<close> rule is slightly less trivial --- it is the only one based on | |
| 142 | recursion, which is expressed in the semantics by a Kleene-style least | |
| 61541 | 143 | fixed-point construction. The auxiliary statement below, which is by | 
| 144 | induction on the number of iterations is the main point to be proven; the | |
| 61932 | 145 | rest is by routine application of the semantics of \<^verbatim>\<open>WHILE\<close>. | 
| 146 | \<close> | |
| 10148 | 147 | |
| 18241 | 148 | theorem while: | 
| 55656 | 149 | assumes body: "\<turnstile> (P \<inter> b) c P" | 
| 150 | shows "\<turnstile> P (While b X c) (P \<inter> -b)" | |
| 10148 | 151 | proof | 
| 55656 | 152 | fix s s' assume s: "s \<in> P" | 
| 10148 | 153 | assume "Sem (While b X c) s s'" | 
| 18241 | 154 | then obtain n where "iter n b (Sem c) s s'" by auto | 
| 55656 | 155 | from this and s show "s' \<in> P \<inter> -b" | 
| 20503 | 156 | proof (induct n arbitrary: s) | 
| 19122 | 157 | case 0 | 
| 37671 | 158 | then show ?case by auto | 
| 11987 | 159 | next | 
| 19122 | 160 | case (Suc n) | 
| 55656 | 161 | then obtain s'' where b: "s \<in> b" and sem: "Sem c s s''" | 
| 37671 | 162 | and iter: "iter n b (Sem c) s'' s'" by auto | 
| 55656 | 163 | from Suc and b have "s \<in> P \<inter> b" by simp | 
| 164 | with body sem have "s'' \<in> P" .. | |
| 11987 | 165 | with iter show ?case by (rule Suc) | 
| 10148 | 166 | qed | 
| 167 | qed | |
| 168 | ||
| 169 | ||
| 58614 | 170 | subsection \<open>Concrete syntax for assertions\<close> | 
| 10148 | 171 | |
| 61932 | 172 | text \<open> | 
| 173 | We now introduce concrete syntax for describing commands (with embedded | |
| 174 | expressions) and assertions. The basic technique is that of semantic | |
| 175 | ``quote-antiquote''. A \<^emph>\<open>quotation\<close> is a syntactic entity delimited by an | |
| 176 | implicit abstraction, say over the state space. An \<^emph>\<open>antiquotation\<close> is a | |
| 177 | marked expression within a quotation that refers the implicit argument; a | |
| 178 | typical antiquotation would select (or even update) components from the | |
| 179 | state. | |
| 10148 | 180 | |
| 61541 | 181 | We will see some examples later in the concrete rules and applications. | 
| 10148 | 182 | |
| 61541 | 183 | \<^medskip> | 
| 184 | The following specification of syntax and translations is for Isabelle | |
| 185 | experts only; feel free to ignore it. | |
| 10148 | 186 | |
| 61541 | 187 | While the first part is still a somewhat intelligible specification of the | 
| 188 | concrete syntactic representation of our Hoare language, the actual ``ML | |
| 189 | drivers'' is quite involved. Just note that the we re-use the basic | |
| 69597 | 190 | quote/antiquote translations as already defined in Isabelle/Pure (see \<^ML>\<open>Syntax_Trans.quote_tr\<close>, and \<^ML>\<open>Syntax_Trans.quote_tr'\<close>,). | 
| 61932 | 191 | \<close> | 
| 10148 | 192 | |
| 193 | syntax | |
| 55662 | 194 |   "_quote" :: "'b \<Rightarrow> ('a \<Rightarrow> 'b)"
 | 
| 195 |   "_antiquote" :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b"  ("\<acute>_" [1000] 1000)
 | |
| 196 |   "_Subst" :: "'a bexp \<Rightarrow> 'b \<Rightarrow> idt \<Rightarrow> 'a bexp"  ("_[_'/\<acute>_]" [1000] 999)
 | |
| 197 |   "_Assert" :: "'a \<Rightarrow> 'a set"  ("(\<lbrace>_\<rbrace>)" [0] 1000)
 | |
| 198 |   "_Assign" :: "idt \<Rightarrow> 'b \<Rightarrow> 'a com"  ("(\<acute>_ :=/ _)" [70, 65] 61)
 | |
| 199 | "_Cond" :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com \<Rightarrow> 'a com" | |
| 200 |     ("(0IF _/ THEN _/ ELSE _/ FI)" [0, 0, 0] 61)
 | |
| 201 | "_While_inv" :: "'a bexp \<Rightarrow> 'a assn \<Rightarrow> 'a com \<Rightarrow> 'a com" | |
| 202 |     ("(0WHILE _/ INV _ //DO _ /OD)"  [0, 0, 0] 61)
 | |
| 203 |   "_While" :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com"  ("(0WHILE _ //DO _ /OD)"  [0, 0] 61)
 | |
| 10148 | 204 | |
| 205 | translations | |
| 55662 | 206 | "\<lbrace>b\<rbrace>" \<rightharpoonup> "CONST Collect (_quote b)" | 
| 207 | "B [a/\<acute>x]" \<rightharpoonup> "\<lbrace>\<acute>(_update_name x (\<lambda>_. a)) \<in> B\<rbrace>" | |
| 208 | "\<acute>x := a" \<rightharpoonup> "CONST Basic (_quote (\<acute>(_update_name x (\<lambda>_. a))))" | |
| 55656 | 209 | "IF b THEN c1 ELSE c2 FI" \<rightharpoonup> "CONST Cond \<lbrace>b\<rbrace> c1 c2" | 
| 55662 | 210 | "WHILE b INV i DO c OD" \<rightharpoonup> "CONST While \<lbrace>b\<rbrace> i c" | 
| 211 | "WHILE b DO c OD" \<rightleftharpoons> "WHILE b INV CONST undefined DO c OD" | |
| 10148 | 212 | |
| 58614 | 213 | parse_translation \<open> | 
| 10148 | 214 | let | 
| 69597 | 215 | fun quote_tr [t] = Syntax_Trans.quote_tr \<^syntax_const>\<open>_antiquote\<close> t | 
| 10148 | 216 |       | quote_tr ts = raise TERM ("quote_tr", ts);
 | 
| 69597 | 217 | in [(\<^syntax_const>\<open>_quote\<close>, K quote_tr)] end | 
| 58614 | 218 | \<close> | 
| 10148 | 219 | |
| 61932 | 220 | text \<open> | 
| 221 | As usual in Isabelle syntax translations, the part for printing is more | |
| 222 | complicated --- we cannot express parts as macro rules as above. Don't look | |
| 223 | here, unless you have to do similar things for yourself. | |
| 224 | \<close> | |
| 10148 | 225 | |
| 58614 | 226 | print_translation \<open> | 
| 10148 | 227 | let | 
| 228 | fun quote_tr' f (t :: ts) = | |
| 69597 | 229 | Term.list_comb (f $ Syntax_Trans.quote_tr' \<^syntax_const>\<open>_antiquote\<close> t, ts) | 
| 10148 | 230 | | quote_tr' _ _ = raise Match; | 
| 231 | ||
| 69597 | 232 | val assert_tr' = quote_tr' (Syntax.const \<^syntax_const>\<open>_Assert\<close>); | 
| 10148 | 233 | |
| 69597 | 234 | fun bexp_tr' name ((Const (\<^const_syntax>\<open>Collect\<close>, _) $ t) :: ts) = | 
| 10148 | 235 | quote_tr' (Syntax.const name) (t :: ts) | 
| 236 | | bexp_tr' _ _ = raise Match; | |
| 237 | ||
| 25706 | 238 | fun assign_tr' (Abs (x, _, f $ k $ Bound 0) :: ts) = | 
| 69597 | 239 | quote_tr' (Syntax.const \<^syntax_const>\<open>_Assign\<close> $ Syntax_Trans.update_name_tr' f) | 
| 42284 | 240 | (Abs (x, dummyT, Syntax_Trans.const_abs_tr' k) :: ts) | 
| 10148 | 241 | | assign_tr' _ = raise Match; | 
| 242 | in | |
| 69597 | 243 | [(\<^const_syntax>\<open>Collect\<close>, K assert_tr'), | 
| 244 | (\<^const_syntax>\<open>Basic\<close>, K assign_tr'), | |
| 245 | (\<^const_syntax>\<open>Cond\<close>, K (bexp_tr' \<^syntax_const>\<open>_Cond\<close>)), | |
| 246 | (\<^const_syntax>\<open>While\<close>, K (bexp_tr' \<^syntax_const>\<open>_While_inv\<close>))] | |
| 10148 | 247 | end | 
| 58614 | 248 | \<close> | 
| 10148 | 249 | |
| 250 | ||
| 58614 | 251 | subsection \<open>Rules for single-step proof \label{sec:hoare-isar}\<close>
 | 
| 10148 | 252 | |
| 61932 | 253 | text \<open> | 
| 254 | We are now ready to introduce a set of Hoare rules to be used in single-step | |
| 255 | structured proofs in Isabelle/Isar. We refer to the concrete syntax | |
| 256 | introduce above. | |
| 10148 | 257 | |
| 61541 | 258 | \<^medskip> | 
| 259 | Assertions of Hoare Logic may be manipulated in calculational proofs, with | |
| 260 | the inclusion expressed in terms of sets or predicates. Reversed order is | |
| 61932 | 261 | supported as well. | 
| 262 | \<close> | |
| 10148 | 263 | |
| 55656 | 264 | lemma [trans]: "\<turnstile> P c Q \<Longrightarrow> P' \<subseteq> P \<Longrightarrow> \<turnstile> P' c Q" | 
| 10148 | 265 | by (unfold Valid_def) blast | 
| 55656 | 266 | lemma [trans] : "P' \<subseteq> P \<Longrightarrow> \<turnstile> P c Q \<Longrightarrow> \<turnstile> P' c Q" | 
| 10148 | 267 | by (unfold Valid_def) blast | 
| 268 | ||
| 55656 | 269 | lemma [trans]: "Q \<subseteq> Q' \<Longrightarrow> \<turnstile> P c Q \<Longrightarrow> \<turnstile> P c Q'" | 
| 10148 | 270 | by (unfold Valid_def) blast | 
| 55656 | 271 | lemma [trans]: "\<turnstile> P c Q \<Longrightarrow> Q \<subseteq> Q' \<Longrightarrow> \<turnstile> P c Q'" | 
| 10148 | 272 | by (unfold Valid_def) blast | 
| 273 | ||
| 274 | lemma [trans]: | |
| 55656 | 275 | "\<turnstile> \<lbrace>\<acute>P\<rbrace> c Q \<Longrightarrow> (\<And>s. P' s \<longrightarrow> P s) \<Longrightarrow> \<turnstile> \<lbrace>\<acute>P'\<rbrace> c Q" | 
| 10148 | 276 | by (simp add: Valid_def) | 
| 277 | lemma [trans]: | |
| 55656 | 278 | "(\<And>s. P' s \<longrightarrow> P s) \<Longrightarrow> \<turnstile> \<lbrace>\<acute>P\<rbrace> c Q \<Longrightarrow> \<turnstile> \<lbrace>\<acute>P'\<rbrace> c Q" | 
| 10148 | 279 | by (simp add: Valid_def) | 
| 280 | ||
| 281 | lemma [trans]: | |
| 55656 | 282 | "\<turnstile> P c \<lbrace>\<acute>Q\<rbrace> \<Longrightarrow> (\<And>s. Q s \<longrightarrow> Q' s) \<Longrightarrow> \<turnstile> P c \<lbrace>\<acute>Q'\<rbrace>" | 
| 10148 | 283 | by (simp add: Valid_def) | 
| 284 | lemma [trans]: | |
| 55656 | 285 | "(\<And>s. Q s \<longrightarrow> Q' s) \<Longrightarrow> \<turnstile> P c \<lbrace>\<acute>Q\<rbrace> \<Longrightarrow> \<turnstile> P c \<lbrace>\<acute>Q'\<rbrace>" | 
| 10148 | 286 | by (simp add: Valid_def) | 
| 287 | ||
| 288 | ||
| 61932 | 289 | text \<open> | 
| 290 | Identity and basic assignments.\<^footnote>\<open>The \<open>hoare\<close> method introduced in | |
| 291 |   \S\ref{sec:hoare-vcg} is able to provide proper instances for any number of
 | |
| 292 | basic assignments, without producing additional verification conditions.\<close> | |
| 293 | \<close> | |
| 10148 | 294 | |
| 55656 | 295 | lemma skip [intro?]: "\<turnstile> P SKIP P" | 
| 10148 | 296 | proof - | 
| 55656 | 297 |   have "\<turnstile> {s. id s \<in> P} SKIP P" by (rule basic)
 | 
| 37671 | 298 | then show ?thesis by simp | 
| 10148 | 299 | qed | 
| 300 | ||
| 55656 | 301 | lemma assign: "\<turnstile> P [\<acute>a/\<acute>x::'a] \<acute>x := \<acute>a P" | 
| 10148 | 302 | by (rule basic) | 
| 303 | ||
| 61932 | 304 | text \<open> | 
| 305 | Note that above formulation of assignment corresponds to our preferred way | |
| 306 |   to model state spaces, using (extensible) record types in HOL @{cite
 | |
| 307 | "Naraschewski-Wenzel:1998:HOOL"}. For any record field \<open>x\<close>, Isabelle/HOL | |
| 308 | provides a functions \<open>x\<close> (selector) and \<open>x_update\<close> (update). Above, there is | |
| 309 | only a place-holder appearing for the latter kind of function: due to | |
| 310 | concrete syntax \<open>\<acute>x := \<acute>a\<close> also contains \<open>x_update\<close>.\<^footnote>\<open>Note that due to the | |
| 311 | external nature of HOL record fields, we could not even state a general | |
| 312 | theorem relating selector and update functions (if this were required here); | |
| 313 | this would only work for any particular instance of record fields introduced | |
| 314 | so far.\<close> | |
| 10148 | 315 | |
| 61541 | 316 | \<^medskip> | 
| 61932 | 317 | Sequential composition --- normalizing with associativity achieves proper of | 
| 318 | chunks of code verified separately. | |
| 319 | \<close> | |
| 10148 | 320 | |
| 321 | lemmas [trans, intro?] = seq | |
| 322 | ||
| 55656 | 323 | lemma seq_assoc [simp]: "\<turnstile> P c1;(c2;c3) Q \<longleftrightarrow> \<turnstile> P (c1;c2);c3 Q" | 
| 10148 | 324 | by (auto simp add: Valid_def) | 
| 325 | ||
| 58614 | 326 | text \<open>Conditional statements.\<close> | 
| 10148 | 327 | |
| 328 | lemmas [trans, intro?] = cond | |
| 329 | ||
| 330 | lemma [trans, intro?]: | |
| 55656 | 331 | "\<turnstile> \<lbrace>\<acute>P \<and> \<acute>b\<rbrace> c1 Q | 
| 332 | \<Longrightarrow> \<turnstile> \<lbrace>\<acute>P \<and> \<not> \<acute>b\<rbrace> c2 Q | |
| 333 | \<Longrightarrow> \<turnstile> \<lbrace>\<acute>P\<rbrace> IF \<acute>b THEN c1 ELSE c2 FI Q" | |
| 10148 | 334 | by (rule cond) (simp_all add: Valid_def) | 
| 335 | ||
| 58614 | 336 | text \<open>While statements --- with optional invariant.\<close> | 
| 10148 | 337 | |
| 55662 | 338 | lemma [intro?]: "\<turnstile> (P \<inter> b) c P \<Longrightarrow> \<turnstile> P (While b P c) (P \<inter> -b)" | 
| 10148 | 339 | by (rule while) | 
| 340 | ||
| 55662 | 341 | lemma [intro?]: "\<turnstile> (P \<inter> b) c P \<Longrightarrow> \<turnstile> P (While b undefined c) (P \<inter> -b)" | 
| 10148 | 342 | by (rule while) | 
| 343 | ||
| 344 | ||
| 345 | lemma [intro?]: | |
| 55656 | 346 | "\<turnstile> \<lbrace>\<acute>P \<and> \<acute>b\<rbrace> c \<lbrace>\<acute>P\<rbrace> | 
| 347 | \<Longrightarrow> \<turnstile> \<lbrace>\<acute>P\<rbrace> WHILE \<acute>b INV \<lbrace>\<acute>P\<rbrace> DO c OD \<lbrace>\<acute>P \<and> \<not> \<acute>b\<rbrace>" | |
| 10148 | 348 | by (simp add: while Collect_conj_eq Collect_neg_eq) | 
| 349 | ||
| 350 | lemma [intro?]: | |
| 55656 | 351 | "\<turnstile> \<lbrace>\<acute>P \<and> \<acute>b\<rbrace> c \<lbrace>\<acute>P\<rbrace> | 
| 352 | \<Longrightarrow> \<turnstile> \<lbrace>\<acute>P\<rbrace> WHILE \<acute>b DO c OD \<lbrace>\<acute>P \<and> \<not> \<acute>b\<rbrace>" | |
| 10148 | 353 | by (simp add: while Collect_conj_eq Collect_neg_eq) | 
| 354 | ||
| 355 | ||
| 58614 | 356 | subsection \<open>Verification conditions \label{sec:hoare-vcg}\<close>
 | 
| 10148 | 357 | |
| 61932 | 358 | text \<open> | 
| 359 | We now load the \<^emph>\<open>original\<close> ML file for proof scripts and tactic definition | |
| 63680 | 360 | for the Hoare Verification Condition Generator (see \<^dir>\<open>~~/src/HOL/Hoare\<close>). | 
| 361 | As far as we are concerned here, the result is a proof method \<open>hoare\<close>, which | |
| 362 | may be applied to a Hoare Logic assertion to extract purely logical | |
| 363 | verification conditions. It is important to note that the method requires | |
| 364 | \<^verbatim>\<open>WHILE\<close> loops to be fully annotated with invariants beforehand. | |
| 365 | Furthermore, only \<^emph>\<open>concrete\<close> pieces of code are handled --- the underlying | |
| 366 | tactic fails ungracefully if supplied with meta-variables or parameters, for | |
| 367 | example. | |
| 61932 | 368 | \<close> | 
| 10148 | 369 | |
| 13862 | 370 | lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q" | 
| 18193 | 371 | by (auto simp add: Valid_def) | 
| 13862 | 372 | |
| 373 | lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q"
 | |
| 18193 | 374 | by (auto simp: Valid_def) | 
| 13862 | 375 | |
| 376 | lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (c1;c2) R" | |
| 18193 | 377 | by (auto simp: Valid_def) | 
| 13862 | 378 | |
| 379 | lemma CondRule: | |
| 18193 | 380 |   "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}
 | 
| 381 | \<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q" | |
| 382 | by (auto simp: Valid_def) | |
| 13862 | 383 | |
| 18241 | 384 | lemma iter_aux: | 
| 55656 | 385 | "\<forall>s s'. Sem c s s' \<longrightarrow> s \<in> I \<and> s \<in> b \<longrightarrow> s' \<in> I \<Longrightarrow> | 
| 386 | (\<And>s s'. s \<in> I \<Longrightarrow> iter n b (Sem c) s s' \<Longrightarrow> s' \<in> I \<and> s' \<notin> b)" | |
| 387 | by (induct n) auto | |
| 13862 | 388 | |
| 389 | lemma WhileRule: | |
| 18193 | 390 | "p \<subseteq> i \<Longrightarrow> Valid (i \<inter> b) c i \<Longrightarrow> i \<inter> (-b) \<subseteq> q \<Longrightarrow> Valid p (While b i c) q" | 
| 391 | apply (clarsimp simp: Valid_def) | |
| 392 | apply (drule iter_aux) | |
| 393 | prefer 2 | |
| 394 | apply assumption | |
| 395 | apply blast | |
| 396 | apply blast | |
| 397 | done | |
| 13862 | 398 | |
| 26303 | 399 | lemma Compl_Collect: "- Collect b = {x. \<not> b x}"
 | 
| 400 | by blast | |
| 401 | ||
| 67443 
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
 wenzelm parents: 
63680diff
changeset | 402 | lemmas AbortRule = SkipRule \<comment> \<open>dummy version\<close> | 
| 28457 
25669513fd4c
major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
 wenzelm parents: 
26303diff
changeset | 403 | |
| 69605 | 404 | ML_file \<open>~~/src/HOL/Hoare/hoare_tac.ML\<close> | 
| 10148 | 405 | |
| 58614 | 406 | method_setup hoare = | 
| 407 | \<open>Scan.succeed (fn ctxt => | |
| 30510 
4120fc59dd85
unified type Proof.method and pervasive METHOD combinators;
 wenzelm parents: 
28524diff
changeset | 408 | (SIMPLE_METHOD' | 
| 58614 | 409 | (Hoare.hoare_tac ctxt | 
| 410 |         (simp_tac (put_simpset HOL_basic_ss ctxt addsimps [@{thm "Record.K_record_comp"}] )))))\<close>
 | |
| 10148 | 411 | "verification condition generator for Hoare logic" | 
| 412 | ||
| 13703 | 413 | end |