src/HOL/Hoare/hoare_tac.ML
author wenzelm
Wed Aug 29 16:46:08 2007 +0200 (2007-08-29)
changeset 24475 a297ae4ff188
child 26300 03def556e26e
permissions -rw-r--r--
added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm@24475
     1
(*  Title:      HOL/Hoare/hoare_tac.ML
wenzelm@24475
     2
    ID:         $Id$
wenzelm@24475
     3
    Author:     Leonor Prensa Nieto & Tobias Nipkow
wenzelm@24475
     4
    Copyright   1998 TUM
wenzelm@24475
     5
wenzelm@24475
     6
Derivation of the proof rules and, most importantly, the VCG tactic.
wenzelm@24475
     7
*)
wenzelm@24475
     8
wenzelm@24475
     9
(*** The tactics ***)
wenzelm@24475
    10
wenzelm@24475
    11
(*****************************************************************************)
wenzelm@24475
    12
(** The function Mset makes the theorem                                     **)
wenzelm@24475
    13
(** "?Mset <= {(x1,...,xn). ?P (x1,...,xn)} ==> ?Mset <= {s. ?P s}",        **)
wenzelm@24475
    14
(** where (x1,...,xn) are the variables of the particular program we are    **)
wenzelm@24475
    15
(** working on at the moment of the call                                    **)
wenzelm@24475
    16
(*****************************************************************************)
wenzelm@24475
    17
wenzelm@24475
    18
local open HOLogic in
wenzelm@24475
    19
wenzelm@24475
    20
(** maps (%x1 ... xn. t) to [x1,...,xn] **)
wenzelm@24475
    21
fun abs2list (Const ("split",_) $ (Abs(x,T,t))) = Free (x, T)::abs2list t
wenzelm@24475
    22
  | abs2list (Abs(x,T,t)) = [Free (x, T)]
wenzelm@24475
    23
  | abs2list _ = [];
wenzelm@24475
    24
wenzelm@24475
    25
(** maps {(x1,...,xn). t} to [x1,...,xn] **)
wenzelm@24475
    26
fun mk_vars (Const ("Collect",_) $ T) = abs2list T
wenzelm@24475
    27
  | mk_vars _ = [];
wenzelm@24475
    28
wenzelm@24475
    29
(** abstraction of body over a tuple formed from a list of free variables. 
wenzelm@24475
    30
Types are also built **)
wenzelm@24475
    31
fun mk_abstupleC []     body = absfree ("x", unitT, body)
wenzelm@24475
    32
  | mk_abstupleC (v::w) body = let val (n,T) = dest_Free v
wenzelm@24475
    33
                               in if w=[] then absfree (n, T, body)
wenzelm@24475
    34
        else let val z  = mk_abstupleC w body;
wenzelm@24475
    35
                 val T2 = case z of Abs(_,T,_) => T
wenzelm@24475
    36
                        | Const (_, Type (_,[_, Type (_,[T,_])])) $ _ => T;
wenzelm@24475
    37
       in Const ("split", (T --> T2 --> boolT) --> mk_prodT (T,T2) --> boolT) 
wenzelm@24475
    38
          $ absfree (n, T, z) end end;
wenzelm@24475
    39
wenzelm@24475
    40
(** maps [x1,...,xn] to (x1,...,xn) and types**)
wenzelm@24475
    41
fun mk_bodyC []      = HOLogic.unit
wenzelm@24475
    42
  | mk_bodyC (x::xs) = if xs=[] then x 
wenzelm@24475
    43
               else let val (n, T) = dest_Free x ;
wenzelm@24475
    44
                        val z = mk_bodyC xs;
wenzelm@24475
    45
                        val T2 = case z of Free(_, T) => T
wenzelm@24475
    46
                                         | Const ("Pair", Type ("fun", [_, Type
wenzelm@24475
    47
                                            ("fun", [_, T])])) $ _ $ _ => T;
wenzelm@24475
    48
                 in Const ("Pair", [T, T2] ---> mk_prodT (T, T2)) $ x $ z end;
wenzelm@24475
    49
wenzelm@24475
    50
(** maps a goal of the form:
wenzelm@24475
    51
        1. [| P |] ==> VARS x1 ... xn {._.} _ {._.} or to [x1,...,xn]**) 
wenzelm@24475
    52
