| author | haftmann | 
| Fri, 13 Mar 2009 19:17:57 +0100 | |
| changeset 30518 | 07b45c1aa788 | 
| parent 30496 | 7cdcc9dd95cb | 
| child 30685 | dd5fe091ff04 | 
| permissions | -rw-r--r-- | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
1  | 
(* Authors: Larry Paulson and Tobias Nipkow  | 
| 23164 | 2  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
3  | 
Simprocs and decision procedure for numerals and linear arithmetic.  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
4  | 
*)  | 
| 23164 | 5  | 
|
6  | 
structure Int_Numeral_Simprocs =  | 
|
7  | 
struct  | 
|
8  | 
||
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
9  | 
(*reorientation simprules using ==, for the following simproc*)  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
10  | 
val meta_zero_reorient = @{thm zero_reorient} RS eq_reflection
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
11  | 
val meta_one_reorient = @{thm one_reorient} RS eq_reflection
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
12  | 
val meta_number_of_reorient = @{thm number_of_reorient} RS eq_reflection
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
13  | 
|
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
14  | 
(*reorientation simplification procedure: reorients (polymorphic)  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
15  | 
0 = x, 1 = x, nnn = x provided x isn't 0, 1 or a Int.*)  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
16  | 
fun reorient_proc sg _ (_ $ t $ u) =  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
17  | 
case u of  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
18  | 
      Const(@{const_name HOL.zero}, _) => NONE
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
19  | 
    | Const(@{const_name HOL.one}, _) => NONE
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
20  | 
    | Const(@{const_name Int.number_of}, _) $ _ => NONE
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
21  | 
| _ => SOME (case t of  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
22  | 
        Const(@{const_name HOL.zero}, _) => meta_zero_reorient
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
23  | 
      | Const(@{const_name HOL.one}, _) => meta_one_reorient
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
24  | 
      | Const(@{const_name Int.number_of}, _) $ _ => meta_number_of_reorient)
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
25  | 
|
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
26  | 
val reorient_simproc =  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
27  | 
  Arith_Data.prep_simproc ("reorient_simproc", ["0=x", "1=x", "number_of w = x"], reorient_proc);
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
28  | 
|
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
29  | 
(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic isn't complicated by the abstract 0 and 1.*)  | 
| 25481 | 30  | 
val numeral_syms = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym];
 | 
| 23164 | 31  | 
|
32  | 
||
33  | 
(** Utilities **)  | 
|
34  | 
||
35  | 
fun mk_number T n = HOLogic.number_of_const T $ HOLogic.mk_numeral n;  | 
|
36  | 
||
37  | 
fun find_first_numeral past (t::terms) =  | 
|
38  | 
((snd (HOLogic.dest_number t), rev past @ terms)  | 
|
39  | 
handle TERM _ => find_first_numeral (t::past) terms)  | 
|
40  | 
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
 | 
|
41  | 
||
42  | 
val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
 | 
|
43  | 
||
44  | 
fun mk_minus t =  | 
|
45  | 
let val T = Term.fastype_of t  | 
|
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
46  | 
  in Const (@{const_name HOL.uminus}, T --> T) $ t end;
 | 
| 23164 | 47  | 
|
48  | 
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)  | 
|
49  | 
fun mk_sum T [] = mk_number T 0  | 
|
50  | 
| mk_sum T [t,u] = mk_plus (t, u)  | 
|
51  | 
| mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);  | 
|
52  | 
||
53  | 
(*this version ALWAYS includes a trailing zero*)  | 
|
54  | 
fun long_mk_sum T [] = mk_number T 0  | 
|
55  | 
| long_mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);  | 
|
56  | 
||
57  | 
val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} Term.dummyT;
 | 
|
58  | 
||
59  | 
(*decompose additions AND subtractions as a sum*)  | 
|
60  | 
fun dest_summing (pos, Const (@{const_name HOL.plus}, _) $ t $ u, ts) =
 | 
|
61  | 
dest_summing (pos, t, dest_summing (pos, u, ts))  | 
|
62  | 
  | dest_summing (pos, Const (@{const_name HOL.minus}, _) $ t $ u, ts) =
 | 
|
63  | 
dest_summing (pos, t, dest_summing (not pos, u, ts))  | 
|
64  | 
| dest_summing (pos, t, ts) =  | 
|
65  | 
if pos then t::ts else mk_minus t :: ts;  | 
|
66  | 
||
67  | 
fun dest_sum t = dest_summing (true, t, []);  | 
|
68  | 
||
69  | 
val mk_diff = HOLogic.mk_binop @{const_name HOL.minus};
 | 
|
70  | 
val dest_diff = HOLogic.dest_bin @{const_name HOL.minus} Term.dummyT;
 | 
