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