src/HOL/Tools/numeral_simprocs.ML
author haftmann
Fri Feb 05 14:33:50 2010 +0100 (2010-02-05)
changeset 35028 108662d50512
parent 34974 18b41bba42b5
child 35030 f2f1e50bf65d
permissions -rw-r--r--
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann@31068
     1
(* Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
haftmann@31068
     2
   Copyright   2000  University of Cambridge
wenzelm@23164
     3
haftmann@31068
     4
Simprocs for the integer numerals.
wenzelm@23164
     5
*)
wenzelm@23164
     6
wenzelm@23164
     7
(*To quote from Provers/Arith/cancel_numeral_factor.ML:
wenzelm@23164
     8
wenzelm@23164
     9
Cancels common coefficients in balanced expressions:
wenzelm@23164
    10
wenzelm@23164
    11
     u*#m ~~ u'*#m'  ==  #n*u ~~ #n'*u'
wenzelm@23164
    12
wenzelm@23164
    13
where ~~ is an appropriate balancing operation (e.g. =, <=, <, div, /)
wenzelm@23164
    14
and d = gcd(m,m') and n=m/d and n'=m'/d.
wenzelm@23164
    15
*)
wenzelm@23164
    16
haftmann@31068
    17
signature NUMERAL_SIMPROCS =
haftmann@31068
    18
sig
haftmann@31068
    19
  val assoc_fold_simproc: simproc
haftmann@31068
    20
  val combine_numerals: simproc
haftmann@31068
    21
  val cancel_numerals: simproc list
haftmann@31068
    22
  val cancel_factors: simproc list
haftmann@31068
    23
  val cancel_numeral_factors: simproc list
haftmann@31068
    24
  val field_combine_numerals: simproc
haftmann@31068
    25
  val field_cancel_numeral_factors: simproc list
haftmann@31068
    26
  val num_ss: simpset
haftmann@31068
    27
end;
haftmann@31068
    28
haftmann@31068
    29
structure Numeral_Simprocs : NUMERAL_SIMPROCS =
haftmann@31068
    30
struct
haftmann@31068
    31
haftmann@33359
    32
val mk_number = Arith_Data.mk_number;
haftmann@33359
    33
val mk_sum = Arith_Data.mk_sum;
haftmann@33359
    34
val long_mk_sum = Arith_Data.long_mk_sum;
haftmann@33359
    35
val dest_sum = Arith_Data.dest_sum;
haftmann@31068
    36
haftmann@34974
    37
val mk_diff = HOLogic.mk_binop @{const_name Algebras.minus};
haftmann@34974
    38
val dest_diff = HOLogic.dest_bin @{const_name Algebras.minus} Term.dummyT;
haftmann@31068
    39
haftmann@34974
    40
val mk_times = HOLogic.mk_binop @{const_name Algebras.times};
haftmann@31068
    41
haftmann@34974
    42
fun one_of T = Const(@{const_name Algebras.one}, T);
haftmann@31068
    43
haftmann@31068
    44
(* build product with trailing 1 rather than Numeral 1 in order to avoid the
haftmann@31068
    45
   unnecessary restriction to type class number_ring
haftmann@31068
    46
   which is not required for cancellation of common factors in divisions.
haftmann@31068
    47
*)
haftmann@31068
    48
fun mk_prod T = 
haftmann@31068
    49
  let val one = one_of T
haftmann@31068
    50
  fun mk [] = one
haftmann@31068
    51
    | mk [t] = t
haftmann@31068
    52
    | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)
haftmann@31068
    53
  in mk end;
haftmann@31068
    54
haftmann@31068
    55
(*This version ALWAYS includes a trailing one*)
haftmann@31068
    56
fun long_mk_prod T []        = one_of T
haftmann@31068
    57
  | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);
haftmann@31068
    58
haftmann@34974
    59
val dest_times = HOLogic.dest_bin @{const_name Algebras.times} Term.dummyT;
haftmann@31068
    60
haftmann@31068
    61
fun dest_prod t =
haftmann@31068
    62
      let val (t,u) = dest_times t
haftmann@31068
    63
      in dest_prod t @ dest_prod u end
haftmann@31068
    64
      handle TERM _ => [t];
haftmann@31068
    65
haftmann@33359
    66
fun find_first_numeral past (t::terms) =
haftmann@33359
    67
        ((snd (HOLogic.dest_number t), rev past @ terms)
haftmann@33359
    68
         handle TERM _ => find_first_numeral (t::past) terms)
haftmann@33359
    69
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
haftmann@33359
    70
haftmann@31068
    71