|
71  | 
||
72  | 
val mk_times = HOLogic.mk_binop @{const_name HOL.times};
 | 
|
73  | 
||
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
74  | 
fun one_of T = Const(@{const_name HOL.one},T);
 | 
| 
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
75  | 
|
| 
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
76  | 
(* build product with trailing 1 rather than Numeral 1 in order to avoid the  | 
| 
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
77  | 
unnecessary restriction to type class number_ring  | 
| 
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
78  | 
which is not required for cancellation of common factors in divisions.  | 
| 
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
79  | 
*)  | 
| 23164 | 80  | 
fun mk_prod T =  | 
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
81  | 
let val one = one_of T  | 
| 23164 | 82  | 
fun mk [] = one  | 
83  | 
| mk [t] = t  | 
|
84  | 
| mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)  | 
|
85  | 
in mk end;  | 
|
86  | 
||
87  | 
(*This version ALWAYS includes a trailing one*)  | 
|
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
88  | 
fun long_mk_prod T [] = one_of T  | 
| 23164 | 89  | 
| long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);  | 
90  | 
||
91  | 
val dest_times = HOLogic.dest_bin @{const_name HOL.times} Term.dummyT;
 | 
|
92  | 
||
93  | 
fun dest_prod t =  | 
|
94  | 
let val (t,u) = dest_times t  | 
|
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
95  | 
in dest_prod t @ dest_prod u end  | 
| 23164 | 96  | 
handle TERM _ => [t];  | 
97  | 
||
98  | 
(*DON'T do the obvious simplifications; that would create special cases*)  | 
|
99  | 
fun mk_coeff (k, t) = mk_times (mk_number (Term.fastype_of t) k, t);  | 
|
100  | 
||
101  | 
(*Express t as a product of (possibly) a numeral with other sorted terms*)  | 
|
102  | 
fun dest_coeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_coeff (~sign) t
 | 
|
103  | 
| dest_coeff sign t =  | 
|
| 
29269
 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 
wenzelm 
parents: 
28952 
diff
changeset
 | 
104  | 
let val ts = sort TermOrd.term_ord (dest_prod t)  | 
| 23164 | 105  | 
val (n, ts') = find_first_numeral [] ts  | 
106  | 
handle TERM _ => (1, ts)  | 
|
107  | 
in (sign*n, mk_prod (Term.fastype_of t) ts') end;  | 
|
108  | 
||
109  | 
(*Find first coefficient-term THAT MATCHES u*)  | 
|
110  | 
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
 | 
|
111  | 
| find_first_coeff past u (t::terms) =  | 
|
112  | 
let val (n,u') = dest_coeff 1 t  | 
|
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
113  | 
in if u aconv u' then (n, rev past @ terms)  | 
| 
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
114  | 
else find_first_coeff (t::past) u terms  | 
| 23164 | 115  | 
end  | 
116  | 
handle TERM _ => find_first_coeff (t::past) u terms;  | 
|
117  | 
||
118  | 
(*Fractions as pairs of ints. Can't use Rat.rat because the representation  | 
|
119  | 
needs to preserve negative values in the denominator.*)  | 
|
| 
24630
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24266 
diff
changeset
 | 
120  | 
fun mk_frac (p, q) = if q = 0 then raise Div else (p, q);  | 
| 23164 | 121  | 
|
122  | 
(*Don't reduce fractions; sums must be proved by rule add_frac_eq.  | 
|
123  | 
Fractions are reduced later by the cancel_numeral_factor simproc.*)  | 
|
| 
24630
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24266 
diff
changeset
 | 
124  | 
fun add_frac ((p1, q1), (p2, q2)) = (p1 * q2 + p2 * q1, q1 * q2);  | 
| 23164 | 125  | 
|
126  | 
val mk_divide = HOLogic.mk_binop @{const_name HOL.divide};
 | 
|
127  | 
||
128  | 
(*Build term (p / q) * t*)  | 
|
129  | 
fun mk_fcoeff ((p, q), t) =  | 
|
130  | 
let val T = Term.fastype_of t  | 
|
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
131  | 
in mk_times (mk_divide (mk_number T p, mk_number T q), t) end;  | 
| 23164 | 132  | 
|
133  | 
(*Express t as a product of a fraction with other sorted terms*)  | 
|
134  | 
fun dest_fcoeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_fcoeff (~sign) t
 | 
|
135  | 
  | dest_fcoeff sign (Const (@{const_name HOL.divide}, _) $ t $ u) =
 | 
|
136  | 
let val (p, t') = dest_coeff sign t  | 
|
137  | 
val (q, u') = dest_coeff 1 u  | 
|
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
138  | 
in (mk_frac (p, q), mk_divide (t', u')) end  | 
| 23164 | 139  | 
| dest_fcoeff sign t =  | 
140  | 
let val (p, t') = dest_coeff sign t  | 
|
141  | 
val T = Term.fastype_of t  | 
|
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
142  | 
in (mk_frac (p, 1), mk_divide (t', one_of T)) end;  | 
| 23164 | 143  | 
|
144  | 
||
| 30518 | 145  | 
(** New term ordering so that AC-rewriting brings numerals to the front **)  | 
146  | 
||
147  | 
(*Order integers by absolute value and then by sign. The standard integer  | 
|
148  | 
ordering is not well-founded.*)  | 
|
149  | 
fun num_ord (i,j) =  | 
|
150  | 
(case int_ord (abs i, abs j) of  | 
|
151  | 
EQUAL => int_ord (Int.sign i, Int.sign j)  | 
|
152  | 
| ord => ord);  | 
|
153  | 
||
154  | 
(*This resembles TermOrd.term_ord, but it puts binary numerals before other  | 
|
155  | 
non-atomic terms.*)  | 
|
156  | 
local open Term  | 
|
157  | 
in  | 
|
158  | 
fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =  | 
|
159  | 
(case numterm_ord (t, u) of EQUAL => TermOrd.typ_ord (T, U) | ord => ord)  | 
|
160  | 
| numterm_ord  | 
|
161  | 
     (Const(@{const_name Int.number_of}, _) $ v, Const(@{const_name Int.number_of}, _) $ w) =
 | 
|
162  | 
num_ord (HOLogic.dest_numeral v, HOLogic.dest_numeral w)  | 
|
163  | 
  | numterm_ord (Const(@{const_name Int.number_of}, _) $ _, _) = LESS
 | 
|
164  | 
  | numterm_ord (_, Const(@{const_name Int.number_of}, _) $ _) = GREATER
 | 
|
165  | 
| numterm_ord (t, u) =  | 
|
166  | 
(case int_ord (size_of_term t, size_of_term u) of  | 
|
167  | 
EQUAL =>  | 
|
168  | 
let val (f, ts) = strip_comb t and (g, us) = strip_comb u in  | 
|
169  | 
(case TermOrd.hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)  | 
|
170  | 
end  | 
|
171  | 
| ord => ord)  | 
|
172  | 
and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)  | 
|
173  | 
end;  | 
|
174  | 
||
175  | 
fun numtermless tu = (numterm_ord tu = LESS);  | 
|
176  | 
||
177  | 
val num_ss = HOL_ss settermless numtermless;  | 
|
178  | 
||
179  | 
||
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
180  | 
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)  | 
| 23164 | 181  | 
val add_0s = thms "add_0s";  | 
| 
23400
 
a64b39e5809b
The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
 
nipkow 
parents: 
23365 
diff
changeset
 | 
182  | 
val mult_1s = thms "mult_1s" @ [thm"mult_1_left", thm"mult_1_right", thm"divide_1"];  | 
| 23164 | 183  | 
|
184  | 
(*Simplify inverse Numeral1, a/Numeral1*)  | 
|
185  | 
val inverse_1s = [@{thm inverse_numeral_1}];
 | 
|
186  | 
val divide_1s = [@{thm divide_numeral_1}];
 | 
|
187  | 
||
188  | 
(*To perform binary arithmetic. The "left" rewriting handles patterns  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
189  | 
created by the Int_Numeral_Simprocs, such as 3 * (5 * x). *)  | 
| 25481 | 190  | 
val simps = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym,
 | 
191  | 
                 @{thm add_number_of_left}, @{thm mult_number_of_left}] @
 | 
|
192  | 
                @{thms arith_simps} @ @{thms rel_simps};
 | 
|
| 23164 | 193  | 
|
194  | 
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms  | 
|
195  | 
during re-arrangement*)  | 
|
196  | 
val non_add_simps =  | 
|
| 25481 | 197  | 
  subtract Thm.eq_thm [@{thm add_number_of_left}, @{thm number_of_add} RS sym] simps;
 | 
| 23164 | 198  | 
|
199  | 
(*To evaluate binary negations of coefficients*)  | 
|
| 
26075
 
815f3ccc0b45
added lemma lists {normalize,succ,pred,minus,add,mult}_bin_simps
 
huffman 
parents: 
25919 
diff
changeset
 | 
200  | 
val minus_simps = [@{thm numeral_m1_eq_minus_1} RS sym, @{thm number_of_minus} RS sym] @
 | 
| 
 
815f3ccc0b45
added lemma lists {normalize,succ,pred,minus,add,mult}_bin_simps
 
huffman 
parents: 
25919 
diff
changeset
 | 
201  | 
                   @{thms minus_bin_simps} @ @{thms pred_bin_simps};
 | 
| 23164 | 202  | 
|
203  | 
(*To let us treat subtraction as addition*)  | 
|
204  | 
val diff_simps = [@{thm diff_minus}, @{thm minus_add_distrib}, @{thm minus_minus}];
 | 
|
205  | 
||
206  | 
(*To let us treat division as multiplication*)  | 
|
207  | 
val divide_simps = [@{thm divide_inverse}, @{thm inverse_mult_distrib}, @{thm inverse_inverse_eq}];
 | 
|
208  | 
||
209  | 
(*push the unary minus down: - x * y = x * - y *)  | 
|
210  | 
val minus_mult_eq_1_to_2 =  | 
|
211  | 
    [@{thm minus_mult_left} RS sym, @{thm minus_mult_right}] MRS trans |> standard;
 | 
|
212  | 
||
213  | 
(*to extract again any uncancelled minuses*)  | 
|
214  | 
val minus_from_mult_simps =  | 
|
215  | 
    [@{thm minus_minus}, @{thm minus_mult_left} RS sym, @{thm minus_mult_right} RS sym];
 | 
|
216  | 
||
217  | 
(*combine unary minus with numeric literals, however nested within a product*)  | 
|
218  | 
val mult_minus_simps =  | 
|
219  | 
    [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
 | 
|
220  | 
||
221  | 
structure CancelNumeralsCommon =  | 
|
222  | 
struct  | 
|
223  | 
val mk_sum = mk_sum  | 
|
224  | 
val dest_sum = dest_sum  | 
|
225  | 
val mk_coeff = mk_coeff  | 
|
226  | 
val dest_coeff = dest_coeff 1  | 
|
227  | 
val find_first_coeff = find_first_coeff []  | 
|
| 30518 | 228  | 
val trans_tac = K Arith_Data.trans_tac  | 
| 23164 | 229  | 
|
230  | 
val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @  | 
|
| 23881 | 231  | 
    diff_simps @ minus_simps @ @{thms add_ac}
 | 
| 23164 | 232  | 
val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps  | 
| 23881 | 233  | 
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
 | 
| 23164 | 234  | 
fun norm_tac ss =  | 
235  | 
ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))  | 
|
236  | 
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))  | 
|
237  | 
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))  | 
|
238  | 
||
239  | 
val numeral_simp_ss = HOL_ss addsimps add_0s @ simps  | 
|
240  | 
fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))  | 
|
| 30518 | 241  | 
val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s)  | 
| 23164 | 242  | 
end;  | 
243  | 
||
244  | 
||
245  | 
structure EqCancelNumerals = CancelNumeralsFun  | 
|
246  | 
(open CancelNumeralsCommon  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
247  | 
val prove_conv = Arith_Data.prove_conv  | 
| 23164 | 248  | 
val mk_bal = HOLogic.mk_eq  | 
249  | 
val dest_bal = HOLogic.dest_bin "op =" Term.dummyT  | 
|
| 25481 | 250  | 
  val bal_add1 = @{thm eq_add_iff1} RS trans
 | 
251  | 
  val bal_add2 = @{thm eq_add_iff2} RS trans
 | 
|
| 23164 | 252  | 
);  | 
253  | 
||
254  | 
structure LessCancelNumerals = CancelNumeralsFun  | 
|
255  | 
(open CancelNumeralsCommon  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
256  | 
val prove_conv = Arith_Data.prove_conv  | 
| 23881 | 257  | 
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less}
 | 
