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