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