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