(*DON'T do the obvious simplifications; that would create special cases*)
haftmann@31068
    72
fun mk_coeff (k, t) = mk_times (mk_number (Term.fastype_of t) k, t);
haftmann@31068
    73
haftmann@31068
    74
(*Express t as a product of (possibly) a numeral with other sorted terms*)
haftmann@34974
    75
fun dest_coeff sign (Const (@{const_name Algebras.uminus}, _) $ t) = dest_coeff (~sign) t
haftmann@31068
    76
  | dest_coeff sign t =
haftmann@31068
    77
    let val ts = sort TermOrd.term_ord (dest_prod t)
haftmann@31068
    78
        val (n, ts') = find_first_numeral [] ts
haftmann@31068
    79
                          handle TERM _ => (1, ts)
haftmann@31068
    80
    in (sign*n, mk_prod (Term.fastype_of t) ts') end;
haftmann@31068
    81
haftmann@31068
    82
(*Find first coefficient-term THAT MATCHES u*)
haftmann@31068
    83
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
haftmann@31068
    84
  | find_first_coeff past u (t::terms) =
haftmann@31068
    85
        let val (n,u') = dest_coeff 1 t
haftmann@31068
    86
        in if u aconv u' then (n, rev past @ terms)
haftmann@31068
    87
                         else find_first_coeff (t::past) u terms
haftmann@31068
    88
        end
haftmann@31068
    89
        handle TERM _ => find_first_coeff (t::past) u terms;
haftmann@31068
    90
haftmann@31068
    91
(*Fractions as pairs of ints. Can't use Rat.rat because the representation
haftmann@31068
    92
  needs to preserve negative values in the denominator.*)
haftmann@31068
    93
fun mk_frac (p, q) = if q = 0 then raise Div else (p, q);
haftmann@31068
    94
haftmann@31068
    95
(*Don't reduce fractions; sums must be proved by rule add_frac_eq.
haftmann@31068
    96
  Fractions are reduced later by the cancel_numeral_factor simproc.*)
haftmann@31068
    97
fun add_frac ((p1, q1), (p2, q2)) = (p1 * q2 + p2 * q1, q1 * q2);
haftmann@31068
    98
haftmann@34974
    99
val mk_divide = HOLogic.mk_binop @{const_name Algebras.divide};
haftmann@31068
   100
haftmann@31068
   101
(*Build term (p / q) * t*)
haftmann@31068
   102
fun mk_fcoeff ((p, q), t) =
haftmann@31068
   103
  let val T = Term.fastype_of t
haftmann@31068
   104
  in mk_times (mk_divide (mk_number T p, mk_number T q), t) end;
haftmann@31068
   105
haftmann@31068
   106
(*Express t as a product of a fraction with other sorted terms*)
haftmann@34974
   107
fun dest_fcoeff sign (Const (@{const_name Algebras.uminus}, _) $ t) = dest_fcoeff (~sign) t
haftmann@34974
   108
  | dest_fcoeff sign (Const (@{const_name Algebras.divide}, _) $ t $ u) =
haftmann@31068
   109
    let val (p, t') = dest_coeff sign t
haftmann@31068
   110
        val (q, u') = dest_coeff 1 u
haftmann@31068
   111
    in (mk_frac (p, q), mk_divide (t', u')) end
haftmann@31068
   112
  | dest_fcoeff sign t =
haftmann@31068
   113
    let val (p, t') = dest_coeff sign t
haftmann@31068
   114
        val T = Term.fastype_of t
haftmann@31068
   115
    in (mk_frac (p, 1), mk_divide (t', one_of T)) end;
haftmann@31068
   116
haftmann@31068
   117
haftmann@31068
   118
(** New term ordering so that AC-rewriting brings numerals to the front **)
haftmann@31068
   119
haftmann@31068
   120
(*Order integers by absolute value and then by sign. The standard integer
haftmann@31068
   121
  ordering is not well-founded.*)
haftmann@31068
   122
fun num_ord (i,j) =
haftmann@31068
   123
  (case int_ord (abs i, abs j) of
haftmann@31068
   124
    EQUAL => int_ord (Int.sign i, Int.sign j) 
haftmann@31068
   125
  | ord => ord);
haftmann@31068
   126
haftmann@31068
   127
(*This resembles TermOrd.term_ord, but it puts binary numerals before other
haftmann@31068
   128
  non-atomic terms.*)
haftmann@31068
   129
local open Term 
haftmann@31068
   130
in 
haftmann@31068
   131
fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =
haftmann@31068
   132
      (case numterm_ord (t, u) of EQUAL => TermOrd.typ_ord (T, U) | ord => ord)
haftmann@31068
   133
  | numterm_ord
haftmann@31068
   134
     (Const(@{const_name Int.number_of}, _) $ v, Const(@{const_name Int.number_of}, _) $ w) =
haftmann@31068
   135
     num_ord (HOLogic.dest_numeral v, HOLogic.dest_numeral w)
haftmann@31068
   136
  | numterm_ord (Const(@{const_name Int.number_of}, _) $ _, _) = LESS
haftmann@31068
   137
  | numterm_ord (_, Const(@{const_name Int.number_of}, _) $ _) = GREATER
haftmann@31068
   138
  | numterm_ord (t, u) =
haftmann@31068
   139
      (case int_ord (size_of_term t, size_of_term u) of
haftmann@31068
   140
        EQUAL =>
haftmann@31068
   141
          let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
haftmann@31068
   142
            (case TermOrd.hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)
haftmann@31068
   143
          end
haftmann@31068
   144
      | ord => ord)
haftmann@31068
   145
and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
haftmann@31068
   146
end;
haftmann@31068
   147
haftmann@31068
   148
fun numtermless tu = (numterm_ord tu = LESS);
haftmann@31068
   149
haftmann@31068
   150
val num_ss = HOL_ss settermless numtermless;
haftmann@31068
   151
haftmann@31068
   152
(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic isn't complicated by the abstract 0 and 1.*)
haftmann@31068
   153
val numeral_syms = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym];
haftmann@31068
   154
haftmann@31068
   155
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
haftmann@31068
   156
val add_0s =  @{thms add_0s};
haftmann@31068
   157
val mult_1s = @{thms mult_1s mult_1_left mult_1_right divide_1};
haftmann@31068
   158
haftmann@31068
   159
(*Simplify inverse Numeral1, a/Numeral1*)
haftmann@31068
   160
val inverse_1s = [@{thm inverse_numeral_1}];
haftmann@31068
   161
val divide_1s = [@{thm divide_numeral_1}];
haftmann@31068
   162
haftmann@31068
   163
(*To perform binary arithmetic.  The "left" rewriting handles patterns
haftmann@31068
   164
  created by the Numeral_Simprocs, such as 3 * (5 * x). *)
haftmann@31068
   165
val simps = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym,
haftmann@31068
   166
                 @{thm add_number_of_left}, @{thm mult_number_of_left}] @
haftmann@31068
   167
                @{thms arith_simps} @ @{thms rel_simps};
haftmann@31068
   168
haftmann@31068
   169
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
haftmann@31068
   170
  during re-arrangement*)
haftmann@31068
   171
val non_add_simps =
haftmann@31068
   172
  subtract Thm.eq_thm [@{thm add_number_of_left}, @{thm number_of_add} RS sym] simps;
wenzelm@23164
   173
haftmann@31068
   174
(*To evaluate binary negations of coefficients*)
haftmann@31068
   175
val minus_simps = [@{thm numeral_m1_eq_minus_1} RS sym, @{thm number_of_minus} RS sym] @
haftmann@31068
   176
                   @{thms minus_bin_simps} @ @{thms pred_bin_simps};
haftmann@31068
   177
haftmann@31068
   178
(*To let us treat subtraction as addition*)
haftmann@31068
   179
val diff_simps = [@{thm diff_minus}, @{thm minus_add_distrib}, @{thm minus_minus}];
haftmann@31068
   180
haftmann@31068
   181
(*To let us treat division as multiplication*)
haftmann@31068
   182
val divide_simps = [@{thm divide_inverse}, @{thm inverse_mult_distrib}, @{thm inverse_inverse_eq}];
haftmann@31068
   183
haftmann@31068
   184
(*push the unary minus down: - x * y = x * - y *)
haftmann@31068
   185
val minus_mult_eq_1_to_2 =
wenzelm@32957
   186
    [@{thm mult_minus_left}, @{thm minus_mult_right}] MRS trans |> Drule.standard;
haftmann@31068
   187
haftmann@31068
   188
(*to extract again any uncancelled minuses*)
haftmann@31068
   189
val minus_from_mult_simps =
haftmann@31068
   190
    [@{thm minus_minus}, @{thm mult_minus_left}, @{thm mult_minus_right}];
haftmann@31068
   191
haftmann@31068
   192
(*combine unary minus with numeric literals, however nested within a product*)
haftmann@31068
   193
val mult_minus_simps =
haftmann@31068
   194
    [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
haftmann@31068
   195
haftmann@31068
   196
val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
haftmann@31068
   197
  diff_simps @ minus_simps @ @{thms add_ac}
haftmann@31068
   198
val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
haftmann@31068
   199
val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
haftmann@31068
   200
haftmann@31068
   201
structure CancelNumeralsCommon =
haftmann@31068
   202
  struct
haftmann@31068
   203
  val mk_sum            = mk_sum
haftmann@31068
   204
  val dest_sum          = dest_sum
haftmann@31068
   205
  val mk_coeff          = mk_coeff
haftmann@31068
   206
  val dest_coeff        = dest_coeff 1
haftmann@31068
   207
  val find_first_coeff  = find_first_coeff []
wenzelm@31368
   208
  fun trans_tac _       = Arith_Data.trans_tac
haftmann@31068
   209
haftmann@31068
   210
  fun norm_tac ss =
haftmann@31068
   211
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
haftmann@31068
   212
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
haftmann@31068
   213
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
haftmann@31068
   214
haftmann@31068
   215
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
haftmann@31068
   216
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
haftmann@31068
   217
  val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s)
haftmann@31068
   218
  end;
haftmann@31068
   219
haftmann@31068
   220
haftmann@31068
   221
structure EqCancelNumerals = CancelNumeralsFun
haftmann@31068
   222
 (open CancelNumeralsCommon
haftmann@31068
   223
  val prove_conv = Arith_Data.prove_conv
haftmann@31068
   224
  val mk_bal   = HOLogic.mk_eq
haftmann@31068
   225
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
haftmann@31068
   226
  val bal_add1 = @{thm eq_add_iff1} RS trans
haftmann@31068
   227
  val bal_add2 = @{thm eq_add_iff2} RS trans
haftmann@31068
   228
);
haftmann@31068
   229
haftmann@31068
   230
structure LessCancelNumerals = CancelNumeralsFun
haftmann@31068
   231
 (open CancelNumeralsCommon
haftmann@31068
   232
  val prove_conv = Arith_Data.prove_conv
haftmann@34974
   233
  val mk_bal   = HOLogic.mk_binrel @{const_name Algebras.less}
haftmann@34974
   234
  val dest_bal = HOLogic.dest_bin @{const_name Algebras.less} Term.dummyT
haftmann@31068
   235
  val bal_add1 = @{thm less_add_iff1} RS trans
haftmann@31068
   236
  val bal_add2 = @{thm less_add_iff2} RS trans
haftmann@31068
   237
);
haftmann@31068
   238
haftmann@31068
   239
structure LeCancelNumerals = CancelNumeralsFun
haftmann@31068
   240
 (open CancelNumeralsCommon
haftmann@31068
   241
  val prove_conv = Arith_Data.prove_conv
haftmann@34974
   242
  val mk_bal   = HOLogic.mk_binrel @{const_name Algebras.less_eq}
haftmann@34974
   243
  val dest_bal = HOLogic.dest_bin @{const_name Algebras.less_eq} Term.dummyT
haftmann@31068
   244
  val bal_add1 = @{thm le_add_iff1} RS trans
haftmann@31068
   245
  val bal_add2 = @{thm le_add_iff2} RS trans
haftmann@31068
   246
);
haftmann@31068
   247
haftmann@31068
   248
val cancel_numerals =
wenzelm@32155
   249
  map (Arith_Data.prep_simproc @{theory})
haftmann@31068
   250
   [("inteq_cancel_numerals",
haftmann@31068
   251
     ["(l::'a::number_ring) + m = n",
haftmann@31068
   252
      "(l::'a::number_ring) = m + n",
haftmann@31068
   253
      "(l::'a::number_ring) - m = n",
haftmann@31068
   254
      "(l::'a::number_ring) = m - n",
haftmann@31068
   255
      "(l::'a::number_ring) * m = n",
haftmann@31068
   256
      "(l::'a::number_ring) = m * n"],
haftmann@31068
   257
     K EqCancelNumerals.proc),
haftmann@31068
   258
    ("intless_cancel_numerals",
haftmann@35028
   259
     ["(l::'a::{linordered_idom,number_ring}) + m < n",
haftmann@35028
   260
      "(l::'a::{linordered_idom,number_ring}) < m + n",
haftmann@35028
   261
      "(l::'a::{linordered_idom,number_ring}) - m < n",
haftmann@35028
   262
      "(l::'a::{linordered_idom,number_ring}) < m - n",
haftmann@35028
   263
      "(l::'a::{linordered_idom,number_ring}) * m < n",
haftmann@35028
   264
      "(l::'a::{linordered_idom,number_ring}) < m * n"],
haftmann@31068
   265
     K LessCancelNumerals.proc),
haftmann@31068
   266
    ("intle_cancel_numerals",
haftmann@35028
   267
     ["(l::'a::{linordered_idom,number_ring}) + m <= n",
haftmann@35028
   268
      "(l::'a::{linordered_idom,number_ring}) <= m + n",
haftmann@35028
   269
      "(l::'a::{linordered_idom,number_ring}) - m <= n",
haftmann@35028
   270
      "(l::'a::{linordered_idom,number_ring}) <= m - n",
haftmann@35028
   271
      "(l::'a::{linordered_idom,number_ring}) * m <= n",
haftmann@35028
   272
      "(l::'a::{linordered_idom,number_ring}) <= m * n"],
haftmann@31068
   273
     K LeCancelNumerals.proc)];
haftmann@31068
   274
haftmann@31068
   275
structure CombineNumeralsData =
haftmann@31068
   276
  struct
haftmann@31068
   277
  type coeff            = int
haftmann@31068
   278
  val iszero            = (fn x => x = 0)
haftmann@31068
   279
  val add               = op +
haftmann@31068
   280
  val mk_sum            = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
haftmann@31068
   281
  val dest_sum          = dest_sum
haftmann@31068
   282
  val mk_coeff          = mk_coeff
haftmann@31068
   283
  val dest_coeff        = dest_coeff 1
haftmann@31068
   284
  val left_distrib      = @{thm combine_common_factor} RS trans
haftmann@31068
   285
  val prove_conv        = Arith_Data.prove_conv_nohyps
wenzelm@31368
   286
  fun trans_tac _       = Arith_Data.trans_tac
haftmann@31068
   287
haftmann@31068
   288
  fun norm_tac ss =
haftmann@31068
   289
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
haftmann@31068
   290
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
haftmann@31068
   291
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
haftmann@31068
   292
haftmann@31068
   293
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
haftmann@31068
   294
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
haftmann@31068
   295
  val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s)
haftmann@31068
   296
  end;
haftmann@31068
   297
haftmann@31068
   298
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
haftmann@31068
   299
haftmann@31068
   300
(*Version for fields, where coefficients can be fractions*)
haftmann@31068
   301
structure FieldCombineNumeralsData =
haftmann@31068
   302
  struct
haftmann@31068
   303
  type coeff            = int * int
haftmann@31068
   304
  val iszero            = (fn (p, q) => p = 0)
haftmann@31068
   305
  val add               = add_frac
haftmann@31068
   306
  val mk_sum            = long_mk_sum
haftmann@31068
   307
  val dest_sum          = dest_sum
haftmann@31068
   308
  val mk_coeff          = mk_fcoeff
haftmann@31068
   309
  val dest_coeff        = dest_fcoeff 1
haftmann@31068
   310
  val left_distrib      = @{thm combine_common_factor} RS trans
haftmann@31068
   311
  val prove_conv        = Arith_Data.prove_conv_nohyps
wenzelm@31368
   312
  fun trans_tac _       = Arith_Data.trans_tac
haftmann@31068
   313
haftmann@31068
   314
  val norm_ss1a = norm_ss1 addsimps inverse_1s @ divide_simps
haftmann@31068
   315
  fun norm_tac ss =
haftmann@31068
   316
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1a))
haftmann@31068
   317
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
haftmann@31068
   318
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
haftmann@31068
   319
haftmann@31068
   320
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps @ [@{thm add_frac_eq}]
haftmann@31068
   321
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
haftmann@31068
   322
  val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s @ divide_1s)
haftmann@31068
   323
  end;
haftmann@31068
   324
haftmann@31068
   325
structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);
haftmann@31068
   326
haftmann@31068
   327
val combine_numerals =
wenzelm@32155
   328
  Arith_Data.prep_simproc @{theory}
haftmann@31068
   329
    ("int_combine_numerals", 
haftmann@31068
   330
     ["(i::'a::number_ring) + j", "(i::'a::number_ring) - j"], 
haftmann@31068
   331
     K CombineNumerals.proc);
haftmann@31068
   332
haftmann@31068
   333
val field_combine_numerals =
wenzelm@32155
   334
  Arith_Data.prep_simproc @{theory}
haftmann@31068
   335
    ("field_combine_numerals", 
haftmann@31068
   336
     ["(i::'a::{number_ring,field,division_by_zero}) + j",
haftmann@31068
   337
      "(i::'a::{number_ring,field,division_by_zero}) - j"], 
haftmann@31068
   338
     K FieldCombineNumerals.proc);
haftmann@31068
   339
haftmann@31068
   340
(** Constant folding for multiplication in semirings **)
haftmann@31068
   341
haftmann@31068
   342
(*We do not need folding for addition: combine_numerals does the same thing*)
haftmann@31068
   343
haftmann@31068
   344
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
haftmann@31068
   345
struct
haftmann@31068
   346
  val assoc_ss = HOL_ss addsimps @{thms mult_ac}
haftmann@31068
   347
  val eq_reflection = eq_reflection
haftmann@31068
   348
  fun is_numeral (Const(@{const_name Int.number_of}, _) $ _) = true
haftmann@31068
   349
    | is_numeral _ = false;
haftmann@31068
   350
end;
haftmann@31068
   351
haftmann@31068
   352
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
haftmann@31068
   353
haftmann@31068
   354
val assoc_fold_simproc =
wenzelm@32155
   355
  Arith_Data.prep_simproc @{theory}
haftmann@31068
   356
   ("semiring_assoc_fold", ["(a::'a::comm_semiring_1_cancel) * b"],
haftmann@31068
   357
    K Semiring_Times_Assoc.proc);
wenzelm@23164
   358
wenzelm@23164
   359
structure CancelNumeralFactorCommon =
wenzelm@23164
   360
  struct
wenzelm@23164
   361
  val mk_coeff          = mk_coeff
wenzelm@23164
   362
  val dest_coeff        = dest_coeff 1
wenzelm@31368
   363
  fun trans_tac _       = Arith_Data.trans_tac
wenzelm@23164
   364
wenzelm@23164
   365
  val norm_ss1 = HOL_ss addsimps minus_from_mult_simps @ mult_1s
wenzelm@23164
   366
  val norm_ss2 = HOL_ss addsimps simps @ mult_minus_simps
haftmann@23881
   367
  val norm_ss3 = HOL_ss addsimps @{thms mult_ac}
wenzelm@23164
   368
  fun norm_tac ss =
wenzelm@23164
   369
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23164
   370
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23164
   371
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
wenzelm@23164
   372
haftmann@31068
   373
  val numeral_simp_ss = HOL_ss addsimps
haftmann@31068
   374
    [@{thm eq_number_of_eq}, @{thm less_number_of}, @{thm le_number_of}] @ simps
wenzelm@23164
   375
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
haftmann@30518
   376
  val simplify_meta_eq = Arith_Data.simplify_meta_eq
wenzelm@23164
   377
    [@{thm add_0}, @{thm add_0_right}, @{thm mult_zero_left},
huffman@26086
   378
      @{thm mult_zero_right}, @{thm mult_Bit1}, @{thm mult_1_right}];
wenzelm@23164
   379
  end
wenzelm@23164
   380
haftmann@30931
   381
(*Version for semiring_div*)
haftmann@30931
   382
structure DivCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   383
 (open CancelNumeralFactorCommon
haftmann@30496
   384
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   385
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
haftmann@30931
   386
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} Term.dummyT
haftmann@30931
   387
  val cancel = @{thm div_mult_mult1} RS trans
wenzelm@23164
   388
  val neg_exchanges = false
wenzelm@23164
   389
)
wenzelm@23164
   390
wenzelm@23164
   391
(*Version for fields*)
wenzelm@23164
   392
structure DivideCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   393
 (open CancelNumeralFactorCommon
haftmann@30496
   394
  val prove_conv = Arith_Data.prove_conv
haftmann@34974
   395
  val mk_bal   = HOLogic.mk_binop @{const_name Algebras.divide}
haftmann@34974
   396
  val dest_bal = HOLogic.dest_bin @{const_name Algebras.divide} Term.dummyT
nipkow@23413
   397
  val cancel = @{thm mult_divide_mult_cancel_left} RS trans
wenzelm@23164
   398
  val neg_exchanges = false
wenzelm@23164
   399
)
wenzelm@23164
   400
wenzelm@23164
   401
structure EqCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   402
 (open CancelNumeralFactorCommon
haftmann@30496
   403
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   404
  val mk_bal   = HOLogic.mk_eq
wenzelm@23164
   405
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
wenzelm@23164
   406
  val cancel = @{thm mult_cancel_left} RS trans
wenzelm@23164
   407
  val neg_exchanges = false
wenzelm@23164
   408
)
wenzelm@23164
   409
wenzelm@23164
   410
structure LessCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   411
 (open CancelNumeralFactorCommon
haftmann@30496
   412
  val prove_conv = Arith_Data.prove_conv
haftmann@34974
   413
  val mk_bal   = HOLogic.mk_binrel @{const_name Algebras.less}
haftmann@34974
   414
  val dest_bal = HOLogic.dest_bin @{const_name Algebras.less} Term.dummyT
wenzelm@23164
   415
  val cancel = @{thm mult_less_cancel_left} RS trans
wenzelm@23164
   416
  val neg_exchanges = true
wenzelm@23164
   417
)
wenzelm@23164
   418
wenzelm@23164
   419
structure LeCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   420
 (open CancelNumeralFactorCommon
haftmann@30496
   421
  val prove_conv = Arith_Data.prove_conv
haftmann@34974
   422
  val mk_bal   = HOLogic.mk_binrel @{const_name Algebras.less_eq}
haftmann@34974
   423
  val dest_bal = HOLogic.dest_bin @{const_name Algebras.less_eq} Term.dummyT
wenzelm@23164
   424
  val cancel = @{thm mult_le_cancel_left} RS trans
wenzelm@23164
   425
  val neg_exchanges = true
wenzelm@23164
   426
)
wenzelm@23164
   427
wenzelm@23164
   428
val cancel_numeral_factors =
wenzelm@32155
   429
  map (Arith_Data.prep_simproc @{theory})
wenzelm@23164
   430
   [("ring_eq_cancel_numeral_factor",
wenzelm@23164
   431
     ["(l::'a::{idom,number_ring}) * m = n",
wenzelm@23164
   432
      "(l::'a::{idom,number_ring}) = m * n"],
wenzelm@23164
   433
     K EqCancelNumeralFactor.proc),
wenzelm@23164
   434
    ("ring_less_cancel_numeral_factor",
haftmann@35028
   435
     ["(l::'a::{linordered_idom,number_ring}) * m < n",
haftmann@35028
   436
      "(l::'a::{linordered_idom,number_ring}) < m * n"],
wenzelm@23164
   437
     K LessCancelNumeralFactor.proc),
wenzelm@23164
   438
    ("ring_le_cancel_numeral_factor",
haftmann@35028
   439
     ["(l::'a::{linordered_idom,number_ring}) * m <= n",
haftmann@35028
   440
      "(l::'a::{linordered_idom,number_ring}) <= m * n"],
wenzelm@23164
   441
     K LeCancelNumeralFactor.proc),
wenzelm@23164
   442
    ("int_div_cancel_numeral_factors",
haftmann@30931
   443
     ["((l::'a::{semiring_div,number_ring}) * m) div n",
haftmann@30931
   444
      "(l::'a::{semiring_div,number_ring}) div (m * n)"],
haftmann@30931
   445
     K DivCancelNumeralFactor.proc),
wenzelm@23164
   446
    ("divide_cancel_numeral_factor",
wenzelm@23164
   447
     ["((l::'a::{division_by_zero,field,number_ring}) * m) / n",
wenzelm@23164
   448
      "(l::'a::{division_by_zero,field,number_ring}) / (m * n)",
wenzelm@23164
   449
      "((number_of v)::'a::{division_by_zero,field,number_ring}) / (number_of w)"],
wenzelm@23164
   450
     K DivideCancelNumeralFactor.proc)];
wenzelm@23164
   451
wenzelm@23164
   452
val field_cancel_numeral_factors =
wenzelm@32155
   453
  map (Arith_Data.prep_simproc @{theory})
wenzelm@23164
   454
   [("field_eq_cancel_numeral_factor",
wenzelm@23164
   455
     ["(l::'a::{field,number_ring}) * m = n",
wenzelm@23164
   456
      "(l::'a::{field,number_ring}) = m * n"],
wenzelm@23164
   457
     K EqCancelNumeralFactor.proc),
wenzelm@23164
   458
    ("field_cancel_numeral_factor",
wenzelm@23164
   459
     ["((l::'a::{division_by_zero,field,number_ring}) * m) / n",
wenzelm@23164
   460
      "(l::'a::{division_by_zero,field,number_ring}) / (m * n)",
wenzelm@23164
   461
      "((number_of v)::'a::{division_by_zero,field,number_ring}) / (number_of w)"],
wenzelm@23164
   462
     K DivideCancelNumeralFactor.proc)]
wenzelm@23164
   463
wenzelm@23164
   464
wenzelm@23164
   465
(** Declarations for ExtractCommonTerm **)
wenzelm@23164
   466
wenzelm@23164
   467
(*Find first term that matches u*)
wenzelm@23164
   468
fun find_first_t past u []         = raise TERM ("find_first_t", [])
wenzelm@23164
   469
  | find_first_t past u (t::terms) =
wenzelm@23164
   470
        if u aconv t then (rev past @ terms)
wenzelm@23164
   471
        else find_first_t (t::past) u terms
wenzelm@23164
   472
        handle TERM _ => find_first_t (t::past) u terms;
wenzelm@23164
   473
wenzelm@23164
   474
(** Final simplification for the CancelFactor simprocs **)
haftmann@30518
   475
val simplify_one = Arith_Data.simplify_meta_eq  
nipkow@30031
   476
  [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_by_1}, @{thm numeral_1_eq_1}];
