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