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