author | wenzelm |
Mon, 20 May 2013 17:11:17 +0200 | |
changeset 52086 | fcb40cadc173 |
parent 51930 | 52fd62618631 |
child 52467 | 24c6ddb48cb8 |
permissions | -rw-r--r-- |
37744 | 1 |
(* Title: HOL/Tools/groebner.ML |
23252 | 2 |
Author: Amine Chaieb, TU Muenchen |
3 |
*) |
|
4 |
||
5 |
signature GROEBNER = |
|
6 |
sig |
|
41453 | 7 |
val ring_and_ideal_conv: |
30866 | 8 |
{idom: thm list, ring: cterm list * thm list, field: cterm list * thm list, |
9 |
vars: cterm list, semiring: cterm list * thm list, ideal : thm list} -> |
|
23487 | 10 |
(cterm -> Rat.rat) -> (Rat.rat -> cterm) -> |
11 |
conv -> conv -> |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
12 |
{ring_conv: Proof.context -> conv, |
36723 | 13 |
simple_ideal: (cterm list -> cterm -> (cterm * cterm -> order) -> cterm list), |
14 |
multi_ideal: cterm list -> cterm list -> cterm list -> (cterm * cterm) list, |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
15 |
poly_eq_ss: simpset, unwind_conv: Proof.context -> conv} |
36723 | 16 |
val ring_tac: thm list -> thm list -> Proof.context -> int -> tactic |
17 |
val ideal_tac: thm list -> thm list -> Proof.context -> int -> tactic |
|
18 |
val algebra_tac: thm list -> thm list -> Proof.context -> int -> tactic |
|
23252 | 19 |
end |
20 |
||
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
21 |
structure Groebner : GROEBNER = |
23252 | 22 |
struct |
23 |
||
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
24 |
fun is_comb ct = |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
25 |
(case Thm.term_of ct of |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
26 |
_ $ _ => true |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
27 |
| _ => false); |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
28 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
29 |
val concl = Thm.cprop_of #> Thm.dest_arg; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
30 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
31 |
fun is_binop ct ct' = |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
32 |
(case Thm.term_of ct' of |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
33 |
c $ _ $ _ => term_of ct aconv c |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
34 |
| _ => false); |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
35 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
36 |
fun dest_binary ct ct' = |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
37 |
if is_binop ct ct' then Thm.dest_binop ct' |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
38 |
else raise CTERM ("dest_binary: bad binop", [ct, ct']) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
39 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
40 |
fun inst_thm inst = Thm.instantiate ([], inst); |
23557 | 41 |
|
23252 | 42 |
val rat_0 = Rat.zero; |
43 |
val rat_1 = Rat.one; |
|
44 |
val minus_rat = Rat.neg; |
|
45 |
val denominator_rat = Rat.quotient_of_rat #> snd #> Rat.rat_of_int; |
|
46 |
fun int_of_rat a = |
|
47 |
case Rat.quotient_of_rat a of (i,1) => i | _ => error "int_of_rat: not an int"; |
|
23514 | 48 |
val lcm_rat = fn x => fn y => Rat.rat_of_int (Integer.lcm (int_of_rat x) (int_of_rat y)); |
23252 | 49 |
|
50 |
val (eqF_intr, eqF_elim) = |
|
36713 | 51 |
let val [th1,th2] = @{thms PFalse} |
23252 | 52 |
in (fn th => th COMP th2, fn th => th COMP th1) end; |
53 |
||
54 |
val (PFalse, PFalse') = |
|
36713 | 55 |
let val PFalse_eq = nth @{thms simp_thms} 13 |
23252 | 56 |
in (PFalse_eq RS iffD1, PFalse_eq RS iffD2) end; |
57 |
||
58 |
||
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
59 |
(* Type for recording history, i.e. how a polynomial was obtained. *) |
23252 | 60 |
|
61 |
datatype history = |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
62 |
Start of int |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
63 |
| Mmul of (Rat.rat * int list) * history |
23252 | 64 |
| Add of history * history; |
65 |
||
66 |
||
67 |
(* Monomial ordering. *) |
|
68 |
||
69 |
fun morder_lt m1 m2= |
|
70 |
let fun lexorder l1 l2 = |
|
71 |
case (l1,l2) of |
|
72 |
([],[]) => false |
|
73 |
| (x1::o1,x2::o2) => x1 > x2 orelse x1 = x2 andalso lexorder o1 o2 |
|
74 |
| _ => error "morder: inconsistent monomial lengths" |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
75 |
val n1 = Integer.sum m1 |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
76 |
val n2 = Integer.sum m2 in |
23252 | 77 |
n1 < n2 orelse n1 = n2 andalso lexorder m1 m2 |
78 |
end; |
|
79 |
||
80 |
fun morder_le m1 m2 = morder_lt m1 m2 orelse (m1 = m2); |
|
81 |
||
82 |
fun morder_gt m1 m2 = morder_lt m2 m1; |
|
83 |
||
84 |
(* Arithmetic on canonical polynomials. *) |
|
85 |
||
86 |
fun grob_neg l = map (fn (c,m) => (minus_rat c,m)) l; |
|
87 |
||
88 |
fun grob_add l1 l2 = |
|
89 |
case (l1,l2) of |
|
90 |
([],l2) => l2 |
|
91 |
| (l1,[]) => l1 |
|
92 |
| ((c1,m1)::o1,(c2,m2)::o2) => |
|
93 |
if m1 = m2 then |
|
94 |
let val c = c1+/c2 val rest = grob_add o1 o2 in |
|
95 |
if c =/ rat_0 then rest else (c,m1)::rest end |
|
96 |
else if morder_lt m2 m1 then (c1,m1)::(grob_add o1 l2) |
|
97 |
else (c2,m2)::(grob_add l1 o2); |
|
98 |
||
99 |
fun grob_sub l1 l2 = grob_add l1 (grob_neg l2); |
|
100 |
||
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
101 |
fun grob_mmul (c1,m1) (c2,m2) = (c1*/c2, ListPair.map (op +) (m1, m2)); |
23252 | 102 |
|
103 |
fun grob_cmul cm pol = map (grob_mmul cm) pol; |
|
104 |
||
105 |
fun grob_mul l1 l2 = |
|
106 |
case l1 of |
|
107 |
[] => [] |
|
108 |
| (h1::t1) => grob_add (grob_cmul h1 l2) (grob_mul t1 l2); |
|
109 |
||
110 |
fun grob_inv l = |
|
111 |
case l of |
|
112 |
[(c,vs)] => if (forall (fn x => x = 0) vs) then |
|
113 |
if (c =/ rat_0) then error "grob_inv: division by zero" |
|
114 |
else [(rat_1 // c,vs)] |
|
115 |
else error "grob_inv: non-constant divisor polynomial" |
|
116 |
| _ => error "grob_inv: non-constant divisor polynomial"; |
|
117 |
||
118 |
fun grob_div l1 l2 = |
|
119 |
case l2 of |
|
120 |
[(c,l)] => if (forall (fn x => x = 0) l) then |
|
121 |
if c =/ rat_0 then error "grob_div: division by zero" |
|
122 |
else grob_cmul (rat_1 // c,l) l1 |
|
123 |
else error "grob_div: non-constant divisor polynomial" |
|
124 |
| _ => error "grob_div: non-constant divisor polynomial"; |
|
125 |
||
126 |
fun grob_pow vars l n = |
|
127 |
if n < 0 then error "grob_pow: negative power" |
|
128 |
else if n = 0 then [(rat_1,map (fn v => 0) vars)] |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
129 |
else grob_mul l (grob_pow vars l (n - 1)); |
23252 | 130 |
|
131 |
fun degree vn p = |
|
132 |
case p of |
|
133 |
[] => error "Zero polynomial" |
|
134 |
| [(c,ns)] => nth ns vn |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
135 |
| (c,ns)::p' => Int.max (nth ns vn, degree vn p'); |
23252 | 136 |
|
137 |
fun head_deg vn p = let val d = degree vn p in |
|
138 |
(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; |
|
139 |
||
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
140 |
val is_zerop = forall (fn (c,ns) => c =/ rat_0 andalso forall (curry (op =) 0) ns); |
23252 | 141 |
val grob_pdiv = |
142 |
let fun pdiv_aux vn (n,a) p k s = |
|
143 |
if is_zerop s then (k,s) else |
|
144 |
let val (m,b) = head_deg vn s |
|
145 |
in if m < n then (k,s) else |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
146 |
let val p' = grob_mul p [(rat_1, map_index (fn (i,v) => if i = vn then m - n else 0) |
23252 | 147 |
(snd (hd s)))] |
148 |
in if a = b then pdiv_aux vn (n,a) p k (grob_sub s p') |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
149 |
else pdiv_aux vn (n,a) p (k + 1) (grob_sub (grob_mul a s) (grob_mul b p')) |
23252 | 150 |
end |
151 |
end |
|
152 |
in fn vn => fn s => fn p => pdiv_aux vn (head_deg vn p) p 0 s |
|
153 |
end; |
|
154 |
||
155 |
(* Monomial division operation. *) |
|
156 |
||
157 |
fun mdiv (c1,m1) (c2,m2) = |
|
158 |
(c1//c2, |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
159 |
map2 (fn n1 => fn n2 => if n1 < n2 then error "mdiv" else n1 - n2) m1 m2); |
23252 | 160 |
|
161 |
(* Lowest common multiple of two monomials. *) |
|
162 |
||
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
163 |
fun mlcm (c1,m1) (c2,m2) = (rat_1, ListPair.map Int.max (m1, m2)); |
23252 | 164 |
|
165 |
(* Reduce monomial cm by polynomial pol, returning replacement for cm. *) |
|
166 |
||
167 |
fun reduce1 cm (pol,hpol) = |
|
168 |
case pol of |
|
169 |
[] => error "reduce1" |
|
170 |
| cm1::cms => ((let val (c,m) = mdiv cm cm1 in |
|
171 |
(grob_cmul (minus_rat c,m) cms, |
|
172 |
Mmul((minus_rat c,m),hpol)) end) |
|
173 |
handle ERROR _ => error "reduce1"); |
|
174 |
||
175 |
(* Try this for all polynomials in a basis. *) |
|
176 |
fun tryfind f l = |
|
177 |
case l of |
|
178 |
[] => error "tryfind" |
|
179 |
| (h::t) => ((f h) handle ERROR _ => tryfind f t); |
|
180 |
||
181 |
fun reduceb cm basis = tryfind (fn p => reduce1 cm p) basis; |
|
182 |
||
183 |
(* Reduction of a polynomial (always picking largest monomial possible). *) |
|
184 |
||
185 |
fun reduce basis (pol,hist) = |
|
186 |
case pol of |
|
187 |
[] => (pol,hist) |
|
188 |
| cm::ptl => ((let val (q,hnew) = reduceb cm basis in |
|
189 |
reduce basis (grob_add q ptl,Add(hnew,hist)) end) |
|
190 |
handle (ERROR _) => |
|
191 |
(let val (q,hist') = reduce basis (ptl,hist) in |
|
192 |
(cm::q,hist') end)); |
|
193 |
||
194 |
(* Check for orthogonality w.r.t. LCM. *) |
|
195 |
||
196 |
fun orthogonal l p1 p2 = |
|
197 |
snd l = snd(grob_mmul (hd p1) (hd p2)); |
|
198 |
||
199 |
(* Compute S-polynomial of two polynomials. *) |
|
200 |
||
201 |
fun spoly cm ph1 ph2 = |
|
202 |
case (ph1,ph2) of |
|
203 |
(([],h),p) => ([],h) |
|
204 |
| (p,([],h)) => ([],h) |
|
205 |
| ((cm1::ptl1,his1),(cm2::ptl2,his2)) => |
|
206 |
(grob_sub (grob_cmul (mdiv cm cm1) ptl1) |
|
207 |
(grob_cmul (mdiv cm cm2) ptl2), |
|
208 |
Add(Mmul(mdiv cm cm1,his1), |
|
209 |
Mmul(mdiv (minus_rat(fst cm),snd cm) cm2,his2))); |
|
210 |
||
211 |
(* Make a polynomial monic. *) |
|
212 |
||
213 |
fun monic (pol,hist) = |
|
23579 | 214 |
if null pol then (pol,hist) else |
23252 | 215 |
let val (c',m') = hd pol in |
216 |
(map (fn (c,m) => (c//c',m)) pol, |
|
217 |
Mmul((rat_1 // c',map (K 0) m'),hist)) end; |
|
218 |
||
219 |
(* The most popular heuristic is to order critical pairs by LCM monomial. *) |
|
220 |
||
221 |
fun forder ((c1,m1),_) ((c2,m2),_) = morder_lt m1 m2; |
|
222 |
||
223 |
fun poly_lt p q = |
|
224 |
case (p,q) of |
|
225 |
(p,[]) => false |
|
226 |
| ([],q) => true |
|
227 |
| ((c1,m1)::o1,(c2,m2)::o2) => |
|
228 |
c1 </ c2 orelse |
|
229 |
c1 =/ c2 andalso ((morder_lt m1 m2) orelse m1 = m2 andalso poly_lt o1 o2); |
|
230 |
||
231 |
fun align ((p,hp),(q,hq)) = |
|
232 |
if poly_lt p q then ((p,hp),(q,hq)) else ((q,hq),(p,hp)); |
|
233 |
||
234 |
fun poly_eq p1 p2 = |
|
40718 | 235 |
eq_list (fn ((c1, m1), (c2, m2)) => c1 =/ c2 andalso (m1: int list) = m2) (p1, p2); |
23252 | 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 = |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
251 |
length p = 1 andalso forall (fn x => x = 0) (snd(hd p)); |
23252 | 252 |
|
253 |
(* Grobner basis algorithm. *) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
254 |
|
23252 | 255 |
(* FIXME: try to get rid of mergesort? *) |
256 |
fun merge ord l1 l2 = |
|
257 |
case l1 of |
|
258 |
[] => l2 |
|
259 |
| h1::t1 => |
|
260 |
case l2 of |
|
261 |
[] => l1 |
|
262 |
| h2::t2 => if ord h1 h2 then h1::(merge ord t1 l2) |
|
263 |
else h2::(merge ord l1 t2); |
|
264 |
fun mergesort ord l = |
|
265 |
let |
|
266 |
fun mergepairs l1 l2 = |
|
267 |
case (l1,l2) of |
|
268 |
([s],[]) => s |
|
269 |
| (l,[]) => mergepairs [] l |
|
270 |
| (l,[s1]) => mergepairs (s1::l) [] |
|
271 |
| (l,(s1::s2::ss)) => mergepairs ((merge ord s1 s2)::l) ss |
|
23579 | 272 |
in if null l then [] else mergepairs [] (map (fn x => [x]) l) |
23252 | 273 |
end; |
274 |
||
275 |
||
276 |
fun grobner_basis basis pairs = |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
277 |
case pairs of |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
278 |
[] => basis |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
279 |
| (l,(p1,p2))::opairs => |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
280 |
let val (sph as (sp,hist)) = monic (reduce basis (spoly l p1 p2)) |
41453 | 281 |
in |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
282 |
if null sp orelse criterion2 basis (l,(p1,p2)) opairs |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
283 |
then grobner_basis basis opairs |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
284 |
else if constant_poly sp then grobner_basis (sph::basis) [] |
41453 | 285 |
else |
286 |
let |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
287 |
val rawcps = map (fn p => (mlcm (hd(fst p)) (hd sp),align(p,sph))) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
288 |
basis |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
289 |
val newcps = filter (fn (l,(p,q)) => not(orthogonal l (fst p) (fst q))) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
290 |
rawcps |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
291 |
in grobner_basis (sph::basis) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
292 |
(merge forder opairs (mergesort forder newcps)) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
293 |
end |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
294 |
end; |
23252 | 295 |
|
296 |
(* Interreduce initial polynomials. *) |
|
297 |
||
298 |
fun grobner_interreduce rpols ipols = |
|
299 |
case ipols of |
|
300 |
[] => map monic (rev rpols) |
|
301 |
| p::ps => let val p' = reduce (rpols @ ps) p in |
|
23579 | 302 |
if null (fst p') then grobner_interreduce rpols ps |
23252 | 303 |
else grobner_interreduce (p'::rpols) ps end; |
304 |
||
305 |
(* Overall function. *) |
|
306 |
||
307 |
fun grobner pols = |
|
33063 | 308 |
let val npols = map_index (fn (n, p) => (p, Start n)) pols |
23579 | 309 |
val phists = filter (fn (p,_) => not (null p)) npols |
23252 | 310 |
val bas = grobner_interreduce [] (map monic phists) |
25538 | 311 |
val prs0 = map_product pair bas bas |
23252 | 312 |
val prs1 = filter (fn ((x,_),(y,_)) => poly_lt x y) prs0 |
313 |
val prs2 = map (fn (p,q) => (mlcm (hd(fst p)) (hd(fst q)),(p,q))) prs1 |
|
314 |
val prs3 = |
|
315 |
filter (fn (l,(p,q)) => not(orthogonal l (fst p) (fst q))) prs2 in |
|
316 |
grobner_basis bas (mergesort forder prs3) end; |
|
317 |
||
318 |
(* Get proof of contradiction from Grobner basis. *) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
319 |
|
23252 | 320 |
fun find p l = |
321 |
case l of |
|
322 |
[] => error "find" |
|
323 |
| (h::t) => if p(h) then h else find p t; |
|
324 |
||
325 |
fun grobner_refute pols = |
|
326 |
let val gb = grobner pols in |
|
327 |
snd(find (fn (p,h) => length p = 1 andalso forall (fn x=> x=0) (snd(hd p))) gb) |
|
328 |
end; |
|
329 |
||
330 |
(* Turn proof into a certificate as sum of multipliers. *) |
|
331 |
(* In principle this is very inefficient: in a heavily shared proof it may *) |
|
332 |
(* make the same calculation many times. Could put in a cache or something. *) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
333 |
|
23252 | 334 |
fun resolve_proof vars prf = |
335 |
case prf of |
|
336 |
Start(~1) => [] |
|
337 |
| Start m => [(m,[(rat_1,map (K 0) vars)])] |
|
338 |
| Mmul(pol,lin) => |
|
339 |
let val lis = resolve_proof vars lin in |
|
340 |
map (fn (n,p) => (n,grob_cmul pol p)) lis end |
|
341 |
| Add(lin1,lin2) => |
|
342 |
let val lis1 = resolve_proof vars lin1 |
|
343 |
val lis2 = resolve_proof vars lin2 |
|
33042 | 344 |
val dom = distinct (op =) (union (op =) (map fst lis1) (map fst lis2)) |
23252 | 345 |
in |
23557 | 346 |
map (fn n => let val a = these (AList.lookup (op =) lis1 n) |
347 |
val b = these (AList.lookup (op =) lis2 n) |
|
348 |
in (n,grob_add a b) end) dom end; |
|
23252 | 349 |
|
350 |
(* Run the procedure and produce Weak Nullstellensatz certificate. *) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
351 |
|
23252 | 352 |
fun grobner_weak vars pols = |
353 |
let val cert = resolve_proof vars (grobner_refute pols) |
|
354 |
val l = |
|
355 |
fold_rev (fold_rev (lcm_rat o denominator_rat o fst) o snd) cert (rat_1) in |
|
356 |
(l,map (fn (i,p) => (i,map (fn (d,m) => (l*/d,m)) p)) cert) end; |
|
357 |
||
358 |
(* Prove a polynomial is in ideal generated by others, using Grobner basis. *) |
|
359 |
||
360 |
fun grobner_ideal vars pols pol = |
|
361 |
let val (pol',h) = reduce (grobner pols) (grob_neg pol,Start(~1)) in |
|
24913 | 362 |
if not (null pol') then error "grobner_ideal: not in the ideal" else |
23252 | 363 |
resolve_proof vars h end; |
364 |
||
365 |
(* Produce Strong Nullstellensatz certificate for a power of pol. *) |
|
366 |
||
367 |
fun grobner_strong vars pols pol = |
|
368 |
let val vars' = @{cterm "True"}::vars |
|
369 |
val grob_z = [(rat_1,1::(map (fn x => 0) vars))] |
|
370 |
val grob_1 = [(rat_1,(map (fn x => 0) vars'))] |
|
371 |
fun augment p= map (fn (c,m) => (c,0::m)) p |
|
372 |
val pols' = map augment pols |
|
373 |
val pol' = augment pol |
|
374 |
val allpols = (grob_sub (grob_mul grob_z pol') grob_1)::pols' |
|
375 |
val (l,cert) = grobner_weak vars' allpols |
|
33029 | 376 |
val d = fold (fold (Integer.max o hd o snd) o snd) cert 0 |
23252 | 377 |
fun transform_monomial (c,m) = |
378 |
grob_cmul (c,tl m) (grob_pow vars pol (d - hd m)) |
|
379 |
fun transform_polynomial q = fold_rev (grob_add o transform_monomial) q [] |
|
380 |
val cert' = map (fn (c,q) => (c-1,transform_polynomial q)) |
|
381 |
(filter (fn (k,_) => k <> 0) cert) in |
|
382 |
(d,l,cert') end; |
|
383 |
||
384 |
||
385 |
(* Overall parametrized universal procedure for (semi)rings. *) |
|
386 |
(* We return an ideal_conv and the actual ring prover. *) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
387 |
|
23252 | 388 |
fun refute_disj rfn tm = |
389 |
case term_of tm of |
|
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset
|
390 |
Const(@{const_name HOL.disj},_)$l$r => |
41453 | 391 |
Drule.compose_single |
392 |
(refute_disj rfn (Thm.dest_arg tm), 2, |
|
393 |
Drule.compose_single (refute_disj rfn (Thm.dest_arg1 tm), 2, disjE)) |
|
23252 | 394 |
| _ => rfn tm ; |
395 |
||
36713 | 396 |
val notnotD = @{thm notnotD}; |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
397 |
fun mk_binop ct x y = Thm.apply (Thm.apply ct x) y |
23252 | 398 |
|
399 |
fun is_neg t = |
|
400 |
case term_of t of |
|
38558 | 401 |
(Const(@{const_name Not},_)$p) => true |
23252 | 402 |
| _ => false; |
403 |
fun is_eq t = |
|
404 |
case term_of t of |
|
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
38795
diff
changeset
|
405 |
(Const(@{const_name HOL.eq},_)$_$_) => true |
23252 | 406 |
| _ => false; |
407 |
||
408 |
fun end_itlist f l = |
|
409 |
case l of |
|
410 |
[] => error "end_itlist" |
|
411 |
| [x] => x |
|
412 |
| (h::t) => f h (end_itlist f t); |
|
413 |
||
414 |
val list_mk_binop = fn b => end_itlist (mk_binop b); |
|
415 |
||
416 |
val list_dest_binop = fn b => |
|
417 |
let fun h acc t = |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
418 |
((let val (l,r) = dest_binary b t in h (h acc r) l end) |
23252 | 419 |
handle CTERM _ => (t::acc)) (* Why had I handle _ => ? *) |
420 |
in h [] |
|
421 |
end; |
|
422 |
||
423 |
val strip_exists = |
|
424 |
let fun h (acc, t) = |
|
41453 | 425 |
case term_of t of |
426 |
Const (@{const_name Ex}, _) $ Abs (x, T, p) => |
|
427 |
h (Thm.dest_abs NONE (Thm.dest_arg t) |>> (fn v => v::acc)) |
|
23252 | 428 |
| _ => (acc,t) |
429 |
in fn t => h ([],t) |
|
430 |
end; |
|
431 |
||
432 |
fun is_forall t = |
|
433 |
case term_of t of |
|
38558 | 434 |
(Const(@{const_name All},_)$Abs(_,_,_)) => true |
23252 | 435 |
| _ => false; |
436 |
||
437 |
val mk_object_eq = fn th => th COMP meta_eq_to_obj_eq; |
|
36713 | 438 |
val bool_simps = @{thms bool_simps}; |
439 |
val nnf_simps = @{thms nnf_simps}; |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
440 |
fun nnf_conv ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
441 |
Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps bool_simps addsimps nnf_simps) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
442 |
|
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
443 |
fun weak_dnf_conv ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
444 |
Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps @{thms weak_dnf_simps}); |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
445 |
|
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
446 |
val initial_ss = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
447 |
simpset_of (put_simpset HOL_basic_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
448 |
addsimps nnf_simps |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
449 |
addsimps [not_all, not_ex] |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
450 |
addsimps map (fn th => th RS sym) (@{thms ex_simps} @ @{thms all_simps})); |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
451 |
fun initial_conv ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
452 |
Simplifier.rewrite (put_simpset initial_ss ctxt); |
23252 | 453 |
|
454 |
val specl = fold_rev (fn x => fn th => instantiate' [] [SOME x] (th RS spec)); |
|
455 |
||
456 |
val cTrp = @{cterm "Trueprop"}; |
|
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset
|
457 |
val cConj = @{cterm HOL.conj}; |
23252 | 458 |
val (cNot,false_tm) = (@{cterm "Not"}, @{cterm "False"}); |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
459 |
val assume_Trueprop = Thm.apply cTrp #> Thm.assume; |
23252 | 460 |
val list_mk_conj = list_mk_binop cConj; |
461 |
val conjs = list_dest_binop cConj; |
|
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
462 |
val mk_neg = Thm.apply cNot; |
23252 | 463 |
|
41453 | 464 |
fun striplist dest = |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
465 |
let |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
466 |
fun h acc x = case try dest x of |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
467 |
SOME (a,b) => h (h acc b) a |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
468 |
| NONE => x::acc |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
469 |
in h [] end; |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
470 |
fun list_mk_binop b = foldr1 (fn (s,t) => Thm.apply (Thm.apply b s) t); |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
471 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
472 |
val eq_commute = mk_meta_eq @{thm eq_commute}; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
473 |
|
41453 | 474 |
fun sym_conv eq = |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
475 |
let val (l,r) = Thm.dest_binop eq |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
476 |
in instantiate' [SOME (ctyp_of_term l)] [SOME l, SOME r] eq_commute |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
477 |
end; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
478 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
479 |
(* FIXME : copied from cqe.ML -- complex QE*) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
480 |
fun conjuncts ct = |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
481 |
case term_of ct of |
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset
|
482 |
@{term HOL.conj}$_$_ => (Thm.dest_arg1 ct)::(conjuncts (Thm.dest_arg ct)) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
483 |
| _ => [ct]; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
484 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
485 |
fun fold1 f = foldr1 (uncurry f); |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
486 |
|
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
487 |
val list_conj = fold1 (fn c => fn c' => Thm.apply (Thm.apply @{cterm HOL.conj} c) c') ; |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
488 |
|
41453 | 489 |
fun mk_conj_tab th = |
490 |
let fun h acc th = |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
491 |
case prop_of th of |
41453 | 492 |
@{term "Trueprop"}$(@{term HOL.conj}$p$q) => |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
493 |
h (h acc (th RS conjunct2)) (th RS conjunct1) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
494 |
| @{term "Trueprop"}$p => (p,th)::acc |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
495 |
in fold (Termtab.insert Thm.eq_thm) (h [] th) Termtab.empty end; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
496 |
|
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset
|
497 |
fun is_conj (@{term HOL.conj}$_$_) = true |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
498 |
| is_conj _ = false; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
499 |
|
41453 | 500 |
fun prove_conj tab cjs = |
501 |
case cjs of |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
502 |
[c] => if is_conj (term_of c) then prove_conj tab (conjuncts c) else tab c |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
503 |
| c::cs => conjI OF [prove_conj tab [c], prove_conj tab cs]; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
504 |
|
41453 | 505 |
fun conj_ac_rule eq = |
506 |
let |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
507 |
val (l,r) = Thm.dest_equals eq |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
508 |
val ctabl = mk_conj_tab (Thm.assume (Thm.apply @{cterm Trueprop} l)) |
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
509 |
val ctabr = mk_conj_tab (Thm.assume (Thm.apply @{cterm Trueprop} r)) |
33035 | 510 |
fun tabl c = the (Termtab.lookup ctabl (term_of c)) |
511 |
fun tabr c = the (Termtab.lookup ctabr (term_of c)) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
512 |
val thl = prove_conj tabl (conjuncts r) |> implies_intr_hyps |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
513 |
val thr = prove_conj tabr (conjuncts l) |> implies_intr_hyps |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
514 |
val eqI = instantiate' [] [SOME l, SOME r] @{thm iffI} |
41453 | 515 |
in Thm.implies_elim (Thm.implies_elim eqI thl) thr |> mk_meta_eq end; |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
516 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
517 |
(* END FIXME.*) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
518 |
|
41453 | 519 |
(* Conversion for the equivalence of existential statements where |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
520 |
EX quantifiers are rearranged differently *) |
41453 | 521 |
fun ext T = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat Ex} |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
522 |
fun mk_ex v t = Thm.apply (ext (ctyp_of_term v)) (Thm.lambda v t) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
523 |
|
41453 | 524 |
fun choose v th th' = case concl_of th of |
525 |
@{term Trueprop} $ (Const(@{const_name Ex},_)$_) => |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
526 |
let |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
527 |
val p = (funpow 2 Thm.dest_arg o cprop_of) th |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
528 |
val T = (hd o Thm.dest_ctyp o ctyp_of_term) p |
41453 | 529 |
val th0 = Conv.fconv_rule (Thm.beta_conversion true) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
530 |
(instantiate' [SOME T] [SOME p, (SOME o Thm.dest_arg o cprop_of) th'] exE) |
41453 | 531 |
val pv = (Thm.rhs_of o Thm.beta_conversion true) |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
532 |
(Thm.apply @{cterm Trueprop} (Thm.apply p v)) |
41453 | 533 |
val th1 = Thm.forall_intr v (Thm.implies_intr pv th') |
534 |
in Thm.implies_elim (Thm.implies_elim th0 th) th1 end |
|
535 |
| _ => error "" (* FIXME ? *) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
536 |
|
41453 | 537 |
fun simple_choose v th = |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
538 |
choose v (Thm.assume ((Thm.apply @{cterm Trueprop} o mk_ex v) |
41453 | 539 |
((Thm.dest_arg o hd o #hyps o Thm.crep_thm) th))) th |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
540 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
541 |
|
41453 | 542 |
fun mkexi v th = |
543 |
let |
|
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
544 |
val p = Thm.lambda v (Thm.dest_arg (Thm.cprop_of th)) |
41453 | 545 |
in Thm.implies_elim |
546 |
(Conv.fconv_rule (Thm.beta_conversion true) |
|
547 |
(instantiate' [SOME (ctyp_of_term v)] [SOME p, SOME v] @{thm exI})) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
548 |
th |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
549 |
end |
41453 | 550 |
fun ex_eq_conv t = |
551 |
let |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
552 |
val (p0,q0) = Thm.dest_binop t |
41453 | 553 |
val (vs',P) = strip_exists p0 |
554 |
val (vs,_) = strip_exists q0 |
|
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
555 |
val th = Thm.assume (Thm.apply @{cterm Trueprop} P) |
41453 | 556 |
val th1 = implies_intr_hyps (fold simple_choose vs' (fold mkexi vs th)) |
557 |
val th2 = implies_intr_hyps (fold simple_choose vs (fold mkexi vs' th)) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
558 |
val p = (Thm.dest_arg o Thm.dest_arg1 o cprop_of) th1 |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
559 |
val q = (Thm.dest_arg o Thm.dest_arg o cprop_of) th1 |
41453 | 560 |
in Thm.implies_elim (Thm.implies_elim (instantiate' [] [SOME p, SOME q] iffI) th1) th2 |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
561 |
|> mk_meta_eq |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
562 |
end; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
563 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
564 |
|
41453 | 565 |
fun getname v = case term_of v of |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
566 |
Free(s,_) => s |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
567 |
| Var ((s,_),_) => s |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
568 |
| _ => "x" |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
569 |
fun mk_eq s t = Thm.apply (Thm.apply @{cterm "op == :: bool => _"} s) t |
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
570 |
fun mkeq s t = Thm.apply @{cterm Trueprop} (Thm.apply (Thm.apply @{cterm "op = :: bool => _"} s) t) |
41453 | 571 |
fun mk_exists v th = Drule.arg_cong_rule (ext (ctyp_of_term v)) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
572 |
(Thm.abstract_rule (getname v) v th) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
573 |
fun simp_ex_conv ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
574 |
Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps @{thms simp_thms(39)}) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
575 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
576 |
fun frees t = Thm.add_cterm_frees t []; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
577 |
fun free_in v t = member op aconvc (frees t) v; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
578 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
579 |
val vsubst = let |
41453 | 580 |
fun vsubst (t,v) tm = |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
581 |
(Thm.rhs_of o Thm.beta_conversion false) (Thm.apply (Thm.lambda v tm) t) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
582 |
in fold vsubst end; |
23252 | 583 |
|
584 |
||
585 |
(** main **) |
|
586 |
||
587 |
fun ring_and_ideal_conv |
|
41453 | 588 |
{vars, semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), |
30866 | 589 |
field = (f_ops, f_rules), idom, ideal} |
23252 | 590 |
dest_const mk_const ring_eq_conv ring_normalize_conv = |
591 |
let |
|
592 |
val [add_pat, mul_pat, pow_pat, zero_tm, one_tm] = sr_ops; |
|
593 |
val [ring_add_tm, ring_mul_tm, ring_pow_tm] = |
|
41453 | 594 |
map Thm.dest_fun2 [add_pat, mul_pat, pow_pat]; |
23252 | 595 |
|
596 |
val (ring_sub_tm, ring_neg_tm) = |
|
597 |
(case r_ops of |
|
41453 | 598 |
[sub_pat, neg_pat] => (Thm.dest_fun2 sub_pat, Thm.dest_fun neg_pat) |
30866 | 599 |
|_ => (@{cterm "True"}, @{cterm "True"})); |
600 |
||
601 |
val (field_div_tm, field_inv_tm) = |
|
602 |
(case f_ops of |
|
41453 | 603 |
[div_pat, inv_pat] => (Thm.dest_fun2 div_pat, Thm.dest_fun inv_pat) |
30866 | 604 |
| _ => (@{cterm "True"}, @{cterm "True"})); |
23252 | 605 |
|
606 |
val [idom_thm, neq_thm] = idom; |
|
41453 | 607 |
val [idl_sub, idl_add0] = |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
608 |
if length ideal = 2 then ideal else [eq_commute, eq_commute] |
30866 | 609 |
fun ring_dest_neg t = |
41453 | 610 |
let val (l,r) = Thm.dest_comb t |
611 |
in if Term.could_unify(term_of l,term_of ring_neg_tm) then r |
|
30866 | 612 |
else raise CTERM ("ring_dest_neg", [t]) |
613 |
end |
|
23252 | 614 |
|
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
615 |
val ring_mk_neg = fn tm => Thm.apply (ring_neg_tm) (tm); |
30866 | 616 |
fun field_dest_inv t = |
41453 | 617 |
let val (l,r) = Thm.dest_comb t in |
618 |
if Term.could_unify(term_of l, term_of field_inv_tm) then r |
|
30866 | 619 |
else raise CTERM ("field_dest_inv", [t]) |
23252 | 620 |
end |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
621 |
val ring_dest_add = dest_binary ring_add_tm; |
23252 | 622 |
val ring_mk_add = mk_binop ring_add_tm; |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
623 |
val ring_dest_sub = dest_binary ring_sub_tm; |
23252 | 624 |
val ring_mk_sub = mk_binop ring_sub_tm; |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
625 |
val ring_dest_mul = dest_binary ring_mul_tm; |
23252 | 626 |
val ring_mk_mul = mk_binop ring_mul_tm; |
30866 | 627 |
val field_dest_div = dest_binary field_div_tm; |
628 |
val field_mk_div = mk_binop field_div_tm; |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
629 |
val ring_dest_pow = dest_binary ring_pow_tm; |
23252 | 630 |
val ring_mk_pow = mk_binop ring_pow_tm ; |
631 |
fun grobvars tm acc = |
|
632 |
if can dest_const tm then acc |
|
41453 | 633 |
else if can ring_dest_neg tm then grobvars (Thm.dest_arg tm) acc |
634 |
else if can ring_dest_pow tm then grobvars (Thm.dest_arg1 tm) acc |
|
23252 | 635 |
else if can ring_dest_add tm orelse can ring_dest_sub tm |
636 |
orelse can ring_dest_mul tm |
|
41453 | 637 |
then grobvars (Thm.dest_arg1 tm) (grobvars (Thm.dest_arg tm) acc) |
30866 | 638 |
else if can field_dest_inv tm |
639 |
then |
|
41453 | 640 |
let val gvs = grobvars (Thm.dest_arg tm) [] |
30866 | 641 |
in if null gvs then acc else tm::acc |
642 |
end |
|
643 |
else if can field_dest_div tm then |
|
41453 | 644 |
let val lvs = grobvars (Thm.dest_arg1 tm) acc |
645 |
val gvs = grobvars (Thm.dest_arg tm) [] |
|
30866 | 646 |
in if null gvs then lvs else tm::acc |
41453 | 647 |
end |
23252 | 648 |
else tm::acc ; |
649 |
||
650 |
fun grobify_term vars tm = |
|
651 |
((if not (member (op aconvc) vars tm) then raise CTERM ("Not a variable", [tm]) else |
|
652 |
[(rat_1,map (fn i => if i aconvc tm then 1 else 0) vars)]) |
|
653 |
handle CTERM _ => |
|
654 |
((let val x = dest_const tm |
|
655 |
in if x =/ rat_0 then [] else [(x,map (fn v => 0) vars)] |
|
656 |
end) |
|
657 |
handle ERROR _ => |
|
658 |
((grob_neg(grobify_term vars (ring_dest_neg tm))) |
|
659 |
handle CTERM _ => |
|
660 |
( |
|
30866 | 661 |
(grob_inv(grobify_term vars (field_dest_inv tm))) |
41453 | 662 |
handle CTERM _ => |
23252 | 663 |
((let val (l,r) = ring_dest_add tm |
664 |
in grob_add (grobify_term vars l) (grobify_term vars r) |
|
665 |
end) |
|
666 |
handle CTERM _ => |
|
667 |
((let val (l,r) = ring_dest_sub tm |
|
668 |
in grob_sub (grobify_term vars l) (grobify_term vars r) |
|
669 |
end) |
|
670 |
handle CTERM _ => |
|
671 |
((let val (l,r) = ring_dest_mul tm |
|
672 |
in grob_mul (grobify_term vars l) (grobify_term vars r) |
|
673 |
end) |
|
674 |
handle CTERM _ => |
|
30866 | 675 |
( (let val (l,r) = field_dest_div tm |
23252 | 676 |
in grob_div (grobify_term vars l) (grobify_term vars r) |
677 |
end) |
|
30866 | 678 |
handle CTERM _ => |
23252 | 679 |
((let val (l,r) = ring_dest_pow tm |
680 |
in grob_pow vars (grobify_term vars l) ((term_of #> HOLogic.dest_number #> snd) r) |
|
681 |
end) |
|
682 |
handle CTERM _ => error "grobify_term: unknown or invalid term"))))))))); |
|
41453 | 683 |
val eq_tm = idom_thm |> concl |> Thm.dest_arg |> Thm.dest_arg |> Thm.dest_fun2; |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
684 |
val dest_eq = dest_binary eq_tm; |
23252 | 685 |
|
686 |
fun grobify_equation vars tm = |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
687 |
let val (l,r) = dest_binary eq_tm tm |
23252 | 688 |
in grob_sub (grobify_term vars l) (grobify_term vars r) |
689 |
end; |
|
690 |
||
691 |
fun grobify_equations tm = |
|
692 |
let |
|
693 |
val cjs = conjs tm |
|
41453 | 694 |
val rawvars = |
695 |
fold_rev (fn eq => fn a => grobvars (Thm.dest_arg1 eq) (grobvars (Thm.dest_arg eq) a)) cjs [] |
|
696 |
val vars = sort (fn (x, y) => Term_Ord.term_ord (term_of x, term_of y)) |
|
23252 | 697 |
(distinct (op aconvc) rawvars) |
698 |
in (vars,map (grobify_equation vars) cjs) |
|
699 |
end; |
|
700 |
||
701 |
val holify_polynomial = |
|
702 |
let fun holify_varpow (v,n) = |
|
37388 | 703 |
if n = 1 then v else ring_mk_pow v (Numeral.mk_cnumber @{ctyp nat} n) (* FIXME *) |
23252 | 704 |
fun holify_monomial vars (c,m) = |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
705 |
let val xps = map holify_varpow (filter (fn (_,n) => n <> 0) (vars ~~ m)) |
23252 | 706 |
in end_itlist ring_mk_mul (mk_const c :: xps) |
707 |
end |
|
708 |
fun holify_polynomial vars p = |
|
23579 | 709 |
if null p then mk_const (rat_0) |
23252 | 710 |
else end_itlist ring_mk_add (map (holify_monomial vars) p) |
711 |
in holify_polynomial |
|
712 |
end ; |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
713 |
|
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
714 |
fun idom_rule ctxt = simplify (put_simpset HOL_basic_ss ctxt addsimps [idom_thm]); |
23252 | 715 |
fun prove_nz n = eqF_elim |
716 |
(ring_eq_conv(mk_binop eq_tm (mk_const n) (mk_const(rat_0)))); |
|
717 |
val neq_01 = prove_nz (rat_1); |
|
718 |
fun neq_rule n th = [prove_nz n, th] MRS neq_thm; |
|
41453 | 719 |
fun mk_add th1 = Thm.combination (Drule.arg_cong_rule ring_add_tm th1); |
23252 | 720 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
721 |
fun refute ctxt tm = |
23557 | 722 |
if tm aconvc false_tm then assume_Trueprop tm else |
29800
f73a68c9d810
Now catch ERROR exception thrown by find and friends
chaieb
parents:
29269
diff
changeset
|
723 |
((let |
23557 | 724 |
val (nths0,eths0) = List.partition (is_neg o concl) (HOLogic.conj_elims (assume_Trueprop tm)) |
41453 | 725 |
val nths = filter (is_eq o Thm.dest_arg o concl) nths0 |
23252 | 726 |
val eths = filter (is_eq o concl) eths0 |
727 |
in |
|
728 |
if null eths then |
|
729 |
let |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
730 |
val th1 = end_itlist (fn th1 => fn th2 => idom_rule ctxt (HOLogic.conj_intr th1 th2)) nths |
41453 | 731 |
val th2 = |
732 |
Conv.fconv_rule |
|
733 |
((Conv.arg_conv #> Conv.arg_conv) (Conv.binop_conv ring_normalize_conv)) th1 |
|
734 |
val conc = th2 |> concl |> Thm.dest_arg |
|
23252 | 735 |
val (l,r) = conc |> dest_eq |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
736 |
in Thm.implies_intr (Thm.apply cTrp tm) |
41453 | 737 |
(Thm.equal_elim (Drule.arg_cong_rule cTrp (eqF_intr th2)) |
738 |
(Thm.reflexive l |> mk_object_eq)) |
|
23252 | 739 |
end |
740 |
else |
|
741 |
let |
|
742 |
val (vars,l,cert,noteqth) =( |
|
743 |
if null nths then |
|
744 |
let val (vars,pols) = grobify_equations(list_mk_conj(map concl eths)) |
|
745 |
val (l,cert) = grobner_weak vars pols |
|
746 |
in (vars,l,cert,neq_01) |
|
747 |
end |
|
748 |
else |
|
749 |
let |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
750 |
val nth = end_itlist (fn th1 => fn th2 => idom_rule ctxt (HOLogic.conj_intr th1 th2)) nths |
23252 | 751 |
val (vars,pol::pols) = |
41453 | 752 |
grobify_equations(list_mk_conj(Thm.dest_arg(concl nth)::map concl eths)) |
23252 | 753 |
val (deg,l,cert) = grobner_strong vars pols pol |
41453 | 754 |
val th1 = |
755 |
Conv.fconv_rule ((Conv.arg_conv o Conv.arg_conv) (Conv.binop_conv ring_normalize_conv)) nth |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
756 |
val th2 = funpow deg (idom_rule ctxt o HOLogic.conj_intr th1) neq_01 |
23252 | 757 |
in (vars,l,cert,th2) |
758 |
end) |
|
759 |
val cert_pos = map (fn (i,p) => (i,filter (fn (c,m) => c >/ rat_0) p)) cert |
|
760 |
val cert_neg = map (fn (i,p) => (i,map (fn (c,m) => (minus_rat c,m)) |
|
761 |
(filter (fn (c,m) => c </ rat_0) p))) cert |
|
762 |
val herts_pos = map (fn (i,p) => (i,holify_polynomial vars p)) cert_pos |
|
763 |
val herts_neg = map (fn (i,p) => (i,holify_polynomial vars p)) cert_neg |
|
764 |
fun thm_fn pols = |
|
41453 | 765 |
if null pols then Thm.reflexive(mk_const rat_0) else |
23252 | 766 |
end_itlist mk_add |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
767 |
(map (fn (i,p) => Drule.arg_cong_rule (Thm.apply ring_mul_tm p) |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23579
diff
changeset
|
768 |
(nth eths i |> mk_meta_eq)) pols) |
23252 | 769 |
val th1 = thm_fn herts_pos |
770 |
val th2 = thm_fn herts_neg |
|
41453 | 771 |
val th3 = HOLogic.conj_intr(mk_add (Thm.symmetric th1) th2 |> mk_object_eq) noteqth |
772 |
val th4 = |
|
773 |
Conv.fconv_rule ((Conv.arg_conv o Conv.arg_conv o Conv.binop_conv) ring_normalize_conv) |
|
774 |
(neq_rule l th3) |
|
775 |
val (l,r) = dest_eq(Thm.dest_arg(concl th4)) |
|
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
776 |
in Thm.implies_intr (Thm.apply cTrp tm) |
41453 | 777 |
(Thm.equal_elim (Drule.arg_cong_rule cTrp (eqF_intr th4)) |
778 |
(Thm.reflexive l |> mk_object_eq)) |
|
23252 | 779 |
end |
41453 | 780 |
end) handle ERROR _ => raise CTERM ("Groebner-refute: unable to refute",[tm])) |
23252 | 781 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
782 |
fun ring ctxt tm = |
23252 | 783 |
let |
784 |
fun mk_forall x p = |
|
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
785 |
Thm.apply |
41453 | 786 |
(Drule.cterm_rule (instantiate' [SOME (ctyp_of_term x)] []) |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
787 |
@{cpat "All:: (?'a => bool) => _"}) (Thm.lambda x p) |
41453 | 788 |
val avs = Thm.add_cterm_frees tm [] |
23252 | 789 |
val P' = fold mk_forall avs tm |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
790 |
val th1 = initial_conv ctxt (mk_neg P') |
23252 | 791 |
val (evs,bod) = strip_exists(concl th1) in |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
792 |
if is_forall bod then raise CTERM("ring: non-universal formula",[tm]) |
23252 | 793 |
else |
794 |
let |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
795 |
val th1a = weak_dnf_conv ctxt bod |
23252 | 796 |
val boda = concl th1a |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
797 |
val th2a = refute_disj (refute ctxt) boda |
23252 | 798 |
val th2b = [mk_object_eq th1a, (th2a COMP notI) COMP PFalse'] MRS trans |
41453 | 799 |
val th2 = fold (fn v => fn th => (Thm.forall_intr v th) COMP allI) evs (th2b RS PFalse) |
800 |
val th3 = |
|
801 |
Thm.equal_elim |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
802 |
(Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps [not_ex RS sym]) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
803 |
(th2 |> cprop_of)) th2 |
23252 | 804 |
in specl avs |
805 |
([[[mk_object_eq th1, th3 RS PFalse'] MRS trans] MRS PFalse] MRS notnotD) |
|
806 |
end |
|
807 |
end |
|
808 |
fun ideal tms tm ord = |
|
809 |
let |
|
810 |
val rawvars = fold_rev grobvars (tm::tms) [] |
|
811 |
val vars = sort ord (distinct (fn (x,y) => (term_of x) aconv (term_of y)) rawvars) |
|
812 |
val pols = map (grobify_term vars) tms |
|
813 |
val pol = grobify_term vars tm |
|
814 |
val cert = grobner_ideal vars pols pol |
|
33063 | 815 |
in map_range (fn n => these (AList.lookup (op =) cert n) |> holify_polynomial vars) |
816 |
(length pols) |
|
23252 | 817 |
end |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
818 |
|
41453 | 819 |
fun poly_eq_conv t = |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
820 |
let val (a,b) = Thm.dest_binop t |
41453 | 821 |
in Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv ring_normalize_conv)) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
822 |
(instantiate' [] [SOME a, SOME b] idl_sub) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
823 |
end |
41453 | 824 |
val poly_eq_simproc = |
825 |
let |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
826 |
fun proc phi ctxt t = |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
827 |
let val th = poly_eq_conv t |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
828 |
in if Thm.is_reflexive th then NONE else SOME th |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
829 |
end |
41453 | 830 |
in make_simproc {lhss = [Thm.lhs_of idl_sub], |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
831 |
name = "poly_eq_simproc", proc = proc, identifier = []} |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
832 |
end; |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
833 |
val poly_eq_ss = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
834 |
simpset_of (put_simpset HOL_basic_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
835 |
addsimps @{thms simp_thms} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
836 |
addsimprocs [poly_eq_simproc]) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
837 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
838 |
local |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
839 |
fun is_defined v t = |
41453 | 840 |
let |
841 |
val mons = striplist(dest_binary ring_add_tm) t |
|
842 |
in member (op aconvc) mons v andalso |
|
843 |
forall (fn m => v aconvc m |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
844 |
orelse not(member (op aconvc) (Thm.add_cterm_frees m []) v)) mons |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
845 |
end |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
846 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
847 |
fun isolate_variable vars tm = |
41453 | 848 |
let |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
849 |
val th = poly_eq_conv tm |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
850 |
val th' = (sym_conv then_conv poly_eq_conv) tm |
41453 | 851 |
val (v,th1) = |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
852 |
case find_first(fn v=> is_defined v (Thm.dest_arg1 (Thm.rhs_of th))) vars of |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
853 |
SOME v => (v,th') |
41453 | 854 |
| NONE => (the (find_first |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
855 |
(fn v => is_defined v (Thm.dest_arg1 (Thm.rhs_of th'))) vars) ,th) |
41453 | 856 |
val th2 = Thm.transitive th1 |
857 |
(instantiate' [] [(SOME o Thm.dest_arg1 o Thm.rhs_of) th1, SOME v] |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
858 |
idl_add0) |
41453 | 859 |
in Conv.fconv_rule(funpow 2 Conv.arg_conv ring_normalize_conv) th2 |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
860 |
end |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
861 |
in |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
862 |
fun unwind_polys_conv ctxt tm = |
41453 | 863 |
let |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
864 |
val (vars,bod) = strip_exists tm |
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset
|
865 |
val cjs = striplist (dest_binary @{cterm HOL.conj}) bod |
41453 | 866 |
val th1 = (the (get_first (try (isolate_variable vars)) cjs) |
51930
52fd62618631
prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents:
51717
diff
changeset
|
867 |
handle Option.Option => raise CTERM ("unwind_polys_conv",[tm])) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
868 |
val eq = Thm.lhs_of th1 |
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset
|
869 |
val bod' = list_mk_binop @{cterm HOL.conj} (eq::(remove op aconvc eq cjs)) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
870 |
val th2 = conj_ac_rule (mk_eq bod bod') |
41453 | 871 |
val th3 = |
872 |
Thm.transitive th2 |
|
873 |
(Drule.binop_cong_rule @{cterm HOL.conj} th1 |
|
874 |
(Thm.reflexive (Thm.dest_arg (Thm.rhs_of th2)))) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
875 |
val v = Thm.dest_arg1(Thm.dest_arg1(Thm.rhs_of th3)) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
876 |
val vars' = (remove op aconvc v vars) @ [v] |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
877 |
val th4 = Conv.fconv_rule (Conv.arg_conv (simp_ex_conv ctxt)) (mk_exists v th3) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
878 |
val th5 = ex_eq_conv (mk_eq tm (fold mk_ex (remove op aconvc v vars) (Thm.lhs_of th4))) |
41453 | 879 |
in Thm.transitive th5 (fold mk_exists (remove op aconvc v vars) th4) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
880 |
end; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
881 |
end |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
882 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
883 |
local |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
884 |
fun scrub_var v m = |
41453 | 885 |
let |
886 |
val ps = striplist ring_dest_mul m |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
887 |
val ps' = remove op aconvc v ps |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
888 |
in if null ps' then one_tm else fold1 ring_mk_mul ps' |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
889 |
end |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
890 |
fun find_multipliers v mons = |
41453 | 891 |
let |
892 |
val mons1 = filter (fn m => free_in v m) mons |
|
893 |
val mons2 = map (scrub_var v) mons1 |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
894 |
in if null mons2 then zero_tm else fold1 ring_mk_add mons2 |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
895 |
end |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
896 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
897 |
fun isolate_monomials vars tm = |
41453 | 898 |
let |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
899 |
val (cmons,vmons) = |
33049
c38f02fdf35d
curried inter as canonical list operation (beware of argument order)
haftmann
parents:
33039
diff
changeset
|
900 |
List.partition (fn m => null (inter (op aconvc) vars (frees m))) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
901 |
(striplist ring_dest_add tm) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
902 |
val cofactors = map (fn v => find_multipliers v vmons) vars |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
903 |
val cnc = if null cmons then zero_tm |
46497
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents:
42793
diff
changeset
|
904 |
else Thm.apply ring_neg_tm |
41453 | 905 |
(list_mk_binop ring_add_tm cmons) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
906 |
in (cofactors,cnc) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
907 |
end; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
908 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
909 |
fun isolate_variables evs ps eq = |
41453 | 910 |
let |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
911 |
val vars = filter (fn v => free_in v eq) evs |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
912 |
val (qs,p) = isolate_monomials vars eq |
41453 | 913 |
val rs = ideal (qs @ ps) p |
35408 | 914 |
(fn (s,t) => Term_Ord.term_ord (term_of s, term_of t)) |
33957 | 915 |
in (eq, take (length qs) rs ~~ vars) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
916 |
end; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
917 |
fun subst_in_poly i p = Thm.rhs_of (ring_normalize_conv (vsubst i p)); |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
918 |
in |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
919 |
fun solve_idealism evs ps eqs = |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
920 |
if null evs then [] else |
41453 | 921 |
let |
33035 | 922 |
val (eq,cfs) = get_first (try (isolate_variables evs ps)) eqs |> the |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
923 |
val evs' = subtract op aconvc evs (map snd cfs) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
924 |
val eqs' = map (subst_in_poly cfs) (remove op aconvc eq eqs) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
925 |
in cfs @ solve_idealism evs' ps eqs' |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
926 |
end; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
927 |
end; |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
928 |
|
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
929 |
|
41453 | 930 |
in {ring_conv = ring, simple_ideal = ideal, multi_ideal = solve_idealism, |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
931 |
poly_eq_ss = poly_eq_ss, unwind_conv = unwind_polys_conv} |
23252 | 932 |
end; |
933 |
||
934 |
||
935 |
fun find_term bounds tm = |
|
936 |
(case term_of tm of |
|
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
38795
diff
changeset
|
937 |
Const (@{const_name HOL.eq}, T) $ _ $ _ => |
23252 | 938 |
if domain_type T = HOLogic.boolT then find_args bounds tm |
41453 | 939 |
else Thm.dest_arg tm |
940 |
| Const (@{const_name Not}, _) $ _ => find_term bounds (Thm.dest_arg tm) |
|
941 |
| Const (@{const_name All}, _) $ _ => find_body bounds (Thm.dest_arg tm) |
|
942 |
| Const (@{const_name Ex}, _) $ _ => find_body bounds (Thm.dest_arg tm) |
|
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset
|
943 |
| Const (@{const_name HOL.conj}, _) $ _ $ _ => find_args bounds tm |
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset
|
944 |
| Const (@{const_name HOL.disj}, _) $ _ $ _ => find_args bounds tm |
38786
e46e7a9cb622
formerly unnamed infix impliciation now named HOL.implies
haftmann
parents:
38558
diff
changeset
|
945 |
| Const (@{const_name HOL.implies}, _) $ _ $ _ => find_args bounds tm |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
946 |
| @{term "op ==>"} $_$_ => find_args bounds tm |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
947 |
| Const("op ==",_)$_$_ => find_args bounds tm |
41453 | 948 |
| @{term Trueprop}$_ => find_term bounds (Thm.dest_arg tm) |
23252 | 949 |
| _ => raise TERM ("find_term", [])) |
950 |
and find_args bounds tm = |
|
951 |
let val (t, u) = Thm.dest_binop tm |
|
952 |
in (find_term bounds t handle TERM _ => find_term bounds u) end |
|
953 |
and find_body bounds b = |
|
41453 | 954 |
let val (_, b') = Thm.dest_abs (SOME (Name.bound bounds)) b |
23252 | 955 |
in find_term (bounds + 1) b' end; |
956 |
||
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
957 |
|
41453 | 958 |
fun get_ring_ideal_convs ctxt form = |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
959 |
case try (find_term 0) form of |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
960 |
NONE => NONE |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
961 |
| SOME tm => |
36752
cf558aeb35b0
delete Groebner_Basis directory -- only one file left
haftmann
parents:
36723
diff
changeset
|
962 |
(case Semiring_Normalizer.match ctxt tm of |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
963 |
NONE => NONE |
41453 | 964 |
| SOME (res as (theory, {is_const, dest_const, |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
965 |
mk_const, conv = ring_eq_conv})) => |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
966 |
SOME (ring_and_ideal_conv theory |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
967 |
dest_const (mk_const (ctyp_of_term tm)) (ring_eq_conv ctxt) |
36752
cf558aeb35b0
delete Groebner_Basis directory -- only one file left
haftmann
parents:
36723
diff
changeset
|
968 |
(Semiring_Normalizer.semiring_normalize_wrapper ctxt res))) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
969 |
|
23579 | 970 |
fun ring_solve ctxt form = |
23252 | 971 |
(case try (find_term 0 (* FIXME !? *)) form of |
41453 | 972 |
NONE => Thm.reflexive form |
23252 | 973 |
| SOME tm => |
36752
cf558aeb35b0
delete Groebner_Basis directory -- only one file left
haftmann
parents:
36723
diff
changeset
|
974 |
(case Semiring_Normalizer.match ctxt tm of |
41453 | 975 |
NONE => Thm.reflexive form |
23252 | 976 |
| SOME (res as (theory, {is_const, dest_const, mk_const, conv = ring_eq_conv})) => |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
977 |
#ring_conv (ring_and_ideal_conv theory |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
978 |
dest_const (mk_const (ctyp_of_term tm)) (ring_eq_conv ctxt) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
979 |
(Semiring_Normalizer.semiring_normalize_wrapper ctxt res)) ctxt form)); |
23252 | 980 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
981 |
fun presimplify ctxt add_thms del_thms = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
982 |
asm_full_simp_tac (put_simpset HOL_basic_ss ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
983 |
addsimps (Algebra_Simplification.get ctxt) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
984 |
delsimps del_thms addsimps add_thms); |
36702
b455ebd63799
moved presimplification rules for algebraic methods into named thms functor
haftmann
parents:
36700
diff
changeset
|
985 |
|
23579 | 986 |
fun ring_tac add_ths del_ths ctxt = |
35625 | 987 |
Object_Logic.full_atomize_tac |
36702
b455ebd63799
moved presimplification rules for algebraic methods into named thms functor
haftmann
parents:
36700
diff
changeset
|
988 |
THEN' presimplify ctxt add_ths del_ths |
23579 | 989 |
THEN' CSUBGOAL (fn (p, i) => |
35625 | 990 |
rtac (let val form = Object_Logic.dest_judgment p |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
991 |
in case get_ring_ideal_convs ctxt form of |
41453 | 992 |
NONE => Thm.reflexive form |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
993 |
| SOME thy => #ring_conv thy ctxt form |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
994 |
end) i |
23579 | 995 |
handle TERM _ => no_tac |
996 |
| CTERM _ => no_tac |
|
997 |
| THM _ => no_tac); |
|
23334 | 998 |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
999 |
local |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1000 |
fun lhs t = case term_of t of |
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
38795
diff
changeset
|
1001 |
Const(@{const_name HOL.eq},_)$_$_ => Thm.dest_arg1 t |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1002 |
| _=> raise CTERM ("ideal_tac - lhs",[t]) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1003 |
fun exitac NONE = no_tac |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1004 |
| exitac (SOME y) = rtac (instantiate' [SOME (ctyp_of_term y)] [NONE,SOME y] exI) 1 |
52086 | 1005 |
|
1006 |
val claset = claset_of @{context} |
|
41453 | 1007 |
in |
1008 |
fun ideal_tac add_ths del_ths ctxt = |
|
36702
b455ebd63799
moved presimplification rules for algebraic methods into named thms functor
haftmann
parents:
36700
diff
changeset
|
1009 |
presimplify ctxt add_ths del_ths |
27671 | 1010 |
THEN' |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1011 |
CSUBGOAL (fn (p, i) => |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1012 |
case get_ring_ideal_convs ctxt p of |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1013 |
NONE => no_tac |
41453 | 1014 |
| SOME thy => |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1015 |
let |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1016 |
fun poly_exists_tac {asms = asms, concl = concl, prems = prems, |
41453 | 1017 |
params = params, context = ctxt, schematics = scs} = |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1018 |
let |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1019 |
val (evs,bod) = strip_exists (Thm.dest_arg concl) |
41453 | 1020 |
val ps = map_filter (try (lhs o Thm.dest_arg)) asms |
1021 |
val cfs = (map swap o #multi_ideal thy evs ps) |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1022 |
(map Thm.dest_arg1 (conjuncts bod)) |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1023 |
val ws = map (exitac o AList.lookup op aconvc cfs) evs |
41453 | 1024 |
in EVERY (rev ws) THEN Method.insert_tac prems 1 |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1025 |
THEN ring_tac add_ths del_ths ctxt 1 |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1026 |
end |
41453 | 1027 |
in |
52086 | 1028 |
clarify_tac (put_claset claset ctxt) i |
41453 | 1029 |
THEN Object_Logic.full_atomize_tac i |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
1030 |
THEN asm_full_simp_tac (put_simpset (#poly_eq_ss thy) ctxt) i |
52086 | 1031 |
THEN clarify_tac (put_claset claset ctxt) i |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47432
diff
changeset
|
1032 |
THEN (REPEAT (CONVERSION (#unwind_conv thy ctxt) i)) |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1033 |
THEN SUBPROOF poly_exists_tac ctxt i |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1034 |
end |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1035 |
handle TERM _ => no_tac |
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1036 |
| CTERM _ => no_tac |
41453 | 1037 |
| THM _ => no_tac); |
23252 | 1038 |
end; |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1039 |
|
41453 | 1040 |
fun algebra_tac add_ths del_ths ctxt i = |
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1041 |
ring_tac add_ths del_ths ctxt i ORELSE ideal_tac add_ths del_ths ctxt i |
41453 | 1042 |
|
25251
759bffe1d416
(1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents:
24913
diff
changeset
|
1043 |
end; |