src/Provers/splitter.ML
author wenzelm
Sat Jul 25 23:41:53 2015 +0200 (2015-07-25)
changeset 60781 2da59cdf531c
parent 60362 befdc10ebb42
child 62870 cf724647f75b
permissions -rw-r--r--
updated to infer_instantiate;
tuned;
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@59970
    12
  val context       : Proof.context
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 *)
wenzelm@58956
    32
  val split_tac       : Proof.context -> thm list -> int -> tactic
wenzelm@58956
    33
  val split_inside_tac: Proof.context -> thm list -> int -> tactic
wenzelm@58956
    34
  val split_asm_tac   : Proof.context -> 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
oheimb@5304
    40
end;
oheimb@5304
    41
wenzelm@32177
    42
functor Splitter(Data: SPLITTER_DATA): SPLITTER =
wenzelm@17881
    43
struct
oheimb@5304
    44
wenzelm@18545
    45
val Const (const_not, _) $ _ =
wenzelm@59970
    46
  Object_Logic.drop_judgment Data.context
wenzelm@18545
    47
    (#1 (Logic.dest_implies (Thm.prop_of Data.notnotD)));
oheimb@5304
    48
wenzelm@18545
    49
val Const (const_or , _) $ _ $ _ =
wenzelm@59970
    50
  Object_Logic.drop_judgment Data.context
wenzelm@18545
    51
    (#1 (Logic.dest_implies (Thm.prop_of Data.disjE)));
wenzelm@18545
    52
wenzelm@59970
    53
val const_Trueprop = Object_Logic.judgment_name Data.context;
wenzelm@18545
    54
berghofe@1721
    55
webertj@20217
    56
fun split_format_err () = error "Wrong format for split rule";
nipkow@4668
    57
wenzelm@59582
    58
fun split_thm_info thm =
wenzelm@59582
    59
  (case Thm.concl_of (Data.mk_eq thm) of
wenzelm@59582
    60
    Const(@{const_name Pure.eq}, _) $ (Var _ $ t) $ c =>
wenzelm@59582
    61
      (case strip_comb t of
wenzelm@59582
    62
        (Const p, _) => (p, case c of (Const (s, _) $ _) => s = const_not | _ => false)
wenzelm@59582
    63
      | _ => split_format_err ())
wenzelm@59582
    64
  | _ => split_format_err ());
oheimb@5304
    65
webertj@20217
    66
fun cmap_of_split_thms thms =
webertj@20217
    67
let
webertj@20217
    68
  val splits = map Data.mk_eq thms
wenzelm@33242
    69
  fun add_thm thm cmap =
wenzelm@59582
    70
    (case Thm.concl_of thm of _ $ (t as _ $ lhs) $ _ =>
wenzelm@33242
    71
       (case strip_comb lhs of (Const(a,aT),args) =>
wenzelm@33242
    72
          let val info = (aT,lhs,thm,fastype_of t,length args)
wenzelm@33242
    73
          in case AList.lookup (op =) cmap a of
wenzelm@33242
    74
               SOME infos => AList.update (op =) (a, info::infos) cmap
wenzelm@33242
    75
             | NONE => (a,[info])::cmap
wenzelm@33242
    76
          end
wenzelm@33242
    77
        | _ => split_format_err())
wenzelm@33242
    78
     | _ => split_format_err())
webertj@20217
    79
in
wenzelm@33242
    80
  fold add_thm splits []
webertj@20217
    81
end;
webertj@20217
    82
berghofe@54216
    83
val abss = fold (Term.abs o pair "");
berghofe@54216
    84
webertj@20217
    85
(* ------------------------------------------------------------------------- *)
webertj@20217
    86
(* mk_case_split_tac                                                         *)
webertj@20217
    87
(* ------------------------------------------------------------------------- *)
webertj@20217
    88
oheimb@5304
    89
fun mk_case_split_tac order =
clasohm@0
    90
let
clasohm@0
    91
berghofe@1686
    92
(************************************************************
berghofe@1686
    93
   Create lift-theorem "trlift" :
berghofe@1686
    94
berghofe@7672
    95
   [| !!x. Q x == R x; P(%x. R x) == C |] ==> P (%x. Q x) == C
berghofe@1686
    96
berghofe@1686
    97
*************************************************************)
oheimb@5304
    98
webertj@20217
    99
val meta_iffD = Data.meta_eq_to_iff RS Data.iffD;  (* (P == Q) ==> Q ==> P *)
webertj@20217
   100
haftmann@22838
   101
val lift = Goal.prove_global Pure.thy ["P", "Q", "R"]
wenzelm@24707
   102
  [Syntax.read_prop_global Pure.thy "!!x :: 'b. Q(x) == R(x) :: 'c"]
wenzelm@24707
   103
  (Syntax.read_prop_global Pure.thy "P(%x. Q(x)) == P(%x. R(x))")
wenzelm@59498
   104
  (fn {context = ctxt, prems} =>
wenzelm@59498
   105
    rewrite_goals_tac ctxt prems THEN resolve_tac ctxt [reflexive_thm] 1)
nipkow@4
   106
wenzelm@59582
   107
val _ $ _ $ (_ $ (_ $ abs_lift) $ _) = Thm.prop_of lift;
berghofe@54216
   108
clasohm@0
   109
val trlift = lift RS transitive_thm;
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
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
   116
           the lift theorem is applied to (see select)
berghofe@1686
   117
   pos   : "path" leading to abstraction, coded as a list
berghofe@1686
   118
   T     : type of body of P(...)
berghofe@1686
   119
*************************************************************************)
berghofe@1686
   120
berghofe@54216
   121
fun mk_cntxt t pos T =
berghofe@54216
   122
  let
berghofe@54216
   123
    fun down [] t = (Bound 0, t)
berghofe@54216
   124
      | down (p :: ps) t =
berghofe@54216
   125
          let
berghofe@54216
   126
            val (h, ts) = strip_comb t
berghofe@54216
   127
            val (ts1, u :: ts2) = chop p ts
berghofe@54216
   128
            val (u1, u2) = down ps u
berghofe@54216
   129
          in
berghofe@54216
   130
            (list_comb (incr_boundvars 1 h,
berghofe@54216
   131
               map (incr_boundvars 1) ts1 @ u1 ::
berghofe@54216
   132
               map (incr_boundvars 1) ts2),
berghofe@54216
   133
             u2)
berghofe@54216
   134
          end;
berghofe@54216
   135
    val (u1, u2) = down (rev pos) t
berghofe@54216
   136
  in (Abs ("", T, u1), u2) end;
nipkow@1030
   137
berghofe@1686
   138
wenzelm@17881
   139
(************************************************************************
berghofe@1686
   140
   Set up term for instantiation of P in the split-theorem
berghofe@1686
   141
   P(...) == rhs
berghofe@1686
   142
berghofe@1686
   143
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
   144
           the split theorem is applied to (see select)
berghofe@1686
   145
   T     : type of body of P(...)
berghofe@4232
   146
   tt    : the term  Const(key,..) $ ...
berghofe@1686
   147
*************************************************************************)
berghofe@1686
   148
berghofe@4232
   149
fun mk_cntxt_splitthm t tt T =
berghofe@4232
   150
  let fun repl lev t =
wenzelm@52131
   151
    if Envir.aeconv(incr_boundvars lev tt, t) then Bound lev
berghofe@4232
   152
    else case t of
berghofe@4232
   153
        (Abs (v, T2, t)) => Abs (v, T2, repl (lev+1) t)
berghofe@4232
   154
      | (Bound i) => Bound (if i>=lev then i+1 else i)
berghofe@4232
   155
      | (t1 $ t2) => (repl lev t1) $ (repl lev t2)
berghofe@4232
   156
      | t => t
berghofe@4232
   157
  in Abs("", T, repl 0 t) end;
berghofe@1686
   158
berghofe@1686
   159
berghofe@1686
   160
(* add all loose bound variables in t to list is *)
wenzelm@33245
   161
fun add_lbnos t is = add_loose_bnos (t, 0, is);
nipkow@1030
   162
berghofe@7672
   163
(* check if the innermost abstraction that needs to be removed
nipkow@1064
   164
   has a body of type T; otherwise the expansion thm will fail later on
nipkow@1064
   165
*)
wenzelm@33029
   166
fun type_test (T, lbnos, apsns) =
wenzelm@42364
   167
  let val (_, U: typ, _) = nth apsns (foldl1 Int.min lbnos)
wenzelm@33029
   168
  in T = U end;
clasohm@0
   169
berghofe@1686
   170
(*************************************************************************
berghofe@1686
   171
   Create a "split_pack".
berghofe@1686
   172
berghofe@1686
   173
   thm   : the relevant split-theorem, i.e. P(...) == rhs , where P(...)
berghofe@1686
   174
           is of the form
berghofe@1686
   175
           P( Const(key,...) $ t_1 $ ... $ t_n )      (e.g. key = "if")
berghofe@1686
   176
   T     : type of P(...)
berghofe@7672
   177
   T'    : type of term to be scanned
berghofe@1686
   178
   n     : number of arguments expected by Const(key,...)
berghofe@1686
   179
   ts    : list of arguments actually found
berghofe@1686
   180
   apsns : list of tuples of the form (T,U,pos), one tuple for each
wenzelm@17881
   181
           abstraction that is encountered on the way to the position where
berghofe@1686
   182
           Const(key, ...) $ ...  occurs, where
berghofe@1686
   183
           T   : type of the variable bound by the abstraction
berghofe@1686
   184
           U   : type of the abstraction's body
berghofe@1686
   185
           pos : "path" leading to the body of the abstraction
berghofe@1686
   186
   pos   : "path" leading to the position where Const(key, ...) $ ...  occurs.
berghofe@1686
   187
   TB    : type of  Const(key,...) $ t_1 $ ... $ t_n
berghofe@1721
   188
   t     : the term Const(key,...) $ t_1 $ ... $ t_n
berghofe@1686
   189
berghofe@1686
   190
   A split pack is a tuple of the form
berghofe@7672
   191
   (thm, apsns, pos, TB, tt)
berghofe@1686
   192
   Note : apsns is reversed, so that the outermost quantifier's position
berghofe@1686
   193
          comes first ! If the terms in ts don't contain variables bound
berghofe@1686
   194
          by other than meta-quantifiers, apsns is empty, because no further
berghofe@1686
   195
          lifting is required before applying the split-theorem.
wenzelm@17881
   196
******************************************************************************)
berghofe@1686
   197
wenzelm@20664
   198
fun mk_split_pack (thm, T: typ, T', n, ts, apsns, pos, TB, t) =
nipkow@1064
   199
  if n > length ts then []
nipkow@1064
   200
  else let val lev = length apsns
haftmann@33955
   201
           val lbnos = fold add_lbnos (take n ts) []
wenzelm@33317
   202
           val flbnos = filter (fn i => i < lev) lbnos
berghofe@4232
   203
           val tt = incr_boundvars (~lev) t
berghofe@7672
   204
       in if null flbnos then
berghofe@7672
   205
            if T = T' then [(thm,[],pos,TB,tt)] else []
berghofe@7672
   206
          else if type_test(T,flbnos,apsns) then [(thm, rev apsns,pos,TB,tt)]
paulson@2143
   207
               else []
nipkow@1064
   208
       end;
clasohm@0
   209
berghofe@1686
   210
berghofe@1686
   211
(****************************************************************************
blanchet@58318
   212
   Recursively scans term for occurrences of Const(key,...) $ ...
blanchet@58318
   213
   Returns a list of "split-packs" (one for each occurrence of Const(key,...) )
berghofe@1686
   214
berghofe@1686
   215
   cmap : association list of split-theorems that should be tried.
berghofe@1686
   216
          The elements have the format (key,(thm,T,n)) , where
berghofe@1686
   217
          key : the theorem's key constant ( Const(key,...) $ ... )
berghofe@1686
   218
          thm : the theorem itself
berghofe@1686
   219
          T   : type of P( Const(key,...) $ ... )
berghofe@1686
   220
          n   : number of arguments expected by Const(key,...)
berghofe@1686
   221
   Ts   : types of parameters
berghofe@1686
   222
   t    : the term to be scanned
berghofe@1686
   223
******************************************************************************)
berghofe@1686
   224
nipkow@13157
   225
(* Simplified first-order matching;
nipkow@13157
   226
   assumes that all Vars in the pattern are distinct;
nipkow@13157
   227
   see Pure/pattern.ML for the full version;
nipkow@13157
   228
*)
nipkow@13157
   229
local
webertj@20217
   230
  exception MATCH
nipkow@13157
   231
in
wenzelm@42367
   232
  fun typ_match thy (tyenv, TU) = Sign.typ_match thy TU tyenv
wenzelm@42367
   233
    handle Type.TYPE_MATCH => raise MATCH;
wenzelm@33242
   234
wenzelm@42367
   235
  fun fomatch thy args =
webertj@20217
   236
    let
webertj@20217
   237
      fun mtch tyinsts = fn
webertj@20217
   238
          (Ts, Var(_,T), t) =>
wenzelm@42367
   239
            typ_match thy (tyinsts, (T, fastype_of1(Ts,t)))
webertj@20217
   240
        | (_, Free (a,T), Free (b,U)) =>
wenzelm@42367
   241
            if a=b then typ_match thy (tyinsts,(T,U)) else raise MATCH
webertj@20217
   242
        | (_, Const (a,T), Const (b,U)) =>
wenzelm@42367
   243
            if a=b then typ_match thy (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)) =>
wenzelm@42367
   247
            mtch (typ_match thy (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;
wenzelm@33242
   252
end;
nipkow@13157
   253
wenzelm@42367
   254
fun split_posns (cmap : (string * (typ * term * thm * typ * int) list) list) thy 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
wenzelm@33245
   263
            fun iter t (i, a) = (i+1, (posns Ts (i::pos) apsns t) @ a);
wenzelm@33245
   264
            val a =
wenzelm@33245
   265
              case h of
wenzelm@33245
   266
                Const(c, cT) =>
wenzelm@33245
   267
                  let fun find [] = []
wenzelm@33245
   268
                        | find ((gcT, pat, thm, T, n)::tups) =
wenzelm@42367
   269
                            let val t2 = list_comb (h, take n ts) in
wenzelm@42367
   270
                              if Sign.typ_instance thy (cT, gcT) andalso fomatch thy (Ts, pat, t2)
wenzelm@42367
   271
                              then mk_split_pack(thm,T,T',n,ts,apsns,pos,type_of1(Ts,t2),t2)
wenzelm@42367
   272
                              else find tups
wenzelm@33245
   273
                            end
wenzelm@33245
   274
                  in find (these (AList.lookup (op =) cmap c)) end
wenzelm@33245
   275
              | _ => []
wenzelm@33245
   276
          in snd (fold iter ts (0, a)) end
nipkow@1030
   277
  in posns Ts [] [] t end;
clasohm@0
   278
webertj@20217
   279
fun shorter ((_,ps,pos,_,_), (_,qs,qos,_,_)) =
wenzelm@59058
   280
  prod_ord (int_ord o apply2 length) (order o apply2 length)
wenzelm@4519
   281
    ((ps, pos), (qs, qos));
wenzelm@4519
   282
berghofe@1686
   283
berghofe@1686
   284
(************************************************************
berghofe@1686
   285
   call split_posns with appropriate parameters
berghofe@1686
   286
*************************************************************)
clasohm@0
   287
wenzelm@60362
   288
fun select thy cmap state i =
wenzelm@42367
   289
  let
wenzelm@59582
   290
    val goal = Thm.term_of (Thm.cprem_of state i);
wenzelm@42367
   291
    val Ts = rev (map #2 (Logic.strip_params goal));
wenzelm@42367
   292
    val _ $ t $ _ = Logic.strip_assums_concl goal;
wenzelm@42367
   293
  in (Ts, t, sort shorter (split_posns cmap thy Ts t)) end;
nipkow@1030
   294
wenzelm@42367
   295
fun exported_split_posns cmap thy Ts t =
wenzelm@42367
   296
  sort shorter (split_posns cmap thy Ts t);
berghofe@1686
   297
berghofe@1686
   298
(*************************************************************
berghofe@1686
   299
   instantiate lift theorem
berghofe@1686
   300
berghofe@1686
   301
   if t is of the form
berghofe@1686
   302
   ... ( Const(...,...) $ Abs( .... ) ) ...
berghofe@1686
   303
   then
berghofe@1686
   304
   P = %a.  ... ( Const(...,...) $ a ) ...
berghofe@1686
   305
   where a has type T --> U
berghofe@1686
   306
berghofe@1686
   307
   Ts      : types of parameters
berghofe@1686
   308
   t       : lefthand side of meta-equality in subgoal
berghofe@1686
   309
             the split theorem is applied to (see cmap)
berghofe@1686
   310
   T,U,pos : see mk_split_pack
berghofe@1686
   311
   state   : current proof state
berghofe@1686
   312
   i       : no. of subgoal
berghofe@1686
   313
**************************************************************)
berghofe@1686
   314
wenzelm@60362
   315
fun inst_lift ctxt Ts t (T, U, pos) state i =
berghofe@7672
   316
  let
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);
wenzelm@60781
   320
    val (Var (P, _), _) =
wenzelm@60781
   321
      strip_comb (fst (Logic.dest_equals
wenzelm@60781
   322
        (Logic.strip_assums_concl (Thm.prop_of trlift'))));
wenzelm@60781
   323
  in infer_instantiate ctxt [(P, Thm.cterm_of ctxt (abss Ts cntxt))] trlift' 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
wenzelm@60362
   339
fun inst_split ctxt 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;
wenzelm@60781
   342
    val (Var (P, _), _) =
wenzelm@60781
   343
      strip_comb (fst (Logic.dest_equals
wenzelm@60781
   344
        (Logic.strip_assums_concl (Thm.prop_of thm'))));
berghofe@7672
   345
    val cntxt = mk_cntxt_splitthm t tt TB;
wenzelm@60781
   346
  in infer_instantiate ctxt [(P, Thm.cterm_of ctxt (abss Ts cntxt))] thm' end;
berghofe@1686
   347
berghofe@7672
   348
berghofe@1686
   349
(*****************************************************************************
berghofe@1686
   350
   The split-tactic
wenzelm@17881
   351
berghofe@1686
   352
   splits : list of split-theorems to be tried
berghofe@1686
   353
   i      : number of subgoal the tactic should be applied to
berghofe@1686
   354
*****************************************************************************)
berghofe@1686
   355
wenzelm@58956
   356
fun split_tac _ [] i = no_tac
wenzelm@58956
   357
  | split_tac ctxt splits i =
webertj@20217
   358
  let val cmap = cmap_of_split_thms splits
wenzelm@60362
   359
      fun lift_tac Ts t p st = compose_tac ctxt (false, inst_lift ctxt Ts t p st i, 2) i st
berghofe@7672
   360
      fun lift_split_tac state =
wenzelm@60362
   361
            let val (Ts, t, splits) = select (Proof_Context.theory_of ctxt) cmap state i
nipkow@1030
   362
            in case splits of
berghofe@7672
   363
                 [] => no_tac state
berghofe@7672
   364
               | (thm, apsns, pos, TB, tt)::_ =>
nipkow@1030
   365
                   (case apsns of
wenzelm@60362
   366
                      [] =>
wenzelm@60362
   367
                        compose_tac ctxt (false, inst_split ctxt Ts t tt thm TB state i, 0) i state
berghofe@7672
   368
                    | p::_ => EVERY [lift_tac Ts t p,
wenzelm@59498
   369
                                     resolve_tac ctxt [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
wenzelm@59498
   373
          (resolve_tac ctxt [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@58956
   389
fun split_asm_tac _ [] = K no_tac
wenzelm@58956
   390
  | split_asm_tac ctxt 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@56245
   396
              fun first_prem_is_disj (Const (@{const_name Pure.imp}, _) $ (Const (c, _)
wenzelm@18545
   397
                    $ (Const (s, _) $ _ $ _ )) $ _ ) = c = const_Trueprop andalso s = const_or
wenzelm@56245
   398
              |   first_prem_is_disj (Const(@{const_name Pure.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@59498
   404
                            then EVERY[eresolve_tac ctxt [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@59498
   408
                           THEN REPEAT (eresolve_tac ctxt [Data.conjE,Data.exE] i)
wenzelm@59498
   409
                           THEN REPEAT (dresolve_tac ctxt [Data.notnotD]   i)) i;
webertj@20217
   410
          in if n<0 then  no_tac  else (DETERM (EVERY'
wenzelm@59498
   411
                [rotate_tac n, eresolve_tac ctxt [Data.contrapos2],
wenzelm@58956
   412
                 split_tac ctxt splits,
wenzelm@59498
   413
                 rotate_tac ~1, eresolve_tac ctxt [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
wenzelm@58956
   419
fun gen_split_tac _ [] = K no_tac
wenzelm@58956
   420
  | gen_split_tac ctxt (split::splits) =
nipkow@10652
   421
      let val (_,asm) = split_thm_info split
wenzelm@58956
   422
      in (if asm then split_asm_tac else split_tac) ctxt [split] ORELSE'
wenzelm@58956
   423
         gen_split_tac ctxt 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@58956
   441
    fun tac ctxt' = (if asm then split_asm_tac else split_tac) ctxt' [split]
wenzelm@58956
   442
  in Simplifier.addloop (ctxt, (split_name name asm, 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@58826
   456
val _ =
wenzelm@58826
   457
  Theory.setup
wenzelm@58826
   458
    (Attrib.setup @{binding split}
wenzelm@58826
   459
      (Attrib.add_del split_add split_del) "declare case split rule");
wenzelm@58826
   460
wenzelm@8634
   461
wenzelm@9703
   462
(* methods *)
wenzelm@8468
   463
wenzelm@8468
   464
val split_modifiers =
wenzelm@58048
   465
 [Args.$$$ splitN -- Args.colon >> K (Method.modifier split_add @{here}),
wenzelm@58048
   466
  Args.$$$ splitN -- Args.add -- Args.colon >> K (Method.modifier split_add @{here}),
wenzelm@58048
   467
  Args.$$$ splitN -- Args.del -- Args.colon >> K (Method.modifier split_del @{here})];
wenzelm@8468
   468
wenzelm@58826
   469
val _ =
wenzelm@58826
   470
  Theory.setup
wenzelm@58826
   471
    (Method.setup @{binding split}
wenzelm@58956
   472
      (Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (CHANGED_PROP o gen_split_tac ctxt ths)))
wenzelm@58826
   473
      "apply case split rule");
oheimb@4189
   474
berghofe@1721
   475
end;