src/HOL/int_arith1.ML
author nipkow
Tue Aug 14 19:23:27 2007 +0200 (2007-08-14)
changeset 24266 bdb48fd8fbdd
parent 24196 f1dbfd7e3223
child 24630 351a308ab58d
permissions -rw-r--r--
extended linear arith capabilities with code by Amine
wenzelm@23164
     1
(*  Title:      HOL/int_arith1.ML
wenzelm@23164
     2
    ID:         $Id$
wenzelm@23164
     3
    Authors:    Larry Paulson and Tobias Nipkow
wenzelm@23164
     4
wenzelm@23164
     5
Simprocs and decision procedure for linear arithmetic.
wenzelm@23164
     6
*)
wenzelm@23164
     7
wenzelm@23164
     8
(** Misc ML bindings **)
wenzelm@23164
     9
wenzelm@23164
    10
val succ_Pls = thm "succ_Pls";
wenzelm@23164
    11
val succ_Min = thm "succ_Min";
wenzelm@23164
    12
val succ_1 = thm "succ_1";
wenzelm@23164
    13
val succ_0 = thm "succ_0";
wenzelm@23164
    14
wenzelm@23164
    15
val pred_Pls = thm "pred_Pls";
wenzelm@23164
    16
val pred_Min = thm "pred_Min";
wenzelm@23164
    17
val pred_1 = thm "pred_1";
wenzelm@23164
    18
val pred_0 = thm "pred_0";
wenzelm@23164
    19
wenzelm@23164
    20
val minus_Pls = thm "minus_Pls";
wenzelm@23164
    21
val minus_Min = thm "minus_Min";
wenzelm@23164
    22
val minus_1 = thm "minus_1";
wenzelm@23164
    23
val minus_0 = thm "minus_0";
wenzelm@23164
    24
wenzelm@23164
    25
val add_Pls = thm "add_Pls";
wenzelm@23164
    26
val add_Min = thm "add_Min";
wenzelm@23164
    27
val add_BIT_11 = thm "add_BIT_11";
wenzelm@23164
    28
val add_BIT_10 = thm "add_BIT_10";
wenzelm@23164
    29
val add_BIT_0 = thm "add_BIT_0";
wenzelm@23164
    30
val add_Pls_right = thm "add_Pls_right";
wenzelm@23164
    31
val add_Min_right = thm "add_Min_right";
wenzelm@23164
    32
wenzelm@23164
    33
val mult_Pls = thm "mult_Pls";
wenzelm@23164
    34
val mult_Min = thm "mult_Min";
wenzelm@23164
    35
val mult_num1 = thm "mult_num1";
wenzelm@23164
    36
val mult_num0 = thm "mult_num0";
wenzelm@23164
    37
wenzelm@23164
    38
val neg_def = thm "neg_def";
wenzelm@23164
    39
val iszero_def = thm "iszero_def";
wenzelm@23164
    40
wenzelm@23164
    41
val number_of_succ = thm "number_of_succ";
wenzelm@23164
    42
val number_of_pred = thm "number_of_pred";
wenzelm@23164
    43
val number_of_minus = thm "number_of_minus";
wenzelm@23164
    44
val number_of_add = thm "number_of_add";
wenzelm@23164
    45
val diff_number_of_eq = thm "diff_number_of_eq";
wenzelm@23164
    46
val number_of_mult = thm "number_of_mult";
wenzelm@23164
    47
val double_number_of_BIT = thm "double_number_of_BIT";
wenzelm@23164
    48
val numeral_0_eq_0 = thm "numeral_0_eq_0";
wenzelm@23164
    49
val numeral_1_eq_1 = thm "numeral_1_eq_1";
wenzelm@23164
    50
val numeral_m1_eq_minus_1 = thm "numeral_m1_eq_minus_1";
wenzelm@23164
    51
val mult_minus1 = thm "mult_minus1";
wenzelm@23164
    52
val mult_minus1_right = thm "mult_minus1_right";
wenzelm@23164
    53
val minus_number_of_mult = thm "minus_number_of_mult";
wenzelm@23164
    54
val zero_less_nat_eq = thm "zero_less_nat_eq";
wenzelm@23164
    55
val eq_number_of_eq = thm "eq_number_of_eq";
wenzelm@23164
    56
val iszero_number_of_Pls = thm "iszero_number_of_Pls";
wenzelm@23164
    57
val nonzero_number_of_Min = thm "nonzero_number_of_Min";
wenzelm@23164
    58
val iszero_number_of_BIT = thm "iszero_number_of_BIT";
wenzelm@23164
    59
val iszero_number_of_0 = thm "iszero_number_of_0";
wenzelm@23164
    60
val iszero_number_of_1 = thm "iszero_number_of_1";
wenzelm@23164
    61
val less_number_of_eq_neg = thm "less_number_of_eq_neg";
wenzelm@23164
    62
val le_number_of_eq = thm "le_number_of_eq";
wenzelm@23164
    63
val not_neg_number_of_Pls = thm "not_neg_number_of_Pls";
wenzelm@23164
    64
val neg_number_of_Min = thm "neg_number_of_Min";
wenzelm@23164
    65
val neg_number_of_BIT = thm "neg_number_of_BIT";
wenzelm@23164
    66
val le_number_of_eq_not_less = thm "le_number_of_eq_not_less";
wenzelm@23164
    67
val abs_number_of = thm "abs_number_of";
wenzelm@23164
    68
val number_of_reorient = thm "number_of_reorient";
wenzelm@23164
    69
val add_number_of_left = thm "add_number_of_left";
wenzelm@23164
    70
val mult_number_of_left = thm "mult_number_of_left";
wenzelm@23164
    71
val add_number_of_diff1 = thm "add_number_of_diff1";
wenzelm@23164
    72
val add_number_of_diff2 = thm "add_number_of_diff2";
wenzelm@23164
    73
