| 
35749
 | 
     1  | 
(*  Title:      HOL/IMP/Hoare_Op.thy
  | 
| 
 | 
     2  | 
    Author:     Tobias Nipkow
  | 
| 
 | 
     3  | 
*)
  | 
| 
 | 
     4  | 
  | 
| 
35754
 | 
     5  | 
header "Soundness and Completeness wrt Operational Semantics"
  | 
| 
35749
 | 
     6  | 
  | 
| 
35754
 | 
     7  | 
theory Hoare_Op imports Hoare begin
  | 
| 
35749
 | 
     8  | 
  | 
| 
 | 
     9  | 
definition
  | 
| 
 | 
    10  | 
  hoare_valid :: "[assn,com,assn] => bool" ("|= {(1_)}/ (_)/ {(1_)}" 50) where
 | 
| 
 | 
    11  | 
  "|= {P}c{Q} = (!s t. \<langle>c,s\<rangle> \<longrightarrow>\<^sub>c t --> P s --> Q t)"
 | 
| 
 | 
    12  | 
  | 
| 
 | 
    13  | 
lemma hoare_sound: "|- {P}c{Q} ==> |= {P}c{Q}"
 | 
| 
 | 
    14  | 
proof(induct rule: hoare.induct)
  | 
| 
 | 
    15  | 
  case (While P b c)
  | 
| 
 | 
    16  | 
  { fix s t
 | 
| 
 | 
    17  | 
    assume "\<langle>WHILE b DO c,s\<rangle> \<longrightarrow>\<^sub>c t"
  | 
| 
 | 
    18  | 
    hence "P s \<longrightarrow> P t \<and> \<not> b t"
  | 
| 
 | 
    19  | 
    proof(induct "WHILE b DO c" s t)
  | 
| 
 | 
    20  | 
      case WhileFalse thus ?case by blast
  | 
| 
 | 
    21  | 
    next
  | 
| 
 | 
    22  | 
      case WhileTrue thus ?case
  | 
| 
 | 
    23  | 
        using While(2) unfolding hoare_valid_def by blast
  | 
| 
 | 
    24  | 
    qed
  | 
| 
 | 
    25  | 
  | 
| 
 | 
    26  | 
  }
  | 
| 
 | 
    27  | 
  thus ?case unfolding hoare_valid_def by blast
  | 
| 
 | 
    28  | 
qed (auto simp: hoare_valid_def)
  | 
| 
 | 
    29  | 
  | 
| 
 | 
    30  | 
definition
  | 
| 
 | 
    31  | 
  wp :: "com => assn => assn" where
  | 
| 
 | 
    32  | 
  "wp c Q = (%s. !t. \<langle>c,s\<rangle> \<longrightarrow>\<^sub>c t --> Q t)"
  | 
| 
 | 
    33  | 
  | 
| 
 | 
    34  | 
lemma wp_SKIP: "wp \<SKIP> Q = Q"
  | 
| 
 | 
    35  | 
by (simp add: wp_def)
  | 
| 
 | 
    36  | 
  | 
| 
 | 
    37  | 
lemma wp_Ass: "wp (x:==a) Q = (%s. Q(s[x\<mapsto>a s]))"
  | 
| 
 | 
    38  | 
by (simp add: wp_def)
  | 
| 
 | 
    39  | 
  | 
| 
 | 
    40  | 
lemma wp_Semi: "wp (c;d) Q = wp c (wp d Q)"
  | 
| 
 | 
    41  | 
by (rule ext) (auto simp: wp_def)
  | 
| 
 | 
    42  | 
  | 
| 
 | 
    43  | 
lemma wp_If:
  | 
| 
 | 
    44  | 
 "wp (\<IF> b \<THEN> c \<ELSE> d) Q = (%s. (b s --> wp c Q s) &  (~b s --> wp d Q s))"
  | 
| 
 | 
    45  | 
by (rule ext) (auto simp: wp_def)
  | 
| 
 | 
    46  | 
  | 
| 
 | 
    47  | 
lemma wp_While_If:
  | 
| 
 | 
    48  | 
 "wp (\<WHILE> b \<DO> c) Q s =
  | 
| 
 | 
    49  | 
  wp (IF b THEN c;\<WHILE> b \<DO> c ELSE SKIP) Q s"
  | 
| 
 | 
    50  | 
unfolding wp_def by (metis equivD1 equivD2 unfold_while)
  | 
| 
 | 
    51  | 
  | 
| 
 | 
    52  | 
lemma wp_While_True: "b s ==>
  | 
| 
 | 
    53  | 
  wp (\<WHILE> b \<DO> c) Q s = wp (c;\<WHILE> b \<DO> c) Q s"
  | 
