author | wenzelm |
Thu, 08 Aug 2002 23:46:09 +0200 | |
changeset 13480 | bb72bd43c6c3 |
parent 13462 | 56610e2ba220 |
child 14756 | 9c8cc63714f4 |
permissions | -rw-r--r-- |
5589 | 1 |
(* Title: Provers/Arith/abel_cancel.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1998 University of Cambridge |
|
5 |
||
6 |
Simplification procedures for abelian groups (e.g. integers, reals) |
|
7 |
||
13462 | 8 |
- Cancel complementary terms in sums |
5589 | 9 |
- Cancel like terms on opposite sides of relations |
10 |
*) |
|
11 |
||
12 |
||
13 |
signature ABEL_CANCEL = |
|
14 |
sig |
|
13462 | 15 |
val ss : simpset (*basic simpset of object-logtic*) |
16 |
val eq_reflection : thm (*object-equality to meta-equality*) |
|
5589 | 17 |
|
13462 | 18 |
val sg_ref : Sign.sg_ref (*signature of the theory of the group*) |
19 |
val T : typ (*the type of group elements*) |
|
5589 | 20 |
|
13462 | 21 |
val zero : term |
7586 | 22 |
val restrict_to_left : thm |
13462 | 23 |
val add_cancel_21 : thm |
24 |
val add_cancel_end : thm |
|
25 |
val add_left_cancel : thm |
|
26 |
val add_assoc : thm |
|
27 |
val add_commute : thm |
|
28 |
val add_left_commute : thm |
|
29 |
val add_0 : thm |
|
30 |
val add_0_right : thm |
|
5589 | 31 |
|
13462 | 32 |
val eq_diff_eq : thm |
33 |
val eqI_rules : thm list |
|
34 |
val dest_eqI : thm -> term |
|
5589 | 35 |
|
13462 | 36 |
val diff_def : thm |
37 |
val minus_add_distrib : thm |
|
38 |
val minus_minus : thm |
|
39 |
val minus_0 : thm |
|
5589 | 40 |
|
13462 | 41 |
val add_inverses : thm list |
42 |
val cancel_simps : thm list |
|
5589 | 43 |
end; |
44 |
||
45 |
||
46 |
functor Abel_Cancel (Data: ABEL_CANCEL) = |
|
47 |
struct |
|
48 |
||
5728
1d1175ef2d56
eliminated 'let ... in structure ...' to make SML/NJ 0.93 happy;
wenzelm
parents:
5610
diff
changeset
|
49 |
open Data; |
1d1175ef2d56
eliminated 'let ... in structure ...' to make SML/NJ 0.93 happy;
wenzelm
parents:
5610
diff
changeset
|
50 |
|
13462 | 51 |
val prepare_ss = Data.ss addsimps [add_assoc, diff_def, |
52 |
minus_add_distrib, minus_minus, |
|
53 |
minus_0, add_0, add_0_right]; |
|
5589 | 54 |
|
55 |
val plus = Const ("op +", [Data.T,Data.T] ---> Data.T); |
|
56 |
val minus = Const ("uminus", Data.T --> Data.T); |
|
57 |
||
58 |
(*Cancel corresponding terms on the two sides of the equation, NOT on |
|
59 |
the same side!*) |
|
13462 | 60 |
val cancel_ss = |
61 |
Data.ss addsimps [add_cancel_21, add_cancel_end, minus_minus] @ |
|
7586 | 62 |
(map (fn th => th RS restrict_to_left) Data.cancel_simps); |
5589 | 63 |
|
7586 | 64 |
val inverse_ss = Data.ss addsimps Data.add_inverses @ Data.cancel_simps; |
5589 | 65 |
|
66 |
fun mk_sum [] = Data.zero |
|
67 |
| mk_sum tms = foldr1 (fn (x,y) => plus $ x $ y) tms; |
|
68 |
||
69 |
(*We map -t to t and (in other cases) t to -t. No need to check the type of |
|
70 |
uminus, since the simproc is only called on sums of type T.*) |
|
71 |
fun negate (Const("uminus",_) $ t) = t |
|
72 |
| negate t = minus $ t; |
|
73 |
||
74 |
(*Flatten a formula built from +, binary - and unary -. |
|
75 |
No need to check types PROVIDED they are checked upon entry!*) |
|
76 |
fun add_terms neg (Const ("op +", _) $ x $ y, ts) = |
|
13462 | 77 |
add_terms neg (x, add_terms neg (y, ts)) |
5589 | 78 |
| add_terms neg (Const ("op -", _) $ x $ y, ts) = |
13462 | 79 |
add_terms neg (x, add_terms (not neg) (y, ts)) |
80 |
| add_terms neg (Const ("uminus", _) $ x, ts) = |
|
81 |
add_terms (not neg) (x, ts) |
|
82 |
| add_terms neg (x, ts) = |
|
83 |
(if neg then negate x else x) :: ts; |
|
5589 | 84 |
|
85 |
fun terms fml = add_terms false (fml, []); |
|
86 |
||
87 |
exception Cancel; |
|
88 |
||
89 |
(*Cancels just the first occurrence of u, leaving the rest unchanged*) |
|
90 |
fun cancelled (u, t::ts) = if u aconv t then ts else t :: cancelled(u,ts) |
|
91 |
| cancelled _ = raise Cancel; |
|
92 |
||
93 |
||
94 |
val trace = ref false; |
|
95 |
||
96 |
(*Make a simproc to cancel complementary terms in sums. Examples: |
|
97 |
x-x = 0 x+(y-x) = y -x+(y+(x+z)) = y+z |
|
13462 | 98 |
It will unfold the definition of diff and associate to the right if |
5589 | 99 |
necessary. Rewriting is faster if the formula is already |
100 |
in that form. |
|
101 |
*) |
|
102 |
||
103 |
fun sum_proc sg _ lhs = |
|
13462 | 104 |
let val _ = if !trace then tracing ("cancel_sums: LHS = " ^ |
105 |
string_of_cterm (cterm_of sg lhs)) |
|
106 |
else () |
|
5589 | 107 |
val (head::tail) = terms lhs |
108 |
val head' = negate head |
|
109 |
val rhs = mk_sum (cancelled (head',tail)) |
|
110 |
and chead' = Thm.cterm_of sg head' |
|
13462 | 111 |
val _ = if !trace then |
112 |
tracing ("RHS = " ^ string_of_cterm (Thm.cterm_of sg rhs)) |
|
113 |
else () |
|
13480
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
114 |
val thm = Tactic.prove sg [] [] (Logic.mk_equals (lhs, rhs)) |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
115 |
(fn _ => rtac eq_reflection 1 THEN |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
116 |
simp_tac prepare_ss 1 THEN |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
117 |
IF_UNSOLVED (simp_tac cancel_ss 1) THEN |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
118 |
IF_UNSOLVED (simp_tac inverse_ss 1)) |
5610 | 119 |
in Some thm end |
5589 | 120 |
handle Cancel => None; |
121 |
||
122 |
||
13462 | 123 |
val sum_conv = |
5589 | 124 |
Simplifier.mk_simproc "cancel_sums" |
9419 | 125 |
(map (Thm.read_cterm (Sign.deref sg_ref)) |
13462 | 126 |
[("x + y", Data.T), ("x - y", Data.T)]) (* FIXME depends on concrete syntax !???!!??! *) |
5589 | 127 |
sum_proc; |
128 |
||
129 |
||
130 |
(*A simproc to cancel like terms on the opposite sides of relations: |
|
131 |
(x + y - z < -z + x) = (y < 0) |
|
132 |
Works for (=) and (<=) as well as (<), if the necessary rules are supplied. |
|
133 |
Reduces the problem to subtraction and calls the previous simproc. |
|
134 |
*) |
|
135 |
||
136 |
(*Cancel the FIRST occurrence of a term. If it's repeated, then repeated |
|
137 |
calls to the simproc will be needed.*) |
|
138 |
fun cancel1 ([], u) = raise Match (*impossible: it's a common term*) |
|
139 |
| cancel1 (t::ts, u) = if t aconv u then ts |
|
13462 | 140 |
else t :: cancel1 (ts,u); |
5589 | 141 |
|
142 |
||
143 |
val sum_cancel_ss = Data.ss addsimprocs [sum_conv] |
|
13462 | 144 |
addsimps [add_0, add_0_right]; |
5589 | 145 |
|
146 |
val add_ac_ss = Data.ss addsimps [add_assoc,add_commute,add_left_commute]; |
|
147 |
||
148 |
fun rel_proc sg _ (lhs as (rel$lt$rt)) = |
|
13462 | 149 |
let val _ = if !trace then tracing ("cancel_relations: LHS = " ^ |
150 |
string_of_cterm (cterm_of sg lhs)) |
|
151 |
else () |
|
5589 | 152 |
val ltms = terms lt |
153 |
and rtms = terms rt |
|
154 |
val common = (*inter_term miscounts repetitions, so squash them*) |
|
13462 | 155 |
gen_distinct (op aconv) (inter_term (ltms, rtms)) |
5589 | 156 |
val _ = if null common then raise Cancel (*nothing to do*) |
13462 | 157 |
else () |
5589 | 158 |
|
159 |
fun cancelled tms = mk_sum (foldl cancel1 (tms, common)) |
|
160 |
||
161 |
val lt' = cancelled ltms |
|
162 |
and rt' = cancelled rtms |
|
163 |
||
164 |
val rhs = rel$lt'$rt' |
|
13462 | 165 |
val _ = if !trace then |
166 |
tracing ("RHS = " ^ string_of_cterm (Thm.cterm_of sg rhs)) |
|
167 |
else () |
|
5589 | 168 |
|
13480
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
169 |
val thm = Tactic.prove sg [] [] (Logic.mk_equals (lhs, rhs)) |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
170 |
(fn _ => rtac eq_reflection 1 THEN |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
171 |
resolve_tac eqI_rules 1 THEN |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
172 |
simp_tac prepare_ss 1 THEN |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
173 |
simp_tac sum_cancel_ss 1 THEN |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
174 |
IF_UNSOLVED (simp_tac add_ac_ss 1)) |
5610 | 175 |
in Some thm end |
5589 | 176 |
handle Cancel => None; |
177 |
||
13462 | 178 |
val rel_conv = |
179 |
Simplifier.simproc_i (Sign.deref sg_ref) "cancel_relations" |
|
180 |
(map Data.dest_eqI eqI_rules) rel_proc; |
|
5589 | 181 |
|
182 |
end; |