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