src/HOL/Integ/int_arith1.ML
author paulson
Wed Dec 03 10:49:34 2003 +0100 (2003-12-03)
changeset 14271 8ed6989228bb
parent 14113 7b3513ba0f86
child 14272 5efbb548107d
permissions -rw-r--r--
Simplification of the development of Integers
wenzelm@9436
     1
(*  Title:      HOL/Integ/int_arith1.ML
wenzelm@9436
     2
    ID:         $Id$
wenzelm@9436
     3
    Authors:    Larry Paulson and Tobias Nipkow
wenzelm@9436
     4
wenzelm@9436
     5
Simprocs and decision procedure for linear arithmetic.
wenzelm@9436
     6
*)
wenzelm@9436
     7
paulson@14271
     8
val zadd_ac = thms "Ring_and_Field.add_ac";
paulson@14271
     9
val zmult_ac = thms "Ring_and_Field.mult_ac";
paulson@14271
    10
paulson@14271
    11
paulson@11868
    12
Addsimps [int_numeral_0_eq_0, int_numeral_1_eq_1];
paulson@11868
    13
wenzelm@9436
    14
(*** Simprocs for numeric literals ***)
wenzelm@9436
    15
wenzelm@9436
    16
(** Combining of literal coefficients in sums of products **)
wenzelm@9436
    17
paulson@11868
    18
Goal "(x < y) = (x-y < (0::int))";
paulson@14271
    19
by (simp_tac (simpset() addsimps compare_rls) 1);
wenzelm@9436
    20
qed "zless_iff_zdiff_zless_0";
wenzelm@9436
    21
paulson@11868
    22
Goal "(x = y) = (x-y = (0::int))";
paulson@14271
    23
by (simp_tac (simpset() addsimps compare_rls) 1);
wenzelm@9436
    24
qed "eq_iff_zdiff_eq_0";
wenzelm@9436
    25
paulson@11868
    26
Goal "(x <= y) = (x-y <= (0::int))";
paulson@14271
    27
by (simp_tac (simpset() addsimps compare_rls) 1);
wenzelm@9436
    28
qed "zle_iff_zdiff_zle_0";
wenzelm@9436
    29
wenzelm@9436
    30
wenzelm@9436
    31
(** For combine_numerals **)
wenzelm@9436
    32
wenzelm@9436
    33
Goal "i*u + (j*u + k) = (i+j)*u + (k::int)";
wenzelm@9436
    34
by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib]) 1);
wenzelm@9436
    35
qed "left_zadd_zmult_distrib";
wenzelm@9436
    36
wenzelm@9436
    37
wenzelm@9436
    38
(** For cancel_numerals **)
wenzelm@9436
    39
wenzelm@9436
    40
val rel_iff_rel_0_rls = map (inst "y" "?u+?v")
wenzelm@13462
    41
                          [zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0,
wenzelm@13462
    42
                           zle_iff_zdiff_zle_0] @
wenzelm@13462
    43
                        map (inst "y" "n")
wenzelm@13462
    44
                          [zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0,
wenzelm@13462
    45
                           zle_iff_zdiff_zle_0];
wenzelm@9436
    46
wenzelm@9436
    47
Goal "!!i::int. (i*u + m = j*u + n) = ((i-j)*u + m = n)";
wenzelm@9436
    48
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
wenzelm@13462
    49
                                     zadd_ac@rel_iff_rel_0_rls) 1);
wenzelm@9436
    50
qed "eq_add_iff1";
wenzelm@9436
    51
wenzelm@9436
    52
Goal "!!i::int. (i*u + m = j*u + n) = (m = (j-i)*u + n)";
wenzelm@9436
    53
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
wenzelm@9436
    54
                                     zadd_ac@rel_iff_rel_0_rls) 1);
wenzelm@9436
    55
qed "eq_add_iff2";
wenzelm@9436
    56
wenzelm@9436
    57
Goal "!!i::int. (i*u + m < j*u + n) = ((i-j)*u + m < n)";
wenzelm@9436
    58
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
wenzelm@9436
    59
                                     zadd_ac@rel_iff_rel_0_rls) 1);
wenzelm@9436
    60
qed "less_add_iff1";
wenzelm@9436
    61
wenzelm@9436
    62
