src/HOL/Hoare/Hoare_Logic.thy
author wenzelm
Wed Aug 22 22:55:41 2012 +0200 (2012-08-22)
changeset 48891 c0eafbd55de3
parent 42174 d0be2722ce9f
child 51717 9e7d1c139569
permissions -rw-r--r--
prefer ML_file over old uses;
wenzelm@41959
     1
(*  Title:      HOL/Hoare/Hoare_Logic.thy
nipkow@5646
     2
    Author:     Leonor Prensa Nieto & Tobias Nipkow
nipkow@5646
     3
    Copyright   1998 TUM
nipkow@1335
     4
nipkow@1335
     5
Sugared semantic embedding of Hoare logic.
nipkow@5646
     6
Strictly speaking a shallow embedding (as implemented by Norbert Galm
nipkow@5646
     7
following Mike Gordon) would suffice. Maybe the datatype com comes in useful
nipkow@5646
     8
later.
nipkow@1335
     9
*)
nipkow@1335
    10
haftmann@35316
    11
theory Hoare_Logic
wenzelm@28457
    12
imports Main
wenzelm@24470
    13
begin
nipkow@1335
    14
wenzelm@42174
    15
type_synonym 'a bexp = "'a set"
wenzelm@42174
    16
type_synonym 'a assn = "'a set"
nipkow@5646
    17
nipkow@5646
    18
datatype
wenzelm@35113
    19
 'a com = Basic "'a \<Rightarrow> 'a"
nipkow@13682
    20
   | Seq "'a com" "'a com"               ("(_;/ _)"      [61,60] 60)
nipkow@13682
    21
   | Cond "'a bexp" "'a com" "'a com"    ("(1IF _/ THEN _ / ELSE _/ FI)"  [0,0,0] 61)
nipkow@13682
    22
   | While "'a bexp" "'a assn" "'a com"  ("(1WHILE _/ INV {_} //DO _ /OD)"  [0,0,0] 61)
wenzelm@35113
    23
wenzelm@35054
    24
abbreviation annskip ("SKIP") where "SKIP == Basic id"
nipkow@5646
    25
wenzelm@42174
    26
type_synonym 'a sem = "'a => 'a => bool"
nipkow@5646
    27
berghofe@36643
    28
inductive Sem :: "'a com \<Rightarrow> 'a sem"
berghofe@36643
    29
where
berghofe@36643
    30
  "Sem (Basic f) s (f s)"
berghofe@36643
    31
| "Sem c1 s s'' \<Longrightarrow> Sem c2 s'' s' \<Longrightarrow> Sem (c1;c2) s s'"
berghofe@36643
    32
| "s \<in> b \<Longrightarrow> Sem c1 s s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) s s'"
berghofe@36643
    33
| "s \<notin> b \<Longrightarrow> Sem c2 s s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) s s'"
berghofe@36643
    34
| "s \<notin> b \<Longrightarrow> Sem (While b x c) s s"
berghofe@36643
    35
| "s \<in> b \<Longrightarrow> Sem c s s'' \<Longrightarrow> Sem (While b x c) s'' s' \<Longrightarrow>
berghofe@36643
    36
   Sem (While b x c) s s'"
nipkow@5646
    37
berghofe@36643
    38
inductive_cases [elim!]:
berghofe@36643
    39
  "Sem (Basic f) s s'" "Sem (c1;c2) s s'"
berghofe@36643
    40
  "Sem (IF b THEN c1 ELSE c2 FI) s s'"
nipkow@5646
    41
wenzelm@38353
    42
definition Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool"
wenzelm@38353
    43
  where "Valid p c q \<longleftrightarrow> (!s s'. Sem c s s' --> s : p --> s' : q)"
wenzelm@5007
    44
wenzelm@5007
    45
wenzelm@35054
    46
syntax
wenzelm@42152
    47
  "_assign" :: "idt => 'b => 'a com"  ("(2_ :=/ _)" [70, 65] 61)