wenzelm@23164
   477
nipkow@30649
   478
fun cancel_simplify_meta_eq ss cancel_th th =
wenzelm@23164
   479
    simplify_one ss (([th, cancel_th]) MRS trans);
wenzelm@23164
   480
nipkow@30649
   481
local
haftmann@31067
   482
  val Tp_Eq = Thm.reflexive (Thm.cterm_of @{theory HOL} HOLogic.Trueprop)
nipkow@30649
   483
  fun Eq_True_elim Eq = 
nipkow@30649
   484
    Thm.equal_elim (Thm.combination Tp_Eq (Thm.symmetric Eq)) @{thm TrueI}
nipkow@30649
   485
in
nipkow@30649
   486
fun sign_conv pos_th neg_th ss t =
nipkow@30649
   487
  let val T = fastype_of t;
haftmann@34974
   488
      val zero = Const(@{const_name Algebras.zero}, T);
haftmann@34974
   489
      val less = Const(@{const_name Algebras.less}, [T,T] ---> HOLogic.boolT);
nipkow@30649
   490
      val pos = less $ zero $ t and neg = less $ t $ zero
nipkow@30649
   491
      fun prove p =
haftmann@31101
   492
        Option.map Eq_True_elim (Lin_Arith.simproc ss p)
nipkow@30649
   493
        handle THM _ => NONE
