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