src/HOL/Hoare/Hoare_Logic.thy
author haftmann
Sat, 20 Aug 2011 01:33:58 +0200
changeset 44324 d972b91ed09d
parent 42174 d0be2722ce9f
child 48891 c0eafbd55de3
permissions -rw-r--r--
compatibility layer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 38353
diff changeset
     1
(*  Title:      HOL/Hoare/Hoare_Logic.thy
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
     2
    Author:     Leonor Prensa Nieto & Tobias Nipkow
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
     3
    Copyright   1998 TUM
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
     4
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
     5
Sugared semantic embedding of Hoare logic.
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
     6
Strictly speaking a shallow embedding (as implemented by Norbert Galm
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
     7
following Mike Gordon) would suffice. Maybe the datatype com comes in useful
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
     8
later.
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
     9
*)
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    10
35316
870dfea4f9c0 dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents: 35113
diff changeset
    11
theory Hoare_Logic
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 24472
diff changeset
    12
imports Main
42153
fa108629d132 use shared copy of hoare_syntax.ML;
wenzelm
parents: 42152
diff changeset
    13
uses ("hoare_syntax.ML") ("hoare_tac.ML")
24470
41c81e23c08d removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents: 21588
diff changeset
    14
begin
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    15
42174
d0be2722ce9f modernized specifications;
wenzelm
parents: 42153
diff changeset
    16
type_synonym 'a bexp = "'a set"
d0be2722ce9f modernized specifications;
wenzelm
parents: 42153
diff changeset
    17
type_synonym 'a assn = "'a set"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    18
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    19
datatype
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 35054
diff changeset
    20
 'a com = Basic "'a \<Rightarrow> 'a"
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    21
   | Seq "'a com" "'a com"               ("(_;/ _)"      [61,60] 60)
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    22
   | Cond "'a bexp" "'a com" "'a com"    ("(1IF _/ THEN _ / ELSE _/ FI)"  [0,0,0] 61)
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    23
   | While "'a bexp" "'a assn" "'a com"  ("(1WHILE _/ INV {_} //DO _ /OD)"  [0,0,0] 61)
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 35054
diff changeset
    24
35054
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    25
abbreviation annskip ("SKIP") where "SKIP == Basic id"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    26
42174
d0be2722ce9f modernized specifications;
wenzelm
parents: 42153
diff changeset
    27
type_synonym 'a sem = "'a => 'a => bool"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    28
36643
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    29
inductive Sem :: "'a com \<Rightarrow> 'a sem"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    30
where
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    31
  "Sem (Basic f) s (f s)"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    32
| "Sem c1 s s'' \<Longrightarrow> Sem c2 s'' s' \<Longrightarrow> Sem (c1;c2) s s'"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    33
| "s \<in> b \<Longrightarrow> Sem c1 s s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) s s'"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    34
| "s \<notin> b \<Longrightarrow> Sem c2 s s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) s s'"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    35
| "s \<notin> b \<Longrightarrow> Sem (While b x c) s s"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    36
| "s \<in> b \<Longrightarrow> Sem c s s'' \<Longrightarrow> Sem (While b x c) s'' s' \<Longrightarrow>
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    37
   Sem (While b x c) s s'"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    38
36643
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    39
inductive_cases [elim!]:
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    40
  "Sem (Basic f) s s'" "Sem (c1;c2) s s'"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    41
  "Sem (IF b THEN c1 ELSE c2 FI) s s'"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    42
38353
d98baa2cf589 modernized specifications;
wenzelm
parents: 37591
diff changeset
    43
definition Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool"
d98baa2cf589 modernized specifications;
wenzelm
parents: 37591
diff changeset
    44
  where "Valid p c q \<longleftrightarrow> (!s s'. Sem c s s' --> s : p --> s' : q)"
5007
0ebd6c91088a nonterminals prog;
wenzelm
parents: 3842
diff changeset
    45
0ebd6c91088a nonterminals prog;
wenzelm
parents: 3842
diff changeset
    46
