src/HOL/Hoare/Hoare_Logic_Abort.thy
author wenzelm
Wed Feb 24 21:55:46 2010 +0100 (2010-02-24)
changeset 35352 fa051b504c3f
parent 35320 f80aee1ed475
child 35417 47ee18b6ae32
permissions -rw-r--r--
observe standard convention for syntax consts;
     1 (*  Title:      HOL/Hoare/HoareAbort.thy
     2     Author:     Leonor Prensa Nieto & Tobias Nipkow
     3     Copyright   2003 TUM
     4 
     5 Like Hoare.thy, but with an Abort statement for modelling run time errors.
     6 *)
     7 
     8 theory Hoare_Logic_Abort
     9 imports Main
    10 uses ("hoare_tac.ML")
    11 begin
    12 
    13 types
    14     'a bexp = "'a set"
    15     'a assn = "'a set"
    16 
    17 datatype
    18  'a com = Basic "'a \<Rightarrow> 'a"
    19    | Abort
    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 types 'a sem = "'a option => 'a option => bool"
    27 
    28 consts iter :: "nat => 'a bexp => 'a sem => 'a sem"
    29 primrec
    30 "iter 0 b S = (\<lambda>s s'. s \<notin> Some ` b \<and> s=s')"
    31 "iter (Suc n) b S =
    32   (\<lambda>s s'. s \<in> Some ` b \<and> (\<exists>s''. S s s'' \<and> iter n b S s'' s'))"
    33 
    34 consts Sem :: "'a com => 'a sem"
    35 primrec
    36 "Sem(Basic f) s s' = (case s of None \<Rightarrow> s' = None | Some t \<Rightarrow> s' = Some(f t))"
    37 "Sem Abort s s' = (s' = None)"
    38 "Sem(c1;c2) s s' = (\<exists>s''. Sem c1 s s'' \<and> Sem c2 s'' s')"
    39 "Sem(IF b THEN c1 ELSE c2 FI) s s' =
    40  (case s of None \<Rightarrow> s' = None
    41   | Some t \<Rightarrow> ((t \<in> b \<longrightarrow> Sem c1 s s') \<and> (t \<notin> b \<longrightarrow> Sem c2 s s')))"
    42 "Sem(While b x c) s s' =
    43  (if s = None then s' = None else \<exists>n. iter n b (Sem c) s s')"
    44 
    45 constdefs Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool"
    46   "Valid p c q == \<forall>s s'. Sem c s s' \<longrightarrow> s : Some ` p \<longrightarrow> s' : Some ` q"
    47 
    48 
    49 
    50 (** parse translations **)
    51 
    52 syntax
    53   "_assign"  :: "id => 'b => 'a com"        ("(2_ :=/ _)" [70,65] 61)
    54 
    55 syntax
    56   "_hoare_abort_vars" :: "[idts, 'a assn,'a com,'a assn] => bool"
    57                  ("VARS _// {_} // _ // {_}" [0,0,55,0] 50)
    58 syntax ("" output)
    59   "_hoare_abort"      :: "['a assn,'a com,'a assn] => bool"
    60                  ("{_} // _ // {_}" [0,55,0] 50)
    61 ML {*
    62 
    63 local
    64 fun free a = Free(a,dummyT)
    65 fun abs((a,T),body) =
    66   let val a = absfree(a, dummyT, body)
    67   in if T = Bound 0 then a else Const(Syntax.constrainAbsC,dummyT) $ a $ T end
    68 in
    69 
    70 fun mk_abstuple [x] body = abs (x, body)
    71   | mk_abstuple (x::xs) body =
    72       Syntax.const @{const_syntax split} $ abs (x, mk_abstuple xs body);
    73 
    74 fun mk_fbody a e [x as (b,_)] = if a=b then e else free b
    75   | mk_fbody a e ((b,_)::xs) =
    76       Syntax.const @{const_syntax Pair} $ (if a=b then e else free b) $ mk_fbody a e xs;
    77 
    78 fun mk_fexp a e xs = mk_abstuple xs (mk_fbody a e xs)
    79 end
    80 *}
    81 
    82 (* bexp_tr & assn_tr *)
    83 (*all meta-variables for bexp except for TRUE are translated as if they
    84   were boolean expressions*)
    85 ML{*
    86 fun bexp_tr (Const ("TRUE", _)) xs = Syntax.const "TRUE"   (* FIXME !? *)
    87   | bexp_tr b xs = Syntax.const @{const_syntax Collect} $ mk_abstuple xs b;
    88 
    89 fun assn_tr r xs = Syntax.const @{const_syntax Collect} $ mk_abstuple xs r;
    90 *}
    91 (* com_tr *)
    92 ML{*
    93 fun com_tr (Const (@{syntax_const "_assign"},_) $ Free (a,_) $ e) xs =
    94       Syntax.const @{const_syntax Basic} $ mk_fexp a e xs
    95   | com_tr (Const (@{const_syntax Basic},_) $ f) xs = Syntax.const @{const_syntax Basic} $ f
    96   | com_tr (Const (@{const_syntax Seq},_) $ c1 $ c2) xs =
    97       Syntax.const @{const_syntax Seq} $ com_tr c1 xs $ com_tr c2 xs
    98   | com_tr (Const (@{const_syntax Cond},_) $ b $ c1 $ c2) xs =
    99       Syntax.const @{const_syntax Cond} $ bexp_tr b xs $ com_tr c1 xs $ com_tr c2 xs
   100   | com_tr (Const (@{const_syntax While},_) $ b $ I $ c) xs =
   101       Syntax.const @{const_syntax While} $ bexp_tr b xs $ assn_tr I xs $ com_tr c xs
   102   | com_tr t _ = t (* if t is just a Free/Var *)
   103 *}
   104 
   105 (* triple_tr *)  (* FIXME does not handle "_idtdummy" *)
   106 ML{*
   107 local
   108 
   109 fun var_tr (Free (a, _)) = (a, Bound 0) (* Bound 0 = dummy term *)
   110   | var_tr (Const (@{syntax_const "_constrain"}, _) $ Free (a, _) $ T) = (a, T);
   111 
   112 fun vars_tr (Const (@{syntax_const "_idts"}, _) $ idt $ vars) = var_tr idt :: vars_tr vars
   113   | vars_tr t = [var_tr t]
   114 
   115 in
   116 fun hoare_vars_tr [vars, pre, prg, post] =
   117       let val xs = vars_tr vars
   118       in Syntax.const @{const_syntax Valid} $
   119          assn_tr pre xs $ com_tr prg xs $ assn_tr post xs
   120       end
   121   | hoare_vars_tr ts = raise TERM ("hoare_vars_tr", ts);
   122 end
   123 *}
   124 
   125 parse_translation {* [(@{syntax_const "_hoare_abort_vars"}, hoare_vars_tr)] *}
   126 
   127 
   128 (*****************************************************************************)
   129 
   130 (*** print translations ***)
   131 ML{*
   132 fun dest_abstuple (Const (@{const_syntax split},_) $ (Abs(v,_, body))) =
   133       subst_bound (Syntax.free v, dest_abstuple body)
   134   | dest_abstuple (Abs(v,_, body)) = subst_bound (Syntax.free v, body)
   135   | dest_abstuple trm = trm;
   136 
   137 fun abs2list (Const (@{const_syntax split},_) $ (Abs(x,T,t))) = Free (x, T)::abs2list t
   138   | abs2list (Abs(x,T,t)) = [Free (x, T)]
   139   | abs2list _ = [];
   140 
   141 fun mk_ts (Const (@{const_syntax split},_) $ (Abs(x,_,t))) = mk_ts t
   142   | mk_ts (Abs(x,_,t)) = mk_ts t
   143   | mk_ts (Const (@{const_syntax Pair},_) $ a $ b) = a::(mk_ts b)
   144   | mk_ts t = [t];
   145 
   146 fun mk_vts (Const (@{const_syntax split},_) $ (Abs(x,_,t))) =
   147            ((Syntax.free x)::(abs2list t), mk_ts t)
   148   | mk_vts (Abs(x,_,t)) = ([Syntax.free x], [t])
   149   | mk_vts t = raise Match;
   150 
   151 fun find_ch [] i xs = (false, (Syntax.free "not_ch", Syntax.free "not_ch"))
   152   | find_ch ((v,t)::vts) i xs =
   153       if t = Bound i then find_ch vts (i-1) xs
   154       else (true, (v, subst_bounds (xs,t)));
   155 
   156 fun is_f (Const (@{const_syntax split},_) $ (Abs(x,_,t))) = true
   157   | is_f (Abs(x,_,t)) = true
   158   | is_f t = false;
   159 *}
   160 
   161 (* assn_tr' & bexp_tr'*)
   162 ML{*
   163 fun assn_tr' (Const (@{const_syntax Collect},_) $ T) = dest_abstuple T
   164   | assn_tr' (Const (@{const_syntax inter},_) $ (Const (@{const_syntax Collect},_) $ T1) $
   165         (Const (@{const_syntax Collect},_) $ T2)) =
   166       Syntax.const @{const_syntax inter} $ dest_abstuple T1 $ dest_abstuple T2
   167   | assn_tr' t = t;
   168 
   169 fun bexp_tr' (Const (@{const_syntax Collect},_) $ T) = dest_abstuple T
   170   | bexp_tr' t = t;
   171 *}
   172 
   173 (*com_tr' *)
   174 ML{*
   175 fun mk_assign f =
   176   let val (vs, ts) = mk_vts f;
   177       val (ch, which) = find_ch (vs~~ts) ((length vs)-1) (rev vs)
   178   in
   179     if ch then Syntax.const @{syntax_const "_assign"} $ fst which $ snd which
   180     else Syntax.const @{const_syntax annskip}
   181   end;
   182 
   183 fun com_tr' (Const (@{const_syntax Basic},_) $ f) =
   184       if is_f f then mk_assign f else Syntax.const @{const_syntax Basic} $ f
   185   | com_tr' (Const (@{const_syntax Seq},_) $ c1 $ c2) =
   186       Syntax.const @{const_syntax Seq} $ com_tr' c1 $ com_tr' c2
   187   | com_tr' (Const (@{const_syntax Cond},_) $ b $ c1 $ c2) =
   188       Syntax.const @{const_syntax Cond} $ bexp_tr' b $ com_tr' c1 $ com_tr' c2
   189   | com_tr' (Const (@{const_syntax While},_) $ b $ I $ c) =
   190       Syntax.const @{const_syntax While} $ bexp_tr' b $ assn_tr' I $ com_tr' c
   191   | com_tr' t = t;
   192 
   193 fun spec_tr' [p, c, q] =
   194   Syntax.const @{syntax_const "_hoare_abort"} $ assn_tr' p $ com_tr' c $ assn_tr' q
   195 *}
   196 
   197 print_translation {* [(@{const_syntax Valid}, spec_tr')] *}
   198 
   199 (*** The proof rules ***)
   200 
   201 lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q"
   202 by (auto simp:Valid_def)
   203 
   204 lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q"
   205 by (auto simp:Valid_def)
   206 
   207 lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (c1;c2) R"
   208 by (auto simp:Valid_def)
   209 
   210 lemma CondRule:
   211  "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}
   212   \<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q"
   213 by (fastsimp simp:Valid_def image_def)
   214 
   215 lemma iter_aux:
   216  "! s s'. Sem c s s' \<longrightarrow> s \<in> Some ` (I \<inter> b) \<longrightarrow> s' \<in> Some ` I \<Longrightarrow>
   217   (\<And>s s'. s \<in> Some ` I \<Longrightarrow> iter n b (Sem c) s s' \<Longrightarrow> s' \<in> Some ` (I \<inter> -b))";
   218 apply(unfold image_def)
   219 apply(induct n)
   220  apply clarsimp
   221 apply(simp (no_asm_use))
   222 apply blast
   223 done
   224 
   225 lemma WhileRule:
   226  "p \<subseteq> i \<Longrightarrow> Valid (i \<inter> b) c i \<Longrightarrow> i \<inter> (-b) \<subseteq> q \<Longrightarrow> Valid p (While b i c) q"
   227 apply(simp add:Valid_def)
   228 apply(simp (no_asm) add:image_def)
   229 apply clarify
   230 apply(drule iter_aux)
   231   prefer 2 apply assumption
   232  apply blast
   233 apply blast
   234 done
   235 
   236 lemma AbortRule: "p \<subseteq> {s. False} \<Longrightarrow> Valid p Abort q"
   237 by(auto simp:Valid_def)
   238 
   239 
   240 subsection {* Derivation of the proof rules and, most importantly, the VCG tactic *}
   241 
   242 lemma Compl_Collect: "-(Collect b) = {x. ~(b x)}"
   243   by blast
   244 
   245 use "hoare_tac.ML"
   246 
   247 method_setup vcg = {*
   248   Scan.succeed (fn ctxt => SIMPLE_METHOD' (hoare_tac ctxt (K all_tac))) *}
   249   "verification condition generator"
   250 
   251 method_setup vcg_simp = {*
   252   Scan.succeed (fn ctxt =>
   253     SIMPLE_METHOD' (hoare_tac ctxt (asm_full_simp_tac (simpset_of ctxt)))) *}
   254   "verification condition generator plus simplification"
   255 
   256 (* Special syntax for guarded statements and guarded array updates: *)
   257 
   258 syntax
   259   "_guarded_com" :: "bool \<Rightarrow> 'a com \<Rightarrow> 'a com"  ("(2_ \<rightarrow>/ _)" 71)
   260   "_array_update" :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a com"  ("(2_[_] :=/ _)" [70, 65] 61)
   261 translations
   262   "P \<rightarrow> c" == "IF P THEN c ELSE CONST Abort FI"
   263   "a[i] := v" => "(i < CONST length a) \<rightarrow> (a := CONST list_update a i v)"
   264   (* reverse translation not possible because of duplicate "a" *)
   265 
   266 text{* Note: there is no special syntax for guarded array access. Thus
   267 you must write @{text"j < length a \<rightarrow> a[i] := a!j"}. *}
   268 
   269 end