src/HOL/Tools/int_arith.ML
author huffman
Wed Apr 29 17:15:01 2009 -0700 (2009-04-29)
changeset 31024 0fdf666e08bf
parent 30802 f9e9e800d27e
child 31068 f591144b0f17
permissions -rw-r--r--
reimplement reorientation simproc using theory data
     1 (* Authors: Larry Paulson and Tobias Nipkow
     2 
     3 Simprocs and decision procedure for numerals and linear arithmetic.
     4 *)
     5 
     6 structure Int_Numeral_Simprocs =
     7 struct
     8 
     9 (** Utilities **)
    10 
    11 fun mk_number T n = HOLogic.number_of_const T $ HOLogic.mk_numeral n;
    12 
    13 fun find_first_numeral past (t::terms) =
    14         ((snd (HOLogic.dest_number t), rev past @ terms)
    15          handle TERM _ => find_first_numeral (t::past) terms)
    16   | find_first_numeral past [] = raise TERM("find_first_numeral", []);
    17 
    18 val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
    19 
    20 fun mk_minus t = 
    21   let val T = Term.fastype_of t
    22   in Const (@{const_name HOL.uminus}, T --> T) $ t end;
    23 
    24 (*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
    25 fun mk_sum T []        = mk_number T 0
    26   | mk_sum T [t,u]     = mk_plus (t, u)
    27   | mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
    28 
    29 (*this version ALWAYS includes a trailing zero*)
    30 fun long_mk_sum T []        = mk_number T 0
    31   | long_mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
    32 
    33 val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} Term.dummyT;
    34 
    35 (*decompose additions AND subtractions as a sum*)
    36 fun dest_summing (pos, Const (@{const_name HOL.plus}, _) $ t $ u, ts) =
    37         dest_summing (pos, t, dest_summing (pos, u, ts))
    38   | dest_summing (pos, Const (@{const_name HOL.minus}, _) $ t $ u, ts) =
    39         dest_summing (pos, t, dest_summing (not pos, u, ts))
    40   | dest_summing (pos, t, ts) =
    41         if pos then t::ts else mk_minus t :: ts;
    42 
    43 fun dest_sum t = dest_summing (true, t, []);
    44 
    45 val mk_diff = HOLogic.mk_binop @{const_name HOL.minus};
    46 val dest_diff = HOLogic.dest_bin @{const_name HOL.minus} Term.dummyT;
    47 
    48 val mk_times = HOLogic.mk_binop @{const_name HOL.times};
    49 
    50 fun one_of T = Const(@{const_name HOL.one},T);
    51 
    52 (* build product with trailing 1 rather than Numeral 1 in order to avoid the
    53    unnecessary restriction to type class number_ring
    54    which is not required for cancellation of common factors in divisions.
    55 *)
    56 fun mk_prod T = 
    57   let val one = one_of T
    58   fun mk [] = one
    59     | mk [t] = t
    60     | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)
    61   in mk end;
    62 
    63 (*This version ALWAYS includes a trailing one*)
    64 fun long_mk_prod T []        = one_of T
    65   | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);
    66 
    67 val dest_times = HOLogic.dest_bin @{const_name HOL.times} Term.dummyT;
    68 
    69 fun dest_prod t =
    70       let val (t,u) = dest_times t
    71       in dest_prod t @ dest_prod u end
    72       handle TERM _ => [t];
    73 
    74 (*DON'T do the obvious simplifications; that would create special cases*)
    75 fun mk_coeff (k, t) = mk_times (mk_number (Term.fastype_of t) k, t);
    76 
    77 (*Express t as a product of (possibly) a numeral with other sorted terms*)
    78 fun dest_coeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_coeff (~sign) t
    79   | dest_coeff sign t =
    80     let val ts = sort TermOrd.term_ord (dest_prod t)
    81         val (n, ts') = find_first_numeral [] ts
    82                           handle TERM _ => (1, ts)
    83     in (sign*n, mk_prod (Term.fastype_of t) ts') end;
    84 
    85 (*Find first coefficient-term THAT MATCHES u*)
    86 fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
    87   | find_first_coeff past u (t::terms) =
    88         let val (n,u') = dest_coeff 1 t
    89         in if u aconv u' then (n, rev past @ terms)
    90                          else find_first_coeff (t::past) u terms
    91         end
    92         handle TERM _ => find_first_coeff (t::past) u terms;
    93 
    94 (*Fractions as pairs of ints. Can't use Rat.rat because the representation
    95   needs to preserve negative values in the denominator.*)
    96 fun mk_frac (p, q) = if q = 0 then raise Div else (p, q);
    97 
    98 (*Don't reduce fractions; sums must be proved by rule add_frac_eq.
    99   Fractions are reduced later by the cancel_numeral_factor simproc.*)
   100 fun add_frac ((p1, q1), (p2, q2)) = (p1 * q2 + p2 * q1, q1 * q2);
   101 
   102 val mk_divide = HOLogic.mk_binop @{const_name HOL.divide};
   103 
   104 (*Build term (p / q) * t*)
   105 fun mk_fcoeff ((p, q), t) =
   106   let val T = Term.fastype_of t
   107   in mk_times (mk_divide (mk_number T p, mk_number T q), t) end;
   108 
   109 (*Express t as a product of a fraction with other sorted terms*)
   110 fun dest_fcoeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_fcoeff (~sign) t
   111   | dest_fcoeff sign (Const (@{const_name HOL.divide}, _) $ t $ u) =
   112     let val (p, t') = dest_coeff sign t
   113         val (q, u') = dest_coeff 1 u
   114     in (mk_frac (p, q), mk_divide (t', u')) end
   115   | dest_fcoeff sign t =
   116     let val (p, t') = dest_coeff sign t
   117         val T = Term.fastype_of t
   118     in (mk_frac (p, 1), mk_divide (t', one_of T)) end;
   119 
   120 
   121 (** New term ordering so that AC-rewriting brings numerals to the front **)
   122 
   123 (*Order integers by absolute value and then by sign. The standard integer
   124   ordering is not well-founded.*)
   125 fun num_ord (i,j) =
   126   (case int_ord (abs i, abs j) of
   127     EQUAL => int_ord (Int.sign i, Int.sign j) 
   128   | ord => ord);
   129 
   130 (*This resembles TermOrd.term_ord, but it puts binary numerals before other
   131   non-atomic terms.*)
   132 local open Term 
   133 in 
   134 fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =
   135       (case numterm_ord (t, u) of EQUAL => TermOrd.typ_ord (T, U) | ord => ord)
   136   | numterm_ord
   137      (Const(@{const_name Int.number_of}, _) $ v, Const(@{const_name Int.number_of}, _) $ w) =
   138      num_ord (HOLogic.dest_numeral v, HOLogic.dest_numeral w)
   139   | numterm_ord (Const(@{const_name Int.number_of}, _) $ _, _) = LESS
   140   | numterm_ord (_, Const(@{const_name Int.number_of}, _) $ _) = GREATER
   141   | numterm_ord (t, u) =
   142       (case int_ord (size_of_term t, size_of_term u) of
   143         EQUAL =>
   144           let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
   145             (case TermOrd.hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)
   146           end
   147       | ord => ord)
   148 and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
   149 end;
   150 
   151 fun numtermless tu = (numterm_ord tu = LESS);
   152 
   153 val num_ss = HOL_ss settermless numtermless;
   154 
   155 (*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic isn't complicated by the abstract 0 and 1.*)
   156 val numeral_syms = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym];
   157 
   158 (*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
   159 val add_0s =  @{thms add_0s};
   160 val mult_1s = @{thms mult_1s mult_1_left mult_1_right divide_1};
   161 
   162 (*Simplify inverse Numeral1, a/Numeral1*)
   163 val inverse_1s = [@{thm inverse_numeral_1}];
   164 val divide_1s = [@{thm divide_numeral_1}];
   165 
   166 (*To perform binary arithmetic.  The "left" rewriting handles patterns
   167   created by the Int_Numeral_Simprocs, such as 3 * (5 * x). *)
   168 val simps = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym,
   169                  @{thm add_number_of_left}, @{thm mult_number_of_left}] @
   170                 @{thms arith_simps} @ @{thms rel_simps};
   171 
   172 (*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
   173   during re-arrangement*)
   174 val non_add_simps =
   175   subtract Thm.eq_thm [@{thm add_number_of_left}, @{thm number_of_add} RS sym] simps;
   176 
   177 (*To evaluate binary negations of coefficients*)
   178 val minus_simps = [@{thm numeral_m1_eq_minus_1} RS sym, @{thm number_of_minus} RS sym] @
   179                    @{thms minus_bin_simps} @ @{thms pred_bin_simps};
   180 
   181 (*To let us treat subtraction as addition*)
   182 val diff_simps = [@{thm diff_minus}, @{thm minus_add_distrib}, @{thm minus_minus}];
   183 
   184 (*To let us treat division as multiplication*)
   185 val divide_simps = [@{thm divide_inverse}, @{thm inverse_mult_distrib}, @{thm inverse_inverse_eq}];
   186 
   187 (*push the unary minus down: - x * y = x * - y *)
   188 val minus_mult_eq_1_to_2 =
   189     [@{thm mult_minus_left}, @{thm minus_mult_right}] MRS trans |> standard;
   190 
   191 (*to extract again any uncancelled minuses*)
   192 val minus_from_mult_simps =
   193     [@{thm minus_minus}, @{thm mult_minus_left}, @{thm mult_minus_right}];
   194 
   195 (*combine unary minus with numeric literals, however nested within a product*)
   196 val mult_minus_simps =
   197     [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
   198 
   199 val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
   200   diff_simps @ minus_simps @ @{thms add_ac}
   201 val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
   202 val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
   203 
   204 structure CancelNumeralsCommon =
   205   struct
   206   val mk_sum            = mk_sum
   207   val dest_sum          = dest_sum
   208   val mk_coeff          = mk_coeff
   209   val dest_coeff        = dest_coeff 1
   210   val find_first_coeff  = find_first_coeff []
   211   val trans_tac         = K Arith_Data.trans_tac
   212 
   213   fun norm_tac ss =
   214     ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
   215     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
   216     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
   217 
   218   val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
   219   fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
   220   val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s)
   221   end;
   222 
   223 
   224 structure EqCancelNumerals = CancelNumeralsFun
   225  (open CancelNumeralsCommon
   226   val prove_conv = Arith_Data.prove_conv
   227   val mk_bal   = HOLogic.mk_eq
   228   val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
   229   val bal_add1 = @{thm eq_add_iff1} RS trans
   230   val bal_add2 = @{thm eq_add_iff2} RS trans
   231 );
   232 
   233 structure LessCancelNumerals = CancelNumeralsFun
   234  (open CancelNumeralsCommon
   235   val prove_conv = Arith_Data.prove_conv
   236   val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less}
   237   val dest_bal = HOLogic.dest_bin @{const_name HOL.less} Term.dummyT
   238   val bal_add1 = @{thm less_add_iff1} RS trans
   239   val bal_add2 = @{thm less_add_iff2} RS trans
   240 );
   241 
   242 structure LeCancelNumerals = CancelNumeralsFun
   243  (open CancelNumeralsCommon
   244   val prove_conv = Arith_Data.prove_conv
   245   val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less_eq}
   246   val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} Term.dummyT
   247   val bal_add1 = @{thm le_add_iff1} RS trans
   248   val bal_add2 = @{thm le_add_iff2} RS trans
   249 );
   250 
   251 val cancel_numerals =
   252   map Arith_Data.prep_simproc
   253    [("inteq_cancel_numerals",
   254      ["(l::'a::number_ring) + m = n",
   255       "(l::'a::number_ring) = m + n",
   256       "(l::'a::number_ring) - m = n",
   257       "(l::'a::number_ring) = m - n",
   258       "(l::'a::number_ring) * m = n",
   259       "(l::'a::number_ring) = m * n"],
   260      K EqCancelNumerals.proc),
   261     ("intless_cancel_numerals",
   262      ["(l::'a::{ordered_idom,number_ring}) + m < n",
   263       "(l::'a::{ordered_idom,number_ring}) < m + n",
   264       "(l::'a::{ordered_idom,number_ring}) - m < n",
   265       "(l::'a::{ordered_idom,number_ring}) < m - n",
   266       "(l::'a::{ordered_idom,number_ring}) * m < n",
   267       "(l::'a::{ordered_idom,number_ring}) < m * n"],
   268      K LessCancelNumerals.proc),
   269     ("intle_cancel_numerals",
   270      ["(l::'a::{ordered_idom,number_ring}) + m <= n",
   271       "(l::'a::{ordered_idom,number_ring}) <= m + n",
   272       "(l::'a::{ordered_idom,number_ring}) - m <= n",
   273       "(l::'a::{ordered_idom,number_ring}) <= m - n",
   274       "(l::'a::{ordered_idom,number_ring}) * m <= n",
   275       "(l::'a::{ordered_idom,number_ring}) <= m * n"],
   276      K LeCancelNumerals.proc)];
   277 
   278 
   279 structure CombineNumeralsData =
   280   struct
   281   type coeff            = int
   282   val iszero            = (fn x => x = 0)
   283   val add               = op +
   284   val mk_sum            = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
   285   val dest_sum          = dest_sum
   286   val mk_coeff          = mk_coeff
   287   val dest_coeff        = dest_coeff 1
   288   val left_distrib      = @{thm combine_common_factor} RS trans
   289   val prove_conv        = Arith_Data.prove_conv_nohyps
   290   val trans_tac         = K Arith_Data.trans_tac
   291 
   292   fun norm_tac ss =
   293     ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
   294     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
   295     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
   296 
   297   val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
   298   fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
   299   val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s)
   300   end;
   301 
   302 structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
   303 
   304 (*Version for fields, where coefficients can be fractions*)
   305 structure FieldCombineNumeralsData =
   306   struct
   307   type coeff            = int * int
   308   val iszero            = (fn (p, q) => p = 0)
   309   val add               = add_frac
   310   val mk_sum            = long_mk_sum
   311   val dest_sum          = dest_sum
   312   val mk_coeff          = mk_fcoeff
   313   val dest_coeff        = dest_fcoeff 1
   314   val left_distrib      = @{thm combine_common_factor} RS trans
   315   val prove_conv        = Arith_Data.prove_conv_nohyps
   316   val trans_tac         = K Arith_Data.trans_tac
   317 
   318   val norm_ss1a = norm_ss1 addsimps inverse_1s @ divide_simps
   319   fun norm_tac ss =
   320     ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1a))
   321     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
   322     THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
   323 
   324   val numeral_simp_ss = HOL_ss addsimps add_0s @ simps @ [@{thm add_frac_eq}]
   325   fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
   326   val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s @ divide_1s)
   327   end;
   328 
   329 structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);
   330 
   331 val combine_numerals =
   332   Arith_Data.prep_simproc
   333     ("int_combine_numerals", 
   334      ["(i::'a::number_ring) + j", "(i::'a::number_ring) - j"], 
   335      K CombineNumerals.proc);
   336 
   337 val field_combine_numerals =
   338   Arith_Data.prep_simproc
   339     ("field_combine_numerals", 
   340      ["(i::'a::{number_ring,field,division_by_zero}) + j",
   341       "(i::'a::{number_ring,field,division_by_zero}) - j"], 
   342      K FieldCombineNumerals.proc);
   343 
   344 (** Constant folding for multiplication in semirings **)
   345 
   346 (*We do not need folding for addition: combine_numerals does the same thing*)
   347 
   348 structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
   349 struct
   350   val assoc_ss = HOL_ss addsimps @{thms mult_ac}
   351   val eq_reflection = eq_reflection
   352   fun is_numeral (Const(@{const_name Int.number_of}, _) $ _) = true
   353     | is_numeral _ = false;
   354 end;
   355 
   356 structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
   357 
   358 val assoc_fold_simproc =
   359   Arith_Data.prep_simproc
   360    ("semiring_assoc_fold", ["(a::'a::comm_semiring_1_cancel) * b"],
   361     K Semiring_Times_Assoc.proc);
   362 
   363 end;
   364 
   365 Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
   366 Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
   367 Addsimprocs [Int_Numeral_Simprocs.field_combine_numerals];
   368 Addsimprocs [Int_Numeral_Simprocs.assoc_fold_simproc];
   369 
   370 (*examples:
   371 print_depth 22;
   372 set timing;
   373 set trace_simp;
   374 fun test s = (Goal s, by (Simp_tac 1));
   375 
   376 test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
   377 
   378 test "2*u = (u::int)";
   379 test "(i + j + 12 + (k::int)) - 15 = y";
   380 test "(i + j + 12 + (k::int)) - 5 = y";
   381 
   382 test "y - b < (b::int)";
   383 test "y - (3*b + c) < (b::int) - 2*c";
   384 
   385 test "(2*x - (u*v) + y) - v*3*u = (w::int)";
   386 test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
   387 test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
   388 test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
   389 
   390 test "(i + j + 12 + (k::int)) = u + 15 + y";
   391 test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
   392 
   393 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)";
   394 
   395 test "a + -(b+c) + b = (d::int)";
   396 test "a + -(b+c) - b = (d::int)";
   397 
   398 (*negative numerals*)
   399 test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
   400 test "(i + j + -3 + (k::int)) < u + 5 + y";
   401 test "(i + j + 3 + (k::int)) < u + -6 + y";
   402 test "(i + j + -12 + (k::int)) - 15 = y";
   403 test "(i + j + 12 + (k::int)) - -15 = y";
   404 test "(i + j + -12 + (k::int)) - -15 = y";
   405 *)
   406 
   407 (*** decision procedure for linear arithmetic ***)
   408 
   409 (*---------------------------------------------------------------------------*)
   410 (* Linear arithmetic                                                         *)
   411 (*---------------------------------------------------------------------------*)
   412 
   413 (*
   414 Instantiation of the generic linear arithmetic package for int.
   415 *)
   416 
   417 structure Int_Arith =
   418 struct
   419 
   420 (* Update parameters of arithmetic prover *)
   421 
   422 (* reduce contradictory =/</<= to False *)
   423 
   424 (* Evaluation of terms of the form "m R n" where R is one of "=", "<=" or "<",
   425    and m and n are ground terms over rings (roughly speaking).
   426    That is, m and n consist only of 1s combined with "+", "-" and "*".
   427 *)
   428 
   429 val zeroth = (symmetric o mk_meta_eq) @{thm of_int_0};
   430 
   431 val lhss0 = [@{cpat "0::?'a::ring"}];
   432 
   433 fun proc0 phi ss ct =
   434   let val T = ctyp_of_term ct
   435   in if typ_of T = @{typ int} then NONE else
   436      SOME (instantiate' [SOME T] [] zeroth)
   437   end;
   438 
   439 val zero_to_of_int_zero_simproc =
   440   make_simproc {lhss = lhss0, name = "zero_to_of_int_zero_simproc",
   441   proc = proc0, identifier = []};
   442 
   443 val oneth = (symmetric o mk_meta_eq) @{thm of_int_1};
   444 
   445 val lhss1 = [@{cpat "1::?'a::ring_1"}];
   446 
   447 fun proc1 phi ss ct =
   448   let val T = ctyp_of_term ct
   449   in if typ_of T = @{typ int} then NONE else
   450      SOME (instantiate' [SOME T] [] oneth)
   451   end;
   452 
   453 val one_to_of_int_one_simproc =
   454   make_simproc {lhss = lhss1, name = "one_to_of_int_one_simproc",
   455   proc = proc1, identifier = []};
   456 
   457 val allowed_consts =
   458   [@{const_name "op ="}, @{const_name "HOL.times"}, @{const_name "HOL.uminus"},
   459    @{const_name "HOL.minus"}, @{const_name "HOL.plus"},
   460    @{const_name "HOL.zero"}, @{const_name "HOL.one"}, @{const_name "HOL.less"},
   461    @{const_name "HOL.less_eq"}];
   462 
   463 fun check t = case t of
   464    Const(s,t) => if s = @{const_name "HOL.one"} then not (t = @{typ int})
   465                 else s mem_string allowed_consts
   466  | a$b => check a andalso check b
   467  | _ => false;
   468 
   469 val conv =
   470   Simplifier.rewrite
   471    (HOL_basic_ss addsimps
   472      ((map (fn th => th RS sym) [@{thm of_int_add}, @{thm of_int_mult},
   473              @{thm of_int_diff},  @{thm of_int_minus}])@
   474       [@{thm of_int_less_iff}, @{thm of_int_le_iff}, @{thm of_int_eq_iff}])
   475      addsimprocs [zero_to_of_int_zero_simproc,one_to_of_int_one_simproc]);
   476 
   477 fun sproc phi ss ct = if check (term_of ct) then SOME (conv ct) else NONE
   478 
   479 val lhss' =
   480   [@{cpat "(?x::?'a::ring_char_0) = (?y::?'a)"},
   481    @{cpat "(?x::?'a::ordered_idom) < (?y::?'a)"},
   482    @{cpat "(?x::?'a::ordered_idom) <= (?y::?'a)"}]
   483 
   484 val zero_one_idom_simproc =
   485   make_simproc {lhss = lhss' , name = "zero_one_idom_simproc",
   486   proc = sproc, identifier = []}
   487 
   488 val add_rules =
   489     simp_thms @ @{thms arith_simps} @ @{thms rel_simps} @ @{thms arith_special} @
   490     @{thms int_arith_rules}
   491 
   492 val nat_inj_thms = [@{thm zle_int} RS iffD2, @{thm int_int_eq} RS iffD2]
   493 
   494 val int_numeral_base_simprocs = Int_Numeral_Simprocs.assoc_fold_simproc :: zero_one_idom_simproc
   495   :: Int_Numeral_Simprocs.combine_numerals
   496   :: Int_Numeral_Simprocs.cancel_numerals;
   497 
   498 val setup =
   499   Lin_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
   500    {add_mono_thms = add_mono_thms,
   501     mult_mono_thms = @{thm mult_strict_left_mono} :: @{thm mult_left_mono} :: mult_mono_thms,
   502     inj_thms = nat_inj_thms @ inj_thms,
   503     lessD = lessD @ [@{thm zless_imp_add1_zle}],
   504     neqE = neqE,
   505     simpset = simpset addsimps add_rules
   506                       addsimprocs int_numeral_base_simprocs
   507                       addcongs [if_weak_cong]}) #>
   508   arith_inj_const (@{const_name of_nat}, HOLogic.natT --> HOLogic.intT) #>
   509   arith_discrete @{type_name Int.int}
   510 
   511 val fast_int_arith_simproc =
   512   Simplifier.simproc (the_context ())
   513   "fast_int_arith" 
   514      ["(m::'a::{ordered_idom,number_ring}) < n",
   515       "(m::'a::{ordered_idom,number_ring}) <= n",
   516       "(m::'a::{ordered_idom,number_ring}) = n"] (K Lin_Arith.lin_arith_simproc);
   517 
   518 end;
   519 
   520 Addsimprocs [Int_Arith.fast_int_arith_simproc];