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