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