src/HOL/IMP/Hoare_Sound_Complete.thy
author wenzelm
Sun, 13 Dec 2020 23:11:41 +0100
changeset 72907 3883f536d84d
parent 67019 7a3724078363
permissions -rw-r--r--
unused (see 29566b6810f7);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
     2
63538
d7b5e2a222c2 added new vcg based on existentially quantified while-rule
nipkow
parents: 54809
diff changeset
     3
subsection \<open>Soundness and Completeness\<close>
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
     4
63538
d7b5e2a222c2 added new vcg based on existentially quantified while-rule
nipkow
parents: 54809
diff changeset
     5
theory Hoare_Sound_Complete
d7b5e2a222c2 added new vcg based on existentially quantified while-rule
nipkow
parents: 54809
diff changeset
     6
imports Hoare
d7b5e2a222c2 added new vcg based on existentially quantified while-rule
nipkow
parents: 54809
diff changeset
     7
begin
d7b5e2a222c2 added new vcg based on existentially quantified while-rule
nipkow
parents: 54809
diff changeset
     8
d7b5e2a222c2 added new vcg based on existentially quantified while-rule
nipkow
parents: 54809
diff changeset
     9
subsubsection "Soundness"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    10
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    11
lemma hoare_sound: "\<turnstile> {P}c{Q}  \<Longrightarrow>  \<Turnstile> {P}c{Q}"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 43158
diff changeset
    12
proof(induction rule: hoare.induct)
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    13
  case (While P b c)
67019
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 63538
diff changeset
    14
  have "(WHILE b DO c,s) \<Rightarrow> t  \<Longrightarrow>  P s  \<Longrightarrow>  P t \<and> \<not> bval b t" for s t
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 63538
diff changeset
    15
  proof(induction "WHILE b DO c" s t rule: big_step_induct)
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 63538
diff changeset
    16
    case WhileFalse thus ?case by blast
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 63538
diff changeset
    17
  next
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 63538
diff changeset
    18
    case WhileTrue thus ?case
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 63538
diff changeset
    19
      using While.IH unfolding hoare_valid_def by blast
7a3724078363 Replaced { } proofs by local lemmas; added Hoare logic with logical variables.
nipkow
parents: 63538
diff changeset
    20
  qed
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    21
  thus ?case unfolding hoare_valid_def by blast
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    22
qed (auto simp: hoare_valid_def)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    23
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    24
63538
d7b5e2a222c2 added new vcg based on existentially quantified while-rule
nipkow
parents: 54809
diff changeset
    25
subsubsection "Weakest Precondition"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    26
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    27
definition wp :: "com \<Rightarrow> assn \<Rightarrow> assn" where
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    28
"wp c Q = (\<lambda>s. \<forall>t. (c,s) \<Rightarrow> t  \<longrightarrow>  Q t)"
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    29
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    30
lemma wp_SKIP[simp]: "wp SKIP Q = Q"
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    31
by (rule ext) (auto simp: wp_def)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    32
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    33
lemma wp_Ass[simp]: "wp (x::=a) Q = (\<lambda>s. Q(s[a/x]))"
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    34
by (rule ext) (auto simp: wp_def)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    35
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52373
diff changeset
    36
lemma wp_Seq[simp]: "wp (c\<^sub>1;;c\<^sub>2) Q = wp c\<^sub>1 (wp c\<^sub>2 Q)"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    37
by (rule ext) (auto simp: wp_def)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    38
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    39
lemma wp_If[simp]:
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52373
diff changeset
    40
 "wp (IF b THEN c\<^sub>1 ELSE c\<^sub>2) Q =
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52373
diff changeset
    41
 (\<lambda>s. if bval b s then wp c\<^sub>1 Q s else wp c\<^sub>2 Q s)"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    42
by (rule ext) (auto simp: wp_def)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    43
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    44
lemma wp_While_If:
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    45
 "wp (WHILE b DO c) Q s =
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 47818
diff changeset
    46
  wp (IF b THEN c;;WHILE b DO c ELSE SKIP) Q s"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    47
unfolding wp_def by (metis unfold_while)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    48
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    49
lemma wp_While_True[simp]: "bval b s \<Longrightarrow>
52046
bc01725d7918 replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
nipkow
parents: 47818
diff changeset
    50
  wp (WHILE b DO c) Q s = wp (c;; WHILE b DO c) Q s"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    51
by(simp add: wp_While_If)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    52
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    53
lemma wp_While_False[simp]: "\<not> bval b s \<Longrightarrow> wp (WHILE b DO c) Q s = Q s"
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    54
by(simp add: wp_While_If)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    55
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    56
63538
d7b5e2a222c2 added new vcg based on existentially quantified while-rule
nipkow
parents: 54809
diff changeset
    57
subsubsection "Completeness"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    58
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    59
lemma wp_is_pre: "\<turnstile> {wp c Q} c {Q}"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 43158
diff changeset
    60
proof(induction c arbitrary: Q)
52373
a231e6f89737 simplified proofs
nipkow
parents: 52316
diff changeset
    61
  case If thus ?case by(auto intro: conseq)
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    62
next
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    63
  case (While b c)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    64
  let ?w = "WHILE b DO c"
52193
nipkow
parents: 52168
diff changeset
    65
  show "\<turnstile> {wp ?w Q} ?w {Q}"
nipkow
parents: 52168
diff changeset
    66
  proof(rule While')
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    67
    show "\<turnstile> {\<lambda>s. wp ?w Q s \<and> bval b s} c {wp ?w Q}"
52193
nipkow
parents: 52168
diff changeset
    68
    proof(rule strengthen_pre[OF _ While.IH])
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    69
      show "\<forall>s. wp ?w Q s \<and> bval b s \<longrightarrow> wp c (wp ?w Q) s" by auto
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    70
    qed
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    71
    show "\<forall>s. wp ?w Q s \<and> \<not> bval b s \<longrightarrow> Q s" by auto
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    72
  qed
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    73
qed auto
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    74
52291
b7c8675437ec corrected name
nipkow
parents: 52193
diff changeset
    75
lemma hoare_complete: assumes "\<Turnstile> {P}c{Q}" shows "\<turnstile> {P}c{Q}"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    76
proof(rule strengthen_pre)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    77
  show "\<forall>s. P s \<longrightarrow> wp c Q s" using assms
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    78
    by (auto simp: hoare_valid_def wp_def)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    79
  show "\<turnstile> {wp c Q} c {Q}" by(rule wp_is_pre)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    80
qed
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    81
54809
319358e48bb1 added lemma
nipkow
parents: 53015
diff changeset
    82
corollary hoare_sound_complete: "\<turnstile> {P}c{Q} \<longleftrightarrow> \<Turnstile> {P}c{Q}"
319358e48bb1 added lemma
nipkow
parents: 53015
diff changeset
    83
by (metis hoare_complete hoare_sound)
319358e48bb1 added lemma
nipkow
parents: 53015
diff changeset
    84
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    85
end