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