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