nipkow@30649
   494
    in case prove pos of
nipkow@30649
   495
         SOME th => SOME(th RS pos_th)
nipkow@30649
   496
       | NONE => (case prove neg of
nipkow@30649
   497
                    SOME th => SOME(th RS neg_th)
nipkow@30649
   498
                  | NONE => NONE)
nipkow@30649
   499
    end;
nipkow@30649
   500
end
nipkow@30649
   501
wenzelm@23164
   502
structure CancelFactorCommon =
wenzelm@23164
   503
  struct
wenzelm@23164
   504
  val mk_sum            = long_mk_prod
wenzelm@23164
   505
  val dest_sum          = dest_prod
wenzelm@23164
   506
  val mk_coeff          = mk_coeff
wenzelm@23164
   507
  val dest_coeff        = dest_coeff
wenzelm@23164
   508
  val find_first        = find_first_t []
wenzelm@31368
   509
  fun trans_tac _       = Arith_Data.trans_tac
haftmann@23881
   510
  val norm_ss = HOL_ss addsimps mult_1s @ @{thms mult_ac}
wenzelm@23164
   511
  fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
nipkow@30649
   512
  val simplify_meta_eq  = cancel_simplify_meta_eq 
wenzelm@23164
   513
  end;
wenzelm@23164
   514
