author | paulson |
Tue, 10 Feb 2004 12:02:11 +0100 | |
changeset 14378 | 69c4d5997669 |
parent 14377 | f454b3004f8f |
permissions | -rw-r--r-- |
13957 | 1 |
(* Title: ComplexBin.ML |
2 |
Author: Jacques D. Fleuriot |
|
3 |
Copyright: 2001 University of Edinburgh |
|
4 |
Descrition: Binary arithmetic for the complex numbers |
|
5 |
*) |
|
6 |
||
7 |
(** complex_of_real (coercion from real to complex) **) |
|
8 |
||
9 |
Goal "complex_of_real (number_of w) = number_of w"; |
|
10 |
by (simp_tac (simpset() addsimps [complex_number_of_def]) 1); |
|
11 |
qed "complex_number_of"; |
|
12 |
Addsimps [complex_number_of]; |
|
13 |
||
14 |
Goalw [complex_number_of_def] "Numeral0 = (0::complex)"; |
|
15 |
by (Simp_tac 1); |
|
16 |
qed "complex_numeral_0_eq_0"; |
|
17 |
||
18 |
Goalw [complex_number_of_def] "Numeral1 = (1::complex)"; |
|
19 |
by (Simp_tac 1); |
|
20 |
qed "complex_numeral_1_eq_1"; |
|
21 |
||
22 |
(** Addition **) |
|
23 |
||
24 |
Goal "(number_of v :: complex) + number_of v' = number_of (bin_add v v')"; |
|
25 |
by (simp_tac |
|
26 |
(HOL_ss addsimps [complex_number_of_def, |
|
27 |
complex_of_real_add, add_real_number_of]) 1); |
|
28 |
qed "add_complex_number_of"; |
|
29 |
Addsimps [add_complex_number_of]; |
|
30 |
||
31 |
||
32 |
(** Subtraction **) |
|
33 |
||
34 |
Goalw [complex_number_of_def] |
|
35 |
"- (number_of w :: complex) = number_of (bin_minus w)"; |
|
36 |
by (simp_tac |
|
37 |
(HOL_ss addsimps [minus_real_number_of, complex_of_real_minus RS sym]) 1); |
|
38 |
qed "minus_complex_number_of"; |
|
39 |
Addsimps [minus_complex_number_of]; |
|
40 |
||
41 |
Goalw [complex_number_of_def, complex_diff_def] |
|
42 |
"(number_of v :: complex) - number_of w = number_of (bin_add v (bin_minus w))"; |
|
43 |
by (Simp_tac 1); |
|
44 |
qed "diff_complex_number_of"; |
|
45 |
Addsimps [diff_complex_number_of]; |
|
46 |
||
47 |
||
48 |
(** Multiplication **) |
|
49 |
||
50 |
Goal "(number_of v :: complex) * number_of v' = number_of (bin_mult v v')"; |
|
51 |
by (simp_tac |
|
52 |
(HOL_ss addsimps [complex_number_of_def, |
|
53 |
complex_of_real_mult, mult_real_number_of]) 1); |
|
54 |
qed "mult_complex_number_of"; |
|
55 |
Addsimps [mult_complex_number_of]; |
|
56 |
||
57 |
Goal "(2::complex) = 1 + 1"; |
|
58 |
by (simp_tac (simpset() addsimps [complex_numeral_1_eq_1 RS sym]) 1); |
|
59 |
val lemma = result(); |
|
60 |
||
61 |
(*For specialist use: NOT as default simprules*) |
|
62 |
Goal "2 * z = (z+z::complex)"; |
|
14373 | 63 |
by (simp_tac (simpset () addsimps [lemma, left_distrib]) 1); |
13957 | 64 |
qed "complex_mult_2"; |
65 |
||
66 |
Goal "z * 2 = (z+z::complex)"; |
|
14373 | 67 |
by (stac mult_commute 1 THEN rtac complex_mult_2 1); |
13957 | 68 |
qed "complex_mult_2_right"; |
69 |
||
70 |
(** Equals (=) **) |
|
71 |
||
72 |
Goal "((number_of v :: complex) = number_of v') = \ |
|
14378
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14377
diff
changeset
|
73 |
\ iszero (number_of (bin_add v (bin_minus v')) :: int)"; |
13957 | 74 |
by (simp_tac |
75 |
(HOL_ss addsimps [complex_number_of_def, |
|
76 |
complex_of_real_eq_iff, eq_real_number_of]) 1); |
|
77 |
qed "eq_complex_number_of"; |
|
78 |
Addsimps [eq_complex_number_of]; |
|
79 |
||
80 |
(*** New versions of existing theorems involving 0, 1 ***) |
|
81 |
||
82 |
Goal "- 1 = (-1::complex)"; |
|
83 |
by (simp_tac (simpset() addsimps [complex_numeral_1_eq_1 RS sym]) 1); |
|
84 |
qed "complex_minus_1_eq_m1"; |
|
85 |
||
86 |
Goal "-1 * z = -(z::complex)"; |
|
87 |
by (simp_tac (simpset() addsimps [complex_minus_1_eq_m1 RS sym]) 1); |
|
88 |
qed "complex_mult_minus1"; |
|
89 |
||
90 |
Goal "z * -1 = -(z::complex)"; |
|
14373 | 91 |
by (stac mult_commute 1 THEN rtac complex_mult_minus1 1); |
13957 | 92 |
qed "complex_mult_minus1_right"; |
93 |
||
94 |
Addsimps [complex_mult_minus1,complex_mult_minus1_right]; |
|
95 |
||
96 |
||
97 |
(*Maps 0 to Numeral0 and 1 to Numeral1 and -Numeral1 to -1*) |
|
98 |
val complex_numeral_ss = |
|
99 |
hypreal_numeral_ss addsimps [complex_numeral_0_eq_0 RS sym, complex_numeral_1_eq_1 RS sym, |
|
100 |
complex_minus_1_eq_m1]; |
|
101 |
||
102 |
fun rename_numerals th = |
|
103 |
asm_full_simplify complex_numeral_ss (Thm.transfer (the_context ()) th); |
|
104 |
||
105 |
(*Now insert some identities previously stated for 0 and 1c*) |
|
106 |
||
107 |
Addsimps [complex_numeral_0_eq_0,complex_numeral_1_eq_1]; |
|
108 |
||
109 |
Goal "number_of v + (number_of w + z) = (number_of(bin_add v w) + z::complex)"; |
|
110 |
by (auto_tac (claset(),simpset() addsimps [complex_add_assoc RS sym])); |
|
111 |
qed "complex_add_number_of_left"; |
|
112 |
||
113 |
Goal "number_of v *(number_of w * z) = (number_of(bin_mult v w) * z::complex)"; |
|
14373 | 114 |
by (simp_tac (simpset() addsimps [mult_assoc RS sym]) 1); |
13957 | 115 |
qed "complex_mult_number_of_left"; |
116 |
||
117 |
Goalw [complex_diff_def] |
|
118 |
"number_of v + (number_of w - c) = number_of(bin_add v w) - (c::complex)"; |
|
119 |
by (rtac complex_add_number_of_left 1); |
|
120 |
qed "complex_add_number_of_diff1"; |
|
121 |
||
122 |
Goal "number_of v + (c - number_of w) = \ |
|
123 |
\ number_of (bin_add v (bin_minus w)) + (c::complex)"; |
|
14373 | 124 |
by (auto_tac (claset(),simpset() addsimps [complex_diff_def]@ add_ac)); |
13957 | 125 |
qed "complex_add_number_of_diff2"; |
126 |
||
127 |
Addsimps [complex_add_number_of_left, complex_mult_number_of_left, |
|
128 |
complex_add_number_of_diff1, complex_add_number_of_diff2]; |
|
129 |
||
130 |
||
131 |
(**** Simprocs for numeric literals ****) |
|
132 |
||
133 |
(** Combining of literal coefficients in sums of products **) |
|
134 |
||
135 |
Goal "(x = y) = (x-y = (0::complex))"; |
|
14373 | 136 |
by (simp_tac (simpset() addsimps [diff_eq_eq]) 1); |
13957 | 137 |
qed "complex_eq_iff_diff_eq_0"; |
138 |
||
139 |
||
140 |
||
141 |
structure Complex_Numeral_Simprocs = |
|
142 |
struct |
|
143 |
||
144 |
(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic in simprocs |
|
145 |
isn't complicated by the abstract 0 and 1.*) |
|
146 |
val numeral_syms = [complex_numeral_0_eq_0 RS sym, complex_numeral_1_eq_1 RS sym]; |
|
147 |
||
148 |
||
149 |
(*Utilities*) |
|
150 |
||
151 |
val complexT = Type("Complex.complex",[]); |
|
152 |
||
153 |
fun mk_numeral n = HOLogic.number_of_const complexT $ HOLogic.mk_bin n; |
|
154 |
||
155 |
val dest_numeral = Real_Numeral_Simprocs.dest_numeral; |
|
156 |
val find_first_numeral = Real_Numeral_Simprocs.find_first_numeral; |
|
157 |
||
158 |
val zero = mk_numeral 0; |
|
159 |
val mk_plus = HOLogic.mk_binop "op +"; |
|
160 |
||
161 |
val uminus_const = Const ("uminus", complexT --> complexT); |
|
162 |
||
163 |
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*) |
|
164 |
fun mk_sum [] = zero |
|
165 |
| mk_sum [t,u] = mk_plus (t, u) |
|
166 |
| mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
167 |
||
168 |
(*this version ALWAYS includes a trailing zero*) |
|
169 |
fun long_mk_sum [] = zero |
|
170 |
| long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
171 |
||
172 |
val dest_plus = HOLogic.dest_bin "op +" complexT; |
|
173 |
||
174 |
(*decompose additions AND subtractions as a sum*) |
|
175 |
fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) = |
|
176 |
dest_summing (pos, t, dest_summing (pos, u, ts)) |
|
177 |
| dest_summing (pos, Const ("op -", _) $ t $ u, ts) = |
|
178 |
dest_summing (pos, t, dest_summing (not pos, u, ts)) |
|
179 |
| dest_summing (pos, t, ts) = |
|
180 |
if pos then t::ts else uminus_const$t :: ts; |
|
181 |
||
182 |
fun dest_sum t = dest_summing (true, t, []); |
|
183 |
||
184 |
val mk_diff = HOLogic.mk_binop "op -"; |
|
185 |
val dest_diff = HOLogic.dest_bin "op -" complexT; |
|
186 |
||
187 |
val one = mk_numeral 1; |
|
188 |
val mk_times = HOLogic.mk_binop "op *"; |
|
189 |
||
190 |
fun mk_prod [] = one |
|
191 |
| mk_prod [t] = t |
|
192 |
| mk_prod (t :: ts) = if t = one then mk_prod ts |
|
193 |
else mk_times (t, mk_prod ts); |
|
194 |
||
195 |
val dest_times = HOLogic.dest_bin "op *" complexT; |
|
196 |
||
197 |
fun dest_prod t = |
|
198 |
let val (t,u) = dest_times t |
|
199 |
in dest_prod t @ dest_prod u end |
|
200 |
handle TERM _ => [t]; |
|
201 |
||
202 |
(*DON'T do the obvious simplifications; that would create special cases*) |
|
203 |
fun mk_coeff (k, ts) = mk_times (mk_numeral k, ts); |
|
204 |
||
205 |
(*Express t as a product of (possibly) a numeral with other sorted terms*) |
|
206 |
fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t |
|
207 |
| dest_coeff sign t = |
|
208 |
let val ts = sort Term.term_ord (dest_prod t) |
|
209 |
val (n, ts') = find_first_numeral [] ts |
|
210 |
handle TERM _ => (1, ts) |
|
211 |
in (sign*n, mk_prod ts') end; |
|
212 |
||
213 |
(*Find first coefficient-term THAT MATCHES u*) |
|
214 |
fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) |
|
215 |
| find_first_coeff past u (t::terms) = |
|
216 |
let val (n,u') = dest_coeff 1 t |
|
217 |
in if u aconv u' then (n, rev past @ terms) |
|
218 |
else find_first_coeff (t::past) u terms |
|
219 |
end |
|
220 |
handle TERM _ => find_first_coeff (t::past) u terms; |
|
221 |
||
222 |
||
223 |
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1*) |
|
224 |
val add_0s = map rename_numerals [complex_add_zero_left, complex_add_zero_right]; |
|
225 |
val mult_plus_1s = map rename_numerals [complex_mult_one_left, complex_mult_one_right]; |
|
226 |
val mult_minus_1s = map rename_numerals |
|
227 |
[complex_mult_minus1, complex_mult_minus1_right]; |
|
228 |
val mult_1s = mult_plus_1s @ mult_minus_1s; |
|
229 |
||
230 |
(*To perform binary arithmetic*) |
|
231 |
val bin_simps = |
|
232 |
[complex_numeral_0_eq_0 RS sym, complex_numeral_1_eq_1 RS sym, |
|
233 |
add_complex_number_of, complex_add_number_of_left, |
|
234 |
minus_complex_number_of, diff_complex_number_of, mult_complex_number_of, |
|
235 |
complex_mult_number_of_left] @ bin_arith_simps @ bin_rel_simps; |
|
236 |
||
14123 | 237 |
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms |
238 |
during re-arrangement*) |
|
239 |
val non_add_bin_simps = |
|
240 |
bin_simps \\ [complex_add_number_of_left, add_complex_number_of]; |
|
241 |
||
13957 | 242 |
(*To evaluate binary negations of coefficients*) |
243 |
val complex_minus_simps = NCons_simps @ |
|
244 |
[complex_minus_1_eq_m1,minus_complex_number_of, |
|
245 |
bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min, |
|
246 |
bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min]; |
|
247 |
||
248 |
(*To let us treat subtraction as addition*) |
|
14373 | 249 |
val diff_simps = [complex_diff_def, minus_add_distrib, minus_minus]; |
13957 | 250 |
|
251 |
(* push the unary minus down: - x * y = x * - y *) |
|
252 |
val complex_minus_mult_eq_1_to_2 = |
|
14373 | 253 |
[minus_mult_left RS sym, minus_mult_right] MRS trans |
13957 | 254 |
|> standard; |
255 |
||
256 |
(*to extract again any uncancelled minuses*) |
|
257 |
val complex_minus_from_mult_simps = |
|
14373 | 258 |
[minus_minus, minus_mult_left RS sym, minus_mult_right RS sym]; |
13957 | 259 |
|
260 |
(*combine unary minus with numeric literals, however nested within a product*) |
|
261 |
val complex_mult_minus_simps = |
|
14373 | 262 |
[mult_assoc, minus_mult_left, complex_minus_mult_eq_1_to_2]; |
13957 | 263 |
|
264 |
(*Final simplification: cancel + and * *) |
|
265 |
val simplify_meta_eq = |
|
266 |
Int_Numeral_Simprocs.simplify_meta_eq |
|
14373 | 267 |
[add_zero_left, add_zero_right, |
268 |
mult_zero_left, mult_zero_right, mult_1, mult_1_right]; |
|
13957 | 269 |
|
270 |
val prep_simproc = Real_Numeral_Simprocs.prep_simproc; |
|
271 |
||
272 |
||
273 |
structure CancelNumeralsCommon = |
|
274 |
struct |
|
275 |
val mk_sum = mk_sum |
|
276 |
val dest_sum = dest_sum |
|
277 |
val mk_coeff = mk_coeff |
|
278 |
val dest_coeff = dest_coeff 1 |
|
279 |
val find_first_coeff = find_first_coeff [] |
|
280 |
val trans_tac = Real_Numeral_Simprocs.trans_tac |
|
281 |
val norm_tac = |
|
282 |
ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@ |
|
14373 | 283 |
complex_minus_simps@add_ac)) |
14123 | 284 |
THEN ALLGOALS (simp_tac (HOL_ss addsimps non_add_bin_simps@complex_mult_minus_simps)) |
13957 | 285 |
THEN ALLGOALS |
286 |
(simp_tac (HOL_ss addsimps complex_minus_from_mult_simps@ |
|
14373 | 287 |
add_ac@mult_ac)) |
13957 | 288 |
val numeral_simp_tac = ALLGOALS (simp_tac (HOL_ss addsimps add_0s@bin_simps)) |
289 |
val simplify_meta_eq = simplify_meta_eq |
|
290 |
end; |
|
291 |
||
292 |
||
293 |
structure EqCancelNumerals = CancelNumeralsFun |
|
294 |
(open CancelNumeralsCommon |
|
295 |
val prove_conv = Bin_Simprocs.prove_conv |
|
296 |
val mk_bal = HOLogic.mk_eq |
|
297 |
val dest_bal = HOLogic.dest_bin "op =" complexT |
|
14373 | 298 |
val bal_add1 = eq_add_iff1 RS trans |
299 |
val bal_add2 = eq_add_iff2 RS trans |
|
13957 | 300 |
); |
301 |
||
302 |
||
303 |
val cancel_numerals = |
|
304 |
map prep_simproc |
|
305 |
[("complexeq_cancel_numerals", |
|
306 |
["(l::complex) + m = n", "(l::complex) = m + n", |
|
307 |
"(l::complex) - m = n", "(l::complex) = m - n", |
|
308 |
"(l::complex) * m = n", "(l::complex) = m * n"], |
|
309 |
EqCancelNumerals.proc)]; |
|
310 |
||
311 |
structure CombineNumeralsData = |
|
312 |
struct |
|
313 |
val add = op + : int*int -> int |
|
314 |
val mk_sum = long_mk_sum (*to work for e.g. #2*x + #3*x *) |
|
315 |
val dest_sum = dest_sum |
|
316 |
val mk_coeff = mk_coeff |
|
317 |
val dest_coeff = dest_coeff 1 |
|
14373 | 318 |
val left_distrib = combine_common_factor RS trans |
13957 | 319 |
val prove_conv = Bin_Simprocs.prove_conv_nohyps |
320 |
val trans_tac = Real_Numeral_Simprocs.trans_tac |
|
321 |
val norm_tac = |
|
322 |
ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@ |
|
14373 | 323 |
complex_minus_simps@add_ac)) |
14123 | 324 |
THEN ALLGOALS (simp_tac (HOL_ss addsimps non_add_bin_simps@complex_mult_minus_simps)) |
13957 | 325 |
THEN ALLGOALS (simp_tac (HOL_ss addsimps complex_minus_from_mult_simps@ |
14373 | 326 |
add_ac@mult_ac)) |
13957 | 327 |
val numeral_simp_tac = ALLGOALS |
328 |
(simp_tac (HOL_ss addsimps add_0s@bin_simps)) |
|
329 |
val simplify_meta_eq = simplify_meta_eq |
|
330 |
end; |
|
331 |
||
332 |
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); |
|
333 |
||
334 |
val combine_numerals = |
|
335 |
prep_simproc ("complex_combine_numerals", |
|
336 |
["(i::complex) + j", "(i::complex) - j"], |
|
337 |
CombineNumerals.proc); |
|
338 |
||
339 |
||
340 |
(** Declarations for ExtractCommonTerm **) |
|
341 |
||
342 |
(*this version ALWAYS includes a trailing one*) |
|
343 |
fun long_mk_prod [] = one |
|
344 |
| long_mk_prod (t :: ts) = mk_times (t, mk_prod ts); |
|
345 |
||
346 |
(*Find first term that matches u*) |
|
347 |
fun find_first past u [] = raise TERM("find_first", []) |
|
348 |
| find_first past u (t::terms) = |
|
349 |
if u aconv t then (rev past @ terms) |
|
350 |
else find_first (t::past) u terms |
|
351 |
handle TERM _ => find_first (t::past) u terms; |
|
352 |
||
353 |
(*Final simplification: cancel + and * *) |
|
354 |
fun cancel_simplify_meta_eq cancel_th th = |
|
355 |
Int_Numeral_Simprocs.simplify_meta_eq |
|
356 |
[complex_mult_one_left, complex_mult_one_right] |
|
357 |
(([th, cancel_th]) MRS trans); |
|
358 |
||
359 |
(*** Making constant folding work for 0 and 1 too ***) |
|
360 |
||
361 |
structure ComplexAbstractNumeralsData = |
|
362 |
struct |
|
363 |
val dest_eq = HOLogic.dest_eq o HOLogic.dest_Trueprop o concl_of |
|
364 |
val is_numeral = Bin_Simprocs.is_numeral |
|
365 |
val numeral_0_eq_0 = complex_numeral_0_eq_0 |
|
366 |
val numeral_1_eq_1 = complex_numeral_1_eq_1 |
|
367 |
val prove_conv = Bin_Simprocs.prove_conv_nohyps_novars |
|
368 |
fun norm_tac simps = ALLGOALS (simp_tac (HOL_ss addsimps simps)) |
|
369 |
val simplify_meta_eq = Bin_Simprocs.simplify_meta_eq |
|
14377 | 370 |
end; |
13957 | 371 |
|
14377 | 372 |
structure ComplexAbstractNumerals = AbstractNumeralsFun (ComplexAbstractNumeralsData); |
13957 | 373 |
|
374 |
(*For addition, we already have rules for the operand 0. |
|
375 |
Multiplication is omitted because there are already special rules for |
|
376 |
both 0 and 1 as operands. Unary minus is trivial, just have - 1 = -1. |
|
377 |
For the others, having three patterns is a compromise between just having |
|
378 |
one (many spurious calls) and having nine (just too many!) *) |
|
379 |
val eval_numerals = |
|
380 |
map prep_simproc |
|
381 |
[("complex_add_eval_numerals", |
|
382 |
["(m::complex) + 1", "(m::complex) + number_of v"], |
|
383 |
ComplexAbstractNumerals.proc add_complex_number_of), |
|
384 |
("complex_diff_eval_numerals", |
|
385 |
["(m::complex) - 1", "(m::complex) - number_of v"], |
|
386 |
ComplexAbstractNumerals.proc diff_complex_number_of), |
|
387 |
("complex_eq_eval_numerals", |
|
388 |
["(m::complex) = 0", "(m::complex) = 1", "(m::complex) = number_of v"], |
|
14377 | 389 |
ComplexAbstractNumerals.proc eq_complex_number_of)]; |
13957 | 390 |
|
391 |
end; |
|
392 |
||
393 |
Addsimprocs Complex_Numeral_Simprocs.eval_numerals; |
|
394 |
Addsimprocs Complex_Numeral_Simprocs.cancel_numerals; |
|
395 |
Addsimprocs [Complex_Numeral_Simprocs.combine_numerals]; |
|
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::complex)"; |
|
404 |
test " 2*u = (u::complex)"; |
|
405 |
test "(i + j + 12 + (k::complex)) - 15 = y"; |
|
406 |
test "(i + j + 12 + (k::complex)) - 5 = y"; |
|
407 |
||
408 |
test "( 2*x - (u*v) + y) - v* 3*u = (w::complex)"; |
|
409 |
test "( 2*x*u*v + (u*v)* 4 + y) - v*u* 4 = (w::complex)"; |
|
410 |
test "( 2*x*u*v + (u*v)* 4 + y) - v*u = (w::complex)"; |
|
411 |
test "u*v - (x*u*v + (u*v)* 4 + y) = (w::complex)"; |
|
412 |
||
413 |
test "(i + j + 12 + (k::complex)) = u + 15 + y"; |
|
414 |
test "(i + j* 2 + 12 + (k::complex)) = j + 5 + y"; |
|
415 |
||
416 |
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::complex)"; |
|
417 |
||
418 |
test "a + -(b+c) + b = (d::complex)"; |
|
419 |
test "a + -(b+c) - b = (d::complex)"; |
|
420 |
||
421 |
(*negative numerals*) |
|
422 |
test "(i + j + -2 + (k::complex)) - (u + 5 + y) = zz"; |
|
423 |
||
424 |
test "(i + j + -12 + (k::complex)) - 15 = y"; |
|
425 |
test "(i + j + 12 + (k::complex)) - -15 = y"; |
|
426 |
test "(i + j + -12 + (k::complex)) - -15 = y"; |
|
427 |
||
428 |
*) |
|
429 |
||
430 |
||
431 |
(** Constant folding for complex plus and times **) |
|
432 |
||
433 |
structure Complex_Times_Assoc_Data : ASSOC_FOLD_DATA = |
|
434 |
struct |
|
435 |
val ss = HOL_ss |
|
436 |
val eq_reflection = eq_reflection |
|
437 |
val sg_ref = Sign.self_ref (Theory.sign_of (the_context ())) |
|
438 |
val T = Complex_Numeral_Simprocs.complexT |
|
439 |
val plus = Const ("op *", [T,T] ---> T) |
|
14373 | 440 |
val add_ac = mult_ac |
13957 | 441 |
end; |
442 |
||
443 |
structure Complex_Times_Assoc = Assoc_Fold (Complex_Times_Assoc_Data); |
|
444 |
||
445 |
Addsimprocs [Complex_Times_Assoc.conv]; |
|
446 |
||
447 |
Addsimps [complex_of_real_zero_iff]; |
|
448 |
||
449 |
||
14377 | 450 |
(*Convert??? |
13957 | 451 |
Goalw [complex_number_of_def] |
452 |
"((number_of xa :: complex) + ii * number_of ya = \ |
|
453 |
\ number_of xb) = \ |
|
454 |
\ (((number_of xa :: complex) = number_of xb) & \ |
|
455 |
\ ((number_of ya :: complex) = 0))"; |
|
456 |
by (auto_tac (claset(), HOL_ss addsimps [complex_eq_cancel_iff2, |
|
457 |
complex_of_real_zero_iff])); |
|
458 |
qed "complex_number_of_eq_cancel_iff2"; |
|
459 |
Addsimps [complex_number_of_eq_cancel_iff2]; |
|
460 |
||
461 |
Goalw [complex_number_of_def] |
|
462 |
"((number_of xa :: complex) + number_of ya * ii = \ |
|
463 |
\ number_of xb) = \ |
|
464 |
\ (((number_of xa :: complex) = number_of xb) & \ |
|
465 |
\ ((number_of ya :: complex) = 0))"; |
|
466 |
by (auto_tac (claset(), HOL_ss addsimps [complex_eq_cancel_iff2a, |
|
467 |
complex_of_real_zero_iff])); |
|
468 |
qed "complex_number_of_eq_cancel_iff2a"; |
|
469 |
Addsimps [complex_number_of_eq_cancel_iff2a]; |
|
470 |
||
471 |
Goalw [complex_number_of_def] |
|
472 |
"((number_of xa :: complex) + ii * number_of ya = \ |
|
473 |
\ ii * number_of yb) = \ |
|
474 |
\ (((number_of xa :: complex) = 0) & \ |
|
475 |
\ ((number_of ya :: complex) = number_of yb))"; |
|
476 |
by (auto_tac (claset(), HOL_ss addsimps [complex_eq_cancel_iff3, |
|
477 |
complex_of_real_zero_iff])); |
|
478 |
qed "complex_number_of_eq_cancel_iff3"; |
|
479 |
Addsimps [complex_number_of_eq_cancel_iff3]; |
|
480 |
||
481 |
Goalw [complex_number_of_def] |
|
482 |
"((number_of xa :: complex) + number_of ya * ii= \ |
|
483 |
\ ii * number_of yb) = \ |
|
484 |
\ (((number_of xa :: complex) = 0) & \ |
|
485 |
\ ((number_of ya :: complex) = number_of yb))"; |
|
486 |
by (auto_tac (claset(), HOL_ss addsimps [complex_eq_cancel_iff3a, |
|
487 |
complex_of_real_zero_iff])); |
|
488 |
qed "complex_number_of_eq_cancel_iff3a"; |
|
489 |
Addsimps [complex_number_of_eq_cancel_iff3a]; |
|
14377 | 490 |
*) |
13957 | 491 |
|
492 |
Goalw [complex_number_of_def] "cnj (number_of v :: complex) = number_of v"; |
|
493 |
by (rtac complex_cnj_complex_of_real 1); |
|
494 |
qed "complex_number_of_cnj"; |
|
495 |
Addsimps [complex_number_of_cnj]; |
|
496 |
||
497 |
Goalw [complex_number_of_def] |
|
498 |
"cmod(number_of v :: complex) = abs (number_of v :: real)"; |
|
499 |
by (auto_tac (claset(), HOL_ss addsimps [complex_mod_complex_of_real])); |
|
500 |
qed "complex_number_of_cmod"; |
|
501 |
Addsimps [complex_number_of_cmod]; |
|
502 |
||
503 |
Goalw [complex_number_of_def] |
|
504 |
"Re(number_of v :: complex) = number_of v"; |
|
505 |
by (auto_tac (claset(), HOL_ss addsimps [Re_complex_of_real])); |
|
506 |
qed "complex_number_of_Re"; |
|
507 |
Addsimps [complex_number_of_Re]; |
|
508 |
||
509 |
Goalw [complex_number_of_def] |
|
510 |
"Im(number_of v :: complex) = 0"; |
|
511 |
by (auto_tac (claset(), HOL_ss addsimps [Im_complex_of_real])); |
|
512 |
qed "complex_number_of_Im"; |
|
513 |
Addsimps [complex_number_of_Im]; |
|
514 |
||
515 |
Goalw [expi_def] |
|
516 |
"expi((2::complex) * complex_of_real pi * ii) = 1"; |
|
517 |
by (auto_tac (claset(),simpset() addsimps [complex_Re_mult_eq, |
|
518 |
complex_Im_mult_eq,cis_def])); |
|
519 |
qed "expi_two_pi_i"; |
|
520 |
Addsimps [expi_two_pi_i]; |
|
521 |
||
522 |
(*examples: |
|
523 |
print_depth 22; |
|
524 |
set timing; |
|
525 |
set trace_simp; |
|
526 |
fun test s = (Goal s, by (Simp_tac 1)); |
|
527 |
||
528 |
test "23 * ii + 45 * ii= (x::complex)"; |
|
529 |
||
530 |
test "5 * ii + 12 - 45 * ii= (x::complex)"; |
|
531 |
test "5 * ii + 40 - 12 * ii + 9 = (x::complex) + 89 * ii"; |
|
532 |
test "5 * ii + 40 - 12 * ii + 9 - 78 = (x::complex) + 89 * ii"; |
|
533 |
||
534 |
test "l + 10 * ii + 90 + 3*l + 9 + 45 * ii= (x::complex)"; |
|
535 |
test "87 + 10 * ii + 90 + 3*7 + 9 + 45 * ii= (x::complex)"; |
|
536 |
||
537 |
||
538 |
*) |