258  | 
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less} Term.dummyT
 | 
|
| 25481 | 259  | 
  val bal_add1 = @{thm less_add_iff1} RS trans
 | 
260  | 
  val bal_add2 = @{thm less_add_iff2} RS trans
 | 
|
| 23164 | 261  | 
);  | 
262  | 
||
263  | 
structure LeCancelNumerals = CancelNumeralsFun  | 
|
264  | 
(open CancelNumeralsCommon  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
265  | 
val prove_conv = Arith_Data.prove_conv  | 
| 23881 | 266  | 
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less_eq}
 | 
267  | 
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} Term.dummyT
 | 
|
| 25481 | 268  | 
  val bal_add1 = @{thm le_add_iff1} RS trans
 | 
269  | 
  val bal_add2 = @{thm le_add_iff2} RS trans
 | 
|
| 23164 | 270  | 
);  | 
271  | 
||
272  | 
val cancel_numerals =  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
273  | 
map Arith_Data.prep_simproc  | 
| 23164 | 274  | 
   [("inteq_cancel_numerals",
 | 
275  | 
["(l::'a::number_ring) + m = n",  | 
|
276  | 
"(l::'a::number_ring) = m + n",  | 
|
277  | 
"(l::'a::number_ring) - m = n",  | 
|
278  | 
"(l::'a::number_ring) = m - n",  | 
|
279  | 
"(l::'a::number_ring) * m = n",  | 
|
280  | 
"(l::'a::number_ring) = m * n"],  | 
|
281  | 
K EqCancelNumerals.proc),  | 
|
282  | 
    ("intless_cancel_numerals",
 | 
|
283  | 
     ["(l::'a::{ordered_idom,number_ring}) + m < n",
 | 
|
284  | 
      "(l::'a::{ordered_idom,number_ring}) < m + n",
 | 
|
285  | 
      "(l::'a::{ordered_idom,number_ring}) - m < n",
 | 
|
286  | 
      "(l::'a::{ordered_idom,number_ring}) < m - n",
 | 
|
287  | 
      "(l::'a::{ordered_idom,number_ring}) * m < n",
 | 
|
288  | 
      "(l::'a::{ordered_idom,number_ring}) < m * n"],
 | 
|
289  | 
K LessCancelNumerals.proc),  | 
|
290  | 
    ("intle_cancel_numerals",
 | 
|
291  | 
     ["(l::'a::{ordered_idom,number_ring}) + m <= n",
 | 
|
292  | 
      "(l::'a::{ordered_idom,number_ring}) <= m + n",
 | 
|
293  | 
      "(l::'a::{ordered_idom,number_ring}) - m <= n",
 | 
|
294  | 
      "(l::'a::{ordered_idom,number_ring}) <= m - n",
 | 
|
295  | 
      "(l::'a::{ordered_idom,number_ring}) * m <= n",
 | 
|
296  | 
      "(l::'a::{ordered_idom,number_ring}) <= m * n"],
 | 
|
297  | 
K LeCancelNumerals.proc)];  | 
|
298  | 
||
299  | 
||
300  | 
structure CombineNumeralsData =  | 
|
301  | 
struct  | 
|
| 
24630
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24266 
diff
changeset
 | 
