src/HOL/Integ/int_arith1.ML
author wenzelm
Mon Oct 17 23:10:10 2005 +0200 (2005-10-17)
changeset 17875 d81094515061
parent 16973 b2a894562b8f
child 17956 369e2af8ee45
permissions -rw-r--r--
change_claset/simpset;
Simplifier.inherit_context instead of Simplifier.inherit_bounds;
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@14329
     8
(** Misc ML bindings **)
paulson@14329
     9
paulson@14272
    10
val bin_succ_Pls = thm"bin_succ_Pls";
paulson@14272
    11
val bin_succ_Min = thm"bin_succ_Min";
paulson@15013
    12
val bin_succ_1 = thm"bin_succ_1";
paulson@15013
    13
val bin_succ_0 = thm"bin_succ_0";
paulson@15013
    14
paulson@14272
    15
val bin_pred_Pls = thm"bin_pred_Pls";
paulson@14272
    16
val bin_pred_Min = thm"bin_pred_Min";
paulson@15013
    17
val bin_pred_1 = thm"bin_pred_1";
paulson@15013
    18
val bin_pred_0 = thm"bin_pred_0";
paulson@15013
    19
paulson@14272
    20
val bin_minus_Pls = thm"bin_minus_Pls";
paulson@14272
    21
val bin_minus_Min = thm"bin_minus_Min";
paulson@15013
    22
val bin_minus_1 = thm"bin_minus_1";
paulson@15013
    23
val bin_minus_0 = thm"bin_minus_0";
paulson@15013
    24
paulson@14272
    25
val bin_add_Pls = thm"bin_add_Pls";
paulson@14272
    26
val bin_add_Min = thm"bin_add_Min";
paulson@14272
    27
val bin_add_BIT_11 = thm"bin_add_BIT_11";
paulson@14272
    28
val bin_add_BIT_10 = thm"bin_add_BIT_10";
paulson@14272
    29
val bin_add_BIT_0 = thm"bin_add_BIT_0";
paulson@14272
    30
val bin_add_Pls_right = thm"bin_add_Pls_right";
paulson@14272
    31
val bin_add_Min_right = thm"bin_add_Min_right";
paulson@15013
    32
paulson@15013
    33
val bin_mult_Pls = thm"bin_mult_Pls";
paulson@15013
    34
val bin_mult_Min = thm"bin_mult_Min";
paulson@14272
    35
val bin_mult_1 = thm"bin_mult_1";
paulson@14272
    36
val bin_mult_0 = thm"bin_mult_0";
paulson@15013
    37
paulson@15013
    38
val neg_def = thm "neg_def";
paulson@15013
    39
val iszero_def = thm "iszero_def";
paulson@15013
    40
paulson@14272
    41
val number_of_succ = thm"number_of_succ";
paulson@14272
    42
val number_of_pred = thm"number_of_pred";
paulson@14272
    43
val number_of_minus = thm"number_of_minus";
paulson@14272
    44
val number_of_add = thm"number_of_add";
paulson@14272
    45
val diff_number_of_eq = thm"diff_number_of_eq";
paulson@14272
    46
val number_of_mult = thm"number_of_mult";
paulson@14272
    47
val double_number_of_BIT = thm"double_number_of_BIT";
paulson@14387
    48
val numeral_0_eq_0 = thm"numeral_0_eq_0";
paulson@14387
    49
val numeral_1_eq_1 = thm"numeral_1_eq_1";
paulson@14387
    50
val numeral_m1_eq_minus_1 = thm"numeral_m1_eq_minus_1";
paulson@14387
    51
val mult_minus1 = thm"mult_minus1";
paulson@14387
    52
val mult_minus1_right = thm"mult_minus1_right";
paulson@14387
    53
val minus_number_of_mult = thm"minus_number_of_mult";
paulson@14272
    54
val zero_less_nat_eq = thm"zero_less_nat_eq";
paulson@14272
    55
val eq_number_of_eq = thm"eq_number_of_eq";
paulson@14272
    56
val iszero_number_of_Pls = thm"iszero_number_of_Pls";
paulson@14272
    57
val nonzero_number_of_Min = thm"nonzero_number_of_Min";
paulson@14272
    58
val iszero_number_of_BIT = thm"iszero_number_of_BIT";
paulson@14272
    59
val iszero_number_of_0 = thm"iszero_number_of_0";
paulson@14272
    60
val iszero_number_of_1 = thm"iszero_number_of_1";
paulson@14272
    61
val less_number_of_eq_neg = thm"less_number_of_eq_neg";
paulson@14387
    62
val le_number_of_eq = thm"le_number_of_eq";
paulson@14272
    63
val not_neg_number_of_Pls = thm"not_neg_number_of_Pls";
paulson@14272
    64
