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