src/HOL/IMP/Hoare.thy
author wenzelm
Wed, 19 Dec 2001 00:26:04 +0100
changeset 12546 0c90e581379f
parent 12434 ff2efde4574d
child 13630 a013a9dd370f
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1476
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1447
diff changeset
     1
(*  Title:      HOL/IMP/Hoare.thy
938
621be7ec81d7 *** empty log message ***
nipkow
parents: 937
diff changeset
     2
    ID:         $Id$
1476
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1447
diff changeset
     3
    Author:     Tobias Nipkow
936
a6d7b4084761 Hoare logic
nipkow
parents:
diff changeset
     4
    Copyright   1995 TUM
a6d7b4084761 Hoare logic
nipkow
parents:
diff changeset
     5
*)
a6d7b4084761 Hoare logic
nipkow
parents:
diff changeset
     6
12431
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
     7
header "Inductive Definition of Hoare Logic"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
     8
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
     9
theory Hoare = Denotation:
1447
bc2c0acbbf29 Added a verified verification-condition generator.
nipkow
parents: 1374
diff changeset
    10
12431
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    11
types assn = "state => bool"
1447
bc2c0acbbf29 Added a verified verification-condition generator.
nipkow
parents: 1374
diff changeset
    12
12431
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    13
constdefs hoare_valid :: "[assn,com,assn] => bool" ("|= {(1_)}/ (_)/ {(1_)}" 50)
1696
e84bff5c519b A completely new version of IMP.
nipkow
parents: 1486
diff changeset
    14
          "|= {P}c{Q} == !s t. (s,t) : C(c) --> P s --> Q t"
939
534955033ed2 Added pretty-printing coments
nipkow
parents: 938
diff changeset
    15
1696
e84bff5c519b A completely new version of IMP.
nipkow
parents: 1486
diff changeset
    16
consts hoare :: "(assn * com * assn) set"
12546
wenzelm
parents: 12434
diff changeset
    17
syntax "_hoare" :: "[bool,com,bool] => bool" ("|- ({(1_)}/ (_)/ {(1_)})" 50)
1486
7b95d7b49f7a Introduced qed_spec_mp.
nipkow
parents: 1481
diff changeset
    18
translations "|- {P}c{Q}" == "(P,c,Q) : hoare"
939
534955033ed2 Added pretty-printing coments
nipkow
parents: 938
diff changeset
    19
1789
aade046ec6d5 Quotes now optional around inductive set
paulson
parents: 1696
diff changeset
    20
inductive hoare
12431
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    21
intros
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    22
  skip: "|- {P}\<SKIP>{P}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    23
  ass:  "|- {%s. P(s[x\<mapsto>a s])} x:==a {P}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    24
  semi: "[| |- {P}c{Q}; |- {Q}d{R} |] ==> |- {P} c;d {R}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    25
  If: "[| |- {%s. P s & b s}c{Q}; |- {%s. P s & ~b s}d{Q} |] ==>
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    26
      |- {P} \<IF> b \<THEN> c \<ELSE> d {Q}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    27
  While: "|- {%s. P s & b s} c {P} ==>
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    28
         |- {P} \<WHILE> b \<DO> c {%s. P s & ~b s}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    29
  conseq: "[| !s. P' s --> P s; |- {P}c{Q}; !s. Q s --> Q' s |] ==>
1486
7b95d7b49f7a Introduced qed_spec_mp.
nipkow
parents: 1481
diff changeset
    30
          |- {P'}c{Q'}"
1481
03f096efa26d Modified datatype com.
nipkow
parents: 1476
diff changeset
    31
12431
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    32
constdefs wp :: "com => assn => assn"
2810
c4e16b36bc57 Added wp_while.
nipkow
parents: 1789
diff changeset
    33
          "wp c Q == (%s. !t. (s,t) : C(c) --> Q t)"
939
534955033ed2 Added pretty-printing coments
nipkow
parents: 938
diff changeset
    34