fun get_vars thm = let  val c = Logic.unprotect (concl_of (thm));
wenzelm@24475
    53
                        val d = Logic.strip_assums_concl c;
wenzelm@24475
    54
                        val Const _ $ pre $ _ $ _ = dest_Trueprop d;
wenzelm@24475
    55
      in mk_vars pre end;
wenzelm@24475
    56
wenzelm@24475
    57
wenzelm@24475
    58
(** Makes Collect with type **)
wenzelm@24475
    59
fun mk_CollectC trm = let val T as Type ("fun",[t,_]) = fastype_of trm 
wenzelm@24475
    60
                      in Collect_const t $ trm end;
wenzelm@24475
    61
wenzelm@24475
    62
fun inclt ty = Const (@{const_name HOL.less_eq}, [ty,ty] ---> boolT);
wenzelm@24475
    63
wenzelm@24475
    64
(** Makes "Mset <= t" **)
wenzelm@24475
    65
fun Mset_incl t = let val MsetT = fastype_of t 
wenzelm@24475
    66
                 in mk_Trueprop ((inclt MsetT) $ Free ("Mset", MsetT) $ t) end;
wenzelm@24475
    67
wenzelm@24475
    68
wenzelm@24475
    69
fun Mset thm = let val vars = get_vars(thm);
wenzelm@24475
    70
                   val varsT = fastype_of (mk_bodyC vars);
wenzelm@24475
    71
                   val big_Collect = mk_CollectC (mk_abstupleC vars 
wenzelm@24475
    72
                         (Free ("P",varsT --> boolT) $ mk_bodyC vars));
wenzelm@24475
    73
                   val small_Collect = mk_CollectC (Abs("x",varsT,
wenzelm@24475
    74
                           Free ("P",varsT --> boolT) $ Bound 0));
wenzelm@24475
    75
                   val impl = implies $ (Mset_incl big_Collect) $ 
wenzelm@24475
    76
                                          (Mset_incl small_Collect);
