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