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