val neg_number_of_Min = thm"neg_number_of_Min";
paulson@14272
    65
val neg_number_of_BIT = thm"neg_number_of_BIT";
paulson@14272
    66
val le_number_of_eq_not_less = thm"le_number_of_eq_not_less";
paulson@14387
    67
val abs_number_of = thm"abs_number_of";
paulson@14272
    68
val number_of_reorient = thm"number_of_reorient";
paulson@14272
    69
val add_number_of_left = thm"add_number_of_left";
paulson@14272
    70
val mult_number_of_left = thm"mult_number_of_left";
paulson@14272
    71
val add_number_of_diff1 = thm"add_number_of_diff1";
paulson@14272
    72
val add_number_of_diff2 = thm"add_number_of_diff2";
paulson@14272
    73
val less_iff_diff_less_0 = thm"less_iff_diff_less_0";
paulson@14272
    74
val eq_iff_diff_eq_0 = thm"eq_iff_diff_eq_0";
paulson@14272
    75
val le_iff_diff_le_0 = thm"le_iff_diff_le_0";
wenzelm@9436
    76
paulson@14272
    77
val bin_arith_extra_simps = thms"bin_arith_extra_simps";
paulson@14272
    78
val bin_arith_simps = thms"bin_arith_simps";
paulson@14272
    79
val bin_rel_simps = thms"bin_rel_simps";
wenzelm@9436
    80
paulson@14272
    81
val zless_imp_add1_zle = thm "zless_imp_add1_zle";
wenzelm@9436
    82
paulson@14272
    83
val combine_common_factor = thm"combine_common_factor";
paulson@14272
    84
val eq_add_iff1 = thm"eq_add_iff1";
paulson@14272
    85
val eq_add_iff2 = thm"eq_add_iff2";
paulson@14272
    86
val less_add_iff1 = thm"less_add_iff1";
paulson@14272
    87
val less_add_iff2 = thm"less_add_iff2";
paulson@14272
    88
val le_add_iff1 = thm"le_add_iff1";
paulson@14272
    89
val le_add_iff2 = thm"le_add_iff2";
wenzelm@9436
    90
paulson@14387
    91
val arith_special = thms"arith_special";
wenzelm@9436
    92
paulson@14272
    93
structure Bin_Simprocs =
paulson@14272
    94
  struct
paulson@14272
    95
  fun prove_conv tacs sg (hyps: thm list) xs (t, u) =
skalberg@15531
    96
    if t aconv u then NONE
paulson@14272
    97
    else
paulson@14272
    98
      let val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u))
skalberg@15531
    99
      in SOME (Tactic.prove sg xs [] eq (K (EVERY tacs))) end
wenzelm@9436
   100
paulson@14272
   101
  fun prove_conv_nohyps tacs sg = prove_conv tacs sg [];
paulson@14272
   102
  fun prove_conv_nohyps_novars tacs sg = prove_conv tacs sg [] [];
paulson@14272
   103
paulson@14272
   104
  fun prep_simproc (name, pats, proc) =
paulson@14272
   105
    Simplifier.simproc (Theory.sign_of (the_context())) name pats proc;
paulson@14272
   106
paulson@14272
   107
  fun is_numeral (Const("Numeral.number_of", _) $ w) = true
paulson@14272
   108
    | is_numeral _ = false
wenzelm@9436
   109
paulson@14272
   110
  fun simplify_meta_eq f_number_of_eq f_eq =
paulson@14272
   111
      mk_meta_eq ([f_eq, f_number_of_eq] MRS trans)
wenzelm@9436
   112
paulson@14272
   113
  (*reorientation simprules using ==, for the following simproc*)
paulson@14272
   114
  val meta_zero_reorient = zero_reorient RS eq_reflection
paulson@14272
   115
  val meta_one_reorient = one_reorient RS eq_reflection
paulson@14272
   116
  val meta_number_of_reorient = number_of_reorient RS eq_reflection
wenzelm@9436
   117