wenzelm@23164
   515
(*mult_cancel_left requires a ring with no zero divisors.*)
wenzelm@23164
   516
structure EqCancelFactor = ExtractCommonTermFun
wenzelm@23164
   517
 (open CancelFactorCommon
haftmann@30496
   518
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   519
  val mk_bal   = HOLogic.mk_eq
wenzelm@23164
   520
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
wenzelm@31368
   521
  fun simp_conv _ _ = SOME @{thm mult_cancel_left}
nipkow@30649
   522
);
nipkow@30649
   523
nipkow@30649
   524
(*for ordered rings*)
nipkow@30649
   525
structure LeCancelFactor = ExtractCommonTermFun
nipkow@30649
   526
 (open CancelFactorCommon
nipkow@30649
   527
  val prove_conv = Arith_Data.prove_conv
haftmann@34974
   528
  val mk_bal   = HOLogic.mk_binrel @{const_name Algebras.less_eq}
haftmann@34974
   529
  val dest_bal = HOLogic.dest_bin @{const_name Algebras.less_eq} Term.dummyT
nipkow@30649
   530
  val simp_conv = sign_conv
nipkow@30649
   531
    @{thm mult_le_cancel_left_pos} @{thm mult_le_cancel_left_neg}
nipkow@30649
   532
);
nipkow@30649
   533
