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