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