src/HOL/Hoare/hoare_tac.ML
author huffman
Fri Mar 30 12:32:35 2012 +0200 (2012-03-30)
changeset 47220 52426c62b5d0
parent 44241 7943b69f0188
child 51717 9e7d1c139569
permissions -rw-r--r--
replace lemmas eval_nat_numeral with a simpler reformulation
     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 (* FIXME structure Hoare: HOARE *)
     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
    19 
    20 (** maps (%x1 ... xn. t) to [x1,...,xn] **)
    21 fun abs2list (Const (@{const_name prod_case}, _) $ 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 (@{const_name 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", HOLogic.unitT) body
    32   | mk_abstupleC [v] body = absfree (dest_Free v) body
    33   | mk_abstupleC (v :: w) body =
    34       let
    35         val (x, T) = dest_Free v;
    36         val z = mk_abstupleC w body;
    37         val T2 =
    38           (case z of
    39             Abs (_, T, _) => T
    40           | Const (_, Type (_, [_, Type (_, [T, _])])) $ _ => T);
    41       in
    42         Const (@{const_name prod_case},
    43             (T --> T2 --> HOLogic.boolT) --> HOLogic.mk_prodT (T, T2) --> HOLogic.boolT) $
    44           absfree (x, T) z
    45       end;
    46 
    47 (** maps [x1,...,xn] to (x1,...,xn) and types**)
    48 fun mk_bodyC []      = HOLogic.unit
    49   | mk_bodyC (x::xs) = if xs=[] then x
    50                else let val (n, T) = dest_Free x ;
    51                         val z = mk_bodyC xs;
    52                         val T2 = case z of Free(_, T) => T
    53                                          | Const (@{const_name Pair}, Type ("fun", [_, Type
    54                                             ("fun", [_, T])])) $ _ $ _ => T;
    55                  in Const (@{const_name Pair}, [T, T2] ---> HOLogic.mk_prodT (T, T2)) $ x $ z end;
    56 
    57 (** maps a subgoal of the form:
    58         VARS x1 ... xn {._.} _ {._.} or to [x1,...,xn]**)
    59 fun get_vars c =
    60   let
    61     val d = Logic.strip_assums_concl c;
    62     val Const _ $ pre $ _ $ _ = HOLogic.dest_Trueprop d;
    63   in mk_vars pre end;
    64 
    65 fun mk_CollectC trm =
    66   let val T as Type ("fun",[t,_]) = fastype_of trm
    67   in HOLogic.Collect_const t $ trm end;
    68 
    69 fun inclt ty = Const (@{const_name Orderings.less_eq}, [ty,ty] ---> HOLogic.boolT);
    70 
    71 in
    72 
    73 fun Mset ctxt prop =
    74   let
    75     val [(Mset, _), (P, _)] = Variable.variant_frees ctxt [] [("Mset", ()), ("P", ())];
    76 
    77     val vars = get_vars prop;
    78     val varsT = fastype_of (mk_bodyC vars);
    79     val big_Collect = mk_CollectC (mk_abstupleC vars (Free (P, varsT --> HOLogic.boolT) $ mk_bodyC vars));
    80     val small_Collect = mk_CollectC (Abs ("x", varsT, Free (P, varsT --> HOLogic.boolT) $ Bound 0));
    81 
    82     val MsetT = fastype_of big_Collect;
    83     fun Mset_incl t = HOLogic.mk_Trueprop (inclt MsetT $ Free (Mset, MsetT) $ t);
    84     val impl = Logic.mk_implies (Mset_incl big_Collect, Mset_incl small_Collect);
    85     val th = Goal.prove ctxt [Mset, P] [] impl (fn _ => blast_tac ctxt 1);
    86  in (vars, th) end;
    87 
    88 end;
    89 
    90 
    91 (*****************************************************************************)
    92 (** Simplifying:                                                            **)
    93 (** Some useful lemmata, lists and simplification tactics to control which  **)
    94 (** theorems are used to simplify at each moment, so that the original      **)
    95 (** input does not suffer any unexpected transformation                     **)
    96 (*****************************************************************************)
    97 
    98 (**Simp_tacs**)
    99 
   100 val before_set2pred_simp_tac =
   101   (simp_tac (HOL_basic_ss addsimps [Collect_conj_eq RS sym, @{thm Compl_Collect}]));
   102 
   103 val split_simp_tac = (simp_tac (HOL_basic_ss addsimps [@{thm split_conv}]));
   104 
   105 (*****************************************************************************)
   106 (** set2pred_tac transforms sets inclusion into predicates implication,     **)
   107 (** maintaining the original variable names.                                **)
   108 (** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1"              **)
   109 (** Subgoals containing intersections (A Int B) or complement sets (-A)     **)
   110 (** are first simplified by "before_set2pred_simp_tac", that returns only   **)
   111 (** subgoals of the form "{x. P x} <= {x. Q x}", which are easily           **)
   112 (** transformed.                                                            **)
   113 (** This transformation may solve very easy subgoals due to a ligth         **)
   114 (** simplification done by (split_all_tac)                                  **)
   115 (*****************************************************************************)
   116 
   117 fun set2pred_tac var_names = SUBGOAL (fn (goal, i) =>
   118   before_set2pred_simp_tac i THEN_MAYBE
   119   EVERY [
   120     rtac subsetI i,
   121     rtac CollectI i,
   122     dtac CollectD i,
   123     TRY (split_all_tac i) THEN_MAYBE
   124      (rename_tac var_names i THEN full_simp_tac (HOL_basic_ss addsimps [@{thm split_conv}]) i)]);
   125 
   126 (*****************************************************************************)
   127 (** BasicSimpTac is called to simplify all verification conditions. It does **)
   128 (** a light simplification by applying "mem_Collect_eq", then it calls      **)
   129 (** MaxSimpTac, which solves subgoals of the form "A <= A",                 **)
   130 (** and transforms any other into predicates, applying then                 **)
   131 (** the tactic chosen by the user, which may solve the subgoal completely.  **)
   132 (*****************************************************************************)
   133 
   134 fun MaxSimpTac var_names tac = FIRST'[rtac subset_refl, set2pred_tac var_names THEN_MAYBE' tac];
   135 
   136 fun BasicSimpTac var_names tac =
   137   simp_tac
   138     (HOL_basic_ss addsimps [mem_Collect_eq, @{thm split_conv}] addsimprocs [Record.simproc])
   139   THEN_MAYBE' MaxSimpTac var_names tac;
   140 
   141 
   142 (** hoare_rule_tac **)
   143 
   144 fun hoare_rule_tac (vars, Mlem) tac =
   145   let
   146     val var_names = map (fst o dest_Free) vars;
   147     fun wlp_tac i =
   148       rtac @{thm SeqRule} i THEN rule_tac false (i + 1)
   149     and rule_tac pre_cond i st = st |> (*abstraction over st prevents looping*)
   150       ((wlp_tac i THEN rule_tac pre_cond i)
   151         ORELSE
   152         (FIRST [
   153           rtac @{thm SkipRule} i,
   154           rtac @{thm AbortRule} i,
   155           EVERY [
   156             rtac @{thm BasicRule} i,
   157             rtac Mlem i,
   158             split_simp_tac i],
   159           EVERY [
   160             rtac @{thm CondRule} i,
   161             rule_tac false (i + 2),
   162             rule_tac false (i + 1)],
   163           EVERY [
   164             rtac @{thm WhileRule} i,
   165             BasicSimpTac var_names tac (i + 2),
   166             rule_tac true (i + 1)]]
   167          THEN (if pre_cond then BasicSimpTac var_names tac i else rtac subset_refl i)));
   168   in rule_tac end;
   169 
   170 
   171 (** tac is the tactic the user chooses to solve or simplify **)
   172 (** the final verification conditions                       **)
   173 
   174 fun hoare_tac ctxt (tac: int -> tactic) = SUBGOAL (fn (goal, i) =>
   175   SELECT_GOAL (hoare_rule_tac (Mset ctxt goal) tac true 1) i);
   176