src/HOL/Hoare/hoare_tac.ML
 author wenzelm Mon Mar 17 18:37:00 2008 +0100 (2008-03-17) changeset 26300 03def556e26e parent 24475 a297ae4ff188 child 27244 af0a44372d1f permissions -rw-r--r--
removed duplicate lemmas;
```     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 = implies \$ (Mset_incl big_Collect) \$
```
```    76                                           (Mset_incl small_Collect);
```
```    77    in Goal.prove (ProofContext.init (Thm.theory_of_thm thm)) ["Mset", "P"] [] impl (K (CLASET' blast_tac 1)) end;
```
```    78
```
```    79 end;
```
```    80
```
```    81
```
```    82 (*****************************************************************************)
```
```    83 (** Simplifying:                                                            **)
```
```    84 (** Some useful lemmata, lists and simplification tactics to control which  **)
```
```    85 (** theorems are used to simplify at each moment, so that the original      **)
```
```    86 (** input does not suffer any unexpected transformation                     **)
```
```    87 (*****************************************************************************)
```
```    88
```
```    89 (**Simp_tacs**)
```
```    90
```
```    91 val before_set2pred_simp_tac =
```
```    92   (simp_tac (HOL_basic_ss addsimps [Collect_conj_eq RS sym, @{thm Compl_Collect}]));
```
```    93
```
```    94 val split_simp_tac = (simp_tac (HOL_basic_ss addsimps [split_conv]));
```
```    95
```
```    96 (*****************************************************************************)
```
```    97 (** set2pred transforms sets inclusion into predicates implication,         **)
```
```    98 (** maintaining the original variable names.                                **)
```
```    99 (** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1"              **)
```
```   100 (** Subgoals containing intersections (A Int B) or complement sets (-A)     **)
```
```   101 (** are first simplified by "before_set2pred_simp_tac", that returns only   **)
```
```   102 (** subgoals of the form "{x. P x} <= {x. Q x}", which are easily           **)
```
```   103 (** transformed.                                                            **)
```
```   104 (** This transformation may solve very easy subgoals due to a ligth         **)
```
```   105 (** simplification done by (split_all_tac)                                  **)
```
```   106 (*****************************************************************************)
```
```   107
```
```   108 fun set2pred i thm =
```
```   109   let val var_names = map (fst o dest_Free) (get_vars thm) in
```
```   110     ((before_set2pred_simp_tac i) THEN_MAYBE
```
```   111      (EVERY [rtac subsetI i,
```
```   112              rtac CollectI i,
```
```   113              dtac CollectD i,
```
```   114              (TRY(split_all_tac i)) THEN_MAYBE
```
```   115              ((rename_params_tac var_names i) THEN
```
```   116               (full_simp_tac (HOL_basic_ss addsimps [split_conv]) i)) ])) thm
```
```   117   end;
```
```   118
```
```   119 (*****************************************************************************)
```
```   120 (** BasicSimpTac is called to simplify all verification conditions. It does **)
```
```   121 (** a light simplification by applying "mem_Collect_eq", then it calls      **)
```
```   122 (** MaxSimpTac, which solves subgoals of the form "A <= A",                 **)
```
```   123 (** and transforms any other into predicates, applying then                 **)
```
```   124 (** the tactic chosen by the user, which may solve the subgoal completely.  **)
```
```   125 (*****************************************************************************)
```
```   126
```
```   127 fun MaxSimpTac tac = FIRST'[rtac subset_refl, set2pred THEN_MAYBE' tac];
```
```   128
```
```   129 fun BasicSimpTac tac =
```
```   130   simp_tac
```
```   131     (HOL_basic_ss addsimps [mem_Collect_eq,split_conv] addsimprocs [record_simproc])
```
```   132   THEN_MAYBE' MaxSimpTac tac;
```
```   133
```
```   134 (** HoareRuleTac **)
```
```   135
```
```   136 fun WlpTac Mlem tac i =
```
```   137   rtac @{thm SeqRule} i THEN  HoareRuleTac Mlem tac false (i+1)
```
```   138 and HoareRuleTac Mlem tac pre_cond i st = st |>
```
```   139         (*abstraction over st prevents looping*)
```
```   140     ( (WlpTac Mlem tac i THEN HoareRuleTac Mlem tac pre_cond i)
```
```   141       ORELSE
```
```   142       (FIRST[rtac @{thm SkipRule} i,
```
```   143              EVERY[rtac @{thm BasicRule} i,
```
```   144                    rtac Mlem i,
```
```   145                    split_simp_tac i],
```
```   146              EVERY[rtac @{thm CondRule} i,
```
```   147                    HoareRuleTac Mlem tac false (i+2),
```
```   148                    HoareRuleTac Mlem tac false (i+1)],
```
```   149              EVERY[rtac @{thm WhileRule} i,
```
```   150                    BasicSimpTac tac (i+2),
```
```   151                    HoareRuleTac Mlem tac true (i+1)] ]
```
```   152        THEN (if pre_cond then (BasicSimpTac tac i) else (rtac subset_refl i)) ));
```
```   153
```
```   154
```
```   155 (** tac:(int -> tactic) is the tactic the user chooses to solve or simplify **)
```
```   156 (** the final verification conditions                                       **)
```
```   157
```
```   158 fun hoare_tac tac i thm =
```
```   159   let val Mlem = Mset(thm)
```
```   160   in SELECT_GOAL(EVERY[HoareRuleTac Mlem tac true 1]) i thm end;
```