src/HOL/Hoare/Hoare_Logic.thy
author haftmann
Mon Jun 05 15:59:41 2017 +0200 (2017-06-05)
changeset 66010 2f7d39285a1a
parent 62042 6c6ccf573479
child 67443 3abf6a722518
permissions -rw-r--r--
executable domain membership checks
     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 begin
    14 
    15 type_synonym 'a bexp = "'a set"
    16 type_synonym 'a assn = "'a set"
    17 
    18 datatype 'a com =
    19   Basic "'a \<Rightarrow> 'a"
    20 | Seq "'a com" "'a com"               ("(_;/ _)"      [61,60] 60)
    21 | Cond "'a bexp" "'a com" "'a com"    ("(1IF _/ THEN _ / ELSE _/ FI)"  [0,0,0] 61)
    22 | While "'a bexp" "'a assn" "'a com"  ("(1WHILE _/ INV {_} //DO _ /OD)"  [0,0,0] 61)
    23 
    24 abbreviation annskip ("SKIP") where "SKIP == Basic id"
    25 
    26 type_synonym 'a sem = "'a => 'a => bool"
    27 
    28 inductive Sem :: "'a com \<Rightarrow> 'a sem"
    29 where
    30   "Sem (Basic f) s (f s)"
    31 | "Sem c1 s s'' \<Longrightarrow> Sem c2 s'' s' \<Longrightarrow> Sem (c1;c2) s s'"
    32 | "s \<in> b \<Longrightarrow> Sem c1 s s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) s s'"
    33 | "s \<notin> b \<Longrightarrow> Sem c2 s s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) s s'"
    34 | "s \<notin> b \<Longrightarrow> Sem (While b x c) s s"
    35 | "s \<in> b \<Longrightarrow> Sem c s s'' \<Longrightarrow> Sem (While b x c) s'' s' \<Longrightarrow>
    36    Sem (While b x c) s s'"
    37 
    38 inductive_cases [elim!]:
    39   "Sem (Basic f) s s'" "Sem (c1;c2) s s'"
    40   "Sem (IF b THEN c1 ELSE c2 FI) s s'"
    41 
    42 definition Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool"
    43   where "Valid p c q \<longleftrightarrow> (!s s'. Sem c s s' --> s : p --> s' : q)"
    44 
    45 
    46 syntax
    47   "_assign" :: "idt => 'b => 'a com"  ("(2_ :=/ _)" [70, 65] 61)
    48 
    49 syntax
    50  "_hoare_vars" :: "[idts, 'a assn,'a com,'a assn] => bool"
    51                  ("VARS _// {_} // _ // {_}" [0,0,55,0] 50)
    52 syntax ("" output)
    53  "_hoare"      :: "['a assn,'a com,'a assn] => bool"
    54                  ("{_} // _ // {_}" [0,55,0] 50)
    55 
    56 ML_file "hoare_syntax.ML"
    57 parse_translation \<open>[(@{syntax_const "_hoare_vars"}, K Hoare_Syntax.hoare_vars_tr)]\<close>
    58 print_translation \<open>[(@{const_syntax Valid}, K (Hoare_Syntax.spec_tr' @{syntax_const "_hoare"}))]\<close>
    59 
    60 
    61 lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q"
    62 by (auto simp:Valid_def)
    63 
    64 lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q"
    65 by (auto simp:Valid_def)
    66 
    67 lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (c1;c2) R"
    68 by (auto simp:Valid_def)
    69 
    70 lemma CondRule:
    71  "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}
    72   \<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q"
    73 by (auto simp:Valid_def)
    74 
    75 lemma While_aux:
    76   assumes "Sem (WHILE b INV {i} DO c OD) s s'"
    77   shows "\<forall>s s'. Sem c s s' \<longrightarrow> s \<in> I \<and> s \<in> b \<longrightarrow> s' \<in> I \<Longrightarrow>
    78     s \<in> I \<Longrightarrow> s' \<in> I \<and> s' \<notin> b"
    79   using assms
    80   by (induct "WHILE b INV {i} DO c OD" s s') auto
    81 
    82 lemma WhileRule:
    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"
    84 apply (clarsimp simp:Valid_def)
    85 apply(drule While_aux)
    86   apply assumption
    87  apply blast
    88 apply blast
    89 done
    90 
    91 
    92 lemma Compl_Collect: "-(Collect b) = {x. ~(b x)}"
    93   by blast
    94 
    95 lemmas AbortRule = SkipRule  \<comment> "dummy version"
    96 ML_file "hoare_tac.ML"
    97 
    98 method_setup vcg = \<open>
    99   Scan.succeed (fn ctxt => SIMPLE_METHOD' (Hoare.hoare_tac ctxt (K all_tac)))\<close>
   100   "verification condition generator"
   101 
   102 method_setup vcg_simp = \<open>
   103   Scan.succeed (fn ctxt =>
   104     SIMPLE_METHOD' (Hoare.hoare_tac ctxt (asm_full_simp_tac ctxt)))\<close>
   105   "verification condition generator plus simplification"
   106 
   107 end