src/Provers/nat_transitive.ML
author wenzelm
Fri, 07 Mar 1997 11:48:46 +0100
changeset 2754 59bd96046ad6
parent 2273 d1bcc10be8d7
child 2920 feab36851df3
permissions -rw-r--r--
moved settings comment to build;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2114
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
     1
(*  Title:      Provers/nat_transitive.ML
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
     2
    ID:         $Id$
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
     3
    Author:     Tobias Nipkow
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
     4
    Copyright   1996  TU Munich
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
     5
*)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
     6
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
     7
(***
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
     8
A very simple package for inequalities over nat.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
     9
It uses all premises of the form
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    10
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    11
t = u, t < u, t <= u, ~(t < u), ~(t <= u)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    12
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    13
where t and u must be of type nat, to
2273
d1bcc10be8d7 Fixed spelling error
paulson
parents: 2114
diff changeset
    14
1. either derive a contradiction,
2114
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    15
   in which case the conclusion can be any term,
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    16
2. or prove the conclusion, which must be of the same form as the premises.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    17
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    18
The package
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    19
- does not deal with the relation ~=
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    20
- treats `pred', +, *, ... as atomic terms. Hence it can prove
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    21
  [| x < y+z; y+z < u |] ==> Suc x < u
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    22
  but not
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    23
  [| x < y+z; z < u |] ==> Suc x < y+u
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    24
- takes only (in)equalities which are atomic premises into account. It does
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    25
  not deal with logical operators like -->, & etc. Hence it cannot prove 
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    26
  [| x < y+z & y+z < u |] ==> Suc x < u
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    27
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    28
In order not to fall foul of the above limitations, the following hints are
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    29
useful:
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    30
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    31
1. You may need to run `by(safe_tac HOL_cs)' in order to bring out the atomic
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    32
   premises.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    33
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    34
2. To get rid of ~= in the premises, it is advisable to use a rule like
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    35
   nat_neqE = "[| (m::nat) ~= n; m < n ==> P; n < m ==> P |] ==> P" : thm
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    36
   (the name nat_eqE is chosen in HOL), for example as follows:
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    37
   by(safe_tac (HOL_cs addSEs [nat_neqE])
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    38
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    39
3. To get rid of `pred', you may be able to do the following:
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    40
   expand `pred(m)' into `case m of 0 => 0 | Suc n => n' and use split_tac
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    41
   to turn the case-expressions into logical case distinctions. In HOL:
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    42
   simp_tac (... addsimps [pred_def] setloop (split_tac [expand_nat_case]))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    43
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    44
The basic tactic is `trans_tac'. In order to use `trans_tac' as a solver in
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    45
the simplifier, `cut_trans_tac' is also provided, which cuts the given thms
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    46
in as facts.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    47
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    48
Notes:
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    49
- It should easily be possible to adapt this package to other numeric types
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    50
  like int.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    51
- There is ample scope for optimizations, which so far have not proved
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    52
  necessary.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    53
- The code can be simplified by adding the negated conclusion to the
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    54
  premises to derive a contradiction. However, this would restrict the
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    55
  package to classical logics.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    56
***)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    57
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    58
(* The package works for arbitrary logics.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    59
   You just need to instantiate the following parameter structure.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    60
*)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    61
signature LESS_ARITH =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    62
sig
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    63
  val lessI:            thm (* n < Suc n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    64
  val zero_less_Suc:    thm (* 0 < Suc n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    65
  val less_reflE:       thm (* n < n ==> P *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    66
  val less_zeroE:       thm (* n < 0 ==> P *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    67
  val less_incr:        thm (* m < n ==> Suc m < Suc n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    68
  val less_decr:        thm (* Suc m < Suc n ==> m < n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    69
  val less_incr_rhs:    thm (* m < n ==> m < Suc n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    70
  val less_decr_lhs:    thm (* Suc m < n ==> m < n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    71
  val less_trans_Suc:   thm (* [| i < j; j < k |] ==> Suc i < k *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    72
  val leD:              thm (* m <= n ==> m < Suc n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    73
  val not_lessD:        thm (* ~(m < n) ==> n < Suc m *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    74
  val not_leD:          thm (* ~(m <= n) ==> n < m *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    75
  val eqD1:             thm (* m = n ==> m < Suc n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    76
  val eqD2:             thm (* m = n ==> m < Suc n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    77
  val not_lessI:        thm (* n < Suc m ==> ~(m < n) *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    78
  val leI:              thm (* m < Suc n ==> m <= n *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    79
  val not_leI:          thm (* n < m ==> ~(m <= n) *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    80
  val eqI:              thm (* [| m < Suc n; n < Suc m |] ==> n = m *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    81
  val is_zero: term -> bool
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    82
  val decomp:  term -> (term * int * string * term * int)option
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    83
(* decomp(`Suc^i(x) Rel Suc^j(y)') should yield (x,i,Rel,y,j)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    84
   where Rel is one of "<", "~<", "<=", "~<=" and "=" *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    85
end;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    86
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    87
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    88
signature TRANS_TAC =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    89
sig
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    90
  val trans_tac: int -> tactic
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    91
  val cut_trans_tac: thm list -> int -> tactic
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    92
end;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    93
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    94
functor Trans_Tac_Fun(Less:LESS_ARITH):TRANS_TAC =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    95
struct
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    96
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    97
datatype proof = Asm of int
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    98
               | Thm of proof list * thm
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
    99
               | Incr1 of proof * int (* Increment 1 side *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   100
               | Incr2 of proof * int (* Increment 2 sides *);
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   101
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   102
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   103
(*** Turn proof objects into thms ***)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   104
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   105
fun incr2(th,i) = if i=0 then th else
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   106
                  if i>0 then incr2(th RS Less.less_incr,i-1)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   107
                  else incr2(th RS Less.less_decr,i+1);
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   108
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   109
fun incr1(th,i) = if i=0 then th else
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   110
                  if i>0 then incr1(th RS Less.less_incr_rhs,i-1)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   111
                  else incr1(th RS Less.less_decr_lhs,i+1);
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   112
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   113
fun prove asms =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   114
  let fun pr(Asm i) = nth_elem(i,asms)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   115
        | pr(Thm(prfs,thm)) = (map pr prfs) MRS thm
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   116
        | pr(Incr1(p,i)) = incr1(pr p,i)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   117
        | pr(Incr2(p,i)) = incr2(pr p,i)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   118
  in pr end;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   119
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   120
(*** Internal representation of inequalities
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   121
(x,i,y,j) means x+i < y+j.
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   122
Leads to simpler case distinctions than the normalized x < y+k
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   123
***)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   124
type less = term * int * term * int * proof;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   125
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   126
(*** raised when contradiction is found ***)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   127
exception Contr of proof;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   128
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   129
(*** raised when goal can't be proved ***)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   130
exception Cant;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   131
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   132
infix subsumes;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   133
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   134
fun (x,i,y,j:int,_) subsumes (x',i',y',j',_) =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   135
  x=x' andalso y=y' andalso j-i<=j'-i';
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   136
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   137
fun trivial(x,i:int,y,j,_)  =  (x=y orelse Less.is_zero(x)) andalso i<j;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   138
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   139
(*** transitive closure ***)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   140
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   141
(* Very naive: computes all consequences of a set of less-statements. *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   142
(* In the worst case very expensive not just in time but also space *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   143
(* Could easily be optimized but there are ususally only a few < asms *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   144
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   145
fun add new =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   146
  let fun adds([],news) = new::news
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   147
        | adds(old::olds,news) = if new subsumes old then adds(olds,news)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   148
                                 else adds(olds,old::news)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   149
  in adds end;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   150
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   151
fun ctest(less as (x,i,y,j,p)) =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   152
  if x=y andalso i>=j
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   153
  then raise Contr(Thm([Incr1(Incr2(p,~j),j-i)],Less.less_reflE)) else
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   154
  if Less.is_zero(y) andalso i>=j
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   155
  then raise Contr(Thm([Incr2(p,~j)],Less.less_zeroE))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   156
  else less;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   157
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   158
fun mktrans((x,i,_,j,p):less,(_,k,z,l,q)) =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   159
  ctest(if j >= k
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   160
        then (x,i+1,z,l+(j-k),Thm([p,Incr2(q,j-k)],Less.less_trans_Suc))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   161
        else (x,i+(k-j)+1,z,l,Thm([Incr2(p,k-j),q],Less.less_trans_Suc)));
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   162
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   163
fun trans (new as (x,i,y,j,p)) olds =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   164
  let fun tr(news, old as (x1,i1,y1,j1,p1):less) =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   165
           if y1=x then mktrans(old,new)::news else
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   166
           if x1=y then mktrans(new,old)::news else news
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   167
  in foldl tr ([],olds) end;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   168
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   169
fun close1(olds: less list)(new:less):less list =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   170
      if trivial new orelse exists (fn old => old subsumes new) olds then olds
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   171
      else let val news = trans new olds
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   172
           in  close (add new (olds,[])) news end
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   173
and close (olds: less list) ([]:less list) = olds
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   174
  | close olds ((new:less)::news) = close (close1 olds (ctest new)) news;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   175
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   176
(*** end of transitive closure ***)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   177
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   178
(* recognize and solve trivial goal *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   179
fun triv_sol(x,i,y,j,_) =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   180
  if x=y andalso i<j
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   181
  then Some(Incr1(Incr2(Thm([],Less.lessI),i),j-i)) else
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   182
  if Less.is_zero(x) andalso i<j
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   183
  then Some(Incr1(Incr2(Thm([],Less.zero_less_Suc),i),j-i-1))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   184
  else None;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   185
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   186
(* solve less starting from facts *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   187
fun solve facts (less as (x,i,y,j,_)) =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   188
  case triv_sol less of
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   189
    None => (case find_first (fn fact => fact subsumes less) facts of
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   190
               None => raise Cant
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   191
             | Some(a,m,b,n,p) => Incr1(Incr2(p,j-n),n+i-m-j))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   192
  | Some prf => prf;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   193
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   194
(* turn term into a less-tuple *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   195
fun mkasm(t,n) =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   196
  case Less.decomp(t) of
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   197
    Some(x,i,rel,y,j) => (case rel of
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   198
      "<"   => [(x,i,y,j,Asm n)]
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   199
    | "~<"  => [(y,j,x,i+1,Thm([Asm n],Less.not_lessD))]
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   200
    | "<="  => [(x,i,y,j+1,Thm([Asm n],Less.leD))]
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   201
    | "~<=" => [(y,j,x,i,Thm([Asm n],Less.not_leD))]
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   202
    | "="   => [(x,i,y,j+1,Thm([Asm n],Less.eqD1)),
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   203
                (y,j,x,i+1,Thm([Asm n],Less.eqD2))]
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   204
    | "~="  => []
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   205
    | _     => error("trans_tac/decomp: unknown relation " ^ rel))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   206
  | None => [];
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   207
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   208
(* mkconcl t returns a pair (goals,proof) where goals is a list of *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   209
(* less-subgoals to solve, and proof the validation which proves the concl t *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   210
(* from the subgoals. Asm ~1 is dummy *)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   211
fun mkconcl t =
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   212
  case Less.decomp(t) of
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   213
    Some(x,i,rel,y,j) => (case rel of
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   214
      "<"   => ([(x,i,y,j,Asm ~1)],Asm 0)
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   215
    | "~<"  => ([(y,j,x,i+1,Asm ~1)],Thm([Asm 0],Less.not_lessI))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   216
    | "<="  => ([(x,i,y,j+1,Asm ~1)],Thm([Asm 0],Less.leI))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   217
    | "~<=" => ([(y,j,x,i,Asm ~1)],Thm([Asm 0],Less.not_leI))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   218
    | "="   => ([(x,i,y,j+1,Asm ~1),(y,j,x,i+1,Asm ~1)],
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   219
                Thm([Asm 0,Asm 1],Less.eqI))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   220
    | "~="  => raise Cant
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   221
    | _     => error("trans_tac/decomp: unknown relation " ^ rel))
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   222
  | None => raise Cant;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   223
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   224
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   225
val trans_tac = SUBGOAL (fn (A,n) =>
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   226
  let val Hs = Logic.strip_assums_hyp A
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   227
      val C = Logic.strip_assums_concl A
2273
d1bcc10be8d7 Fixed spelling error
paulson
parents: 2114
diff changeset
   228
      val lesss = flat(ListPair.map mkasm (Hs, 0 upto (length Hs - 1)))
2114
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   229
      val clesss = close [] lesss
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   230
      val (subgoals,prf) = mkconcl C
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   231
      val prfs = map (solve clesss) subgoals
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   232
  in METAHYPS (fn asms => let val thms = map (prove asms) prfs
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   233
                          in rtac (prove thms prf) 1 end) n
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   234
  end
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   235
  handle Contr(p) => METAHYPS (fn asms => rtac (prove asms p) 1) n
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   236
       | Cant => no_tac);
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   237
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   238
fun cut_trans_tac thms = cut_facts_tac thms THEN' trans_tac;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   239
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   240
end;
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   241
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   242
(*** Tests
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   243
fun test s = prove_goal Nat.thy ("!!m::nat." ^ s) (fn _ => [trans_tac 1]);
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   244
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   245
test "[| i<j; j<=k; ~(l < Suc k); ~(m <= l) |] ==> Suc(Suc i) < m";
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   246
test "[| i<j; j<=k; ~(l < Suc k); ~(m <= l) |] ==> Suc(Suc(Suc i)) <= m";
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   247
test "[| i<j; j<=k; ~(l < Suc k); ~(m <= l) |] ==> ~ m <= Suc(Suc i)";
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   248
test "[| i<j; j<=k; ~(l < Suc k); ~(m <= l) |] ==> ~ m < Suc(Suc(Suc i))";
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   249
test "[| i<j; j<=k; ~(l < Suc k); ~(m <= l); m <= Suc(Suc(Suc i)) |] \
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   250
\     ==> m = Suc(Suc(Suc i))";
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   251
test "[| i<j; j<=k; ~(l < Suc k); ~(m <= l); m=n; n <= Suc(Suc(Suc i)) |] \
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   252
\     ==> m = Suc(Suc(Suc i))";
c6a7fd523a5a Solves simple arithmetic goals.
nipkow
parents:
diff changeset
   253
***)