302  | 
type coeff = int  | 
| 
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24266 
diff
changeset
 | 
303  | 
val iszero = (fn x => x = 0)  | 
| 
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24266 
diff
changeset
 | 
304  | 
val add = op +  | 
| 23164 | 305  | 
val mk_sum = long_mk_sum (*to work for e.g. 2*x + 3*x *)  | 
306  | 
val dest_sum = dest_sum  | 
|
307  | 
val mk_coeff = mk_coeff  | 
|
308  | 
val dest_coeff = dest_coeff 1  | 
|
| 25481 | 309  | 
  val left_distrib      = @{thm combine_common_factor} RS trans
 | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
310  | 
val prove_conv = Arith_Data.prove_conv_nohyps  | 
| 30518 | 311  | 
val trans_tac = K Arith_Data.trans_tac  | 
| 23164 | 312  | 
|
313  | 
val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @  | 
|
| 23881 | 314  | 
    diff_simps @ minus_simps @ @{thms add_ac}
 | 
| 23164 | 315  | 
val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps  | 
| 23881 | 316  | 
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
 | 
| 23164 | 317  | 
fun norm_tac ss =  | 
318  | 
ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))  | 
|
319  | 
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))  | 
|
320  | 
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))  | 
|
321  | 
||
322  | 
val numeral_simp_ss = HOL_ss addsimps add_0s @ simps  | 
|
323  | 
fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))  | 
|
| 30518 | 324  | 
val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s)  | 
| 23164 | 325  | 
end;  | 
326  | 
||
327  | 
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);  | 
|
328  | 
||
329  | 
(*Version for fields, where coefficients can be fractions*)  | 
|
330  | 
structure FieldCombineNumeralsData =  | 
|
331  | 
struct  | 
|
| 
24630
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24266 
diff
changeset
 | 
