src/Provers/splitter.ML
author wenzelm
Wed Jan 07 13:55:29 1998 +0100 (1998-01-07)
changeset 4519 055f2067d373
parent 4453 bcb28bb925c1
child 4668 131989b78417
permissions -rw-r--r--
adapted to new sort function;
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@4
     7
clasohm@0
     8
Use:
clasohm@0
     9
clasohm@0
    10
val split_tac = mk_case_split_tac iffD;
clasohm@0
    11
oheimb@4189
    12
by(split_tac splits i);
clasohm@0
    13
clasohm@0
    14
where splits = [P(elim(...)) == rhs, ...]
clasohm@0
    15
      iffD  = [| P <-> Q; Q |] ==> P (* is called iffD2 in HOL *)
clasohm@0
    16
clasohm@0
    17
*)
clasohm@0
    18
berghofe@1721
    19
local
berghofe@1721
    20
berghofe@1721
    21
fun mk_case_split_tac_2 iffD order =
clasohm@0
    22
let
clasohm@0
    23
berghofe@1686
    24
berghofe@1686
    25
(************************************************************
berghofe@1686
    26
   Create lift-theorem "trlift" :
berghofe@1686
    27
berghofe@1686
    28
   [| !! x. Q(x)==R(x) ; P(R) == C |] ==> P(Q)==C
berghofe@1686
    29
berghofe@1686
    30
*************************************************************)
berghofe@1686
    31
 
nipkow@943
    32