val less_iff_diff_less_0 = thm "less_iff_diff_less_0";
wenzelm@23164
    74
val eq_iff_diff_eq_0 = thm "eq_iff_diff_eq_0";
wenzelm@23164
    75
val le_iff_diff_le_0 = thm "le_iff_diff_le_0";
wenzelm@23164
    76
wenzelm@23164
    77
val arith_extra_simps = thms "arith_extra_simps";
wenzelm@23164
    78
val arith_simps = thms "arith_simps";
wenzelm@23164
    79
val rel_simps = thms "rel_simps";
wenzelm@23164
    80
wenzelm@23164
    81
val zless_imp_add1_zle = thm "zless_imp_add1_zle";
wenzelm@23164
    82
wenzelm@23164
    83
val combine_common_factor = thm "combine_common_factor";
wenzelm@23164
    84
val eq_add_iff1 = thm "eq_add_iff1";
wenzelm@23164
    85
val eq_add_iff2 = thm "eq_add_iff2";
wenzelm@23164
    86
val less_add_iff1 = thm "less_add_iff1";
wenzelm@23164
    87
val less_add_iff2 = thm "less_add_iff2";
wenzelm@23164
    88
val le_add_iff1 = thm "le_add_iff1";
wenzelm@23164
    89
val le_add_iff2 = thm "le_add_iff2";
wenzelm@23164
    90
wenzelm@23164
    91
val arith_special = thms "arith_special";
wenzelm@23164
    92
wenzelm@23164
    93
structure Int_Numeral_Base_Simprocs =
wenzelm@23164
    94
  struct
wenzelm@23164
    95
  fun prove_conv tacs ctxt (_: thm list) (t, u) =
wenzelm@23164
    96
    if t aconv u then NONE
wenzelm@23164
    97
    else
wenzelm@23164
    98
      let val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u))
wenzelm@23164
    99
      in SOME (Goal.prove ctxt [] [] eq (K (EVERY tacs))) end
wenzelm@23164
   100
wenzelm@23164
   101
  fun prove_conv_nohyps tacs sg = prove_conv tacs sg [];
wenzelm@23164
   102
wenzelm@23164
   103
  fun prep_simproc (name, pats, proc) =
wenzelm@23164
   104
    Simplifier.simproc (the_context()) name pats proc;
wenzelm@23164
   105
wenzelm@23164
   106
  fun is_numeral (Const(@{const_name Numeral.number_of}, _) $ w) = true
wenzelm@23164
   107
    | is_numeral _ = false
wenzelm@23164
   108
wenzelm@23164
   109
  fun simplify_meta_eq f_number_of_eq f_eq =
wenzelm@23164
   110
      mk_meta_eq ([f_eq, f_number_of_eq] MRS trans)
wenzelm@23164
   111
wenzelm@23164
   112
  (*reorientation simprules using ==, for the following simproc*)
haftmann@23881
   113
  val meta_zero_reorient = @{thm zero_reorient} RS eq_reflection
haftmann@23881
   114
  val meta_one_reorient = @{thm one_reorient} RS eq_reflection
wenzelm@23164
   115
  val meta_number_of_reorient = number_of_reorient RS eq_reflection
wenzelm@23164
   116
