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