wenzelm@24475
    77
   in Goal.prove (ProofContext.init (Thm.theory_of_thm thm)) ["Mset", "P"] [] impl (K (CLASET' blast_tac 1)) end;
wenzelm@24475
    78
wenzelm@24475
    79
end;
wenzelm@24475
    80
wenzelm@24475
    81
wenzelm@24475
    82
(*****************************************************************************)
wenzelm@24475
    83
(** Simplifying:                                                            **)
wenzelm@24475
    84
(** Some useful lemmata, lists and simplification tactics to control which  **)
wenzelm@24475
    85
(** theorems are used to simplify at each moment, so that the original      **)
wenzelm@24475
    86
(** input does not suffer any unexpected transformation                     **)
wenzelm@24475
    87
(*****************************************************************************)
wenzelm@24475
    88
wenzelm@24475
    89
Goal "-(Collect b) = {x. ~(b x)}";
wenzelm@24475
    90
by (Fast_tac 1);
wenzelm@24475
    91
qed "Compl_Collect";
wenzelm@24475
    92
wenzelm@24475
    93
wenzelm@24475
    94
(**Simp_tacs**)
wenzelm@24475
    95
wenzelm@24475
    96
val before_set2pred_simp_tac =
wenzelm@24475
    97
  (simp_tac (HOL_basic_ss addsimps [Collect_conj_eq RS sym,Compl_Collect]));
wenzelm@24475
    98
wenzelm@24475
    99
val split_simp_tac = (simp_tac (HOL_basic_ss addsimps [split_conv]));
wenzelm@24475
   100
wenzelm@24475
   101
(*****************************************************************************)
wenzelm@24475
   102
(** set2pred transforms sets inclusion into predicates implication,         **)
wenzelm@24475
   103
(** maintaining the original variable names.                                **)
wenzelm@24475
   104
(** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1"              **)
wenzelm@24475
   105
(** Subgoals containing intersections (A Int B) or complement sets (-A)     **)
wenzelm@24475
   106
(** are first simplified by "before_set2pred_simp_tac", that returns only   **)
wenzelm@24475
   107
(** subgoals of the form "{x. P x} <= {x. Q x}", which are easily           **)
wenzelm@24475
   108
(** transformed.                                                            **)
wenzelm@24475
   109
(** This transformation may solve very easy subgoals due to a ligth         **)
wenzelm@24475
   110
(** simplification done by (split_all_tac)                                  **)
wenzelm@24475
   111
(*****************************************************************************)
wenzelm@24475
   112
wenzelm@24475
   113
fun set2pred i thm =
wenzelm@24475
   114
  let val var_names = map (fst o dest_Free) (get_vars thm) in
wenzelm@24475
   115
    ((before_set2pred_simp_tac i) THEN_MAYBE
wenzelm@24475
   116
     (EVERY [rtac subsetI i, 
wenzelm@24475
   117
             rtac CollectI i,
wenzelm@24475
   118
             dtac CollectD i,
wenzelm@24475
   119
             (TRY(split_all_tac i)) THEN_MAYBE
wenzelm@24475
   120
             ((rename_params_tac var_names i) THEN
wenzelm@24475
   121
              (full_simp_tac (HOL_basic_ss addsimps [split_conv]) i)) ])) thm
wenzelm@24475
   122
  end;
wenzelm@24475
   123
wenzelm@24475
   124
(*****************************************************************************)
wenzelm@24475
   125
(** BasicSimpTac is called to simplify all verification conditions. It does **)
wenzelm@24475
   126
(** a light simplification by applying "mem_Collect_eq", then it calls      **)
wenzelm@24475
   127
(** MaxSimpTac, which solves subgoals of the form "A <= A",                 **)
wenzelm@24475
   128
(** and transforms any other into predicates, applying then                 **)
wenzelm@24475
   129
(** the tactic chosen by the user, which may solve the subgoal completely.  **)
wenzelm@24475
   130
(*****************************************************************************)
wenzelm@24475
   131
wenzelm@24475
   132
fun MaxSimpTac tac = FIRST'[rtac subset_refl, set2pred THEN_MAYBE' tac];
wenzelm@24475
   133
wenzelm@24475
   134
fun BasicSimpTac tac =
wenzelm@24475
   135
  simp_tac
wenzelm@24475
   136
    (HOL_basic_ss addsimps [mem_Collect_eq,split_conv] addsimprocs [record_simproc])
wenzelm@24475
   137
  THEN_MAYBE' MaxSimpTac tac;
wenzelm@24475
   138
wenzelm@24475
   139
(** HoareRuleTac **)
wenzelm@24475
   140
wenzelm@24475
   141
fun WlpTac Mlem tac i =
wenzelm@24475
   142
  rtac @{thm SeqRule} i THEN  HoareRuleTac Mlem tac false (i+1)
wenzelm@24475
   143
and HoareRuleTac Mlem tac pre_cond i st = st |>
wenzelm@24475
   144
        (*abstraction over st prevents looping*)
wenzelm@24475
   145
    ( (WlpTac Mlem tac i THEN HoareRuleTac Mlem tac pre_cond i)
wenzelm@24475
   146
      ORELSE
wenzelm@24475
   147
      (FIRST[rtac @{thm SkipRule} i,
wenzelm@24475
   148
             EVERY[rtac @{thm BasicRule} i,
wenzelm@24475
   149
                   rtac Mlem i,
wenzelm@24475
   150
                   split_simp_tac i],
wenzelm@24475
   151
             EVERY[rtac @{thm CondRule} i,
wenzelm@24475
   152
                   HoareRuleTac Mlem tac false (i+2),
wenzelm@24475
   153
                   HoareRuleTac Mlem tac false (i+1)],
wenzelm@24475
   154
             EVERY[rtac @{thm WhileRule} i,
wenzelm@24475
   155
                   BasicSimpTac tac (i+2),
wenzelm@24475
   156
                   HoareRuleTac Mlem tac true (i+1)] ] 
wenzelm@24475
   157
       THEN (if pre_cond then (BasicSimpTac tac i) else (rtac subset_refl i)) ));
wenzelm@24475
   158
wenzelm@24475
   159
wenzelm@24475
   160
(** tac:(int -> tactic) is the tactic the user chooses to solve or simplify **)
wenzelm@24475
   161
(** the final verification conditions                                       **)
wenzelm@24475
   162
 
wenzelm@24475
   163
fun hoare_tac tac i thm =
wenzelm@24475
   164
  let val Mlem = Mset(thm)
wenzelm@24475
   165
  in SELECT_GOAL(EVERY[HoareRuleTac Mlem tac true 1]) i thm end;