wenzelm@23164
   117
  (*reorientation simplification procedure: reorients (polymorphic) 
wenzelm@23164
   118
    0 = x, 1 = x, nnn = x provided x isn't 0, 1 or a numeral.*)
wenzelm@23164
   119
  fun reorient_proc sg _ (_ $ t $ u) =
wenzelm@23164
   120
    case u of
wenzelm@23164
   121
	Const(@{const_name HOL.zero}, _) => NONE
wenzelm@23164
   122
      | Const(@{const_name HOL.one}, _) => NONE
wenzelm@23164
   123
      | Const(@{const_name Numeral.number_of}, _) $ _ => NONE
wenzelm@23164
   124
      | _ => SOME (case t of
wenzelm@23164
   125
		  Const(@{const_name HOL.zero}, _) => meta_zero_reorient
wenzelm@23164
   126
		| Const(@{const_name HOL.one}, _) => meta_one_reorient
wenzelm@23164
   127
		| Const(@{const_name Numeral.number_of}, _) $ _ => meta_number_of_reorient)
wenzelm@23164
   128
wenzelm@23164
   129
  val reorient_simproc = 
wenzelm@23164
   130
      prep_simproc ("reorient_simproc", ["0=x", "1=x", "number_of w = x"], reorient_proc)
wenzelm@23164
   131
wenzelm@23164
   132
  end;
wenzelm@23164
   133
wenzelm@23164
   134
wenzelm@23164
   135
Addsimprocs [Int_Numeral_Base_Simprocs.reorient_simproc];
wenzelm@23164
   136
wenzelm@23164
   137
wenzelm@23164
   138
structure Int_Numeral_Simprocs =
wenzelm@23164
   139
struct
wenzelm@23164
   140
wenzelm@23164
   141
(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic in Int_Numeral_Base_Simprocs
wenzelm@23164
   142
  isn't complicated by the abstract 0 and 1.*)
wenzelm@23164
   143
val numeral_syms = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym];
wenzelm@23164
   144
wenzelm@23164
   145
(** New term ordering so that AC-rewriting brings numerals to the front **)
wenzelm@23164
   146
wenzelm@23164
   147
(*Order integers by absolute value and then by sign. The standard integer
wenzelm@23164
   148
  ordering is not well-founded.*)
wenzelm@23164
   149
fun num_ord (i,j) =
wenzelm@23164
   150
      (case IntInf.compare (IntInf.abs i, IntInf.abs j) of
wenzelm@23164
   151
            EQUAL => int_ord (IntInf.sign i, IntInf.sign j) 
wenzelm@23164
   152
          | ord => ord);
wenzelm@23164
   153
wenzelm@23164
   154
(*This resembles Term.term_ord, but it puts binary numerals before other
wenzelm@23164
   155
  non-atomic terms.*)
wenzelm@23164
   156
local open Term 
wenzelm@23164
   157
in 
wenzelm@23164
   158
fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =
wenzelm@23164
   159
      (case numterm_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
wenzelm@23164
   160
  | numterm_ord
wenzelm@23164
   161
     (Const(@{const_name Numeral.number_of}, _) $ v, Const(@{const_name Numeral.number_of}, _) $ w) =
wenzelm@23164
   162
     num_ord (HOLogic.dest_numeral v, HOLogic.dest_numeral w)
wenzelm@23164
   163
  | numterm_ord (Const(@{const_name Numeral.number_of}, _) $ _, _) = LESS
wenzelm@23164
   164
  | numterm_ord (_, Const(@{const_name Numeral.number_of}, _) $ _) = GREATER
wenzelm@23164
   165
  | numterm_ord (t, u) =
wenzelm@23164
   166
      (case int_ord (size_of_term t, size_of_term u) of
wenzelm@23164
   167
        EQUAL =>
wenzelm@23164
   168
          let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
wenzelm@23164
   169
            (case hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)
wenzelm@23164
   170
          end
wenzelm@23164
   171
      | ord => ord)
wenzelm@23164
   172
and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
wenzelm@23164
   173
end;
wenzelm@23164
   174
wenzelm@23164
   175
fun numtermless tu = (numterm_ord tu = LESS);
wenzelm@23164
   176
wenzelm@23164
   177
(*Defined in this file, but perhaps needed only for Int_Numeral_Base_Simprocs of type nat.*)
wenzelm@23164
   178
val num_ss = HOL_ss settermless numtermless;
wenzelm@23164
   179
wenzelm@23164
   180
wenzelm@23164
   181
(** Utilities **)
wenzelm@23164
   182
wenzelm@23164
   183
fun mk_number T n = HOLogic.number_of_const T $ HOLogic.mk_numeral n;
wenzelm@23164
   184
wenzelm@23164
   185
fun find_first_numeral past (t::terms) =
wenzelm@23164
   186
        ((snd (HOLogic.dest_number t), rev past @ terms)
wenzelm@23164
   187
         handle TERM _ => find_first_numeral (t::past) terms)
wenzelm@23164
   188
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
wenzelm@23164
   189
wenzelm@23164
   190
val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
wenzelm@23164
   191
wenzelm@23164
   192
fun mk_minus t = 
wenzelm@23164
   193
  let val T = Term.fastype_of t
nipkow@23400
   194
  in Const (@{const_name HOL.uminus}, T --> T) $ t end;
wenzelm@23164
   195
wenzelm@23164
   196
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
wenzelm@23164
   197
fun mk_sum T []        = mk_number T 0
wenzelm@23164
   198
  | mk_sum T [t,u]     = mk_plus (t, u)
wenzelm@23164
   199
  | mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
wenzelm@23164
   200
wenzelm@23164
   201
(*this version ALWAYS includes a trailing zero*)
wenzelm@23164
   202
fun long_mk_sum T []        = mk_number T 0
wenzelm@23164
   203
  | long_mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
wenzelm@23164
   204
wenzelm@23164
   205
val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} Term.dummyT;
wenzelm@23164
   206
wenzelm@23164
   207
(*decompose additions AND subtractions as a sum*)
wenzelm@23164
   208
fun dest_summing (pos, Const (@{const_name HOL.plus}, _) $ t $ u, ts) =
wenzelm@23164
   209
        dest_summing (pos, t, dest_summing (pos, u, ts))
wenzelm@23164
   210
  | dest_summing (pos, Const (@{const_name HOL.minus}, _) $ t $ u, ts) =
wenzelm@23164
   211
        dest_summing (pos, t, dest_summing (not pos, u, ts))
wenzelm@23164
   212
  | dest_summing (pos, t, ts) =
wenzelm@23164
   213
        if pos then t::ts else mk_minus t :: ts;
wenzelm@23164
   214
wenzelm@23164
   215
fun dest_sum t = dest_summing (true, t, []);
wenzelm@23164
   216
wenzelm@23164
   217
val mk_diff = HOLogic.mk_binop @{const_name HOL.minus};
wenzelm@23164
   218
val dest_diff = HOLogic.dest_bin @{const_name HOL.minus} Term.dummyT;
wenzelm@23164
   219
wenzelm@23164
   220
val mk_times = HOLogic.mk_binop @{const_name HOL.times};
wenzelm@23164
   221
nipkow@23400
   222
fun one_of T = Const(@{const_name HOL.one},T);
nipkow@23400
   223
nipkow@23400
   224
(* build product with trailing 1 rather than Numeral 1 in order to avoid the
nipkow@23400
   225
   unnecessary restriction to type class number_ring
nipkow@23400
   226
   which is not required for cancellation of common factors in divisions.
nipkow@23400
   227
*)
wenzelm@23164
   228
fun mk_prod T = 
nipkow@23400
   229
  let val one = one_of T
wenzelm@23164
   230
  fun mk [] = one
wenzelm@23164
   231
    | mk [t] = t
wenzelm@23164
   232
    | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)
wenzelm@23164
   233
  in mk end;
wenzelm@23164
   234
wenzelm@23164
   235
(*This version ALWAYS includes a trailing one*)
nipkow@23400
   236
fun long_mk_prod T []        = one_of T
wenzelm@23164
   237
  | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);
wenzelm@23164
   238
wenzelm@23164
   239
val dest_times = HOLogic.dest_bin @{const_name HOL.times} Term.dummyT;
wenzelm@23164
   240
wenzelm@23164
   241
fun dest_prod t =
wenzelm@23164
   242
      let val (t,u) = dest_times t
nipkow@23400
   243
      in dest_prod t @ dest_prod u end
