author | wenzelm |
Thu, 05 Jul 2007 00:06:18 +0200 | |
changeset 23580 | 998a6fda9bb6 |
parent 23559 | 0de527730294 |
child 23880 | 64b9806e160b |
permissions | -rw-r--r-- |
23252 | 1 |
(* Title: HOL/Tools/Groebner_Basis/normalizer.ML |
2 |
ID: $Id$ |
|
3 |
Author: Amine Chaieb, TU Muenchen |
|
4 |
*) |
|
5 |
||
6 |
signature NORMALIZER = |
|
7 |
sig |
|
23485 | 8 |
val semiring_normalize_conv : Proof.context -> conv |
9 |
val semiring_normalize_ord_conv : Proof.context -> (cterm -> cterm -> bool) -> conv |
|
23252 | 10 |
val semiring_normalize_tac : Proof.context -> int -> tactic |
23485 | 11 |
val semiring_normalize_wrapper : Proof.context -> NormalizerData.entry -> conv |
12 |
val semiring_normalize_ord_wrapper : Proof.context -> NormalizerData.entry -> |
|
13 |
(cterm -> cterm -> bool) -> conv |
|
23252 | 14 |
val semiring_normalizers_conv : |
15 |
cterm list -> cterm list * thm list -> cterm list * thm list -> |
|
23485 | 16 |
(cterm -> bool) * conv * conv * conv -> (cterm -> cterm -> bool) -> |
17 |
{add: conv, mul: conv, neg: conv, main: conv, pow: conv, sub: conv} |
|
23252 | 18 |
end |
19 |
||
20 |
structure Normalizer: NORMALIZER = |
|
21 |
struct |
|
23559 | 22 |
|
23 |
open Conv Misc; |
|
23252 | 24 |
|
25 |
(* Very basic stuff for terms *) |
|
26 |
val dest_numeral = term_of #> HOLogic.dest_number #> snd; |
|
27 |
val is_numeral = can dest_numeral; |
|
28 |
||
29 |
val numeral01_conv = Simplifier.rewrite |
|
30 |
(HOL_basic_ss addsimps [numeral_1_eq_1, numeral_0_eq_0]); |
|
31 |
val zero1_numeral_conv = |
|
32 |
Simplifier.rewrite (HOL_basic_ss addsimps [numeral_1_eq_1 RS sym, numeral_0_eq_0 RS sym]); |
|
23580
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
33 |
fun zerone_conv cv = zero1_numeral_conv then_conv cv then_conv numeral01_conv; |
23252 | 34 |
val natarith = [@{thm "add_nat_number_of"}, @{thm "diff_nat_number_of"}, |
35 |
@{thm "mult_nat_number_of"}, @{thm "eq_nat_number_of"}, |
|
36 |
@{thm "less_nat_number_of"}]; |
|
37 |
val nat_add_conv = |
|
38 |
zerone_conv |
|
39 |
(Simplifier.rewrite |
|
40 |
(HOL_basic_ss |
|
41 |
addsimps arith_simps @ natarith @ rel_simps |
|
42 |
@ [if_False, if_True, add_0, add_Suc, add_number_of_left, Suc_eq_add_numeral_1] |
|
43 |
@ map (fn th => th RS sym) numerals)); |
|
44 |
||
45 |
val nat_mul_conv = nat_add_conv; |
|
46 |
val zeron_tm = @{cterm "0::nat"}; |
|
47 |
val onen_tm = @{cterm "1::nat"}; |
|
48 |
val true_tm = @{cterm "True"}; |
|
49 |
||
50 |
||
51 |
(* The main function! *) |
|
52 |
fun semiring_normalizers_conv vars (sr_ops, sr_rules) (r_ops, r_rules) |
|
53 |
(is_semiring_constant, semiring_add_conv, semiring_mul_conv, semiring_pow_conv) = |
|
54 |
let |
|
55 |
||
56 |
val [pthm_02, pthm_03, pthm_04, pthm_05, pthm_07, pthm_08, |
|
57 |
pthm_09, pthm_10, pthm_11, pthm_12, pthm_13, pthm_14, pthm_15, pthm_16, |
|
58 |
pthm_17, pthm_18, pthm_19, pthm_21, pthm_22, pthm_23, pthm_24, |
|
59 |
pthm_25, pthm_26, pthm_27, pthm_28, pthm_29, pthm_30, pthm_31, pthm_32, |
|
60 |
pthm_33, pthm_34, pthm_35, pthm_36, pthm_37, pthm_38,pthm_39,pthm_40] = sr_rules; |
|
61 |
||
62 |
val [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry] = vars; |
|
63 |
val [add_pat, mul_pat, pow_pat, zero_tm, one_tm] = sr_ops; |
|
64 |
val [add_tm, mul_tm, pow_tm] = map (Thm.dest_fun o Thm.dest_fun) [add_pat, mul_pat, pow_pat]; |
|
65 |
||
66 |
val dest_add = dest_binop add_tm |
|
67 |
val dest_mul = dest_binop mul_tm |
|
68 |
fun dest_pow tm = |
|
69 |
let val (l,r) = dest_binop pow_tm tm |
|
70 |
in if is_numeral r then (l,r) else raise CTERM ("dest_pow",[tm]) |
|
71 |
end; |
|
72 |
val is_add = is_binop add_tm |
|
73 |
val is_mul = is_binop mul_tm |
|
74 |
fun is_pow tm = is_binop pow_tm tm andalso is_numeral(Thm.dest_arg tm); |
|
75 |
||
76 |
val (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub,cx',cy') = |
|
77 |
(case (r_ops, r_rules) of |
|
78 |
([], []) => (TrueI, TrueI, true_tm, true_tm, (fn t => (t,t)), K false, true_tm, true_tm) |
|
79 |
| ([sub_pat, neg_pat], [neg_mul, sub_add]) => |
|
80 |
let |
|
81 |
val sub_tm = Thm.dest_fun (Thm.dest_fun sub_pat) |
|
82 |
val neg_tm = Thm.dest_fun neg_pat |
|
83 |
val dest_sub = dest_binop sub_tm |
|
84 |
val is_sub = is_binop sub_tm |
|
85 |
in (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub, neg_mul |> concl |> Thm.dest_arg, |
|
86 |
sub_add |> concl |> Thm.dest_arg |> Thm.dest_arg) |
|
87 |
end); |
|
88 |
in fn variable_order => |
|
89 |
let |
|
90 |
||
91 |
(* Conversion for "x^n * x^m", with either x^n = x and/or x^m = x possible. *) |
|
92 |
(* Also deals with "const * const", but both terms must involve powers of *) |
|
93 |
(* the same variable, or both be constants, or behaviour may be incorrect. *) |
|
94 |
||
95 |
fun powvar_mul_conv tm = |
|
96 |
let |
|
97 |
val (l,r) = dest_mul tm |
|
98 |
in if is_semiring_constant l andalso is_semiring_constant r |
|
99 |
then semiring_mul_conv tm |
|
100 |
else |
|
101 |
((let |
|
102 |
val (lx,ln) = dest_pow l |
|
103 |
in |
|
104 |
((let val (rx,rn) = dest_pow r |
|
105 |
val th1 = inst_thm [(cx,lx),(cp,ln),(cq,rn)] pthm_29 |
|
106 |
val (tm1,tm2) = Thm.dest_comb(concl th1) in |
|
107 |
transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end) |
|
108 |
handle CTERM _ => |
|
109 |
(let val th1 = inst_thm [(cx,lx),(cq,ln)] pthm_31 |
|
110 |
val (tm1,tm2) = Thm.dest_comb(concl th1) in |
|
111 |
transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)) end) |
|
112 |
handle CTERM _ => |
|
113 |
((let val (rx,rn) = dest_pow r |
|
114 |
val th1 = inst_thm [(cx,rx),(cq,rn)] pthm_30 |
|
115 |
val (tm1,tm2) = Thm.dest_comb(concl th1) in |
|
116 |
transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end) |
|
117 |
handle CTERM _ => inst_thm [(cx,l)] pthm_32 |
|
118 |
||
119 |
)) |
|
120 |
end; |
|
121 |
||
122 |
(* Remove "1 * m" from a monomial, and just leave m. *) |
|
123 |
||
124 |
fun monomial_deone th = |
|
125 |
(let val (l,r) = dest_mul(concl th) in |
|
126 |
if l aconvc one_tm |
|
127 |
then transitive th (inst_thm [(ca,r)] pthm_13) else th end) |
|
128 |
handle CTERM _ => th; |
|
129 |
||
130 |
(* Conversion for "(monomial)^n", where n is a numeral. *) |
|
131 |
||
132 |
val monomial_pow_conv = |
|
133 |
let |
|
134 |
fun monomial_pow tm bod ntm = |
|
135 |
if not(is_comb bod) |
|
136 |
then reflexive tm |
|
137 |
else |
|
138 |
if is_semiring_constant bod |
|
139 |
then semiring_pow_conv tm |
|
140 |
else |
|
141 |
let |
|
142 |
val (lopr,r) = Thm.dest_comb bod |
|
143 |
in if not(is_comb lopr) |
|
144 |
then reflexive tm |
|
145 |
else |
|
146 |
let |
|
147 |
val (opr,l) = Thm.dest_comb lopr |
|
148 |
in |
|
149 |
if opr aconvc pow_tm andalso is_numeral r |
|
150 |
then |
|
151 |
let val th1 = inst_thm [(cx,l),(cp,r),(cq,ntm)] pthm_34 |
|
152 |
val (l,r) = Thm.dest_comb(concl th1) |
|
153 |
in transitive th1 (Drule.arg_cong_rule l (nat_mul_conv r)) |
|
154 |
end |
|
155 |
else |
|
156 |
if opr aconvc mul_tm |
|
157 |
then |
|
158 |
let |
|
159 |
val th1 = inst_thm [(cx,l),(cy,r),(cq,ntm)] pthm_33 |
|
160 |
val (xy,z) = Thm.dest_comb(concl th1) |
|
161 |
val (x,y) = Thm.dest_comb xy |
|
162 |
val thl = monomial_pow y l ntm |
|
163 |
val thr = monomial_pow z r ntm |
|
164 |
in transitive th1 (combination (Drule.arg_cong_rule x thl) thr) |
|
165 |
end |
|
166 |
else reflexive tm |
|
167 |
end |
|
168 |
end |
|
169 |
in fn tm => |
|
170 |
let |
|
171 |
val (lopr,r) = Thm.dest_comb tm |
|
172 |
val (opr,l) = Thm.dest_comb lopr |
|
173 |
in if not (opr aconvc pow_tm) orelse not(is_numeral r) |
|
174 |
then raise CTERM ("monomial_pow_conv", [tm]) |
|
175 |
else if r aconvc zeron_tm |
|
176 |
then inst_thm [(cx,l)] pthm_35 |
|
177 |
else if r aconvc onen_tm |
|
178 |
then inst_thm [(cx,l)] pthm_36 |
|
179 |
else monomial_deone(monomial_pow tm l r) |
|
180 |
end |
|
181 |
end; |
|
182 |
||
183 |
(* Multiplication of canonical monomials. *) |
|
184 |
val monomial_mul_conv = |
|
185 |
let |
|
186 |
fun powvar tm = |
|
187 |
if is_semiring_constant tm then one_tm |
|
188 |
else |
|
189 |
((let val (lopr,r) = Thm.dest_comb tm |
|
190 |
val (opr,l) = Thm.dest_comb lopr |
|
191 |
in if opr aconvc pow_tm andalso is_numeral r then l |
|
192 |
else raise CTERM ("monomial_mul_conv",[tm]) end) |
|
193 |
handle CTERM _ => tm) (* FIXME !? *) |
|
194 |
fun vorder x y = |
|
195 |
if x aconvc y then 0 |
|
196 |
else |
|
197 |
if x aconvc one_tm then ~1 |
|
198 |
else if y aconvc one_tm then 1 |
|
199 |
else if variable_order x y then ~1 else 1 |
|
200 |
fun monomial_mul tm l r = |
|
201 |
((let val (lx,ly) = dest_mul l val vl = powvar lx |
|
202 |
in |
|
203 |
((let |
|
204 |
val (rx,ry) = dest_mul r |
|
205 |
val vr = powvar rx |
|
206 |
val ord = vorder vl vr |
|
207 |
in |
|
208 |
if ord = 0 |
|
209 |
then |
|
210 |
let |
|
211 |
val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] pthm_15 |
|
212 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
213 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
214 |
val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2 |
|
215 |
val th3 = transitive th1 th2 |
|
216 |
val (tm5,tm6) = Thm.dest_comb(concl th3) |
|
217 |
val (tm7,tm8) = Thm.dest_comb tm6 |
|
218 |
val th4 = monomial_mul tm6 (Thm.dest_arg tm7) tm8 |
|
219 |
in transitive th3 (Drule.arg_cong_rule tm5 th4) |
|
220 |
end |
|
221 |
else |
|
222 |
let val th0 = if ord < 0 then pthm_16 else pthm_17 |
|
223 |
val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] th0 |
|
224 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
225 |
val (tm3,tm4) = Thm.dest_comb tm2 |
|
226 |
in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4)) |
|
227 |
end |
|
228 |
end) |
|
229 |
handle CTERM _ => |
|
230 |
(let val vr = powvar r val ord = vorder vl vr |
|
231 |
in |
|
232 |
if ord = 0 then |
|
233 |
let |
|
234 |
val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_18 |
|
235 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
236 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
237 |
val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2 |
|
238 |
in transitive th1 th2 |
|
239 |
end |
|
240 |
else |
|
241 |
if ord < 0 then |
|
242 |
let val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_19 |
|
243 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
244 |
val (tm3,tm4) = Thm.dest_comb tm2 |
|
245 |
in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4)) |
|
246 |
end |
|
247 |
else inst_thm [(ca,l),(cb,r)] pthm_09 |
|
248 |
end)) end) |
|
249 |
handle CTERM _ => |
|
250 |
(let val vl = powvar l in |
|
251 |
((let |
|
252 |
val (rx,ry) = dest_mul r |
|
253 |
val vr = powvar rx |
|
254 |
val ord = vorder vl vr |
|
255 |
in if ord = 0 then |
|
256 |
let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_21 |
|
257 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
258 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
259 |
in transitive th1 (Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2) |
|
260 |
end |
|
261 |
else if ord > 0 then |
|
262 |
let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_22 |
|
263 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
264 |
val (tm3,tm4) = Thm.dest_comb tm2 |
|
265 |
in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4)) |
|
266 |
end |
|
267 |
else reflexive tm |
|
268 |
end) |
|
269 |
handle CTERM _ => |
|
270 |
(let val vr = powvar r |
|
271 |
val ord = vorder vl vr |
|
272 |
in if ord = 0 then powvar_mul_conv tm |
|
273 |
else if ord > 0 then inst_thm [(ca,l),(cb,r)] pthm_09 |
|
274 |
else reflexive tm |
|
275 |
end)) end)) |
|
276 |
in fn tm => let val (l,r) = dest_mul tm in monomial_deone(monomial_mul tm l r) |
|
277 |
end |
|
278 |
end; |
|
279 |
(* Multiplication by monomial of a polynomial. *) |
|
280 |
||
281 |
val polynomial_monomial_mul_conv = |
|
282 |
let |
|
283 |
fun pmm_conv tm = |
|
284 |
let val (l,r) = dest_mul tm |
|
285 |
in |
|
286 |
((let val (y,z) = dest_add r |
|
287 |
val th1 = inst_thm [(cx,l),(cy,y),(cz,z)] pthm_37 |
|
288 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
289 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
290 |
val th2 = combination (Drule.arg_cong_rule tm3 (monomial_mul_conv tm4)) (pmm_conv tm2) |
|
291 |
in transitive th1 th2 |
|
292 |
end) |
|
293 |
handle CTERM _ => monomial_mul_conv tm) |
|
294 |
end |
|
295 |
in pmm_conv |
|
296 |
end; |
|
297 |
||
298 |
(* Addition of two monomials identical except for constant multiples. *) |
|
299 |
||
300 |
fun monomial_add_conv tm = |
|
301 |
let val (l,r) = dest_add tm |
|
302 |
in if is_semiring_constant l andalso is_semiring_constant r |
|
303 |
then semiring_add_conv tm |
|
304 |
else |
|
305 |
let val th1 = |
|
306 |
if is_mul l andalso is_semiring_constant(Thm.dest_arg1 l) |
|
307 |
then if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r) then |
|
308 |
inst_thm [(ca,Thm.dest_arg1 l),(cm,Thm.dest_arg r), (cb,Thm.dest_arg1 r)] pthm_02 |
|
309 |
else inst_thm [(ca,Thm.dest_arg1 l),(cm,r)] pthm_03 |
|
310 |
else if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r) |
|
311 |
then inst_thm [(cm,l),(ca,Thm.dest_arg1 r)] pthm_04 |
|
312 |
else inst_thm [(cm,r)] pthm_05 |
|
313 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
314 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
315 |
val th2 = Drule.arg_cong_rule tm3 (semiring_add_conv tm4) |
|
316 |
val th3 = transitive th1 (Drule.fun_cong_rule th2 tm2) |
|
317 |
val tm5 = concl th3 |
|
318 |
in |
|
319 |
if (Thm.dest_arg1 tm5) aconvc zero_tm |
|
320 |
then transitive th3 (inst_thm [(ca,Thm.dest_arg tm5)] pthm_11) |
|
321 |
else monomial_deone th3 |
|
322 |
end |
|
323 |
end; |
|
324 |
||
325 |
(* Ordering on monomials. *) |
|
326 |
||
327 |
fun striplist dest = |
|
328 |
let fun strip x acc = |
|
329 |
((let val (l,r) = dest x in |
|
330 |
strip l (strip r acc) end) |
|
331 |
handle CTERM _ => x::acc) (* FIXME !? *) |
|
332 |
in fn x => strip x [] |
|
333 |
end; |
|
334 |
||
335 |
||
336 |
fun powervars tm = |
|
337 |
let val ptms = striplist dest_mul tm |
|
338 |
in if is_semiring_constant (hd ptms) then tl ptms else ptms |
|
339 |
end; |
|
340 |
val num_0 = 0; |
|
341 |
val num_1 = 1; |
|
342 |
fun dest_varpow tm = |
|
343 |
((let val (x,n) = dest_pow tm in (x,dest_numeral n) end) |
|
344 |
handle CTERM _ => |
|
345 |
(tm,(if is_semiring_constant tm then num_0 else num_1))); |
|
346 |
||
347 |
val morder = |
|
348 |
let fun lexorder l1 l2 = |
|
349 |
case (l1,l2) of |
|
350 |
([],[]) => 0 |
|
351 |
| (vps,[]) => ~1 |
|
352 |
| ([],vps) => 1 |
|
353 |
| (((x1,n1)::vs1),((x2,n2)::vs2)) => |
|
354 |
if variable_order x1 x2 then 1 |
|
355 |
else if variable_order x2 x1 then ~1 |
|
356 |
else if n1 < n2 then ~1 |
|
357 |
else if n2 < n1 then 1 |
|
358 |
else lexorder vs1 vs2 |
|
359 |
in fn tm1 => fn tm2 => |
|
360 |
let val vdegs1 = map dest_varpow (powervars tm1) |
|
361 |
val vdegs2 = map dest_varpow (powervars tm2) |
|
362 |
val deg1 = fold_rev ((curry (op +)) o snd) vdegs1 num_0 |
|
363 |
val deg2 = fold_rev ((curry (op +)) o snd) vdegs2 num_0 |
|
364 |
in if deg1 < deg2 then ~1 else if deg1 > deg2 then 1 |
|
365 |
else lexorder vdegs1 vdegs2 |
|
366 |
end |
|
367 |
end; |
|
368 |
||
369 |
(* Addition of two polynomials. *) |
|
370 |
||
371 |
val polynomial_add_conv = |
|
372 |
let |
|
373 |
fun dezero_rule th = |
|
374 |
let |
|
375 |
val tm = concl th |
|
376 |
in |
|
377 |
if not(is_add tm) then th else |
|
378 |
let val (lopr,r) = Thm.dest_comb tm |
|
379 |
val l = Thm.dest_arg lopr |
|
380 |
in |
|
381 |
if l aconvc zero_tm |
|
382 |
then transitive th (inst_thm [(ca,r)] pthm_07) else |
|
383 |
if r aconvc zero_tm |
|
384 |
then transitive th (inst_thm [(ca,l)] pthm_08) else th |
|
385 |
end |
|
386 |
end |
|
387 |
fun padd tm = |
|
388 |
let |
|
389 |
val (l,r) = dest_add tm |
|
390 |
in |
|
391 |
if l aconvc zero_tm then inst_thm [(ca,r)] pthm_07 |
|
392 |
else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_08 |
|
393 |
else |
|
394 |
if is_add l |
|
395 |
then |
|
396 |
let val (a,b) = dest_add l |
|
397 |
in |
|
398 |
if is_add r then |
|
399 |
let val (c,d) = dest_add r |
|
400 |
val ord = morder a c |
|
401 |
in |
|
402 |
if ord = 0 then |
|
403 |
let val th1 = inst_thm [(ca,a),(cb,b),(cc,c),(cd,d)] pthm_23 |
|
404 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
405 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
406 |
val th2 = Drule.arg_cong_rule tm3 (monomial_add_conv tm4) |
|
407 |
in dezero_rule (transitive th1 (combination th2 (padd tm2))) |
|
408 |
end |
|
409 |
else (* ord <> 0*) |
|
410 |
let val th1 = |
|
411 |
if ord > 0 then inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24 |
|
412 |
else inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25 |
|
413 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
414 |
in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2))) |
|
415 |
end |
|
416 |
end |
|
417 |
else (* not (is_add r)*) |
|
418 |
let val ord = morder a r |
|
419 |
in |
|
420 |
if ord = 0 then |
|
421 |
let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_26 |
|
422 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
423 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
424 |
val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2 |
|
425 |
in dezero_rule (transitive th1 th2) |
|
426 |
end |
|
427 |
else (* ord <> 0*) |
|
428 |
if ord > 0 then |
|
429 |
let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24 |
|
430 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
431 |
in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2))) |
|
432 |
end |
|
433 |
else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27) |
|
434 |
end |
|
435 |
end |
|
436 |
else (* not (is_add l)*) |
|
437 |
if is_add r then |
|
438 |
let val (c,d) = dest_add r |
|
439 |
val ord = morder l c |
|
440 |
in |
|
441 |
if ord = 0 then |
|
442 |
let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_28 |
|
443 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
444 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
445 |
val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2 |
|
446 |
in dezero_rule (transitive th1 th2) |
|
447 |
end |
|
448 |
else |
|
449 |
if ord > 0 then reflexive tm |
|
450 |
else |
|
451 |
let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25 |
|
452 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
453 |
in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2))) |
|
454 |
end |
|
455 |
end |
|
456 |
else |
|
457 |
let val ord = morder l r |
|
458 |
in |
|
459 |
if ord = 0 then monomial_add_conv tm |
|
460 |
else if ord > 0 then dezero_rule(reflexive tm) |
|
461 |
else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27) |
|
462 |
end |
|
463 |
end |
|
464 |
in padd |
|
465 |
end; |
|
466 |
||
467 |
(* Multiplication of two polynomials. *) |
|
468 |
||
469 |
val polynomial_mul_conv = |
|
470 |
let |
|
471 |
fun pmul tm = |
|
472 |
let val (l,r) = dest_mul tm |
|
473 |
in |
|
474 |
if not(is_add l) then polynomial_monomial_mul_conv tm |
|
475 |
else |
|
476 |
if not(is_add r) then |
|
477 |
let val th1 = inst_thm [(ca,l),(cb,r)] pthm_09 |
|
478 |
in transitive th1 (polynomial_monomial_mul_conv(concl th1)) |
|
479 |
end |
|
480 |
else |
|
481 |
let val (a,b) = dest_add l |
|
482 |
val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_10 |
|
483 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
484 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
485 |
val th2 = Drule.arg_cong_rule tm3 (polynomial_monomial_mul_conv tm4) |
|
486 |
val th3 = transitive th1 (combination th2 (pmul tm2)) |
|
487 |
in transitive th3 (polynomial_add_conv (concl th3)) |
|
488 |
end |
|
489 |
end |
|
490 |
in fn tm => |
|
491 |
let val (l,r) = dest_mul tm |
|
492 |
in |
|
493 |
if l aconvc zero_tm then inst_thm [(ca,r)] pthm_11 |
|
494 |
else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_12 |
|
495 |
else if l aconvc one_tm then inst_thm [(ca,r)] pthm_13 |
|
496 |
else if r aconvc one_tm then inst_thm [(ca,l)] pthm_14 |
|
497 |
else pmul tm |
|
498 |
end |
|
499 |
end; |
|
500 |
||
501 |
(* Power of polynomial (optimized for the monomial and trivial cases). *) |
|
502 |
||
23580
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
503 |
fun num_conv n = |
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
504 |
nat_add_conv (Thm.capply @{cterm Suc} (Numeral.mk_cnumber @{ctyp nat} (dest_numeral n - 1))) |
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
505 |
|> Thm.symmetric; |
23252 | 506 |
|
507 |
||
508 |
val polynomial_pow_conv = |
|
509 |
let |
|
510 |
fun ppow tm = |
|
511 |
let val (l,n) = dest_pow tm |
|
512 |
in |
|
513 |
if n aconvc zeron_tm then inst_thm [(cx,l)] pthm_35 |
|
514 |
else if n aconvc onen_tm then inst_thm [(cx,l)] pthm_36 |
|
515 |
else |
|
516 |
let val th1 = num_conv n |
|
517 |
val th2 = inst_thm [(cx,l),(cq,Thm.dest_arg (concl th1))] pthm_38 |
|
518 |
val (tm1,tm2) = Thm.dest_comb(concl th2) |
|
519 |
val th3 = transitive th2 (Drule.arg_cong_rule tm1 (ppow tm2)) |
|
520 |
val th4 = transitive (Drule.arg_cong_rule (Thm.dest_fun tm) th1) th3 |
|
521 |
in transitive th4 (polynomial_mul_conv (concl th4)) |
|
522 |
end |
|
523 |
end |
|
524 |
in fn tm => |
|
525 |
if is_add(Thm.dest_arg1 tm) then ppow tm else monomial_pow_conv tm |
|
526 |
end; |
|
527 |
||
528 |
(* Negation. *) |
|
529 |
||
23580
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
530 |
fun polynomial_neg_conv tm = |
23252 | 531 |
let val (l,r) = Thm.dest_comb tm in |
532 |
if not (l aconvc neg_tm) then raise CTERM ("polynomial_neg_conv",[tm]) else |
|
533 |
let val th1 = inst_thm [(cx',r)] neg_mul |
|
534 |
val th2 = transitive th1 (arg1_conv semiring_mul_conv (concl th1)) |
|
535 |
in transitive th2 (polynomial_monomial_mul_conv (concl th2)) |
|
536 |
end |
|
537 |
end; |
|
538 |
||
539 |
||
540 |
(* Subtraction. *) |
|
23580
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
541 |
fun polynomial_sub_conv tm = |
23252 | 542 |
let val (l,r) = dest_sub tm |
543 |
val th1 = inst_thm [(cx',l),(cy',r)] sub_add |
|
544 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
545 |
val th2 = Drule.arg_cong_rule tm1 (polynomial_neg_conv tm2) |
|
546 |
in transitive th1 (transitive th2 (polynomial_add_conv (concl th2))) |
|
547 |
end; |
|
548 |
||
549 |
(* Conversion from HOL term. *) |
|
550 |
||
551 |
fun polynomial_conv tm = |
|
23407
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
552 |
if is_semiring_constant tm then semiring_add_conv tm |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
553 |
else if not(is_comb tm) then reflexive tm |
23252 | 554 |
else |
555 |
let val (lopr,r) = Thm.dest_comb tm |
|
556 |
in if lopr aconvc neg_tm then |
|
557 |
let val th1 = Drule.arg_cong_rule lopr (polynomial_conv r) |
|
558 |
in transitive th1 (polynomial_neg_conv (concl th1)) |
|
559 |
end |
|
560 |
else |
|
561 |
if not(is_comb lopr) then reflexive tm |
|
562 |
else |
|
563 |
let val (opr,l) = Thm.dest_comb lopr |
|
564 |
in if opr aconvc pow_tm andalso is_numeral r |
|
565 |
then |
|
566 |
let val th1 = Drule.fun_cong_rule (Drule.arg_cong_rule opr (polynomial_conv l)) r |
|
567 |
in transitive th1 (polynomial_pow_conv (concl th1)) |
|
568 |
end |
|
569 |
else |
|
570 |
if opr aconvc add_tm orelse opr aconvc mul_tm orelse opr aconvc sub_tm |
|
571 |
then |
|
572 |
let val th1 = combination (Drule.arg_cong_rule opr (polynomial_conv l)) (polynomial_conv r) |
|
573 |
val f = if opr aconvc add_tm then polynomial_add_conv |
|
574 |
else if opr aconvc mul_tm then polynomial_mul_conv |
|
575 |
else polynomial_sub_conv |
|
576 |
in transitive th1 (f (concl th1)) |
|
577 |
end |
|
578 |
else reflexive tm |
|
579 |
end |
|
580 |
end; |
|
581 |
in |
|
582 |
{main = polynomial_conv, |
|
583 |
add = polynomial_add_conv, |
|
584 |
mul = polynomial_mul_conv, |
|
585 |
pow = polynomial_pow_conv, |
|
586 |
neg = polynomial_neg_conv, |
|
587 |
sub = polynomial_sub_conv} |
|
588 |
end |
|
589 |
end; |
|
590 |
||
591 |
val nat_arith = @{thms "nat_arith"}; |
|
592 |
val nat_exp_ss = HOL_basic_ss addsimps (nat_number @ nat_arith @ arith_simps @ rel_simps) |
|
593 |
addsimps [Let_def, if_False, if_True, add_0, add_Suc]; |
|
594 |
||
23407
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
595 |
fun simple_cterm_ord t u = Term.term_ord (term_of t, term_of u) = LESS; |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
596 |
fun semiring_normalize_ord_wrapper ctxt ({vars, semiring, ring, idom}, |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
597 |
{conv, dest_const, mk_const, is_const}) ord = |
23252 | 598 |
let |
599 |
val pow_conv = |
|
600 |
arg_conv (Simplifier.rewrite nat_exp_ss) |
|
601 |
then_conv Simplifier.rewrite |
|
602 |
(HOL_basic_ss addsimps [nth (snd semiring) 31, nth (snd semiring) 34]) |
|
23330
01c09922ce59
Conversion for computation on constants now depends on the context
chaieb
parents:
23259
diff
changeset
|
603 |
then_conv conv ctxt |
01c09922ce59
Conversion for computation on constants now depends on the context
chaieb
parents:
23259
diff
changeset
|
604 |
val dat = (is_const, conv ctxt, conv ctxt, pow_conv) |
23252 | 605 |
val {main, ...} = semiring_normalizers_conv vars semiring ring dat ord |
606 |
in main end; |
|
607 |
||
23407
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
608 |
fun semiring_normalize_wrapper ctxt data = |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
609 |
semiring_normalize_ord_wrapper ctxt data simple_cterm_ord; |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
610 |
|
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
611 |
fun semiring_normalize_ord_conv ctxt ord tm = |
23252 | 612 |
(case NormalizerData.match ctxt tm of |
613 |
NONE => reflexive tm |
|
23407
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
614 |
| SOME res => semiring_normalize_ord_wrapper ctxt res ord tm); |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
615 |
|
23252 | 616 |
|
23407
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
617 |
fun semiring_normalize_conv ctxt = semiring_normalize_ord_conv ctxt simple_cterm_ord; |
23252 | 618 |
|
619 |
fun semiring_normalize_tac ctxt = SUBGOAL (fn (goal, i) => |
|
620 |
rtac (semiring_normalize_conv ctxt |
|
621 |
(cterm_of (ProofContext.theory_of ctxt) (fst (Logic.dest_equals goal)))) i); |
|
622 |
end; |