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