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