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