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