src/Provers/splitter.ML
author lcp
Tue Jan 18 16:37:12 1994 +0100 (1994-01-18)
changeset 231 cb6a24451544
parent 4 2695ba9b40f7
child 927 305e7cfda869
permissions -rw-r--r--
Updated refs to old Sign functions
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
clasohm@0
    22
val lift = prove_goal Pure.thy
clasohm@0
    23
  "[| !!x::'b::logic. Q(x) == R(x) |] ==> P(%x.Q(x)) == P(%x.R(x))::'a::logic"
clasohm@0
    24
  (fn [prem] => [rewtac prem, rtac reflexive_thm 1]);
nipkow@4
    25
clasohm@0
    26
val trlift = lift RS transitive_thm;
clasohm@0
    27
val _ $ (Var(P,PT)$_) $ _ = concl_of trlift;
clasohm@0
    28
clasohm@0
    29
fun contains cmap =
clasohm@0
    30
  let fun isin i (Abs(_,_,b)) = isin 0 b
clasohm@0
    31
        | isin i (s$t) = isin (i+1) s orelse isin 0 t
clasohm@0
    32
        | isin i (Const(c,_)) = (case assoc(cmap,c) of
clasohm@0
    33
                                   Some(n,_) => n <= i
clasohm@0
    34
                                 | None => false)
clasohm@0
    35
        | isin _ _ = false
clasohm@0
    36
  in isin 0 end;
clasohm@0
    37
clasohm@0
    38
fun mk_context cmap Ts maxi t =
clasohm@0
    39
  let fun var (t,i) = Var(("X",i),type_of1(Ts,t))
clasohm@0
    40
clasohm@0
    41
      fun mka((None,i,ts),t) = if contains cmap t
clasohm@0
    42
            then let val (u,T,j) = mk(t,i) in (Some(T),j,ts@[u]) end
clasohm@0
    43
            else (None,i+1,ts@[var(t,i)])
clasohm@0
    44
        | mka((some,i,ts),t) = (some,i+1,ts@[var(t,i)])
clasohm@0
    45
      and mk(t as Abs _, i) = (Bound 0,type_of1(Ts,t),i)
clasohm@0
    46
        | mk(t,i) =
clasohm@0
    47
            let val (f,ts) = strip_comb t
clasohm@0
    48
                val (Some(T),j,us) = foldl mka ((None,i,[]),ts)
clasohm@0
    49
            in (list_comb(f,us),T,j) end
clasohm@0
    50
clasohm@0
    51
      val (u,T,_) = mk(t,maxi+1)
clasohm@0
    52
  in Abs("",T,u) end;
clasohm@0
    53
clasohm@0
    54
fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
clasohm@0
    55
clasohm@0
    56
fun goal_concl i thm = Logic.strip_assums_concl(nth_subgoal i thm);
clasohm@0
    57
clasohm@0
    58
fun inst_lift cmap state lift i =
clasohm@0
    59
  let val sg = #sign(rep_thm state)
clasohm@0
    60
      val tsig = #tsig(Sign.rep_sg sg)
clasohm@0
    61
      val goali = nth_subgoal i state
clasohm@0
    62
      val Ts = map #2 (Logic.strip_params goali)
clasohm@0
    63
      val _ $ t $ _ = Logic.strip_assums_concl goali;
clasohm@0
    64
      val cntxt = mk_context cmap (rev Ts) (#maxidx(rep_thm lift)) t
lcp@231
    65
      val cu = cterm_of sg cntxt
lcp@231
    66
      val uT = #T(rep_cterm cu)
lcp@231
    67
      val cP' = cterm_of sg (Var(P,uT))
clasohm@0
    68
      val ixnTs = Type.typ_match tsig ([],(PT,uT));
lcp@231
    69
      val ixncTs = map (fn (x,y) => (x,ctyp_of sg y)) ixnTs;
clasohm@0
    70
  in instantiate (ixncTs, [(cP',cu)]) lift end;
clasohm@0
    71
clasohm@0
    72
fun splittable al i thm =
clasohm@0
    73
  let val tm = goal_concl i thm
clasohm@0
    74
      fun nobound j k (Abs(_,_,t)) = nobound j (k+1) t
clasohm@0
    75
        | nobound j k (s$t) = nobound j k s andalso nobound j k t
clasohm@0
    76
        | nobound j k (Bound n) = n < k orelse k+j <= n
clasohm@0
    77
        | nobound _ _ _ = true;
clasohm@0
    78
      fun find j (None,t) = (case t of
clasohm@0
    79
            Abs(_,_,t) => find (j+1) (None,t)
clasohm@0
    80
          | _ => let val (h,args) = strip_comb t
clasohm@0
    81
                     fun no() = foldl (find j) (None,args)
clasohm@0
    82
                 in case h of
clasohm@0
    83
                      Const(c,_) =>
clasohm@0
    84
                        (case assoc(al,c) of
clasohm@0
    85
                           Some(n,thm) =>
clasohm@0
    86
                             if length args < n then no()
clasohm@0
    87
                             else if forall (nobound j 0) (take(n,args))
clasohm@0
    88
                                  then Some(thm)
clasohm@0
    89
                                  else no()
clasohm@0
    90
                         | None => no())
clasohm@0
    91
                    | _ => no()
clasohm@0
    92
                 end)
clasohm@0
    93
        | find _ (some,_) = some
clasohm@0
    94
  in find 0 (None,tm) end;
clasohm@0
    95
clasohm@0
    96
fun split_tac [] i = no_tac
clasohm@0
    97
  | split_tac splits i =
clasohm@0
    98
  let fun const(thm) = let val _$(_$lhs)$_ = concl_of thm
clasohm@0
    99
                           val (Const(a,_),args) = strip_comb lhs
clasohm@0
   100
                       in (a,(length args,thm)) end
clasohm@0
   101
      val cmap = map const splits;
clasohm@0
   102
      fun lift thm = rtac (inst_lift cmap thm trlift i) i
clasohm@0
   103
      fun lift_split thm =
clasohm@0
   104
            case splittable cmap i thm of
clasohm@0
   105
              Some(split) => rtac split i
clasohm@0
   106
            | None => EVERY[STATE lift, rtac reflexive_thm (i+1),
clasohm@0
   107
                            STATE lift_split]
clasohm@0
   108
  in STATE(fn thm =>
clasohm@0
   109
       if (i <= nprems_of thm)  andalso  contains cmap (goal_concl i thm)
clasohm@0
   110
       then EVERY[rtac iffD i, STATE lift_split]
clasohm@0
   111
       else no_tac)
clasohm@0
   112
  end;
clasohm@0
   113
clasohm@0
   114
in split_tac end;