35054
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    47
syntax
42152
6c17259724b2 Hoare syntax: standard abstraction syntax admits source positions;
wenzelm
parents: 42054
diff changeset
    48
  "_assign" :: "idt => 'b => 'a com"  ("(2_ :=/ _)" [70, 65] 61)
35054
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    49
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    50
syntax
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    51
 "_hoare_vars" :: "[idts, 'a assn,'a com,'a assn] => bool"
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    52
                 ("VARS _// {_} // _ // {_}" [0,0,55,0] 50)
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    53
syntax ("" output)
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    54
 "_hoare"      :: "['a assn,'a com,'a assn] => bool"
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    55
                 ("{_} // _ // {_}" [0,55,0] 50)
13764
3e180bf68496 removed some problems with print translations
nipkow
parents: 13737
diff changeset
    56
42153
fa108629d132 use shared copy of hoare_syntax.ML;
wenzelm
parents: 42152
diff changeset
    57
use "hoare_syntax.ML"
fa108629d132 use shared copy of hoare_syntax.ML;
wenzelm
parents: 42152
diff changeset
    58
parse_translation {* [(@{syntax_const "_hoare_vars"}, Hoare_Syntax.hoare_vars_tr)] *}
fa108629d132 use shared copy of hoare_syntax.ML;
wenzelm
parents: 42152
diff changeset
    59
print_translation {* [(@{const_syntax Valid}, Hoare_Syntax.spec_tr' @{syntax_const "_hoare"})] *}
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    60
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    61
13857
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    62
lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q"
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    63
by (auto simp:Valid_def)
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    64
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    65
lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q"
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    66
by (auto simp:Valid_def)
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    67
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    68
lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (c1;c2) R"
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    69
by (auto simp:Valid_def)
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    70
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    71
lemma CondRule:
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    72
 "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    73
  \<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q"
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    74
by (auto simp:Valid_def)
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    75
36643
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    76
lemma While_aux:
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    77
  assumes "Sem (WHILE b INV {i} DO c OD) s s'"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    78
  shows "\<forall>s s'. Sem c s s' \<longrightarrow> s \<in> I \<and> s \<in> b \<longrightarrow> s' \<in> I \<Longrightarrow>
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    79
    s \<in> I \<Longrightarrow> s' \<in> I \<and> s' \<notin> b"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    80
  using assms
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    81
  by (induct "WHILE b INV {i} DO c OD" s s') auto
13857
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    82
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    83
lemma WhileRule:
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    84
 "p \<subseteq> i \<Longrightarrow> Valid (i \<inter> b) c i \<Longrightarrow> i \<inter> (-b) \<subseteq> q \<Longrightarrow> Valid p (While b i c) q"
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    85
apply (clarsimp simp:Valid_def)
36643
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    86
apply(drule While_aux)
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    87
  apply assumption
13857
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    88
 apply blast
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    89
apply blast
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    90
done
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    91
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    92
24470
41c81e23c08d removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents: 21588
diff changeset
    93
lemma Compl_Collect: "-(Collect b) = {x. ~(b x)}"
41c81e23c08d removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents: 21588
diff changeset
    94
  by blast
41c81e23c08d removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents: 21588
diff changeset
    95
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 24472
diff changeset
    96
lemmas AbortRule = SkipRule  -- "dummy version"
24472
943ef707396c added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents: 24470
diff changeset
    97
use "hoare_tac.ML"
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    98
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    99
method_setup vcg = {*
30549
d2d7874648bd simplified method setup;
wenzelm
parents: 30510
diff changeset
   100
  Scan.succeed (fn ctxt => SIMPLE_METHOD' (hoare_tac ctxt (K all_tac))) *}
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   101
  "verification condition generator"
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   102
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   103
method_setup vcg_simp = {*
30549
d2d7874648bd simplified method setup;
wenzelm
parents: 30510
diff changeset
   104
  Scan.succeed (fn ctxt =>
32149
ef59550a55d3 renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents: 32134
diff changeset
   105
    SIMPLE_METHOD' (hoare_tac ctxt (asm_full_simp_tac (simpset_of ctxt)))) *}
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   106
  "verification condition generator plus simplification"
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   107
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   108
end