src/HOL/Hoare/Hoare_Logic.thy
author haftmann
Fri, 15 Feb 2013 08:31:31 +0100
changeset 51143 0a2371e7ced3
parent 48891 c0eafbd55de3
child 51717 9e7d1c139569
permissions -rw-r--r--
two target language numeral types: integer and natural, as replacement for code_numeral; former theory HOL/Library/Code_Numeral_Types replaces HOL/Code_Numeral; refined stack of theories implementing int and/or nat by target language numerals; reduced number of target language numeral types to exactly one
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
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
    13
begin
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    14
42174
d0be2722ce9f modernized specifications;
wenzelm
parents: 42153
diff changeset
    15
type_synonym 'a bexp = "'a set"
d0be2722ce9f modernized specifications;
wenzelm
parents: 42153
diff changeset
    16
type_synonym 'a assn = "'a set"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    17
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    18
datatype
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 35054
diff changeset
    19
 'a com = Basic "'a \<Rightarrow> 'a"
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    20
   | Seq "'a com" "'a com"               ("(_;/ _)"      [61,60] 60)
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    21
   | Cond "'a bexp" "'a com" "'a com"    ("(1IF _/ THEN _ / ELSE _/ FI)"  [0,0,0] 61)
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    22
   | While "'a bexp" "'a assn" "'a com"  ("(1WHILE _/ INV {_} //DO _ /OD)"  [0,0,0] 61)
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 35054
diff changeset
    23
35054
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    24
abbreviation annskip ("SKIP") where "SKIP == Basic id"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    25
42174
d0be2722ce9f modernized specifications;
wenzelm
parents: 42153
diff changeset
    26
type_synonym 'a sem = "'a => 'a => bool"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    27
36643
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    28
inductive Sem :: "'a com \<Rightarrow> 'a sem"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    29
where
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    30
  "Sem (Basic f) s (f s)"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    31
| "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
    32
| "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
    33
| "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
    34
| "s \<notin> b \<Longrightarrow> Sem (While b x c) s s"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    35
| "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
    36
   Sem (While b x c) s s'"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    37
36643
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    38
inductive_cases [elim!]:
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    39
  "Sem (Basic f) s s'" "Sem (c1;c2) s s'"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    40
  "Sem (IF b THEN c1 ELSE c2 FI) s s'"
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5565
diff changeset
    41
38353
d98baa2cf589 modernized specifications;
wenzelm
parents: 37591
diff changeset
    42
definition Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool"
d98baa2cf589 modernized specifications;
wenzelm
parents: 37591
diff changeset
    43
  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
    44
0ebd6c91088a nonterminals prog;
wenzelm
parents: 3842
diff changeset
    45
35054
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    46
syntax
42152
6c17259724b2 Hoare syntax: standard abstraction syntax admits source positions;
wenzelm
parents: 42054
diff changeset
    47
  "_assign" :: "idt => 'b => 'a com"  ("(2_ :=/ _)" [70, 65] 61)
35054
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    48
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    49
syntax
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    50
 "_hoare_vars" :: "[idts, 'a assn,'a com,'a assn] => bool"
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    51
                 ("VARS _// {_} // _ // {_}" [0,0,55,0] 50)
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    52
syntax ("" output)
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    53
 "_hoare"      :: "['a assn,'a com,'a assn] => bool"
a5db9779b026 modernized some syntax translations;
wenzelm
parents: 32149
diff changeset
    54
                 ("{_} // _ // {_}" [0,55,0] 50)
13764
3e180bf68496 removed some problems with print translations
nipkow
parents: 13737
diff changeset
    55
48891
c0eafbd55de3 prefer ML_file over old uses;
wenzelm
parents: 42174
diff changeset
    56
ML_file "hoare_syntax.ML"
42153
fa108629d132 use shared copy of hoare_syntax.ML;
wenzelm
parents: 42152
diff changeset
    57
parse_translation {* [(@{syntax_const "_hoare_vars"}, Hoare_Syntax.hoare_vars_tr)] *}
fa108629d132 use shared copy of hoare_syntax.ML;
wenzelm
parents: 42152
diff changeset
    58
print_translation {* [(@{const_syntax Valid}, Hoare_Syntax.spec_tr' @{syntax_const "_hoare"})] *}
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    59
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    60
13857
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    61
lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q"
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    62
by (auto simp:Valid_def)
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    63
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    64
lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q"
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    65
by (auto simp:Valid_def)
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    66
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    67
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
    68
by (auto simp:Valid_def)
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    69
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    70
lemma CondRule:
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    71
 "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
    72
  \<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
    73
by (auto simp:Valid_def)
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    74
36643
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    75
lemma While_aux:
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    76
  assumes "Sem (WHILE b INV {i} DO c OD) s s'"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    77
  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
    78
    s \<in> I \<Longrightarrow> s' \<in> I \<and> s' \<notin> b"
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    79
  using assms
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    80
  by (induct "WHILE b INV {i} DO c OD" s s') auto
13857
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    81
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    82
lemma WhileRule:
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    83
 "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
    84
apply (clarsimp simp:Valid_def)
36643
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    85
apply(drule While_aux)
f36588af1ba1 Turned Sem into an inductive definition.
berghofe
parents: 35416
diff changeset
    86
  apply assumption
13857
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    87
 apply blast
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    88
apply blast
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    89
done
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    90
11d7c5a8dbb7 *** empty log message ***
nipkow
parents: 13764
diff changeset
    91
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
    92
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
    93
  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
    94
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
    95
lemmas AbortRule = SkipRule  -- "dummy version"
48891
c0eafbd55de3 prefer ML_file over old uses;
wenzelm
parents: 42174
diff changeset
    96
ML_file "hoare_tac.ML"
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    97
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
    98
method_setup vcg = {*
30549
d2d7874648bd simplified method setup;
wenzelm
parents: 30510
diff changeset
    99
  Scan.succeed (fn ctxt => SIMPLE_METHOD' (hoare_tac ctxt (K all_tac))) *}
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   100
  "verification condition generator"
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   101
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   102
method_setup vcg_simp = {*
30549
d2d7874648bd simplified method setup;
wenzelm
parents: 30510
diff changeset
   103
  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
   104
    SIMPLE_METHOD' (hoare_tac ctxt (asm_full_simp_tac (simpset_of ctxt)))) *}
13682
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   105
  "verification condition generator plus simplification"
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   106
91674c8a008b conversion ML -> thy
nipkow
parents: 11606
diff changeset
   107
end