src/Provers/splitter.ML
author nipkow
Fri May 17 11:25:07 2002 +0200 (2002-05-17)
changeset 13157 4a4599f78f18
parent 10821 dcb75538f542
child 13855 644692eca537
permissions -rw-r--r--
allowed more general split rules to cope with div/mod 2
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@13157
     7
Deals with equalities of the form ?P(f args) = ...
nipkow@13157
     8
where "f args" must be a first-order term without duplicate variables.
clasohm@0
     9
*)
clasohm@0
    10
oheimb@5304
    11
infix 4 addsplits delsplits;
oheimb@5304
    12
oheimb@5304
    13
signature SPLITTER_DATA =
oheimb@5304
    14
sig
oheimb@5304
    15
  structure Simplifier: SIMPLIFIER
oheimb@5553
    16
  val mk_eq         : thm -> thm
oheimb@5304
    17
  val meta_eq_to_iff: thm (* "x == y ==> x = y"                    *)
oheimb@5304
    18
  val iffD          : thm (* "[| P = Q; Q |] ==> P"                *)
oheimb@5304
    19
  val disjE         : thm (* "[| P | Q; P ==> R; Q ==> R |] ==> R" *)
oheimb@5304
    20
  val conjE         : thm (* "[| P & Q; [| P; Q |] ==> R |] ==> R" *)
oheimb@5304
    21
  val exE           : thm (* "[|  x. P x; !!x. P x ==> Q |] ==> Q" *)
oheimb@5304
    22
  val contrapos     : thm (* "[| ~ Q; P ==> Q |] ==> ~ P"          *)
oheimb@5304
    23
  val contrapos2    : thm (* "[| Q; ~ P ==> ~ Q |] ==> P"          *)
oheimb@5304
    24
  val notnotD       : thm (* "~ ~ P ==> P"                         *)
oheimb@5304
    25
end
oheimb@5304
    26
oheimb@5304
    27
signature SPLITTER =
oheimb@5304
    28
sig
oheimb@5304
    29
  type simpset
oheimb@5304
    30
  val split_tac       : thm list -> int -> tactic
oheimb@5304
    31
  val split_inside_tac: thm list -> int -> tactic
oheimb@5304
    32
  val split_asm_tac   : thm list -> int -> tactic
oheimb@5304
    33
  val addsplits       : simpset * thm list -> simpset
oheimb@5304
    34
  val delsplits       : simpset * thm list -> simpset
oheimb@5304
    35
  val Addsplits       : thm list -> unit
oheimb@5304
    36
  val Delsplits       : thm list -> unit
wenzelm@8468
    37
  val split_add_global: theory attribute
wenzelm@8468
    38
  val split_del_global: theory attribute
wenzelm@8468
    39
  val split_add_local: Proof.context attribute
wenzelm@8468
    40
  val split_del_local: Proof.context attribute