332  | 
type coeff = int * int  | 
| 
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24266 
diff
changeset
 | 
333  | 
val iszero = (fn (p, q) => p = 0)  | 
| 23164 | 334  | 
val add = add_frac  | 
335  | 
val mk_sum = long_mk_sum  | 
|
336  | 
val dest_sum = dest_sum  | 
|
337  | 
val mk_coeff = mk_fcoeff  | 
|
338  | 
val dest_coeff = dest_fcoeff 1  | 
|
| 25481 | 339  | 
  val left_distrib      = @{thm combine_common_factor} RS trans
 | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
340  | 
val prove_conv = Arith_Data.prove_conv_nohyps  | 
| 30518 | 341  | 
val trans_tac = K Arith_Data.trans_tac  | 
| 23164 | 342  | 
|
343  | 
val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @  | 
|
| 23881 | 344  | 
    inverse_1s @ divide_simps @ diff_simps @ minus_simps @ @{thms add_ac}
 | 
| 23164 | 345  | 
val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps  | 
| 23881 | 346  | 
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
 | 
| 23164 | 347  | 
fun norm_tac ss =  | 
348  | 
ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))  | 
|
349  | 
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))  | 
|
350  | 
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))  | 
|
351  | 
||
352  | 
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps @ [@{thm add_frac_eq}]
 | 