12431
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    35
(*  
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    36
Soundness (and part of) relative completeness of Hoare rules
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    37
wrt denotational semantics
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    38
*)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    39
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    40
lemma hoare_conseq1: "[| !s. P' s --> P s; |- {P}c{Q} |] ==> |- {P'}c{Q}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    41
apply (erule hoare.conseq)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    42
apply  assumption
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    43
apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    44
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    45
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    46
lemma hoare_conseq2: "[| |- {P}c{Q}; !s. Q s --> Q' s |] ==> |- {P}c{Q'}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    47
apply (rule hoare.conseq)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    48
prefer 2 apply    (assumption)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    49
apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    50
apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    51
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    52
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    53
lemma hoare_sound: "|- {P}c{Q} ==> |= {P}c{Q}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    54
apply (unfold hoare_valid_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    55
apply (erule hoare.induct)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    56
     apply (simp_all (no_asm_simp))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    57
  apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    58
 apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    59
apply (rule allI, rule allI, rule impI)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    60
apply (erule lfp_induct2)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    61
 apply (rule Gamma_mono)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    62
apply (unfold Gamma_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    63
apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    64
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    65
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    66
lemma wp_SKIP: "wp \<SKIP> Q = Q"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    67
apply (unfold wp_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    68
apply (simp (no_asm))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    69
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    70
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    71
lemma wp_Ass: "wp (x:==a) Q = (%s. Q(s[x\<mapsto>a s]))"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    72
apply (unfold wp_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    73
apply (simp (no_asm))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    74
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    75
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    76
lemma wp_Semi: "wp (c;d) Q = wp c (wp d Q)"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    77
apply (unfold wp_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    78
apply (simp (no_asm))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    79
apply (rule ext)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    80
apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    81
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    82
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    83
lemma wp_If: 
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    84
 "wp (\<IF> b \<THEN> c \<ELSE> d) Q = (%s. (b s --> wp c Q s) &  (~b s --> wp d Q s))"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    85
apply (unfold wp_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    86
apply (simp (no_asm))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    87
apply (rule ext)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    88
apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    89
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    90
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    91
lemma wp_While_True: 
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    92
  "b s ==> wp (\<WHILE> b \<DO> c) Q s = wp (c;\<WHILE> b \<DO> c) Q s"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    93
apply (unfold wp_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    94
apply (subst C_While_If)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    95
apply (simp (no_asm_simp))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    96
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    97
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    98
lemma wp_While_False: "~b s ==> wp (\<WHILE> b \<DO> c) Q s = Q s"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
    99
apply (unfold wp_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   100
apply (subst C_While_If)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   101
apply (simp (no_asm_simp))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   102
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   103
12434
kleing
parents: 12431
diff changeset
   104
lemmas [simp] = wp_SKIP wp_Ass wp_Semi wp_If wp_While_True wp_While_False
12431
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   105
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   106
(*Not suitable for rewriting: LOOPS!*)
12434
kleing
parents: 12431
diff changeset
   107
lemma wp_While_if: 
kleing
parents: 12431
diff changeset
   108
  "wp (\<WHILE> b \<DO> c) Q s = (if b s then wp (c;\<WHILE> b \<DO> c) Q s else Q s)"
12431
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   109
apply (simp (no_asm))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   110
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   111
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   112
lemma wp_While: "wp (\<WHILE> b \<DO> c) Q s =  
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   113
   (s : gfp(%S.{s. if b s then wp c (%s. s:S) s else Q s}))"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   114
apply (simp (no_asm))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   115
apply (rule iffI)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   116
 apply (rule weak_coinduct)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   117
  apply (erule CollectI)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   118
 apply safe
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   119
  apply (rotate_tac -1)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   120
  apply simp
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   121
 apply (rotate_tac -1)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   122
 apply simp
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   123
apply (simp add: wp_def Gamma_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   124
apply (intro strip)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   125
apply (rule mp)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   126
 prefer 2 apply (assumption)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   127
apply (erule lfp_induct2)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   128
apply (fast intro!: monoI)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   129
apply (subst gfp_unfold)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   130
 apply (fast intro!: monoI)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   131
apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   132
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   133
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   134
declare C_while [simp del]
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   135
12434
kleing
parents: 12431
diff changeset
   136
lemmas [intro!] = hoare.skip hoare.ass hoare.semi hoare.If 
12431
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   137
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   138
lemma wp_is_pre [rule_format (no_asm)]: "!Q. |- {wp c Q} c {Q}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   139
apply (induct_tac "c")
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   140
    apply (simp_all (no_asm))
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   141
    apply fast+
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   142
 apply (blast intro: hoare_conseq1)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   143
apply safe
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   144
apply (rule hoare_conseq2)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   145
 apply (rule hoare.While)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   146
 apply (rule hoare_conseq1)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   147
  prefer 2 apply (fast)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   148
  apply safe
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   149
 apply (rotate_tac -1, simp)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   150
apply (rotate_tac -1, simp)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   151
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   152
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   153
lemma hoare_relative_complete: "|= {P}c{Q} ==> |- {P}c{Q}"
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   154
apply (rule hoare_conseq1 [OF _ wp_is_pre])
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   155
apply (unfold hoare_valid_def wp_def)
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   156
apply fast
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   157
done
07ec657249e5 converted to Isar
kleing
parents: 9241
diff changeset
   158
939
534955033ed2 Added pretty-printing coments
nipkow
parents: 938
diff changeset
   159
end