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