src/Provers/splitter.ML
author wenzelm
Sat Dec 14 17:28:05 2013 +0100 (2013-12-14)
changeset 54742 7a86358a3c0b
parent 54216 c0c453ce70a7
child 56245 84fc7dfa3cd4
permissions -rw-r--r--
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
clarified tool context in some boundary cases;
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
signature SPLITTER_DATA =
oheimb@5304
    11
sig
wenzelm@32177
    12
  val thy           : theory
oheimb@5553
    13
  val mk_eq         : thm -> thm
webertj@20217
    14
  val meta_eq_to_iff: thm (* "x == y ==> x = y"                      *)
webertj@20217
    15
  val iffD          : thm (* "[| P = Q; Q |] ==> P"                  *)
webertj@20217
    16
  val disjE         : thm (* "[| P | Q; P ==> R; Q ==> R |] ==> R"   *)
webertj@20217
    17
  val conjE         : thm (* "[| P & Q; [| P; Q |] ==> R |] ==> R"   *)
webertj@20217
    18
  val exE           : thm (* "[| EX x. P x; !!x. P x ==> Q |] ==> Q" *)
webertj@20217
    19
  val contrapos     : thm (* "[| ~ Q; P ==> Q |] ==> ~ P"            *)
webertj@20217
    20
  val contrapos2    : thm (* "[| Q; ~ P ==> ~ Q |] ==> P"            *)
webertj@20217
    21
  val notnotD       : thm (* "~ ~ P ==> P"                           *)
oheimb@5304
    22
end
oheimb@5304
    23
oheimb@5304
    24
signature SPLITTER =
oheimb@5304
    25
sig
webertj@20217
    26
  (* somewhat more internal functions *)
wenzelm@33242
    27
  val cmap_of_split_thms: thm list -> (string * (typ * term * thm * typ * int) list) list
wenzelm@33242
    28
  val split_posns: (string * (typ * term * thm * typ * int) list) list ->
wenzelm@33242
    29
    theory -> typ list -> term -> (thm * (typ * typ * int list) list * int list * typ * term) list
wenzelm@33242
    30
    (* first argument is a "cmap", returns a list of "split packs" *)
webertj@20217
    31
  (* the "real" interface, providing a number of tactics *)
oheimb@5304
    32
  val split_tac       : thm list -> int -> tactic
oheimb@5304
    33
  val split_inside_tac: thm list -> int -> tactic
oheimb@5304
    34
  val split_asm_tac   : thm list -> int -> tactic
wenzelm@51717
    35
  val add_split: thm -> Proof.context -> Proof.context
wenzelm@51717
    36
  val del_split: thm -> Proof.context -> Proof.context
wenzelm@18728
    37
  val split_add: attribute
wenzelm@18728
    38
  val split_del: attribute
wenzelm@30513
    39
  val split_modifiers : Method.modifier parser list
wenzelm@18708
    40
  val setup: theory -> theory
oheimb@5304
    41
end;
oheimb@5304
    42
wenzelm@32177
    43
functor Splitter(Data: SPLITTER_DATA): SPLITTER =
wenzelm@17881
    44
struct
oheimb@5304
    45
wenzelm@18545
    46
val Const (const_not, _) $ _ =
wenzelm@35625
    47
  Object_Logic.drop_judgment Data.thy
