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