wenzelm@23164
   244
      handle TERM _ => [t];
wenzelm@23164
   245
wenzelm@23164
   246
(*DON'T do the obvious simplifications; that would create special cases*)
wenzelm@23164
   247
fun mk_coeff (k, t) = mk_times (mk_number (Term.fastype_of t) k, t);
wenzelm@23164
   248
wenzelm@23164
   249
(*Express t as a product of (possibly) a numeral with other sorted terms*)
wenzelm@23164
   250
fun dest_coeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_coeff (~sign) t
wenzelm@23164
   251
  | dest_coeff sign t =
wenzelm@23164
   252
    let val ts = sort Term.term_ord (dest_prod t)
wenzelm@23164
   253
        val (n, ts') = find_first_numeral [] ts
wenzelm@23164
   254
                          handle TERM _ => (1, ts)
wenzelm@23164
   255
    in (sign*n, mk_prod (Term.fastype_of t) ts') end;
wenzelm@23164
   256
wenzelm@23164
   257
(*Find first coefficient-term THAT MATCHES u*)
wenzelm@23164
   258
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
wenzelm@23164
   259
  | find_first_coeff past u (t::terms) =
wenzelm@23164
   260
        let val (n,u') = dest_coeff 1 t
nipkow@23400
   261
        in if u aconv u' then (n, rev past @ terms)
nipkow@23400
   262
                         else find_first_coeff (t::past) u terms
wenzelm@23164
   263
        end
wenzelm@23164
   264
        handle TERM _ => find_first_coeff (t::past) u terms;
wenzelm@23164
   265
wenzelm@23164
   266
(*Fractions as pairs of ints. Can't use Rat.rat because the representation
wenzelm@23164
   267
  needs to preserve negative values in the denominator.*)
wenzelm@23164
   268
fun mk_frac (p, q : IntInf.int) = if q = 0 then raise Div else (p, q);
wenzelm@23164
   269
wenzelm@23164
   270
(*Don't reduce fractions; sums must be proved by rule add_frac_eq.
wenzelm@23164
   271
  Fractions are reduced later by the cancel_numeral_factor simproc.*)
wenzelm@23164
   272
fun add_frac ((p1 : IntInf.int, q1 : IntInf.int), (p2, q2)) = (p1 * q2 + p2 * q1, q1 * q2);
wenzelm@23164
   273
wenzelm@23164
   274
val mk_divide = HOLogic.mk_binop @{const_name HOL.divide};
wenzelm@23164
   275
wenzelm@23164
   276
(*Build term (p / q) * t*)
wenzelm@23164
   277
fun mk_fcoeff ((p, q), t) =
wenzelm@23164
   278
  let val T = Term.fastype_of t
nipkow@23400
   279
  in mk_times (mk_divide (mk_number T p, mk_number T q), t) end;
wenzelm@23164
   280
wenzelm@23164
   281
(*Express t as a product of a fraction with other sorted terms*)
wenzelm@23164
   282
fun dest_fcoeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_fcoeff (~sign) t
wenzelm@23164
   283
  | dest_fcoeff sign (Const (@{const_name HOL.divide}, _) $ t $ u) =
wenzelm@23164
   284
    let val (p, t') = dest_coeff sign t
wenzelm@23164
   285
        val (q, u') = dest_coeff 1 u
nipkow@23400
   286
    in (mk_frac (p, q), mk_divide (t', u')) end
wenzelm@23164
   287
  | dest_fcoeff sign t =
wenzelm@23164
   288
    let val (p, t') = dest_coeff sign t
wenzelm@23164
   289
        val T = Term.fastype_of t
nipkow@23400
   290
    in (mk_frac (p, 1), mk_divide (t', one_of T)) end;
wenzelm@23164
   291
wenzelm@23164
   292
nipkow@23400
   293
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
wenzelm@23164
   294
val add_0s =  thms "add_0s";
nipkow@23400
   295
val mult_1s = thms "mult_1s" @ [thm"mult_1_left", thm"mult_1_right", thm"divide_1"];
wenzelm@23164
   296
wenzelm@23164
   297
(*Simplify inverse Numeral1, a/Numeral1*)
wenzelm@23164
   298
val inverse_1s = [@{thm inverse_numeral_1}];
wenzelm@23164
   299
val divide_1s = [@{thm divide_numeral_1}];
wenzelm@23164
   300
wenzelm@23164
   301
(*To perform binary arithmetic.  The "left" rewriting handles patterns
wenzelm@23164
   302
  created by the Int_Numeral_Base_Simprocs, such as 3 * (5 * x). *)
wenzelm@23164
   303
val simps = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym,
wenzelm@23164
   304
                 add_number_of_left, mult_number_of_left] @
wenzelm@23164
   305
                arith_simps @ rel_simps;
wenzelm@23164
   306
wenzelm@23164
   307
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
wenzelm@23164
   308
  during re-arrangement*)
wenzelm@23164
   309
val non_add_simps =
wenzelm@23164
   310
  subtract Thm.eq_thm [add_number_of_left, number_of_add RS sym] simps;
wenzelm@23164
   311
wenzelm@23164
   312
(*To evaluate binary negations of coefficients*)
wenzelm@23164
   313
val minus_simps = [numeral_m1_eq_minus_1 RS sym, number_of_minus RS sym,
wenzelm@23164
   314
                   minus_1, minus_0, minus_Pls, minus_Min,
wenzelm@23164
   315
                   pred_1, pred_0, pred_Pls, pred_Min];
wenzelm@23164
   316
wenzelm@23164
   317
(*To let us treat subtraction as addition*)
wenzelm@23164
   318
val diff_simps = [@{thm diff_minus}, @{thm minus_add_distrib}, @{thm minus_minus}];
wenzelm@23164
   319
wenzelm@23164
   320
(*To let us treat division as multiplication*)
wenzelm@23164
   321
val divide_simps = [@{thm divide_inverse}, @{thm inverse_mult_distrib}, @{thm inverse_inverse_eq}];
wenzelm@23164
   322
wenzelm@23164
   323
(*push the unary minus down: - x * y = x * - y *)
wenzelm@23164
   324
val minus_mult_eq_1_to_2 =
wenzelm@23164
   325
    [@{thm minus_mult_left} RS sym, @{thm minus_mult_right}] MRS trans |> standard;
wenzelm@23164
   326
wenzelm@23164
   327
(*to extract again any uncancelled minuses*)
wenzelm@23164
   328
val minus_from_mult_simps =
wenzelm@23164
   329
    [@{thm minus_minus}, @{thm minus_mult_left} RS sym, @{thm minus_mult_right} RS sym];
wenzelm@23164
   330
wenzelm@23164
   331
(*combine unary minus with numeric literals, however nested within a product*)
wenzelm@23164
   332
val mult_minus_simps =
wenzelm@23164
   333
    [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
wenzelm@23164
   334
wenzelm@23164
   335
(*Apply the given rewrite (if present) just once*)
wenzelm@23164
   336
fun trans_tac NONE      = all_tac
wenzelm@23164
   337
  | trans_tac (SOME th) = ALLGOALS (rtac (th RS trans));
wenzelm@23164
   338
wenzelm@23164
   339
fun simplify_meta_eq rules =
wenzelm@23164
   340
  let val ss0 = HOL_basic_ss addeqcongs [eq_cong2] addsimps rules
wenzelm@23164
   341
  in fn ss => simplify (Simplifier.inherit_context ss ss0) o mk_meta_eq end
wenzelm@23164
   342
wenzelm@23164
   343
structure CancelNumeralsCommon =
wenzelm@23164
   344
  struct
wenzelm@23164
   345
  val mk_sum            = mk_sum
wenzelm@23164
   346
  val dest_sum          = dest_sum
wenzelm@23164
   347
  val mk_coeff          = mk_coeff
wenzelm@23164
   348
  val dest_coeff        = dest_coeff 1
wenzelm@23164
   349
  val find_first_coeff  = find_first_coeff []
wenzelm@23164
   350
  val trans_tac         = fn _ => trans_tac
wenzelm@23164
   351
wenzelm@23164
   352
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
haftmann@23881
   353
    diff_simps @ minus_simps @ @{thms add_ac}
wenzelm@23164
   354
  val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
haftmann@23881
   355
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
wenzelm@23164
   356
  fun norm_tac ss =
wenzelm@23164
   357
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23164
   358
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23164
   359
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
wenzelm@23164
   360
wenzelm@23164
   361
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
wenzelm@23164
   362
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
wenzelm@23164
   363
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s)
wenzelm@23164
   364
  end;
wenzelm@23164
   365
wenzelm@23164
   366
wenzelm@23164
   367
structure EqCancelNumerals = CancelNumeralsFun
wenzelm@23164
   368
 (open CancelNumeralsCommon
wenzelm@23164
   369
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
wenzelm@23164
   370
  val mk_bal   = HOLogic.mk_eq
wenzelm@23164
   371
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
wenzelm@23164
   372
  val bal_add1 = eq_add_iff1 RS trans
wenzelm@23164
   373
  val bal_add2 = eq_add_iff2 RS trans
wenzelm@23164
   374
);
wenzelm@23164
   375
wenzelm@23164
   376
structure LessCancelNumerals = CancelNumeralsFun
wenzelm@23164
   377
 (open CancelNumeralsCommon
wenzelm@23164
   378
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
haftmann@23881
   379
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less}
haftmann@23881
   380
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less} Term.dummyT
wenzelm@23164
   381
  val bal_add1 = less_add_iff1 RS trans
wenzelm@23164
   382
  val bal_add2 = less_add_iff2 RS trans
wenzelm@23164
   383
);
wenzelm@23164
   384
wenzelm@23164
   385
structure LeCancelNumerals = CancelNumeralsFun
wenzelm@23164
   386
 (open CancelNumeralsCommon
wenzelm@23164
   387
  val prove_conv = Int_Numeral_Base_Simprocs.prove_conv
haftmann@23881
   388
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less_eq}
haftmann@23881
   389
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} Term.dummyT
wenzelm@23164
   390
  val bal_add1 = le_add_iff1 RS trans
wenzelm@23164
   391
  val bal_add2 = le_add_iff2 RS trans
wenzelm@23164
   392
);
wenzelm@23164
   393
wenzelm@23164
   394
val cancel_numerals =
wenzelm@23164
   395
  map Int_Numeral_Base_Simprocs.prep_simproc
wenzelm@23164
   396
   [("inteq_cancel_numerals",
wenzelm@23164
   397
     ["(l::'a::number_ring) + m = n",
wenzelm@23164
   398
      "(l::'a::number_ring) = m + n",
wenzelm@23164
   399
      "(l::'a::number_ring) - m = n",
wenzelm@23164
   400
      "(l::'a::number_ring) = m - n",
wenzelm@23164
   401
      "(l::'a::number_ring) * m = n",
wenzelm@23164
   402
      "(l::'a::number_ring) = m * n"],
wenzelm@23164
   403
     K EqCancelNumerals.proc),
wenzelm@23164
   404
    ("intless_cancel_numerals",
wenzelm@23164
   405
     ["(l::'a::{ordered_idom,number_ring}) + m < n",
wenzelm@23164
   406
      "(l::'a::{ordered_idom,number_ring}) < m + n",
wenzelm@23164
   407
      "(l::'a::{ordered_idom,number_ring}) - m < n",
wenzelm@23164
   408
      "(l::'a::{ordered_idom,number_ring}) < m - n",
wenzelm@23164
   409
      "(l::'a::{ordered_idom,number_ring}) * m < n",
wenzelm@23164
   410
      "(l::'a::{ordered_idom,number_ring}) < m * n"],
wenzelm@23164
   411
     K LessCancelNumerals.proc),
wenzelm@23164
   412
    ("intle_cancel_numerals",
wenzelm@23164
   413
     ["(l::'a::{ordered_idom,number_ring}) + m <= n",
wenzelm@23164
   414
      "(l::'a::{ordered_idom,number_ring}) <= m + n",
wenzelm@23164
   415
      "(l::'a::{ordered_idom,number_ring}) - m <= n",
wenzelm@23164
   416
      "(l::'a::{ordered_idom,number_ring}) <= m - n",
wenzelm@23164
   417
      "(l::'a::{ordered_idom,number_ring}) * m <= n",
wenzelm@23164
   418
      "(l::'a::{ordered_idom,number_ring}) <= m * n"],
wenzelm@23164
   419
     K LeCancelNumerals.proc)];
wenzelm@23164
   420
wenzelm@23164
   421
wenzelm@23164
   422
structure CombineNumeralsData =
wenzelm@23164
   423
  struct
wenzelm@23164
   424
  type coeff            = IntInf.int
wenzelm@23164
   425
  val iszero            = (fn x : IntInf.int => x = 0)
wenzelm@23164
   426
  val add               = IntInf.+
wenzelm@23164
   427
  val mk_sum            = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
wenzelm@23164
   428
  val dest_sum          = dest_sum
wenzelm@23164
   429
  val mk_coeff          = mk_coeff
wenzelm@23164
   430
  val dest_coeff        = dest_coeff 1
wenzelm@23164
   431
  val left_distrib      = combine_common_factor RS trans
wenzelm@23164
   432
  val prove_conv        = Int_Numeral_Base_Simprocs.prove_conv_nohyps
wenzelm@23164
   433
  val trans_tac         = fn _ => trans_tac
wenzelm@23164
   434
wenzelm@23164
   435
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
haftmann@23881
   436
    diff_simps @ minus_simps @ @{thms add_ac}
wenzelm@23164
   437
  val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
haftmann@23881
   438
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
wenzelm@23164
   439
  fun norm_tac ss =
wenzelm@23164
   440
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23164
   441
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23164
   442
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
wenzelm@23164
   443
wenzelm@23164
   444
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
wenzelm@23164
   445
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
wenzelm@23164
   446
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s)
wenzelm@23164
   447
  end;
wenzelm@23164
   448
wenzelm@23164
   449
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
wenzelm@23164
   450
wenzelm@23164
   451
(*Version for fields, where coefficients can be fractions*)
wenzelm@23164
   452
structure FieldCombineNumeralsData =
wenzelm@23164
   453
  struct
wenzelm@23164
   454
  type coeff            = IntInf.int * IntInf.int
wenzelm@23164
   455
  val iszero            = (fn (p : IntInf.int, q) => p = 0)
wenzelm@23164
   456
  val add               = add_frac
wenzelm@23164
   457
  val mk_sum            = long_mk_sum
wenzelm@23164
   458
  val dest_sum          = dest_sum
wenzelm@23164
   459
  val mk_coeff          = mk_fcoeff
wenzelm@23164
   460
  val dest_coeff        = dest_fcoeff 1
wenzelm@23164
   461
  val left_distrib      = combine_common_factor RS trans
wenzelm@23164
   462
  val prove_conv        = Int_Numeral_Base_Simprocs.prove_conv_nohyps
wenzelm@23164
   463
  val trans_tac         = fn _ => trans_tac
wenzelm@23164
   464
wenzelm@23164
   465
  val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
haftmann@23881
   466
    inverse_1s @ divide_simps @ diff_simps @ minus_simps @ @{thms add_ac}
wenzelm@23164
   467
  val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
haftmann@23881
   468
  val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
wenzelm@23164
   469
  fun norm_tac ss =
wenzelm@23164
   470
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23164
   471
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23164
   472
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
wenzelm@23164
   473
wenzelm@23164
   474
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps @ [@{thm add_frac_eq}]
wenzelm@23164
   475
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
wenzelm@23164
   476
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s @ divide_1s)
wenzelm@23164
   477
  end;
wenzelm@23164
   478
wenzelm@23164
   479
structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);
wenzelm@23164
   480
