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