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