src/HOL/Hoare/hoare_tac.ML
author wenzelm
Mon Jun 23 23:45:39 2008 +0200 (2008-06-23)
changeset 27330 1af2598b5f7d
parent 27244 af0a44372d1f
child 28457 25669513fd4c
permissions -rw-r--r--
Logic.all/mk_equals/mk_implies;
     1 (*  Title:      HOL/Hoare/hoare_tac.ML
     2     ID:         $Id$
     3     Author:     Leonor Prensa Nieto & Tobias Nipkow
     4     Copyright   1998 TUM
     5 
     6 Derivation of the proof rules and, most importantly, the VCG tactic.
     7 *)
     8 
     9 (*** The tactics ***)
    10 
    11 (*****************************************************************************)
    12 (** The function Mset makes the theorem                                     **)
    13 (** "?Mset <= {(x1,...,xn). ?P (x1,...,xn)} ==> ?Mset <= {s. ?P s}",        **)
    14 (** where (x1,...,xn) are the variables of the particular program we are    **)
    15 (** working on at the moment of the call                                    **)
    16 (*****************************************************************************)
    17 
    18 local open HOLogic in
    19 
    20 (** maps (%x1 ... xn. t) to [x1,...,xn] **)
    21 fun abs2list (Const ("split",_) $ (Abs(x,T,t))) = Free (x, T)::abs2list t
    22   | abs2list (Abs(x,T,t)) = [Free (x, T)]
    23   | abs2list _ = [];
    24 
    25 (** maps {(x1,...,xn). t} to [x1,...,xn] **)
    26 fun mk_vars (Const ("Collect",_) $ T) = abs2list T
    27   | mk_vars _ = [];
    28 
    29 (** abstraction of body over a tuple formed from a list of free variables. 
    30 Types are also built **)
    31 fun mk_abstupleC []     body = absfree ("x", unitT, body)
    32   | mk_abstupleC (v::w) body = let val (n,T) = dest_Free v
    33                                in if w=[] then absfree (n, T, body)
    34         else let val z  = mk_abstupleC w body;
    35                  val T2 = case z of Abs(_,T,_) => T
    36                         | Const (_, Type (_,[_, Type (_,[T,_])])) $ _ => T;
    37        in Const ("split", (T --> T2 --> boolT) --> mk_prodT (T,T2) --> boolT) 
    38           $ absfree (n, T, z) end end;
    39 
    40 (** maps [x1,...,xn] to (x1,...,xn) and types**)
    41 fun mk_bodyC []      = HOLogic.unit
    42   | mk_bodyC (x::xs) = if xs=[] then x 
    43                else let val (n, T) = dest_Free x ;
    44                         val z = mk_bodyC xs;
    45                         val T2 = case z of Free(_, T) => T
    46                                          | Const ("Pair", Type ("fun", [_, Type
    47                                             ("fun", [_, T])])) $ _ $ _ => T;
    48                  in Const ("Pair", [T, T2] ---> mk_prodT (T, T2)) $ x $ z end;
    49 
    50 (** maps a goal of the form:
    51         1. [| P |] ==> VARS x1 ... xn {._.} _ {._.} or to [x1,...,xn]**) 
    52 fun get_vars thm = let  val c = Logic.unprotect (concl_of (thm));
    53                         val d = Logic.strip_assums_concl c;
    54                         val Const _ $ pre $ _ $ _ = dest_Trueprop d;
    55       in mk_vars pre end;
    56 
    57 
    58 (** Makes Collect with type **)
    59 fun mk_CollectC trm = let val T as Type ("fun",[t,_]) = fastype_of trm 
    60                       in Collect_const t $ trm end;
    61 
    62 fun inclt ty = Const (@{const_name HOL.less_eq}, [ty,ty] ---> boolT);
    63 
    64 (** Makes "Mset <= t" **)
    65 fun Mset_incl t = let val MsetT = fastype_of t 
    66                  in mk_Trueprop ((inclt MsetT) $ Free ("Mset", MsetT) $ t) end;
    67 
    68 
    69 fun Mset thm = let val vars = get_vars(thm);
    70                    val varsT = fastype_of (mk_bodyC vars);
    71                    val big_Collect = mk_CollectC (mk_abstupleC vars 
    72                          (Free ("P",varsT --> boolT) $ mk_bodyC vars));
    73                    val small_Collect = mk_CollectC (Abs("x",varsT,
    74                            Free ("P",varsT --> boolT) $ Bound 0));
    75                    val impl = Logic.mk_implies (Mset_incl big_Collect, Mset_incl small_Collect);
    76    in Goal.prove (ProofContext.init (Thm.theory_of_thm thm)) ["Mset", "P"] [] impl (K (CLASET' blast_tac 1)) end;
    77 
    78 end;
    79 
    80 
    81 (*****************************************************************************)
    82 (** Simplifying:                                                            **)
    83 (** Some useful lemmata, lists and simplification tactics to control which  **)
    84 (** theorems are used to simplify at each moment, so that the original      **)
    85 (** input does not suffer any unexpected transformation                     **)
    86 (*****************************************************************************)
    87 
    88 (**Simp_tacs**)
    89 
    90 val before_set2pred_simp_tac =
    91   (simp_tac (HOL_basic_ss addsimps [Collect_conj_eq RS sym, @{thm Compl_Collect}]));
    92 
    93 val split_simp_tac = (simp_tac (HOL_basic_ss addsimps [split_conv]));
    94 
    95 (*****************************************************************************)
    96 (** set2pred transforms sets inclusion into predicates implication,         **)
    97 (** maintaining the original variable names.                                **)
    98 (** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1"              **)
    99 (** Subgoals containing intersections (A Int B) or complement sets (-A)     **)
   100 (** are first simplified by "before_set2pred_simp_tac", that returns only   **)
   101 (** subgoals of the form "{x. P x} <= {x. Q x}", which are easily           **)
   102 (** transformed.                                                            **)
   103 (** This transformation may solve very easy subgoals due to a ligth         **)
   104 (** simplification done by (split_all_tac)                                  **)
   105 (*****************************************************************************)
   106 
   107 fun set2pred i thm =
   108   let val var_names = map (fst o dest_Free) (get_vars thm) in
   109     ((before_set2pred_simp_tac i) THEN_MAYBE
   110      (EVERY [rtac subsetI i, 
   111              rtac CollectI i,
   112              dtac CollectD i,
   113              (TRY(split_all_tac i)) THEN_MAYBE
   114              ((rename_tac var_names i) THEN
   115               (full_simp_tac (HOL_basic_ss addsimps [split_conv]) i)) ])) thm
   116   end;
   117 
   118 (*****************************************************************************)
   119 (** BasicSimpTac is called to simplify all verification conditions. It does **)
   120 (** a light simplification by applying "mem_Collect_eq", then it calls      **)
   121 (** MaxSimpTac, which solves subgoals of the form "A <= A",                 **)
   122 (** and transforms any other into predicates, applying then                 **)
   123 (** the tactic chosen by the user, which may solve the subgoal completely.  **)
   124 (*****************************************************************************)
   125 
   126 fun MaxSimpTac tac = FIRST'[rtac subset_refl, set2pred THEN_MAYBE' tac];
   127 
   128 fun BasicSimpTac tac =
   129   simp_tac
   130     (HOL_basic_ss addsimps [mem_Collect_eq,split_conv] addsimprocs [record_simproc])
   131   THEN_MAYBE' MaxSimpTac tac;
   132 
   133 (** HoareRuleTac **)
   134 
   135 fun WlpTac Mlem tac i =
   136   rtac @{thm SeqRule} i THEN  HoareRuleTac Mlem tac false (i+1)
   137 and HoareRuleTac Mlem tac pre_cond i st = st |>
   138         (*abstraction over st prevents looping*)
   139     ( (WlpTac Mlem tac i THEN HoareRuleTac Mlem tac pre_cond i)
   140       ORELSE
   141       (FIRST[rtac @{thm SkipRule} i,
   142              EVERY[rtac @{thm BasicRule} i,
   143                    rtac Mlem i,
   144                    split_simp_tac i],
   145              EVERY[rtac @{thm CondRule} i,
   146                    HoareRuleTac Mlem tac false (i+2),
   147                    HoareRuleTac Mlem tac false (i+1)],
   148              EVERY[rtac @{thm WhileRule} i,
   149                    BasicSimpTac tac (i+2),
   150                    HoareRuleTac Mlem tac true (i+1)] ] 
   151        THEN (if pre_cond then (BasicSimpTac tac i) else (rtac subset_refl i)) ));
   152 
   153 
   154 (** tac:(int -> tactic) is the tactic the user chooses to solve or simplify **)
   155 (** the final verification conditions                                       **)
   156  
   157 fun hoare_tac tac i thm =
   158   let val Mlem = Mset(thm)
   159   in SELECT_GOAL(EVERY[HoareRuleTac Mlem tac true 1]) i thm end;