13957
|
1 |
(* Title: NSComplexBin.ML
|
|
2 |
Author: Jacques D. Fleuriot
|
|
3 |
Copyright: 2001 University of Edinburgh
|
|
4 |
Descrition: Binary arithmetic for the nonstandard complex numbers
|
|
5 |
*)
|
|
6 |
|
|
7 |
(** hcomplex_of_complex (coercion from complex to nonstandard complex) **)
|
|
8 |
|
|
9 |
Goal "hcomplex_of_complex (number_of w) = number_of w";
|
|
10 |
by (simp_tac (simpset() addsimps [hcomplex_number_of_def]) 1);
|
|
11 |
qed "hcomplex_number_of";
|
|
12 |
Addsimps [hcomplex_number_of];
|
|
13 |
|
|
14 |
Goalw [hypreal_of_real_def]
|
|
15 |
"hcomplex_of_hypreal (hypreal_of_real x) = \
|
|
16 |
\ hcomplex_of_complex(complex_of_real x)";
|
|
17 |
by (simp_tac (simpset() addsimps [hcomplex_of_hypreal,
|
|
18 |
hcomplex_of_complex_def,complex_of_real_def]) 1);
|
|
19 |
qed "hcomplex_of_hypreal_eq_hcomplex_of_complex";
|
|
20 |
|
|
21 |
Goalw [complex_number_of_def,hypreal_number_of_def]
|
|
22 |
"hcomplex_of_complex (number_of w) = hcomplex_of_hypreal(number_of w)";
|
|
23 |
by (rtac (hcomplex_of_hypreal_eq_hcomplex_of_complex RS sym) 1);
|
|
24 |
qed "hcomplex_hypreal_number_of";
|
|
25 |
|
|
26 |
Goalw [hcomplex_number_of_def] "Numeral0 = (0::hcomplex)";
|
|
27 |
by (simp_tac (simpset() addsimps [hcomplex_of_complex_zero RS sym]) 1);
|
|
28 |
qed "hcomplex_numeral_0_eq_0";
|
|
29 |
|
|
30 |
Goalw [hcomplex_number_of_def] "Numeral1 = (1::hcomplex)";
|
|
31 |
by (simp_tac (simpset() addsimps [hcomplex_of_complex_one RS sym]) 1);
|
|
32 |
qed "hcomplex_numeral_1_eq_1";
|
|
33 |
|
|
34 |
(*
|
|
35 |
Goal "z + hcnj z = \
|
|
36 |
\ hcomplex_of_hypreal (2 * hRe(z))";
|
|
37 |
by (res_inst_tac [("z","z")] eq_Abs_hcomplex 1);
|
|
38 |
by (auto_tac (claset(),HOL_ss addsimps [hRe,hcnj,hcomplex_add,
|
|
39 |
hypreal_mult,hcomplex_of_hypreal,complex_add_cnj]));
|
|
40 |
qed "hcomplex_add_hcnj";
|
|
41 |
|
|
42 |
Goal "z - hcnj z = \
|
|
43 |
\ hcomplex_of_hypreal (hypreal_of_real #2 * hIm(z)) * iii";
|
|
44 |
by (res_inst_tac [("z","z")] eq_Abs_hcomplex 1);
|
|
45 |
by (auto_tac (claset(),simpset() addsimps [hIm,hcnj,hcomplex_diff,
|
|
46 |
hypreal_of_real_def,hypreal_mult,hcomplex_of_hypreal,
|
|
47 |
complex_diff_cnj,iii_def,hcomplex_mult]));
|
|
48 |
qed "hcomplex_diff_hcnj";
|
|
49 |
*)
|
|
50 |
|
|
51 |
(** Addition **)
|
|
52 |
|
|
53 |
Goal "(number_of v :: hcomplex) + number_of v' = number_of (bin_add v v')";
|
|
54 |
by (simp_tac
|
|
55 |
(HOL_ss addsimps [hcomplex_number_of_def,
|
|
56 |
hcomplex_of_complex_add RS sym, add_complex_number_of]) 1);
|
|
57 |
qed "add_hcomplex_number_of";
|
|
58 |
Addsimps [add_hcomplex_number_of];
|
|
59 |
|
|
60 |
|
|
61 |
(** Subtraction **)
|
|
62 |
|
|
63 |
Goalw [hcomplex_number_of_def]
|
|
64 |
"- (number_of w :: hcomplex) = number_of (bin_minus w)";
|
|
65 |
by (simp_tac
|
|
66 |
(HOL_ss addsimps [minus_complex_number_of, hcomplex_of_complex_minus RS sym]) 1);
|
|
67 |
qed "minus_hcomplex_number_of";
|
|
68 |
Addsimps [minus_hcomplex_number_of];
|
|
69 |
|
|
70 |
Goalw [hcomplex_number_of_def, hcomplex_diff_def]
|
|
71 |
"(number_of v :: hcomplex) - number_of w = \
|
|
72 |
\ number_of (bin_add v (bin_minus w))";
|
|
73 |
by (Simp_tac 1);
|
|
74 |
qed "diff_hcomplex_number_of";
|
|
75 |
Addsimps [diff_hcomplex_number_of];
|
|
76 |
|
|
77 |
|
|
78 |
(** Multiplication **)
|
|
79 |
|
|
80 |
Goal "(number_of v :: hcomplex) * number_of v' = number_of (bin_mult v v')";
|
|
81 |
by (simp_tac
|
|
82 |
(HOL_ss addsimps [hcomplex_number_of_def,
|
|
83 |
hcomplex_of_complex_mult RS sym, mult_complex_number_of]) 1);
|
|
84 |
qed "mult_hcomplex_number_of";
|
|
85 |
Addsimps [mult_hcomplex_number_of];
|
|
86 |
|
|
87 |
Goal "(2::hcomplex) = 1 + 1";
|
|
88 |
by (simp_tac (simpset() addsimps [hcomplex_numeral_1_eq_1 RS sym]) 1);
|
|
89 |
val lemma = result();
|
|
90 |
|
|
91 |
(*For specialist use: NOT as default simprules*)
|
|
92 |
Goal "2 * z = (z+z::hcomplex)";
|
|
93 |
by (simp_tac (simpset() addsimps [lemma, hcomplex_add_mult_distrib]) 1);
|
|
94 |
qed "hcomplex_mult_2";
|
|
95 |
|
|
96 |
Goal "z * 2 = (z+z::hcomplex)";
|
|
97 |
by (stac hcomplex_mult_commute 1 THEN rtac hcomplex_mult_2 1);
|
|
98 |
qed "hcomplex_mult_2_right";
|
|
99 |
|
|
100 |
(** Equals (=) **)
|
|
101 |
|
|
102 |
Goal "((number_of v :: hcomplex) = number_of v') = \
|
|
103 |
\ iszero (number_of (bin_add v (bin_minus v')))";
|
|
104 |
by (simp_tac
|
|
105 |
(HOL_ss addsimps [hcomplex_number_of_def,
|
|
106 |
hcomplex_of_complex_eq_iff, eq_complex_number_of]) 1);
|
|
107 |
qed "eq_hcomplex_number_of";
|
|
108 |
Addsimps [eq_hcomplex_number_of];
|
|
109 |
|
|
110 |
(*** New versions of existing theorems involving 0, 1hc ***)
|
|
111 |
|
|
112 |
Goal "- 1 = (-1::hcomplex)";
|
|
113 |
by (simp_tac (simpset() addsimps [hcomplex_numeral_1_eq_1 RS sym]) 1);
|
|
114 |
qed "hcomplex_minus_1_eq_m1";
|
|
115 |
|
|
116 |
Goal "-1 * z = -(z::hcomplex)";
|
|
117 |
by (simp_tac (simpset() addsimps [hcomplex_minus_1_eq_m1 RS sym]) 1);
|
|
118 |
qed "hcomplex_mult_minus1";
|
|
119 |
|
|
120 |
Goal "z * -1 = -(z::hcomplex)";
|
|
121 |
by (stac hcomplex_mult_commute 1 THEN rtac hcomplex_mult_minus1 1);
|
|
122 |
qed "hcomplex_mult_minus1_right";
|
|
123 |
|
|
124 |
Addsimps [hcomplex_mult_minus1,hcomplex_mult_minus1_right];
|
|
125 |
|
|
126 |
(*Maps 0 to Numeral0 and 1 to Numeral1 and -Numeral1 to -1*)
|
|
127 |
val hcomplex_numeral_ss =
|
|
128 |
complex_numeral_ss addsimps [hcomplex_numeral_0_eq_0 RS sym, hcomplex_numeral_1_eq_1 RS sym,
|
|
129 |
hcomplex_minus_1_eq_m1];
|
|
130 |
|
|
131 |
fun rename_numerals th =
|
|
132 |
asm_full_simplify hcomplex_numeral_ss (Thm.transfer (the_context ()) th);
|
|
133 |
|
|
134 |
|
|
135 |
(*Now insert some identities previously stated for 0 and 1hc*)
|
|
136 |
|
|
137 |
|
|
138 |
Addsimps [hcomplex_numeral_0_eq_0,hcomplex_numeral_1_eq_1];
|
|
139 |
|
|
140 |
Goal "number_of v + (number_of w + z) = (number_of(bin_add v w) + z::hcomplex)";
|
|
141 |
by (auto_tac (claset(),simpset() addsimps [hcomplex_add_assoc RS sym]));
|
|
142 |
qed "hcomplex_add_number_of_left";
|
|
143 |
|
|
144 |
Goal "number_of v *(number_of w * z) = (number_of(bin_mult v w) * z::hcomplex)";
|
|
145 |
by (simp_tac (simpset() addsimps [hcomplex_mult_assoc RS sym]) 1);
|
|
146 |
qed "hcomplex_mult_number_of_left";
|
|
147 |
|
|
148 |
Goalw [hcomplex_diff_def]
|
|
149 |
"number_of v + (number_of w - c) = number_of(bin_add v w) - (c::hcomplex)";
|
|
150 |
by (rtac hcomplex_add_number_of_left 1);
|
|
151 |
qed "hcomplex_add_number_of_diff1";
|
|
152 |
|
|
153 |
Goal "number_of v + (c - number_of w) = \
|
|
154 |
\ number_of (bin_add v (bin_minus w)) + (c::hcomplex)";
|
|
155 |
by (auto_tac (claset(),simpset() addsimps [hcomplex_diff_def]@ hcomplex_add_ac));
|
|
156 |
qed "hcomplex_add_number_of_diff2";
|
|
157 |
|
|
158 |
Addsimps [hcomplex_add_number_of_left, hcomplex_mult_number_of_left,
|
|
159 |
hcomplex_add_number_of_diff1, hcomplex_add_number_of_diff2];
|
|
160 |
|
|
161 |
|
|
162 |
(**** Simprocs for numeric literals ****)
|
|
163 |
|
|
164 |
(** Combining of literal coefficients in sums of products **)
|
|
165 |
|
|
166 |
Goal "(x = y) = (x-y = (0::hcomplex))";
|
|
167 |
by (simp_tac (simpset() addsimps [hcomplex_diff_eq_eq]) 1);
|
|
168 |
qed "hcomplex_eq_iff_diff_eq_0";
|
|
169 |
|
|
170 |
(** For combine_numerals **)
|
|
171 |
|
|
172 |
Goal "i*u + (j*u + k) = (i+j)*u + (k::hcomplex)";
|
|
173 |
by (asm_simp_tac (simpset() addsimps [hcomplex_add_mult_distrib]
|
|
174 |
@ hcomplex_add_ac) 1);
|
|
175 |
qed "left_hcomplex_add_mult_distrib";
|
|
176 |
|
|
177 |
(** For cancel_numerals **)
|
|
178 |
|
|
179 |
Goal "((x::hcomplex) = u + v) = (x - (u + v) = 0)";
|
|
180 |
by (auto_tac (claset(),simpset() addsimps [hcomplex_diff_eq_eq]));
|
|
181 |
qed "hcomplex_eq_add_diff_eq_0";
|
|
182 |
|
|
183 |
Goal "((x::hcomplex) = n) = (x - n = 0)";
|
|
184 |
by (auto_tac (claset(),simpset() addsimps [hcomplex_diff_eq_eq]));
|
|
185 |
qed "hcomplex_eq_diff_eq_0";
|
|
186 |
|
|
187 |
val hcomplex_rel_iff_rel_0_rls = [hcomplex_eq_diff_eq_0,hcomplex_eq_add_diff_eq_0];
|
|
188 |
|
|
189 |
Goal "!!i::hcomplex. (i*u + m = j*u + n) = ((i-j)*u + m = n)";
|
|
190 |
by (auto_tac (claset(), simpset() addsimps [hcomplex_add_mult_distrib,
|
|
191 |
hcomplex_diff_def] @ hcomplex_add_ac));
|
|
192 |
by (asm_simp_tac (simpset() addsimps [hcomplex_add_assoc RS sym]) 1);
|
|
193 |
by (simp_tac (simpset() addsimps [hcomplex_add_assoc]) 1);
|
|
194 |
qed "hcomplex_eq_add_iff1";
|
|
195 |
|
|
196 |
Goal "!!i::hcomplex. (i*u + m = j*u + n) = (m = (j-i)*u + n)";
|
|
197 |
by (res_inst_tac [("z","i")] eq_Abs_hcomplex 1);
|
|
198 |
by (res_inst_tac [("z","j")] eq_Abs_hcomplex 1);
|
|
199 |
by (res_inst_tac [("z","u")] eq_Abs_hcomplex 1);
|
|
200 |
by (res_inst_tac [("z","m")] eq_Abs_hcomplex 1);
|
|
201 |
by (res_inst_tac [("z","n")] eq_Abs_hcomplex 1);
|
|
202 |
by (auto_tac (claset(), simpset() addsimps [hcomplex_diff,hcomplex_add,
|
|
203 |
hcomplex_mult,complex_eq_add_iff2]));
|
|
204 |
qed "hcomplex_eq_add_iff2";
|
|
205 |
|
|
206 |
|
|
207 |
structure HComplex_Numeral_Simprocs =
|
|
208 |
struct
|
|
209 |
|
|
210 |
(*Utilities*)
|
|
211 |
|
|
212 |
val hcomplexT = Type("NSComplex.hcomplex",[]);
|
|
213 |
|
|
214 |
fun mk_numeral n = HOLogic.number_of_const hcomplexT $ HOLogic.mk_bin n;
|
|
215 |
|
|
216 |
val dest_numeral = Complex_Numeral_Simprocs.dest_numeral;
|
|
217 |
|
|
218 |
val find_first_numeral = Complex_Numeral_Simprocs.find_first_numeral;
|
|
219 |
|
|
220 |
val zero = mk_numeral 0;
|
|
221 |
val mk_plus = HOLogic.mk_binop "op +";
|
|
222 |
|
|
223 |
val uminus_const = Const ("uminus", hcomplexT --> hcomplexT);
|
|
224 |
|
|
225 |
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
|
|
226 |
fun mk_sum [] = zero
|
|
227 |
| mk_sum [t,u] = mk_plus (t, u)
|
|
228 |
| mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
|
|
229 |
|
|
230 |
(*this version ALWAYS includes a trailing zero*)
|
|
231 |
fun long_mk_sum [] = zero
|
|
232 |
| long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
|
|
233 |
|
|
234 |
val dest_plus = HOLogic.dest_bin "op +" hcomplexT;
|
|
235 |
|
|
236 |
(*decompose additions AND subtractions as a sum*)
|
|
237 |
fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) =
|
|
238 |
dest_summing (pos, t, dest_summing (pos, u, ts))
|
|
239 |
| dest_summing (pos, Const ("op -", _) $ t $ u, ts) =
|
|
240 |
dest_summing (pos, t, dest_summing (not pos, u, ts))
|
|
241 |
| dest_summing (pos, t, ts) =
|
|
242 |
if pos then t::ts else uminus_const$t :: ts;
|
|
243 |
|
|
244 |
fun dest_sum t = dest_summing (true, t, []);
|
|
245 |
|
|
246 |
val mk_diff = HOLogic.mk_binop "op -";
|
|
247 |
val dest_diff = HOLogic.dest_bin "op -" hcomplexT;
|
|
248 |
|
|
249 |
val one = mk_numeral 1;
|
|
250 |
val mk_times = HOLogic.mk_binop "op *";
|
|
251 |
|
|
252 |
fun mk_prod [] = one
|
|
253 |
| mk_prod [t] = t
|
|
254 |
| mk_prod (t :: ts) = if t = one then mk_prod ts
|
|
255 |
else mk_times (t, mk_prod ts);
|
|
256 |
|
|
257 |
val dest_times = HOLogic.dest_bin "op *" hcomplexT;
|
|
258 |
|
|
259 |
fun dest_prod t =
|
|
260 |
let val (t,u) = dest_times t
|
|
261 |
in dest_prod t @ dest_prod u end
|
|
262 |
handle TERM _ => [t];
|
|
263 |
|
|
264 |
(*DON'T do the obvious simplifications; that would create special cases*)
|
|
265 |
fun mk_coeff (k, ts) = mk_times (mk_numeral k, ts);
|
|
266 |
|
|
267 |
(*Express t as a product of (possibly) a numeral with other sorted terms*)
|
|
268 |
fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t
|
|
269 |
| dest_coeff sign t =
|
|
270 |
let val ts = sort Term.term_ord (dest_prod t)
|
|
271 |
val (n, ts') = find_first_numeral [] ts
|
|
272 |
handle TERM _ => (1, ts)
|
|
273 |
in (sign*n, mk_prod ts') end;
|
|
274 |
|
|
275 |
(*Find first coefficient-term THAT MATCHES u*)
|
|
276 |
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
|
|
277 |
| find_first_coeff past u (t::terms) =
|
|
278 |
let val (n,u') = dest_coeff 1 t
|
|
279 |
in if u aconv u' then (n, rev past @ terms)
|
|
280 |
else find_first_coeff (t::past) u terms
|
|
281 |
end
|
|
282 |
handle TERM _ => find_first_coeff (t::past) u terms;
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1*)
|
|
287 |
val add_0s = map rename_numerals [hcomplex_add_zero_left, hcomplex_add_zero_right];
|
|
288 |
val mult_plus_1s = map rename_numerals [hcomplex_mult_one_left, hcomplex_mult_one_right];
|
|
289 |
val mult_minus_1s = map rename_numerals [hcomplex_mult_minus1, hcomplex_mult_minus1_right];
|
|
290 |
val mult_1s = mult_plus_1s @ mult_minus_1s;
|
|
291 |
|
|
292 |
(*To perform binary arithmetic*)
|
|
293 |
val bin_simps =
|
|
294 |
[hcomplex_numeral_0_eq_0 RS sym, hcomplex_numeral_1_eq_1 RS sym,
|
|
295 |
add_hcomplex_number_of, hcomplex_add_number_of_left,
|
|
296 |
minus_hcomplex_number_of, diff_hcomplex_number_of, mult_hcomplex_number_of,
|
|
297 |
hcomplex_mult_number_of_left] @ bin_arith_simps @ bin_rel_simps;
|
|
298 |
|
14123
|
299 |
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
|
|
300 |
during re-arrangement*)
|
|
301 |
val non_add_bin_simps =
|
|
302 |
bin_simps \\ [hcomplex_add_number_of_left, add_hcomplex_number_of];
|
|
303 |
|
13957
|
304 |
(*To evaluate binary negations of coefficients*)
|
|
305 |
val hcomplex_minus_simps = NCons_simps @
|
|
306 |
[hcomplex_minus_1_eq_m1,minus_hcomplex_number_of,
|
|
307 |
bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min,
|
|
308 |
bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min];
|
|
309 |
|
|
310 |
|
|
311 |
(*To let us treat subtraction as addition*)
|
|
312 |
val diff_simps = [hcomplex_diff_def, hcomplex_minus_add_distrib,
|
14318
|
313 |
minus_minus];
|
13957
|
314 |
|
|
315 |
(*push the unary minus down: - x * y = x * - y *)
|
|
316 |
val hcomplex_minus_mult_eq_1_to_2 =
|
|
317 |
[hcomplex_minus_mult_eq1 RS sym, hcomplex_minus_mult_eq2] MRS trans
|
|
318 |
|> standard;
|
|
319 |
|
|
320 |
(*to extract again any uncancelled minuses*)
|
|
321 |
val hcomplex_minus_from_mult_simps =
|
14318
|
322 |
[minus_minus, hcomplex_minus_mult_eq1 RS sym,
|
13957
|
323 |
hcomplex_minus_mult_eq2 RS sym];
|
|
324 |
|
|
325 |
(*combine unary minus with numeric literals, however nested within a product*)
|
|
326 |
val hcomplex_mult_minus_simps =
|
|
327 |
[hcomplex_mult_assoc, hcomplex_minus_mult_eq1, hcomplex_minus_mult_eq_1_to_2];
|
|
328 |
|
|
329 |
(*Final simplification: cancel + and * *)
|
|
330 |
val simplify_meta_eq =
|
|
331 |
Int_Numeral_Simprocs.simplify_meta_eq
|
|
332 |
[hcomplex_add_zero_left, hcomplex_add_zero_right,
|
|
333 |
hcomplex_mult_zero_left, hcomplex_mult_zero_right, hcomplex_mult_one_left,
|
|
334 |
hcomplex_mult_one_right];
|
|
335 |
|
|
336 |
val prep_simproc = Complex_Numeral_Simprocs.prep_simproc;
|
|
337 |
|
|
338 |
|
|
339 |
structure CancelNumeralsCommon =
|
|
340 |
struct
|
|
341 |
val mk_sum = mk_sum
|
|
342 |
val dest_sum = dest_sum
|
|
343 |
val mk_coeff = mk_coeff
|
|
344 |
val dest_coeff = dest_coeff 1
|
|
345 |
val find_first_coeff = find_first_coeff []
|
|
346 |
val trans_tac = Real_Numeral_Simprocs.trans_tac
|
|
347 |
val norm_tac =
|
|
348 |
ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
|
|
349 |
hcomplex_minus_simps@hcomplex_add_ac))
|
14123
|
350 |
THEN ALLGOALS (simp_tac (HOL_ss addsimps non_add_bin_simps@hcomplex_mult_minus_simps))
|
13957
|
351 |
THEN ALLGOALS
|
|
352 |
(simp_tac (HOL_ss addsimps hcomplex_minus_from_mult_simps@
|
|
353 |
hcomplex_add_ac@hcomplex_mult_ac))
|
|
354 |
val numeral_simp_tac = ALLGOALS (simp_tac (HOL_ss addsimps add_0s@bin_simps))
|
|
355 |
val simplify_meta_eq = simplify_meta_eq
|
|
356 |
end;
|
|
357 |
|
|
358 |
|
|
359 |
structure EqCancelNumerals = CancelNumeralsFun
|
|
360 |
(open CancelNumeralsCommon
|
|
361 |
val prove_conv = Bin_Simprocs.prove_conv
|
|
362 |
val mk_bal = HOLogic.mk_eq
|
|
363 |
val dest_bal = HOLogic.dest_bin "op =" hcomplexT
|
|
364 |
val bal_add1 = hcomplex_eq_add_iff1 RS trans
|
|
365 |
val bal_add2 = hcomplex_eq_add_iff2 RS trans
|
|
366 |
);
|
|
367 |
|
|
368 |
|
|
369 |
val cancel_numerals =
|
|
370 |
map prep_simproc
|
|
371 |
[("hcomplexeq_cancel_numerals",
|
|
372 |
["(l::hcomplex) + m = n", "(l::hcomplex) = m + n",
|
|
373 |
"(l::hcomplex) - m = n", "(l::hcomplex) = m - n",
|
|
374 |
"(l::hcomplex) * m = n", "(l::hcomplex) = m * n"],
|
|
375 |
EqCancelNumerals.proc)];
|
|
376 |
|
|
377 |
structure CombineNumeralsData =
|
|
378 |
struct
|
|
379 |
val add = op + : int*int -> int
|
|
380 |
val mk_sum = long_mk_sum (*to work for e.g. #2*x + #3*x *)
|
|
381 |
val dest_sum = dest_sum
|
|
382 |
val mk_coeff = mk_coeff
|
|
383 |
val dest_coeff = dest_coeff 1
|
|
384 |
val left_distrib = left_hcomplex_add_mult_distrib RS trans
|
|
385 |
val prove_conv = Bin_Simprocs.prove_conv_nohyps
|
|
386 |
val trans_tac = Real_Numeral_Simprocs.trans_tac
|
|
387 |
val norm_tac =
|
|
388 |
ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
|
|
389 |
hcomplex_minus_simps@hcomplex_add_ac))
|
14123
|
390 |
THEN ALLGOALS (simp_tac (HOL_ss addsimps non_add_bin_simps@hcomplex_mult_minus_simps))
|
13957
|
391 |
THEN ALLGOALS (simp_tac (HOL_ss addsimps hcomplex_minus_from_mult_simps@
|
|
392 |
hcomplex_add_ac@hcomplex_mult_ac))
|
|
393 |
val numeral_simp_tac = ALLGOALS
|
|
394 |
(simp_tac (HOL_ss addsimps add_0s@bin_simps))
|
|
395 |
val simplify_meta_eq = simplify_meta_eq
|
|
396 |
end;
|
|
397 |
|
|
398 |
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
|
|
399 |
|
|
400 |
val combine_numerals =
|
|
401 |
prep_simproc ("hcomplex_combine_numerals",
|
|
402 |
["(i::hcomplex) + j", "(i::hcomplex) - j"],
|
|
403 |
CombineNumerals.proc);
|
|
404 |
|
|
405 |
(** Declarations for ExtractCommonTerm **)
|
|
406 |
|
|
407 |
(*this version ALWAYS includes a trailing one*)
|
|
408 |
fun long_mk_prod [] = one
|
|
409 |
| long_mk_prod (t :: ts) = mk_times (t, mk_prod ts);
|
|
410 |
|
|
411 |
(*Find first term that matches u*)
|
|
412 |
fun find_first past u [] = raise TERM("find_first", [])
|
|
413 |
| find_first past u (t::terms) =
|
|
414 |
if u aconv t then (rev past @ terms)
|
|
415 |
else find_first (t::past) u terms
|
|
416 |
handle TERM _ => find_first (t::past) u terms;
|
|
417 |
|
|
418 |
(*Final simplification: cancel + and * *)
|
|
419 |
fun cancel_simplify_meta_eq cancel_th th =
|
|
420 |
Int_Numeral_Simprocs.simplify_meta_eq
|
|
421 |
[hcomplex_mult_one_left, hcomplex_mult_one_right]
|
|
422 |
(([th, cancel_th]) MRS trans);
|
|
423 |
|
|
424 |
(*** Making constant folding work for 0 and 1 too ***)
|
|
425 |
|
|
426 |
structure HComplexAbstractNumeralsData =
|
|
427 |
struct
|
|
428 |
val dest_eq = HOLogic.dest_eq o HOLogic.dest_Trueprop o concl_of
|
|
429 |
val is_numeral = Bin_Simprocs.is_numeral
|
|
430 |
val numeral_0_eq_0 = hcomplex_numeral_0_eq_0
|
|
431 |
val numeral_1_eq_1 = hcomplex_numeral_1_eq_1
|
|
432 |
val prove_conv = Bin_Simprocs.prove_conv_nohyps_novars
|
|
433 |
fun norm_tac simps = ALLGOALS (simp_tac (HOL_ss addsimps simps))
|
|
434 |
val simplify_meta_eq = Bin_Simprocs.simplify_meta_eq
|
|
435 |
end
|
|
436 |
|
|
437 |
structure HComplexAbstractNumerals = AbstractNumeralsFun (HComplexAbstractNumeralsData)
|
|
438 |
|
|
439 |
(*For addition, we already have rules for the operand 0.
|
|
440 |
Multiplication is omitted because there are already special rules for
|
|
441 |
both 0 and 1 as operands. Unary minus is trivial, just have - 1 = -1.
|
|
442 |
For the others, having three patterns is a compromise between just having
|
|
443 |
one (many spurious calls) and having nine (just too many!) *)
|
|
444 |
val eval_numerals =
|
|
445 |
map prep_simproc
|
|
446 |
[("hcomplex_add_eval_numerals",
|
|
447 |
["(m::hcomplex) + 1", "(m::hcomplex) + number_of v"],
|
|
448 |
HComplexAbstractNumerals.proc add_hcomplex_number_of),
|
|
449 |
("hcomplex_diff_eval_numerals",
|
|
450 |
["(m::hcomplex) - 1", "(m::hcomplex) - number_of v"],
|
|
451 |
HComplexAbstractNumerals.proc diff_hcomplex_number_of),
|
|
452 |
("hcomplex_eq_eval_numerals",
|
|
453 |
["(m::hcomplex) = 0", "(m::hcomplex) = 1", "(m::hcomplex) = number_of v"],
|
|
454 |
HComplexAbstractNumerals.proc eq_hcomplex_number_of)]
|
|
455 |
|
|
456 |
end;
|
|
457 |
|
|
458 |
Addsimprocs HComplex_Numeral_Simprocs.eval_numerals;
|
|
459 |
Addsimprocs HComplex_Numeral_Simprocs.cancel_numerals;
|
|
460 |
Addsimprocs [HComplex_Numeral_Simprocs.combine_numerals];
|
|
461 |
|
|
462 |
|
|
463 |
(*examples:
|
|
464 |
print_depth 22;
|
|
465 |
set timing;
|
|
466 |
set trace_simp;
|
|
467 |
fun test s = (Goal s, by (Simp_tac 1));
|
|
468 |
|
|
469 |
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::hcomplex)";
|
|
470 |
test " 2*u = (u::hcomplex)";
|
|
471 |
test "(i + j + 12 + (k::hcomplex)) - 15 = y";
|
|
472 |
test "(i + j + 12 + (k::hcomplex)) - 5 = y";
|
|
473 |
|
|
474 |
test "( 2*x - (u*v) + y) - v* 3*u = (w::hcomplex)";
|
|
475 |
test "( 2*x*u*v + (u*v)* 4 + y) - v*u* 4 = (w::hcomplex)";
|
|
476 |
test "( 2*x*u*v + (u*v)* 4 + y) - v*u = (w::hcomplex)";
|
|
477 |
test "u*v - (x*u*v + (u*v)* 4 + y) = (w::hcomplex)";
|
|
478 |
|
|
479 |
test "(i + j + 12 + (k::hcomplex)) = u + 15 + y";
|
|
480 |
test "(i + j* 2 + 12 + (k::hcomplex)) = j + 5 + y";
|
|
481 |
|
|
482 |
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::hcomplex)";
|
|
483 |
|
|
484 |
test "a + -(b+c) + b = (d::hcomplex)";
|
|
485 |
test "a + -(b+c) - b = (d::hcomplex)";
|
|
486 |
|
|
487 |
(*negative numerals*)
|
|
488 |
test "(i + j + -2 + (k::hcomplex)) - (u + 5 + y) = zz";
|
|
489 |
|
|
490 |
test "(i + j + -12 + (k::hcomplex)) - 15 = y";
|
|
491 |
test "(i + j + 12 + (k::hcomplex)) - -15 = y";
|
|
492 |
test "(i + j + -12 + (k::hcomplex)) - -15 = y";
|
|
493 |
*)
|
|
494 |
|
|
495 |
(** Constant folding for hcomplex plus and times **)
|
|
496 |
|
|
497 |
structure HComplex_Times_Assoc_Data : ASSOC_FOLD_DATA =
|
|
498 |
struct
|
|
499 |
val ss = HOL_ss
|
|
500 |
val eq_reflection = eq_reflection
|
|
501 |
val sg_ref = Sign.self_ref (Theory.sign_of (the_context ()))
|
|
502 |
val T = HComplex_Numeral_Simprocs.hcomplexT
|
|
503 |
val plus = Const ("op *", [T,T] ---> T)
|
|
504 |
val add_ac = hcomplex_mult_ac
|
|
505 |
end;
|
|
506 |
|
|
507 |
structure HComplex_Times_Assoc = Assoc_Fold (HComplex_Times_Assoc_Data);
|
|
508 |
|
|
509 |
Addsimprocs [HComplex_Times_Assoc.conv];
|
|
510 |
|
|
511 |
Addsimps [hcomplex_of_complex_zero_iff];
|
|
512 |
|
|
513 |
(*Simplification of x-y = 0 *)
|
|
514 |
|
|
515 |
AddIffs [hcomplex_eq_iff_diff_eq_0 RS sym];
|
|
516 |
|
|
517 |
(** extra thms **)
|
|
518 |
|
|
519 |
Goal "(hcnj z = 0) = (z = 0)";
|
|
520 |
by (auto_tac (claset(),simpset() addsimps [hcomplex_hcnj_zero_iff]));
|
|
521 |
qed "hcomplex_hcnj_num_zero_iff";
|
|
522 |
Addsimps [hcomplex_hcnj_num_zero_iff];
|
|
523 |
|
|
524 |
Goal "0 = Abs_hcomplex (hcomplexrel `` {%n. 0})";
|
|
525 |
by (simp_tac (simpset() addsimps [hcomplex_zero_def RS meta_eq_to_obj_eq RS sym]) 1);
|
|
526 |
qed "hcomplex_zero_num";
|
|
527 |
|
|
528 |
Goal "1 = Abs_hcomplex (hcomplexrel `` {%n. 1})";
|
|
529 |
by (simp_tac (simpset() addsimps [hcomplex_one_def RS meta_eq_to_obj_eq RS sym]) 1);
|
|
530 |
qed "hcomplex_one_num";
|
|
531 |
|
|
532 |
(*** Real and imaginary stuff ***)
|
|
533 |
|
|
534 |
Goalw [hcomplex_number_of_def]
|
|
535 |
"((number_of xa :: hcomplex) + iii * number_of ya = \
|
|
536 |
\ number_of xb + iii * number_of yb) = \
|
|
537 |
\ (((number_of xa :: hcomplex) = number_of xb) & \
|
|
538 |
\ ((number_of ya :: hcomplex) = number_of yb))";
|
|
539 |
by (auto_tac (claset(), HOL_ss addsimps [hcomplex_eq_cancel_iff,
|
|
540 |
hcomplex_hypreal_number_of]));
|
|
541 |
qed "hcomplex_number_of_eq_cancel_iff";
|
|
542 |
Addsimps [hcomplex_number_of_eq_cancel_iff];
|
|
543 |
|
|
544 |
Goalw [hcomplex_number_of_def]
|
|
545 |
"((number_of xa :: hcomplex) + number_of ya * iii = \
|
|
546 |
\ number_of xb + number_of yb * iii) = \
|
|
547 |
\ (((number_of xa :: hcomplex) = number_of xb) & \
|
|
548 |
\ ((number_of ya :: hcomplex) = number_of yb))";
|
|
549 |
by (auto_tac (claset(), HOL_ss addsimps [hcomplex_eq_cancel_iffA,
|
|
550 |
hcomplex_hypreal_number_of]));
|
|
551 |
qed "hcomplex_number_of_eq_cancel_iffA";
|
|
552 |
Addsimps [hcomplex_number_of_eq_cancel_iffA];
|
|
553 |
|
|
554 |
Goalw [hcomplex_number_of_def]
|
|
555 |
"((number_of xa :: hcomplex) + number_of ya * iii = \
|
|
556 |
\ number_of xb + iii * number_of yb) = \
|
|
557 |
\ (((number_of xa :: hcomplex) = number_of xb) & \
|
|
558 |
\ ((number_of ya :: hcomplex) = number_of yb))";
|
|
559 |
by (auto_tac (claset(), HOL_ss addsimps [hcomplex_eq_cancel_iffB,
|
|
560 |
hcomplex_hypreal_number_of]));
|
|
561 |
qed "hcomplex_number_of_eq_cancel_iffB";
|
|
562 |
Addsimps [hcomplex_number_of_eq_cancel_iffB];
|
|
563 |
|
|
564 |
Goalw [hcomplex_number_of_def]
|
|
565 |
"((number_of xa :: hcomplex) + iii * number_of ya = \
|
|
566 |
\ number_of xb + number_of yb * iii) = \
|
|
567 |
\ (((number_of xa :: hcomplex) = number_of xb) & \
|
|
568 |
\ ((number_of ya :: hcomplex) = number_of yb))";
|
|
569 |
by (auto_tac (claset(), HOL_ss addsimps [hcomplex_eq_cancel_iffC,
|
|
570 |
hcomplex_hypreal_number_of]));
|
|
571 |
qed "hcomplex_number_of_eq_cancel_iffC";
|
|
572 |
Addsimps [hcomplex_number_of_eq_cancel_iffC];
|
|
573 |
|
|
574 |
Goalw [hcomplex_number_of_def]
|
|
575 |
"((number_of xa :: hcomplex) + iii * number_of ya = \
|
|
576 |
\ number_of xb) = \
|
|
577 |
\ (((number_of xa :: hcomplex) = number_of xb) & \
|
|
578 |
\ ((number_of ya :: hcomplex) = 0))";
|
|
579 |
by (auto_tac (claset(), HOL_ss addsimps [hcomplex_eq_cancel_iff2,
|
|
580 |
hcomplex_hypreal_number_of,hcomplex_of_hypreal_zero_iff]));
|
|
581 |
qed "hcomplex_number_of_eq_cancel_iff2";
|
|
582 |
Addsimps [hcomplex_number_of_eq_cancel_iff2];
|
|
583 |
|
|
584 |
Goalw [hcomplex_number_of_def]
|
|
585 |
"((number_of xa :: hcomplex) + number_of ya * iii = \
|
|
586 |
\ number_of xb) = \
|
|
587 |
\ (((number_of xa :: hcomplex) = number_of xb) & \
|
|
588 |
\ ((number_of ya :: hcomplex) = 0))";
|
|
589 |
by (auto_tac (claset(), HOL_ss addsimps [hcomplex_eq_cancel_iff2a,
|
|
590 |
hcomplex_hypreal_number_of,hcomplex_of_hypreal_zero_iff]));
|
|
591 |
qed "hcomplex_number_of_eq_cancel_iff2a";
|
|
592 |
Addsimps [hcomplex_number_of_eq_cancel_iff2a];
|
|
593 |
|
|
594 |
Goalw [hcomplex_number_of_def]
|
|
595 |
"((number_of xa :: hcomplex) + iii * number_of ya = \
|
|
596 |
\ iii * number_of yb) = \
|
|
597 |
\ (((number_of xa :: hcomplex) = 0) & \
|
|
598 |
\ ((number_of ya :: hcomplex) = number_of yb))";
|
|
599 |
by (auto_tac (claset(), HOL_ss addsimps [hcomplex_eq_cancel_iff3,
|
|
600 |
hcomplex_hypreal_number_of,hcomplex_of_hypreal_zero_iff]));
|
|
601 |
qed "hcomplex_number_of_eq_cancel_iff3";
|
|
602 |
Addsimps [hcomplex_number_of_eq_cancel_iff3];
|
|
603 |
|
|
604 |
Goalw [hcomplex_number_of_def]
|
|
605 |
"((number_of xa :: hcomplex) + number_of ya * iii= \
|
|
606 |
\ iii * number_of yb) = \
|
|
607 |
\ (((number_of xa :: hcomplex) = 0) & \
|
|
608 |
\ ((number_of ya :: hcomplex) = number_of yb))";
|
|
609 |
by (auto_tac (claset(), HOL_ss addsimps [hcomplex_eq_cancel_iff3a,
|
|
610 |
hcomplex_hypreal_number_of,hcomplex_of_hypreal_zero_iff]));
|
|
611 |
qed "hcomplex_number_of_eq_cancel_iff3a";
|
|
612 |
Addsimps [hcomplex_number_of_eq_cancel_iff3a];
|
|
613 |
|
|
614 |
Goalw [hcomplex_number_of_def] "hcnj (number_of v :: hcomplex) = number_of v";
|
|
615 |
by (rtac (hcomplex_hypreal_number_of RS ssubst) 1);
|
|
616 |
by (rtac hcomplex_hcnj_hcomplex_of_hypreal 1);
|
|
617 |
qed "hcomplex_number_of_hcnj";
|
|
618 |
Addsimps [hcomplex_number_of_hcnj];
|
|
619 |
|
|
620 |
Goalw [hcomplex_number_of_def]
|
|
621 |
"hcmod(number_of v :: hcomplex) = abs (number_of v :: hypreal)";
|
|
622 |
by (rtac (hcomplex_hypreal_number_of RS ssubst) 1);
|
|
623 |
by (auto_tac (claset(), HOL_ss addsimps [hcmod_hcomplex_of_hypreal]));
|
|
624 |
qed "hcomplex_number_of_hcmod";
|
|
625 |
Addsimps [hcomplex_number_of_hcmod];
|
|
626 |
|
|
627 |
Goalw [hcomplex_number_of_def]
|
|
628 |
"hRe(number_of v :: hcomplex) = number_of v";
|
|
629 |
by (rtac (hcomplex_hypreal_number_of RS ssubst) 1);
|
|
630 |
by (auto_tac (claset(), HOL_ss addsimps [hRe_hcomplex_of_hypreal]));
|
|
631 |
qed "hcomplex_number_of_hRe";
|
|
632 |
Addsimps [hcomplex_number_of_hRe];
|
|
633 |
|
|
634 |
Goalw [hcomplex_number_of_def]
|
|
635 |
"hIm(number_of v :: hcomplex) = 0";
|
|
636 |
by (rtac (hcomplex_hypreal_number_of RS ssubst) 1);
|
|
637 |
by (auto_tac (claset(), HOL_ss addsimps [hIm_hcomplex_of_hypreal]));
|
|
638 |
qed "hcomplex_number_of_hIm";
|
|
639 |
Addsimps [hcomplex_number_of_hIm];
|
|
640 |
|
|
641 |
|
|
642 |
|