wenzelm@23164
   481
val combine_numerals =
wenzelm@23164
   482
  Int_Numeral_Base_Simprocs.prep_simproc
wenzelm@23164
   483
    ("int_combine_numerals", 
wenzelm@23164
   484
     ["(i::'a::number_ring) + j", "(i::'a::number_ring) - j"], 
wenzelm@23164
   485
     K CombineNumerals.proc);
wenzelm@23164
   486
wenzelm@23164
   487
val field_combine_numerals =
wenzelm@23164
   488
  Int_Numeral_Base_Simprocs.prep_simproc
wenzelm@23164
   489
    ("field_combine_numerals", 
wenzelm@23164
   490
     ["(i::'a::{number_ring,field,division_by_zero}) + j",
wenzelm@23164
   491
      "(i::'a::{number_ring,field,division_by_zero}) - j"], 
wenzelm@23164
   492
     K FieldCombineNumerals.proc);
wenzelm@23164
   493
wenzelm@23164
   494
end;
wenzelm@23164
   495
wenzelm@23164
   496
Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
wenzelm@23164
   497
Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
wenzelm@23164
   498
Addsimprocs [Int_Numeral_Simprocs.field_combine_numerals];
wenzelm@23164
   499
wenzelm@23164
   500
(*examples:
wenzelm@23164
   501
print_depth 22;
wenzelm@23164
   502
set timing;
wenzelm@23164
   503
set trace_simp;
wenzelm@23164
   504
fun test s = (Goal s, by (Simp_tac 1));
wenzelm@23164
   505
wenzelm@23164
   506
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
wenzelm@23164
   507
wenzelm@23164
   508
test "2*u = (u::int)";
wenzelm@23164
   509
test "(i + j + 12 + (k::int)) - 15 = y";
wenzelm@23164
   510
test "(i + j + 12 + (k::int)) - 5 = y";
wenzelm@23164
   511
wenzelm@23164
   512
test "y - b < (b::int)";
wenzelm@23164
   513
test "y - (3*b + c) < (b::int) - 2*c";
wenzelm@23164
   514
wenzelm@23164
   515
test "(2*x - (u*v) + y) - v*3*u = (w::int)";
wenzelm@23164
   516
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
wenzelm@23164
   517
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
wenzelm@23164
   518
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
wenzelm@23164
   519
wenzelm@23164
   520
test "(i + j + 12 + (k::int)) = u + 15 + y";
wenzelm@23164
   521
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
wenzelm@23164
   522
wenzelm@23164
   523
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)";
wenzelm@23164
   524
