author | chaieb |
Mon, 11 Jun 2007 18:28:16 +0200 | |
changeset 23330 | 01c09922ce59 |
parent 23259 | ccee01b8d1c5 |
child 23334 | 2443224cf6d7 |
permissions | -rw-r--r-- |
23252 | 1 |
(* Title: HOL/Tools/Groebner_Basis/groebner.ML |
2 |
ID: $Id$ |
|
3 |
Author: Amine Chaieb, TU Muenchen |
|
4 |
*) |
|
5 |
||
6 |
signature GROEBNER = |
|
7 |
sig |
|
8 |
val ring_and_ideal_conv : |
|
9 |
{idom: thm list, ring: cterm list * thm list, vars: cterm list, |
|
10 |
semiring: Thm.cterm list * thm list} -> |
|
11 |
(Thm.cterm -> Rat.rat) -> (Rat.rat -> Thm.cterm) -> |
|
12 |
Conv.conv -> Conv.conv -> |
|
13 |
Conv.conv * (cterm list -> cterm -> (cterm * cterm -> order) -> cterm list) |
|
14 |
val ring_conv : Proof.context -> cterm -> thm |
|
15 |
end |
|
16 |
||
17 |
structure Groebner: GROEBNER = |
|
18 |
struct |
|
19 |
open Normalizer; |
|
20 |
open Misc; |
|
21 |
||
22 |
(* FIXME :: Already present in Tools/Presburger/qelim.ML but is much more general!! *) |
|
23 |
fun cterm_frees ct = |
|
24 |
let fun h acc t = |
|
25 |
case (term_of t) of |
|
26 |
_$_ => h (h acc (Thm.dest_arg t)) (Thm.dest_fun t) |
|
27 |
| Abs(_,_,_) => Thm.dest_abs NONE t ||> h acc |> uncurry (remove (op aconvc)) |
|
28 |
| Free _ => insert (op aconvc) t acc |
|
29 |
| _ => acc |
|
30 |
in h [] ct end; |
|
31 |
||
32 |
fun assocd x al d = case AList.lookup (op =) al x of SOME y => y | NONE => d; |
|
33 |
val rat_0 = Rat.zero; |
|
34 |
val rat_1 = Rat.one; |
|
35 |
val minus_rat = Rat.neg; |
|
36 |
val denominator_rat = Rat.quotient_of_rat #> snd #> Rat.rat_of_int; |
|
37 |
fun int_of_rat a = |
|
38 |
case Rat.quotient_of_rat a of (i,1) => i | _ => error "int_of_rat: not an int"; |
|
39 |
val lcm_rat = fn x => fn y => Rat.rat_of_int (lcm (int_of_rat x, int_of_rat y)); |
|
40 |
||
41 |
val (eqF_intr, eqF_elim) = |
|
42 |
let val [th1,th2] = thms "PFalse" |
|
43 |
in (fn th => th COMP th2, fn th => th COMP th1) end; |
|
44 |
||
45 |
val (PFalse, PFalse') = |
|
46 |
let val PFalse_eq = nth (thms "simp_thms") 13 |
|
47 |
in (PFalse_eq RS iffD1, PFalse_eq RS iffD2) end; |
|
48 |
||
49 |
||
50 |
(* ------------------------------------------------------------------------- *) |
|
51 |
(* Type for recording history, i.e. how a polynomial was obtained. *) |
|
52 |
(* ------------------------------------------------------------------------- *) |
|
53 |
||
54 |
datatype history = |
|
23259 | 55 |
Start of integer |
56 |
| Mmul of (Rat.rat * (integer list)) * history |
|
23252 | 57 |
| Add of history * history; |
58 |
||
59 |
||
60 |
(* Monomial ordering. *) |
|
61 |
||
62 |
fun morder_lt m1 m2= |
|
63 |
let fun lexorder l1 l2 = |
|
64 |
case (l1,l2) of |
|
65 |
([],[]) => false |
|
66 |
| (x1::o1,x2::o2) => x1 > x2 orelse x1 = x2 andalso lexorder o1 o2 |
|
67 |
| _ => error "morder: inconsistent monomial lengths" |
|
23259 | 68 |
val n1 = fold Integer.add m1 0 |
69 |
val n2 = fold Integer.add m2 0 in |
|
23252 | 70 |
n1 < n2 orelse n1 = n2 andalso lexorder m1 m2 |
71 |
end; |
|
72 |
||
73 |
fun morder_le m1 m2 = morder_lt m1 m2 orelse (m1 = m2); |
|
74 |
||
75 |
fun morder_gt m1 m2 = morder_lt m2 m1; |
|
76 |
||
77 |
(* Arithmetic on canonical polynomials. *) |
|
78 |
||
79 |
fun grob_neg l = map (fn (c,m) => (minus_rat c,m)) l; |
|
80 |
||
81 |
fun grob_add l1 l2 = |
|
82 |
case (l1,l2) of |
|
83 |
([],l2) => l2 |
|
84 |
| (l1,[]) => l1 |
|
85 |
| ((c1,m1)::o1,(c2,m2)::o2) => |
|
86 |
if m1 = m2 then |
|
87 |
let val c = c1+/c2 val rest = grob_add o1 o2 in |
|
88 |
if c =/ rat_0 then rest else (c,m1)::rest end |
|
89 |
else if morder_lt m2 m1 then (c1,m1)::(grob_add o1 l2) |
|
90 |
else (c2,m2)::(grob_add l1 o2); |
|
91 |
||
92 |
fun grob_sub l1 l2 = grob_add l1 (grob_neg l2); |
|
93 |
||
23259 | 94 |
fun grob_mmul (c1,m1) (c2,m2) = (c1*/c2,map2 Integer.add m1 m2); |
23252 | 95 |
|
96 |
fun grob_cmul cm pol = map (grob_mmul cm) pol; |
|
97 |
||
98 |
fun grob_mul l1 l2 = |
|
99 |
case l1 of |
|
100 |
[] => [] |
|
101 |
| (h1::t1) => grob_add (grob_cmul h1 l2) (grob_mul t1 l2); |
|
102 |
||
103 |
fun grob_inv l = |
|
104 |
case l of |
|
105 |
[(c,vs)] => if (forall (fn x => x = 0) vs) then |
|
106 |
if (c =/ rat_0) then error "grob_inv: division by zero" |
|
107 |
else [(rat_1 // c,vs)] |
|
108 |
else error "grob_inv: non-constant divisor polynomial" |
|
109 |
| _ => error "grob_inv: non-constant divisor polynomial"; |
|
110 |
||
111 |
fun grob_div l1 l2 = |
|
112 |
case l2 of |
|
113 |
[(c,l)] => if (forall (fn x => x = 0) l) then |
|
114 |
if c =/ rat_0 then error "grob_div: division by zero" |
|
115 |
else grob_cmul (rat_1 // c,l) l1 |
|
116 |
else error "grob_div: non-constant divisor polynomial" |
|
117 |
| _ => error "grob_div: non-constant divisor polynomial"; |
|
118 |
||
119 |
fun grob_pow vars l n = |
|
120 |
if n < 0 then error "grob_pow: negative power" |
|
121 |
else if n = 0 then [(rat_1,map (fn v => 0) vars)] |
|
23259 | 122 |
else grob_mul l (grob_pow vars l (n -% 1)); |
23252 | 123 |
|
23259 | 124 |
val max = fn (x: integer) => fn y => if x < y then y else x; |
23252 | 125 |
|
126 |
fun degree vn p = |
|
127 |
case p of |
|
128 |
[] => error "Zero polynomial" |
|
129 |
| [(c,ns)] => nth ns vn |
|
130 |
| (c,ns)::p' => max (nth ns vn) (degree vn p'); |
|
131 |
||
132 |
fun head_deg vn p = let val d = degree vn p in |
|
133 |
(d,fold (fn (c,r) => fn q => grob_add q [(c, map_index (fn (i,n) => if i = vn then 0 else n) r)]) (filter (fn (c,ns) => c <>/ rat_0 andalso nth ns vn = d) p) []) end; |
|
134 |
||
23259 | 135 |
val is_zerop = forall (fn (c,ns) => c =/ rat_0 andalso forall (curry (op =) (0: integer)) ns); |
23252 | 136 |
val grob_pdiv = |
137 |
let fun pdiv_aux vn (n,a) p k s = |
|
138 |
if is_zerop s then (k,s) else |
|
139 |
let val (m,b) = head_deg vn s |
|
140 |
in if m < n then (k,s) else |
|
23259 | 141 |
let val p' = grob_mul p [(rat_1, map_index (fn (i,v) => if i = vn then m -% n else 0) |
23252 | 142 |
(snd (hd s)))] |
143 |
in if a = b then pdiv_aux vn (n,a) p k (grob_sub s p') |
|
23259 | 144 |
else pdiv_aux vn (n,a) p (k +% 1) (grob_sub (grob_mul a s) (grob_mul b p')) |
23252 | 145 |
end |
146 |
end |
|
147 |
in fn vn => fn s => fn p => pdiv_aux vn (head_deg vn p) p 0 s |
|
148 |
end; |
|
149 |
||
150 |
(* Monomial division operation. *) |
|
151 |
||
152 |
fun mdiv (c1,m1) (c2,m2) = |
|
153 |
(c1//c2, |
|
23259 | 154 |
map2 (fn n1 => fn n2 => if n1 < n2 then error "mdiv" else n1 -% n2) m1 m2); |
23252 | 155 |
|
156 |
(* Lowest common multiple of two monomials. *) |
|
157 |
||
158 |
fun mlcm (c1,m1) (c2,m2) = (rat_1,map2 max m1 m2); |
|
159 |
||
160 |
(* Reduce monomial cm by polynomial pol, returning replacement for cm. *) |
|
161 |
||
162 |
fun reduce1 cm (pol,hpol) = |
|
163 |
case pol of |
|
164 |
[] => error "reduce1" |
|
165 |
| cm1::cms => ((let val (c,m) = mdiv cm cm1 in |
|
166 |
(grob_cmul (minus_rat c,m) cms, |
|
167 |
Mmul((minus_rat c,m),hpol)) end) |
|
168 |
handle ERROR _ => error "reduce1"); |
|
169 |
||
170 |
(* Try this for all polynomials in a basis. *) |
|
171 |
fun tryfind f l = |
|
172 |
case l of |
|
173 |
[] => error "tryfind" |
|
174 |
| (h::t) => ((f h) handle ERROR _ => tryfind f t); |
|
175 |
||
176 |
fun reduceb cm basis = tryfind (fn p => reduce1 cm p) basis; |
|
177 |
||
178 |
(* Reduction of a polynomial (always picking largest monomial possible). *) |
|
179 |
||
180 |
fun reduce basis (pol,hist) = |
|
181 |
case pol of |
|
182 |
[] => (pol,hist) |
|
183 |
| cm::ptl => ((let val (q,hnew) = reduceb cm basis in |
|
184 |
reduce basis (grob_add q ptl,Add(hnew,hist)) end) |
|
185 |
handle (ERROR _) => |
|
186 |
(let val (q,hist') = reduce basis (ptl,hist) in |
|
187 |
(cm::q,hist') end)); |
|
188 |
||
189 |
(* Check for orthogonality w.r.t. LCM. *) |
|
190 |
||
191 |
fun orthogonal l p1 p2 = |
|
192 |
snd l = snd(grob_mmul (hd p1) (hd p2)); |
|
193 |
||
194 |
(* Compute S-polynomial of two polynomials. *) |
|
195 |
||
196 |
fun spoly cm ph1 ph2 = |
|
197 |
case (ph1,ph2) of |
|
198 |
(([],h),p) => ([],h) |
|
199 |
| (p,([],h)) => ([],h) |
|
200 |
| ((cm1::ptl1,his1),(cm2::ptl2,his2)) => |
|
201 |
(grob_sub (grob_cmul (mdiv cm cm1) ptl1) |
|
202 |
(grob_cmul (mdiv cm cm2) ptl2), |
|
203 |
Add(Mmul(mdiv cm cm1,his1), |
|
204 |
Mmul(mdiv (minus_rat(fst cm),snd cm) cm2,his2))); |
|
205 |
||
206 |
(* Make a polynomial monic. *) |
|
207 |
||
208 |
fun monic (pol,hist) = |
|
209 |
if pol = [] then (pol,hist) else |
|
210 |
let val (c',m') = hd pol in |
|
211 |
(map (fn (c,m) => (c//c',m)) pol, |
|
212 |
Mmul((rat_1 // c',map (K 0) m'),hist)) end; |
|
213 |
||
214 |
(* The most popular heuristic is to order critical pairs by LCM monomial. *) |
|
215 |
||
216 |
fun forder ((c1,m1),_) ((c2,m2),_) = morder_lt m1 m2; |
|
217 |
||
218 |
fun poly_lt p q = |
|
219 |
case (p,q) of |
|
220 |
(p,[]) => false |
|
221 |
| ([],q) => true |
|
222 |
| ((c1,m1)::o1,(c2,m2)::o2) => |
|
223 |
c1 </ c2 orelse |
|
224 |
c1 =/ c2 andalso ((morder_lt m1 m2) orelse m1 = m2 andalso poly_lt o1 o2); |
|
225 |
||
226 |
fun align ((p,hp),(q,hq)) = |
|
227 |
if poly_lt p q then ((p,hp),(q,hq)) else ((q,hq),(p,hp)); |
|
228 |
fun forall2 p l1 l2 = |
|
229 |
case (l1,l2) of |
|
230 |
([],[]) => true |
|
231 |
| (h1::t1,h2::t2) => p h1 h2 andalso forall2 p t1 t2 |
|
232 |
| _ => false; |
|
233 |
||
234 |
fun poly_eq p1 p2 = |
|
235 |
forall2 (fn (c1,m1) => fn (c2,m2) => c1 =/ c2 andalso m1 = m2) p1 p2; |
|
236 |
||
237 |
fun memx ((p1,h1),(p2,h2)) ppairs = |
|
238 |
not (exists (fn ((q1,_),(q2,_)) => poly_eq p1 q1 andalso poly_eq p2 q2) ppairs); |
|
239 |
||
240 |
(* Buchberger's second criterion. *) |
|
241 |
||
242 |
fun criterion2 basis (lcm,((p1,h1),(p2,h2))) opairs = |
|
243 |
exists (fn g => not(poly_eq (fst g) p1) andalso not(poly_eq (fst g) p2) andalso |
|
244 |
can (mdiv lcm) (hd(fst g)) andalso |
|
245 |
not(memx (align (g,(p1,h1))) (map snd opairs)) andalso |
|
246 |
not(memx (align (g,(p2,h2))) (map snd opairs))) basis; |
|
247 |
||
248 |
(* Test for hitting constant polynomial. *) |
|
249 |
||
250 |
fun constant_poly p = |
|
23259 | 251 |
length p = 1 andalso forall (fn x => x = (0: integer)) (snd(hd p)); |
23252 | 252 |
|
253 |
(* ------------------------------------------------------------------------- *) |
|
254 |
(* Grobner basis algorithm. *) |
|
255 |
(* ------------------------------------------------------------------------- *) |
|
256 |
(* FIXME: try to get rid of mergesort? *) |
|
257 |
fun merge ord l1 l2 = |
|
258 |
case l1 of |
|
259 |
[] => l2 |
|
260 |
| h1::t1 => |
|
261 |
case l2 of |
|
262 |
[] => l1 |
|
263 |
| h2::t2 => if ord h1 h2 then h1::(merge ord t1 l2) |
|
264 |
else h2::(merge ord l1 t2); |
|
265 |
fun mergesort ord l = |
|
266 |
let |
|
267 |
fun mergepairs l1 l2 = |
|
268 |
case (l1,l2) of |
|
269 |
([s],[]) => s |
|
270 |
| (l,[]) => mergepairs [] l |
|
271 |
| (l,[s1]) => mergepairs (s1::l) [] |
|
272 |
| (l,(s1::s2::ss)) => mergepairs ((merge ord s1 s2)::l) ss |
|
273 |
in if l = [] then [] else mergepairs [] (map (fn x => [x]) l) |
|
274 |
end; |
|
275 |
||
276 |
||
277 |
fun grobner_basis basis pairs = |
|
278 |
(writeln (Int.toString(length basis)^" basis elements and "^ |
|
279 |
Int.toString(length pairs)^" critical pairs"); |
|
280 |
case pairs of |
|
281 |
[] => basis |
|
282 |
| (l,(p1,p2))::opairs => |
|
283 |
let val (sph as (sp,hist)) = monic (reduce basis (spoly l p1 p2)) |
|
284 |
in if sp = [] orelse criterion2 basis (l,(p1,p2)) opairs |
|
285 |
then grobner_basis basis opairs |
|
286 |
else if constant_poly sp then grobner_basis (sph::basis) [] |
|
287 |
else let val rawcps = map (fn p => (mlcm (hd(fst p)) (hd sp),align(p,sph))) |
|
288 |
basis |
|
289 |
val newcps = filter |
|
290 |
(fn (l,(p,q)) => not(orthogonal l (fst p) (fst q))) |
|
291 |
rawcps |
|
292 |
in grobner_basis (sph::basis) |
|
293 |
(merge forder opairs (mergesort forder newcps)) |
|
294 |
end |
|
295 |
end); |
|
296 |
||
297 |
(* ------------------------------------------------------------------------- *) |
|
298 |
(* Interreduce initial polynomials. *) |
|
299 |
(* ------------------------------------------------------------------------- *) |
|
300 |
||
301 |
fun grobner_interreduce rpols ipols = |
|
302 |
case ipols of |
|
303 |
[] => map monic (rev rpols) |
|
304 |
| p::ps => let val p' = reduce (rpols @ ps) p in |
|
305 |
if fst p' = [] then grobner_interreduce rpols ps |
|
306 |
else grobner_interreduce (p'::rpols) ps end; |
|
307 |
||
308 |
(* ------------------------------------------------------------------------- *) |
|
309 |
(* Overall function. *) |
|
310 |
(* ------------------------------------------------------------------------- *) |
|
311 |
||
312 |
fun grobner pols = |
|
23259 | 313 |
let val npols = map2 (fn p => fn n => (p,Start n)) pols |
314 |
(map Integer.int (0 upto (length pols - 1))) |
|
23252 | 315 |
val phists = filter (fn (p,_) => p <> []) npols |
316 |
val bas = grobner_interreduce [] (map monic phists) |
|
317 |
val prs0 = product bas bas |
|
318 |
val prs1 = filter (fn ((x,_),(y,_)) => poly_lt x y) prs0 |
|
319 |
val prs2 = map (fn (p,q) => (mlcm (hd(fst p)) (hd(fst q)),(p,q))) prs1 |
|
320 |
val prs3 = |
|
321 |
filter (fn (l,(p,q)) => not(orthogonal l (fst p) (fst q))) prs2 in |
|
322 |
grobner_basis bas (mergesort forder prs3) end; |
|
323 |
||
324 |
(* ------------------------------------------------------------------------- *) |
|
325 |
(* Get proof of contradiction from Grobner basis. *) |
|
326 |
(* ------------------------------------------------------------------------- *) |
|
327 |
fun find p l = |
|
328 |
case l of |
|
329 |
[] => error "find" |
|
330 |
| (h::t) => if p(h) then h else find p t; |
|
331 |
||
332 |
fun grobner_refute pols = |
|
333 |
let val gb = grobner pols in |
|
334 |
snd(find (fn (p,h) => length p = 1 andalso forall (fn x=> x=0) (snd(hd p))) gb) |
|
335 |
end; |
|
336 |
||
337 |
(* ------------------------------------------------------------------------- *) |
|
338 |
(* Turn proof into a certificate as sum of multipliers. *) |
|
339 |
(* *) |
|
340 |
(* In principle this is very inefficient: in a heavily shared proof it may *) |
|
341 |
(* make the same calculation many times. Could put in a cache or something. *) |
|
342 |
(* ------------------------------------------------------------------------- *) |
|
343 |
fun assoc x l = snd(find (fn p => fst p = x) l); |
|
344 |
||
345 |
fun resolve_proof vars prf = |
|
346 |
case prf of |
|
347 |
Start(~1) => [] |
|
348 |
| Start m => [(m,[(rat_1,map (K 0) vars)])] |
|
349 |
| Mmul(pol,lin) => |
|
350 |
let val lis = resolve_proof vars lin in |
|
351 |
map (fn (n,p) => (n,grob_cmul pol p)) lis end |
|
352 |
| Add(lin1,lin2) => |
|
353 |
let val lis1 = resolve_proof vars lin1 |
|
354 |
val lis2 = resolve_proof vars lin2 |
|
355 |
val dom = distinct (op =) ((map fst lis1) union (map fst lis2)) |
|
356 |
in |
|
357 |
map (fn n => let val a = ((assoc n lis1) handle _ => []) (* FIXME *) |
|
358 |
val b = ((assoc n lis2) handle _ => []) in (* FIXME *) |
|
359 |
(n,grob_add a b) end) dom end; |
|
360 |
||
361 |
(* ------------------------------------------------------------------------- *) |
|
362 |
(* Run the procedure and produce Weak Nullstellensatz certificate. *) |
|
363 |
(* ------------------------------------------------------------------------- *) |
|
364 |
fun grobner_weak vars pols = |
|
365 |
let val cert = resolve_proof vars (grobner_refute pols) |
|
366 |
val l = |
|
367 |
fold_rev (fold_rev (lcm_rat o denominator_rat o fst) o snd) cert (rat_1) in |
|
368 |
(l,map (fn (i,p) => (i,map (fn (d,m) => (l*/d,m)) p)) cert) end; |
|
369 |
||
370 |
(* ------------------------------------------------------------------------- *) |
|
371 |
(* Prove a polynomial is in ideal generated by others, using Grobner basis. *) |
|
372 |
(* ------------------------------------------------------------------------- *) |
|
373 |
||
374 |
fun grobner_ideal vars pols pol = |
|
375 |
let val (pol',h) = reduce (grobner pols) (grob_neg pol,Start(~1)) in |
|
376 |
if pol <> [] then error "grobner_ideal: not in the ideal" else |
|
377 |
resolve_proof vars h end; |
|
378 |
||
379 |
(* ------------------------------------------------------------------------- *) |
|
380 |
(* Produce Strong Nullstellensatz certificate for a power of pol. *) |
|
381 |
(* ------------------------------------------------------------------------- *) |
|
382 |
||
383 |
fun grobner_strong vars pols pol = |
|
384 |
let val vars' = @{cterm "True"}::vars |
|
385 |
val grob_z = [(rat_1,1::(map (fn x => 0) vars))] |
|
386 |
val grob_1 = [(rat_1,(map (fn x => 0) vars'))] |
|
387 |
fun augment p= map (fn (c,m) => (c,0::m)) p |
|
388 |
val pols' = map augment pols |
|
389 |
val pol' = augment pol |
|
390 |
val allpols = (grob_sub (grob_mul grob_z pol') grob_1)::pols' |
|
391 |
val (l,cert) = grobner_weak vars' allpols |
|
392 |
val d = fold_rev (fold_rev (max o hd o snd) o snd) cert 0 |
|
393 |
fun transform_monomial (c,m) = |
|
394 |
grob_cmul (c,tl m) (grob_pow vars pol (d - hd m)) |
|
395 |
fun transform_polynomial q = fold_rev (grob_add o transform_monomial) q [] |
|
396 |
val cert' = map (fn (c,q) => (c-1,transform_polynomial q)) |
|
397 |
(filter (fn (k,_) => k <> 0) cert) in |
|
398 |
(d,l,cert') end; |
|
399 |
||
400 |
fun string_of_pol vars pol = |
|
401 |
foldl (fn ((c,m),s) => ((Rat.string_of_rat c) |
|
402 |
^ "*(" ^ |
|
403 |
(snd (foldl |
|
404 |
(fn (e,(i,s)) => |
|
405 |
(i+ 1, |
|
406 |
(nth vars i |
|
23259 | 407 |
|>cterm_of (the_context()) (* FIXME *) |
23252 | 408 |
|> string_of_cterm)^ "^" |
409 |
^ (Int.toString e) ^" * " ^ s)) (0,"0") m)) |
|
410 |
^ ") + ") ^ s) "" pol; |
|
411 |
||
412 |
||
413 |
(* ------------------------------------------------------------------------- *) |
|
414 |
(* Overall parametrized universal procedure for (semi)rings. *) |
|
415 |
(* We return an ideal_conv and the actual ring prover. *) |
|
416 |
(* ------------------------------------------------------------------------- *) |
|
417 |
fun refute_disj rfn tm = |
|
418 |
case term_of tm of |
|
419 |
Const("op |",_)$l$r => |
|
420 |
Drule.compose_single(refute_disj rfn (Thm.dest_arg tm),2,Drule.compose_single(refute_disj rfn (Thm.dest_arg1 tm),2,disjE)) |
|
421 |
| _ => rfn tm ; |
|
422 |
||
423 |
val notnotD = @{thm "notnotD"}; |
|
424 |
fun mk_binop ct x y = |
|
425 |
Thm.capply (Thm.capply ct x) y |
|
426 |
||
427 |
val mk_comb = Thm.capply; |
|
428 |
fun is_neg t = |
|
429 |
case term_of t of |
|
430 |
(Const("Not",_)$p) => true |
|
431 |
| _ => false; |
|
432 |
fun is_eq t = |
|
433 |
case term_of t of |
|
434 |
(Const("op =",_)$_$_) => true |
|
435 |
| _ => false; |
|
436 |
||
437 |
fun end_itlist f l = |
|
438 |
case l of |
|
439 |
[] => error "end_itlist" |
|
440 |
| [x] => x |
|
441 |
| (h::t) => f h (end_itlist f t); |
|
442 |
||
443 |
val list_mk_binop = fn b => end_itlist (mk_binop b); |
|
444 |
||
445 |
val list_dest_binop = fn b => |
|
446 |
let fun h acc t = |
|
447 |
((let val (l,r) = dest_binop b t in h (h acc r) l end) |
|
448 |
handle CTERM _ => (t::acc)) (* Why had I handle _ => ? *) |
|
449 |
in h [] |
|
450 |
end; |
|
451 |
||
452 |
val strip_exists = |
|
453 |
let fun h (acc, t) = |
|
454 |
case (term_of t) of |
|
455 |
Const("Ex",_)$Abs(x,T,p) => h (Thm.dest_abs NONE (Thm.dest_arg t) |>> (fn v => v::acc)) |
|
456 |
| _ => (acc,t) |
|
457 |
in fn t => h ([],t) |
|
458 |
end; |
|
459 |
||
460 |
fun is_forall t = |
|
461 |
case term_of t of |
|
462 |
(Const("All",_)$Abs(_,_,_)) => true |
|
463 |
| _ => false; |
|
464 |
||
465 |
val mk_object_eq = fn th => th COMP meta_eq_to_obj_eq; |
|
466 |
val bool_simps = @{thms "bool_simps"}; |
|
467 |
val nnf_simps = @{thms "nnf_simps"}; |
|
468 |
val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps bool_simps addsimps nnf_simps) |
|
469 |
val weak_dnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps (@{thms "weak_dnf_simps"})); |
|
470 |
val initial_conv = |
|
471 |
Simplifier.rewrite |
|
472 |
(HOL_basic_ss addsimps nnf_simps |
|
473 |
addsimps [not_all, not_ex] addsimps map (fn th => th RS sym) (ex_simps @ all_simps)); |
|
474 |
||
475 |
val specl = fold_rev (fn x => fn th => instantiate' [] [SOME x] (th RS spec)); |
|
476 |
||
477 |
val cTrp = @{cterm "Trueprop"}; |
|
478 |
val cConj = @{cterm "op &"}; |
|
479 |
val (cNot,false_tm) = (@{cterm "Not"}, @{cterm "False"}); |
|
480 |
val ASSUME = mk_comb cTrp #> assume; |
|
481 |
val list_mk_conj = list_mk_binop cConj; |
|
482 |
val conjs = list_dest_binop cConj; |
|
483 |
val mk_neg = mk_comb cNot; |
|
484 |
||
485 |
||
486 |
||
487 |
(** main **) |
|
488 |
||
489 |
fun ring_and_ideal_conv |
|
490 |
{vars, semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), idom} |
|
491 |
dest_const mk_const ring_eq_conv ring_normalize_conv = |
|
492 |
let |
|
493 |
val [add_pat, mul_pat, pow_pat, zero_tm, one_tm] = sr_ops; |
|
494 |
val [ring_add_tm, ring_mul_tm, ring_pow_tm] = |
|
495 |
map (Thm.dest_fun o Thm.dest_fun) [add_pat, mul_pat, pow_pat]; |
|
496 |
||
497 |
val (ring_sub_tm, ring_neg_tm) = |
|
498 |
(case r_ops of |
|
499 |
[] => (@{cterm "True"}, @{cterm "True"}) |
|
500 |
| [sub_pat, neg_pat] => (Thm.dest_fun (Thm.dest_fun sub_pat), Thm.dest_fun neg_pat)); |
|
501 |
||
502 |
val [idom_thm, neq_thm] = idom; |
|
503 |
||
504 |
val ring_dest_neg = |
|
505 |
fn t => let val (l,r) = Thm.dest_comb t in |
|
506 |
if could_unify(term_of l,term_of ring_neg_tm) then r else raise CTERM ("ring_dest_neg", [t]) |
|
507 |
end |
|
508 |
||
509 |
val ring_mk_neg = fn tm => mk_comb (ring_neg_tm) (tm); |
|
510 |
(* |
|
511 |
fun ring_dest_inv t = |
|
512 |
let val (l,r) = Thm.dest_comb t in |
|
513 |
if could_unify(term_of l, term_of ring_inv_tm) then r else raise CTERM "ring_dest_inv" |
|
514 |
end |
|
515 |
*) |
|
516 |
val ring_dest_add = dest_binop ring_add_tm; |
|
517 |
val ring_mk_add = mk_binop ring_add_tm; |
|
518 |
val ring_dest_sub = dest_binop ring_sub_tm; |
|
519 |
val ring_mk_sub = mk_binop ring_sub_tm; |
|
520 |
val ring_dest_mul = dest_binop ring_mul_tm; |
|
521 |
val ring_mk_mul = mk_binop ring_mul_tm; |
|
522 |
(* val ring_dest_div = dest_binop ring_div_tm; |
|
523 |
val ring_mk_div = mk_binop ring_div_tm;*) |
|
524 |
val ring_dest_pow = dest_binop ring_pow_tm; |
|
525 |
val ring_mk_pow = mk_binop ring_pow_tm ; |
|
526 |
fun grobvars tm acc = |
|
527 |
if can dest_const tm then acc |
|
528 |
else if can ring_dest_neg tm then grobvars (Thm.dest_arg tm) acc |
|
529 |
else if can ring_dest_pow tm then grobvars (Thm.dest_arg1 tm) acc |
|
530 |
else if can ring_dest_add tm orelse can ring_dest_sub tm |
|
531 |
orelse can ring_dest_mul tm |
|
532 |
then grobvars (Thm.dest_arg1 tm) (grobvars (Thm.dest_arg tm) acc) |
|
533 |
(* else if can ring_dest_inv tm |
|
534 |
then |
|
535 |
let val gvs = grobvars (Thm.dest_arg tm) [] in |
|
536 |
if gvs = [] then acc else tm::acc |
|
537 |
end |
|
538 |
else if can ring_dest_div tm then |
|
539 |
let val lvs = grobvars (Thm.dest_arg1 tm) acc |
|
540 |
val gvs = grobvars (Thm.dest_arg tm) [] |
|
541 |
in if gvs = [] then lvs else tm::acc |
|
542 |
end *) |
|
543 |
else tm::acc ; |
|
544 |
||
545 |
fun grobify_term vars tm = |
|
546 |
((if not (member (op aconvc) vars tm) then raise CTERM ("Not a variable", [tm]) else |
|
547 |
[(rat_1,map (fn i => if i aconvc tm then 1 else 0) vars)]) |
|
548 |
handle CTERM _ => |
|
549 |
((let val x = dest_const tm |
|
550 |
in if x =/ rat_0 then [] else [(x,map (fn v => 0) vars)] |
|
551 |
end) |
|
552 |
handle ERROR _ => |
|
553 |
((grob_neg(grobify_term vars (ring_dest_neg tm))) |
|
554 |
handle CTERM _ => |
|
555 |
( |
|
556 |
(* (grob_inv(grobify_term vars (ring_dest_inv tm))) |
|
557 |
handle CTERM _ => *) |
|
558 |
((let val (l,r) = ring_dest_add tm |
|
559 |
in grob_add (grobify_term vars l) (grobify_term vars r) |
|
560 |
end) |
|
561 |
handle CTERM _ => |
|
562 |
((let val (l,r) = ring_dest_sub tm |
|
563 |
in grob_sub (grobify_term vars l) (grobify_term vars r) |
|
564 |
end) |
|
565 |
handle CTERM _ => |
|
566 |
((let val (l,r) = ring_dest_mul tm |
|
567 |
in grob_mul (grobify_term vars l) (grobify_term vars r) |
|
568 |
end) |
|
569 |
handle CTERM _ => |
|
570 |
( |
|
571 |
(* (let val (l,r) = ring_dest_div tm |
|
572 |
in grob_div (grobify_term vars l) (grobify_term vars r) |
|
573 |
end) |
|
574 |
handle CTERM _ => *) |
|
575 |
((let val (l,r) = ring_dest_pow tm |
|
576 |
in grob_pow vars (grobify_term vars l) ((term_of #> HOLogic.dest_number #> snd) r) |
|
577 |
end) |
|
578 |
handle CTERM _ => error "grobify_term: unknown or invalid term"))))))))); |
|
579 |
val eq_tm = idom_thm |> concl |> Thm.dest_arg |> Thm.dest_arg |> Thm.dest_fun |> Thm.dest_fun ; |
|
580 |
(*ring_integral |> hd |> concl |> Thm.dest_arg |
|
581 |
|> Thm.dest_abs NONE |> snd |> Thm.dest_fun |> Thm.dest_fun; *) |
|
582 |
val dest_eq = dest_binop eq_tm; |
|
583 |
||
584 |
fun grobify_equation vars tm = |
|
585 |
let val (l,r) = dest_binop eq_tm tm |
|
586 |
in grob_sub (grobify_term vars l) (grobify_term vars r) |
|
587 |
end; |
|
588 |
||
589 |
fun grobify_equations tm = |
|
590 |
let |
|
591 |
val cjs = conjs tm |
|
592 |
val rawvars = fold_rev (fn eq => fn a => |
|
593 |
grobvars (Thm.dest_arg1 eq) (grobvars (Thm.dest_arg eq) a)) cjs [] |
|
594 |
val vars = sort (fn (x, y) => Term.term_ord(term_of x,term_of y)) |
|
595 |
(distinct (op aconvc) rawvars) |
|
596 |
in (vars,map (grobify_equation vars) cjs) |
|
597 |
end; |
|
598 |
||
599 |
val holify_polynomial = |
|
600 |
let fun holify_varpow (v,n) = |
|
601 |
if n = 1 then v else ring_mk_pow v (mk_cnumber @{ctyp "nat"} n) (* FIXME *) |
|
602 |
fun holify_monomial vars (c,m) = |
|
23259 | 603 |
let val xps = map holify_varpow (filter (fn (_,n) => n <> (0: integer)) (vars ~~ m)) |
23252 | 604 |
in end_itlist ring_mk_mul (mk_const c :: xps) |
605 |
end |
|
606 |
fun holify_polynomial vars p = |
|
607 |
if p = [] then mk_const (rat_0) |
|
608 |
else end_itlist ring_mk_add (map (holify_monomial vars) p) |
|
609 |
in holify_polynomial |
|
610 |
end ; |
|
611 |
val idom_rule = simplify (HOL_basic_ss addsimps [idom_thm]); |
|
612 |
fun prove_nz n = eqF_elim |
|
613 |
(ring_eq_conv(mk_binop eq_tm (mk_const n) (mk_const(rat_0)))); |
|
614 |
val neq_01 = prove_nz (rat_1); |
|
615 |
fun neq_rule n th = [prove_nz n, th] MRS neq_thm; |
|
616 |
fun mk_add th1 = combination(Drule.arg_cong_rule ring_add_tm th1); |
|
617 |
||
618 |
fun refute tm = |
|
619 |
if tm aconvc false_tm then ASSUME tm else |
|
620 |
let |
|
621 |
val (nths0,eths0) = List.partition (is_neg o concl) (conjuncts(ASSUME tm)) |
|
622 |
val nths = filter (is_eq o Thm.dest_arg o concl) nths0 |
|
623 |
val eths = filter (is_eq o concl) eths0 |
|
624 |
in |
|
625 |
if null eths then |
|
626 |
let |
|
627 |
val th1 = end_itlist (fn th1 => fn th2 => idom_rule(conji th1 th2)) nths |
|
628 |
val th2 = Conv.fconv_rule |
|
629 |
((arg_conv #> arg_conv) |
|
630 |
(binop_conv ring_normalize_conv)) th1 |
|
631 |
val conc = th2 |> concl |> Thm.dest_arg |
|
632 |
val (l,r) = conc |> dest_eq |
|
633 |
in implies_intr (mk_comb cTrp tm) |
|
634 |
(equal_elim (Drule.arg_cong_rule cTrp (eqF_intr th2)) |
|
635 |
(reflexive l |> mk_object_eq)) |
|
636 |
end |
|
637 |
else |
|
638 |
let |
|
639 |
val (vars,l,cert,noteqth) =( |
|
640 |
if null nths then |
|
641 |
let val (vars,pols) = grobify_equations(list_mk_conj(map concl eths)) |
|
642 |
val (l,cert) = grobner_weak vars pols |
|
643 |
in (vars,l,cert,neq_01) |
|
644 |
end |
|
645 |
else |
|
646 |
let |
|
647 |
val nth = end_itlist (fn th1 => fn th2 => idom_rule(conji th1 th2)) nths |
|
648 |
val (vars,pol::pols) = |
|
649 |
grobify_equations(list_mk_conj(Thm.dest_arg(concl nth)::map concl eths)) |
|
650 |
val (deg,l,cert) = grobner_strong vars pols pol |
|
651 |
val th1 = Conv.fconv_rule((arg_conv o arg_conv)(binop_conv ring_normalize_conv)) nth |
|
23259 | 652 |
val th2 = funpow (Integer.machine_int deg) (idom_rule o conji th1) neq_01 |
23252 | 653 |
in (vars,l,cert,th2) |
654 |
end) |
|
655 |
val _ = writeln ("Translating certificate to HOL inferences") |
|
656 |
val cert_pos = map (fn (i,p) => (i,filter (fn (c,m) => c >/ rat_0) p)) cert |
|
657 |
val cert_neg = map (fn (i,p) => (i,map (fn (c,m) => (minus_rat c,m)) |
|
658 |
(filter (fn (c,m) => c </ rat_0) p))) cert |
|
659 |
val herts_pos = map (fn (i,p) => (i,holify_polynomial vars p)) cert_pos |
|
660 |
val herts_neg = map (fn (i,p) => (i,holify_polynomial vars p)) cert_neg |
|
661 |
fun thm_fn pols = |
|
662 |
if null pols then reflexive(mk_const rat_0) else |
|
663 |
end_itlist mk_add |
|
23259 | 664 |
(map (fn (i,p) => Drule.arg_cong_rule (mk_comb ring_mul_tm p) |
665 |
(nth eths (Integer.machine_int i) |> mk_meta_eq)) pols) |
|
23252 | 666 |
val th1 = thm_fn herts_pos |
667 |
val th2 = thm_fn herts_neg |
|
668 |
val th3 = conji(mk_add (symmetric th1) th2 |> mk_object_eq) noteqth |
|
669 |
val th4 = Conv.fconv_rule ((arg_conv o arg_conv o binop_conv) ring_normalize_conv) |
|
670 |
(neq_rule l th3) |
|
671 |
val (l,r) = dest_eq(Thm.dest_arg(concl th4)) |
|
672 |
in implies_intr (mk_comb cTrp tm) |
|
673 |
(equal_elim (Drule.arg_cong_rule cTrp (eqF_intr th4)) |
|
674 |
(reflexive l |> mk_object_eq)) |
|
675 |
end |
|
676 |
end |
|
677 |
||
678 |
fun ring tm = |
|
679 |
let |
|
680 |
fun mk_forall x p = |
|
681 |
mk_comb (Drule.cterm_rule (instantiate' [SOME (ctyp_of_term x)] []) @{cpat "All:: (?'a => bool) => _"}) (Thm.cabs x p) |
|
682 |
val avs = cterm_frees tm |
|
683 |
val P' = fold mk_forall avs tm |
|
684 |
val th1 = initial_conv(mk_neg P') |
|
685 |
val (evs,bod) = strip_exists(concl th1) in |
|
686 |
if is_forall bod then error "ring: non-universal formula" |
|
687 |
else |
|
688 |
let |
|
689 |
val th1a = weak_dnf_conv bod |
|
690 |
val boda = concl th1a |
|
691 |
val th2a = refute_disj refute boda |
|
692 |
val th2b = [mk_object_eq th1a, (th2a COMP notI) COMP PFalse'] MRS trans |
|
693 |
val th2 = fold (fn v => fn th => (forall_intr v th) COMP allI) evs (th2b RS PFalse) |
|
694 |
val th3 = equal_elim |
|
695 |
(Simplifier.rewrite (HOL_basic_ss addsimps [not_ex RS sym]) |
|
696 |
(th2 |> cprop_of)) th2 |
|
697 |
in specl avs |
|
698 |
([[[mk_object_eq th1, th3 RS PFalse'] MRS trans] MRS PFalse] MRS notnotD) |
|
699 |
end |
|
700 |
end |
|
701 |
fun ideal tms tm ord = |
|
702 |
let |
|
703 |
val rawvars = fold_rev grobvars (tm::tms) [] |
|
704 |
val vars = sort ord (distinct (fn (x,y) => (term_of x) aconv (term_of y)) rawvars) |
|
705 |
val pols = map (grobify_term vars) tms |
|
706 |
val pol = grobify_term vars tm |
|
707 |
val cert = grobner_ideal vars pols pol |
|
708 |
in map (fn n => let val p = assocd n cert [] in holify_polynomial vars p end) |
|
23259 | 709 |
(map Integer.int (0 upto (length pols - 1))) |
23252 | 710 |
end |
711 |
in (ring,ideal) |
|
712 |
end; |
|
713 |
||
714 |
||
715 |
fun find_term bounds tm = |
|
716 |
(case term_of tm of |
|
717 |
Const ("op =", T) $ _ $ _ => |
|
718 |
if domain_type T = HOLogic.boolT then find_args bounds tm |
|
719 |
else Thm.dest_arg tm |
|
720 |
| Const ("Not", _) $ _ => find_term bounds (Thm.dest_arg tm) |
|
721 |
| Const ("All", _) $ _ => find_body bounds (Thm.dest_arg tm) |
|
722 |
| Const ("Ex", _) $ _ => find_body bounds (Thm.dest_arg tm) |
|
723 |
| Const ("op &", _) $ _ $ _ => find_args bounds tm |
|
724 |
| Const ("op |", _) $ _ $ _ => find_args bounds tm |
|
725 |
| Const ("op -->", _) $ _ $ _ => find_args bounds tm |
|
726 |
| _ => raise TERM ("find_term", [])) |
|
727 |
and find_args bounds tm = |
|
728 |
let val (t, u) = Thm.dest_binop tm |
|
729 |
in (find_term bounds t handle TERM _ => find_term bounds u) end |
|
730 |
and find_body bounds b = |
|
731 |
let val (_, b') = Thm.dest_abs (SOME (Name.bound bounds)) b |
|
732 |
in find_term (bounds + 1) b' end; |
|
733 |
||
734 |
fun ring_conv ctxt form = |
|
735 |
(case try (find_term 0 (* FIXME !? *)) form of |
|
736 |
NONE => reflexive form |
|
737 |
| SOME tm => |
|
738 |
(case NormalizerData.match ctxt tm of |
|
739 |
NONE => reflexive form |
|
740 |
| SOME (res as (theory, {is_const, dest_const, mk_const, conv = ring_eq_conv})) => |
|
741 |
fst (ring_and_ideal_conv theory |
|
23330
01c09922ce59
Conversion for computation on constants now depends on the context
chaieb
parents:
23259
diff
changeset
|
742 |
dest_const (mk_const (Thm.ctyp_of_term tm)) (ring_eq_conv ctxt) |
01c09922ce59
Conversion for computation on constants now depends on the context
chaieb
parents:
23259
diff
changeset
|
743 |
(semiring_normalize_wrapper ctxt res)) form)); |
23252 | 744 |
|
745 |
end; |