Goal "!!i::int. (i*u + m < j*u + n) = (m < (j-i)*u + n)";
wenzelm@9436
    63
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
wenzelm@9436
    64
                                     zadd_ac@rel_iff_rel_0_rls) 1);
wenzelm@9436
    65
qed "less_add_iff2";
wenzelm@9436
    66
wenzelm@9436
    67
Goal "!!i::int. (i*u + m <= j*u + n) = ((i-j)*u + m <= n)";
wenzelm@9436
    68
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
wenzelm@9436
    69
                                     zadd_ac@rel_iff_rel_0_rls) 1);
wenzelm@9436
    70
qed "le_add_iff1";
wenzelm@9436
    71
wenzelm@9436
    72
Goal "!!i::int. (i*u + m <= j*u + n) = (m <= (j-i)*u + n)";
wenzelm@9436
    73
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]
wenzelm@9436
    74
                                     @zadd_ac@rel_iff_rel_0_rls) 1);
wenzelm@9436
    75
qed "le_add_iff2";
wenzelm@9436
    76
wenzelm@9436
    77
wenzelm@9436
    78
structure Int_Numeral_Simprocs =
wenzelm@9436
    79
struct
wenzelm@9436
    80
paulson@11868
    81
(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic in simprocs
paulson@11868
    82
  isn't complicated by the abstract 0 and 1.*)
paulson@11868
    83
val numeral_syms = [int_numeral_0_eq_0 RS sym, int_numeral_1_eq_1 RS sym];
paulson@11868
    84
val numeral_sym_ss = HOL_ss addsimps numeral_syms;
paulson@11868
    85
wenzelm@13462
    86
fun rename_numerals th =
paulson@11868
    87
    simplify numeral_sym_ss (Thm.transfer (the_context ()) th);
paulson@11868
    88
wenzelm@9436
    89
(*Utilities*)
wenzelm@9436
    90
nipkow@10693
    91
fun mk_numeral n = HOLogic.number_of_const HOLogic.intT $ HOLogic.mk_bin n;
wenzelm@9436
    92
wenzelm@9436
    93
(*Decodes a binary INTEGER*)
paulson@11868
    94
fun dest_numeral (Const("0", _)) = 0
paulson@11868
    95
  | dest_numeral (Const("1", _)) = 1
wenzelm@13462
    96
  | dest_numeral (Const("Numeral.number_of", _) $ w) =
wenzelm@10890
    97
     (HOLogic.dest_binum w
wenzelm@10890
    98
      handle TERM _ => raise TERM("Int_Numeral_Simprocs.dest_numeral:1", [w]))
wenzelm@9436
    99
  | dest_numeral t = raise TERM("Int_Numeral_Simprocs.dest_numeral:2", [t]);
wenzelm@9436
   100
wenzelm@9436
   101
fun find_first_numeral past (t::terms) =
wenzelm@13462
   102
        ((dest_numeral t, rev past @ terms)
wenzelm@13462
   103
         handle TERM _ => find_first_numeral (t::past) terms)
wenzelm@9436
   104
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
wenzelm@9436
   105
wenzelm@9436
   106
val zero = mk_numeral 0;
wenzelm@9436
   107
val mk_plus = HOLogic.mk_binop "op +";
wenzelm@9436
   108
wenzelm@9436
   109
val uminus_const = Const ("uminus", HOLogic.intT --> HOLogic.intT);
wenzelm@9436
   110
paulson@11868
   111
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
wenzelm@9436
   112
fun mk_sum []        = zero
wenzelm@9436
   113
  | mk_sum [t,u]     = mk_plus (t, u)
wenzelm@9436
   114
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@9436
   115
wenzelm@9436
   116
(*this version ALWAYS includes a trailing zero*)
wenzelm@9436
   117
fun long_mk_sum []        = zero
wenzelm@9436
   118
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@9436
   119
wenzelm@9436
   120
val dest_plus = HOLogic.dest_bin "op +" HOLogic.intT;
wenzelm@9436
   121
wenzelm@9436
   122
(*decompose additions AND subtractions as a sum*)
wenzelm@9436
   123
fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) =
wenzelm@9436
   124
        dest_summing (pos, t, dest_summing (pos, u, ts))
