src/HOL/Hoare/hoare_tac.ML
author hoelzl
Thu Sep 02 10:14:32 2010 +0200 (2010-09-02)
changeset 39072 1030b1a166ef
parent 38012 3ca193a6ae5a
child 41449 7339f0e7c513
permissions -rw-r--r--
Add lessThan_Suc_eq_insert_0
     1 (*  Title:      HOL/Hoare/hoare_tac.ML
     2     Author:     Leonor Prensa Nieto & Tobias Nipkow
     3 
     4 Derivation of the proof rules and, most importantly, the VCG tactic.
     5 *)
     6 
     7 (*** The tactics ***)
     8 
     9 (*****************************************************************************)
    10 (** The function Mset makes the theorem                                     **)
    11 (** "?Mset <= {(x1,...,xn). ?P (x1,...,xn)} ==> ?Mset <= {s. ?P s}",        **)
    12 (** where (x1,...,xn) are the variables of the particular program we are    **)
    13 (** working on at the moment of the call                                    **)
    14 (*****************************************************************************)
    15 
    16 local open HOLogic in
    17 
    18 (** maps (%x1 ... xn. t) to [x1,...,xn] **)
    19 fun abs2list (Const (@{const_name prod_case}, _) $ Abs (x, T, t)) = Free (x, T) :: abs2list t
    20   | abs2list (Abs (x, T, t)) = [Free (x, T)]
    21   | abs2list _ = [];
    22 
    23 (** maps {(x1,...,xn). t} to [x1,...,xn] **)
    24 fun mk_vars (Const (@{const_name Collect},_) $ T) = abs2list T
    25   | mk_vars _ = [];
    26 
    27 (** abstraction of body over a tuple formed from a list of free variables.
    28 Types are also built **)
    29 fun mk_abstupleC []     body = absfree ("x", unitT, body)
    30   | mk_abstupleC (v::w) body = let val (n,T) = dest_Free v
    31                                in if w=[] then absfree (n, T, body)
    32         else let val z  = mk_abstupleC w body;
    33                  val T2 = case z of Abs(_,T,_) => T
    34                         | Const (_, Type (_,[_, Type (_,[T,_])])) $ _ => T;
    35        in Const (@{const_name prod_case}, (T --> T2 --> boolT) --> mk_prodT (T,T2) --> boolT)
    36           $ absfree (n, T, z) end end;
    37 
    38 (** maps [x1,...,xn] to (x1,...,xn) and types**)
    39 fun mk_bodyC []      = HOLogic.unit
    40   | mk_bodyC (x::xs) = if xs=[] then x
    41                else let val (n, T) = dest_Free x ;
    42                         val z = mk_bodyC xs;
    43                         val T2 = case z of Free(_, T) => T
    44                                          | Const (@{const_name Pair}, Type ("fun", [_, Type
    45                                             ("fun", [_, T])])) $ _ $ _ => T;
    46                  in Const (@{const_name Pair}, [T, T2] ---> mk_prodT (T, T2)) $ x $ z end;
    47 
    48 (** maps a subgoal of the form:
    49         VARS x1 ... xn {._.} _ {._.} or to [x1,...,xn]**)
    50 fun get_vars c =
    51   let
    52     val d = Logic.strip_assums_concl c;
    53     val Const _ $ pre $ _ $ _ = dest_Trueprop d;
    54   in mk_vars pre end;
    55 
    56 fun mk_CollectC trm =
    57   let val T as Type ("fun",[t,_]) = fastype_of trm
    58   in Collect_const t $ trm end;
    59 
    60 fun inclt ty = Const (@{const_name Orderings.less_eq}, [ty,ty] ---> boolT);
    61 
    62 
    63 fun Mset ctxt prop =
    64   let
    65     val [(Mset, _), (P, _)] = Variable.variant_frees ctxt [] [("Mset", ()), ("P", ())];
    66 
    67     val vars = get_vars prop;
    68     val varsT = fastype_of (mk_bodyC vars);
    69     val big_Collect = mk_CollectC (mk_abstupleC vars (Free (P, varsT --> boolT) $ mk_bodyC vars));
    70     val small_Collect = mk_CollectC (Abs ("x", varsT, Free (P, varsT --> boolT) $ Bound 0));
    71 
    72     val MsetT = fastype_of big_Collect;
    73     fun Mset_incl t = mk_Trueprop (inclt MsetT $ Free (Mset, MsetT) $ t);
    74     val impl = Logic.mk_implies (Mset_incl big_Collect, Mset_incl small_Collect);
    75     val th = Goal.prove ctxt [Mset, P] [] impl (fn _ => blast_tac (claset_of ctxt) 1);
    76  in (vars, th) 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 [@{thm split_conv}]));
    94 
    95 (*****************************************************************************)
    96 (** set2pred_tac 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_tac var_names = SUBGOAL (fn (goal, i) =>
   108   before_set2pred_simp_tac i THEN_MAYBE
   109   EVERY [
   110     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 full_simp_tac (HOL_basic_ss addsimps [@{thm split_conv}]) i)]);
   115 
   116 (*****************************************************************************)
   117 (** BasicSimpTac is called to simplify all verification conditions. It does **)
   118 (** a light simplification by applying "mem_Collect_eq", then it calls      **)
   119 (** MaxSimpTac, which solves subgoals of the form "A <= A",                 **)
   120 (** and transforms any other into predicates, applying then                 **)
   121 (** the tactic chosen by the user, which may solve the subgoal completely.  **)
   122 (*****************************************************************************)
   123 
   124 fun MaxSimpTac var_names tac = FIRST'[rtac subset_refl, set2pred_tac var_names THEN_MAYBE' tac];
   125 
   126 fun BasicSimpTac var_names tac =
   127   simp_tac
   128     (HOL_basic_ss addsimps [mem_Collect_eq, @{thm split_conv}] addsimprocs [Record.simproc])
   129   THEN_MAYBE' MaxSimpTac var_names tac;
   130 
   131 
   132 (** hoare_rule_tac **)
   133 
   134 fun hoare_rule_tac (vars, Mlem) tac =
   135   let
   136     val var_names = map (fst o dest_Free) vars;
   137     fun wlp_tac i =
   138       rtac @{thm SeqRule} i THEN rule_tac false (i + 1)
   139     and rule_tac pre_cond i st = st |> (*abstraction over st prevents looping*)
   140       ((wlp_tac i THEN rule_tac pre_cond i)
   141         ORELSE
   142         (FIRST [
   143           rtac @{thm SkipRule} i,
   144           rtac @{thm AbortRule} i,
   145           EVERY [
   146             rtac @{thm BasicRule} i,
   147             rtac Mlem i,
   148             split_simp_tac i],
   149           EVERY [
   150             rtac @{thm CondRule} i,
   151             rule_tac false (i + 2),
   152             rule_tac false (i + 1)],
   153           EVERY [
   154             rtac @{thm WhileRule} i,
   155             BasicSimpTac var_names tac (i + 2),
   156             rule_tac true (i + 1)]]
   157          THEN (if pre_cond then BasicSimpTac var_names tac i else rtac subset_refl i)));
   158   in rule_tac end;
   159 
   160 
   161 (** tac is the tactic the user chooses to solve or simplify **)
   162 (** the final verification conditions                       **)
   163 
   164 fun hoare_tac ctxt (tac: int -> tactic) = SUBGOAL (fn (goal, i) =>
   165   SELECT_GOAL (hoare_rule_tac (Mset ctxt goal) tac true 1) i);
   166