|
353  | 
fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))  | 
|
| 30518 | 354  | 
val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s @ divide_1s)  | 
| 23164 | 355  | 
end;  | 
356  | 
||
357  | 
structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);  | 
|
358  | 
||
359  | 
val combine_numerals =  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
360  | 
Arith_Data.prep_simproc  | 
| 23164 | 361  | 
    ("int_combine_numerals", 
 | 
362  | 
["(i::'a::number_ring) + j", "(i::'a::number_ring) - j"],  | 
|
363  | 
K CombineNumerals.proc);  | 
|
364  | 
||
365  | 
val field_combine_numerals =  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
366  | 
Arith_Data.prep_simproc  | 
| 23164 | 367  | 
    ("field_combine_numerals", 
 | 
368  | 
     ["(i::'a::{number_ring,field,division_by_zero}) + j",
 | 
|
369  | 
      "(i::'a::{number_ring,field,division_by_zero}) - j"], 
 | 
|
370  | 
K FieldCombineNumerals.proc);  | 
|
371  | 
||
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
372  | 
(** Constant folding for multiplication in semirings **)  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
373  | 
|
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
374  | 
(*We do not need folding for addition: combine_numerals does the same thing*)  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
375  | 
|
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
376  | 
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
377  | 
struct  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
378  | 
  val assoc_ss = HOL_ss addsimps @{thms mult_ac}
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
379  | 
val eq_reflection = eq_reflection  | 
| 23164 | 380  | 
end;  | 
381  | 
||
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
382  | 
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
383  | 
|
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
384  | 
val assoc_fold_simproc =  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
385  | 
Arith_Data.prep_simproc  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
386  | 
   ("semiring_assoc_fold", ["(a::'a::comm_semiring_1_cancel) * b"],
 | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
387  | 
K Semiring_Times_Assoc.proc);  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
388  | 
|
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
389  | 
end;  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
390  | 
|
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
391  | 
Addsimprocs [Int_Numeral_Simprocs.reorient_simproc];  | 
| 23164 | 392  | 
Addsimprocs Int_Numeral_Simprocs.cancel_numerals;  | 
393  | 
Addsimprocs [Int_Numeral_Simprocs.combine_numerals];  | 
|
394  | 
Addsimprocs [Int_Numeral_Simprocs.field_combine_numerals];  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
395  | 
Addsimprocs [Int_Numeral_Simprocs.assoc_fold_simproc];  | 
| 23164 | 396  | 
|
397  | 
(*examples:  | 
|
398  | 
print_depth 22;  | 
|
399  | 
set timing;  | 
|
400  | 
set trace_simp;  | 
|
401  | 
fun test s = (Goal s, by (Simp_tac 1));  | 
|
402  | 
||
403  | 
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";  | 
|
404  | 
||
405  | 
test "2*u = (u::int)";  | 
|
406  | 
test "(i + j + 12 + (k::int)) - 15 = y";  | 
|
407  | 
test "(i + j + 12 + (k::int)) - 5 = y";  | 
|
408  | 
||
409  | 
test "y - b < (b::int)";  | 
|
410  | 
test "y - (3*b + c) < (b::int) - 2*c";  | 
|
411  | 
||
412  | 
test "(2*x - (u*v) + y) - v*3*u = (w::int)";  | 
|
413  | 
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";  | 
|
414  | 
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";  | 
|
415  | 
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";  | 
|
416  | 
||
417  | 
test "(i + j + 12 + (k::int)) = u + 15 + y";  | 
|
418  | 
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";  | 
|
419  | 
||
420  | 
test "2*y + 3*z + 6*w + 2*y + 3*z + 2*u = 2*y' + 3*z' + 6*w' + 2*y' + 3*z' + u + (vv::int)";  | 
|
421  | 
||
422  | 
test "a + -(b+c) + b = (d::int)";  | 
|
423  | 
test "a + -(b+c) - b = (d::int)";  | 
|
424  | 
||
425  | 
(*negative numerals*)  | 
|
426  | 
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";  | 
|
427  | 
test "(i + j + -3 + (k::int)) < u + 5 + y";  | 
|
428  | 
test "(i + j + 3 + (k::int)) < u + -6 + y";  | 
|
429  | 
test "(i + j + -12 + (k::int)) - 15 = y";  | 
|
430  | 
test "(i + j + 12 + (k::int)) - -15 = y";  | 
|
431  | 
test "(i + j + -12 + (k::int)) - -15 = y";  | 
|
432  | 
*)  | 
|
433  | 
||
434  | 
(*** decision procedure for linear arithmetic ***)  | 
|
435  | 
||
436  | 
(*---------------------------------------------------------------------------*)  | 
|
437  | 
(* Linear arithmetic *)  | 
|
438  | 
(*---------------------------------------------------------------------------*)  | 
|
439  | 
||
440  | 
(*  | 
|
441  | 
Instantiation of the generic linear arithmetic package for int.  | 
|
442  | 
*)  | 
|
443  | 
||
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
444  | 
structure Int_Arith =  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
445  | 
struct  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
446  | 
|
| 23164 | 447  | 
(* Update parameters of arithmetic prover *)  | 
448  | 
||
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
449  | 
(* reduce contradictory =/</<= to False *)  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
450  | 
|
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
451  | 
(* Evaluation of terms of the form "m R n" where R is one of "=", "<=" or "<",  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
452  | 
and m and n are ground terms over rings (roughly speaking).  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
453  | 
That is, m and n consist only of 1s combined with "+", "-" and "*".  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
454  | 
*)  | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
455  | 
|
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
456  | 
val zeroth = (symmetric o mk_meta_eq) @{thm of_int_0};
 | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