wenzelm@9436
   125
  | dest_summing (pos, Const ("op -", _) $ t $ u, ts) =
wenzelm@9436
   126
        dest_summing (pos, t, dest_summing (not pos, u, ts))
wenzelm@9436
   127
  | dest_summing (pos, t, ts) =
wenzelm@13462
   128
        if pos then t::ts else uminus_const$t :: ts;
wenzelm@9436
   129
wenzelm@9436
   130
fun dest_sum t = dest_summing (true, t, []);
wenzelm@9436
   131
wenzelm@9436
   132
val mk_diff = HOLogic.mk_binop "op -";
wenzelm@9436
   133
val dest_diff = HOLogic.dest_bin "op -" HOLogic.intT;
wenzelm@9436
   134
wenzelm@9436
   135
val one = mk_numeral 1;
wenzelm@9436
   136
val mk_times = HOLogic.mk_binop "op *";
wenzelm@9436
   137
wenzelm@9436
   138
fun mk_prod [] = one
wenzelm@9436
   139
  | mk_prod [t] = t
wenzelm@9436
   140
  | mk_prod (t :: ts) = if t = one then mk_prod ts
wenzelm@9436
   141
                        else mk_times (t, mk_prod ts);
wenzelm@9436
   142
wenzelm@9436
   143
val dest_times = HOLogic.dest_bin "op *" HOLogic.intT;
wenzelm@9436
   144
wenzelm@9436
   145
fun dest_prod t =
wenzelm@13462
   146
      let val (t,u) = dest_times t
wenzelm@9436
   147
      in  dest_prod t @ dest_prod u  end
wenzelm@9436
   148
      handle TERM _ => [t];
wenzelm@9436
   149
wenzelm@13462
   150