paulson@14272
   118
  (*reorientation simplification procedure: reorients (polymorphic) 
paulson@14272
   119
    0 = x, 1 = x, nnn = x provided x isn't 0, 1 or a numeral.*)
paulson@14272
   120
  fun reorient_proc sg _ (_ $ t $ u) =
paulson@14272
   121
    case u of
skalberg@15531
   122
	Const("0", _) => NONE
skalberg@15531
   123
      | Const("1", _) => NONE
skalberg@15531
   124
      | Const("Numeral.number_of", _) $ _ => NONE
skalberg@15531
   125
      | _ => SOME (case t of
paulson@14272
   126
		  Const("0", _) => meta_zero_reorient
paulson@14272
   127
		| Const("1", _) => meta_one_reorient
paulson@14272
   128
		| Const("Numeral.number_of", _) $ _ => meta_number_of_reorient)
paulson@14272
   129
paulson@14272
   130
  val reorient_simproc = 
paulson@14272
   131
      prep_simproc ("reorient_simproc", ["0=x", "1=x", "number_of w = x"], reorient_proc)
paulson@14272
   132
paulson@14272
   133
  end;
paulson@14272
   134
paulson@14272
   135
paulson@14387
   136
Addsimps arith_special;
paulson@14272
   137
Addsimprocs [Bin_Simprocs.reorient_simproc];
wenzelm@9436
   138
wenzelm@9436
   139
wenzelm@9436
   140
structure Int_Numeral_Simprocs =
wenzelm@9436
   141
struct
wenzelm@9436
   142
paulson@11868
   143
(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic in simprocs
paulson@11868
   144
  isn't complicated by the abstract 0 and 1.*)
paulson@14387
   145
val numeral_syms = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym];
paulson@11868
   146
paulson@14474
   147
(** New term ordering so that AC-rewriting brings numerals to the front **)
paulson@14474
   148
paulson@14474
   149
(*Order integers by absolute value and then by sign. The standard integer
paulson@14474
   150
  ordering is not well-founded.*)
paulson@14474
   151
fun num_ord (i,j) =
paulson@15965
   152
      (case IntInf.compare (IntInf.abs i, IntInf.abs j) of
wenzelm@16973
   153
            EQUAL => int_ord (IntInf.sign i, IntInf.sign j) 
paulson@14474
   154
          | ord => ord);
paulson@14474
   155
paulson@14474
   156
(*This resembles Term.term_ord, but it puts binary numerals before other
paulson@14474
   157
  non-atomic terms.*)
paulson@14474
   158
local open Term 
paulson@14474
   159
in 
paulson@14474
   160
fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =
paulson@14474
   161
      (case numterm_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
paulson@14474
   162
  | numterm_ord
paulson@14474
   163
     (Const("Numeral.number_of", _) $ v, Const("Numeral.number_of", _) $ w) =
paulson@14474
   164
     num_ord (HOLogic.dest_binum v, HOLogic.dest_binum w)
paulson@14474
   165
  | numterm_ord (Const("Numeral.number_of", _) $ _, _) = LESS
paulson@14474
   166
  | numterm_ord (_, Const("Numeral.number_of", _) $ _) = GREATER
paulson@14474
   167
  | numterm_ord (t, u) =
wenzelm@16973
   168
      (case int_ord (size_of_term t, size_of_term u) of
paulson@14474
   169
        EQUAL =>
paulson@14474
   170
          let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
paulson@14474
   171
            (case hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)
paulson@14474
   172
          end
paulson@14474
   173
      | ord => ord)
paulson@14474
   174
and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
paulson@14474
   175
end;
paulson@14474
   176
paulson@14474
   177
fun numtermless tu = (numterm_ord tu = LESS);
paulson@14474
   178
paulson@14474
   179
(*Defined in this file, but perhaps needed only for simprocs of type nat.*)
paulson@14474
   180
val num_ss = HOL_ss settermless numtermless;
paulson@14474
   181
paulson@14474
   182
paulson@14474
   183
(** Utilities **)
wenzelm@9436
   184
paulson@14387
   185
fun mk_numeral T n = HOLogic.number_of_const T $ HOLogic.mk_bin n;
wenzelm@9436
   186
wenzelm@9436
   187
(*Decodes a binary INTEGER*)
paulson@11868
   188
fun dest_numeral (Const("0", _)) = 0
paulson@11868
   189
  | dest_numeral (Const("1", _)) = 1
wenzelm@13462
   190
  | dest_numeral (Const("Numeral.number_of", _) $ w) =
wenzelm@10890
   191
     (HOLogic.dest_binum w
wenzelm@10890
   192
      handle TERM _ => raise TERM("Int_Numeral_Simprocs.dest_numeral:1", [w]))
wenzelm@9436
   193
  | dest_numeral t = raise TERM("Int_Numeral_Simprocs.dest_numeral:2", [t]);
wenzelm@9436
   194
wenzelm@9436
   195
fun find_first_numeral past (t::terms) =
wenzelm@13462
   196
        ((dest_numeral t, rev past @ terms)
wenzelm@13462
   197
         handle TERM _ => find_first_numeral (t::past) terms)
wenzelm@9436
   198
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
wenzelm@9436
   199
wenzelm@9436
   200
val mk_plus = HOLogic.mk_binop "op +";
wenzelm@9436
   201
paulson@14387
   202
fun mk_minus t = 
paulson@14387
   203
  let val T = Term.fastype_of t
paulson@14387
   204
  in Const ("uminus", T --> T) $ t
paulson@14387
   205
  end;
wenzelm@9436
   206
paulson@11868
   207
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
paulson@14387
   208
fun mk_sum T []        = mk_numeral T 0
paulson@14387
   209
  | mk_sum T [t,u]     = mk_plus (t, u)
paulson@14387
   210
  | mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
wenzelm@9436
   211
wenzelm@9436
   212
(*this version ALWAYS includes a trailing zero*)
paulson@14387
   213
fun long_mk_sum T []        = mk_numeral T 0
paulson@14387
   214
  | long_mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
wenzelm@9436
   215
paulson@14387
   216
val dest_plus = HOLogic.dest_bin "op +" Term.dummyT;
wenzelm@9436
   217
wenzelm@9436
   218
(*decompose additions AND subtractions as a sum*)
wenzelm@9436
   219
fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) =
wenzelm@9436
   220
        dest_summing (pos, t, dest_summing (pos, u, ts))
wenzelm@9436
   221
  | dest_summing (pos, Const ("op -", _) $ t $ u, ts) =
wenzelm@9436
   222
        dest_summing (pos, t, dest_summing (not pos, u, ts))
wenzelm@9436
   223
  | dest_summing (pos, t, ts) =
paulson@14387
   224
        if pos then t::ts else mk_minus t :: ts;
wenzelm@9436
   225
wenzelm@9436
   226
fun dest_sum t = dest_summing (true, t, []);
wenzelm@9436
   227
wenzelm@9436
   228
val mk_diff = HOLogic.mk_binop "op -";
paulson@14387
   229
val dest_diff = HOLogic.dest_bin "op -" Term.dummyT;
wenzelm@9436
   230
wenzelm@9436
   231
val mk_times = HOLogic.mk_binop "op *";
wenzelm@9436
   232
paulson@14387
   233
fun mk_prod T = 
paulson@14387
   234
  let val one = mk_numeral T 1
paulson@14387
   235
  fun mk [] = one
paulson@14387
   236
    | mk [t] = t
paulson@14387
   237
    | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)