457  | 
|
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
458  | 
val lhss0 = [@{cpat "0::?'a::ring"}];
 | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
459  | 
|
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
460  | 
fun proc0 phi ss ct =  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
461  | 
let val T = ctyp_of_term ct  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
462  | 
  in if typ_of T = @{typ int} then NONE else
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
463  | 
SOME (instantiate' [SOME T] [] zeroth)  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
464  | 
end;  | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
465  | 
|
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
466  | 
val zero_to_of_int_zero_simproc =  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
467  | 
  make_simproc {lhss = lhss0, name = "zero_to_of_int_zero_simproc",
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
468  | 
proc = proc0, identifier = []};  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
469  | 
|
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
470  | 
val oneth = (symmetric o mk_meta_eq) @{thm of_int_1};
 | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
471  | 
|
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
472  | 
val lhss1 = [@{cpat "1::?'a::ring_1"}];
 | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
473  | 
|
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
474  | 
fun proc1 phi ss ct =  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
475  | 
let val T = ctyp_of_term ct  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
476  | 
  in if typ_of T = @{typ int} then NONE else
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
477  | 
SOME (instantiate' [SOME T] [] oneth)  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
478  | 
end;  | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
479  | 
|
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
480  | 
val one_to_of_int_one_simproc =  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
481  | 
  make_simproc {lhss = lhss1, name = "one_to_of_int_one_simproc",
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
482  | 
proc = proc1, identifier = []};  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
483  | 
|
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
484  | 
val allowed_consts =  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
485  | 
  [@{const_name "op ="}, @{const_name "HOL.times"}, @{const_name "HOL.uminus"},
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
486  | 
   @{const_name "HOL.minus"}, @{const_name "HOL.plus"},
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
487  | 
   @{const_name "HOL.zero"}, @{const_name "HOL.one"}, @{const_name "HOL.less"},
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
488  | 
   @{const_name "HOL.less_eq"}];
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
489  | 
|
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
490  | 
fun check t = case t of  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
491  | 
   Const(s,t) => if s = @{const_name "HOL.one"} then not (t = @{typ int})
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
492  | 
else s mem_string allowed_consts  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
493  | 
| a$b => check a andalso check b  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
494  | 
| _ => false;  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
495  | 
|
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
496  | 
val conv =  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
497  | 
Simplifier.rewrite  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
498  | 
(HOL_basic_ss addsimps  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
499  | 
     ((map (fn th => th RS sym) [@{thm of_int_add}, @{thm of_int_mult},
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
500  | 
             @{thm of_int_diff},  @{thm of_int_minus}])@
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
501  | 
      [@{thm of_int_less_iff}, @{thm of_int_le_iff}, @{thm of_int_eq_iff}])
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
502  | 
addsimprocs [zero_to_of_int_zero_simproc,one_to_of_int_one_simproc]);  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
503  | 
|
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
504  | 
fun sproc phi ss ct = if check (term_of ct) then SOME (conv ct) else NONE  | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
505  | 
|
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
506  | 
val lhss' =  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
507  | 
  [@{cpat "(?x::?'a::ring_char_0) = (?y::?'a)"},
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
508  | 
   @{cpat "(?x::?'a::ordered_idom) < (?y::?'a)"},
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
509  | 
   @{cpat "(?x::?'a::ordered_idom) <= (?y::?'a)"}]
 | 
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
510  | 
|
| 
24266
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
511  | 
val zero_one_idom_simproc =  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
512  | 
  make_simproc {lhss = lhss' , name = "zero_one_idom_simproc",
 | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
513  | 
proc = sproc, identifier = []}  | 
| 
 
bdb48fd8fbdd
extended linear arith capabilities with code by Amine
 
nipkow 
parents: 
24196 
diff
changeset
 | 
514  | 
|
| 23164 | 515  | 
val add_rules =  | 
| 25481 | 516  | 
    simp_thms @ @{thms arith_simps} @ @{thms rel_simps} @ @{thms arith_special} @
 | 
| 23164 | 517  | 
    [@{thm neg_le_iff_le}, @{thm numeral_0_eq_0}, @{thm numeral_1_eq_1},
 | 
518  | 
     @{thm minus_zero}, @{thm diff_minus}, @{thm left_minus}, @{thm right_minus},
 | 
|
| 
26086
 
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
 
huffman 
parents: 
26075 
diff
changeset
 | 
519  | 
     @{thm mult_zero_left}, @{thm mult_zero_right}, @{thm mult_Bit1}, @{thm mult_1_right},
 | 
| 23164 | 520  | 
     @{thm minus_mult_left} RS sym, @{thm minus_mult_right} RS sym,
 | 
521  | 
     @{thm minus_add_distrib}, @{thm minus_minus}, @{thm mult_assoc},
 | 
|
| 23365 | 522  | 
     @{thm of_nat_0}, @{thm of_nat_1}, @{thm of_nat_Suc}, @{thm of_nat_add},
 | 
523  | 
     @{thm of_nat_mult}, @{thm of_int_0}, @{thm of_int_1}, @{thm of_int_add},
 | 
|
524  | 
     @{thm of_int_mult}]
 | 
|
| 23164 | 525  | 
|
| 23365 | 526  | 
val nat_inj_thms = [@{thm zle_int} RS iffD2, @{thm int_int_eq} RS iffD2]
 | 
| 23164 | 527  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
528  | 
val int_numeral_base_simprocs = Int_Numeral_Simprocs.assoc_fold_simproc :: zero_one_idom_simproc  | 
| 23164 | 529  | 
:: Int_Numeral_Simprocs.combine_numerals  | 
530  | 
:: Int_Numeral_Simprocs.cancel_numerals;  | 
|
531  | 
||
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
532  | 
val setup =  | 
| 24093 | 533  | 
  LinArith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
 | 
| 23164 | 534  | 
   {add_mono_thms = add_mono_thms,
 | 
535  | 
    mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono} :: mult_mono_thms,
 | 
|
536  | 
inj_thms = nat_inj_thms @ inj_thms,  | 
|
| 25481 | 537  | 
    lessD = lessD @ [@{thm zless_imp_add1_zle}],
 | 
| 23164 | 538  | 
neqE = neqE,  | 
539  | 
simpset = simpset addsimps add_rules  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
540  | 
addsimprocs int_numeral_base_simprocs  | 
| 23164 | 541  | 
addcongs [if_weak_cong]}) #>  | 
| 24196 | 542  | 
  arith_inj_const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT) #>
 | 
| 
25919
 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
 
haftmann 
parents: 
25481 
diff
changeset
 | 
543  | 
  arith_discrete @{type_name Int.int}
 | 
| 23164 | 544  | 
|
545  | 
val fast_int_arith_simproc =  | 
|
| 
28262
 
aa7ca36d67fd
back to dynamic the_context(), because static @{theory} is invalidated if ML environment changes within the same code block;
 
wenzelm 
parents: 
26086 
diff
changeset
 | 
546  | 
Simplifier.simproc (the_context ())  | 
| 23164 | 547  | 
"fast_int_arith"  | 
548  | 
     ["(m::'a::{ordered_idom,number_ring}) < n",
 | 
|
549  | 
      "(m::'a::{ordered_idom,number_ring}) <= n",
 | 
|
| 24093 | 550  | 
      "(m::'a::{ordered_idom,number_ring}) = n"] (K LinArith.lin_arith_simproc);
 | 
| 23164 | 551  | 
|
| 
30496
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
552  | 
end;  | 
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
553  | 
|
| 
 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 
haftmann 
parents: 
29269 
diff
changeset
 | 
554  | 
Addsimprocs [Int_Arith.fast_int_arith_simproc];  |