(*DON'T do the obvious simplifications; that would create special cases*)
wenzelm@9436
   151
fun mk_coeff (k, ts) = mk_times (mk_numeral k, ts);
wenzelm@9436
   152
wenzelm@9436
   153
(*Express t as a product of (possibly) a numeral with other sorted terms*)
wenzelm@9436
   154
fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t
wenzelm@9436
   155
  | dest_coeff sign t =
wenzelm@9436
   156
    let val ts = sort Term.term_ord (dest_prod t)
wenzelm@13462
   157
        val (n, ts') = find_first_numeral [] ts
wenzelm@9436
   158
                          handle TERM _ => (1, ts)
wenzelm@9436
   159
    in (sign*n, mk_prod ts') end;
wenzelm@9436
   160
wenzelm@9436
   161
(*Find first coefficient-term THAT MATCHES u*)
wenzelm@13462
   162
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
wenzelm@9436
   163
  | find_first_coeff past u (t::terms) =
wenzelm@13462
   164
        let val (n,u') = dest_coeff 1 t
wenzelm@13462
   165
        in  if u aconv u' then (n, rev past @ terms)
wenzelm@13462
   166
                          else find_first_coeff (t::past) u terms
wenzelm@13462
   167
        end
wenzelm@13462
   168
        handle TERM _ => find_first_coeff (t::past) u terms;
wenzelm@9436
   169
wenzelm@9436
   170
paulson@11868
   171
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1*)
paulson@11868
   172
val add_0s =  map rename_numerals [zadd_0, zadd_0_right];
paulson@11868
   173
val mult_1s = map rename_numerals [zmult_1, zmult_1_right] @
paulson@11868
   174
              [zmult_minus1, zmult_minus1_right];
wenzelm@9436
   175
paulson@11868
   176
(*To perform binary arithmetic.  The "left" rewriting handles patterns
paulson@11868
   177
  created by the simprocs, such as 3 * (5 * x). *)
paulson@11868
   178
val bin_simps = [int_numeral_0_eq_0 RS sym, int_numeral_1_eq_1 RS sym,
wenzelm@13462
   179
                 add_number_of_left, mult_number_of_left] @
paulson@11868
   180
                bin_arith_simps @ bin_rel_simps;
wenzelm@9436
   181
paulson@14113
   182
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
paulson@14113
   183
  during re-arrangement*)
paulson@14113
   184
val non_add_bin_simps = 
paulson@14113
   185
    bin_simps \\ [add_number_of_left, number_of_add RS sym];
paulson@14113
   186
wenzelm@9436
   187
(*To evaluate binary negations of coefficients*)
wenzelm@9436
   188
val zminus_simps = NCons_simps @
wenzelm@13462
   189
                   [zminus_1_eq_m1, number_of_minus RS sym,
wenzelm@13462
   190
                    bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min,
wenzelm@13462
   191
                    bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min];
wenzelm@9436
   192
wenzelm@9436
   193
(*To let us treat subtraction as addition*)
wenzelm@9436
   194
val diff_simps = [zdiff_def, zminus_zadd_distrib, zminus_zminus];
wenzelm@9436
   195
paulson@10713
   196
(*push the unary minus down: - x * y = x * - y *)
wenzelm@13462
   197
val int_minus_mult_eq_1_to_2 =
paulson@10713
   198
    [zmult_zminus, zmult_zminus_right RS sym] MRS trans |> standard;
paulson@10713
   199
paulson@10713
   200
(*to extract again any uncancelled minuses*)
wenzelm@13462
   201
val int_minus_from_mult_simps =
paulson@10713
   202
    [zminus_zminus, zmult_zminus, zmult_zminus_right];
paulson@10713
   203
paulson@10713
   204
(*combine unary minus with numeric literals, however nested within a product*)
paulson@10713
   205
val int_mult_minus_simps =
paulson@10713
   206
    [zmult_assoc, zmult_zminus RS sym, int_minus_mult_eq_1_to_2];
paulson@10713
   207
wenzelm@9436
   208
(*Apply the given rewrite (if present) just once*)
wenzelm@9436
   209
fun trans_tac None      = all_tac
wenzelm@9436
   210
  | trans_tac (Some th) = ALLGOALS (rtac (th RS trans));
wenzelm@9436
   211
wenzelm@9436
   212
fun simplify_meta_eq rules =
wenzelm@9436
   213
    simplify (HOL_basic_ss addeqcongs[eq_cong2] addsimps rules)
paulson@12975
   214
    o mk_meta_eq;
wenzelm@9436
   215
wenzelm@9436
   216
structure CancelNumeralsCommon =
wenzelm@9436
   217
  struct
wenzelm@13462
   218
  val mk_sum            = mk_sum
wenzelm@13462
   219
  val dest_sum          = dest_sum
wenzelm@13462
   220
  val mk_coeff          = mk_coeff
wenzelm@13462
   221
  val dest_coeff        = dest_coeff 1
wenzelm@13462
   222
  val find_first_coeff  = find_first_coeff []
wenzelm@9436
   223
  val trans_tac         = trans_tac
wenzelm@13462
   224
  val norm_tac =
paulson@11868
   225
     ALLGOALS (simp_tac (HOL_ss addsimps numeral_syms@add_0s@mult_1s@
paulson@11868
   226
                                         diff_simps@zminus_simps@zadd_ac))
paulson@14113
   227
     THEN ALLGOALS (simp_tac (HOL_ss addsimps non_add_bin_simps@int_mult_minus_simps))
paulson@10713
   228
     THEN ALLGOALS (simp_tac (HOL_ss addsimps int_minus_from_mult_simps@
paulson@10713
   229
                                              zadd_ac@zmult_ac))
wenzelm@13462
   230
  val numeral_simp_tac  = ALLGOALS (simp_tac (HOL_ss addsimps add_0s@bin_simps))
wenzelm@9436
   231
  val simplify_meta_eq  = simplify_meta_eq (add_0s@mult_1s)
wenzelm@9436
   232
  end;
wenzelm@9436
   233
wenzelm@9436
   234
wenzelm@9436
   235
structure EqCancelNumerals = CancelNumeralsFun
wenzelm@9436
   236
 (open CancelNumeralsCommon
wenzelm@13485
   237
  val prove_conv = Bin_Simprocs.prove_conv
wenzelm@9436
   238
  val mk_bal   = HOLogic.mk_eq
wenzelm@9436
   239
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.intT
wenzelm@9436
   240
  val bal_add1 = eq_add_iff1 RS trans
wenzelm@9436
   241
  val bal_add2 = eq_add_iff2 RS trans
wenzelm@9436
   242
);
wenzelm@9436
   243
wenzelm@9436
   244
structure LessCancelNumerals = CancelNumeralsFun
wenzelm@9436
   245
 (open CancelNumeralsCommon
wenzelm@13485
   246
  val prove_conv = Bin_Simprocs.prove_conv
wenzelm@9436
   247
  val mk_bal   = HOLogic.mk_binrel "op <"
wenzelm@9436
   248
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.intT
wenzelm@9436
   249
  val bal_add1 = less_add_iff1 RS trans
wenzelm@9436
   250
  val bal_add2 = less_add_iff2 RS trans
wenzelm@9436
   251
);
wenzelm@9436
   252
wenzelm@9436
   253
structure LeCancelNumerals = CancelNumeralsFun
wenzelm@9436
   254
 (open CancelNumeralsCommon
wenzelm@13485
   255
  val prove_conv = Bin_Simprocs.prove_conv
wenzelm@9436
   256
  val mk_bal   = HOLogic.mk_binrel "op <="
wenzelm@9436
   257
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.intT
wenzelm@9436
   258
  val bal_add1 = le_add_iff1 RS trans
wenzelm@9436
   259
  val bal_add2 = le_add_iff2 RS trans
wenzelm@9436
   260
);
wenzelm@9436
   261
wenzelm@13462
   262
val cancel_numerals =
paulson@11868
   263
  map Bin_Simprocs.prep_simproc
wenzelm@9436
   264
   [("inteq_cancel_numerals",
wenzelm@13462
   265
     ["(l::int) + m = n", "(l::int) = m + n",
wenzelm@13462
   266
      "(l::int) - m = n", "(l::int) = m - n",
wenzelm@13462
   267
      "(l::int) * m = n", "(l::int) = m * n"],
wenzelm@9436
   268
     EqCancelNumerals.proc),
wenzelm@13462
   269
    ("intless_cancel_numerals",
wenzelm@13462
   270
     ["(l::int) + m < n", "(l::int) < m + n",
wenzelm@13462
   271
      "(l::int) - m < n", "(l::int) < m - n",
wenzelm@13462
   272
      "(l::int) * m < n", "(l::int) < m * n"],
wenzelm@9436
   273
     LessCancelNumerals.proc),
wenzelm@13462
   274
    ("intle_cancel_numerals",
wenzelm@13462
   275
     ["(l::int) + m <= n", "(l::int) <= m + n",
wenzelm@13462
   276
      "(l::int) - m <= n", "(l::int) <= m - n",
wenzelm@13462
   277
      "(l::int) * m <= n", "(l::int) <= m * n"],
wenzelm@9436
   278
     LeCancelNumerals.proc)];
wenzelm@9436
   279
wenzelm@9436
   280
wenzelm@9436
   281
structure CombineNumeralsData =
wenzelm@9436
   282
  struct
wenzelm@13462
   283
  val add               = op + : int*int -> int
wenzelm@13462
   284
  val mk_sum            = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
wenzelm@13462
   285
  val dest_sum          = dest_sum
wenzelm@13462
   286
  val mk_coeff          = mk_coeff
wenzelm@13462
   287
  val dest_coeff        = dest_coeff 1
wenzelm@13462
   288
  val left_distrib      = left_zadd_zmult_distrib RS trans
wenzelm@13485
   289
  val prove_conv        = Bin_Simprocs.prove_conv_nohyps
wenzelm@9436
   290
  val trans_tac          = trans_tac
wenzelm@13462
   291
  val norm_tac =
paulson@11868
   292
     ALLGOALS (simp_tac (HOL_ss addsimps numeral_syms@add_0s@mult_1s@
paulson@11868
   293
                                         diff_simps@zminus_simps@zadd_ac))
paulson@14113
   294
     THEN ALLGOALS (simp_tac (HOL_ss addsimps non_add_bin_simps@int_mult_minus_simps))
paulson@10713
   295
     THEN ALLGOALS (simp_tac (HOL_ss addsimps int_minus_from_mult_simps@
paulson@10713
   296
                                              zadd_ac@zmult_ac))
wenzelm@13462
   297
  val numeral_simp_tac  = ALLGOALS
wenzelm@9436
   298
                    (simp_tac (HOL_ss addsimps add_0s@bin_simps))
wenzelm@9436
   299
  val simplify_meta_eq  = simplify_meta_eq (add_0s@mult_1s)
wenzelm@9436
   300
  end;
wenzelm@9436
   301
wenzelm@9436
   302
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
wenzelm@13462
   303
wenzelm@13462
   304
val combine_numerals =
wenzelm@13462
   305
  Bin_Simprocs.prep_simproc
wenzelm@13462
   306
    ("int_combine_numerals", ["(i::int) + j", "(i::int) - j"], CombineNumerals.proc);
wenzelm@9436
   307
wenzelm@9436
   308
end;
wenzelm@9436
   309
wenzelm@9436
   310
Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
wenzelm@9436
   311
Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
wenzelm@9436
   312
wenzelm@9436
   313
(*The Abel_Cancel simprocs are now obsolete*)
wenzelm@9436
   314
Delsimprocs [Int_Cancel.sum_conv, Int_Cancel.rel_conv];
wenzelm@9436
   315
wenzelm@9436
   316
(*examples:
wenzelm@9436
   317
print_depth 22;
wenzelm@9436
   318
set timing;
wenzelm@9436
   319
set trace_simp;
wenzelm@13462
   320
fun test s = (Goal s, by (Simp_tac 1));
wenzelm@9436
   321
wenzelm@11704
   322
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
wenzelm@9436
   323
wenzelm@11704
   324
test "2*u = (u::int)";
wenzelm@11704
   325
test "(i + j + 12 + (k::int)) - 15 = y";
wenzelm@11704
   326
test "(i + j + 12 + (k::int)) - 5 = y";
wenzelm@9436
   327
wenzelm@9436
   328
test "y - b < (b::int)";
wenzelm@11704
   329
test "y - (3*b + c) < (b::int) - 2*c";
wenzelm@9436
   330
wenzelm@11704
   331
test "(2*x - (u*v) + y) - v*3*u = (w::int)";
wenzelm@11704
   332
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
wenzelm@11704
   333
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
wenzelm@11704
   334
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
wenzelm@9436
   335
wenzelm@11704
   336
test "(i + j + 12 + (k::int)) = u + 15 + y";
wenzelm@11704
   337
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
wenzelm@9436
   338
wenzelm@11704
   339
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@9436
   340
wenzelm@9436
   341
test "a + -(b+c) + b = (d::int)";
wenzelm@9436
   342
test "a + -(b+c) - b = (d::int)";
wenzelm@9436
   343
wenzelm@9436
   344
(*negative numerals*)
wenzelm@11704
   345
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
wenzelm@11704
   346
test "(i + j + -3 + (k::int)) < u + 5 + y";
wenzelm@11704
   347
test "(i + j + 3 + (k::int)) < u + -6 + y";
wenzelm@11704
   348
test "(i + j + -12 + (k::int)) - 15 = y";
wenzelm@11704
   349
test "(i + j + 12 + (k::int)) - -15 = y";
wenzelm@11704
   350
test "(i + j + -12 + (k::int)) - -15 = y";
wenzelm@9436
   351
*)
wenzelm@9436
   352
wenzelm@9436
   353
wenzelm@9436
   354
(** Constant folding for integer plus and times **)
wenzelm@9436
   355
wenzelm@9436
   356
(*We do not need
wenzelm@9436
   357
    structure Nat_Plus_Assoc = Assoc_Fold (Nat_Plus_Assoc_Data);
wenzelm@9436
   358
    structure Int_Plus_Assoc = Assoc_Fold (Int_Plus_Assoc_Data);
wenzelm@9436
   359
  because combine_numerals does the same thing*)
wenzelm@9436
   360
wenzelm@9436
   361
structure Int_Times_Assoc_Data : ASSOC_FOLD_DATA =
wenzelm@9436
   362
struct
wenzelm@13462
   363
  val ss                = HOL_ss
wenzelm@13462
   364
  val eq_reflection     = eq_reflection
wenzelm@9436
   365
  val sg_ref = Sign.self_ref (Theory.sign_of (the_context ()))
wenzelm@13462
   366
  val T      = HOLogic.intT
wenzelm@9436
   367
  val plus   = Const ("op *", [HOLogic.intT,HOLogic.intT] ---> HOLogic.intT);
wenzelm@9436
   368
  val add_ac = zmult_ac
wenzelm@9436
   369
end;
wenzelm@9436
   370
wenzelm@9436
   371
structure Int_Times_Assoc = Assoc_Fold (Int_Times_Assoc_Data);
wenzelm@9436
   372
wenzelm@9436
   373
Addsimprocs [Int_Times_Assoc.conv];
wenzelm@9436
   374
wenzelm@9436
   375
wenzelm@9436
   376
(** The same for the naturals **)
wenzelm@9436
   377
wenzelm@9436
   378
structure Nat_Times_Assoc_Data : ASSOC_FOLD_DATA =
wenzelm@9436
   379
struct
wenzelm@13462
   380
  val ss                = HOL_ss
wenzelm@13462
   381
  val eq_reflection     = eq_reflection
wenzelm@9436
   382
  val sg_ref = Sign.self_ref (Theory.sign_of (the_context ()))
wenzelm@13462
   383
  val T      = HOLogic.natT
wenzelm@9436
   384
  val plus   = Const ("op *", [HOLogic.natT,HOLogic.natT] ---> HOLogic.natT);
wenzelm@9436
   385
  val add_ac = mult_ac
wenzelm@9436
   386
end;
wenzelm@9436
   387
wenzelm@9436
   388
structure Nat_Times_Assoc = Assoc_Fold (Nat_Times_Assoc_Data);
wenzelm@9436
   389
wenzelm@9436
   390
Addsimprocs [Nat_Times_Assoc.conv];
wenzelm@9436
   391
wenzelm@9436
   392
wenzelm@9436
   393
(*** decision procedure for linear arithmetic ***)
wenzelm@9436
   394
wenzelm@9436
   395
(*---------------------------------------------------------------------------*)
wenzelm@9436
   396
(* Linear arithmetic                                                         *)
wenzelm@9436
   397
(*---------------------------------------------------------------------------*)
wenzelm@9436
   398
wenzelm@9436
   399
(*
wenzelm@9436
   400
Instantiation of the generic linear arithmetic package for int.
wenzelm@9436
   401
*)
wenzelm@9436
   402
wenzelm@9436
   403
(* Update parameters of arithmetic prover *)
wenzelm@9436
   404
local
wenzelm@9436
   405
wenzelm@9436
   406
(* reduce contradictory <= to False *)
wenzelm@13462
   407
val add_rules =
wenzelm@13462
   408
    simp_thms @ bin_arith_simps @ bin_rel_simps @
paulson@11868
   409
    [int_numeral_0_eq_0, int_numeral_1_eq_1,
paulson@12018
   410
     zminus_0, zadd_0, zadd_0_right, zdiff_def,
wenzelm@13462
   411
     zadd_zminus_inverse, zadd_zminus_inverse2,
wenzelm@13462
   412
     zmult_0, zmult_0_right,
nipkow@12482
   413
     zmult_1, zmult_1_right,
nipkow@12482
   414
     zmult_zminus, zmult_zminus_right,
paulson@11868
   415
     zminus_zadd_distrib, zminus_zminus, zmult_assoc,
nipkow@13499
   416
     int_0, int_1, int_Suc, zadd_int RS sym, zmult_int RS sym];
wenzelm@9436
   417
wenzelm@9436
   418
val simprocs = [Int_Times_Assoc.conv, Int_Numeral_Simprocs.combine_numerals]@
wenzelm@13462
   419
               Int_Numeral_Simprocs.cancel_numerals @
paulson@11868
   420
               Bin_Simprocs.eval_numerals;
wenzelm@9436
   421
wenzelm@9436
   422
val add_mono_thms_int =
wenzelm@9436
   423
  map (fn s => prove_goal (the_context ()) s
wenzelm@9436
   424
                 (fn prems => [cut_facts_tac prems 1,
wenzelm@9436
   425
                      asm_simp_tac (simpset() addsimps [zadd_zle_mono]) 1]))
wenzelm@9436
   426
    ["(i <= j) & (k <= l) ==> i + k <= j + (l::int)",
wenzelm@9436
   427
     "(i  = j) & (k <= l) ==> i + k <= j + (l::int)",
wenzelm@9436
   428
     "(i <= j) & (k  = l) ==> i + k <= j + (l::int)",
wenzelm@9436
   429
     "(i  = j) & (k  = l) ==> i + k  = j + (l::int)"
wenzelm@9436
   430
    ];
wenzelm@9436
   431
wenzelm@9436
   432
in
wenzelm@9436
   433
wenzelm@9436
   434
val int_arith_setup =
nipkow@10693
   435
 [Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, simpset} =>
wenzelm@9436
   436
   {add_mono_thms = add_mono_thms @ add_mono_thms_int,
nipkow@10693
   437
    mult_mono_thms = mult_mono_thms,
nipkow@10574
   438
    inj_thms = [zle_int RS iffD2,int_int_eq RS iffD2] @ inj_thms,
wenzelm@9436
   439
    lessD = lessD @ [add1_zle_eq RS iffD2],
wenzelm@9436
   440
    simpset = simpset addsimps add_rules
wenzelm@9436
   441
                      addsimprocs simprocs
wenzelm@9436
   442
                      addcongs [if_weak_cong]}),
nipkow@10834
   443
  arith_inj_const ("IntDef.int", HOLogic.natT --> HOLogic.intT),
wenzelm@9436
   444
  arith_discrete ("IntDef.int", true)];