nipkow@30649
   534
(*for ordered rings*)
nipkow@30649
   535
structure LessCancelFactor = ExtractCommonTermFun
nipkow@30649
   536
 (open CancelFactorCommon
nipkow@30649
   537
  val prove_conv = Arith_Data.prove_conv
haftmann@34974
   538
  val mk_bal   = HOLogic.mk_binrel @{const_name Algebras.less}
haftmann@34974
   539
  val dest_bal = HOLogic.dest_bin @{const_name Algebras.less} Term.dummyT
nipkow@30649
   540
  val simp_conv = sign_conv
nipkow@30649
   541
    @{thm mult_less_cancel_left_pos} @{thm mult_less_cancel_left_neg}
wenzelm@23164
   542
);
wenzelm@23164
   543
haftmann@30931
   544
(*for semirings with division*)
haftmann@30931
   545
structure DivCancelFactor = ExtractCommonTermFun
wenzelm@23164
   546
 (open CancelFactorCommon
haftmann@30496
   547
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   548
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
haftmann@30931
   549
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} Term.dummyT
wenzelm@31368
   550
  fun simp_conv _ _ = SOME @{thm div_mult_mult1_if}
wenzelm@23164
   551
);
wenzelm@23164
   552
haftmann@30931
   553
structure ModCancelFactor = ExtractCommonTermFun
nipkow@24395
   554
 (open CancelFactorCommon
haftmann@30496
   555
  val prove_conv = Arith_Data.prove_conv
nipkow@24395
   556
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.mod}
haftmann@31067
   557
  val dest_bal = HOLogic.dest_bin @{const_name Divides.mod} Term.dummyT
wenzelm@31368
   558
  fun simp_conv _ _ = SOME @{thm mod_mult_mult1}
nipkow@24395
   559
);
nipkow@24395
   560
haftmann@30931
   561
(*for idoms*)
haftmann@30931
   562
structure DvdCancelFactor = ExtractCommonTermFun
nipkow@23969
   563
 (open CancelFactorCommon
haftmann@30496
   564
  val prove_conv = Arith_Data.prove_conv
haftmann@27651
   565
  val mk_bal   = HOLogic.mk_binrel @{const_name Ring_and_Field.dvd}
huffman@29981
   566
  val dest_bal = HOLogic.dest_bin @{const_name Ring_and_Field.dvd} Term.dummyT
wenzelm@31368
   567
  fun simp_conv _ _ = SOME @{thm dvd_mult_cancel_left}
nipkow@23969
   568
);
nipkow@23969
   569