wenzelm@18545
    48
    (#1 (Logic.dest_implies (Thm.prop_of Data.notnotD)));
oheimb@5304
    49
wenzelm@18545
    50
val Const (const_or , _) $ _ $ _ =
wenzelm@35625
    51
  Object_Logic.drop_judgment Data.thy
wenzelm@18545
    52
    (#1 (Logic.dest_implies (Thm.prop_of Data.disjE)));
wenzelm@18545
    53
wenzelm@35625
    54
val const_Trueprop = Object_Logic.judgment_name Data.thy;
wenzelm@18545
    55
berghofe@1721
    56
webertj@20217
    57
fun split_format_err () = error "Wrong format for split rule";
nipkow@4668
    58
oheimb@5553
    59
fun split_thm_info thm = case concl_of (Data.mk_eq thm) of
berghofe@13855
    60
     Const("==", _) $ (Var _ $ t) $ c => (case strip_comb t of
berghofe@13855
    61
       (Const p, _) => (p, case c of (Const (s, _) $ _) => s = const_not | _ => false)
berghofe@13855
    62
     | _ => split_format_err ())
berghofe@13855
    63
   | _ => split_format_err ();
oheimb@5304
    64
webertj@20217
    65
fun cmap_of_split_thms thms =
webertj@20217
    66
let
webertj@20217
    67
  val splits = map Data.mk_eq thms
wenzelm@33242
    68
  fun add_thm thm cmap =
wenzelm@33242
    69
    (case concl_of thm of _ $ (t as _ $ lhs) $ _ =>
wenzelm@33242
    70
       (case strip_comb lhs of (Const(a,aT),args) =>
wenzelm@33242
    71
          let val info = (aT,lhs,thm,fastype_of t,length args)
wenzelm@33242
    72
          in case AList.lookup (op =) cmap a of
wenzelm@33242
    73
               SOME infos => AList.update (op =) (a, info::infos) cmap
wenzelm@33242
    74
             | NONE => (a,[info])::cmap
wenzelm@33242
    75
          end
wenzelm@33242
    76
        | _ => split_format_err())
wenzelm@33242
    77
     | _ => split_format_err())
webertj@20217
    78
in
wenzelm@33242
    79
  fold add_thm splits []
webertj@20217
    80
end;
webertj@20217
    81
berghofe@54216
    82
val abss = fold (Term.abs o pair "");
berghofe@54216
    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@54742
   103
  (fn {context = ctxt, prems} => rewrite_goals_tac ctxt prems THEN rtac reflexive_thm 1)
nipkow@4
   104
berghofe@54216
   105
val _ $ _ $ (_ $ (_ $ abs_lift) $ _) = prop_of lift;
berghofe@54216
   106
clasohm@0
   107
val trlift = lift RS transitive_thm;
clasohm@0
   108
clasohm@0
   109
wenzelm@17881
   110
(************************************************************************
berghofe@1686
   111
   Set up term for instantiation of P in the lift-theorem
wenzelm@17881
   112
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
*************************************************************************)
berghofe@1686
   118
berghofe@54216
   119
fun mk_cntxt t pos T =
berghofe@54216
   120
  let
berghofe@54216
   121
    fun down [] t = (Bound 0, t)
berghofe@54216
   122
      | down (p :: ps) t =
berghofe@54216
   123
          let
berghofe@54216
   124
            val (h, ts) = strip_comb t
berghofe@54216
   125
            val (ts1, u :: ts2) = chop p ts
berghofe@54216
   126
            val (u1, u2) = down ps u
berghofe@54216
   127
          in
berghofe@54216
   128
            (list_comb (incr_boundvars 1 h,
berghofe@54216
   129
               map (incr_boundvars 1) ts1 @ u1 ::
berghofe@54216
   130
               map (incr_boundvars 1) ts2),
berghofe@54216
   131
             u2)
berghofe@54216
   132
          end;
berghofe@54216
   133
    val (u1, u2) = down (rev pos) t
berghofe@54216
   134
  in (Abs ("", T, u1), u2) 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 =
wenzelm@52131
   149
    if Envir.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 *)
wenzelm@33245
   159
fun add_lbnos t is = 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
*)
wenzelm@33029
   164
fun type_test (T, lbnos, apsns) =
wenzelm@42364
   165
  let val (_, U: typ, _) = nth apsns (foldl1 Int.min lbnos)
wenzelm@33029
   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
haftmann@33955
   199
           val lbnos = fold add_lbnos (take n ts) []
wenzelm@33317
   200
           val flbnos = 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
wenzelm@42367
   230
  fun typ_match thy (tyenv, TU) = Sign.typ_match thy TU tyenv
wenzelm@42367
   231
    handle Type.TYPE_MATCH => raise MATCH;
wenzelm@33242
   232
wenzelm@42367
   233
  fun fomatch thy args =
webertj@20217
   234
    let
webertj@20217
   235
      fun mtch tyinsts = fn
webertj@20217
   236
          (Ts, Var(_,T), t) =>
wenzelm@42367
   237
            typ_match thy (tyinsts, (T, fastype_of1(Ts,t)))
webertj@20217
   238
        | (_, Free (a,T), Free (b,U)) =>
wenzelm@42367
   239
            if a=b then typ_match thy (tyinsts,(T,U)) else raise MATCH
webertj@20217
   240
        | (_, Const (a,T), Const (b,U)) =>
wenzelm@42367
   241
            if a=b then typ_match thy (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)) =>
