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