wenzelm@23164
   570
(*Version for all fields, including unordered ones (type complex).*)
wenzelm@23164
   571
structure DivideCancelFactor = ExtractCommonTermFun
wenzelm@23164
   572
 (open CancelFactorCommon
haftmann@30496
   573
  val prove_conv = Arith_Data.prove_conv
haftmann@34974
   574
  val mk_bal   = HOLogic.mk_binop @{const_name Algebras.divide}
haftmann@34974
   575
  val dest_bal = HOLogic.dest_bin @{const_name Algebras.divide} Term.dummyT
wenzelm@31368
   576
  fun simp_conv _ _ = SOME @{thm mult_divide_mult_cancel_left_if}
wenzelm@23164
   577
);
wenzelm@23164
   578
wenzelm@23164
   579
val cancel_factors =
wenzelm@32155
   580
  map (Arith_Data.prep_simproc @{theory})
wenzelm@23164
   581
   [("ring_eq_cancel_factor",
haftmann@30931
   582
     ["(l::'a::idom) * m = n",
haftmann@30931
   583
      "(l::'a::idom) = m * n"],
nipkow@30649
   584
     K EqCancelFactor.proc),
haftmann@35028
   585
    ("linlinordered_ring_le_cancel_factor",
haftmann@35028
   586
     ["(l::'a::linordered_ring) * m <= n",
haftmann@35028
   587
      "(l::'a::linordered_ring) <= m * n"],
nipkow@30649
   588
     K LeCancelFactor.proc),
haftmann@35028
   589
    ("linlinordered_ring_less_cancel_factor",
haftmann@35028
   590
     ["(l::'a::linordered_ring) * m < n",
haftmann@35028
   591
      "(l::'a::linordered_ring) < m * n"],
nipkow@30649
   592
     K LessCancelFactor.proc),
wenzelm@23164
   593
    ("int_div_cancel_factor",
haftmann@30931
   594
     ["((l::'a::semiring_div) * m) div n", "(l::'a::semiring_div) div (m * n)"],
haftmann@30931
   595
     K DivCancelFactor.proc),
nipkow@24395
   596
    ("int_mod_cancel_factor",
haftmann@30931
   597
     ["((l::'a::semiring_div) * m) mod n", "(l::'a::semiring_div) mod (m * n)"],
haftmann@30931
   598
     K ModCancelFactor.proc),
huffman@29981
   599
    ("dvd_cancel_factor",
huffman@29981
   600
     ["((l::'a::idom) * m) dvd n", "(l::'a::idom) dvd (m * n)"],
haftmann@30931
   601
     K DvdCancelFactor.proc),
wenzelm@23164
   602
    ("divide_cancel_factor",
nipkow@23400
   603
     ["((l::'a::{division_by_zero,field}) * m) / n",
nipkow@23400
   604
      "(l::'a::{division_by_zero,field}) / (m * n)"],
wenzelm@23164
   605
     K DivideCancelFactor.proc)];
wenzelm@23164
   606
wenzelm@23164
   607
end;
wenzelm@23164
   608
haftmann@31068
   609
Addsimprocs Numeral_Simprocs.cancel_numerals;
haftmann@31068
   610
Addsimprocs [Numeral_Simprocs.combine_numerals];
haftmann@31068
   611
Addsimprocs [Numeral_Simprocs.field_combine_numerals];
haftmann@31068
   612
Addsimprocs [Numeral_Simprocs.assoc_fold_simproc];
haftmann@31068
   613
haftmann@31068
   614
(*examples:
haftmann@31068
   615
print_depth 22;
haftmann@31068
   616
set timing;
haftmann@31068
   617
set trace_simp;
haftmann@31068
   618
fun test s = (Goal s, by (Simp_tac 1));
haftmann@31068
   619
haftmann@31068
   620
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
haftmann@31068
   621
haftmann@31068
   622
test "2*u = (u::int)";
haftmann@31068
   623
test "(i + j + 12 + (k::int)) - 15 = y";
haftmann@31068
   624
test "(i + j + 12 + (k::int)) - 5 = y";
haftmann@31068
   625
haftmann@31068
   626
test "y - b < (b::int)";
haftmann@31068
   627
test "y - (3*b + c) < (b::int) - 2*c";
haftmann@31068
   628
haftmann@31068
   629
test "(2*x - (u*v) + y) - v*3*u = (w::int)";
haftmann@31068
   630
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
haftmann@31068
   631
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
haftmann@31068
   632
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
haftmann@31068
   633
haftmann@31068
   634
test "(i + j + 12 + (k::int)) = u + 15 + y";
haftmann@31068
   635
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
haftmann@31068
   636
haftmann@31068
   637
test "2*y + 3*z + 6*w + 2*y + 3*z + 2*u = 2*y' + 3*z' + 6*w' + 2*y' + 3*z' + u + (vv::int)";
haftmann@31068
   638
haftmann@31068
   639
test "a + -(b+c) + b = (d::int)";
haftmann@31068
   640
test "a + -(b+c) - b = (d::int)";
haftmann@31068
   641
haftmann@31068
   642
(*negative numerals*)
haftmann@31068
   643
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
haftmann@31068
   644
test "(i + j + -3 + (k::int)) < u + 5 + y";
haftmann@31068
   645
test "(i + j + 3 + (k::int)) < u + -6 + y";
haftmann@31068
   646
test "(i + j + -12 + (k::int)) - 15 = y";
haftmann@31068
   647
test "(i + j + 12 + (k::int)) - -15 = y";
haftmann@31068
   648
test "(i + j + -12 + (k::int)) - -15 = y";
haftmann@31068
   649
*)
haftmann@31068
   650
haftmann@31068
   651
Addsimprocs Numeral_Simprocs.cancel_numeral_factors;
haftmann@31068
   652
haftmann@31068
   653
