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