| 
 | 
    54  | 
by(simp add: wp_While_If wp_If wp_SKIP)
  | 
| 
 | 
    55  | 
  | 
| 
 | 
    56  | 
lemma wp_While_False: "~b s ==> wp (\<WHILE> b \<DO> c) Q s = Q s"
  | 
| 
 | 
    57  | 
by(simp add: wp_While_If wp_If wp_SKIP)
  | 
| 
 | 
    58  | 
  | 
| 
 | 
    59  | 
lemmas [simp] = wp_SKIP wp_Ass wp_Semi wp_If wp_While_True wp_While_False
  | 
| 
 | 
    60  | 
  | 
| 
 | 
    61  | 
lemma wp_is_pre: "|- {wp c Q} c {Q}"
 | 
| 
 | 
    62  | 
proof(induct c arbitrary: Q)
  | 
| 
 | 
    63  | 
  case SKIP show ?case by auto
  | 
| 
 | 
    64  | 
next
  | 
| 
 | 
    65  | 
  case Assign show ?case by auto
  | 
| 
 | 
    66  | 
next
  | 
| 
 | 
    67  | 
  case Semi thus ?case by(auto intro: semi)
  | 
| 
 | 
    68  | 
next
  | 
| 
 | 
    69  | 
  case (Cond b c1 c2)
  | 
| 
 | 
    70  | 
  let ?If = "IF b THEN c1 ELSE c2"
  | 
| 
 | 
    71  | 
  show ?case
  | 
| 
 | 
    72  | 
  proof(rule If)
  | 
| 
 | 
    73  | 
    show "|- {\<lambda>s. wp ?If Q s \<and> b s} c1 {Q}"
 | 
| 
 | 
    74  | 
    proof(rule strengthen_pre[OF _ Cond(1)])
  | 
| 
 | 
    75  | 
      show "\<forall>s. wp ?If Q s \<and> b s \<longrightarrow> wp c1 Q s" by auto
  | 
| 
 | 
    76  | 
    qed
  | 
| 
 | 
    77  | 
    show "|- {\<lambda>s. wp ?If Q s \<and> \<not> b s} c2 {Q}"
 | 
| 
 | 
    78  | 
    proof(rule strengthen_pre[OF _ Cond(2)])
  | 
| 
 | 
    79  | 
      show "\<forall>s. wp ?If Q s \<and> \<not> b s \<longrightarrow> wp c2 Q s" by auto
  | 
| 
 | 
    80  | 
    qed
  | 
| 
 | 
    81  | 
  qed
  | 
| 
 | 
    82  | 
next
  | 
| 
 | 
    83  | 
  case (While b c)
  | 
| 
 | 
    84  | 
  let ?w = "WHILE b DO c"
  | 
| 
 | 
    85  | 
  have "|- {wp ?w Q} ?w {\<lambda>s. wp ?w Q s \<and> \<not> b s}"
 | 
| 
 | 
    86  | 
  proof(rule hoare.While)
  | 
| 
 | 
    87  | 
    show "|- {\<lambda>s. wp ?w Q s \<and> b s} c {wp ?w Q}"
 | 
| 
 | 
    88  | 
    proof(rule strengthen_pre[OF _ While(1)])
  | 
| 
 | 
    89  | 
      show "\<forall>s. wp ?w Q s \<and> b s \<longrightarrow> wp c (wp ?w Q) s" by auto
  | 
| 
 | 
    90  | 
    qed
  | 
| 
 | 
    91  | 
  qed
  | 
| 
 | 
    92  | 
  thus ?case
  | 
| 
 | 
    93  | 
  proof(rule weaken_post)
  | 
| 
 | 
    94  | 
    show "\<forall>s. wp ?w Q s \<and> \<not> b s \<longrightarrow> Q s" by auto
  | 
| 
 | 
    95  | 
  qed
  | 
| 
 | 
    96  | 
qed
  | 
| 
 | 
    97  | 
  | 
| 
 | 
    98  | 
lemma hoare_relative_complete: assumes "|= {P}c{Q}" shows "|- {P}c{Q}"
 | 
| 
 | 
    99  | 
proof(rule strengthen_pre)
  | 
| 
 | 
   100  | 
  show "\<forall>s. P s \<longrightarrow> wp c Q s" using assms
  | 
| 
 | 
   101  | 
    by (auto simp: hoare_valid_def wp_def)
  | 
| 
 | 
   102  | 
  show "|- {wp c Q} c {Q}" by(rule wp_is_pre)
 | 
| 
 | 
   103  | 
qed
  | 
| 
 | 
   104  | 
  | 
| 
 | 
   105  | 
end
  |