src/Provers/splitter.ML
author berghofe
Thu Apr 25 13:03:57 1996 +0200 (1996-04-25 ago)
changeset 1686 c67d543bc395
parent 1064 5d6fb2c938e0
child 1721 445654b6cb95
permissions -rw-r--r--
Added functions mk_cntxt_splitthm and inst_split which instantiate
the split-rule before it is applied.
Inserted some comments.
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
clasohm@0
    12
by(case_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
clasohm@0
    19
fun mk_case_split_tac iffD =
clasohm@0
    20
let
clasohm@0
    21
berghofe@1686
    22
berghofe@1686
    23
(************************************************************
berghofe@1686
    24
   Create lift-theorem "trlift" :
berghofe@1686
    25
berghofe@1686
    26
   [| !! x. Q(x)==R(x) ; P(R) == C |] ==> P(Q)==C
berghofe@1686
    27
berghofe@1686
    28
*************************************************************)
berghofe@1686
    29
 
nipkow@943
    30
val lift =
nipkow@943
    31
  let val ct = read_cterm (#sign(rep_thm iffD))
nipkow@943
    32
           ("[| !!x::'b::logic. Q(x) == R(x) |] ==> \
nipkow@943
    33
            \P(%x.Q(x)) == P(%x.R(x))::'a::logic",propT)
nipkow@943
    34
  in prove_goalw_cterm [] ct
nipkow@943
    35
     (fn [prem] => [rewtac prem, rtac reflexive_thm 1])
nipkow@943
    36
  end;
nipkow@4
    37
clasohm@0
    38
val trlift = lift RS transitive_thm;
clasohm@0
    39
val _ $ (Var(P,PT)$_) $ _ = concl_of trlift;
clasohm@0
    40
clasohm@0
    41
berghofe@1686
    42
(************************************************************************ 
berghofe@1686
    43
   Set up term for instantiation of P in the lift-theorem
berghofe@1686
    44
   
berghofe@1686
    45
   Ts    : types of parameters (i.e. variables bound by meta-quantifiers)
berghofe@1686
    46
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
    47
           the lift theorem is applied to (see select)
berghofe@1686
    48
   pos   : "path" leading to abstraction, coded as a list
berghofe@1686
    49
   T     : type of body of P(...)
berghofe@1686
    50
   maxi  : maximum index of Vars
berghofe@1686
    51
*************************************************************************)
berghofe@1686
    52
nipkow@1030
    53
fun mk_cntxt Ts t pos T maxi =
nipkow@1030
    54
  let fun var (t,i) = Var(("X",i),type_of1(Ts,t));
nipkow@1030
    55
      fun down [] t i = Bound 0
nipkow@1030
    56
        | down (p::ps) t i =
nipkow@1030
    57
            let val (h,ts) = strip_comb t
nipkow@1030
    58
                val v1 = map var (take(p,ts) ~~ (i upto (i+p-1)))
nipkow@1030
    59
                val u::us = drop(p,ts)
nipkow@1030
    60
                val v2 = map var (us ~~ ((i+p) upto (i+length(ts)-2)))
nipkow@1030
    61
      in list_comb(h,v1@[down ps u (i+length ts)]@v2) end;
nipkow@1030
    62
  in Abs("", T, down (rev pos) t maxi) end;
nipkow@1030
    63
berghofe@1686
    64
berghofe@1686
    65
(************************************************************************ 
berghofe@1686
    66
   Set up term for instantiation of P in the split-theorem
berghofe@1686
    67
   P(...) == rhs
berghofe@1686
    68
berghofe@1686
    69
   Ts    : types of parameters (i.e. variables bound by meta-quantifiers)
berghofe@1686
    70
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
    71
           the split theorem is applied to (see select)
berghofe@1686
    72
   pos   : "path" leading to body of P(...), coded as a list
berghofe@1686
    73
   T     : type of body of P(...)
berghofe@1686
    74
   maxi  : maximum index of Vars
berghofe@1686
    75
berghofe@1686
    76
   bvars : type of variables bound by other than meta-quantifiers
berghofe@1686
    77
*************************************************************************)
berghofe@1686
    78
berghofe@1686
    79
fun mk_cntxt_splitthm Ts t pos T maxi =
berghofe@1686
    80
  let fun down [] t i bvars = Bound (length bvars)
berghofe@1686
    81
        | down (p::ps) (Abs(v,T2,t)) i bvars = Abs(v,T2,down ps t i (T2::bvars))
berghofe@1686
    82
        | down (p::ps) t i bvars =
berghofe@1686
    83
            let val vars = map Bound (0 upto ((length bvars)-1))
berghofe@1686
    84
                val (h,ts) = strip_comb t
berghofe@1686
    85
                fun var (t,i) = list_comb(Var(("X",i),bvars ---> (type_of1(bvars @ Ts,t))),vars);
berghofe@1686
    86
                val v1 = map var (take(p,ts) ~~ (i upto (i+p-1)))
berghofe@1686
    87
                val u::us = drop(p,ts)
berghofe@1686
    88
                val v2 = map var (us ~~ ((i+p) upto (i+length(ts)-2)))
berghofe@1686
    89
            in list_comb(h,v1@[down ps u (i+length ts) bvars]@v2) end;
berghofe@1686
    90
  in Abs("",T,down (rev pos) t maxi []) end;
berghofe@1686
    91
berghofe@1686
    92
berghofe@1686
    93
(* add all loose bound variables in t to list is *)
nipkow@1030
    94
fun add_lbnos(is,t) = add_loose_bnos(t,0,is);
nipkow@1030
    95
nipkow@1064
    96
(* check if the innermost quantifier that needs to be removed
nipkow@1064
    97
   has a body of type T; otherwise the expansion thm will fail later on
nipkow@1064
    98
*)
nipkow@1064
    99
fun type_test(T,lbnos,apsns) =
nipkow@1064
   100
  let val (_,U,_) = nth_elem(min lbnos,apsns)
nipkow@1064
   101
  in T=U end;
clasohm@0
   102
berghofe@1686
   103
(*************************************************************************
berghofe@1686
   104
   Create a "split_pack".
berghofe@1686
   105
berghofe@1686
   106
   thm   : the relevant split-theorem, i.e. P(...) == rhs , where P(...)
berghofe@1686
   107
           is of the form
berghofe@1686
   108
           P( Const(key,...) $ t_1 $ ... $ t_n )      (e.g. key = "if")
berghofe@1686
   109
   T     : type of P(...)
berghofe@1686
   110
   n     : number of arguments expected by Const(key,...)
berghofe@1686
   111
   ts    : list of arguments actually found
berghofe@1686
   112
   apsns : list of tuples of the form (T,U,pos), one tuple for each
berghofe@1686
   113
           abstraction that is encountered on the way to the position where 
berghofe@1686
   114
           Const(key, ...) $ ...  occurs, where
berghofe@1686
   115
           T   : type of the variable bound by the abstraction
berghofe@1686
   116
           U   : type of the abstraction's body
berghofe@1686
   117
           pos : "path" leading to the body of the abstraction
berghofe@1686
   118
   pos   : "path" leading to the position where Const(key, ...) $ ...  occurs.
berghofe@1686
   119
   TB    : type of  Const(key,...) $ t_1 $ ... $ t_n
berghofe@1686
   120
berghofe@1686
   121
   A split pack is a tuple of the form
berghofe@1686
   122
   (thm, apsns, pos, TB)
berghofe@1686
   123
   Note : apsns is reversed, so that the outermost quantifier's position
berghofe@1686
   124
          comes first ! If the terms in ts don't contain variables bound
berghofe@1686
   125
          by other than meta-quantifiers, apsns is empty, because no further
berghofe@1686
   126
          lifting is required before applying the split-theorem.
berghofe@1686
   127
******************************************************************************) 
berghofe@1686
   128
berghofe@1686
   129
fun mk_split_pack(thm,T,n,ts,apsns,pos,TB) =
nipkow@1064
   130
  if n > length ts then []
nipkow@1064
   131
  else let val lev = length apsns
nipkow@1030
   132
           val lbnos = foldl add_lbnos ([],take(n,ts))
nipkow@1030
   133
           val flbnos = filter (fn i => i < lev) lbnos
berghofe@1686
   134
       in if null flbnos then [(thm,[],pos,TB)]
berghofe@1686
   135
          else if type_test(T,flbnos,apsns) then [(thm, rev apsns,pos,TB)] else []
nipkow@1064
   136
       end;
clasohm@0
   137
berghofe@1686
   138
berghofe@1686
   139
(****************************************************************************
berghofe@1686
   140
   Recursively scans term for occurences of Const(key,...) $ ...
berghofe@1686
   141
   Returns a list of "split-packs" (one for each occurence of Const(key,...) )
berghofe@1686
   142
berghofe@1686
   143
   cmap : association list of split-theorems that should be tried.
berghofe@1686
   144
          The elements have the format (key,(thm,T,n)) , where
berghofe@1686
   145
          key : the theorem's key constant ( Const(key,...) $ ... )
berghofe@1686
   146
          thm : the theorem itself
berghofe@1686
   147
          T   : type of P( Const(key,...) $ ... )
berghofe@1686
   148
          n   : number of arguments expected by Const(key,...)
berghofe@1686
   149
   Ts   : types of parameters
berghofe@1686
   150
   t    : the term to be scanned
berghofe@1686
   151
******************************************************************************)
berghofe@1686
   152
nipkow@1030
   153
fun split_posns cmap Ts t =
nipkow@1030
   154
  let fun posns Ts pos apsns (Abs(_,T,t)) =
nipkow@1030
   155
            let val U = fastype_of1(T::Ts,t)
nipkow@1030
   156
            in posns (T::Ts) (0::pos) ((T,U,pos)::apsns) t end
nipkow@1030
   157
        | posns Ts pos apsns t =
nipkow@1030
   158
            let val (h,ts) = strip_comb t
nipkow@1030
   159
                fun iter((i,a),t) = (i+1, (posns Ts (i::pos) apsns t) @ a);
nipkow@1030
   160
                val a = case h of
nipkow@1030
   161
                  Const(c,_) =>
nipkow@1030
   162
                    (case assoc(cmap,c) of
berghofe@1686
   163
                       Some(thm,T,n) => mk_split_pack(thm,T,n,ts,apsns,pos,type_of1(Ts,t))
nipkow@1030
   164
                     | None => [])
nipkow@1030
   165
                | _ => []
nipkow@1030
   166
             in snd(foldl iter ((0,a),ts)) end
nipkow@1030
   167
  in posns Ts [] [] t end;
clasohm@0
   168
berghofe@1686
   169
clasohm@0
   170
fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
clasohm@0
   171
berghofe@1686
   172
fun shorter((_,ps,pos,_),(_,qs,qos,_)) =
berghofe@1686
   173
  let val ms = length ps and ns = length qs
berghofe@1686
   174
  in ms < ns orelse (ms = ns andalso length pos >= length qos) end;
berghofe@1686
   175
berghofe@1686
   176
berghofe@1686
   177
(************************************************************
berghofe@1686
   178
   call split_posns with appropriate parameters
berghofe@1686
   179
*************************************************************)
clasohm@0
   180
nipkow@1030
   181
fun select cmap state i =
nipkow@1030
   182
  let val goali = nth_subgoal i state
nipkow@1030
   183
      val Ts = rev(map #2 (Logic.strip_params goali))
nipkow@1030
   184
      val _ $ t $ _ = Logic.strip_assums_concl goali;
nipkow@1030
   185
  in (Ts,t,sort shorter (split_posns cmap Ts t)) end;
nipkow@1030
   186
berghofe@1686
   187
berghofe@1686
   188
(*************************************************************
berghofe@1686
   189
   instantiate lift theorem
berghofe@1686
   190
berghofe@1686
   191
   if t is of the form
berghofe@1686
   192
   ... ( Const(...,...) $ Abs( .... ) ) ...
berghofe@1686
   193
   then
berghofe@1686
   194
   P = %a.  ... ( Const(...,...) $ a ) ...
berghofe@1686
   195
   where a has type T --> U
berghofe@1686
   196
berghofe@1686
   197
   Ts      : types of parameters
berghofe@1686
   198
   t       : lefthand side of meta-equality in subgoal
berghofe@1686
   199
             the split theorem is applied to (see cmap)
berghofe@1686
   200
   T,U,pos : see mk_split_pack
berghofe@1686
   201
   state   : current proof state
berghofe@1686
   202
   lift    : the lift theorem
berghofe@1686
   203
   i       : no. of subgoal
berghofe@1686
   204
**************************************************************)
berghofe@1686
   205
nipkow@1030
   206
fun inst_lift Ts t (T,U,pos) state lift i =
clasohm@0
   207
  let val sg = #sign(rep_thm state)
clasohm@0
   208
      val tsig = #tsig(Sign.rep_sg sg)
nipkow@1030
   209
      val cntxt = mk_cntxt Ts t pos (T-->U) (#maxidx(rep_thm lift))
lcp@231
   210
      val cu = cterm_of sg cntxt
lcp@231
   211
      val uT = #T(rep_cterm cu)
lcp@231
   212
      val cP' = cterm_of sg (Var(P,uT))
clasohm@0
   213
      val ixnTs = Type.typ_match tsig ([],(PT,uT));
lcp@231
   214
      val ixncTs = map (fn (x,y) => (x,ctyp_of sg y)) ixnTs;
clasohm@0
   215
  in instantiate (ixncTs, [(cP',cu)]) lift end;
clasohm@0
   216
clasohm@0
   217
berghofe@1686
   218
(*************************************************************
berghofe@1686
   219
   instantiate split theorem
berghofe@1686
   220
berghofe@1686
   221
   Ts    : types of parameters
berghofe@1686
   222
   t     : lefthand side of meta-equality in subgoal
berghofe@1686
   223
           the split theorem is applied to (see cmap)
berghofe@1686
   224
   pos   : "path" to the body of P(...)
berghofe@1686
   225
   thm   : the split theorem
berghofe@1686
   226
   TB    : type of body of P(...)
berghofe@1686
   227
   state : current proof state
berghofe@1686
   228
**************************************************************)
berghofe@1686
   229
berghofe@1686
   230
fun inst_split Ts t pos thm TB state =
berghofe@1686
   231
  let val _$((Var(P2,PT2))$_)$_ = concl_of thm
berghofe@1686
   232
      val sg = #sign(rep_thm state)
berghofe@1686
   233
      val tsig = #tsig(Sign.rep_sg sg)
berghofe@1686
   234
      val cntxt = mk_cntxt_splitthm Ts t pos TB (#maxidx(rep_thm thm))
berghofe@1686
   235
      val cu = cterm_of sg cntxt
berghofe@1686
   236
      val uT = #T(rep_cterm cu)
berghofe@1686
   237
      val cP' = cterm_of sg (Var(P2,uT))
berghofe@1686
   238
      val ixnTs = Type.typ_match tsig ([],(PT2,uT));
berghofe@1686
   239
      val ixncTs = map (fn (x,y) => (x,ctyp_of sg y)) ixnTs;
berghofe@1686
   240
  in instantiate (ixncTs, [(cP',cu)]) thm end;
berghofe@1686
   241
berghofe@1686
   242
berghofe@1686
   243
(*****************************************************************************
berghofe@1686
   244
   The split-tactic
berghofe@1686
   245
   
berghofe@1686
   246
   splits : list of split-theorems to be tried
berghofe@1686
   247
   i      : number of subgoal the tactic should be applied to
berghofe@1686
   248
*****************************************************************************)
berghofe@1686
   249
clasohm@0
   250
fun split_tac [] i = no_tac
clasohm@0
   251
  | split_tac splits i =
nipkow@1030
   252
  let fun const(thm) = let val _$(t as _$lhs)$_ = concl_of thm
clasohm@0
   253
                           val (Const(a,_),args) = strip_comb lhs
nipkow@1030
   254
                       in (a,(thm,fastype_of t,length args)) end
clasohm@0
   255
      val cmap = map const splits;
nipkow@1030
   256
      fun lift Ts t p state = rtac (inst_lift Ts t p state trlift i) i
nipkow@1030
   257
      fun lift_split state =
nipkow@1030
   258
            let val (Ts,t,splits) = select cmap state i
nipkow@1030
   259
            in case splits of
nipkow@1030
   260
                 [] => no_tac
berghofe@1686
   261
               | (thm,apsns,pos,TB)::_ =>
nipkow@1030
   262
                   (case apsns of
berghofe@1686
   263
                      [] => STATE(fn state => rtac (inst_split Ts t pos thm TB state) i)
nipkow@1030
   264
                    | p::_ => EVERY[STATE(lift Ts t p),
nipkow@1030
   265
                                    rtac reflexive_thm (i+1),
nipkow@1030
   266
                                    STATE lift_split])
nipkow@1030
   267
            end
clasohm@0
   268
  in STATE(fn thm =>
nipkow@1030
   269
       if i <= nprems_of thm then rtac iffD i THEN STATE lift_split
clasohm@0
   270
       else no_tac)
clasohm@0
   271
  end;
clasohm@0
   272
clasohm@0
   273
in split_tac end;