author | wenzelm |
Sat, 06 Oct 2001 00:02:46 +0200 | |
changeset 11704 | 3c50a2cd6f00 |
parent 11701 | 3d51fbf81c17 |
child 11713 | 883d559b0b8c |
permissions | -rw-r--r-- |
10751 | 1 |
(* Title: HOL/Hyperreal/HyperBin.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 2000 University of Cambridge |
|
5 |
||
6 |
Binary arithmetic for the hypreals (integer literals only). |
|
7 |
*) |
|
8 |
||
9 |
(** hypreal_of_real (coercion from int to real) **) |
|
10 |
||
11 |
Goal "hypreal_of_real (number_of w) = number_of w"; |
|
12 |
by (simp_tac (simpset() addsimps [hypreal_number_of_def]) 1); |
|
13 |
qed "hypreal_number_of"; |
|
14 |
Addsimps [hypreal_number_of]; |
|
15 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
10784
diff
changeset
|
16 |
Goalw [hypreal_number_of_def] "(0::hypreal) = Numeral0"; |
10751 | 17 |
by (simp_tac (simpset() addsimps [hypreal_of_real_zero RS sym]) 1); |
18 |
qed "zero_eq_numeral_0"; |
|
19 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
10784
diff
changeset
|
20 |
Goalw [hypreal_number_of_def] "1hr = Numeral1"; |
10751 | 21 |
by (simp_tac (simpset() addsimps [hypreal_of_real_one RS sym]) 1); |
22 |
qed "one_eq_numeral_1"; |
|
23 |
||
24 |
(** Addition **) |
|
25 |
||
26 |
Goal "(number_of v :: hypreal) + number_of v' = number_of (bin_add v v')"; |
|
27 |
by (simp_tac |
|
28 |
(HOL_ss addsimps [hypreal_number_of_def, |
|
29 |
hypreal_of_real_add RS sym, add_real_number_of]) 1); |
|
30 |
qed "add_hypreal_number_of"; |
|
31 |
Addsimps [add_hypreal_number_of]; |
|
32 |
||
33 |
||
34 |
(** Subtraction **) |
|
35 |
||
36 |
Goalw [hypreal_number_of_def] |
|
37 |
"- (number_of w :: hypreal) = number_of (bin_minus w)"; |
|
38 |
by (simp_tac |
|
39 |
(HOL_ss addsimps [minus_real_number_of, hypreal_of_real_minus RS sym]) 1); |
|
40 |
qed "minus_hypreal_number_of"; |
|
41 |
Addsimps [minus_hypreal_number_of]; |
|
42 |
||
43 |
Goalw [hypreal_number_of_def, hypreal_diff_def] |
|
44 |
"(number_of v :: hypreal) - number_of w = \ |
|
45 |
\ number_of (bin_add v (bin_minus w))"; |
|
46 |
by (Simp_tac 1); |
|
47 |
qed "diff_hypreal_number_of"; |
|
48 |
Addsimps [diff_hypreal_number_of]; |
|
49 |
||
50 |
||
51 |
(** Multiplication **) |
|
52 |
||
53 |
Goal "(number_of v :: hypreal) * number_of v' = number_of (bin_mult v v')"; |
|
54 |
by (simp_tac |
|
55 |
(HOL_ss addsimps [hypreal_number_of_def, |
|
56 |
hypreal_of_real_mult RS sym, mult_real_number_of]) 1); |
|
57 |
qed "mult_hypreal_number_of"; |
|
58 |
Addsimps [mult_hypreal_number_of]; |
|
59 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
60 |
Goal "(2::hypreal) = Numeral1 + Numeral1"; |
10751 | 61 |
by (Simp_tac 1); |
62 |
val lemma = result(); |
|
63 |
||
64 |
(*For specialist use: NOT as default simprules*) |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
65 |
Goal "2 * z = (z+z::hypreal)"; |
10751 | 66 |
by (simp_tac (simpset () |
67 |
addsimps [lemma, hypreal_add_mult_distrib, |
|
68 |
one_eq_numeral_1 RS sym]) 1); |
|
69 |
qed "hypreal_mult_2"; |
|
70 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
71 |
Goal "z * 2 = (z+z::hypreal)"; |
10751 | 72 |
by (stac hypreal_mult_commute 1 THEN rtac hypreal_mult_2 1); |
73 |
qed "hypreal_mult_2_right"; |
|
74 |
||
75 |
||
76 |
(*** Comparisons ***) |
|
77 |
||
78 |
(** Equals (=) **) |
|
79 |
||
80 |
Goal "((number_of v :: hypreal) = number_of v') = \ |
|
81 |
\ iszero (number_of (bin_add v (bin_minus v')))"; |
|
82 |
by (simp_tac |
|
83 |
(HOL_ss addsimps [hypreal_number_of_def, |
|
84 |
hypreal_of_real_eq_iff, eq_real_number_of]) 1); |
|
85 |
qed "eq_hypreal_number_of"; |
|
86 |
Addsimps [eq_hypreal_number_of]; |
|
87 |
||
88 |
(** Less-than (<) **) |
|
89 |
||
90 |
(*"neg" is used in rewrite rules for binary comparisons*) |
|
91 |
Goal "((number_of v :: hypreal) < number_of v') = \ |
|
92 |
\ neg (number_of (bin_add v (bin_minus v')))"; |
|
93 |
by (simp_tac |
|
94 |
(HOL_ss addsimps [hypreal_number_of_def, hypreal_of_real_less_iff, |
|
95 |
less_real_number_of]) 1); |
|
96 |
qed "less_hypreal_number_of"; |
|
97 |
Addsimps [less_hypreal_number_of]; |
|
98 |
||
99 |
||
100 |
(** Less-than-or-equals (<=) **) |
|
101 |
||
102 |
Goal "(number_of x <= (number_of y::hypreal)) = \ |
|
103 |
\ (~ number_of y < (number_of x::hypreal))"; |
|
104 |
by (rtac (linorder_not_less RS sym) 1); |
|
105 |
qed "le_hypreal_number_of_eq_not_less"; |
|
106 |
Addsimps [le_hypreal_number_of_eq_not_less]; |
|
107 |
||
108 |
(*** New versions of existing theorems involving 0, 1hr ***) |
|
109 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
110 |
Goal "- Numeral1 = (-1::hypreal)"; |
10751 | 111 |
by (Simp_tac 1); |
112 |
qed "minus_numeral_one"; |
|
113 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
114 |
(*Maps 0 to Numeral0 and 1hr to Numeral1 and -1hr to -1*) |
10751 | 115 |
val hypreal_numeral_ss = |
116 |
real_numeral_ss addsimps [zero_eq_numeral_0, one_eq_numeral_1, |
|
117 |
minus_numeral_one]; |
|
118 |
||
119 |
fun rename_numerals th = |
|
120 |
asm_full_simplify hypreal_numeral_ss (Thm.transfer (the_context ()) th); |
|
121 |
||
122 |
||
123 |
(*Now insert some identities previously stated for 0 and 1hr*) |
|
124 |
||
125 |
(** HyperDef **) |
|
126 |
||
127 |
Addsimps (map rename_numerals |
|
128 |
[hypreal_minus_zero, hypreal_minus_zero_iff, |
|
129 |
hypreal_add_zero_left, hypreal_add_zero_right, |
|
130 |
hypreal_diff_zero, hypreal_diff_zero_right, |
|
131 |
hypreal_mult_0_right, hypreal_mult_0, |
|
132 |
hypreal_mult_1_right, hypreal_mult_1, |
|
133 |
hypreal_inverse_1, hypreal_minus_zero_less_iff]); |
|
134 |
||
135 |
bind_thm ("hypreal_0_less_mult_iff", |
|
136 |
rename_numerals hypreal_zero_less_mult_iff); |
|
137 |
bind_thm ("hypreal_0_le_mult_iff", |
|
138 |
rename_numerals hypreal_zero_le_mult_iff); |
|
139 |
bind_thm ("hypreal_mult_less_0_iff", |
|
140 |
rename_numerals hypreal_mult_less_zero_iff); |
|
141 |
bind_thm ("hypreal_mult_le_0_iff", |
|
142 |
rename_numerals hypreal_mult_le_zero_iff); |
|
143 |
||
144 |
bind_thm ("hypreal_inverse_less_0", rename_numerals hypreal_inverse_less_zero); |
|
145 |
bind_thm ("hypreal_inverse_gt_0", rename_numerals hypreal_inverse_gt_zero); |
|
146 |
||
147 |
Addsimps [zero_eq_numeral_0,one_eq_numeral_1]; |
|
148 |
||
149 |
||
150 |
(** Simplification of arithmetic when nested to the right **) |
|
151 |
||
152 |
Goal "number_of v + (number_of w + z) = (number_of(bin_add v w) + z::hypreal)"; |
|
153 |
by Auto_tac; |
|
154 |
qed "hypreal_add_number_of_left"; |
|
155 |
||
156 |
Goal "number_of v *(number_of w * z) = (number_of(bin_mult v w) * z::hypreal)"; |
|
157 |
by (simp_tac (simpset() addsimps [hypreal_mult_assoc RS sym]) 1); |
|
158 |
qed "hypreal_mult_number_of_left"; |
|
159 |
||
160 |
Goalw [hypreal_diff_def] |
|
161 |
"number_of v + (number_of w - c) = number_of(bin_add v w) - (c::hypreal)"; |
|
162 |
by (rtac hypreal_add_number_of_left 1); |
|
163 |
qed "hypreal_add_number_of_diff1"; |
|
164 |
||
165 |
Goal "number_of v + (c - number_of w) = \ |
|
166 |
\ number_of (bin_add v (bin_minus w)) + (c::hypreal)"; |
|
167 |
by (stac (diff_hypreal_number_of RS sym) 1); |
|
168 |
by Auto_tac; |
|
169 |
qed "hypreal_add_number_of_diff2"; |
|
170 |
||
171 |
Addsimps [hypreal_add_number_of_left, hypreal_mult_number_of_left, |
|
172 |
hypreal_add_number_of_diff1, hypreal_add_number_of_diff2]; |
|
173 |
||
174 |
||
175 |
(**** Simprocs for numeric literals ****) |
|
176 |
||
177 |
(** Combining of literal coefficients in sums of products **) |
|
178 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
10784
diff
changeset
|
179 |
Goal "(x < y) = (x-y < (Numeral0::hypreal))"; |
10751 | 180 |
by (simp_tac (simpset() addsimps [hypreal_diff_less_eq]) 1); |
181 |
qed "hypreal_less_iff_diff_less_0"; |
|
182 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
10784
diff
changeset
|
183 |
Goal "(x = y) = (x-y = (Numeral0::hypreal))"; |
10751 | 184 |
by (simp_tac (simpset() addsimps [hypreal_diff_eq_eq]) 1); |
185 |
qed "hypreal_eq_iff_diff_eq_0"; |
|
186 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
10784
diff
changeset
|
187 |
Goal "(x <= y) = (x-y <= (Numeral0::hypreal))"; |
10751 | 188 |
by (simp_tac (simpset() addsimps [hypreal_diff_le_eq]) 1); |
189 |
qed "hypreal_le_iff_diff_le_0"; |
|
190 |
||
191 |
||
192 |
(** For combine_numerals **) |
|
193 |
||
194 |
Goal "i*u + (j*u + k) = (i+j)*u + (k::hypreal)"; |
|
195 |
by (asm_simp_tac (simpset() addsimps [hypreal_add_mult_distrib]) 1); |
|
196 |
qed "left_hypreal_add_mult_distrib"; |
|
197 |
||
198 |
||
199 |
(** For cancel_numerals **) |
|
200 |
||
201 |
val rel_iff_rel_0_rls = |
|
202 |
map (inst "y" "?u+?v") |
|
203 |
[hypreal_less_iff_diff_less_0, hypreal_eq_iff_diff_eq_0, |
|
204 |
hypreal_le_iff_diff_le_0] @ |
|
205 |
map (inst "y" "n") |
|
206 |
[hypreal_less_iff_diff_less_0, hypreal_eq_iff_diff_eq_0, |
|
207 |
hypreal_le_iff_diff_le_0]; |
|
208 |
||
209 |
Goal "!!i::hypreal. (i*u + m = j*u + n) = ((i-j)*u + m = n)"; |
|
210 |
by (asm_simp_tac |
|
211 |
(simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@ |
|
212 |
hypreal_add_ac@rel_iff_rel_0_rls) 1); |
|
213 |
qed "hypreal_eq_add_iff1"; |
|
214 |
||
215 |
Goal "!!i::hypreal. (i*u + m = j*u + n) = (m = (j-i)*u + n)"; |
|
216 |
by (asm_simp_tac |
|
217 |
(simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@ |
|
218 |
hypreal_add_ac@rel_iff_rel_0_rls) 1); |
|
219 |
qed "hypreal_eq_add_iff2"; |
|
220 |
||
221 |
Goal "!!i::hypreal. (i*u + m < j*u + n) = ((i-j)*u + m < n)"; |
|
222 |
by (asm_simp_tac |
|
223 |
(simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@ |
|
224 |
hypreal_add_ac@rel_iff_rel_0_rls) 1); |
|
225 |
qed "hypreal_less_add_iff1"; |
|
226 |
||
227 |
Goal "!!i::hypreal. (i*u + m < j*u + n) = (m < (j-i)*u + n)"; |
|
228 |
by (asm_simp_tac |
|
229 |
(simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@ |
|
230 |
hypreal_add_ac@rel_iff_rel_0_rls) 1); |
|
231 |
qed "hypreal_less_add_iff2"; |
|
232 |
||
233 |
Goal "!!i::hypreal. (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"; |
|
234 |
by (asm_simp_tac |
|
235 |
(simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@ |
|
236 |
hypreal_add_ac@rel_iff_rel_0_rls) 1); |
|
237 |
qed "hypreal_le_add_iff1"; |
|
238 |
||
239 |
Goal "!!i::hypreal. (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"; |
|
240 |
by (asm_simp_tac |
|
241 |
(simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@ |
|
242 |
hypreal_add_ac@rel_iff_rel_0_rls) 1); |
|
243 |
qed "hypreal_le_add_iff2"; |
|
244 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
245 |
Goal "(z::hypreal) * -1 = -z"; |
10751 | 246 |
by (stac (minus_numeral_one RS sym) 1); |
247 |
by (stac (hypreal_minus_mult_eq2 RS sym) 1); |
|
248 |
by Auto_tac; |
|
249 |
qed "hypreal_mult_minus_1_right"; |
|
250 |
Addsimps [hypreal_mult_minus_1_right]; |
|
251 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
252 |
Goal "-1 * (z::hypreal) = -z"; |
10751 | 253 |
by (simp_tac (simpset() addsimps [hypreal_mult_commute]) 1); |
254 |
qed "hypreal_mult_minus_1"; |
|
255 |
Addsimps [hypreal_mult_minus_1]; |
|
256 |
||
257 |
||
258 |
||
259 |
structure Hyperreal_Numeral_Simprocs = |
|
260 |
struct |
|
261 |
||
262 |
(*Utilities*) |
|
263 |
||
264 |
||
265 |
val hyprealT = Type("HyperDef.hypreal",[]); |
|
266 |
||
267 |
fun mk_numeral n = HOLogic.number_of_const hyprealT $ HOLogic.mk_bin n; |
|
268 |
||
269 |
val dest_numeral = Real_Numeral_Simprocs.dest_numeral; |
|
270 |
||
271 |
val find_first_numeral = Real_Numeral_Simprocs.find_first_numeral; |
|
272 |
||
273 |
val zero = mk_numeral 0; |
|
274 |
val mk_plus = HOLogic.mk_binop "op +"; |
|
275 |
||
276 |
val uminus_const = Const ("uminus", hyprealT --> hyprealT); |
|
277 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
10784
diff
changeset
|
278 |
(*Thus mk_sum[t] yields t+Numeral0; longer sums don't have a trailing zero*) |
10751 | 279 |
fun mk_sum [] = zero |
280 |
| mk_sum [t,u] = mk_plus (t, u) |
|
281 |
| mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
282 |
||
283 |
(*this version ALWAYS includes a trailing zero*) |
|
284 |
fun long_mk_sum [] = zero |
|
285 |
| long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
286 |
||
287 |
val dest_plus = HOLogic.dest_bin "op +" hyprealT; |
|
288 |
||
289 |
(*decompose additions AND subtractions as a sum*) |
|
290 |
fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) = |
|
291 |
dest_summing (pos, t, dest_summing (pos, u, ts)) |
|
292 |
| dest_summing (pos, Const ("op -", _) $ t $ u, ts) = |
|
293 |
dest_summing (pos, t, dest_summing (not pos, u, ts)) |
|
294 |
| dest_summing (pos, t, ts) = |
|
295 |
if pos then t::ts else uminus_const$t :: ts; |
|
296 |
||
297 |
fun dest_sum t = dest_summing (true, t, []); |
|
298 |
||
299 |
val mk_diff = HOLogic.mk_binop "op -"; |
|
300 |
val dest_diff = HOLogic.dest_bin "op -" hyprealT; |
|
301 |
||
302 |
val one = mk_numeral 1; |
|
303 |
val mk_times = HOLogic.mk_binop "op *"; |
|
304 |
||
305 |
fun mk_prod [] = one |
|
306 |
| mk_prod [t] = t |
|
307 |
| mk_prod (t :: ts) = if t = one then mk_prod ts |
|
308 |
else mk_times (t, mk_prod ts); |
|
309 |
||
310 |
val dest_times = HOLogic.dest_bin "op *" hyprealT; |
|
311 |
||
312 |
fun dest_prod t = |
|
313 |
let val (t,u) = dest_times t |
|
314 |
in dest_prod t @ dest_prod u end |
|
315 |
handle TERM _ => [t]; |
|
316 |
||
317 |
(*DON'T do the obvious simplifications; that would create special cases*) |
|
318 |
fun mk_coeff (k, ts) = mk_times (mk_numeral k, ts); |
|
319 |
||
320 |
(*Express t as a product of (possibly) a numeral with other sorted terms*) |
|
321 |
fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t |
|
322 |
| dest_coeff sign t = |
|
323 |
let val ts = sort Term.term_ord (dest_prod t) |
|
324 |
val (n, ts') = find_first_numeral [] ts |
|
325 |
handle TERM _ => (1, ts) |
|
326 |
in (sign*n, mk_prod ts') end; |
|
327 |
||
328 |
(*Find first coefficient-term THAT MATCHES u*) |
|
329 |
fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) |
|
330 |
| find_first_coeff past u (t::terms) = |
|
331 |
let val (n,u') = dest_coeff 1 t |
|
332 |
in if u aconv u' then (n, rev past @ terms) |
|
333 |
else find_first_coeff (t::past) u terms |
|
334 |
end |
|
335 |
handle TERM _ => find_first_coeff (t::past) u terms; |
|
336 |
||
337 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
10784
diff
changeset
|
338 |
(*Simplify Numeral1*n and n*Numeral1 to n*) |
10751 | 339 |
val add_0s = map rename_numerals |
340 |
[hypreal_add_zero_left, hypreal_add_zero_right]; |
|
341 |
val mult_plus_1s = map rename_numerals |
|
342 |
[hypreal_mult_1, hypreal_mult_1_right]; |
|
343 |
val mult_minus_1s = map rename_numerals |
|
344 |
[hypreal_mult_minus_1, hypreal_mult_minus_1_right]; |
|
345 |
val mult_1s = mult_plus_1s @ mult_minus_1s; |
|
346 |
||
347 |
(*To perform binary arithmetic*) |
|
348 |
val bin_simps = |
|
349 |
[add_hypreal_number_of, hypreal_add_number_of_left, |
|
350 |
minus_hypreal_number_of, diff_hypreal_number_of, mult_hypreal_number_of, |
|
351 |
hypreal_mult_number_of_left] @ bin_arith_simps @ bin_rel_simps; |
|
352 |
||
353 |
(*To evaluate binary negations of coefficients*) |
|
354 |
val hypreal_minus_simps = NCons_simps @ |
|
355 |
[minus_hypreal_number_of, |
|
356 |
bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min, |
|
357 |
bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min]; |
|
358 |
||
359 |
(*To let us treat subtraction as addition*) |
|
360 |
val diff_simps = [hypreal_diff_def, hypreal_minus_add_distrib, |
|
361 |
hypreal_minus_minus]; |
|
362 |
||
363 |
(*push the unary minus down: - x * y = x * - y *) |
|
364 |
val hypreal_minus_mult_eq_1_to_2 = |
|
365 |
[hypreal_minus_mult_eq1 RS sym, hypreal_minus_mult_eq2] MRS trans |
|
366 |
|> standard; |
|
367 |
||
368 |
(*to extract again any uncancelled minuses*) |
|
369 |
val hypreal_minus_from_mult_simps = |
|
370 |
[hypreal_minus_minus, hypreal_minus_mult_eq1 RS sym, |
|
371 |
hypreal_minus_mult_eq2 RS sym]; |
|
372 |
||
373 |
(*combine unary minus with numeric literals, however nested within a product*) |
|
374 |
val hypreal_mult_minus_simps = |
|
375 |
[hypreal_mult_assoc, hypreal_minus_mult_eq1, hypreal_minus_mult_eq_1_to_2]; |
|
376 |
||
377 |
(*Apply the given rewrite (if present) just once*) |
|
378 |
fun trans_tac None = all_tac |
|
379 |
| trans_tac (Some th) = ALLGOALS (rtac (th RS trans)); |
|
380 |
||
381 |
fun prove_conv name tacs sg (hyps: thm list) (t,u) = |
|
382 |
if t aconv u then None |
|
383 |
else |
|
384 |
let val ct = cterm_of sg (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u))) |
|
385 |
in Some |
|
386 |
(prove_goalw_cterm [] ct (K tacs) |
|
387 |
handle ERROR => error |
|
388 |
("The error(s) above occurred while trying to prove " ^ |
|
389 |
string_of_cterm ct ^ "\nInternal failure of simproc " ^ name)) |
|
390 |
end; |
|
391 |
||
392 |
(*version without the hyps argument*) |
|
393 |
fun prove_conv_nohyps name tacs sg = prove_conv name tacs sg []; |
|
394 |
||
395 |
(*Final simplification: cancel + and * *) |
|
396 |
val simplify_meta_eq = |
|
397 |
Int_Numeral_Simprocs.simplify_meta_eq |
|
398 |
[hypreal_add_zero_left, hypreal_add_zero_right, |
|
399 |
hypreal_mult_0, hypreal_mult_0_right, hypreal_mult_1, |
|
400 |
hypreal_mult_1_right]; |
|
401 |
||
402 |
val prep_simproc = Real_Numeral_Simprocs.prep_simproc; |
|
403 |
val prep_pats = map Real_Numeral_Simprocs.prep_pat; |
|
404 |
||
405 |
structure CancelNumeralsCommon = |
|
406 |
struct |
|
407 |
val mk_sum = mk_sum |
|
408 |
val dest_sum = dest_sum |
|
409 |
val mk_coeff = mk_coeff |
|
410 |
val dest_coeff = dest_coeff 1 |
|
411 |
val find_first_coeff = find_first_coeff [] |
|
412 |
val trans_tac = trans_tac |
|
413 |
val norm_tac = |
|
414 |
ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@ |
|
415 |
hypreal_minus_simps@hypreal_add_ac)) |
|
416 |
THEN ALLGOALS (simp_tac (HOL_ss addsimps bin_simps@hypreal_mult_minus_simps)) |
|
417 |
THEN ALLGOALS |
|
418 |
(simp_tac (HOL_ss addsimps hypreal_minus_from_mult_simps@ |
|
419 |
hypreal_add_ac@hypreal_mult_ac)) |
|
420 |
val numeral_simp_tac = ALLGOALS (simp_tac (HOL_ss addsimps add_0s@bin_simps)) |
|
421 |
val simplify_meta_eq = simplify_meta_eq |
|
422 |
end; |
|
423 |
||
424 |
||
425 |
structure EqCancelNumerals = CancelNumeralsFun |
|
426 |
(open CancelNumeralsCommon |
|
427 |
val prove_conv = prove_conv "hyprealeq_cancel_numerals" |
|
428 |
val mk_bal = HOLogic.mk_eq |
|
429 |
val dest_bal = HOLogic.dest_bin "op =" hyprealT |
|
430 |
val bal_add1 = hypreal_eq_add_iff1 RS trans |
|
431 |
val bal_add2 = hypreal_eq_add_iff2 RS trans |
|
432 |
); |
|
433 |
||
434 |
structure LessCancelNumerals = CancelNumeralsFun |
|
435 |
(open CancelNumeralsCommon |
|
436 |
val prove_conv = prove_conv "hyprealless_cancel_numerals" |
|
437 |
val mk_bal = HOLogic.mk_binrel "op <" |
|
438 |
val dest_bal = HOLogic.dest_bin "op <" hyprealT |
|
439 |
val bal_add1 = hypreal_less_add_iff1 RS trans |
|
440 |
val bal_add2 = hypreal_less_add_iff2 RS trans |
|
441 |
); |
|
442 |
||
443 |
structure LeCancelNumerals = CancelNumeralsFun |
|
444 |
(open CancelNumeralsCommon |
|
445 |
val prove_conv = prove_conv "hyprealle_cancel_numerals" |
|
446 |
val mk_bal = HOLogic.mk_binrel "op <=" |
|
447 |
val dest_bal = HOLogic.dest_bin "op <=" hyprealT |
|
448 |
val bal_add1 = hypreal_le_add_iff1 RS trans |
|
449 |
val bal_add2 = hypreal_le_add_iff2 RS trans |
|
450 |
); |
|
451 |
||
452 |
val cancel_numerals = |
|
453 |
map prep_simproc |
|
454 |
[("hyprealeq_cancel_numerals", |
|
455 |
prep_pats ["(l::hypreal) + m = n", "(l::hypreal) = m + n", |
|
456 |
"(l::hypreal) - m = n", "(l::hypreal) = m - n", |
|
457 |
"(l::hypreal) * m = n", "(l::hypreal) = m * n"], |
|
458 |
EqCancelNumerals.proc), |
|
459 |
("hyprealless_cancel_numerals", |
|
460 |
prep_pats ["(l::hypreal) + m < n", "(l::hypreal) < m + n", |
|
461 |
"(l::hypreal) - m < n", "(l::hypreal) < m - n", |
|
462 |
"(l::hypreal) * m < n", "(l::hypreal) < m * n"], |
|
463 |
LessCancelNumerals.proc), |
|
464 |
("hyprealle_cancel_numerals", |
|
465 |
prep_pats ["(l::hypreal) + m <= n", "(l::hypreal) <= m + n", |
|
466 |
"(l::hypreal) - m <= n", "(l::hypreal) <= m - n", |
|
467 |
"(l::hypreal) * m <= n", "(l::hypreal) <= m * n"], |
|
468 |
LeCancelNumerals.proc)]; |
|
469 |
||
470 |
||
471 |
structure CombineNumeralsData = |
|
472 |
struct |
|
473 |
val add = op + : int*int -> int |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
474 |
val mk_sum = long_mk_sum (*to work for e.g. 2*x + 3*x *) |
10751 | 475 |
val dest_sum = dest_sum |
476 |
val mk_coeff = mk_coeff |
|
477 |
val dest_coeff = dest_coeff 1 |
|
478 |
val left_distrib = left_hypreal_add_mult_distrib RS trans |
|
479 |
val prove_conv = prove_conv_nohyps "hypreal_combine_numerals" |
|
480 |
val trans_tac = trans_tac |
|
481 |
val norm_tac = |
|
482 |
ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@ |
|
483 |
hypreal_minus_simps@hypreal_add_ac)) |
|
484 |
THEN ALLGOALS (simp_tac (HOL_ss addsimps bin_simps@hypreal_mult_minus_simps)) |
|
485 |
THEN ALLGOALS (simp_tac (HOL_ss addsimps hypreal_minus_from_mult_simps@ |
|
486 |
hypreal_add_ac@hypreal_mult_ac)) |
|
487 |
val numeral_simp_tac = ALLGOALS |
|
488 |
(simp_tac (HOL_ss addsimps add_0s@bin_simps)) |
|
489 |
val simplify_meta_eq = simplify_meta_eq |
|
490 |
end; |
|
491 |
||
492 |
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); |
|
493 |
||
494 |
val combine_numerals = |
|
495 |
prep_simproc ("hypreal_combine_numerals", |
|
496 |
prep_pats ["(i::hypreal) + j", "(i::hypreal) - j"], |
|
497 |
CombineNumerals.proc); |
|
498 |
||
499 |
||
500 |
(** Declarations for ExtractCommonTerm **) |
|
501 |
||
502 |
(*this version ALWAYS includes a trailing one*) |
|
503 |
fun long_mk_prod [] = one |
|
504 |
| long_mk_prod (t :: ts) = mk_times (t, mk_prod ts); |
|
505 |
||
506 |
(*Find first term that matches u*) |
|
507 |
fun find_first past u [] = raise TERM("find_first", []) |
|
508 |
| find_first past u (t::terms) = |
|
509 |
if u aconv t then (rev past @ terms) |
|
510 |
else find_first (t::past) u terms |
|
511 |
handle TERM _ => find_first (t::past) u terms; |
|
512 |
||
513 |
(*Final simplification: cancel + and * *) |
|
514 |
fun cancel_simplify_meta_eq cancel_th th = |
|
515 |
Int_Numeral_Simprocs.simplify_meta_eq |
|
516 |
[hypreal_mult_1, hypreal_mult_1_right] |
|
517 |
(([th, cancel_th]) MRS trans); |
|
518 |
||
519 |
end; |
|
520 |
||
521 |
Addsimprocs Hyperreal_Numeral_Simprocs.cancel_numerals; |
|
522 |
Addsimprocs [Hyperreal_Numeral_Simprocs.combine_numerals]; |
|
523 |
||
524 |
(*The Abel_Cancel simprocs are now obsolete*) |
|
525 |
Delsimprocs [Hyperreal_Cancel.sum_conv, Hyperreal_Cancel.rel_conv]; |
|
526 |
||
527 |
(*examples: |
|
528 |
print_depth 22; |
|
529 |
set timing; |
|
530 |
set trace_simp; |
|
531 |
fun test s = (Goal s, by (Simp_tac 1)); |
|
532 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
533 |
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::hypreal)"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
534 |
test "2*u = (u::hypreal)"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
535 |
test "(i + j + 12 + (k::hypreal)) - 15 = y"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
536 |
test "(i + j + 12 + (k::hypreal)) - 5 = y"; |
10751 | 537 |
|
538 |
test "y - b < (b::hypreal)"; |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
539 |
test "y - (3*b + c) < (b::hypreal) - 2*c"; |
10751 | 540 |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
541 |
test "(2*x - (u*v) + y) - v*3*u = (w::hypreal)"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
542 |
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::hypreal)"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
543 |
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::hypreal)"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
544 |
test "u*v - (x*u*v + (u*v)*4 + y) = (w::hypreal)"; |
10751 | 545 |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
546 |
test "(i + j + 12 + (k::hypreal)) = u + 15 + y"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
547 |
test "(i + j*2 + 12 + (k::hypreal)) = j + 5 + y"; |
10751 | 548 |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
549 |
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::hypreal)"; |
10751 | 550 |
|
551 |
test "a + -(b+c) + b = (d::hypreal)"; |
|
552 |
test "a + -(b+c) - b = (d::hypreal)"; |
|
553 |
||
554 |
(*negative numerals*) |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
555 |
test "(i + j + -2 + (k::hypreal)) - (u + 5 + y) = zz"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
556 |
test "(i + j + -3 + (k::hypreal)) < u + 5 + y"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
557 |
test "(i + j + 3 + (k::hypreal)) < u + -6 + y"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
558 |
test "(i + j + -12 + (k::hypreal)) - 15 = y"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
559 |
test "(i + j + 12 + (k::hypreal)) - -15 = y"; |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
560 |
test "(i + j + -12 + (k::hypreal)) - -15 = y"; |
10751 | 561 |
*) |
562 |
||
563 |
||
564 |
(** Constant folding for hypreal plus and times **) |
|
565 |
||
566 |
(*We do not need |
|
567 |
structure Hyperreal_Plus_Assoc = Assoc_Fold (Hyperreal_Plus_Assoc_Data); |
|
568 |
because combine_numerals does the same thing*) |
|
569 |
||
570 |
structure Hyperreal_Times_Assoc_Data : ASSOC_FOLD_DATA = |
|
571 |
struct |
|
572 |
val ss = HOL_ss |
|
573 |
val eq_reflection = eq_reflection |
|
574 |
val sg_ref = Sign.self_ref (Theory.sign_of (the_context ())) |
|
575 |
val T = Hyperreal_Numeral_Simprocs.hyprealT |
|
576 |
val plus = Const ("op *", [T,T] ---> T) |
|
577 |
val add_ac = hypreal_mult_ac |
|
578 |
end; |
|
579 |
||
580 |
structure Hyperreal_Times_Assoc = Assoc_Fold (Hyperreal_Times_Assoc_Data); |
|
581 |
||
582 |
Addsimprocs [Hyperreal_Times_Assoc.conv]; |
|
583 |
||
584 |
Addsimps [rename_numerals hypreal_of_real_zero_iff]; |
|
585 |
||
586 |
(*Simplification of x-y < 0, etc.*) |
|
587 |
AddIffs [hypreal_less_iff_diff_less_0 RS sym]; |
|
588 |
AddIffs [hypreal_eq_iff_diff_eq_0 RS sym]; |
|
589 |
AddIffs [hypreal_le_iff_diff_le_0 RS sym]; |
|
590 |
||
591 |
||
592 |
(** number_of related to hypreal_of_real **) |
|
593 |
||
594 |
Goal "(number_of w < hypreal_of_real z) = (number_of w < z)"; |
|
595 |
by (stac (hypreal_of_real_less_iff RS sym) 1); |
|
596 |
by (Simp_tac 1); |
|
597 |
qed "number_of_less_hypreal_of_real_iff"; |
|
598 |
Addsimps [number_of_less_hypreal_of_real_iff]; |
|
599 |
||
600 |
Goal "(number_of w <= hypreal_of_real z) = (number_of w <= z)"; |
|
601 |
by (stac (hypreal_of_real_le_iff RS sym) 1); |
|
602 |
by (Simp_tac 1); |
|
603 |
qed "number_of_le_hypreal_of_real_iff"; |
|
604 |
Addsimps [number_of_le_hypreal_of_real_iff]; |
|
605 |
||
606 |
Goal "(hypreal_of_real z < number_of w) = (z < number_of w)"; |
|
607 |
by (stac (hypreal_of_real_less_iff RS sym) 1); |
|
608 |
by (Simp_tac 1); |
|
609 |
qed "hypreal_of_real_less_number_of_iff"; |
|
610 |
Addsimps [hypreal_of_real_less_number_of_iff]; |
|
611 |
||
612 |
Goal "(hypreal_of_real z <= number_of w) = (z <= number_of w)"; |
|
613 |
by (stac (hypreal_of_real_le_iff RS sym) 1); |
|
614 |
by (Simp_tac 1); |
|
615 |
qed "hypreal_of_real_le_number_of_iff"; |
|
616 |
Addsimps [hypreal_of_real_le_number_of_iff]; |
|
617 |
||
10784 | 618 |
(** <= monotonicity results: needed for arithmetic **) |
619 |
||
620 |
Goal "[| i <= j; (0::hypreal) <= k |] ==> i*k <= j*k"; |
|
621 |
by (auto_tac (claset(), |
|
622 |
simpset() addsimps [order_le_less, hypreal_mult_less_mono1])); |
|
623 |
qed "hypreal_mult_le_mono1"; |
|
624 |
||
625 |
Goal "[| i <= j; (0::hypreal) <= k |] ==> k*i <= k*j"; |
|
626 |
by (dtac hypreal_mult_le_mono1 1); |
|
627 |
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [hypreal_mult_commute]))); |
|
628 |
qed "hypreal_mult_le_mono2"; |
|
629 |
||
630 |
Goal "[| u <= v; x <= y; 0 <= v; (0::hypreal) <= x |] ==> u * x <= v * y"; |
|
631 |
by (etac (hypreal_mult_le_mono1 RS order_trans) 1); |
|
632 |
by (assume_tac 1); |
|
633 |
by (etac hypreal_mult_le_mono2 1); |
|
634 |
by (assume_tac 1); |
|
635 |
qed "hypreal_mult_le_mono"; |
|
636 |