wenzelm@23164
   525
test "a + -(b+c) + b = (d::int)";
wenzelm@23164
   526
test "a + -(b+c) - b = (d::int)";
wenzelm@23164
   527
wenzelm@23164
   528
(*negative numerals*)
wenzelm@23164
   529
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
wenzelm@23164
   530
test "(i + j + -3 + (k::int)) < u + 5 + y";
wenzelm@23164
   531
test "(i + j + 3 + (k::int)) < u + -6 + y";
wenzelm@23164
   532
test "(i + j + -12 + (k::int)) - 15 = y";
wenzelm@23164
   533
test "(i + j + 12 + (k::int)) - -15 = y";
wenzelm@23164
   534
test "(i + j + -12 + (k::int)) - -15 = y";
wenzelm@23164
   535
*)
wenzelm@23164
   536
wenzelm@23164
   537
wenzelm@23164
   538
(** Constant folding for multiplication in semirings **)
wenzelm@23164
   539
wenzelm@23164
   540
(*We do not need folding for addition: combine_numerals does the same thing*)
wenzelm@23164
   541
wenzelm@23164
   542
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
wenzelm@23164
   543
struct
haftmann@23881
   544
  val assoc_ss = HOL_ss addsimps @{thms mult_ac}
wenzelm@23164
   545
  val eq_reflection = eq_reflection
