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