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