wenzelm@35054
    48
wenzelm@35054
    49
syntax
wenzelm@35054
    50
 "_hoare_vars" :: "[idts, 'a assn,'a com,'a assn] => bool"
wenzelm@35054
    51
                 ("VARS _// {_} // _ // {_}" [0,0,55,0] 50)
wenzelm@35054
    52
syntax ("" output)
wenzelm@35054
    53
 "_hoare"      :: "['a assn,'a com,'a assn] => bool"
wenzelm@35054
    54
                 ("{_} // _ // {_}" [0,55,0] 50)
nipkow@13764
    55
wenzelm@48891
    56
ML_file "hoare_syntax.ML"
wenzelm@42153
    57
parse_translation {* [(@{syntax_const "_hoare_vars"}, Hoare_Syntax.hoare_vars_tr)] *}
wenzelm@42153
    58
print_translation {* [(@{const_syntax Valid}, Hoare_Syntax.spec_tr' @{syntax_const "_hoare"})] *}
nipkow@1335
    59
nipkow@13682
    60
nipkow@13857
    61
lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q"
nipkow@13857
    62
by (auto simp:Valid_def)
nipkow@13857
    63
nipkow@13857
    64
lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q"
nipkow@13857
    65
by (auto simp:Valid_def)
nipkow@13857
    66
nipkow@13857
    67
lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (c1;c2) R"
nipkow@13857
    68
by (auto simp:Valid_def)
nipkow@13857
    69
nipkow@13857
    70
lemma CondRule:
nipkow@13857
    71
 "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}
nipkow@13857
    72
  \<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q"
nipkow@13857
    73
by (auto simp:Valid_def)
nipkow@13857
    74
berghofe@36643
    75
lemma While_aux:
berghofe@36643
    76
  assumes "Sem (WHILE b INV {i} DO c OD) s s'"
berghofe@36643
    77
  shows "\<forall>s s'. Sem c s s' \<longrightarrow> s \<in> I \<and> s \<in> b \<longrightarrow> s' \<in> I \<Longrightarrow>
berghofe@36643
    78
    s \<in> I \<Longrightarrow> s' \<in> I \<and> s' \<notin> b"
berghofe@36643
    79
  using assms
berghofe@36643
    80
  by (induct "WHILE b INV {i} DO c OD" s s') auto
nipkow@13857
    81
nipkow@13857
    82
lemma WhileRule:
nipkow@13857
    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"
nipkow@13857
    84
apply (clarsimp simp:Valid_def)
berghofe@36643
    85
apply(drule While_aux)
berghofe@36643
    86
  apply assumption
nipkow@13857
    87
 apply blast
nipkow@13857
    88
apply blast
nipkow@13857
    89
done
nipkow@13857
    90
nipkow@13857
    91
wenzelm@24470
    92
lemma Compl_Collect: "-(Collect b) = {x. ~(b x)}"
wenzelm@24470
    93
  by blast
wenzelm@24470
    94
wenzelm@28457
    95
lemmas AbortRule = SkipRule  -- "dummy version"
wenzelm@48891
    96
ML_file "hoare_tac.ML"
nipkow@13682
    97
nipkow@13682
    98
method_setup vcg = {*
wenzelm@30549
    99
  Scan.succeed (fn ctxt => SIMPLE_METHOD' (hoare_tac ctxt (K all_tac))) *}
nipkow@13682
   100
  "verification condition generator"
nipkow@13682
   101
nipkow@13682
   102
method_setup vcg_simp = {*
wenzelm@30549
   103
  Scan.succeed (fn ctxt =>
wenzelm@32149
   104
    SIMPLE_METHOD' (hoare_tac ctxt (asm_full_simp_tac (simpset_of ctxt)))) *}
nipkow@13682
   105
  "verification condition generator plus simplification"
nipkow@13682
   106
nipkow@13682
   107
end