wenzelm@23164
   546
end;
wenzelm@23164
   547
wenzelm@23164
   548
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
wenzelm@23164
   549
wenzelm@23164
   550
val assoc_fold_simproc =
wenzelm@23164
   551
  Int_Numeral_Base_Simprocs.prep_simproc
wenzelm@23164
   552
   ("semiring_assoc_fold", ["(a::'a::comm_semiring_1_cancel) * b"],
wenzelm@23164
   553
    K Semiring_Times_Assoc.proc);
wenzelm@23164
   554
wenzelm@23164
   555
Addsimprocs [assoc_fold_simproc];
wenzelm@23164
   556
wenzelm@23164
   557
wenzelm@23164
   558
wenzelm@23164
   559
wenzelm@23164
   560
(*** decision procedure for linear arithmetic ***)
wenzelm@23164
   561
wenzelm@23164
   562
(*---------------------------------------------------------------------------*)
wenzelm@23164
   563
(* Linear arithmetic                                                         *)
wenzelm@23164
   564
(*---------------------------------------------------------------------------*)
wenzelm@23164
   565
wenzelm@23164
   566
(*
wenzelm@23164
   567
Instantiation of the generic linear arithmetic package for int.
wenzelm@23164
   568
*)
wenzelm@23164
   569
wenzelm@23164
   570
(* Update parameters of arithmetic prover *)
wenzelm@23164
   571
local
wenzelm@23164
   572
nipkow@24266
   573
(* reduce contradictory =/</<= to False *)
nipkow@24266
   574
nipkow@24266
   575
(* Evaluation of terms of the form "m R n" where R is one of "=", "<=" or "<",
nipkow@24266
   576
   and m and n are ground terms over rings (roughly speaking).
nipkow@24266
   577
   That is, m and n consist only of 1s combined with "+", "-" and "*".
nipkow@24266
   578
*)
nipkow@24266
   579
local
nipkow@24266
   580
val zeroth = (symmetric o mk_meta_eq) @{thm of_int_0};
nipkow@24266
   581
val lhss0 = [@{cpat "0::?'a::ring"}];
nipkow@24266
   582
fun proc0 phi ss ct =
nipkow@24266
   583
  let val T = ctyp_of_term ct
nipkow@24266
   584
  in if typ_of T = @{typ int} then NONE else
