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