src/Provers/splitter.ML
author nipkow
Wed Mar 08 14:35:26 1995 +0100 (1995-03-08 ago)
changeset 943 8477483f663f
parent 927 305e7cfda869
child 1030 1d8fa2fc4b9c
permissions -rw-r--r--
Replaced read by read_cterm.
nipkow@4
     1
(*  Title:      Provers/splitter
nipkow@4
     2
    ID:         $Id$
nipkow@4
     3
    Author:     Tobias Nipkow
nipkow@4
     4
    Copyright   1993  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
fun contains cmap =
clasohm@0
    34
  let fun isin i (Abs(_,_,b)) = isin 0 b
clasohm@0
    35
        | isin i (s$t) = isin (i+1) s orelse isin 0 t
clasohm@0
    36
        | isin i (Const(c,_)) = (case assoc(cmap,c) of
clasohm@0
    37
                                   Some(n,_) => n <= i
clasohm@0
    38
                                 | None => false)
clasohm@0
    39
        | isin _ _ = false
clasohm@0
    40
  in isin 0 end;
clasohm@0
    41
clasohm@0
    42
fun mk_context cmap Ts maxi t =
clasohm@0
    43
  let fun var (t,i) = Var(("X",i),type_of1(Ts,t))
clasohm@0
    44
clasohm@0
    45
      fun mka((None,i,ts),t) = if contains cmap t
clasohm@0
    46
            then let val (u,T,j) = mk(t,i) in (Some(T),j,ts@[u]) end
clasohm@0
    47
            else (None,i+1,ts@[var(t,i)])
clasohm@0
    48
        | mka((some,i,ts),t) = (some,i+1,ts@[var(t,i)])
clasohm@0
    49
      and mk(t as Abs _, i) = (Bound 0,type_of1(Ts,t),i)
clasohm@0
    50
        | mk(t,i) =
clasohm@0
    51
            let val (f,ts) = strip_comb t
clasohm@0
    52
                val (Some(T),j,us) = foldl mka ((None,i,[]),ts)
clasohm@0
    53
            in (list_comb(f,us),T,j) end
clasohm@0
    54
clasohm@0
    55
      val (u,T,_) = mk(t,maxi+1)
clasohm@0
    56
  in Abs("",T,u) end;
clasohm@0
    57
clasohm@0
    58
fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
clasohm@0
    59
clasohm@0
    60
fun goal_concl i thm = Logic.strip_assums_concl(nth_subgoal i thm);
clasohm@0
    61
clasohm@0
    62
fun inst_lift cmap state lift i =
clasohm@0
    63
  let val sg = #sign(rep_thm state)
clasohm@0
    64
      val tsig = #tsig(Sign.rep_sg sg)
clasohm@0
    65
      val goali = nth_subgoal i state
clasohm@0
    66
      val Ts = map #2 (Logic.strip_params goali)
clasohm@0
    67
      val _ $ t $ _ = Logic.strip_assums_concl goali;
clasohm@0
    68
      val cntxt = mk_context cmap (rev Ts) (#maxidx(rep_thm lift)) t
lcp@231
    69
      val cu = cterm_of sg cntxt
lcp@231
    70
      val uT = #T(rep_cterm cu)
lcp@231
    71
      val cP' = cterm_of sg (Var(P,uT))
clasohm@0
    72
      val ixnTs = Type.typ_match tsig ([],(PT,uT));
lcp@231
    73
      val ixncTs = map (fn (x,y) => (x,ctyp_of sg y)) ixnTs;
clasohm@0
    74
  in instantiate (ixncTs, [(cP',cu)]) lift end;
clasohm@0
    75
clasohm@0
    76
fun splittable al i thm =
clasohm@0
    77
  let val tm = goal_concl i thm
clasohm@0
    78
      fun nobound j k (Abs(_,_,t)) = nobound j (k+1) t
clasohm@0
    79
        | nobound j k (s$t) = nobound j k s andalso nobound j k t
clasohm@0
    80
        | nobound j k (Bound n) = n < k orelse k+j <= n
clasohm@0
    81
        | nobound _ _ _ = true;
clasohm@0
    82
      fun find j (None,t) = (case t of
clasohm@0
    83
            Abs(_,_,t) => find (j+1) (None,t)
clasohm@0
    84
          | _ => let val (h,args) = strip_comb t
clasohm@0
    85
                     fun no() = foldl (find j) (None,args)
clasohm@0
    86
                 in case h of
clasohm@0
    87
                      Const(c,_) =>
clasohm@0
    88
                        (case assoc(al,c) of
clasohm@0
    89
                           Some(n,thm) =>
clasohm@0
    90
                             if length args < n then no()
clasohm@0
    91
                             else if forall (nobound j 0) (take(n,args))
clasohm@0
    92
                                  then Some(thm)
clasohm@0
    93
                                  else no()
clasohm@0
    94
                         | None => no())
clasohm@0
    95
                    | _ => no()
clasohm@0
    96
                 end)
clasohm@0
    97
        | find _ (some,_) = some
clasohm@0
    98
  in find 0 (None,tm) end;
clasohm@0
    99
clasohm@0
   100
fun split_tac [] i = no_tac
clasohm@0
   101
  | split_tac splits i =
clasohm@0
   102
  let fun const(thm) = let val _$(_$lhs)$_ = concl_of thm
clasohm@0
   103
                           val (Const(a,_),args) = strip_comb lhs
clasohm@0
   104
                       in (a,(length args,thm)) end
clasohm@0
   105
      val cmap = map const splits;
clasohm@0
   106
      fun lift thm = rtac (inst_lift cmap thm trlift i) i
clasohm@0
   107
      fun lift_split thm =
clasohm@0
   108
            case splittable cmap i thm of
clasohm@0
   109
              Some(split) => rtac split i
clasohm@0
   110
            | None => EVERY[STATE lift, rtac reflexive_thm (i+1),
clasohm@0
   111
                            STATE lift_split]
clasohm@0
   112
  in STATE(fn thm =>
clasohm@0
   113
       if (i <= nprems_of thm)  andalso  contains cmap (goal_concl i thm)
clasohm@0
   114
       then EVERY[rtac iffD i, STATE lift_split]
clasohm@0
   115
       else no_tac)
clasohm@0
   116
  end;
clasohm@0
   117
clasohm@0
   118
in split_tac end;