src/Provers/splitter.ML
author oheimb
Wed Aug 12 16:21:18 1998 +0200 (1998-08-12)
changeset 5304 c133f16febc7
parent 4930 89271bc4e7ed
child 5437 f68b9d225942
permissions -rw-r--r--
the splitter is now defined as a functor
moved addsplits, delsplits, Addsplits, Delsplits to Provers/splitter.ML
moved split_thm_info to Provers/splitter.ML
definined atomize via general mk_atomize
removed superfluous rot_eq_tac from simplifier.ML
HOL/simpdata.ML: renamed mk_meta_eq to meta_eq,
re-renamed mk_meta_eq_simp to mk_meta_eq
added Eps_eq to simpset
     1 (*  Title:      Provers/splitter
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   1995  TU Munich
     5 
     6 Generic case-splitter, suitable for most logics.
     7 *)
     8 
     9 infix 4 addsplits delsplits;
    10 
    11 signature SPLITTER_DATA =
    12 sig
    13   structure Simplifier: SIMPLIFIER
    14   val mk_meta_eq    : thm -> thm
    15   val meta_eq_to_iff: thm (* "x == y ==> x = y"                    *)
    16   val iffD          : thm (* "[| P = Q; Q |] ==> P"                *)
    17   val disjE         : thm (* "[| P | Q; P ==> R; Q ==> R |] ==> R" *)
    18   val conjE         : thm (* "[| P & Q; [| P; Q |] ==> R |] ==> R" *)
    19   val exE           : thm (* "[|  x. P x; !!x. P x ==> Q |] ==> Q" *)
    20   val contrapos     : thm (* "[| ~ Q; P ==> Q |] ==> ~ P"          *)
    21   val contrapos2    : thm (* "[| Q; ~ P ==> ~ Q |] ==> P"          *)
    22   val notnotD       : thm (* "~ ~ P ==> P"                         *)
    23 end
    24 
    25 signature SPLITTER =
    26 sig
    27   type simpset
    28   val split_tac       : thm list -> int -> tactic
    29   val split_inside_tac: thm list -> int -> tactic
    30   val split_asm_tac   : thm list -> int -> tactic
    31   val addsplits       : simpset * thm list -> simpset
    32   val delsplits       : simpset * thm list -> simpset
    33   val Addsplits       : thm list -> unit
    34   val Delsplits       : thm list -> unit
    35 end;
    36 
    37 functor SplitterFun(Data: SPLITTER_DATA): SPLITTER =
    38 struct 
    39 
    40 type simpset = Data.Simplifier.simpset;
    41 
    42 val Const ("==>", _) $ (Const ("Trueprop", _) $
    43          (Const (const_not, _) $ _    )) $ _ = #prop (rep_thm(Data.notnotD));
    44 
    45 val Const ("==>", _) $ (Const ("Trueprop", _) $
    46          (Const (const_or , _) $ _ $ _)) $ _ = #prop (rep_thm(Data.disjE));
    47 
    48 fun split_format_err() = error("Wrong format for split rule");
    49 
    50 fun split_thm_info thm = case concl_of (Data.mk_meta_eq thm) of
    51      Const("==", _)$(Var _$t)$c =>
    52         (case strip_comb t of
    53            (Const(a,_),_) => (a,case c of (Const(s,_)$_)=>s=const_not|_=> false)
    54          | _              => split_format_err())
    55    | _ => split_format_err();
    56 
    57 fun mk_case_split_tac order =
    58 let
    59 
    60 
    61 (************************************************************
    62    Create lift-theorem "trlift" :
    63 
    64    [| !! x. Q(x)==R(x) ; P(R) == C |] ==> P(Q)==C
    65 
    66 *************************************************************)
    67 
    68 val meta_iffD = Data.meta_eq_to_iff RS Data.iffD;
    69 val lift =
    70   let val ct = read_cterm (#sign(rep_thm Data.iffD))
    71            ("[| !!x::'b::logic. Q(x) == R(x) |] ==> \
    72             \P(%x. Q(x)) == P(%x. R(x))::'a::logic",propT)
    73   in prove_goalw_cterm [] ct
    74      (fn [prem] => [rewtac prem, rtac reflexive_thm 1])
    75   end;
    76 
    77 val trlift = lift RS transitive_thm;
    78 val _ $ (Var(P,PT)$_) $ _ = concl_of trlift;
    79 
    80 
    81 (************************************************************************ 
    82    Set up term for instantiation of P in the lift-theorem
    83    
    84    Ts    : types of parameters (i.e. variables bound by meta-quantifiers)
    85    t     : lefthand side of meta-equality in subgoal
    86            the lift theorem is applied to (see select)
    87    pos   : "path" leading to abstraction, coded as a list
    88    T     : type of body of P(...)
    89    maxi  : maximum index of Vars
    90 *************************************************************************)
    91 
    92 fun mk_cntxt Ts t pos T maxi =
    93   let fun var (t,i) = Var(("X",i),type_of1(Ts,t));
    94       fun down [] t i = Bound 0
    95         | down (p::ps) t i =
    96             let val (h,ts) = strip_comb t
    97                 val v1 = ListPair.map var (take(p,ts), i upto (i+p-1))
    98                 val u::us = drop(p,ts)
    99                 val v2 = ListPair.map var (us, (i+p) upto (i+length(ts)-2))
   100       in list_comb(h,v1@[down ps u (i+length ts)]@v2) end;
   101   in Abs("", T, down (rev pos) t maxi) end;
   102 
   103 
   104 (************************************************************************ 
   105    Set up term for instantiation of P in the split-theorem
   106    P(...) == rhs
   107 
   108    t     : lefthand side of meta-equality in subgoal
   109            the split theorem is applied to (see select)
   110    T     : type of body of P(...)
   111    tt    : the term  Const(key,..) $ ...
   112 *************************************************************************)
   113 
   114 fun mk_cntxt_splitthm t tt T =
   115   let fun repl lev t =
   116     if incr_boundvars lev tt = t then Bound lev
   117     else case t of
   118         (Abs (v, T2, t)) => Abs (v, T2, repl (lev+1) t)
   119       | (Bound i) => Bound (if i>=lev then i+1 else i)
   120       | (t1 $ t2) => (repl lev t1) $ (repl lev t2)
   121       | t => t
   122   in Abs("", T, repl 0 t) end;
   123 
   124 
   125 (* add all loose bound variables in t to list is *)
   126 fun add_lbnos(is,t) = add_loose_bnos(t,0,is);
   127 
   128 (* check if the innermost quantifier that needs to be removed
   129    has a body of type T; otherwise the expansion thm will fail later on
   130 *)
   131 fun type_test(T,lbnos,apsns) =
   132   let val (_,U,_) = nth_elem(foldl Int.min (hd lbnos, tl lbnos), apsns)
   133   in T=U end;
   134 
   135 (*************************************************************************
   136    Create a "split_pack".
   137 
   138    thm   : the relevant split-theorem, i.e. P(...) == rhs , where P(...)
   139            is of the form
   140            P( Const(key,...) $ t_1 $ ... $ t_n )      (e.g. key = "if")
   141    T     : type of P(...)
   142    n     : number of arguments expected by Const(key,...)
   143    ts    : list of arguments actually found
   144    apsns : list of tuples of the form (T,U,pos), one tuple for each
   145            abstraction that is encountered on the way to the position where 
   146            Const(key, ...) $ ...  occurs, where
   147            T   : type of the variable bound by the abstraction
   148            U   : type of the abstraction's body
   149            pos : "path" leading to the body of the abstraction
   150    pos   : "path" leading to the position where Const(key, ...) $ ...  occurs.
   151    TB    : type of  Const(key,...) $ t_1 $ ... $ t_n
   152    t     : the term Const(key,...) $ t_1 $ ... $ t_n
   153 
   154    A split pack is a tuple of the form
   155    (thm, apsns, pos, TB)
   156    Note : apsns is reversed, so that the outermost quantifier's position
   157           comes first ! If the terms in ts don't contain variables bound
   158           by other than meta-quantifiers, apsns is empty, because no further
   159           lifting is required before applying the split-theorem.
   160 ******************************************************************************) 
   161 
   162 fun mk_split_pack(thm,T,n,ts,apsns,pos,TB,t) =
   163   if n > length ts then []
   164   else let val lev = length apsns
   165            val lbnos = foldl add_lbnos ([],take(n,ts))
   166            val flbnos = filter (fn i => i < lev) lbnos
   167            val tt = incr_boundvars (~lev) t
   168        in if null flbnos then [(thm,[],pos,TB,tt)]
   169           else if type_test(T,flbnos,apsns) then [(thm, rev apsns,pos,TB,tt)] 
   170                else []
   171        end;
   172 
   173 
   174 (****************************************************************************
   175    Recursively scans term for occurences of Const(key,...) $ ...
   176    Returns a list of "split-packs" (one for each occurence of Const(key,...) )
   177 
   178    cmap : association list of split-theorems that should be tried.
   179           The elements have the format (key,(thm,T,n)) , where
   180           key : the theorem's key constant ( Const(key,...) $ ... )
   181           thm : the theorem itself
   182           T   : type of P( Const(key,...) $ ... )
   183           n   : number of arguments expected by Const(key,...)
   184    Ts   : types of parameters
   185    t    : the term to be scanned
   186 ******************************************************************************)
   187 
   188 fun split_posns cmap Ts t =
   189   let fun posns Ts pos apsns (Abs(_,T,t)) =
   190             let val U = fastype_of1(T::Ts,t)
   191             in posns (T::Ts) (0::pos) ((T,U,pos)::apsns) t end
   192         | posns Ts pos apsns t =
   193             let val (h,ts) = strip_comb t
   194                 fun iter((i,a),t) = (i+1, (posns Ts (i::pos) apsns t) @ a);
   195                 val a = case h of
   196                   Const(c,_) =>
   197                     (case assoc(cmap,c) of
   198                        Some(thm, T, n) =>
   199                          let val t2 = list_comb (h, take (n, ts)) in
   200                            mk_split_pack(thm,T,n,ts,apsns,pos,type_of1(Ts, t2),t2)
   201                          end
   202                      | None => [])
   203                 | _ => []
   204              in snd(foldl iter ((0,a),ts)) end
   205   in posns Ts [] [] t end;
   206 
   207 
   208 fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
   209 
   210 fun shorter((_,ps,pos,_,_),(_,qs,qos,_,_)) =
   211   prod_ord (int_ord o pairself length) (order o pairself length)
   212     ((ps, pos), (qs, qos));
   213 
   214 
   215 
   216 (************************************************************
   217    call split_posns with appropriate parameters
   218 *************************************************************)
   219 
   220 fun select cmap state i =
   221   let val goali = nth_subgoal i state
   222       val Ts = rev(map #2 (Logic.strip_params goali))
   223       val _ $ t $ _ = Logic.strip_assums_concl goali;
   224   in (Ts,t, sort shorter (split_posns cmap Ts t)) end;
   225 
   226 
   227 (*************************************************************
   228    instantiate lift theorem
   229 
   230    if t is of the form
   231    ... ( Const(...,...) $ Abs( .... ) ) ...
   232    then
   233    P = %a.  ... ( Const(...,...) $ a ) ...
   234    where a has type T --> U
   235 
   236    Ts      : types of parameters
   237    t       : lefthand side of meta-equality in subgoal
   238              the split theorem is applied to (see cmap)
   239    T,U,pos : see mk_split_pack
   240    state   : current proof state
   241    lift    : the lift theorem
   242    i       : no. of subgoal
   243 **************************************************************)
   244 
   245 fun inst_lift Ts t (T,U,pos) state lift i =
   246   let val sg = #sign(rep_thm state)
   247       val tsig = #tsig(Sign.rep_sg sg)
   248       val cntxt = mk_cntxt Ts t pos (T-->U) (#maxidx(rep_thm lift))
   249       val cu = cterm_of sg cntxt
   250       val uT = #T(rep_cterm cu)
   251       val cP' = cterm_of sg (Var(P,uT))
   252       val ixnTs = Type.typ_match tsig ([],(PT,uT));
   253       val ixncTs = map (fn (x,y) => (x,ctyp_of sg y)) ixnTs;
   254   in instantiate (ixncTs, [(cP',cu)]) lift end;
   255 
   256 
   257 (*************************************************************
   258    instantiate split theorem
   259 
   260    Ts    : types of parameters
   261    t     : lefthand side of meta-equality in subgoal
   262            the split theorem is applied to (see cmap)
   263    tt    : the term  Const(key,..) $ ...
   264    thm   : the split theorem
   265    TB    : type of body of P(...)
   266    state : current proof state
   267    i     : number of subgoal
   268 **************************************************************)
   269 
   270 fun inst_split Ts t tt thm TB state i =
   271   let val _ $ ((Var (P2, PT2)) $ _) $ _ = concl_of thm;
   272       val sg = #sign(rep_thm state)
   273       val tsig = #tsig(Sign.rep_sg sg)
   274       val cntxt = mk_cntxt_splitthm t tt TB;
   275       val T = fastype_of1 (Ts, cntxt);
   276       val ixnTs = Type.typ_match tsig ([],(PT2, T))
   277       val abss = foldl (fn (t, T) => Abs ("", T, t))
   278   in
   279     term_lift_inst_rule (state, i, ixnTs, [((P2, T), abss (cntxt, Ts))], thm)
   280   end;
   281 
   282 (*****************************************************************************
   283    The split-tactic
   284    
   285    splits : list of split-theorems to be tried
   286    i      : number of subgoal the tactic should be applied to
   287 *****************************************************************************)
   288 
   289 fun split_tac [] i = no_tac
   290   | split_tac splits i =
   291   let val splits = map Data.mk_meta_eq splits;
   292       fun const(thm) =
   293             (case concl_of thm of _$(t as _$lhs)$_ =>
   294                (case strip_comb lhs of (Const(a,_),args) =>
   295                   (a,(thm,fastype_of t,length args))
   296                 | _ => split_format_err())
   297              | _ => split_format_err())
   298       val cmap = map const splits;
   299       fun lift_tac Ts t p st = (rtac (inst_lift Ts t p st trlift i) i) st
   300       fun lift_split_tac st = st |>
   301             let val (Ts,t,splits) = select cmap st i
   302             in case splits of
   303                  [] => no_tac
   304                | (thm,apsns,pos,TB,tt)::_ =>
   305                    (case apsns of
   306                       [] => (fn state => state |>
   307 			           compose_tac (false, inst_split Ts t tt thm TB state i, 0) i)
   308                     | p::_ => EVERY[lift_tac Ts t p,
   309                                     rtac reflexive_thm (i+1),
   310                                     lift_split_tac])
   311             end
   312   in COND (has_fewer_prems i) no_tac 
   313           (rtac meta_iffD i THEN lift_split_tac)
   314   end;
   315 
   316 in split_tac end;
   317 
   318 
   319 val split_tac        = mk_case_split_tac              int_ord;
   320 
   321 val split_inside_tac = mk_case_split_tac (rev_order o int_ord);
   322 
   323 
   324 (*****************************************************************************
   325    The split-tactic for premises
   326    
   327    splits : list of split-theorems to be tried
   328 ****************************************************************************)
   329 fun split_asm_tac []     = K no_tac
   330   | split_asm_tac splits = 
   331 
   332   let val cname_list = map (fst o split_thm_info) splits;
   333       fun is_case (a,_) = a mem cname_list;
   334       fun tac (t,i) = 
   335 	  let val n = find_index (exists_Const is_case) 
   336 				 (Logic.strip_assums_hyp t);
   337 	      fun first_prem_is_disj (Const ("==>", _) $ (Const ("Trueprop", _)
   338 				 $ (Const (s, _) $ _ $ _ )) $ _ ) = (s=const_or)
   339 	      |   first_prem_is_disj (Const("all",_)$Abs(_,_,t)) = 
   340 					first_prem_is_disj t
   341 	      |   first_prem_is_disj _ = false;
   342 	      fun flat_prems_tac i = SUBGOAL (fn (t,i) => 
   343 			   (if first_prem_is_disj t
   344 			    then EVERY[etac Data.disjE i,rotate_tac ~1 i,
   345 				       rotate_tac ~1  (i+1),
   346 				       flat_prems_tac (i+1)]
   347 			    else all_tac) 
   348 			   THEN REPEAT (eresolve_tac [Data.conjE,Data.exE] i)
   349 			   THEN REPEAT (dresolve_tac [Data.notnotD]   i)) i;
   350 	  in if n<0 then no_tac else DETERM (EVERY'
   351 		[rotate_tac n, etac Data.contrapos2,
   352 		 split_tac splits, 
   353 		 rotate_tac ~1, etac Data.contrapos, rotate_tac ~1, 
   354 		 flat_prems_tac] i)
   355 	  end;
   356   in SUBGOAL tac
   357   end;
   358 
   359 fun split_name name asm = "split " ^ name ^ (if asm then " asm" else "");
   360 
   361 fun ss addsplits splits =
   362   let fun addsplit (ss,split) =
   363         let val (name,asm) = split_thm_info split
   364         in Data.Simplifier.addloop(ss,(split_name name asm,
   365 		       (if asm then split_asm_tac else split_tac) [split])) end
   366   in foldl addsplit (ss,splits) end;
   367 
   368 fun ss delsplits splits =
   369   let fun delsplit(ss,split) =
   370         let val (name,asm) = split_thm_info split
   371         in Data.Simplifier.delloop(ss,split_name name asm)
   372   end in foldl delsplit (ss,splits) end;
   373 
   374 fun Addsplits splits = (Data.Simplifier.simpset_ref() := 
   375 			Data.Simplifier.simpset() addsplits splits);
   376 fun Delsplits splits = (Data.Simplifier.simpset_ref() := 
   377 			Data.Simplifier.simpset() delsplits splits);
   378 
   379 end;