wenzelm@42367
   245
            mtch (typ_match thy (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;
wenzelm@33242
   250
end;
nipkow@13157
   251
wenzelm@42367
   252
fun split_posns (cmap : (string * (typ * term * thm * typ * int) list) list) thy Ts t =
nipkow@6130
   253
  let
berghofe@7672
   254
    val T' = fastype_of1 (Ts, t);
berghofe@7672
   255
    fun posns Ts pos apsns (Abs (_, T, t)) =
berghofe@7672
   256
          let val U = fastype_of1 (T::Ts,t)
berghofe@7672
   257
          in posns (T::Ts) (0::pos) ((T, U, pos)::apsns) t end
nipkow@6130
   258
      | posns Ts pos apsns t =
nipkow@6130
   259
          let
berghofe@7672
   260
            val (h, ts) = strip_comb t
wenzelm@33245
   261
            fun iter t (i, a) = (i+1, (posns Ts (i::pos) apsns t) @ a);
wenzelm@33245
   262
            val a =
wenzelm@33245
   263
              case h of
wenzelm@33245
   264
                Const(c, cT) =>
wenzelm@33245
   265
                  let fun find [] = []
wenzelm@33245
   266
                        | find ((gcT, pat, thm, T, n)::tups) =
wenzelm@42367
   267
                            let val t2 = list_comb (h, take n ts) in
wenzelm@42367
   268
                              if Sign.typ_instance thy (cT, gcT) andalso fomatch thy (Ts, pat, t2)
wenzelm@42367
   269
                              then mk_split_pack(thm,T,T',n,ts,apsns,pos,type_of1(Ts,t2),t2)
wenzelm@42367
   270
                              else find tups
wenzelm@33245
   271
                            end
wenzelm@33245
   272
                  in find (these (AList.lookup (op =) cmap c)) end
wenzelm@33245
   273
              | _ => []
wenzelm@33245
   274
          in snd (fold iter ts (0, a)) end
nipkow@1030
   275
  in posns Ts [] [] t end;
clasohm@0
   276
webertj@20217
   277
fun shorter ((_,ps,pos,_,_), (_,qs,qos,_,_)) =
wenzelm@4519
   278
  prod_ord (int_ord o pairself length) (order o pairself length)
wenzelm@4519
   279
    ((ps, pos), (qs, qos));
wenzelm@4519
   280
berghofe@1686
   281
berghofe@1686
   282
(************************************************************
berghofe@1686
   283
   call split_posns with appropriate parameters
berghofe@1686
   284
*************************************************************)
clasohm@0
   285
nipkow@1030
   286
fun select cmap state i =
wenzelm@42367
   287
  let
wenzelm@42367
   288
    val thy = Thm.theory_of_thm state
wenzelm@42367
   289
    val goal = term_of (Thm.cprem_of state i);
wenzelm@42367
   290
    val Ts = rev (map #2 (Logic.strip_params goal));
wenzelm@42367
   291
    val _ $ t $ _ = Logic.strip_assums_concl goal;
wenzelm@42367
   292
  in (Ts, t, sort shorter (split_posns cmap thy Ts t)) end;
nipkow@1030
   293
wenzelm@42367
   294
fun exported_split_posns cmap thy Ts t =
wenzelm@42367
   295
  sort shorter (split_posns cmap thy Ts t);
berghofe@1686
   296
berghofe@1686
   297
(*************************************************************
berghofe@1686
   298
   instantiate lift theorem
berghofe@1686
   299
berghofe@1686
   300
   if t is of the form
berghofe@1686
   301
   ... ( Const(...,...) $ Abs( .... ) ) ...
berghofe@1686
   302
   then
berghofe@1686
   303
   P = %a.  ... ( Const(...,...) $ a ) ...
berghofe@1686
   304
   where a has type T --> U
berghofe@1686
   305
berghofe@1686
   306
   Ts      : types of parameters
berghofe@1686
   307
   t       : lefthand side of meta-equality in subgoal
berghofe@1686
   308
             the split theorem is applied to (see cmap)
berghofe@1686
   309
   T,U,pos : see mk_split_pack
berghofe@1686
   310
   state   : current proof state
berghofe@1686
   311
   i       : no. of subgoal
berghofe@1686
   312
**************************************************************)
berghofe@1686
   313
berghofe@7672
   314
fun inst_lift Ts t (T, U, pos) state i =
berghofe@7672
   315
  let
wenzelm@22578
   316
    val cert = cterm_of (Thm.theory_of_thm state);
berghofe@54216
   317
    val (cntxt, u) = mk_cntxt t pos (T --> U);
berghofe@54216
   318
    val trlift' = Thm.lift_rule (Thm.cprem_of state i)
berghofe@54216
   319
      (Thm.rename_boundvars abs_lift u trlift);
berghofe@54216
   320
    val (P, _) = strip_comb (fst (Logic.dest_equals
berghofe@54216
   321
      (Logic.strip_assums_concl (Thm.prop_of trlift'))));
berghofe@54216
   322
  in cterm_instantiate [(cert P, cert (abss Ts 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;
wenzelm@33245
   346
  in cterm_instantiate [(cert P, cert (abss Ts cntxt))] thm'
berghofe@4232
   347
  end;
berghofe@1686
   348
berghofe@7672
   349
berghofe@1686
   350
(*****************************************************************************
berghofe@1686
   351
   The split-tactic
wenzelm@17881
   352
berghofe@1686
   353
   splits : list of split-theorems to be tried
berghofe@1686
   354
   i      : number of subgoal the tactic should be applied to
berghofe@1686
   355
*****************************************************************************)
berghofe@1686
   356
clasohm@0
   357
fun split_tac [] i = no_tac
clasohm@0
   358
  | split_tac splits i =
webertj@20217
   359
  let val cmap = cmap_of_split_thms splits
berghofe@54216
   360
      fun lift_tac Ts t p st = compose_tac (false, inst_lift Ts t p st i, 2) i st
berghofe@7672
   361
      fun lift_split_tac state =
berghofe@7672
   362
            let val (Ts, t, splits) = select cmap state i
nipkow@1030
   363
            in case splits of
berghofe@7672
   364
                 [] => no_tac state
berghofe@7672
   365
               | (thm, apsns, pos, TB, tt)::_ =>
nipkow@1030
   366
                   (case apsns of
berghofe@7672
   367
                      [] => compose_tac (false, inst_split Ts t tt thm TB state i, 0) i state
berghofe@7672
   368
                    | p::_ => EVERY [lift_tac Ts t p,
berghofe@7672
   369
                                     rtac reflexive_thm (i+1),
berghofe@7672
   370
                                     lift_split_tac] state)
nipkow@1030
   371
            end
wenzelm@17881
   372
  in COND (has_fewer_prems i) no_tac
oheimb@5304
   373
          (rtac meta_iffD i THEN lift_split_tac)
clasohm@0
   374
  end;
clasohm@0
   375
webertj@20217
   376
in (split_tac, exported_split_posns) end;  (* mk_case_split_tac *)
berghofe@1721
   377
oheimb@5304
   378
wenzelm@33242
   379
val (split_tac, split_posns) = mk_case_split_tac int_ord;
oheimb@4189
   380
wenzelm@33242
   381
val (split_inside_tac, _) = mk_case_split_tac (rev_order o int_ord);
oheimb@5304
   382
oheimb@4189
   383
oheimb@4189
   384
(*****************************************************************************
oheimb@4189
   385
   The split-tactic for premises
wenzelm@17881
   386
oheimb@4189
   387
   splits : list of split-theorems to be tried
oheimb@5304
   388
****************************************************************************)
wenzelm@33242
   389
fun split_asm_tac [] = K no_tac
wenzelm@17881
   390
  | split_asm_tac splits =
oheimb@5304
   391
berghofe@13855
   392
  let val cname_list = map (fst o fst o split_thm_info) splits;
wenzelm@17881
   393
      fun tac (t,i) =
wenzelm@20664
   394
          let val n = find_index (exists_Const (member (op =) cname_list o #1))
wenzelm@17881
   395
                                 (Logic.strip_assums_hyp t);
wenzelm@18545
   396
              fun first_prem_is_disj (Const ("==>", _) $ (Const (c, _)
wenzelm@18545
   397
                    $ (Const (s, _) $ _ $ _ )) $ _ ) = c = const_Trueprop andalso s = const_or
wenzelm@17881
   398
              |   first_prem_is_disj (Const("all",_)$Abs(_,_,t)) =
wenzelm@17881
   399
                                        first_prem_is_disj t
wenzelm@17881
   400
              |   first_prem_is_disj _ = false;
webertj@20217
   401
      (* does not work properly if the split variable is bound by a quantifier *)
wenzelm@17881
   402
              fun flat_prems_tac i = SUBGOAL (fn (t,i) =>
wenzelm@17881
   403
                           (if first_prem_is_disj t
wenzelm@17881
   404
                            then EVERY[etac Data.disjE i,rotate_tac ~1 i,
wenzelm@17881
   405
                                       rotate_tac ~1  (i+1),
wenzelm@17881
   406
                                       flat_prems_tac (i+1)]
wenzelm@17881
   407
                            else all_tac)
wenzelm@17881
   408
                           THEN REPEAT (eresolve_tac [Data.conjE,Data.exE] i)
wenzelm@17881
   409
                           THEN REPEAT (dresolve_tac [Data.notnotD]   i)) i;
webertj@20217
   410
          in if n<0 then  no_tac  else (DETERM (EVERY'
wenzelm@17881
   411
                [rotate_tac n, etac Data.contrapos2,
wenzelm@17881
   412
                 split_tac splits,
wenzelm@17881
   413
                 rotate_tac ~1, etac Data.contrapos, rotate_tac ~1,
webertj@20217
   414
                 flat_prems_tac] i))
wenzelm@17881
   415
          end;
oheimb@4189
   416
  in SUBGOAL tac
oheimb@4189
   417
  end;
oheimb@4189
   418
nipkow@10652
   419
fun gen_split_tac [] = K no_tac
nipkow@10652
   420
  | gen_split_tac (split::splits) =
nipkow@10652
   421
      let val (_,asm) = split_thm_info split
nipkow@10652
   422
      in (if asm then split_asm_tac else split_tac) [split] ORELSE'
nipkow@10652
   423
         gen_split_tac splits
nipkow@10652
   424
      end;
wenzelm@8468
   425
wenzelm@18688
   426
wenzelm@8468
   427
(** declare split rules **)
wenzelm@8468
   428
wenzelm@45620
   429
(* add_split / del_split *)
wenzelm@8468
   430
wenzelm@33242
   431
fun string_of_typ (Type (s, Ts)) =
wenzelm@33242
   432
      (if null Ts then "" else enclose "(" ")" (commas (map string_of_typ Ts))) ^ s
berghofe@13859
   433
  | string_of_typ _ = "_";
berghofe@13859
   434
wenzelm@17881
   435
fun split_name (name, T) asm = "split " ^
berghofe@13859
   436
  (if asm then "asm " else "") ^ name ^ " :: " ^ string_of_typ T;
oheimb@4189
   437
wenzelm@51717
   438
fun add_split split ctxt =
wenzelm@33242
   439
  let
wenzelm@45620
   440
    val (name, asm) = split_thm_info split
wenzelm@45620
   441
    val tac = (if asm then split_asm_tac else split_tac) [split]
wenzelm@52037
   442
  in Simplifier.addloop (ctxt, (split_name name asm, K tac)) end;
berghofe@1721
   443
wenzelm@51717
   444
fun del_split split ctxt =
wenzelm@45620
   445
  let val (name, asm) = split_thm_info split
wenzelm@51717
   446
  in Simplifier.delloop (ctxt, split_name name asm) end;
berghofe@1721
   447
wenzelm@8468
   448
wenzelm@8468
   449
(* attributes *)
wenzelm@8468
   450
wenzelm@8468
   451
val splitN = "split";
wenzelm@8468
   452
wenzelm@45620
   453
val split_add = Simplifier.attrib add_split;
wenzelm@45620
   454
val split_del = Simplifier.attrib del_split;
wenzelm@8634
   455
wenzelm@8634
   456
wenzelm@9703
   457
(* methods *)
wenzelm@8468
   458
wenzelm@8468
   459
val split_modifiers =
wenzelm@18728
   460
 [Args.$$$ splitN -- Args.colon >> K ((I, split_add): Method.modifier),
wenzelm@18728
   461
  Args.$$$ splitN -- Args.add -- Args.colon >> K (I, split_add),
wenzelm@18728
   462
  Args.$$$ splitN -- Args.del -- Args.colon >> K (I, split_del)];
wenzelm@8468
   463
wenzelm@8468
   464
wenzelm@18688
   465
(* theory setup *)
wenzelm@8468
   466
wenzelm@9703
   467
val setup =
wenzelm@33242
   468
  Attrib.setup @{binding split}
wenzelm@33242
   469
    (Attrib.add_del split_add split_del) "declare case split rule" #>
wenzelm@30722
   470
  Method.setup @{binding split}
wenzelm@30722
   471
    (Attrib.thms >> (fn ths => K (SIMPLE_METHOD' (CHANGED_PROP o gen_split_tac ths))))
wenzelm@30722
   472
    "apply case split rule";
oheimb@4189
   473
berghofe@1721
   474
end;