nipkow@24266
   585
     SOME (instantiate' [SOME T] [] zeroth)
nipkow@24266
   586
  end;
nipkow@24266
   587
val zero_to_of_int_zero_simproc =
nipkow@24266
   588
  make_simproc {lhss = lhss0, name = "zero_to_of_int_zero_simproc",
nipkow@24266
   589
  proc = proc0, identifier = []};
nipkow@24266
   590
nipkow@24266
   591
val oneth = (symmetric o mk_meta_eq) @{thm of_int_1};
nipkow@24266
   592
val lhss1 = [@{cpat "1::?'a::ring_1"}];
nipkow@24266
   593
fun proc1 phi ss ct =
nipkow@24266
   594
  let val T = ctyp_of_term ct
nipkow@24266
   595
  in if typ_of T = @{typ int} then NONE else
nipkow@24266
   596
     SOME (instantiate' [SOME T] [] oneth)
nipkow@24266
   597
  end;
nipkow@24266
   598
val one_to_of_int_one_simproc =
nipkow@24266
   599
  make_simproc {lhss = lhss1, name = "one_to_of_int_one_simproc",
nipkow@24266
   600
  proc = proc1, identifier = []};
nipkow@24266
   601
nipkow@24266
   602
val allowed_consts =
nipkow@24266
   603
  [@{const_name "op ="}, @{const_name "HOL.times"}, @{const_name "HOL.uminus"},
nipkow@24266
   604
   @{const_name "HOL.minus"}, @{const_name "HOL.plus"},
nipkow@24266
   605
   @{const_name "HOL.zero"}, @{const_name "HOL.one"}, @{const_name "HOL.less"},
nipkow@24266
   606
   @{const_name "HOL.less_eq"}];
nipkow@24266
   607
nipkow@24266
   608
fun check t = case t of
nipkow@24266
   609
   Const(s,t) => if s = @{const_name "HOL.one"} then not (t = @{typ int})
nipkow@24266
   610
                else s mem_string allowed_consts
nipkow@24266
   611
 | a$b => check a andalso check b
nipkow@24266
   612
 | _ => false;
nipkow@24266
   613
nipkow@24266
   614
val conv =
nipkow@24266
   615
  Simplifier.rewrite
nipkow@24266
   616
   (HOL_basic_ss addsimps
nipkow@24266
   617
     ((map (fn th => th RS sym) [@{thm of_int_add}, @{thm of_int_mult},
nipkow@24266
   618
             @{thm of_int_diff},  @{thm of_int_minus}])@
nipkow@24266
   619
      [@{thm of_int_less_iff}, @{thm of_int_le_iff}, @{thm of_int_eq_iff}])
nipkow@24266
   620
     addsimprocs [zero_to_of_int_zero_simproc,one_to_of_int_one_simproc]);
nipkow@24266
   621
nipkow@24266
   622
fun sproc phi ss ct = if check (term_of ct) then SOME (conv ct) else NONE
nipkow@24266
   623
val lhss' =
nipkow@24266
   624
  [@{cpat "(?x::?'a::ring_char_0) = (?y::?'a)"},
nipkow@24266
   625
   @{cpat "(?x::?'a::ordered_idom) < (?y::?'a)"},
nipkow@24266
   626
   @{cpat "(?x::?'a::ordered_idom) <= (?y::?'a)"}]
nipkow@24266
   627
in
nipkow@24266
   628
val zero_one_idom_simproc =
nipkow@24266
   629
  make_simproc {lhss = lhss' , name = "zero_one_idom_simproc",
nipkow@24266
   630
  proc = sproc, identifier = []}
nipkow@24266
   631
end;
nipkow@24266
   632
wenzelm@23164
   633
val add_rules =
wenzelm@23164
   634
    simp_thms @ arith_simps @ rel_simps @ arith_special @
wenzelm@23164
   635
    [@{thm neg_le_iff_le}, @{thm numeral_0_eq_0}, @{thm numeral_1_eq_1},
wenzelm@23164
   636
     @{thm minus_zero}, @{thm diff_minus}, @{thm left_minus}, @{thm right_minus},
wenzelm@23164
   637
     @{thm mult_zero_left}, @{thm mult_zero_right}, @{thm mult_num1}, @{thm mult_1_right},
wenzelm@23164
   638
     @{thm minus_mult_left} RS sym, @{thm minus_mult_right} RS sym,
wenzelm@23164
   639
     @{thm minus_add_distrib}, @{thm minus_minus}, @{thm mult_assoc},
huffman@23365
   640
     @{thm of_nat_0}, @{thm of_nat_1}, @{thm of_nat_Suc}, @{thm of_nat_add},
huffman@23365
   641
     @{thm of_nat_mult}, @{thm of_int_0}, @{thm of_int_1}, @{thm of_int_add},
huffman@23365
   642
     @{thm of_int_mult}]
wenzelm@23164
   643
huffman@23365
   644
val nat_inj_thms = [@{thm zle_int} RS iffD2, @{thm int_int_eq} RS iffD2]
wenzelm@23164
   645
nipkow@24266
   646
val Int_Numeral_Base_Simprocs = assoc_fold_simproc :: zero_one_idom_simproc
wenzelm@23164
   647
  :: Int_Numeral_Simprocs.combine_numerals
wenzelm@23164
   648
  :: Int_Numeral_Simprocs.cancel_numerals;
wenzelm@23164
   649
wenzelm@23164
   650
in
wenzelm@23164
   651
wenzelm@23164
   652
val int_arith_setup =
wenzelm@24093
   653
  LinArith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
wenzelm@23164
   654
   {add_mono_thms = add_mono_thms,
wenzelm@23164
   655
    mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono} :: mult_mono_thms,
wenzelm@23164
   656
    inj_thms = nat_inj_thms @ inj_thms,
wenzelm@23164
   657
    lessD = lessD @ [zless_imp_add1_zle],
wenzelm@23164
   658
    neqE = neqE,
wenzelm@23164
   659
    simpset = simpset addsimps add_rules
wenzelm@23164
   660
                      addsimprocs Int_Numeral_Base_Simprocs
wenzelm@23164
   661
                      addcongs [if_weak_cong]}) #>
haftmann@24196
   662
  arith_inj_const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT) #>
wenzelm@23164
   663
  arith_discrete "IntDef.int"
wenzelm@23164
   664
wenzelm@23164
   665
end;
wenzelm@23164
   666
wenzelm@23164
   667
val fast_int_arith_simproc =
wenzelm@23164
   668
  Simplifier.simproc @{theory}
wenzelm@23164
   669
  "fast_int_arith" 
wenzelm@23164
   670
     ["(m::'a::{ordered_idom,number_ring}) < n",
wenzelm@23164
   671
      "(m::'a::{ordered_idom,number_ring}) <= n",
wenzelm@24093
   672
      "(m::'a::{ordered_idom,number_ring}) = n"] (K LinArith.lin_arith_simproc);
wenzelm@23164
   673
wenzelm@23164
   674
Addsimprocs [fast_int_arith_simproc];