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