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