src/HOL/IMP/Hoare.thy
author wenzelm
Fri Mar 28 19:43:54 2008 +0100 (2008-03-28)
changeset 26462 dac4e2bce00d
parent 23746 a455e69c31cc
child 27362 a6dc1769fdda
permissions -rw-r--r--
avoid rebinding of existing facts;
clasohm@1476
     1
(*  Title:      HOL/IMP/Hoare.thy
nipkow@938
     2
    ID:         $Id$
clasohm@1476
     3
    Author:     Tobias Nipkow
nipkow@936
     4
    Copyright   1995 TUM
nipkow@936
     5
*)
nipkow@936
     6
kleing@12431
     7
header "Inductive Definition of Hoare Logic"
kleing@12431
     8
haftmann@16417
     9
theory Hoare imports Denotation begin
nipkow@1447
    10
kleing@12431
    11
types assn = "state => bool"
nipkow@1447
    12
kleing@12431
    13
constdefs hoare_valid :: "[assn,com,assn] => bool" ("|= {(1_)}/ (_)/ {(1_)}" 50)
nipkow@1696
    14
          "|= {P}c{Q} == !s t. (s,t) : C(c) --> P s --> Q t"
nipkow@939
    15
berghofe@23746
    16
inductive
berghofe@23746
    17
  hoare :: "assn => com => assn => bool" ("|- ({(1_)}/ (_)/ {(1_)})" 50)
berghofe@23746
    18
where
kleing@12431
    19
  skip: "|- {P}\<SKIP>{P}"
berghofe@23746
    20
| ass:  "|- {%s. P(s[x\<mapsto>a s])} x:==a {P}"
berghofe@23746
    21
| semi: "[| |- {P}c{Q}; |- {Q}d{R} |] ==> |- {P} c;d {R}"
berghofe@23746
    22
| If: "[| |- {%s. P s & b s}c{Q}; |- {%s. P s & ~b s}d{Q} |] ==>
kleing@12431
    23
      |- {P} \<IF> b \<THEN> c \<ELSE> d {Q}"
berghofe@23746
    24
| While: "|- {%s. P s & b s} c {P} ==>
kleing@12431
    25
         |- {P} \<WHILE> b \<DO> c {%s. P s & ~b s}"
berghofe@23746
    26
| conseq: "[| !s. P' s --> P s; |- {P}c{Q}; !s. Q s --> Q' s |] ==>
nipkow@1486
    27
          |- {P'}c{Q'}"
nipkow@1481
    28
kleing@12431
    29
constdefs wp :: "com => assn => assn"
nipkow@2810
    30
          "wp c Q == (%s. !t. (s,t) : C(c) --> Q t)"
nipkow@939
    31
wenzelm@18372
    32
(*
kleing@12431
    33
Soundness (and part of) relative completeness of Hoare rules
kleing@12431
    34
wrt denotational semantics
kleing@12431
    35
*)
kleing@12431
    36
kleing@12431
    37
lemma hoare_conseq1: "[| !s. P' s --> P s; |- {P}c{Q} |] ==> |- {P'}c{Q}"
kleing@12431
    38
apply (erule hoare.conseq)
kleing@12431
    39
apply  assumption
kleing@12431
    40
apply fast
kleing@12431
    41
done
kleing@12431
    42
kleing@12431
    43
lemma hoare_conseq2: "[| |- {P}c{Q}; !s. Q s --> Q' s |] ==> |- {P}c{Q'}"
kleing@12431
    44
apply (rule hoare.conseq)
kleing@12431
    45
prefer 2 apply    (assumption)
kleing@12431
    46
apply fast
kleing@12431
    47
apply fast
kleing@12431
    48
done
kleing@12431
    49
kleing@12431
    50
lemma hoare_sound: "|- {P}c{Q} ==> |= {P}c{Q}"
kleing@12431
    51
apply (unfold hoare_valid_def)
wenzelm@18372
    52
apply (induct set: hoare)
kleing@12431
    53
     apply (simp_all (no_asm_simp))
kleing@12431
    54
  apply fast
kleing@12431
    55
 apply fast
kleing@12431
    56
apply (rule allI, rule allI, rule impI)
kleing@12431
    57
apply (erule lfp_induct2)
kleing@12431
    58
 apply (rule Gamma_mono)
kleing@12431
    59
apply (unfold Gamma_def)
kleing@12431
    60
apply fast
kleing@12431
    61
done
kleing@12431
    62
kleing@12431
    63
lemma wp_SKIP: "wp \<SKIP> Q = Q"
kleing@12431
    64
apply (unfold wp_def)
kleing@12431
    65
apply (simp (no_asm))
kleing@12431
    66
done
kleing@12431
    67
kleing@12431
    68
lemma wp_Ass: "wp (x:==a) Q = (%s. Q(s[x\<mapsto>a s]))"
kleing@12431
    69