(*examples:
haftmann@31068
   654
print_depth 22;
haftmann@31068
   655
set timing;
haftmann@31068
   656
set trace_simp;
haftmann@31068
   657
fun test s = (Goal s; by (Simp_tac 1));
haftmann@31068
   658
haftmann@31068
   659
test "9*x = 12 * (y::int)";
haftmann@31068
   660
test "(9*x) div (12 * (y::int)) = z";
haftmann@31068
   661
test "9*x < 12 * (y::int)";
haftmann@31068
   662
test "9*x <= 12 * (y::int)";
haftmann@31068
   663
haftmann@31068
   664
test "-99*x = 132 * (y::int)";
haftmann@31068
   665
test "(-99*x) div (132 * (y::int)) = z";
haftmann@31068
   666
test "-99*x < 132 * (y::int)";
haftmann@31068
   667
test "-99*x <= 132 * (y::int)";
haftmann@31068
   668
haftmann@31068
   669
test "999*x = -396 * (y::int)";
haftmann@31068
   670
test "(999*x) div (-396 * (y::int)) = z";
haftmann@31068
   671
test "999*x < -396 * (y::int)";
haftmann@31068
   672
test "999*x <= -396 * (y::int)";
haftmann@31068
   673
haftmann@31068
   674
test "-99*x = -81 * (y::int)";
haftmann@31068
   675
test "(-99*x) div (-81 * (y::int)) = z";
haftmann@31068
   676
test "-99*x <= -81 * (y::int)";
haftmann@31068
   677
test "-99*x < -81 * (y::int)";
haftmann@31068
   678
haftmann@31068
   679
test "-2 * x = -1 * (y::int)";
haftmann@31068
   680
test "-2 * x = -(y::int)";
haftmann@31068
   681
test "(-2 * x) div (-1 * (y::int)) = z";
haftmann@31068
   682
test "-2 * x < -(y::int)";
haftmann@31068
   683
test "-2 * x <= -1 * (y::int)";
haftmann@31068
   684
test "-x < -23 * (y::int)";
haftmann@31068
   685
test "-x <= -23 * (y::int)";
haftmann@31068
   686
*)
haftmann@31068
   687
haftmann@31068
   688
(*And the same examples for fields such as rat or real:
haftmann@31068
   689
test "0 <= (y::rat) * -2";
haftmann@31068
   690
test "9*x = 12 * (y::rat)";
haftmann@31068
   691
test "(9*x) / (12 * (y::rat)) = z";
haftmann@31068
   692
test "9*x < 12 * (y::rat)";
haftmann@31068
   693
test "9*x <= 12 * (y::rat)";
haftmann@31068
   694
haftmann@31068
   695
test "-99*x = 132 * (y::rat)";
haftmann@31068
   696
test "(-99*x) / (132 * (y::rat)) = z";
haftmann@31068
   697
test "-99*x < 132 * (y::rat)";
haftmann@31068
   698
test "-99*x <= 132 * (y::rat)";
haftmann@31068
   699
haftmann@31068
   700
test "999*x = -396 * (y::rat)";
haftmann@31068
   701
test "(999*x) / (-396 * (y::rat)) = z";
haftmann@31068
   702
test "999*x < -396 * (y::rat)";
haftmann@31068
   703
test "999*x <= -396 * (y::rat)";
haftmann@31068
   704
haftmann@31068
   705
test  "(- ((2::rat) * x) <= 2 * y)";
haftmann@31068
   706
test "-99*x = -81 * (y::rat)";
haftmann@31068
   707
test "(-99*x) / (-81 * (y::rat)) = z";
haftmann@31068
   708
test "-99*x <= -81 * (y::rat)";
haftmann@31068
   709
test "-99*x < -81 * (y::rat)";
haftmann@31068
   710
haftmann@31068
   711
test "-2 * x = -1 * (y::rat)";
haftmann@31068
   712
test "-2 * x = -(y::rat)";
haftmann@31068
   713
test "(-2 * x) / (-1 * (y::rat)) = z";
haftmann@31068
   714
test "-2 * x < -(y::rat)";
haftmann@31068
   715
test "-2 * x <= -1 * (y::rat)";
haftmann@31068
   716
test "-x < -23 * (y::rat)";
haftmann@31068
   717
test "-x <= -23 * (y::rat)";
haftmann@31068
   718
*)
haftmann@31068
   719
haftmann@31068
   720
Addsimprocs Numeral_Simprocs.cancel_factors;
wenzelm@23164
   721
wenzelm@23164
   722
wenzelm@23164
   723
(*examples:
wenzelm@23164
   724
print_depth 22;
wenzelm@23164
   725
set timing;
wenzelm@23164
   726
set trace_simp;
wenzelm@23164
   727
fun test s = (Goal s; by (Asm_simp_tac 1));
wenzelm@23164
   728
wenzelm@23164
   729
test "x*k = k*(y::int)";
wenzelm@23164
   730
test "k = k*(y::int)";
wenzelm@23164
   731
test "a*(b*c) = (b::int)";
wenzelm@23164
   732
test "a*(b*c) = d*(b::int)*(x*a)";
wenzelm@23164
   733
wenzelm@23164
   734
test "(x*k) div (k*(y::int)) = (uu::int)";
wenzelm@23164
   735
test "(k) div (k*(y::int)) = (uu::int)";
wenzelm@23164
   736
test "(a*(b*c)) div ((b::int)) = (uu::int)";
wenzelm@23164
   737
test "(a*(b*c)) div (d*(b::int)*(x*a)) = (uu::int)";
wenzelm@23164
   738
*)
wenzelm@23164
   739
wenzelm@23164
   740
(*And the same examples for fields such as rat or real:
wenzelm@23164
   741
print_depth 22;
wenzelm@23164
   742
set timing;
wenzelm@23164
   743
set trace_simp;
wenzelm@23164
   744
fun test s = (Goal s; by (Asm_simp_tac 1));
wenzelm@23164
   745
wenzelm@23164
   746
test "x*k = k*(y::rat)";
wenzelm@23164
   747
test "k = k*(y::rat)";
wenzelm@23164
   748
test "a*(b*c) = (b::rat)";
wenzelm@23164
   749
test "a*(b*c) = d*(b::rat)*(x*a)";
wenzelm@23164
   750
wenzelm@23164
   751
wenzelm@23164
   752
test "(x*k) / (k*(y::rat)) = (uu::rat)";
wenzelm@23164
   753
test "(k) / (k*(y::rat)) = (uu::rat)";
wenzelm@23164
   754
test "(a*(b*c)) / ((b::rat)) = (uu::rat)";
wenzelm@23164
   755
test "(a*(b*c)) / (d*(b::rat)*(x*a)) = (uu::rat)";
wenzelm@23164
   756
wenzelm@23164
   757
(*FIXME: what do we do about this?*)
wenzelm@23164
   758
test "a*(b*c)/(y*z) = d*(b::rat)*(x*a)/z";
wenzelm@23164
   759
*)