src/HOL/Integ/int_arith1.ML
author nipkow
Tue Sep 07 11:42:50 2004 +0200 (2004-09-07)
changeset 15185 8c43ffe2bb32
parent 15184 d2c19aea17bc
child 15531 08c8dad8e399
permissions -rw-r--r--
tuned "discrete" field
     1 (*  Title:      HOL/Integ/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 (** Misc ML bindings **)
     9 
    10 val bin_succ_Pls = thm"bin_succ_Pls";
    11 val bin_succ_Min = thm"bin_succ_Min";
    12 val bin_succ_1 = thm"bin_succ_1";
    13 val bin_succ_0 = thm"bin_succ_0";
    14 
    15 val bin_pred_Pls = thm"bin_pred_Pls";
    16 val bin_pred_Min = thm"bin_pred_Min";
    17 val bin_pred_1 = thm"bin_pred_1";
    18 val bin_pred_0 = thm"bin_pred_0";
    19 
    20 val bin_minus_Pls = thm"bin_minus_Pls";
    21 val bin_minus_Min = thm"bin_minus_Min";
    22 val bin_minus_1 = thm"bin_minus_1";
    23 val bin_minus_0 = thm"bin_minus_0";
    24 
    25 val bin_add_Pls = thm"bin_add_Pls";
    26 val bin_add_Min = thm"bin_add_Min";
    27 val bin_add_BIT_11 = thm"bin_add_BIT_11";
    28 val bin_add_BIT_10 = thm"bin_add_BIT_10";
    29 val bin_add_BIT_0 = thm"bin_add_BIT_0";
    30 val bin_add_Pls_right = thm"bin_add_Pls_right";
    31 val bin_add_Min_right = thm"bin_add_Min_right";
    32 
    33 val bin_mult_Pls = thm"bin_mult_Pls";
    34 val bin_mult_Min = thm"bin_mult_Min";
    35 val bin_mult_1 = thm"bin_mult_1";
    36 val bin_mult_0 = thm"bin_mult_0";
    37 
    38 val neg_def = thm "neg_def";
    39 val iszero_def = thm "iszero_def";
    40 
    41 val number_of_succ = thm"number_of_succ";
    42 val number_of_pred = thm"number_of_pred";
    43 val number_of_minus = thm"number_of_minus";
    44 val number_of_add = thm"number_of_add";
    45 val diff_number_of_eq = thm"diff_number_of_eq";
    46 val number_of_mult = thm"number_of_mult";
    47 val double_number_of_BIT = thm"double_number_of_BIT";
    48 val numeral_0_eq_0 = thm"numeral_0_eq_0";
    49 val numeral_1_eq_1 = thm"numeral_1_eq_1";
    50 val numeral_m1_eq_minus_1 = thm"numeral_m1_eq_minus_1";
    51 val mult_minus1 = thm"mult_minus1";
    52 val mult_minus1_right = thm"mult_minus1_right";
    53 val minus_number_of_mult = thm"minus_number_of_mult";
    54 val zero_less_nat_eq = thm"zero_less_nat_eq";
    55 val eq_number_of_eq = thm"eq_number_of_eq";
    56 val iszero_number_of_Pls = thm"iszero_number_of_Pls";
    57 val nonzero_number_of_Min = thm"nonzero_number_of_Min";
    58 val iszero_number_of_BIT = thm"iszero_number_of_BIT";
    59 val iszero_number_of_0 = thm"iszero_number_of_0";
    60 val iszero_number_of_1 = thm"iszero_number_of_1";
    61 val less_number_of_eq_neg = thm"less_number_of_eq_neg";
    62 val le_number_of_eq = thm"le_number_of_eq";
    63 val not_neg_number_of_Pls = thm"not_neg_number_of_Pls";
    64 val neg_number_of_Min = thm"neg_number_of_Min";
    65 val neg_number_of_BIT = thm"neg_number_of_BIT";
    66 val le_number_of_eq_not_less = thm"le_number_of_eq_not_less";
    67 val abs_number_of = thm"abs_number_of";
    68 val number_of_reorient = thm"number_of_reorient";
    69 val add_number_of_left = thm"add_number_of_left";
    70 val mult_number_of_left = thm"mult_number_of_left";
    71 val add_number_of_diff1 = thm"add_number_of_diff1";
    72 val add_number_of_diff2 = thm"add_number_of_diff2";
    73 val less_iff_diff_less_0 = thm"less_iff_diff_less_0";
    74 val eq_iff_diff_eq_0 = thm"eq_iff_diff_eq_0";
    75 val le_iff_diff_le_0 = thm"le_iff_diff_le_0";
    76 
    77 val bin_arith_extra_simps = thms"bin_arith_extra_simps";
    78 val bin_arith_simps = thms"bin_arith_simps";
    79 val bin_rel_simps = thms"bin_rel_simps";
    80 
    81 val zless_imp_add1_zle = thm "zless_imp_add1_zle";
    82 
    83 val combine_common_factor = thm"combine_common_factor";
    84 val eq_add_iff1 = thm"eq_add_iff1";
    85 val eq_add_iff2 = thm"eq_add_iff2";
    86 val less_add_iff1 = thm"less_add_iff1";
    87 val less_add_iff2 = thm"less_add_iff2";
    88 val le_add_iff1 = thm"le_add_iff1";
    89 val le_add_iff2 = thm"le_add_iff2";
    90 
    91 val arith_special = thms"arith_special";
    92 
    93 structure Bin_Simprocs =
    94   struct
    95   fun prove_conv tacs sg (hyps: thm list) xs (t, u) =
    96     if t aconv u then None
    97     else
    98       let val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u))
    99       in Some (Tactic.prove sg xs [] eq (K (EVERY tacs))) end
   100 
   101   fun prove_conv_nohyps tacs sg = prove_conv tacs sg [];
   102   fun prove_conv_nohyps_novars tacs sg = prove_conv tacs sg [] [];
   103 
   104   fun prep_simproc (name, pats, proc) =
   105     Simplifier.simproc (Theory.sign_of (the_context())) name pats proc;
   106 
   107   fun is_numeral (Const("Numeral.number_of", _) $ w) = true
   108     | is_numeral _ = false
   109 
   110   fun simplify_meta_eq f_number_of_eq f_eq =
   111       mk_meta_eq ([f_eq, f_number_of_eq] MRS trans)
   112 
   113   (*reorientation simprules using ==, for the following simproc*)
   114   val meta_zero_reorient = zero_reorient RS eq_reflection
   115   val meta_one_reorient = one_reorient RS eq_reflection
   116   val meta_number_of_reorient = number_of_reorient RS eq_reflection
   117 
   118   (*reorientation simplification procedure: reorients (polymorphic) 
   119     0 = x, 1 = x, nnn = x provided x isn't 0, 1 or a numeral.*)
   120   fun reorient_proc sg _ (_ $ t $ u) =
   121     case u of
   122 	Const("0", _) => None
   123       | Const("1", _) => None
   124       | Const("Numeral.number_of", _) $ _ => None
   125       | _ => Some (case t of
   126 		  Const("0", _) => meta_zero_reorient
   127 		| Const("1", _) => meta_one_reorient
   128 		| Const("Numeral.number_of", _) $ _ => meta_number_of_reorient)
   129 
   130   val reorient_simproc = 
   131       prep_simproc ("reorient_simproc", ["0=x", "1=x", "number_of w = x"], reorient_proc)
   132 
   133   end;
   134 
   135 
   136 Addsimps arith_special;
   137 Addsimprocs [Bin_Simprocs.reorient_simproc];
   138 
   139 
   140 structure Int_Numeral_Simprocs =
   141 struct
   142 
   143 (*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic in simprocs
   144   isn't complicated by the abstract 0 and 1.*)
   145 val numeral_syms = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym];
   146 
   147 (** New term ordering so that AC-rewriting brings numerals to the front **)
   148 
   149 (*Order integers by absolute value and then by sign. The standard integer
   150   ordering is not well-founded.*)
   151 fun num_ord (i,j) =
   152       (case Int.compare (abs i, abs j) of
   153             EQUAL => Int.compare (Int.sign i, Int.sign j) 
   154           | ord => ord);
   155 
   156 (*This resembles Term.term_ord, but it puts binary numerals before other
   157   non-atomic terms.*)
   158 local open Term 
   159 in 
   160 fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =
   161       (case numterm_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
   162   | numterm_ord
   163      (Const("Numeral.number_of", _) $ v, Const("Numeral.number_of", _) $ w) =
   164      num_ord (HOLogic.dest_binum v, HOLogic.dest_binum w)
   165   | numterm_ord (Const("Numeral.number_of", _) $ _, _) = LESS
   166   | numterm_ord (_, Const("Numeral.number_of", _) $ _) = GREATER
   167   | numterm_ord (t, u) =
   168       (case Int.compare (size_of_term t, size_of_term u) of
   169         EQUAL =>
   170           let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
   171             (case hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)
   172           end
   173       | ord => ord)
   174 and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
   175 end;
   176 
   177 fun numtermless tu = (numterm_ord tu = LESS);
   178 
   179 (*Defined in this file, but perhaps needed only for simprocs of type nat.*)
   180 val num_ss = HOL_ss settermless numtermless;
   181 
   182 
   183 (** Utilities **)
   184 
   185 fun mk_numeral T n = HOLogic.number_of_const T $ HOLogic.mk_bin n;
   186 
   187 (*Decodes a binary INTEGER*)
   188 fun dest_numeral (Const("0", _)) = 0
   189   | dest_numeral (Const("1", _)) = 1
   190   | dest_numeral (Const("Numeral.number_of", _) $ w) =
   191      (HOLogic.dest_binum w
   192       handle TERM _ => raise TERM("Int_Numeral_Simprocs.dest_numeral:1", [w]))
   193   | dest_numeral t = raise TERM("Int_Numeral_Simprocs.dest_numeral:2", [t]);
   194 
   195 fun find_first_numeral past (t::terms) =
   196         ((dest_numeral t, rev past @ terms)
   197          handle TERM _ => find_first_numeral (t::past) terms)
   198   | find_first_numeral past [] = raise TERM("find_first_numeral", []);
   199 
   200 val mk_plus = HOLogic.mk_binop "op +";
   201 
   202 fun mk_minus t = 
   203   let val T = Term.fastype_of t
   204   in Const ("uminus", T --> T) $ t
   205   end;
   206 
   207 (*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
   208 fun mk_sum T []        = mk_numeral T 0
   209   | mk_sum T [t,u]     = mk_plus (t, u)
   210   | mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
   211 
   212 (*this version ALWAYS includes a trailing zero*)
   213 fun long_mk_sum T []        = mk_numeral T 0
   214   | long_mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
   215 
   216 val dest_plus = HOLogic.dest_bin "op +" Term.dummyT;
   217 
   218 (*decompose additions AND subtractions as a sum*)
   219 fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) =
   220         dest_summing (pos, t, dest_summing (pos, u, ts))
   221   | dest_summing (pos, Const ("op -", _) $ t $ u, ts) =
   222         dest_summing (pos, t, dest_summing (not pos, u, ts))
   223   | dest_summing (pos, t, ts) =
   224         if pos then t::ts else mk_minus t :: ts;
   225 
   226 fun dest_sum t = dest_summing (true, t, []);
   227 
   228 val mk_diff = HOLogic.mk_binop "op -";
   229 val dest_diff = HOLogic.dest_bin "op -" Term.dummyT;
   230 
   231 val mk_times = HOLogic.mk_binop "op *";
   232 
   233 fun mk_prod T = 
   234   let val one = mk_numeral T 1
   235   fun mk [] = one
   236     | mk [t] = t
   237     | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)
   238   in mk end;
   239 
   240 (*This version ALWAYS includes a trailing one*)
   241 fun long_mk_prod T []        = mk_numeral T 1
   242   | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);
   243 
   244 val dest_times = HOLogic.dest_bin "op *" Term.dummyT;
   245 
   246 fun dest_prod t =
   247       let val (t,u) = dest_times t
   248       in  dest_prod t @ dest_prod u  end
   249       handle TERM _ => [t];
   250 
   251 (*DON'T do the obvious simplifications; that would create special cases*)
   252 fun mk_coeff (k, t) = mk_times (mk_numeral (Term.fastype_of t) k, t);
   253 
   254 (*Express t as a product of (possibly) a numeral with other sorted terms*)
   255 fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t
   256   | dest_coeff sign t =
   257     let val ts = sort Term.term_ord (dest_prod t)
   258         val (n, ts') = find_first_numeral [] ts
   259                           handle TERM _ => (1, ts)
   260     in (sign*n, mk_prod (Term.fastype_of t) ts') end;
   261 
   262 (*Find first coefficient-term THAT MATCHES u*)
   263 fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
   264   | find_first_coeff past u (t::terms) =
   265         let val (n,u') = dest_coeff 1 t
   266         in  if u aconv u' then (n, rev past @ terms)
   267                           else find_first_coeff (t::past) u terms
   268         end
   269         handle TERM _ => find_first_coeff (t::past) u terms;
   270 
   271 
   272 (*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1*)
   273 val add_0s =  thms "add_0s";
   274 val mult_1s = thms "mult_1s";
   275 
   276 (*To perform binary arithmetic.  The "left" rewriting handles patterns
   277   created by the simprocs, such as 3 * (5 * x). *)
   278 val bin_simps = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym,
   279                  add_number_of_left, mult_number_of_left] @
   280                 bin_arith_simps @ bin_rel_simps;
   281 
   282 (*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
   283   during re-arrangement*)
   284 val non_add_bin_simps = 
   285     bin_simps \\ [add_number_of_left, number_of_add RS sym];
   286 
   287 (*To evaluate binary negations of coefficients*)
   288 val minus_simps = [numeral_m1_eq_minus_1 RS sym, number_of_minus RS sym,
   289                    bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min,
   290                    bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min];
   291 
   292 (*To let us treat subtraction as addition*)
   293 val diff_simps = [diff_minus, minus_add_distrib, minus_minus];
   294 
   295 (*push the unary minus down: - x * y = x * - y *)
   296 val minus_mult_eq_1_to_2 =
   297     [minus_mult_left RS sym, minus_mult_right] MRS trans |> standard;
   298 
   299 (*to extract again any uncancelled minuses*)
   300 val minus_from_mult_simps =
   301     [minus_minus, minus_mult_left RS sym, minus_mult_right RS sym];
   302 
   303 (*combine unary minus with numeric literals, however nested within a product*)
   304 val mult_minus_simps =
   305     [mult_assoc, minus_mult_left, minus_mult_eq_1_to_2];
   306 
   307 (*Apply the given rewrite (if present) just once*)
   308 fun trans_tac None      = all_tac
   309   | trans_tac (Some th) = ALLGOALS (rtac (th RS trans));
   310 
   311 fun simplify_meta_eq rules =
   312     simplify (HOL_basic_ss addeqcongs[eq_cong2] addsimps rules)
   313     o mk_meta_eq;
   314 
   315 structure CancelNumeralsCommon =
   316   struct
   317   val mk_sum            = mk_sum
   318   val dest_sum          = dest_sum
   319   val mk_coeff          = mk_coeff
   320   val dest_coeff        = dest_coeff 1
   321   val find_first_coeff  = find_first_coeff []
   322   val trans_tac         = trans_tac
   323   val norm_tac =
   324      ALLGOALS (simp_tac (num_ss addsimps numeral_syms@add_0s@mult_1s@
   325                                          diff_simps@minus_simps@add_ac))
   326      THEN ALLGOALS (simp_tac (num_ss addsimps non_add_bin_simps@mult_minus_simps))
   327      THEN ALLGOALS (simp_tac (num_ss addsimps minus_from_mult_simps@
   328                                               add_ac@mult_ac))
   329   val numeral_simp_tac  = ALLGOALS (simp_tac (HOL_ss addsimps add_0s@bin_simps))
   330   val simplify_meta_eq  = simplify_meta_eq (add_0s@mult_1s)
   331   end;
   332 
   333 
   334 structure EqCancelNumerals = CancelNumeralsFun
   335  (open CancelNumeralsCommon
   336   val prove_conv = Bin_Simprocs.prove_conv
   337   val mk_bal   = HOLogic.mk_eq
   338   val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
   339   val bal_add1 = eq_add_iff1 RS trans
   340   val bal_add2 = eq_add_iff2 RS trans
   341 );
   342 
   343 structure LessCancelNumerals = CancelNumeralsFun
   344  (open CancelNumeralsCommon
   345   val prove_conv = Bin_Simprocs.prove_conv
   346   val mk_bal   = HOLogic.mk_binrel "op <"
   347   val dest_bal = HOLogic.dest_bin "op <" Term.dummyT
   348   val bal_add1 = less_add_iff1 RS trans
   349   val bal_add2 = less_add_iff2 RS trans
   350 );
   351 
   352 structure LeCancelNumerals = CancelNumeralsFun
   353  (open CancelNumeralsCommon
   354   val prove_conv = Bin_Simprocs.prove_conv
   355   val mk_bal   = HOLogic.mk_binrel "op <="
   356   val dest_bal = HOLogic.dest_bin "op <=" Term.dummyT
   357   val bal_add1 = le_add_iff1 RS trans
   358   val bal_add2 = le_add_iff2 RS trans
   359 );
   360 
   361 val cancel_numerals =
   362   map Bin_Simprocs.prep_simproc
   363    [("inteq_cancel_numerals",
   364      ["(l::'a::number_ring) + m = n",
   365       "(l::'a::number_ring) = m + n",
   366       "(l::'a::number_ring) - m = n",
   367       "(l::'a::number_ring) = m - n",
   368       "(l::'a::number_ring) * m = n",
   369       "(l::'a::number_ring) = m * n"],
   370      EqCancelNumerals.proc),
   371     ("intless_cancel_numerals",
   372      ["(l::'a::{ordered_idom,number_ring}) + m < n",
   373       "(l::'a::{ordered_idom,number_ring}) < m + n",
   374       "(l::'a::{ordered_idom,number_ring}) - m < n",
   375       "(l::'a::{ordered_idom,number_ring}) < m - n",
   376       "(l::'a::{ordered_idom,number_ring}) * m < n",
   377       "(l::'a::{ordered_idom,number_ring}) < m * n"],
   378      LessCancelNumerals.proc),
   379     ("intle_cancel_numerals",
   380      ["(l::'a::{ordered_idom,number_ring}) + m <= n",
   381       "(l::'a::{ordered_idom,number_ring}) <= m + n",
   382       "(l::'a::{ordered_idom,number_ring}) - m <= n",
   383       "(l::'a::{ordered_idom,number_ring}) <= m - n",
   384       "(l::'a::{ordered_idom,number_ring}) * m <= n",
   385       "(l::'a::{ordered_idom,number_ring}) <= m * n"],
   386      LeCancelNumerals.proc)];
   387 
   388 
   389 structure CombineNumeralsData =
   390   struct
   391   val add               = op + : int*int -> int
   392   val mk_sum            = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
   393   val dest_sum          = dest_sum
   394   val mk_coeff          = mk_coeff
   395   val dest_coeff        = dest_coeff 1
   396   val left_distrib      = combine_common_factor RS trans
   397   val prove_conv        = Bin_Simprocs.prove_conv_nohyps
   398   val trans_tac          = trans_tac
   399   val norm_tac =
   400      ALLGOALS (simp_tac (num_ss addsimps numeral_syms@add_0s@mult_1s@
   401                                          diff_simps@minus_simps@add_ac))
   402      THEN ALLGOALS (simp_tac (num_ss addsimps non_add_bin_simps@mult_minus_simps))
   403      THEN ALLGOALS (simp_tac (num_ss addsimps minus_from_mult_simps@
   404                                               add_ac@mult_ac))
   405   val numeral_simp_tac  = ALLGOALS
   406                     (simp_tac (HOL_ss addsimps add_0s@bin_simps))
   407   val simplify_meta_eq  = simplify_meta_eq (add_0s@mult_1s)
   408   end;
   409 
   410 structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
   411 
   412 val combine_numerals =
   413   Bin_Simprocs.prep_simproc
   414     ("int_combine_numerals", 
   415      ["(i::'a::number_ring) + j", "(i::'a::number_ring) - j"], 
   416      CombineNumerals.proc);
   417 
   418 end;
   419 
   420 Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
   421 Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
   422 
   423 (*examples:
   424 print_depth 22;
   425 set timing;
   426 set trace_simp;
   427 fun test s = (Goal s, by (Simp_tac 1));
   428 
   429 test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
   430 
   431 test "2*u = (u::int)";
   432 test "(i + j + 12 + (k::int)) - 15 = y";
   433 test "(i + j + 12 + (k::int)) - 5 = y";
   434 
   435 test "y - b < (b::int)";
   436 test "y - (3*b + c) < (b::int) - 2*c";
   437 
   438 test "(2*x - (u*v) + y) - v*3*u = (w::int)";
   439 test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
   440 test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
   441 test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
   442 
   443 test "(i + j + 12 + (k::int)) = u + 15 + y";
   444 test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
   445 
   446 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)";
   447 
   448 test "a + -(b+c) + b = (d::int)";
   449 test "a + -(b+c) - b = (d::int)";
   450 
   451 (*negative numerals*)
   452 test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
   453 test "(i + j + -3 + (k::int)) < u + 5 + y";
   454 test "(i + j + 3 + (k::int)) < u + -6 + y";
   455 test "(i + j + -12 + (k::int)) - 15 = y";
   456 test "(i + j + 12 + (k::int)) - -15 = y";
   457 test "(i + j + -12 + (k::int)) - -15 = y";
   458 *)
   459 
   460 
   461 (** Constant folding for multiplication in semirings **)
   462 
   463 (*We do not need folding for addition: combine_numerals does the same thing*)
   464 
   465 structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
   466 struct
   467   val ss                = HOL_ss
   468   val eq_reflection     = eq_reflection
   469   val sg_ref = Sign.self_ref (Theory.sign_of (the_context ()))
   470   val add_ac = mult_ac
   471 end;
   472 
   473 structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
   474 
   475 val assoc_fold_simproc =
   476   Bin_Simprocs.prep_simproc
   477    ("semiring_assoc_fold", ["(a::'a::comm_semiring_1_cancel) * b"],
   478     Semiring_Times_Assoc.proc);
   479 
   480 Addsimprocs [assoc_fold_simproc];
   481 
   482 
   483 
   484 
   485 (*** decision procedure for linear arithmetic ***)
   486 
   487 (*---------------------------------------------------------------------------*)
   488 (* Linear arithmetic                                                         *)
   489 (*---------------------------------------------------------------------------*)
   490 
   491 (*
   492 Instantiation of the generic linear arithmetic package for int.
   493 *)
   494 
   495 (* Update parameters of arithmetic prover *)
   496 local
   497 
   498 (* reduce contradictory <= to False *)
   499 val add_rules =
   500     simp_thms @ bin_arith_simps @ bin_rel_simps @ arith_special @
   501     [neg_le_iff_le, numeral_0_eq_0, numeral_1_eq_1,
   502      minus_zero, diff_minus, left_minus, right_minus,
   503      mult_zero_left, mult_zero_right, mult_1, mult_1_right,
   504      minus_mult_left RS sym, minus_mult_right RS sym,
   505      minus_add_distrib, minus_minus, mult_assoc,
   506      of_nat_0, of_nat_1, of_nat_Suc, of_nat_add, of_nat_mult,
   507      of_int_0, of_int_1, of_int_add, of_int_mult, int_eq_of_nat,
   508      zero_neq_one, zero_less_one, zero_le_one, 
   509      zero_neq_one RS not_sym, not_one_le_zero, not_one_less_zero];
   510 
   511 val simprocs = [assoc_fold_simproc, Int_Numeral_Simprocs.combine_numerals]@
   512                Int_Numeral_Simprocs.cancel_numerals;
   513 
   514 in
   515 
   516 val int_arith_setup =
   517  [Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset} =>
   518    {add_mono_thms = add_mono_thms,
   519     mult_mono_thms = [mult_strict_left_mono,mult_left_mono] @ mult_mono_thms,
   520     inj_thms = [zle_int RS iffD2,int_int_eq RS iffD2] @ inj_thms,
   521     lessD = lessD @ [zless_imp_add1_zle],
   522     simpset = simpset addsimps add_rules
   523                       addsimprocs simprocs
   524                       addcongs [if_weak_cong]}),
   525   arith_inj_const ("IntDef.of_nat", HOLogic.natT --> HOLogic.intT),
   526   arith_inj_const ("IntDef.int", HOLogic.natT --> HOLogic.intT),
   527   arith_discrete "IntDef.int"];
   528 
   529 end;
   530 
   531 val fast_int_arith_simproc =
   532   Simplifier.simproc (Theory.sign_of (the_context()))
   533   "fast_int_arith" 
   534      ["(m::'a::{ordered_idom,number_ring}) < n",
   535       "(m::'a::{ordered_idom,number_ring}) <= n",
   536       "(m::'a::{ordered_idom,number_ring}) = n"] Fast_Arith.lin_arith_prover;
   537 
   538 Addsimprocs [fast_int_arith_simproc]
   539 
   540 
   541 (* Some test data
   542 Goal "!!a::int. [| a <= b; c <= d; x+y<z |] ==> a+c <= b+d";
   543 by (fast_arith_tac 1);
   544 Goal "!!a::int. [| a < b; c < d |] ==> a-d+ 2 <= b+(-c)";
   545 by (fast_arith_tac 1);
   546 Goal "!!a::int. [| a < b; c < d |] ==> a+c+ 1 < b+d";
   547 by (fast_arith_tac 1);
   548 Goal "!!a::int. [| a <= b; b+b <= c |] ==> a+a <= c";
   549 by (fast_arith_tac 1);
   550 Goal "!!a::int. [| a+b <= i+j; a<=b; i<=j |] \
   551 \     ==> a+a <= j+j";
   552 by (fast_arith_tac 1);
   553 Goal "!!a::int. [| a+b < i+j; a<b; i<j |] \
   554 \     ==> a+a - - -1 < j+j - 3";
   555 by (fast_arith_tac 1);
   556 Goal "!!a::int. a+b+c <= i+j+k & a<=b & b<=c & i<=j & j<=k --> a+a+a <= k+k+k";
   557 by (arith_tac 1);
   558 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   559 \     ==> a <= l";
   560 by (fast_arith_tac 1);
   561 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   562 \     ==> a+a+a+a <= l+l+l+l";
   563 by (fast_arith_tac 1);
   564 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   565 \     ==> a+a+a+a+a <= l+l+l+l+i";
   566 by (fast_arith_tac 1);
   567 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   568 \     ==> a+a+a+a+a+a <= l+l+l+l+i+l";
   569 by (fast_arith_tac 1);
   570 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   571 \     ==> 6*a <= 5*l+i";
   572 by (fast_arith_tac 1);
   573 *)