val lift =
nipkow@943
    33
  let val ct = read_cterm (#sign(rep_thm iffD))
nipkow@943
    34
           ("[| !!x::'b::logic. Q(x) == R(x) |] ==> \
wenzelm@3835
    35
            \P(%x. Q(x)) == P(%x. R(x))::'a::logic",propT)
nipkow@943
    36
  in prove_goalw_cterm [] ct
nipkow@943
    37
     (fn [prem] => [rewtac prem, rtac reflexive_thm 1])
nipkow@943
    38
  end;
nipkow@4
    39
clasohm@0
    40
val trlift = lift RS transitive_thm;
clasohm@0
    41
val _ $ (Var(P,PT)$_) $ _ = concl_of trlift;
clasohm@0
    42
clasohm@0
    43
berghofe@1686
    44
(************************************************************************ 
berghofe@1686
    45
   Set up term for instantiation of P in the lift-theorem
berghofe@1686
    46
   
berghofe@1686
    47
   Ts    : types of parameters (i.e. variables bound by meta-quantifiers)
berghofe@1686
    48
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
    49
           the lift theorem is applied to (see select)
berghofe@1686
    50
   pos   : "path" leading to abstraction, coded as a list
berghofe@1686
    51
   T     : type of body of P(...)
berghofe@1686
    52
   maxi  : maximum index of Vars
berghofe@1686
    53
*************************************************************************)
berghofe@1686
    54
nipkow@1030
    55
fun mk_cntxt Ts t pos T maxi =
nipkow@1030
    56
  let fun var (t,i) = Var(("X",i),type_of1(Ts,t));
nipkow@1030
    57
      fun down [] t i = Bound 0
nipkow@1030
    58
        | down (p::ps) t i =
nipkow@1030
    59
            let val (h,ts) = strip_comb t
paulson@2266
    60
                val v1 = ListPair.map var (take(p,ts), i upto (i+p-1))
nipkow@1030
    61
                val u::us = drop(p,ts)
paulson@2266
    62
                val v2 = ListPair.map var (us, (i+p) upto (i+length(ts)-2))
nipkow@1030
    63
      in list_comb(h,v1@[down ps u (i+length ts)]@v2) end;
nipkow@1030
    64
  in Abs("", T, down (rev pos) t maxi) end;
nipkow@1030
    65
berghofe@1686
    66
berghofe@1686
    67
(************************************************************************ 
berghofe@1686
    68
   Set up term for instantiation of P in the split-theorem
berghofe@1686
    69
   P(...) == rhs
berghofe@1686
    70
berghofe@1686
    71
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
    72
           the split theorem is applied to (see select)
berghofe@1686
    73
   T     : type of body of P(...)
berghofe@4232
    74
   tt    : the term  Const(key,..) $ ...
berghofe@1686
    75
*************************************************************************)
berghofe@1686
    76
berghofe@4232
    77
fun mk_cntxt_splitthm t tt T =
berghofe@4232
    78
  let fun repl lev t =
berghofe@4232
    79
    if incr_boundvars lev tt = t then Bound lev
berghofe@4232
    80
    else case t of
berghofe@4232
    81
        (Abs (v, T2, t)) => Abs (v, T2, repl (lev+1) t)
berghofe@4232
    82
      | (Bound i) => Bound (if i>=lev then i+1 else i)
berghofe@4232
    83
      | (t1 $ t2) => (repl lev t1) $ (repl lev t2)
berghofe@4232
    84
      | t => t
berghofe@4232
    85
  in Abs("", T, repl 0 t) end;
berghofe@1686
    86
berghofe@1686
    87
berghofe@1686
    88
(* add all loose bound variables in t to list is *)
nipkow@1030
    89
fun add_lbnos(is,t) = add_loose_bnos(t,0,is);
nipkow@1030
    90
nipkow@1064
    91
(* check if the innermost quantifier that needs to be removed
nipkow@1064
    92
   has a body of type T; otherwise the expansion thm will fail later on
nipkow@1064
    93
*)
nipkow@1064
    94
fun type_test(T,lbnos,apsns) =
paulson@2143
    95
  let val (_,U,_) = nth_elem(foldl Int.min (hd lbnos, tl lbnos), apsns)
nipkow@1064
    96
  in T=U end;
clasohm@0
    97
berghofe@1686
    98
(*************************************************************************
berghofe@1686
    99
   Create a "split_pack".
berghofe@1686
   100
berghofe@1686
   101
   thm   : the relevant split-theorem, i.e. P(...) == rhs , where P(...)
berghofe@1686
   102
           is of the form
berghofe@1686
   103
           P( Const(key,...) $ t_1 $ ... $ t_n )      (e.g. key = "if")
berghofe@1686
   104
   T     : type of P(...)
berghofe@1686
   105
   n     : number of arguments expected by Const(key,...)
berghofe@1686
   106
   ts    : list of arguments actually found
berghofe@1686
   107
   apsns : list of tuples of the form (T,U,pos), one tuple for each
berghofe@1686
   108
           abstraction that is encountered on the way to the position where 
berghofe@1686
   109
           Const(key, ...) $ ...  occurs, where
berghofe@1686
   110
           T   : type of the variable bound by the abstraction
berghofe@1686
   111
           U   : type of the abstraction's body
berghofe@1686
   112
           pos : "path" leading to the body of the abstraction
berghofe@1686
   113
   pos   : "path" leading to the position where Const(key, ...) $ ...  occurs.
berghofe@1686
   114
   TB    : type of  Const(key,...) $ t_1 $ ... $ t_n
berghofe@1721
   115
   t     : the term Const(key,...) $ t_1 $ ... $ t_n
berghofe@1686
   116
berghofe@1686
   117
   A split pack is a tuple of the form
berghofe@1686
   118
   (thm, apsns, pos, TB)
berghofe@1686
   119
   Note : apsns is reversed, so that the outermost quantifier's position
berghofe@1686
   120
          comes first ! If the terms in ts don't contain variables bound
berghofe@1686
   121
          by other than meta-quantifiers, apsns is empty, because no further
berghofe@1686
   122
          lifting is required before applying the split-theorem.
berghofe@1686
   123
******************************************************************************) 
berghofe@1686
   124
berghofe@1721
   125
fun mk_split_pack(thm,T,n,ts,apsns,pos,TB,t) =
nipkow@1064
   126
  if n > length ts then []
nipkow@1064
   127
  else let val lev = length apsns
nipkow@1030
   128
           val lbnos = foldl add_lbnos ([],take(n,ts))
nipkow@1030
   129
           val flbnos = filter (fn i => i < lev) lbnos
berghofe@4232
   130
           val tt = incr_boundvars (~lev) t
berghofe@1721
   131
       in if null flbnos then [(thm,[],pos,TB,tt)]
paulson@2143
   132
          else if type_test(T,flbnos,apsns) then [(thm, rev apsns,pos,TB,tt)] 
paulson@2143
   133
               else []
nipkow@1064
   134
       end;
clasohm@0
   135
berghofe@1686
   136
berghofe@1686
   137
(****************************************************************************
berghofe@1686
   138
   Recursively scans term for occurences of Const(key,...) $ ...
berghofe@1686
   139
   Returns a list of "split-packs" (one for each occurence of Const(key,...) )
berghofe@1686
   140
berghofe@1686
   141
   cmap : association list of split-theorems that should be tried.
berghofe@1686
   142
          The elements have the format (key,(thm,T,n)) , where
berghofe@1686
   143
          key : the theorem's key constant ( Const(key,...) $ ... )
berghofe@1686
   144
          thm : the theorem itself
berghofe@1686
   145
          T   : type of P( Const(key,...) $ ... )
berghofe@1686
   146
          n   : number of arguments expected by Const(key,...)
berghofe@1686
   147
   Ts   : types of parameters
berghofe@1686
   148
   t    : the term to be scanned
berghofe@1686
   149
******************************************************************************)
berghofe@1686
   150
nipkow@1030
   151
fun split_posns cmap Ts t =
nipkow@1030
   152
  let fun posns Ts pos apsns (Abs(_,T,t)) =
nipkow@1030
   153
            let val U = fastype_of1(T::Ts,t)
nipkow@1030
   154
            in posns (T::Ts) (0::pos) ((T,U,pos)::apsns) t end
nipkow@1030
   155
        | posns Ts pos apsns t =
nipkow@1030
   156
            let val (h,ts) = strip_comb t
nipkow@1030
   157
                fun iter((i,a),t) = (i+1, (posns Ts (i::pos) apsns t) @ a);
nipkow@1030
   158
                val a = case h of
nipkow@1030
   159
                  Const(c,_) =>
nipkow@1030
   160
                    (case assoc(cmap,c) of
berghofe@4232
   161
                       Some(thm, T, n) =>
berghofe@4232
   162
                         let val t2 = list_comb (h, take (n, ts)) in
berghofe@4232
   163
                           mk_split_pack(thm,T,n,ts,apsns,pos,type_of1(Ts, t2),t2)
berghofe@4232
   164
                         end
nipkow@1030
   165
                     | None => [])
nipkow@1030
   166
                | _ => []
nipkow@1030
   167
             in snd(foldl iter ((0,a),ts)) end
nipkow@1030
   168
  in posns Ts [] [] t end;
clasohm@0
   169
berghofe@1686
   170
clasohm@0
   171
fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
clasohm@0
   172
berghofe@1721
   173
fun shorter((_,ps,pos,_,_),(_,qs,qos,_,_)) =
wenzelm@4519
   174
  prod_ord (int_ord o pairself length) (order o pairself length)
wenzelm@4519
   175
    ((ps, pos), (qs, qos));
wenzelm@4519
   176
berghofe@1686
   177
berghofe@1686
   178
berghofe@1686
   179
(************************************************************
berghofe@1686
   180
   call split_posns with appropriate parameters
berghofe@1686
   181
*************************************************************)
clasohm@0
   182
nipkow@1030
   183
fun select cmap state i =
nipkow@1030
   184
  let val goali = nth_subgoal i state
nipkow@1030
   185
      val Ts = rev(map #2 (Logic.strip_params goali))
nipkow@1030
   186
      val _ $ t $ _ = Logic.strip_assums_concl goali;
wenzelm@4519
   187
  in (Ts,t, sort shorter (split_posns cmap Ts t)) end;
nipkow@1030
   188
berghofe@1686
   189
berghofe@1686
   190
(*************************************************************
berghofe@1686
   191
   instantiate lift theorem
berghofe@1686
   192
berghofe@1686
   193
   if t is of the form
berghofe@1686
   194
   ... ( Const(...,...) $ Abs( .... ) ) ...
berghofe@1686
   195
   then
berghofe@1686
   196
   P = %a.  ... ( Const(...,...) $ a ) ...
berghofe@1686
   197
   where a has type T --> U
berghofe@1686
   198
berghofe@1686
   199
   Ts      : types of parameters
berghofe@1686
   200
   t       : lefthand side of meta-equality in subgoal
berghofe@1686
   201
             the split theorem is applied to (see cmap)
berghofe@1686
   202
   T,U,pos : see mk_split_pack
berghofe@1686
   203
   state   : current proof state
berghofe@1686
   204
   lift    : the lift theorem
berghofe@1686
   205
   i       : no. of subgoal
berghofe@1686
   206
**************************************************************)
berghofe@1686
   207
nipkow@1030
   208
fun inst_lift Ts t (T,U,pos) state lift i =
clasohm@0
   209
  let val sg = #sign(rep_thm state)
clasohm@0
   210
      val tsig = #tsig(Sign.rep_sg sg)
nipkow@1030
   211
      val cntxt = mk_cntxt Ts t pos (T-->U) (#maxidx(rep_thm lift))
lcp@231
   212
      val cu = cterm_of sg cntxt
lcp@231
   213
      val uT = #T(rep_cterm cu)
lcp@231
   214
      val cP' = cterm_of sg (Var(P,uT))
clasohm@0
   215
      val ixnTs = Type.typ_match tsig ([],(PT,uT));
lcp@231
   216
      val ixncTs = map (fn (x,y) => (x,ctyp_of sg y)) ixnTs;
clasohm@0
   217
  in instantiate (ixncTs, [(cP',cu)]) lift end;
clasohm@0
   218
clasohm@0
   219
berghofe@1686
   220
(*************************************************************
berghofe@1686
   221
   instantiate split theorem
berghofe@1686
   222
berghofe@1686
   223
   Ts    : types of parameters
berghofe@1686
   224
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
   225
           the split theorem is applied to (see cmap)
berghofe@4232
   226
   tt    : the term  Const(key,..) $ ...
berghofe@1686
   227
   thm   : the split theorem
berghofe@1686
   228
   TB    : type of body of P(...)
berghofe@1686
   229
   state : current proof state
berghofe@4232
   230
   i     : number of subgoal
berghofe@1686
   231
**************************************************************)
berghofe@1686
   232
berghofe@4232
   233
fun inst_split Ts t tt thm TB state i =
berghofe@4232
   234
  let val _ $ ((Var (P2, PT2)) $ _) $ _ = concl_of thm;
berghofe@1686
   235
      val sg = #sign(rep_thm state)
berghofe@1686
   236
      val tsig = #tsig(Sign.rep_sg sg)
berghofe@4232
   237
      val cntxt = mk_cntxt_splitthm t tt TB;
berghofe@4236
   238
      val T = fastype_of1 (Ts, cntxt);
berghofe@4232
   239
      val ixnTs = Type.typ_match tsig ([],(PT2, T))
berghofe@4232
   240
      val abss = foldl (fn (t, T) => Abs ("", T, t))
berghofe@4232
   241
  in
berghofe@4232
   242
    term_lift_inst_rule (state, i, ixnTs, [((P2, T), abss (cntxt, Ts))], thm)
berghofe@4232
   243
  end;
berghofe@1686
   244
berghofe@1686
   245
berghofe@1686
   246
(*****************************************************************************
berghofe@1686
   247
   The split-tactic
berghofe@1686
   248
   
berghofe@1686
   249
   splits : list of split-theorems to be tried
berghofe@1686
   250
   i      : number of subgoal the tactic should be applied to
berghofe@1686
   251
*****************************************************************************)
berghofe@1686
   252
clasohm@0
   253
fun split_tac [] i = no_tac
clasohm@0
   254
  | split_tac splits i =
nipkow@3918
   255
  let fun const(thm) =
nipkow@3918
   256
            (case concl_of thm of _$(t as _$lhs)$_ =>
nipkow@3918
   257
               (case strip_comb lhs of (Const(a,_),args) =>
nipkow@3918
   258
                  (a,(thm,fastype_of t,length args))
nipkow@3918
   259
                | _ => error("Wrong format for split rule"))
nipkow@3918
   260
             | _ => error("Wrong format for split rule"))
clasohm@0
   261
      val cmap = map const splits;
paulson@3537
   262
      fun lift_tac Ts t p st = (rtac (inst_lift Ts t p st trlift i) i) st
paulson@3537
   263
      fun lift_split_tac st = st |>
paulson@3537
   264
            let val (Ts,t,splits) = select cmap st i
nipkow@1030
   265
            in case splits of
nipkow@1030
   266
                 [] => no_tac
berghofe@1721
   267
               | (thm,apsns,pos,TB,tt)::_ =>
nipkow@1030
   268
                   (case apsns of
paulson@3537
   269
                      [] => (fn state => state |>
berghofe@4232
   270
			           compose_tac (false, inst_split Ts t tt thm TB state i, 0) i)
paulson@3537
   271
                    | p::_ => EVERY[lift_tac Ts t p,
nipkow@1030
   272
                                    rtac reflexive_thm (i+1),
paulson@3537
   273
                                    lift_split_tac])
nipkow@1030
   274
            end
paulson@3537
   275
  in COND (has_fewer_prems i) no_tac 
paulson@3537
   276
          (rtac iffD i THEN lift_split_tac)
clasohm@0
   277
  end;
clasohm@0
   278
clasohm@0
   279
in split_tac end;
berghofe@1721
   280
oheimb@4189
   281
oheimb@4202
   282
fun mk_case_split_asm_tac split_tac 
oheimb@4202
   283
			  (disjE,conjE,exE,contrapos,contrapos2,notnotD) = 
oheimb@4189
   284
let
oheimb@4189
   285
oheimb@4189
   286
(*****************************************************************************
oheimb@4189
   287
   The split-tactic for premises
oheimb@4189
   288
   
oheimb@4189
   289
   splits : list of split-theorems to be tried
oheimb@4189
   290
   i      : number of subgoal the tactic should be applied to
oheimb@4189
   291
*****************************************************************************)
oheimb@4189
   292
oheimb@4202
   293
fun split_asm_tac []     = K no_tac
oheimb@4202
   294
  | split_asm_tac splits = 
oheimb@4189
   295
  let fun const thm =
oheimb@4189
   296
            (case concl_of thm of Const ("Trueprop",_)$
oheimb@4189
   297
				 (Const ("op =", _)$(Var _$t)$_) =>
oheimb@4189
   298
               (case strip_comb t of (Const(a,_),_) => a
oheimb@4189
   299
                | _ => error("Wrong format for split rule"))
oheimb@4189
   300
             | _ =>    error("Wrong format for split rule"))
oheimb@4189
   301
      val cname_list = map const splits;
oheimb@4189
   302
      fun is_case (a,_) = a mem cname_list;
oheimb@4189
   303
      fun tac (t,i) = 
oheimb@4189
   304
	  let val n = find_index (exists_Const is_case) 
oheimb@4189
   305
				 (Logic.strip_assums_hyp t);
oheimb@4189
   306
	      fun first_prem_is_disj (Const ("==>", _) $ (Const ("Trueprop", _)
oheimb@4189
   307
				 $ (Const ("op |", _) $ _ $ _ )) $ _ ) = true
oheimb@4202
   308
	      |   first_prem_is_disj (Const("all",_)$Abs(_,_,t)) = 
oheimb@4202
   309
					first_prem_is_disj t
oheimb@4189
   310
	      |   first_prem_is_disj _ = false;
oheimb@4202
   311
	      fun flat_prems_tac i = SUBGOAL (fn (t,i) => 
oheimb@4189
   312
				   (if first_prem_is_disj t
oheimb@4189
   313
				    then EVERY[etac disjE i, rotate_tac ~1 i,
oheimb@4189
   314
					       rotate_tac ~1  (i+1),
oheimb@4189
   315
					       flat_prems_tac (i+1)]
oheimb@4189
   316
				    else all_tac) 
oheimb@4189
   317
				   THEN REPEAT (eresolve_tac [conjE,exE] i)
oheimb@4202
   318
				   THEN REPEAT (dresolve_tac [notnotD]   i)) i;
oheimb@4189
   319
	  in if n<0 then no_tac else DETERM (EVERY'
oheimb@4189
   320
		[rotate_tac n, etac contrapos2,
oheimb@4189
   321
		 split_tac splits, 
oheimb@4189
   322
		 rotate_tac ~1, etac contrapos, rotate_tac ~1, 
oheimb@4202
   323
		 flat_prems_tac] i)
oheimb@4189
   324
	  end;
oheimb@4189
   325
  in SUBGOAL tac
oheimb@4189
   326
  end;
oheimb@4189
   327
oheimb@4202
   328
in split_asm_tac end;
oheimb@4189
   329
oheimb@4189
   330
berghofe@1721
   331
in
berghofe@1721
   332
wenzelm@4519
   333
fun mk_case_split_tac iffD = mk_case_split_tac_2 iffD int_ord;
berghofe@1721
   334
wenzelm@4519
   335
fun mk_case_split_inside_tac iffD = mk_case_split_tac_2 iffD (rev_order o int_ord);
berghofe@1721
   336
oheimb@4202
   337
val mk_case_split_asm_tac = mk_case_split_asm_tac;
oheimb@4189
   338
berghofe@1721
   339
end;