wenzelm@9436
   445
wenzelm@9436
   446
end;
wenzelm@9436
   447
wenzelm@13462
   448
val fast_int_arith_simproc =
wenzelm@13462
   449
  Simplifier.simproc (Theory.sign_of (the_context()))
wenzelm@13462
   450
  "fast_int_arith" ["(m::int) < n","(m::int) <= n", "(m::int) = n"] Fast_Arith.lin_arith_prover;
wenzelm@9436
   451
wenzelm@9436
   452
Addsimprocs [fast_int_arith_simproc]
wenzelm@13462
   453
wenzelm@9436
   454
wenzelm@9436
   455
(* Some test data
wenzelm@9436
   456
Goal "!!a::int. [| a <= b; c <= d; x+y<z |] ==> a+c <= b+d";
wenzelm@9436
   457
by (fast_arith_tac 1);
wenzelm@11704
   458
Goal "!!a::int. [| a < b; c < d |] ==> a-d+ 2 <= b+(-c)";
wenzelm@9436
   459
by (fast_arith_tac 1);
paulson@11868
   460
Goal "!!a::int. [| a < b; c < d |] ==> a+c+ 1 < b+d";
wenzelm@9436
   461
by (fast_arith_tac 1);
wenzelm@9436
   462
Goal "!!a::int. [| a <= b; b+b <= c |] ==> a+a <= c";
wenzelm@9436
   463
by (fast_arith_tac 1);
wenzelm@9436
   464
Goal "!!a::int. [| a+b <= i+j; a<=b; i<=j |] \
wenzelm@9436
   465
\     ==> a+a <= j+j";
wenzelm@9436
   466
by (fast_arith_tac 1);
wenzelm@9436
   467
Goal "!!a::int. [| a+b < i+j; a<b; i<j |] \
wenzelm@11704
   468
\     ==> a+a - - -1 < j+j - 3";
wenzelm@9436
   469
by (fast_arith_tac 1);
wenzelm@9436
   470
Goal "!!a::int. a+b+c <= i+j+k & a<=b & b<=c & i<=j & j<=k --> a+a+a <= k+k+k";
wenzelm@9436
   471
by (arith_tac 1);
wenzelm@9436
   472
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
wenzelm@9436
   473
\     ==> a <= l";
wenzelm@9436
   474
by (fast_arith_tac 1);
wenzelm@9436
   475
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
wenzelm@9436
   476
\     ==> a+a+a+a <= l+l+l+l";
wenzelm@9436
   477
by (fast_arith_tac 1);
wenzelm@9436
   478
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
wenzelm@9436
   479
\     ==> a+a+a+a+a <= l+l+l+l+i";
wenzelm@9436
   480
by (fast_arith_tac 1);
wenzelm@9436
   481
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
wenzelm@9436
   482
\     ==> a+a+a+a+a+a <= l+l+l+l+i+l";
wenzelm@9436
   483
by (fast_arith_tac 1);
wenzelm@9436
   484
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
wenzelm@11704
   485
\     ==> 6*a <= 5*l+i";
wenzelm@9436
   486
by (fast_arith_tac 1);
wenzelm@9436
   487
*)