apply (unfold wp_def)
kleing@12431
    70
apply (simp (no_asm))
kleing@12431
    71
done
kleing@12431
    72
kleing@12431
    73
lemma wp_Semi: "wp (c;d) Q = wp c (wp d Q)"
kleing@12431
    74
apply (unfold wp_def)
kleing@12431
    75
apply (simp (no_asm))
kleing@12431
    76
apply (rule ext)
kleing@12431
    77
apply fast
kleing@12431
    78
done
kleing@12431
    79
wenzelm@18372
    80
lemma wp_If:
kleing@12431
    81
 "wp (\<IF> b \<THEN> c \<ELSE> d) Q = (%s. (b s --> wp c Q s) &  (~b s --> wp d Q s))"
kleing@12431
    82
apply (unfold wp_def)
kleing@12431
    83
apply (simp (no_asm))
kleing@12431
    84
apply (rule ext)
kleing@12431
    85
apply fast
kleing@12431
    86
done
kleing@12431
    87
wenzelm@18372
    88
lemma wp_While_True:
kleing@12431
    89
  "b s ==> wp (\<WHILE> b \<DO> c) Q s = wp (c;\<WHILE> b \<DO> c) Q s"
kleing@12431
    90
apply (unfold wp_def)
kleing@12431
    91
apply (subst C_While_If)
kleing@12431
    92
apply (simp (no_asm_simp))
kleing@12431
    93
done
kleing@12431
    94
kleing@12431
    95
lemma wp_While_False: "~b s ==> wp (\<WHILE> b \<DO> c) Q s = Q s"
kleing@12431
    96
apply (unfold wp_def)
kleing@12431
    97
apply (subst C_While_If)
kleing@12431
    98
apply (simp (no_asm_simp))
kleing@12431
    99
done
kleing@12431
   100
kleing@12434
   101
lemmas [simp] = wp_SKIP wp_Ass wp_Semi wp_If wp_While_True wp_While_False
kleing@12431
   102
kleing@12431
   103
(*Not suitable for rewriting: LOOPS!*)
wenzelm@18372
   104
lemma wp_While_if:
kleing@12434
   105
  "wp (\<WHILE> b \<DO> c) Q s = (if b s then wp (c;\<WHILE> b \<DO> c) Q s else Q s)"
wenzelm@18372
   106
  by simp
kleing@12431
   107
wenzelm@18372
   108
lemma wp_While: "wp (\<WHILE> b \<DO> c) Q s =
kleing@12431
   109
   (s : gfp(%S.{s. if b s then wp c (%s. s:S) s else Q s}))"
kleing@12431
   110
apply (simp (no_asm))
kleing@12431
   111
apply (rule iffI)
kleing@12431
   112
 apply (rule weak_coinduct)
kleing@12431
   113
  apply (erule CollectI)
kleing@12431
   114
 apply safe
kleing@12431
   115
  apply simp
kleing@12431
   116
 apply simp
kleing@12431
   117
apply (simp add: wp_def Gamma_def)
kleing@12431
   118
apply (intro strip)
kleing@12431
   119
apply (rule mp)
kleing@12431
   120
 prefer 2 apply (assumption)
kleing@12431
   121
apply (erule lfp_induct2)
kleing@12431
   122
apply (fast intro!: monoI)
kleing@12431
   123
apply (subst gfp_unfold)
kleing@12431
   124
 apply (fast intro!: monoI)
kleing@12431
   125
apply fast
kleing@12431
   126
done
kleing@12431
   127
kleing@12431
   128
declare C_while [simp del]
kleing@12431
   129
wenzelm@18372
   130
lemmas [intro!] = hoare.skip hoare.ass hoare.semi hoare.If
kleing@12431
   131
wenzelm@18372
   132
lemma wp_is_pre: "|- {wp c Q} c {Q}"
wenzelm@20503
   133
apply (induct c arbitrary: Q)
kleing@12431
   134
    apply (simp_all (no_asm))
kleing@12431
   135
    apply fast+
kleing@12431
   136
 apply (blast intro: hoare_conseq1)
kleing@12431
   137
apply (rule hoare_conseq2)
kleing@12431
   138
 apply (rule hoare.While)
kleing@12431
   139
 apply (rule hoare_conseq1)
wenzelm@18372
   140
  prefer 2 apply fast
kleing@12431
   141
  apply safe
nipkow@13630
   142
 apply simp
nipkow@13630
   143
apply simp
kleing@12431
   144
done
kleing@12431
   145
kleing@12431
   146
lemma hoare_relative_complete: "|= {P}c{Q} ==> |- {P}c{Q}"
kleing@12431
   147
apply (rule hoare_conseq1 [OF _ wp_is_pre])
kleing@12431
   148
apply (unfold hoare_valid_def wp_def)
kleing@12431
   149
apply fast
kleing@12431
   150
done
kleing@12431
   151
nipkow@939
   152
end