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