wenzelm@8468
    41
  val split_modifiers : (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@8468
    42
  val setup: (theory -> theory) list
oheimb@5304
    43
end;
oheimb@5304
    44
oheimb@5304
    45
functor SplitterFun(Data: SPLITTER_DATA): SPLITTER =
oheimb@5304
    46
struct 
oheimb@5304
    47
wenzelm@8468
    48
structure Simplifier = Data.Simplifier;
wenzelm@8468
    49
type simpset = Simplifier.simpset;
oheimb@5304
    50
oheimb@5304
    51
val Const ("==>", _) $ (Const ("Trueprop", _) $
oheimb@5304
    52
         (Const (const_not, _) $ _    )) $ _ = #prop (rep_thm(Data.notnotD));
oheimb@5304
    53
oheimb@5304
    54
val Const ("==>", _) $ (Const ("Trueprop", _) $
oheimb@5304
    55
         (Const (const_or , _) $ _ $ _)) $ _ = #prop (rep_thm(Data.disjE));
berghofe@1721
    56
nipkow@4668
    57
fun split_format_err() = error("Wrong format for split rule");
nipkow@4668
    58
oheimb@5553
    59
fun split_thm_info thm = case concl_of (Data.mk_eq thm) of
oheimb@5304
    60
     Const("==", _)$(Var _$t)$c =>
oheimb@5304
    61
        (case strip_comb t of
oheimb@5304
    62
           (Const(a,_),_) => (a,case c of (Const(s,_)$_)=>s=const_not|_=> false)
oheimb@5304
    63
         | _              => split_format_err())
oheimb@5304
    64
   | _ => split_format_err();
oheimb@5304
    65
oheimb@5304
    66
fun mk_case_split_tac order =
clasohm@0
    67
let
clasohm@0
    68
berghofe@1686
    69
berghofe@1686
    70
(************************************************************
berghofe@1686
    71
   Create lift-theorem "trlift" :
berghofe@1686
    72
berghofe@7672
    73
   [| !!x. Q x == R x; P(%x. R x) == C |] ==> P (%x. Q x) == C
berghofe@1686
    74
berghofe@1686
    75
*************************************************************)
oheimb@5304
    76
oheimb@5304
    77
val meta_iffD = Data.meta_eq_to_iff RS Data.iffD;
nipkow@943
    78
val lift =
oheimb@5304
    79
  let val ct = read_cterm (#sign(rep_thm Data.iffD))
berghofe@10411
    80
           ("[| !!x. (Q::('b::logic)=>('c::logic))(x) == R(x) |] ==> \
wenzelm@3835
    81
            \P(%x. Q(x)) == P(%x. R(x))::'a::logic",propT)
nipkow@943
    82
  in prove_goalw_cterm [] ct
nipkow@943
    83
     (fn [prem] => [rewtac prem, rtac reflexive_thm 1])
nipkow@943
    84
  end;
nipkow@4
    85
clasohm@0
    86
val trlift = lift RS transitive_thm;
berghofe@7672
    87
val _ $ (P $ _) $ _ = concl_of trlift;
clasohm@0
    88
clasohm@0
    89
berghofe@1686
    90
(************************************************************************ 
berghofe@1686
    91
   Set up term for instantiation of P in the lift-theorem
berghofe@1686
    92
   
berghofe@1686
    93
   Ts    : types of parameters (i.e. variables bound by meta-quantifiers)
berghofe@1686
    94
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
    95
           the lift theorem is applied to (see select)
berghofe@1686
    96
   pos   : "path" leading to abstraction, coded as a list
berghofe@1686
    97
   T     : type of body of P(...)
berghofe@1686
    98
   maxi  : maximum index of Vars
berghofe@1686
    99
*************************************************************************)
berghofe@1686
   100
nipkow@1030
   101
fun mk_cntxt Ts t pos T maxi =
nipkow@1030
   102
  let fun var (t,i) = Var(("X",i),type_of1(Ts,t));
nipkow@1030
   103
      fun down [] t i = Bound 0
nipkow@1030
   104
        | down (p::ps) t i =
nipkow@1030
   105
            let val (h,ts) = strip_comb t
paulson@2266
   106
                val v1 = ListPair.map var (take(p,ts), i upto (i+p-1))
nipkow@1030
   107
                val u::us = drop(p,ts)
paulson@2266
   108
                val v2 = ListPair.map var (us, (i+p) upto (i+length(ts)-2))
nipkow@1030
   109
      in list_comb(h,v1@[down ps u (i+length ts)]@v2) end;
nipkow@1030
   110
  in Abs("", T, down (rev pos) t maxi) end;
nipkow@1030
   111
berghofe@1686
   112
berghofe@1686
   113
(************************************************************************ 
berghofe@1686
   114
   Set up term for instantiation of P in the split-theorem
berghofe@1686
   115
   P(...) == rhs
berghofe@1686
   116
berghofe@1686
   117
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
   118
           the split theorem is applied to (see select)
berghofe@1686
   119
   T     : type of body of P(...)
berghofe@4232
   120
   tt    : the term  Const(key,..) $ ...
berghofe@1686
   121
*************************************************************************)
berghofe@1686
   122
berghofe@4232
   123
fun mk_cntxt_splitthm t tt T =
berghofe@4232
   124
  let fun repl lev t =
berghofe@7672
   125
    if incr_boundvars lev tt aconv t then Bound lev
berghofe@4232
   126
    else case t of
berghofe@4232
   127
        (Abs (v, T2, t)) => Abs (v, T2, repl (lev+1) t)
berghofe@4232
   128
      | (Bound i) => Bound (if i>=lev then i+1 else i)
berghofe@4232
   129
      | (t1 $ t2) => (repl lev t1) $ (repl lev t2)
berghofe@4232
   130
      | t => t
berghofe@4232
   131
  in Abs("", T, repl 0 t) end;
berghofe@1686
   132
berghofe@1686
   133
berghofe@1686
   134
(* add all loose bound variables in t to list is *)
nipkow@1030
   135
fun add_lbnos(is,t) = add_loose_bnos(t,0,is);
nipkow@1030
   136
berghofe@7672
   137
(* check if the innermost abstraction that needs to be removed
nipkow@1064
   138
   has a body of type T; otherwise the expansion thm will fail later on
nipkow@1064
   139
*)
nipkow@1064
   140
fun type_test(T,lbnos,apsns) =
paulson@2143
   141
  let val (_,U,_) = nth_elem(foldl Int.min (hd lbnos, tl lbnos), apsns)
nipkow@1064
   142
  in T=U end;
clasohm@0
   143
berghofe@1686
   144
(*************************************************************************
berghofe@1686
   145
   Create a "split_pack".
berghofe@1686
   146
berghofe@1686
   147
   thm   : the relevant split-theorem, i.e. P(...) == rhs , where P(...)
berghofe@1686
   148
           is of the form
berghofe@1686
   149
           P( Const(key,...) $ t_1 $ ... $ t_n )      (e.g. key = "if")
berghofe@1686
   150
   T     : type of P(...)
berghofe@7672
   151
   T'    : type of term to be scanned
berghofe@1686
   152
   n     : number of arguments expected by Const(key,...)
berghofe@1686
   153
   ts    : list of arguments actually found
berghofe@1686
   154
   apsns : list of tuples of the form (T,U,pos), one tuple for each
berghofe@1686
   155
           abstraction that is encountered on the way to the position where 
berghofe@1686
   156
           Const(key, ...) $ ...  occurs, where
berghofe@1686
   157
           T   : type of the variable bound by the abstraction
berghofe@1686
   158
           U   : type of the abstraction's body
berghofe@1686
   159
           pos : "path" leading to the body of the abstraction
berghofe@1686
   160
   pos   : "path" leading to the position where Const(key, ...) $ ...  occurs.
berghofe@1686
   161
   TB    : type of  Const(key,...) $ t_1 $ ... $ t_n
berghofe@1721
   162
   t     : the term Const(key,...) $ t_1 $ ... $ t_n
berghofe@1686
   163
berghofe@1686
   164
   A split pack is a tuple of the form
berghofe@7672
   165
   (thm, apsns, pos, TB, tt)
berghofe@1686
   166
   Note : apsns is reversed, so that the outermost quantifier's position
berghofe@1686
   167
          comes first ! If the terms in ts don't contain variables bound
berghofe@1686
   168
          by other than meta-quantifiers, apsns is empty, because no further
berghofe@1686
   169
          lifting is required before applying the split-theorem.
berghofe@1686
   170
******************************************************************************) 
berghofe@1686
   171
berghofe@7672
   172
fun mk_split_pack(thm, T, T', n, ts, apsns, pos, TB, t) =
nipkow@1064
   173
  if n > length ts then []
nipkow@1064
   174
  else let val lev = length apsns
nipkow@1030
   175
           val lbnos = foldl add_lbnos ([],take(n,ts))
nipkow@1030
   176
           val flbnos = filter (fn i => i < lev) lbnos
berghofe@4232
   177
           val tt = incr_boundvars (~lev) t
berghofe@7672
   178
       in if null flbnos then
berghofe@7672
   179
            if T = T' then [(thm,[],pos,TB,tt)] else []
berghofe@7672
   180
          else if type_test(T,flbnos,apsns) then [(thm, rev apsns,pos,TB,tt)]
paulson@2143
   181
               else []
nipkow@1064
   182
       end;
clasohm@0
   183
berghofe@1686
   184
berghofe@1686
   185
(****************************************************************************
berghofe@1686
   186
   Recursively scans term for occurences of Const(key,...) $ ...
berghofe@1686
   187
   Returns a list of "split-packs" (one for each occurence of Const(key,...) )
berghofe@1686
   188
berghofe@1686
   189
   cmap : association list of split-theorems that should be tried.
berghofe@1686
   190
          The elements have the format (key,(thm,T,n)) , where
berghofe@1686
   191
          key : the theorem's key constant ( Const(key,...) $ ... )
berghofe@1686
   192
          thm : the theorem itself
berghofe@1686
   193
          T   : type of P( Const(key,...) $ ... )
berghofe@1686
   194
          n   : number of arguments expected by Const(key,...)
berghofe@1686
   195
   Ts   : types of parameters
berghofe@1686
   196
   t    : the term to be scanned
berghofe@1686
   197
******************************************************************************)
berghofe@1686
   198
nipkow@13157
   199
(* Simplified first-order matching;
nipkow@13157
   200
   assumes that all Vars in the pattern are distinct;
nipkow@13157
   201
   see Pure/pattern.ML for the full version;
nipkow@13157
   202
*)
nipkow@13157
   203
local
nipkow@13157
   204
exception MATCH
nipkow@13157
   205
in
nipkow@13157
   206
fun typ_match tsig args = (Type.typ_match tsig args)
nipkow@13157
   207
                          handle Type.TYPE_MATCH => raise MATCH;
nipkow@13157
   208
fun fomatch tsig args =
nipkow@13157
   209
  let
nipkow@13157
   210
    fun mtch tyinsts = fn
nipkow@13157
   211
        (Ts,Var(_,T), t)  => typ_match tsig (tyinsts, (T, fastype_of1(Ts,t)))
nipkow@13157
   212
      | (_,Free (a,T), Free (b,U)) =>
nipkow@13157
   213
          if a=b then typ_match tsig (tyinsts,(T,U)) else raise MATCH
nipkow@13157
   214
      | (_,Const (a,T), Const (b,U))  =>
nipkow@13157
   215
          if a=b then typ_match tsig (tyinsts,(T,U)) else raise MATCH
nipkow@13157
   216
      | (_,Bound i, Bound j)  =>  if  i=j  then tyinsts else raise MATCH
nipkow@13157
   217
      | (Ts,Abs(_,T,t), Abs(_,U,u))  =>
nipkow@13157
   218
          mtch (typ_match tsig (tyinsts,(T,U))) (U::Ts,t,u)
nipkow@13157
   219
      | (Ts, f$t, g$u) => mtch (mtch tyinsts (Ts,f,g)) (Ts, t, u)
nipkow@13157
   220
      | _ => raise MATCH
nipkow@13157
   221
  in (mtch Vartab.empty args; true) handle MATCH => false end;
nipkow@13157
   222
end
nipkow@13157
   223
nipkow@6130
   224
fun split_posns cmap sg Ts t =
nipkow@6130
   225
  let
berghofe@7672
   226
    val T' = fastype_of1 (Ts, t);
berghofe@7672
   227
    fun posns Ts pos apsns (Abs (_, T, t)) =
berghofe@7672
   228
          let val U = fastype_of1 (T::Ts,t)
berghofe@7672
   229
          in posns (T::Ts) (0::pos) ((T, U, pos)::apsns) t end
nipkow@6130
   230
      | posns Ts pos apsns t =
nipkow@6130
   231
          let
berghofe@7672
   232
            val (h, ts) = strip_comb t
berghofe@7672
   233
            fun iter((i, a), t) = (i+1, (posns Ts (i::pos) apsns t) @ a);
nipkow@6130
   234
            val a = case h of
berghofe@7672
   235
              Const(c, cT) =>
nipkow@9267
   236
                let fun find [] = []
nipkow@13157
   237
                      | find ((gcT, pat, thm, T, n)::tups) =
nipkow@13157
   238
                          let val t2 = list_comb (h, take (n, ts))
nipkow@13157
   239
                          in if Sign.typ_instance sg (cT, gcT)
nipkow@13157
   240
                                andalso fomatch (Sign.tsig_of sg) (Ts,pat,t2)
nipkow@13157
   241
                             then mk_split_pack(thm,T,T',n,ts,apsns,pos,type_of1(Ts,t2),t2)
nipkow@13157
   242
                             else find tups
nipkow@13157
   243
                          end
nipkow@9267
   244
                in find (assocs cmap c) end
nipkow@6130
   245
            | _ => []
berghofe@7672
   246
          in snd(foldl iter ((0, a), ts)) end
nipkow@1030
   247
  in posns Ts [] [] t end;
clasohm@0
   248
berghofe@1686
   249
clasohm@0
   250
fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
clasohm@0
   251
berghofe@1721
   252
fun shorter((_,ps,pos,_,_),(_,qs,qos,_,_)) =
wenzelm@4519
   253
  prod_ord (int_ord o pairself length) (order o pairself length)
wenzelm@4519
   254
    ((ps, pos), (qs, qos));
wenzelm@4519
   255
berghofe@1686
   256
berghofe@1686
   257
berghofe@1686
   258
(************************************************************
berghofe@1686
   259
   call split_posns with appropriate parameters
berghofe@1686
   260
*************************************************************)
clasohm@0
   261
nipkow@1030
   262
fun select cmap state i =
nipkow@6130
   263
  let val sg = #sign(rep_thm state)
nipkow@6130
   264
      val goali = nth_subgoal i state
nipkow@1030
   265
      val Ts = rev(map #2 (Logic.strip_params goali))
nipkow@1030
   266
      val _ $ t $ _ = Logic.strip_assums_concl goali;
nipkow@6130
   267
  in (Ts,t, sort shorter (split_posns cmap sg Ts t)) end;
nipkow@1030
   268
berghofe@1686
   269
berghofe@1686
   270
(*************************************************************
berghofe@1686
   271
   instantiate lift theorem
berghofe@1686
   272
berghofe@1686
   273
   if t is of the form
berghofe@1686
   274
   ... ( Const(...,...) $ Abs( .... ) ) ...
berghofe@1686
   275
   then
berghofe@1686
   276
   P = %a.  ... ( Const(...,...) $ a ) ...
berghofe@1686
   277
   where a has type T --> U
berghofe@1686
   278
berghofe@1686
   279
   Ts      : types of parameters
berghofe@1686
   280
   t       : lefthand side of meta-equality in subgoal
berghofe@1686
   281
             the split theorem is applied to (see cmap)
berghofe@1686
   282
   T,U,pos : see mk_split_pack
berghofe@1686
   283
   state   : current proof state
berghofe@1686
   284
   lift    : the lift theorem
berghofe@1686
   285
   i       : no. of subgoal
berghofe@1686
   286
**************************************************************)
berghofe@1686
   287
berghofe@7672
   288
fun inst_lift Ts t (T, U, pos) state i =
berghofe@7672
   289
  let
berghofe@7672
   290
    val cert = cterm_of (sign_of_thm state);
berghofe@7672
   291
    val cntxt = mk_cntxt Ts t pos (T --> U) (#maxidx(rep_thm trlift));    
berghofe@7672
   292
  in cterm_instantiate [(cert P, cert cntxt)] trlift
berghofe@7672
   293
  end;
clasohm@0
   294
clasohm@0
   295
berghofe@1686
   296
(*************************************************************
berghofe@1686
   297
   instantiate split theorem
berghofe@1686
   298
berghofe@1686
   299
   Ts    : types of parameters
berghofe@1686
   300
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
   301
           the split theorem is applied to (see cmap)
berghofe@4232
   302
   tt    : the term  Const(key,..) $ ...
berghofe@1686
   303
   thm   : the split theorem
berghofe@1686
   304
   TB    : type of body of P(...)
berghofe@1686
   305
   state : current proof state
berghofe@4232
   306
   i     : number of subgoal
berghofe@1686
   307
**************************************************************)
berghofe@1686
   308
berghofe@4232
   309
fun inst_split Ts t tt thm TB state i =
berghofe@7672
   310
  let 
berghofe@7672
   311
    val thm' = Thm.lift_rule (state, i) thm;
berghofe@7672
   312
    val (P, _) = strip_comb (fst (Logic.dest_equals
berghofe@7672
   313
      (Logic.strip_assums_concl (#prop (rep_thm thm')))));
berghofe@7672
   314
    val cert = cterm_of (sign_of_thm state);
berghofe@7672
   315
    val cntxt = mk_cntxt_splitthm t tt TB;
berghofe@7672
   316
    val abss = foldl (fn (t, T) => Abs ("", T, t));
berghofe@7672
   317
  in cterm_instantiate [(cert P, cert (abss (cntxt, Ts)))] thm'
berghofe@4232
   318
  end;
berghofe@1686
   319
berghofe@7672
   320
berghofe@1686
   321
(*****************************************************************************
berghofe@1686
   322
   The split-tactic
berghofe@1686
   323
   
berghofe@1686
   324
   splits : list of split-theorems to be tried
berghofe@1686
   325
   i      : number of subgoal the tactic should be applied to
berghofe@1686
   326
*****************************************************************************)
berghofe@1686
   327
clasohm@0
   328
fun split_tac [] i = no_tac
clasohm@0
   329
  | split_tac splits i =
oheimb@5553
   330
  let val splits = map Data.mk_eq splits;
nipkow@9267
   331
      fun add_thm(cmap,thm) =
nipkow@3918
   332
            (case concl_of thm of _$(t as _$lhs)$_ =>
nipkow@6130
   333
               (case strip_comb lhs of (Const(a,aT),args) =>
nipkow@13157
   334
                  let val info = (aT,lhs,thm,fastype_of t,length args)
nipkow@9267
   335
                  in case assoc(cmap,a) of
nipkow@9267
   336
                       Some infos => overwrite(cmap,(a,info::infos))
nipkow@9267
   337
                     | None => (a,[info])::cmap
nipkow@9267
   338
                  end
nipkow@4668
   339
                | _ => split_format_err())
nipkow@4668
   340
             | _ => split_format_err())
nipkow@9267
   341
      val cmap = foldl add_thm ([],splits);
berghofe@7672
   342
      fun lift_tac Ts t p st = rtac (inst_lift Ts t p st i) i st
berghofe@7672
   343
      fun lift_split_tac state =
berghofe@7672
   344
            let val (Ts, t, splits) = select cmap state i
nipkow@1030
   345
            in case splits of
berghofe@7672
   346
                 [] => no_tac state
berghofe@7672
   347
               | (thm, apsns, pos, TB, tt)::_ =>
nipkow@1030
   348
                   (case apsns of
berghofe@7672
   349
                      [] => compose_tac (false, inst_split Ts t tt thm TB state i, 0) i state
berghofe@7672
   350
                    | p::_ => EVERY [lift_tac Ts t p,
berghofe@7672
   351
                                     rtac reflexive_thm (i+1),
berghofe@7672
   352
                                     lift_split_tac] state)
nipkow@1030
   353
            end
paulson@3537
   354
  in COND (has_fewer_prems i) no_tac 
oheimb@5304
   355
          (rtac meta_iffD i THEN lift_split_tac)
clasohm@0
   356
  end;
clasohm@0
   357
clasohm@0
   358
in split_tac end;
berghofe@1721
   359
oheimb@5304
   360
oheimb@5304
   361
val split_tac        = mk_case_split_tac              int_ord;
oheimb@4189
   362
oheimb@5304
   363
val split_inside_tac = mk_case_split_tac (rev_order o int_ord);
oheimb@5304
   364
oheimb@4189
   365
oheimb@4189
   366
(*****************************************************************************
oheimb@4189
   367
   The split-tactic for premises
oheimb@4189
   368
   
oheimb@4189
   369
   splits : list of split-theorems to be tried
oheimb@5304
   370
****************************************************************************)
oheimb@4202
   371
fun split_asm_tac []     = K no_tac
oheimb@4202
   372
  | split_asm_tac splits = 
oheimb@5304
   373
oheimb@4930
   374
  let val cname_list = map (fst o split_thm_info) splits;
oheimb@4189
   375
      fun is_case (a,_) = a mem cname_list;
oheimb@4189
   376
      fun tac (t,i) = 
oheimb@4189
   377
	  let val n = find_index (exists_Const is_case) 
oheimb@4189
   378
				 (Logic.strip_assums_hyp t);
oheimb@4189
   379
	      fun first_prem_is_disj (Const ("==>", _) $ (Const ("Trueprop", _)
oheimb@5304
   380
				 $ (Const (s, _) $ _ $ _ )) $ _ ) = (s=const_or)
oheimb@4202
   381
	      |   first_prem_is_disj (Const("all",_)$Abs(_,_,t)) = 
oheimb@4202
   382
					first_prem_is_disj t
oheimb@4189
   383
	      |   first_prem_is_disj _ = false;
oheimb@5437
   384
      (* does not work properly if the split variable is bound by a quantfier *)
oheimb@4202
   385
	      fun flat_prems_tac i = SUBGOAL (fn (t,i) => 
oheimb@5304
   386
			   (if first_prem_is_disj t
oheimb@5304
   387
			    then EVERY[etac Data.disjE i,rotate_tac ~1 i,
oheimb@5304
   388
				       rotate_tac ~1  (i+1),
oheimb@5304
   389
				       flat_prems_tac (i+1)]
oheimb@5304
   390
			    else all_tac) 
oheimb@5304
   391
			   THEN REPEAT (eresolve_tac [Data.conjE,Data.exE] i)
oheimb@5304
   392
			   THEN REPEAT (dresolve_tac [Data.notnotD]   i)) i;
oheimb@4189
   393
	  in if n<0 then no_tac else DETERM (EVERY'
oheimb@5304
   394
		[rotate_tac n, etac Data.contrapos2,
oheimb@4189
   395
		 split_tac splits, 
oheimb@5304
   396
		 rotate_tac ~1, etac Data.contrapos, rotate_tac ~1, 
oheimb@4202
   397
		 flat_prems_tac] i)
oheimb@4189
   398
	  end;
oheimb@4189
   399
  in SUBGOAL tac
oheimb@4189
   400
  end;
oheimb@4189
   401
nipkow@10652
   402
fun gen_split_tac [] = K no_tac
nipkow@10652
   403
  | gen_split_tac (split::splits) =
nipkow@10652
   404
      let val (_,asm) = split_thm_info split
nipkow@10652
   405
      in (if asm then split_asm_tac else split_tac) [split] ORELSE'
nipkow@10652
   406
         gen_split_tac splits
nipkow@10652
   407
      end;
wenzelm@8468
   408
wenzelm@8468
   409
(** declare split rules **)
wenzelm@8468
   410
wenzelm@8468
   411
(* addsplits / delsplits *)
wenzelm@8468
   412
oheimb@5304
   413
fun split_name name asm = "split " ^ name ^ (if asm then " asm" else "");
oheimb@4189
   414
oheimb@5304
   415
fun ss addsplits splits =
oheimb@5304
   416
  let fun addsplit (ss,split) =
oheimb@5304
   417
        let val (name,asm) = split_thm_info split
wenzelm@8468
   418
        in Simplifier.addloop(ss,(split_name name asm,
oheimb@5304
   419
		       (if asm then split_asm_tac else split_tac) [split])) end
oheimb@5304
   420
  in foldl addsplit (ss,splits) end;
berghofe@1721
   421
oheimb@5304
   422
fun ss delsplits splits =
oheimb@5304
   423
  let fun delsplit(ss,split) =
oheimb@5304
   424
        let val (name,asm) = split_thm_info split
wenzelm@8468
   425
        in Simplifier.delloop(ss,split_name name asm)
oheimb@5304
   426
  end in foldl delsplit (ss,splits) end;
berghofe@1721
   427
wenzelm@8468
   428
fun Addsplits splits = (Simplifier.simpset_ref() := 
wenzelm@8468
   429
			Simplifier.simpset() addsplits splits);
wenzelm@8468
   430
fun Delsplits splits = (Simplifier.simpset_ref() := 
wenzelm@8468
   431
			Simplifier.simpset() delsplits splits);
wenzelm@8468
   432
wenzelm@8468
   433
wenzelm@8468
   434
(* attributes *)
wenzelm@8468
   435
wenzelm@8468
   436
val splitN = "split";
wenzelm@8468
   437
wenzelm@8468
   438
val split_add_global = Simplifier.change_global_ss (op addsplits);
wenzelm@8468
   439
val split_del_global = Simplifier.change_global_ss (op delsplits);
wenzelm@8468
   440
val split_add_local = Simplifier.change_local_ss (op addsplits);
wenzelm@8468
   441
val split_del_local = Simplifier.change_local_ss (op delsplits);
wenzelm@8468
   442
wenzelm@8634
   443
val split_attr =
wenzelm@8634
   444
 (Attrib.add_del_args split_add_global split_del_global,
wenzelm@8634
   445
  Attrib.add_del_args split_add_local split_del_local);
wenzelm@8634
   446
wenzelm@8634
   447
wenzelm@9703
   448
(* methods *)
wenzelm@8468
   449
wenzelm@8468
   450
val split_modifiers =
wenzelm@8815
   451
 [Args.$$$ splitN -- Args.colon >> K ((I, split_add_local): Method.modifier),
wenzelm@10034
   452
  Args.$$$ splitN -- Args.add -- Args.colon >> K (I, split_add_local),
wenzelm@10034
   453
  Args.$$$ splitN -- Args.del -- Args.colon >> K (I, split_del_local)];
wenzelm@8468
   454
nipkow@10652
   455
val split_args = #2 oo Method.syntax Attrib.local_thms;
wenzelm@9807
   456
wenzelm@10821
   457
fun split_meth ths = Method.SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o gen_split_tac ths);
wenzelm@9703
   458
wenzelm@8468
   459
wenzelm@8468
   460
wenzelm@8468
   461
(** theory setup **)
wenzelm@8468
   462
wenzelm@9703
   463
val setup =
wenzelm@9900
   464
 [Attrib.add_attributes [(splitN, split_attr, "declaration of case split rule")],
wenzelm@9900
   465
  Method.add_methods [(splitN, split_meth oo split_args, "apply case split rule")]];
oheimb@4189
   466
berghofe@1721
   467
end;