src/Provers/splitter.ML
author nipkow
Sun Apr 16 11:56:11 1995 +0200 (1995-04-16)
changeset 1064 5d6fb2c938e0
parent 1030 1d8fa2fc4b9c
child 1686 c67d543bc395
permissions -rw-r--r--
Fixed bug.
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
nipkow@943
    22
val lift =
nipkow@943
    23
  let val ct = read_cterm (#sign(rep_thm iffD))
nipkow@943
    24
           ("[| !!x::'b::logic. Q(x) == R(x) |] ==> \
nipkow@943
    25
            \P(%x.Q(x)) == P(%x.R(x))::'a::logic",propT)
nipkow@943
    26
  in prove_goalw_cterm [] ct
nipkow@943
    27
     (fn [prem] => [rewtac prem, rtac reflexive_thm 1])
nipkow@943
    28
  end;
nipkow@4
    29
clasohm@0
    30
val trlift = lift RS transitive_thm;
clasohm@0
    31
val _ $ (Var(P,PT)$_) $ _ = concl_of trlift;
clasohm@0
    32
clasohm@0
    33
nipkow@1030
    34
fun mk_cntxt Ts t pos T maxi =
nipkow@1030
    35
  let fun var (t,i) = Var(("X",i),type_of1(Ts,t));
nipkow@1030
    36
      fun down [] t i = Bound 0
nipkow@1030
    37
        | down (p::ps) t i =
nipkow@1030
    38
            let val (h,ts) = strip_comb t
nipkow@1030
    39
                val v1 = map var (take(p,ts) ~~ (i upto (i+p-1)))
nipkow@1030
    40
                val u::us = drop(p,ts)
nipkow@1030
    41
                val v2 = map var (us ~~ ((i+p) upto (i+length(ts)-2)))
nipkow@1030
    42
      in list_comb(h,v1@[down ps u (i+length ts)]@v2) end;
nipkow@1030
    43
  in Abs("", T, down (rev pos) t maxi) end;
nipkow@1030
    44
nipkow@1030
    45
fun add_lbnos(is,t) = add_loose_bnos(t,0,is);
nipkow@1030
    46
nipkow@1064
    47
(* check if the innermost quantifier that needs to be removed
nipkow@1064
    48
   has a body of type T; otherwise the expansion thm will fail later on
nipkow@1064
    49
*)
nipkow@1064
    50
fun type_test(T,lbnos,apsns) =
nipkow@1064
    51
  let val (_,U,_) = nth_elem(min lbnos,apsns)
nipkow@1064
    52
  in T=U end;
clasohm@0
    53
nipkow@1030
    54
fun mk_split_pack(thm,T,n,ts,apsns) =
nipkow@1064
    55
  if n > length ts then []
nipkow@1064
    56
  else let val lev = length apsns
nipkow@1030
    57
           val lbnos = foldl add_lbnos ([],take(n,ts))
nipkow@1030
    58
           val flbnos = filter (fn i => i < lev) lbnos
nipkow@1064
    59
       in if null flbnos then [(thm,[])]
nipkow@1064
    60
          else if type_test(T,flbnos,apsns) then [(thm, rev apsns)] else []
nipkow@1064
    61
       end;
clasohm@0
    62
nipkow@1030
    63
fun split_posns cmap Ts t =
nipkow@1030
    64
  let fun posns Ts pos apsns (Abs(_,T,t)) =
nipkow@1030
    65
            let val U = fastype_of1(T::Ts,t)
nipkow@1030
    66
            in posns (T::Ts) (0::pos) ((T,U,pos)::apsns) t end
nipkow@1030
    67
        | posns Ts pos apsns t =
nipkow@1030
    68
            let val (h,ts) = strip_comb t
nipkow@1030
    69
                fun iter((i,a),t) = (i+1, (posns Ts (i::pos) apsns t) @ a);
nipkow@1030
    70
                val a = case h of
nipkow@1030
    71
                  Const(c,_) =>
nipkow@1030
    72
                    (case assoc(cmap,c) of
nipkow@1030
    73
                       Some(thm,T,n) => mk_split_pack(thm,T,n,ts,apsns)
nipkow@1030
    74
                     | None => [])
nipkow@1030
    75
                | _ => []
nipkow@1030
    76
             in snd(foldl iter ((0,a),ts)) end
nipkow@1030
    77
  in posns Ts [] [] t end;
clasohm@0
    78
clasohm@0
    79
fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
clasohm@0
    80
nipkow@1030
    81
fun shorter((_,ps),(_,qs)) = length ps <= length qs;
clasohm@0
    82
nipkow@1030
    83
fun select cmap state i =
nipkow@1030
    84
  let val goali = nth_subgoal i state
nipkow@1030
    85
      val Ts = rev(map #2 (Logic.strip_params goali))
nipkow@1030
    86
      val _ $ t $ _ = Logic.strip_assums_concl goali;
nipkow@1030
    87
  in (Ts,t,sort shorter (split_posns cmap Ts t)) end;
nipkow@1030
    88
nipkow@1030
    89
fun inst_lift Ts t (T,U,pos) state lift i =
clasohm@0
    90
  let val sg = #sign(rep_thm state)
clasohm@0
    91
      val tsig = #tsig(Sign.rep_sg sg)
nipkow@1030
    92
      val cntxt = mk_cntxt Ts t pos (T-->U) (#maxidx(rep_thm lift))
lcp@231
    93
      val cu = cterm_of sg cntxt
lcp@231
    94
      val uT = #T(rep_cterm cu)
lcp@231
    95
      val cP' = cterm_of sg (Var(P,uT))
clasohm@0
    96
      val ixnTs = Type.typ_match tsig ([],(PT,uT));
lcp@231
    97
      val ixncTs = map (fn (x,y) => (x,ctyp_of sg y)) ixnTs;
clasohm@0
    98
  in instantiate (ixncTs, [(cP',cu)]) lift end;
clasohm@0
    99
clasohm@0
   100
clasohm@0
   101
fun split_tac [] i = no_tac
clasohm@0
   102
  | split_tac splits i =
nipkow@1030
   103
  let fun const(thm) = let val _$(t as _$lhs)$_ = concl_of thm
clasohm@0
   104
                           val (Const(a,_),args) = strip_comb lhs
nipkow@1030
   105
                       in (a,(thm,fastype_of t,length args)) end
clasohm@0
   106
      val cmap = map const splits;
nipkow@1030
   107
      fun lift Ts t p state = rtac (inst_lift Ts t p state trlift i) i
nipkow@1030
   108
      fun lift_split state =
nipkow@1030
   109
            let val (Ts,t,splits) = select cmap state i
nipkow@1030
   110
            in case splits of
nipkow@1030
   111
                 [] => no_tac
nipkow@1030
   112
               | (thm,apsns)::_ =>
nipkow@1030
   113
                   (case apsns of
nipkow@1030
   114
                      [] => rtac thm i
nipkow@1030
   115
                    | p::_ => EVERY[STATE(lift Ts t p),
nipkow@1030
   116
                                    rtac reflexive_thm (i+1),
nipkow@1030
   117
                                    STATE lift_split])
nipkow@1030
   118
            end
clasohm@0
   119
  in STATE(fn thm =>
nipkow@1030
   120
       if i <= nprems_of thm then rtac iffD i THEN STATE lift_split
clasohm@0
   121
       else no_tac)
clasohm@0
   122
  end;
clasohm@0
   123
clasohm@0
   124
in split_tac end;