paulson@14387
   238
  in mk end;
wenzelm@9436
   239
paulson@14387
   240
(*This version ALWAYS includes a trailing one*)
paulson@14387
   241
fun long_mk_prod T []        = mk_numeral T 1
paulson@14387
   242
  | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);
paulson@14387
   243
paulson@14387
   244
val dest_times = HOLogic.dest_bin "op *" Term.dummyT;
wenzelm@9436
   245
wenzelm@9436
   246
fun dest_prod t =
wenzelm@13462
   247
      let val (t,u) = dest_times t
wenzelm@9436
   248
      in  dest_prod t @ dest_prod u  end
wenzelm@9436
   249
      handle TERM _ => [t];
wenzelm@9436
   250
wenzelm@13462
   251
(*DON'T do the obvious simplifications; that would create special cases*)
paulson@14387
   252
fun mk_coeff (k, t) = mk_times (mk_numeral (Term.fastype_of t) k, t);
wenzelm@9436
   253
wenzelm@9436
   254
(*Express t as a product of (possibly) a numeral with other sorted terms*)
wenzelm@9436
   255
fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t
wenzelm@9436
   256
  | dest_coeff sign t =
wenzelm@9436
   257
    let val ts = sort Term.term_ord (dest_prod t)
wenzelm@13462
   258
        val (n, ts') = find_first_numeral [] ts
wenzelm@9436
   259
                          handle TERM _ => (1, ts)
paulson@14387
   260
    in (sign*n, mk_prod (Term.fastype_of t) ts') end;
wenzelm@9436
   261
wenzelm@9436
   262
(*Find first coefficient-term THAT MATCHES u*)
wenzelm@13462
   263
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
wenzelm@9436
   264
  | find_first_coeff past u (t::terms) =
wenzelm@13462
   265
        let val (n,u') = dest_coeff 1 t
wenzelm@13462
   266
        in  if u aconv u' then (n, rev past @ terms)
wenzelm@13462
   267
                          else find_first_coeff (t::past) u terms
wenzelm@13462
   268
        end
wenzelm@13462
   269
        handle TERM _ => find_first_coeff (t::past) u terms;
wenzelm@9436
   270
wenzelm@9436
   271
paulson@11868
   272
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1*)
paulson@14387
   273
val add_0s =  thms "add_0s";
paulson@14387
   274
val mult_1s = thms "mult_1s";
wenzelm@9436
   275
paulson@11868
   276
(*To perform binary arithmetic.  The "left" rewriting handles patterns
paulson@11868
   277
  created by the simprocs, such as 3 * (5 * x). *)
paulson@14387
   278
val bin_simps = [numeral_0_eq_0 RS sym, numeral_1_eq_1 RS sym,
wenzelm@13462
   279
                 add_number_of_left, mult_number_of_left] @
paulson@11868
   280
                bin_arith_simps @ bin_rel_simps;
wenzelm@9436
   281
paulson@14113
   282
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
paulson@14113
   283
  during re-arrangement*)
paulson@14113
   284
val non_add_bin_simps = 
paulson@14113
   285
    bin_simps \\ [add_number_of_left, number_of_add RS sym];
paulson@14113
   286
wenzelm@9436
   287
(*To evaluate binary negations of coefficients*)
paulson@15013
   288
val minus_simps = [numeral_m1_eq_minus_1 RS sym, number_of_minus RS sym,
paulson@15013
   289
                   bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min,
paulson@15013
   290
                   bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min];
wenzelm@9436
   291
wenzelm@9436
   292
(*To let us treat subtraction as addition*)
paulson@14387
   293
val diff_simps = [diff_minus, minus_add_distrib, minus_minus];
wenzelm@9436
   294
paulson@10713
   295
(*push the unary minus down: - x * y = x * - y *)
paulson@14387
   296
val minus_mult_eq_1_to_2 =
paulson@14387
   297
    [minus_mult_left RS sym, minus_mult_right] MRS trans |> standard;
paulson@10713
   298
paulson@10713
   299
(*to extract again any uncancelled minuses*)
paulson@14387
   300
val minus_from_mult_simps =
paulson@14387
   301
    [minus_minus, minus_mult_left RS sym, minus_mult_right RS sym];
paulson@10713
   302
paulson@10713
   303
(*combine unary minus with numeric literals, however nested within a product*)
paulson@14387
   304
val mult_minus_simps =
paulson@14387
   305
    [mult_assoc, minus_mult_left, minus_mult_eq_1_to_2];
paulson@10713
   306
wenzelm@9436
   307
(*Apply the given rewrite (if present) just once*)
skalberg@15531
   308
fun trans_tac NONE      = all_tac
skalberg@15531
   309
  | trans_tac (SOME th) = ALLGOALS (rtac (th RS trans));
wenzelm@9436
   310
wenzelm@16973
   311
fun simplify_meta_eq rules ss =
wenzelm@17875
   312
    simplify (Simplifier.inherit_context ss HOL_basic_ss addeqcongs[eq_cong2] addsimps rules)
paulson@12975
   313
    o mk_meta_eq;
wenzelm@9436
   314
wenzelm@9436
   315
structure CancelNumeralsCommon =
wenzelm@9436
   316
  struct
wenzelm@13462
   317
  val mk_sum            = mk_sum
wenzelm@13462
   318
  val dest_sum          = dest_sum
wenzelm@13462
   319
  val mk_coeff          = mk_coeff
wenzelm@13462
   320
  val dest_coeff        = dest_coeff 1
wenzelm@13462
   321
  val find_first_coeff  = find_first_coeff []
wenzelm@16973
   322
  val trans_tac         = fn _ => trans_tac
wenzelm@16973
   323
  fun norm_tac ss =
wenzelm@17875
   324
    let val num_ss' = Simplifier.inherit_context ss num_ss in
wenzelm@16973
   325
      ALLGOALS (simp_tac (num_ss' addsimps numeral_syms @ add_0s @ mult_1s @
wenzelm@16973
   326
                                         diff_simps @ minus_simps @ add_ac))
wenzelm@16973
   327
      THEN ALLGOALS (simp_tac (num_ss' addsimps non_add_bin_simps @ mult_minus_simps))
wenzelm@16973
   328
      THEN ALLGOALS (simp_tac (num_ss' addsimps minus_from_mult_simps @ add_ac @ mult_ac))
wenzelm@16973
   329
    end
wenzelm@16973
   330
  fun numeral_simp_tac ss =
wenzelm@17875
   331
    ALLGOALS (simp_tac (Simplifier.inherit_context ss HOL_ss addsimps add_0s @ bin_simps))
wenzelm@16973
   332
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s)
wenzelm@9436
   333
  end;
wenzelm@9436
   334
wenzelm@9436
   335
wenzelm@9436
   336
structure EqCancelNumerals = CancelNumeralsFun
wenzelm@9436
   337
 (open CancelNumeralsCommon
wenzelm@13485
   338
  val prove_conv = Bin_Simprocs.prove_conv
wenzelm@9436
   339
  val mk_bal   = HOLogic.mk_eq
paulson@14387
   340
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
wenzelm@9436
   341
  val bal_add1 = eq_add_iff1 RS trans
wenzelm@9436
   342
  val bal_add2 = eq_add_iff2 RS trans
wenzelm@9436
   343
);
wenzelm@9436
   344
wenzelm@9436
   345
structure LessCancelNumerals = CancelNumeralsFun
wenzelm@9436
   346
 (open CancelNumeralsCommon
wenzelm@13485
   347
  val prove_conv = Bin_Simprocs.prove_conv
wenzelm@9436
   348
  val mk_bal   = HOLogic.mk_binrel "op <"
paulson@14387
   349
  val dest_bal = HOLogic.dest_bin "op <" Term.dummyT
wenzelm@9436
   350
  val bal_add1 = less_add_iff1 RS trans
wenzelm@9436
   351
  val bal_add2 = less_add_iff2 RS trans
wenzelm@9436
   352
);
wenzelm@9436
   353
wenzelm@9436
   354
structure LeCancelNumerals = 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 <="
paulson@14387
   358
  val dest_bal = HOLogic.dest_bin "op <=" Term.dummyT
wenzelm@9436
   359
  val bal_add1 = le_add_iff1 RS trans
wenzelm@9436
   360
  val bal_add2 = le_add_iff2 RS trans
wenzelm@9436
   361
);
wenzelm@9436
   362
wenzelm@13462
   363
val cancel_numerals =
paulson@11868
   364
  map Bin_Simprocs.prep_simproc
wenzelm@9436
   365
   [("inteq_cancel_numerals",
paulson@14387
   366
     ["(l::'a::number_ring) + m = n",
paulson@14387
   367
      "(l::'a::number_ring) = m + n",
paulson@14387
   368
      "(l::'a::number_ring) - m = n",
paulson@14387
   369
      "(l::'a::number_ring) = m - n",
paulson@14387
   370
      "(l::'a::number_ring) * m = n",
paulson@14387
   371
      "(l::'a::number_ring) = m * n"],
wenzelm@9436
   372
     EqCancelNumerals.proc),
wenzelm@13462
   373
    ("intless_cancel_numerals",
obua@14738
   374
     ["(l::'a::{ordered_idom,number_ring}) + m < n",
obua@14738
   375
      "(l::'a::{ordered_idom,number_ring}) < m + n",
obua@14738
   376
      "(l::'a::{ordered_idom,number_ring}) - m < n",
obua@14738
   377
      "(l::'a::{ordered_idom,number_ring}) < m - n",
obua@14738
   378
      "(l::'a::{ordered_idom,number_ring}) * m < n",
obua@14738
   379
      "(l::'a::{ordered_idom,number_ring}) < m * n"],
wenzelm@9436
   380
     LessCancelNumerals.proc),
wenzelm@13462
   381
    ("intle_cancel_numerals",
obua@14738
   382
     ["(l::'a::{ordered_idom,number_ring}) + m <= n",
obua@14738
   383
      "(l::'a::{ordered_idom,number_ring}) <= m + n",
obua@14738
   384
      "(l::'a::{ordered_idom,number_ring}) - m <= n",
obua@14738
   385
      "(l::'a::{ordered_idom,number_ring}) <= m - n",
obua@14738
   386
      "(l::'a::{ordered_idom,number_ring}) * m <= n",
obua@14738
   387
      "(l::'a::{ordered_idom,number_ring}) <= m * n"],
wenzelm@9436
   388
     LeCancelNumerals.proc)];
wenzelm@9436
   389
wenzelm@9436
   390
wenzelm@9436
   391
structure CombineNumeralsData =
wenzelm@9436
   392
  struct
paulson@15965
   393
  val add               = IntInf.+ 
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@16973
   400
  val trans_tac         = fn _ => trans_tac
wenzelm@16973
   401
  fun norm_tac ss =
wenzelm@17875
   402
    let val num_ss' = Simplifier.inherit_context ss num_ss in
wenzelm@16973
   403
      ALLGOALS (simp_tac (num_ss' addsimps numeral_syms @ add_0s @ mult_1s @
wenzelm@16973
   404
                                          diff_simps @ minus_simps @ add_ac))
wenzelm@16973
   405
      THEN ALLGOALS (simp_tac (num_ss' addsimps non_add_bin_simps @ mult_minus_simps))
wenzelm@16973
   406
      THEN ALLGOALS (simp_tac (num_ss' addsimps minus_from_mult_simps @ add_ac @ mult_ac))
wenzelm@16973
   407
    end
wenzelm@16973
   408
  fun numeral_simp_tac ss =
wenzelm@17875
   409
    ALLGOALS (simp_tac (Simplifier.inherit_context ss HOL_ss addsimps add_0s @ bin_simps))
wenzelm@16973
   410
  val simplify_meta_eq = simplify_meta_eq (add_0s @ mult_1s)
wenzelm@9436
   411
  end;
wenzelm@9436
   412
wenzelm@9436
   413
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
wenzelm@13462
   414
wenzelm@13462
   415
val combine_numerals =
wenzelm@13462
   416
  Bin_Simprocs.prep_simproc
paulson@14387
   417
    ("int_combine_numerals", 
paulson@14387
   418
     ["(i::'a::number_ring) + j", "(i::'a::number_ring) - j"], 
paulson@14387
   419
     CombineNumerals.proc);
wenzelm@9436
   420
wenzelm@9436
   421
end;
wenzelm@9436
   422
wenzelm@9436
   423
Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
wenzelm@9436
   424
Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
wenzelm@9436
   425
wenzelm@9436
   426
(*examples:
wenzelm@9436
   427
print_depth 22;
wenzelm@9436
   428
set timing;
wenzelm@9436
   429
set trace_simp;
wenzelm@13462
   430
fun test s = (Goal s, by (Simp_tac 1));
wenzelm@9436
   431
wenzelm@11704
   432
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
wenzelm@9436
   433
wenzelm@11704
   434
test "2*u = (u::int)";
wenzelm@11704
   435
test "(i + j + 12 + (k::int)) - 15 = y";
wenzelm@11704
   436
test "(i + j + 12 + (k::int)) - 5 = y";
wenzelm@9436
   437
wenzelm@9436
   438
test "y - b < (b::int)";
wenzelm@11704
   439
test "y - (3*b + c) < (b::int) - 2*c";
wenzelm@9436
   440
wenzelm@11704
   441
test "(2*x - (u*v) + y) - v*3*u = (w::int)";
wenzelm@11704
   442
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
wenzelm@11704
   443
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
wenzelm@11704
   444
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
wenzelm@9436
   445
wenzelm@11704
   446
test "(i + j + 12 + (k::int)) = u + 15 + y";
wenzelm@11704
   447
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
wenzelm@9436
   448
wenzelm@11704
   449
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
   450
wenzelm@9436
   451
test "a + -(b+c) + b = (d::int)";
wenzelm@9436
   452
test "a + -(b+c) - b = (d::int)";
wenzelm@9436
   453
wenzelm@9436
   454
(*negative numerals*)
wenzelm@11704
   455
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
wenzelm@11704
   456
test "(i + j + -3 + (k::int)) < u + 5 + y";
wenzelm@11704
   457
test "(i + j + 3 + (k::int)) < u + -6 + y";
wenzelm@11704
   458
test "(i + j + -12 + (k::int)) - 15 = y";
wenzelm@11704
   459
test "(i + j + 12 + (k::int)) - -15 = y";
wenzelm@11704
   460
test "(i + j + -12 + (k::int)) - -15 = y";
wenzelm@9436
   461
*)
wenzelm@9436
   462
wenzelm@9436
   463
paulson@14387
   464
(** Constant folding for multiplication in semirings **)
wenzelm@9436
   465
paulson@14387
   466
(*We do not need folding for addition: combine_numerals does the same thing*)
wenzelm@9436
   467
paulson@14387
   468
structure Semiring_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@16423
   472
  val thy_ref = Theory.self_ref (the_context ())
wenzelm@9436
   473
  val add_ac = mult_ac
wenzelm@9436
   474
end;
wenzelm@9436
   475
paulson@14387
   476
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
wenzelm@9436
   477
paulson@14387
   478
val assoc_fold_simproc =
paulson@14387
   479
  Bin_Simprocs.prep_simproc
obua@14738
   480
   ("semiring_assoc_fold", ["(a::'a::comm_semiring_1_cancel) * b"],
paulson@14387
   481
    Semiring_Times_Assoc.proc);
paulson@14387
   482
paulson@14387
   483
Addsimprocs [assoc_fold_simproc];
paulson@14387
   484
paulson@14387
   485
wenzelm@9436
   486
wenzelm@9436
   487
wenzelm@9436
   488
(*** decision procedure for linear arithmetic ***)
wenzelm@9436
   489
wenzelm@9436
   490
(*---------------------------------------------------------------------------*)
wenzelm@9436
   491
(* Linear arithmetic                                                         *)
wenzelm@9436
   492
(*---------------------------------------------------------------------------*)
wenzelm@9436
   493
wenzelm@9436
   494
(*
wenzelm@9436
   495
Instantiation of the generic linear arithmetic package for int.
wenzelm@9436
   496
*)
wenzelm@9436
   497
wenzelm@9436
   498
(* Update parameters of arithmetic prover *)
wenzelm@9436
   499
local
wenzelm@9436
   500
wenzelm@9436
   501
(* reduce contradictory <= to False *)
wenzelm@13462
   502
val add_rules =
paulson@14387
   503
    simp_thms @ bin_arith_simps @ bin_rel_simps @ arith_special @
paulson@14390
   504
    [neg_le_iff_le, numeral_0_eq_0, numeral_1_eq_1,
paulson@14369
   505
     minus_zero, diff_minus, left_minus, right_minus,
paulson@14369
   506
     mult_zero_left, mult_zero_right, mult_1, mult_1_right,
paulson@14369
   507
     minus_mult_left RS sym, minus_mult_right RS sym,
paulson@14369
   508
     minus_add_distrib, minus_minus, mult_assoc,
paulson@14387
   509
     of_nat_0, of_nat_1, of_nat_Suc, of_nat_add, of_nat_mult,
paulson@16473
   510
     of_int_0, of_int_1, of_int_add, of_int_mult, int_eq_of_nat];
wenzelm@9436
   511
paulson@14387
   512
val simprocs = [assoc_fold_simproc, Int_Numeral_Simprocs.combine_numerals]@
paulson@14387
   513
               Int_Numeral_Simprocs.cancel_numerals;
wenzelm@9436
   514
wenzelm@9436
   515
in
wenzelm@9436
   516
wenzelm@9436
   517
val int_arith_setup =
nipkow@15921
   518
 [Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
paulson@14368
   519
   {add_mono_thms = add_mono_thms,
nipkow@15184
   520
    mult_mono_thms = [mult_strict_left_mono,mult_left_mono] @ mult_mono_thms,
nipkow@10574
   521
    inj_thms = [zle_int RS iffD2,int_int_eq RS iffD2] @ inj_thms,
paulson@14272
   522
    lessD = lessD @ [zless_imp_add1_zle],
nipkow@15921
   523
    neqE = thm "linorder_neqE_int" :: neqE,
wenzelm@9436
   524
    simpset = simpset addsimps add_rules
wenzelm@9436
   525
                      addsimprocs simprocs
wenzelm@9436
   526
                      addcongs [if_weak_cong]}),
paulson@14387
   527
  arith_inj_const ("IntDef.of_nat", HOLogic.natT --> HOLogic.intT),
nipkow@10834
   528
  arith_inj_const ("IntDef.int", HOLogic.natT --> HOLogic.intT),
nipkow@15185
   529
  arith_discrete "IntDef.int"];
wenzelm@9436
   530
wenzelm@9436
   531
end;
wenzelm@9436
   532
wenzelm@13462
   533
val fast_int_arith_simproc =
wenzelm@13462
   534
  Simplifier.simproc (Theory.sign_of (the_context()))
paulson@14387
   535
  "fast_int_arith" 
obua@14738
   536
     ["(m::'a::{ordered_idom,number_ring}) < n",
obua@14738
   537
      "(m::'a::{ordered_idom,number_ring}) <= n",
obua@14738
   538
      "(m::'a::{ordered_idom,number_ring}) = n"] Fast_Arith.lin_arith_prover;
wenzelm@9436
   539
wenzelm@9436
   540
Addsimprocs [fast_int_arith_simproc]
wenzelm@13462
   541
wenzelm@9436
   542
wenzelm@15661
   543
(* Some test data
wenzelm@9436
   544
Goal "!!a::int. [| a <= b; c <= d; x+y<z |] ==> a+c <= b+d";
wenzelm@9436
   545
by (fast_arith_tac 1);
wenzelm@11704
   546
Goal "!!a::int. [| a < b; c < d |] ==> a-d+ 2 <= b+(-c)";
wenzelm@9436
   547
by (fast_arith_tac 1);
paulson@11868
   548
Goal "!!a::int. [| a < b; c < d |] ==> a+c+ 1 < b+d";
wenzelm@9436
   549
by (fast_arith_tac 1);
wenzelm@9436
   550
Goal "!!a::int. [| a <= b; b+b <= c |] ==> a+a <= c";
wenzelm@9436
   551
by (fast_arith_tac 1);
wenzelm@9436
   552
Goal "!!a::int. [| a+b <= i+j; a<=b; i<=j |] \
wenzelm@9436
   553
\     ==> a+a <= j+j";
wenzelm@9436
   554
by (fast_arith_tac 1);
wenzelm@9436
   555
Goal "!!a::int. [| a+b < i+j; a<b; i<j |] \
wenzelm@11704
   556
\     ==> a+a - - -1 < j+j - 3";
wenzelm@9436
   557
by (fast_arith_tac 1);
wenzelm@9436
   558
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
   559
by (arith_tac 1);
wenzelm@9436
   560
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
   561
\     ==> a <= l";
wenzelm@9436
   562
by (fast_arith_tac 1);
wenzelm@9436
   563
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
   564
\     ==> a+a+a+a <= l+l+l+l";
wenzelm@9436
   565
by (fast_arith_tac 1);
wenzelm@9436
   566
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
   567
\     ==> a+a+a+a+a <= l+l+l+l+i";
wenzelm@9436
   568
by (fast_arith_tac 1);
wenzelm@9436
   569
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
   570
\     ==> a+a+a+a+a+a <= l+l+l+l+i+l";
wenzelm@9436
   571
by (fast_arith_tac 1);
wenzelm@9436
   572
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
   573
\     ==> 6*a <= 5*l+i";
wenzelm@9436
   574
by (fast_arith_tac 1);
wenzelm@9436
   575
*)