src/HOL/Integ/IntArith.ML
author wenzelm
Tue May 30 16:08:38 2000 +0200 (2000-05-30)
changeset 9000 c20d58286a51
parent 8834 074503906abf
child 9063 0d7628966069
permissions -rw-r--r--
cleaned up;
wenzelm@7707
     1
(*  Title:      HOL/Integ/IntArith.thy
wenzelm@7707
     2
    ID:         $Id$
wenzelm@7707
     3
    Authors:    Larry Paulson and Tobias Nipkow
wenzelm@7707
     4
wenzelm@7707
     5
Simprocs and decision procedure for linear arithmetic.
wenzelm@7707
     6
*)
wenzelm@7707
     7
wenzelm@7707
     8
(*** Simprocs for numeric literals ***)
wenzelm@7707
     9
wenzelm@7707
    10
(** Combining of literal coefficients in sums of products **)
wenzelm@7707
    11
wenzelm@7707
    12
Goal "(x < y) = (x-y < (#0::int))";
wenzelm@7707
    13
by (simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@7707
    14
qed "zless_iff_zdiff_zless_0";
wenzelm@7707
    15
wenzelm@7707
    16
Goal "(x = y) = (x-y = (#0::int))";
wenzelm@7707
    17
by (simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@7707
    18
qed "eq_iff_zdiff_eq_0";
wenzelm@7707
    19
wenzelm@7707
    20
Goal "(x <= y) = (x-y <= (#0::int))";
wenzelm@7707
    21
by (simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@7707
    22
qed "zle_iff_zdiff_zle_0";
wenzelm@7707
    23
wenzelm@7707
    24
paulson@8785
    25
(** For combine_numerals **)
paulson@8785
    26
paulson@8785
    27
Goal "i*u + (j*u + k) = (i+j)*u + (k::int)";
paulson@8785
    28
by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib]) 1);
paulson@8785
    29
qed "left_zadd_zmult_distrib";
paulson@8785
    30
paulson@8785
    31
paulson@8763
    32
(** For cancel_numerals **)
paulson@8763
    33
paulson@8763
    34
Goal "!!i::int. ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)";
paulson@8763
    35
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
paulson@8763
    36
qed "diff_add_eq1";
paulson@8763
    37
paulson@8763
    38
Goal "!!i::int. ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))";
paulson@8763
    39
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
paulson@8763
    40
qed "diff_add_eq2";
paulson@8763
    41
paulson@8763
    42
val rel_iff_rel_0_rls = map (inst "y" "?u+?v")
paulson@8763
    43
                          [zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0, 
paulson@8763
    44
			   zle_iff_zdiff_zle_0] @
paulson@8763
    45
		        map (inst "y" "n")
paulson@8763
    46
                          [zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0, 
paulson@8763
    47
			   zle_iff_zdiff_zle_0];
paulson@8763
    48
paulson@8763
    49
Goal "!!i::int. (i*u + m = j*u + n) = ((i-j)*u + m = n)";
paulson@8763
    50
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
paulson@8763
    51
		                     zadd_ac@rel_iff_rel_0_rls) 1);
paulson@8763
    52
qed "eq_add_iff1";
paulson@8763
    53
paulson@8763
    54
Goal "!!i::int. (i*u + m = j*u + n) = (m = (j-i)*u + n)";
paulson@8763
    55
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
paulson@8763
    56
                                     zadd_ac@rel_iff_rel_0_rls) 1);
paulson@8763
    57
qed "eq_add_iff2";
paulson@8763
    58
paulson@8763
    59
Goal "!!i::int. (i*u + m < j*u + n) = ((i-j)*u + m < n)";
paulson@8763
    60
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
paulson@8763
    61
                                     zadd_ac@rel_iff_rel_0_rls) 1);
paulson@8763
    62
qed "less_add_iff1";
paulson@8763
    63
paulson@8763
    64
Goal "!!i::int. (i*u + m < j*u + n) = (m < (j-i)*u + n)";
paulson@8763
    65
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
paulson@8763
    66
                                     zadd_ac@rel_iff_rel_0_rls) 1);
paulson@8763
    67
qed "less_add_iff2";
paulson@8763
    68
paulson@8763
    69
Goal "!!i::int. (i*u + m <= j*u + n) = ((i-j)*u + m <= n)";
paulson@8763
    70
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
paulson@8763
    71
                                     zadd_ac@rel_iff_rel_0_rls) 1);
paulson@8763
    72
qed "le_add_iff1";
paulson@8763
    73
paulson@8763
    74
Goal "!!i::int. (i*u + m <= j*u + n) = (m <= (j-i)*u + n)";
paulson@8763
    75
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]
paulson@8763
    76
                                     @zadd_ac@rel_iff_rel_0_rls) 1);
paulson@8763
    77
qed "le_add_iff2";
paulson@8763
    78
paulson@8799
    79
(*To tidy up the result of a simproc.  Only the RHS will be simplified.*)
paulson@8834
    80
Goal "u = u' ==> (t==u) == (t==u')";
paulson@8799
    81
by Auto_tac;
paulson@8799
    82
qed "eq_cong2";
paulson@8799
    83
paulson@8763
    84
paulson@8763
    85
structure Int_Numeral_Simprocs =
paulson@8763
    86
struct
paulson@8763
    87
paulson@8763
    88
(*Utilities*)
paulson@8763
    89
paulson@8763
    90
fun mk_numeral n = HOLogic.number_of_const HOLogic.intT $ 
paulson@8763
    91
                   NumeralSyntax.mk_bin n;
paulson@8763
    92
paulson@8763
    93
(*Decodes a binary INTEGER*)
paulson@8785
    94
fun dest_numeral (Const("Numeral.number_of", _) $ w) = 
paulson@8785
    95
     (NumeralSyntax.dest_bin w
paulson@8785
    96
      handle Match => raise TERM("Int_Numeral_Simprocs.dest_numeral:1", [w]))
paulson@8785
    97
  | dest_numeral t = raise TERM("Int_Numeral_Simprocs.dest_numeral:2", [t]);
paulson@8763
    98
paulson@8763
    99
fun find_first_numeral past (t::terms) =
paulson@8763
   100
	((dest_numeral t, rev past @ terms)
paulson@8763
   101
	 handle TERM _ => find_first_numeral (t::past) terms)
paulson@8763
   102
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
paulson@8763
   103
paulson@8763
   104
val zero = mk_numeral 0;
paulson@8763
   105
val mk_plus = HOLogic.mk_binop "op +";
paulson@8763
   106
paulson@8763
   107
val uminus_const = Const ("uminus", HOLogic.intT --> HOLogic.intT);
paulson@8763
   108
paulson@8763
   109
(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
paulson@8763
   110
fun mk_sum []        = zero
paulson@8763
   111
  | mk_sum [t,u]     = mk_plus (t, u)
paulson@8763
   112
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
paulson@8763
   113
paulson@8785
   114
(*this version ALWAYS includes a trailing zero*)
paulson@8785
   115
fun long_mk_sum []        = zero
paulson@8785
   116
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
paulson@8785
   117
paulson@8763
   118
val dest_plus = HOLogic.dest_bin "op +" HOLogic.intT;
paulson@8763
   119
paulson@8763
   120
(*decompose additions AND subtractions as a sum*)
paulson@8763
   121
fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) =
paulson@8763
   122
        dest_summing (pos, t, dest_summing (pos, u, ts))
paulson@8763
   123
  | dest_summing (pos, Const ("op -", _) $ t $ u, ts) =
paulson@8763
   124
        dest_summing (pos, t, dest_summing (not pos, u, ts))
paulson@8763
   125
  | dest_summing (pos, t, ts) =
paulson@8763
   126
	if pos then t::ts else uminus_const$t :: ts;
paulson@8763
   127
paulson@8763
   128
fun dest_sum t = dest_summing (true, t, []);
paulson@8763
   129
paulson@8763
   130
val mk_diff = HOLogic.mk_binop "op -";
paulson@8763
   131
val dest_diff = HOLogic.dest_bin "op -" HOLogic.intT;
paulson@8763
   132
paulson@8763
   133
val one = mk_numeral 1;
paulson@8763
   134
val mk_times = HOLogic.mk_binop "op *";
paulson@8763
   135
paulson@8763
   136
fun mk_prod [] = one
paulson@8763
   137
  | mk_prod [t] = t
paulson@8763
   138
  | mk_prod (t :: ts) = if t = one then mk_prod ts
paulson@8763
   139
                        else mk_times (t, mk_prod ts);
paulson@8763
   140
paulson@8763
   141
val dest_times = HOLogic.dest_bin "op *" HOLogic.intT;
paulson@8763
   142
paulson@8763
   143
fun dest_prod t =
paulson@8763
   144
      let val (t,u) = dest_times t 
paulson@8763
   145
      in  dest_prod t @ dest_prod u  end
paulson@8763
   146
      handle TERM _ => [t];
paulson@8763
   147
paulson@8763
   148
(*DON'T do the obvious simplifications; that would create special cases*) 
paulson@8763
   149
fun mk_coeff (k, ts) = mk_times (mk_numeral k, ts);
paulson@8763
   150
paulson@8763
   151
(*Express t as a product of (possibly) a numeral with other sorted terms*)
paulson@8763
   152
fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t
paulson@8763
   153
  | dest_coeff sign t =
paulson@8763
   154
    let val ts = sort Term.term_ord (dest_prod t)
paulson@8763
   155
	val (n, ts') = find_first_numeral [] ts
paulson@8763
   156
                          handle TERM _ => (1, ts)
paulson@8763
   157
    in (sign*n, mk_prod ts') end;
paulson@8763
   158
paulson@8763
   159
(*Find first coefficient-term THAT MATCHES u*)
paulson@8763
   160
fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) 
paulson@8763
   161
  | find_first_coeff past u (t::terms) =
paulson@8763
   162
	let val (n,u') = dest_coeff 1 t
paulson@8763
   163
	in  if u aconv u' then (n, rev past @ terms)
paulson@8763
   164
			  else find_first_coeff (t::past) u terms
paulson@8763
   165
	end
paulson@8763
   166
	handle TERM _ => find_first_coeff (t::past) u terms;
paulson@8763
   167
paulson@8763
   168
paulson@8763
   169
(*Simplify #1*n and n*#1 to n*)
paulson@8763
   170
val add_0s = [zadd_0, zadd_0_right];
paulson@8763
   171
val mult_1s = [zmult_1, zmult_1_right, zmult_minus1, zmult_minus1_right];
paulson@8763
   172
paulson@8763
   173
(*To perform binary arithmetic*)
paulson@8763
   174
val bin_simps = [number_of_add RS sym, add_number_of_left] @ 
paulson@8763
   175
                bin_arith_simps @ bin_rel_simps;
paulson@8763
   176
paulson@8787
   177
(*To evaluate binary negations of coefficients*)
paulson@8787
   178
val zminus_simps = NCons_simps @
paulson@8787
   179
                   [number_of_minus RS sym, 
paulson@8787
   180
		    bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min,
paulson@8787
   181
		    bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min];
paulson@8787
   182
paulson@8763
   183
(*To let us treat subtraction as addition*)
paulson@8763
   184
val diff_simps = [zdiff_def, zminus_zadd_distrib, zminus_zminus];
paulson@8763
   185
paulson@8776
   186
(*Apply the given rewrite (if present) just once*)
paulson@8799
   187
fun trans_tac None      = all_tac
paulson@8799
   188
  | trans_tac (Some th) = ALLGOALS (rtac (th RS trans));
paulson@8763
   189
paulson@8776
   190
fun prove_conv name tacs sg (t, u) =
paulson@8763
   191
  if t aconv u then None
paulson@8763
   192
  else
paulson@8799
   193
  let val ct = cterm_of sg (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u)))
paulson@8799
   194
  in Some
paulson@8799
   195
     (prove_goalw_cterm [] ct (K tacs)
paulson@8763
   196
      handle ERROR => error 
paulson@8763
   197
	  ("The error(s) above occurred while trying to prove " ^
paulson@8799
   198
	   string_of_cterm ct ^ "\nInternal failure of simproc " ^ name))
paulson@8799
   199
  end;
paulson@8799
   200
paulson@8799
   201
fun simplify_meta_eq rules =
paulson@8799
   202
    mk_meta_eq o
paulson@8834
   203
    simplify (HOL_basic_ss addeqcongs[eq_cong2] addsimps rules)
paulson@8763
   204
paulson@8763
   205
fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
paulson@8763
   206
fun prep_pat s = Thm.read_cterm (Theory.sign_of Int.thy) (s, HOLogic.termT);
paulson@8763
   207
val prep_pats = map prep_pat;
paulson@8763
   208
paulson@8763
   209
structure CancelNumeralsCommon =
paulson@8763
   210
  struct
paulson@8763
   211
  val mk_sum    	= mk_sum
paulson@8763
   212
  val dest_sum		= dest_sum
paulson@8763
   213
  val mk_coeff		= mk_coeff
paulson@8763
   214
  val dest_coeff	= dest_coeff 1
paulson@8763
   215
  val find_first_coeff	= find_first_coeff []
paulson@8799
   216
  val trans_tac         = trans_tac
paulson@8776
   217
  val norm_tac = ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
paulson@8787
   218
                                                     zminus_simps@zadd_ac))
paulson@8763
   219
                 THEN ALLGOALS
paulson@8776
   220
                    (simp_tac (HOL_ss addsimps [zmult_zminus_right RS sym]@
paulson@8776
   221
                                               bin_simps@zadd_ac@zmult_ac))
paulson@8763
   222
  val numeral_simp_tac	= ALLGOALS (simp_tac (HOL_ss addsimps add_0s@bin_simps))
paulson@8799
   223
  val simplify_meta_eq  = simplify_meta_eq (add_0s@mult_1s)
paulson@8763
   224
  end;
paulson@8763
   225
paulson@8763
   226
paulson@8763
   227
structure EqCancelNumerals = CancelNumeralsFun
paulson@8763
   228
 (open CancelNumeralsCommon
paulson@8776
   229
  val prove_conv = prove_conv "inteq_cancel_numerals"
paulson@8763
   230
  val mk_bal   = HOLogic.mk_eq
paulson@8763
   231
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.intT
paulson@8776
   232
  val bal_add1 = eq_add_iff1 RS trans
paulson@8776
   233
  val bal_add2 = eq_add_iff2 RS trans
paulson@8763
   234
);
paulson@8763
   235
paulson@8763
   236
structure LessCancelNumerals = CancelNumeralsFun
paulson@8763
   237
 (open CancelNumeralsCommon
paulson@8776
   238
  val prove_conv = prove_conv "intless_cancel_numerals"
paulson@8763
   239
  val mk_bal   = HOLogic.mk_binrel "op <"
paulson@8763
   240
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.intT
paulson@8776
   241
  val bal_add1 = less_add_iff1 RS trans
paulson@8776
   242
  val bal_add2 = less_add_iff2 RS trans
paulson@8763
   243
);
paulson@8763
   244
paulson@8763
   245
structure LeCancelNumerals = CancelNumeralsFun
paulson@8763
   246
 (open CancelNumeralsCommon
paulson@8776
   247
  val prove_conv = prove_conv "intle_cancel_numerals"
paulson@8763
   248
  val mk_bal   = HOLogic.mk_binrel "op <="
paulson@8763
   249
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.intT
paulson@8776
   250
  val bal_add1 = le_add_iff1 RS trans
paulson@8776
   251
  val bal_add2 = le_add_iff2 RS trans
paulson@8763
   252
);
paulson@8763
   253
paulson@8763
   254
val cancel_numerals = 
paulson@8763
   255
  map prep_simproc
paulson@8763
   256
   [("inteq_cancel_numerals",
paulson@8763
   257
     prep_pats ["(l::int) + m = n", "(l::int) = m + n", 
paulson@8763
   258
		"(l::int) - m = n", "(l::int) = m - n", 
paulson@8763
   259
		"(l::int) * m = n", "(l::int) = m * n"], 
paulson@8763
   260
     EqCancelNumerals.proc),
paulson@8763
   261
    ("intless_cancel_numerals", 
paulson@8763
   262
     prep_pats ["(l::int) + m < n", "(l::int) < m + n", 
paulson@8763
   263
		"(l::int) - m < n", "(l::int) < m - n", 
paulson@8763
   264
		"(l::int) * m < n", "(l::int) < m * n"], 
paulson@8763
   265
     LessCancelNumerals.proc),
paulson@8763
   266
    ("intle_cancel_numerals", 
paulson@8763
   267
     prep_pats ["(l::int) + m <= n", "(l::int) <= m + n", 
paulson@8763
   268
		"(l::int) - m <= n", "(l::int) <= m - n", 
paulson@8763
   269
		"(l::int) * m <= n", "(l::int) <= m * n"], 
paulson@8787
   270
     LeCancelNumerals.proc)];
paulson@8763
   271
paulson@8785
   272
paulson@8785
   273
structure CombineNumeralsData =
paulson@8785
   274
  struct
paulson@8785
   275
  val mk_sum    	= long_mk_sum    (*to work for e.g. #2*x + #3*x *)
paulson@8785
   276
  val dest_sum		= dest_sum
paulson@8785
   277
  val mk_coeff		= mk_coeff
paulson@8785
   278
  val dest_coeff	= dest_coeff 1
paulson@8785
   279
  val left_distrib	= left_zadd_zmult_distrib RS trans
paulson@8785
   280
  val prove_conv	= prove_conv "int_combine_numerals"
paulson@8799
   281
  val trans_tac          = trans_tac
paulson@8785
   282
  val norm_tac = ALLGOALS
paulson@8785
   283
                   (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
paulson@8787
   284
                                              zminus_simps@zadd_ac))
paulson@8785
   285
                 THEN ALLGOALS
paulson@8785
   286
                    (simp_tac (HOL_ss addsimps [zmult_zminus_right RS sym]@
paulson@8785
   287
                                               bin_simps@zadd_ac@zmult_ac))
paulson@8785
   288
  val numeral_simp_tac	= ALLGOALS 
paulson@8785
   289
                    (simp_tac (HOL_ss addsimps add_0s@bin_simps))
paulson@8799
   290
  val simplify_meta_eq  = simplify_meta_eq (add_0s@mult_1s)
paulson@8785
   291
  end;
paulson@8785
   292
paulson@8785
   293
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
paulson@8785
   294
  
paulson@8785
   295
val combine_numerals = 
paulson@8785
   296
    prep_simproc ("int_combine_numerals",
paulson@8787
   297
		  prep_pats ["(i::int) + j", "(i::int) - j"],
paulson@8785
   298
		  CombineNumerals.proc);
paulson@8785
   299
paulson@8763
   300
end;
paulson@8763
   301
paulson@8763
   302
paulson@8763
   303
Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
paulson@8785
   304
Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
paulson@8785
   305
paulson@8785
   306
(*The Abel_Cancel simprocs are now obsolete*)
paulson@8785
   307
Delsimprocs [Int_Cancel.sum_conv, Int_Cancel.rel_conv];
paulson@8763
   308
paulson@8763
   309
(*examples:
paulson@8763
   310
print_depth 22;
wenzelm@9000
   311
set timing;
paulson@8763
   312
set trace_simp;
paulson@8763
   313
fun test s = (Goal s; by (Simp_tac 1)); 
paulson@8763
   314
paulson@8785
   315
test "l + #2 + #2 + #2 + (l + #2) + (oo + #2) = (uu::int)";
paulson@8785
   316
paulson@8763
   317
test "#2*u = (u::int)";
paulson@8763
   318
test "(i + j + #12 + (k::int)) - #15 = y";
paulson@8763
   319
test "(i + j + #12 + (k::int)) - #5 = y";
paulson@8763
   320
paulson@8763
   321
test "y - b < (b::int)";
paulson@8763
   322
test "y - (#3*b + c) < (b::int) - #2*c";
paulson@8763
   323
paulson@8785
   324
test "(#2*x - (u*v) + y) - v*#3*u = (w::int)";
paulson@8763
   325
test "(#2*x*u*v + (u*v)*#4 + y) - v*u*#4 = (w::int)";
paulson@8763
   326
test "(#2*x*u*v + (u*v)*#4 + y) - v*u = (w::int)";
paulson@8785
   327
test "u*v - (x*u*v + (u*v)*#4 + y) = (w::int)";
paulson@8763
   328
paulson@8763
   329
test "(i + j + #12 + (k::int)) = u + #15 + y";
paulson@8763
   330
test "(i + j*#2 + #12 + (k::int)) = j + #5 + y";
paulson@8763
   331
paulson@8763
   332
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)";
paulson@8763
   333
paulson@8785
   334
test "a + -(b+c) + b = (d::int)";
paulson@8785
   335
test "a + -(b+c) - b = (d::int)";
paulson@8785
   336
paulson@8763
   337
(*negative numerals*)
paulson@8763
   338
test "(i + j + #-2 + (k::int)) - (u + #5 + y) = zz";
paulson@8763
   339
test "(i + j + #-3 + (k::int)) < u + #5 + y";
paulson@8763
   340
test "(i + j + #3 + (k::int)) < u + #-6 + y";
paulson@8763
   341
test "(i + j + #-12 + (k::int)) - #15 = y";
paulson@8763
   342
test "(i + j + #12 + (k::int)) - #-15 = y";
paulson@8763
   343
test "(i + j + #-12 + (k::int)) - #-15 = y";
paulson@8763
   344
*)
paulson@8763
   345
paulson@8763
   346
wenzelm@7707
   347
(** Constant folding for integer plus and times **)
wenzelm@7707
   348
wenzelm@7707
   349
(*We do not need
paulson@8785
   350
    structure Nat_Plus_Assoc = Assoc_Fold (Nat_Plus_Assoc_Data);
wenzelm@7707
   351
    structure Int_Plus_Assoc = Assoc_Fold (Int_Plus_Assoc_Data);
paulson@8785
   352
  because combine_numerals does the same thing*)
wenzelm@7707
   353
wenzelm@7707
   354
structure Int_Times_Assoc_Data : ASSOC_FOLD_DATA =
wenzelm@7707
   355
struct
wenzelm@7707
   356
  val ss		= HOL_ss
wenzelm@7707
   357
  val eq_reflection	= eq_reflection
wenzelm@7707
   358
  val thy    = Bin.thy
wenzelm@7707
   359
  val T	     = HOLogic.intT
wenzelm@7707
   360
  val plus   = Const ("op *", [HOLogic.intT,HOLogic.intT] ---> HOLogic.intT);
wenzelm@7707
   361
  val add_ac = zmult_ac
wenzelm@7707
   362
end;
wenzelm@7707
   363
wenzelm@7707
   364
structure Int_Times_Assoc = Assoc_Fold (Int_Times_Assoc_Data);
wenzelm@7707
   365
wenzelm@7707
   366
Addsimprocs [Int_Times_Assoc.conv];
wenzelm@7707
   367
wenzelm@7707
   368
wenzelm@7707
   369
(** The same for the naturals **)
wenzelm@7707
   370
wenzelm@7707
   371
structure Nat_Times_Assoc_Data : ASSOC_FOLD_DATA =
wenzelm@7707
   372
struct
wenzelm@7707
   373
  val ss		= HOL_ss
wenzelm@7707
   374
  val eq_reflection	= eq_reflection
wenzelm@7707
   375
  val thy    = Bin.thy
wenzelm@7707
   376
  val T	     = HOLogic.natT
wenzelm@7707
   377
  val plus   = Const ("op *", [HOLogic.natT,HOLogic.natT] ---> HOLogic.natT);
wenzelm@7707
   378
  val add_ac = mult_ac
wenzelm@7707
   379
end;
wenzelm@7707
   380
wenzelm@7707
   381
structure Nat_Times_Assoc = Assoc_Fold (Nat_Times_Assoc_Data);
wenzelm@7707
   382
paulson@8785
   383
Addsimprocs [Nat_Times_Assoc.conv];
wenzelm@7707
   384
wenzelm@7707
   385
wenzelm@7707
   386
wenzelm@7707
   387
(*** decision procedure for linear arithmetic ***)
wenzelm@7707
   388
wenzelm@7707
   389
(*---------------------------------------------------------------------------*)
wenzelm@7707
   390
(* Linear arithmetic                                                         *)
wenzelm@7707
   391
(*---------------------------------------------------------------------------*)
wenzelm@7707
   392
wenzelm@7707
   393
(*
wenzelm@7707
   394
Instantiation of the generic linear arithmetic package for int.
wenzelm@7707
   395
*)
wenzelm@7707
   396
wenzelm@7707
   397
(* Update parameters of arithmetic prover *)
wenzelm@7707
   398
let
wenzelm@7707
   399
wenzelm@7707
   400
(* reduce contradictory <= to False *)
wenzelm@7707
   401
val add_rules = simp_thms @ bin_arith_simps @ bin_rel_simps @
paulson@8785
   402
                [int_0, zadd_0, zadd_0_right, zdiff_def,
paulson@8785
   403
		 zadd_zminus_inverse, zadd_zminus_inverse2, 
paulson@8785
   404
		 zmult_0, zmult_0_right, 
paulson@8785
   405
		 zmult_1, zmult_1_right, 
paulson@8785
   406
		 zmult_minus1, zmult_minus1_right];
wenzelm@7707
   407
paulson@8785
   408
val simprocs = [Int_Times_Assoc.conv, Int_Numeral_Simprocs.combine_numerals]@
paulson@8785
   409
               Int_Numeral_Simprocs.cancel_numerals;
wenzelm@7707
   410
wenzelm@7707
   411
val add_mono_thms =
wenzelm@7707
   412
  map (fn s => prove_goal Int.thy s
wenzelm@7707
   413
                 (fn prems => [cut_facts_tac prems 1,
wenzelm@7707
   414
                      asm_simp_tac (simpset() addsimps [zadd_zle_mono]) 1]))
wenzelm@7707
   415
    ["(i <= j) & (k <= l) ==> i + k <= j + (l::int)",
wenzelm@7707
   416
     "(i  = j) & (k <= l) ==> i + k <= j + (l::int)",
wenzelm@7707
   417
     "(i <= j) & (k  = l) ==> i + k <= j + (l::int)",
wenzelm@7707
   418
     "(i  = j) & (k  = l) ==> i + k  = j + (l::int)"
wenzelm@7707
   419
    ];
wenzelm@7707
   420
wenzelm@7707
   421
in
wenzelm@7707
   422
LA_Data_Ref.add_mono_thms := !LA_Data_Ref.add_mono_thms @ add_mono_thms;
wenzelm@7707
   423
LA_Data_Ref.lessD := !LA_Data_Ref.lessD @ [add1_zle_eq RS iffD2];
wenzelm@7707
   424
LA_Data_Ref.ss_ref := !LA_Data_Ref.ss_ref addsimps add_rules
paulson@8796
   425
                      addsimprocs simprocs
paulson@8796
   426
                      addcongs [if_weak_cong];
wenzelm@7707
   427
LA_Data_Ref.discrete := !LA_Data_Ref.discrete @ [("IntDef.int",true)]
wenzelm@7707
   428
end;
wenzelm@7707
   429
wenzelm@7707
   430
let
wenzelm@7707
   431
val int_arith_simproc_pats =
wenzelm@7707
   432
  map (fn s => Thm.read_cterm (Theory.sign_of Int.thy) (s, HOLogic.boolT))
wenzelm@7707
   433
      ["(m::int) < n","(m::int) <= n", "(m::int) = n"];
wenzelm@7707
   434
wenzelm@7707
   435
val fast_int_arith_simproc = mk_simproc
wenzelm@7707
   436
  "fast_int_arith" int_arith_simproc_pats Fast_Arith.lin_arith_prover;
wenzelm@7707
   437
in
wenzelm@7707
   438
Addsimprocs [fast_int_arith_simproc]
wenzelm@7707
   439
end;
wenzelm@7707
   440
wenzelm@7707
   441
(* Some test data
wenzelm@7707
   442
Goal "!!a::int. [| a <= b; c <= d; x+y<z |] ==> a+c <= b+d";
wenzelm@7707
   443
by (fast_arith_tac 1);
wenzelm@7707
   444
Goal "!!a::int. [| a < b; c < d |] ==> a-d+ #2 <= b+(-c)";
wenzelm@7707
   445
by (fast_arith_tac 1);
wenzelm@7707
   446
Goal "!!a::int. [| a < b; c < d |] ==> a+c+ #1 < b+d";
wenzelm@7707
   447
by (fast_arith_tac 1);
wenzelm@7707
   448
Goal "!!a::int. [| a <= b; b+b <= c |] ==> a+a <= c";
wenzelm@7707
   449
by (fast_arith_tac 1);
wenzelm@7707
   450
Goal "!!a::int. [| a+b <= i+j; a<=b; i<=j |] \
wenzelm@7707
   451
\     ==> a+a <= j+j";
wenzelm@7707
   452
by (fast_arith_tac 1);
wenzelm@7707
   453
Goal "!!a::int. [| a+b < i+j; a<b; i<j |] \
wenzelm@7707
   454
\     ==> a+a - - #-1 < j+j - #3";
wenzelm@7707
   455
by (fast_arith_tac 1);
wenzelm@7707
   456
Goal "!!a::int. a+b+c <= i+j+k & a<=b & b<=c & i<=j & j<=k --> a+a+a <= k+k+k";
wenzelm@7707
   457
by (arith_tac 1);
wenzelm@7707
   458
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
wenzelm@7707
   459
\     ==> a <= l";
wenzelm@7707
   460
by (fast_arith_tac 1);
wenzelm@7707
   461
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
wenzelm@7707
   462
\     ==> a+a+a+a <= l+l+l+l";
wenzelm@7707
   463
by (fast_arith_tac 1);
wenzelm@7707
   464
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
wenzelm@7707
   465
\     ==> a+a+a+a+a <= l+l+l+l+i";
wenzelm@7707
   466
by (fast_arith_tac 1);
wenzelm@7707
   467
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
wenzelm@7707
   468
\     ==> a+a+a+a+a+a <= l+l+l+l+i+l";
wenzelm@7707
   469
by (fast_arith_tac 1);
nipkow@8257
   470
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
nipkow@8257
   471
\     ==> #6*a <= #5*l+i";
nipkow@8257
   472
by (fast_arith_tac 1);
wenzelm@7707
   473
*)
wenzelm@7707
   474
wenzelm@7707
   475
(*---------------------------------------------------------------------------*)
wenzelm@7707
   476
(* End of linear arithmetic                                                  *)
wenzelm@7707
   477
(*---------------------------------------------------------------------------*)
wenzelm@7707
   478
wenzelm@7707
   479
(** Simplification of inequalities involving numerical constants **)
wenzelm@7707
   480
wenzelm@7707
   481
Goal "(w <= z + (#1::int)) = (w<=z | w = z + (#1::int))";
wenzelm@7707
   482
by (arith_tac 1);
wenzelm@7707
   483
qed "zle_add1_eq";
wenzelm@7707
   484
wenzelm@7707
   485
Goal "(w <= z - (#1::int)) = (w<(z::int))";
wenzelm@7707
   486
by (arith_tac 1);
wenzelm@7707
   487
qed "zle_diff1_eq";
wenzelm@7707
   488
Addsimps [zle_diff1_eq];
wenzelm@7707
   489
wenzelm@7707
   490
(*2nd premise can be proved automatically if v is a literal*)
wenzelm@7707
   491
Goal "[| w <= z; #0 <= v |] ==> w <= z + (v::int)";
wenzelm@7707
   492
by (fast_arith_tac 1);
wenzelm@7707
   493
qed "zle_imp_zle_zadd";
wenzelm@7707
   494
wenzelm@7707
   495
Goal "w <= z ==> w <= z + (#1::int)";
wenzelm@7707
   496
by (fast_arith_tac 1);
wenzelm@7707
   497
qed "zle_imp_zle_zadd1";
wenzelm@7707
   498
wenzelm@7707
   499
(*2nd premise can be proved automatically if v is a literal*)
wenzelm@7707
   500
Goal "[| w < z; #0 <= v |] ==> w < z + (v::int)";
wenzelm@7707
   501
by (fast_arith_tac 1);
wenzelm@7707
   502
qed "zless_imp_zless_zadd";
wenzelm@7707
   503
wenzelm@7707
   504
Goal "w < z ==> w < z + (#1::int)";
wenzelm@7707
   505
by (fast_arith_tac 1);
wenzelm@7707
   506
qed "zless_imp_zless_zadd1";
wenzelm@7707
   507
wenzelm@7707
   508
Goal "(w < z + #1) = (w<=(z::int))";
wenzelm@7707
   509
by (arith_tac 1);
wenzelm@7707
   510
qed "zle_add1_eq_le";
wenzelm@7707
   511
Addsimps [zle_add1_eq_le];
wenzelm@7707
   512
wenzelm@7707
   513
Goal "(z = z + w) = (w = (#0::int))";
wenzelm@7707
   514
by (arith_tac 1);
wenzelm@7707
   515
qed "zadd_left_cancel0";
wenzelm@7707
   516
Addsimps [zadd_left_cancel0];
wenzelm@7707
   517
wenzelm@7707
   518
(*LOOPS as a simprule!*)
wenzelm@7707
   519
Goal "[| w + v < z; #0 <= v |] ==> w < (z::int)";
wenzelm@7707
   520
by (fast_arith_tac 1);
wenzelm@7707
   521
qed "zless_zadd_imp_zless";
wenzelm@7707
   522
wenzelm@7707
   523
(*LOOPS as a simprule!  Analogous to Suc_lessD*)
wenzelm@7707
   524
Goal "w + #1 < z ==> w < (z::int)";
wenzelm@7707
   525
by (fast_arith_tac 1);
wenzelm@7707
   526
qed "zless_zadd1_imp_zless";
wenzelm@7707
   527
wenzelm@7707
   528
Goal "w + #-1 = w - (#1::int)";
wenzelm@7707
   529
by (Simp_tac 1);
wenzelm@7707
   530
qed "zplus_minus1_conv";
wenzelm@7707
   531
wenzelm@7707
   532
wenzelm@7707
   533
(* nat *)
wenzelm@7707
   534
wenzelm@7707
   535
Goal "#0 <= z ==> int (nat z) = z"; 
wenzelm@7707
   536
by (asm_full_simp_tac
wenzelm@7707
   537
    (simpset() addsimps [neg_eq_less_0, zle_def, not_neg_nat]) 1); 
wenzelm@7707
   538
qed "nat_0_le"; 
wenzelm@7707
   539
wenzelm@7707
   540
Goal "z <= #0 ==> nat z = 0"; 
wenzelm@7707
   541
by (case_tac "z = #0" 1);
wenzelm@7707
   542
by (asm_simp_tac (simpset() addsimps [nat_le_int0]) 1); 
wenzelm@7707
   543
by (asm_full_simp_tac 
wenzelm@7707
   544
    (simpset() addsimps [neg_eq_less_0, neg_nat, linorder_neq_iff]) 1);
wenzelm@7707
   545
qed "nat_le_0"; 
wenzelm@7707
   546
wenzelm@7707
   547
Addsimps [nat_0_le, nat_le_0];
wenzelm@7707
   548
wenzelm@7707
   549
val [major,minor] = Goal "[| #0 <= z;  !!m. z = int m ==> P |] ==> P"; 
wenzelm@7707
   550
by (rtac (major RS nat_0_le RS sym RS minor) 1);
wenzelm@7707
   551
qed "nonneg_eq_int"; 
wenzelm@7707
   552
wenzelm@7707
   553
Goal "#0 <= w ==> (nat w = m) = (w = int m)";
wenzelm@7707
   554
by Auto_tac;
wenzelm@7707
   555
qed "nat_eq_iff";
wenzelm@7707
   556
paulson@8796
   557
Goal "#0 <= w ==> (m = nat w) = (w = int m)";
paulson@8796
   558
by Auto_tac;
paulson@8796
   559
qed "nat_eq_iff2";
paulson@8796
   560
wenzelm@7707
   561
Goal "#0 <= w ==> (nat w < m) = (w < int m)";
wenzelm@7707
   562
by (rtac iffI 1);
wenzelm@7707
   563
by (asm_full_simp_tac 
wenzelm@7707
   564
    (simpset() delsimps [zless_int] addsimps [zless_int RS sym]) 2);
wenzelm@7707
   565
by (etac (nat_0_le RS subst) 1);
wenzelm@7707
   566
by (Simp_tac 1);
wenzelm@7707
   567
qed "nat_less_iff";
wenzelm@7707
   568
wenzelm@7707
   569
wenzelm@7707
   570
(*Users don't want to see (int 0), int(Suc 0) or w + - z*)
wenzelm@7707
   571
Addsimps [int_0, int_Suc, symmetric zdiff_def];
wenzelm@7707
   572
wenzelm@7707
   573
Goal "nat #0 = 0";
wenzelm@7707
   574
by (simp_tac (simpset() addsimps [nat_eq_iff]) 1);
wenzelm@7707
   575
qed "nat_0";
wenzelm@7707
   576
wenzelm@7707
   577
Goal "nat #1 = 1";
wenzelm@7707
   578
by (simp_tac (simpset() addsimps [nat_eq_iff]) 1);
wenzelm@7707
   579
qed "nat_1";
wenzelm@7707
   580
wenzelm@7707
   581
Goal "nat #2 = 2";
wenzelm@7707
   582
by (simp_tac (simpset() addsimps [nat_eq_iff]) 1);
wenzelm@7707
   583
qed "nat_2";
wenzelm@7707
   584
wenzelm@7707
   585
Goal "#0 <= w ==> (nat w < nat z) = (w<z)";
wenzelm@7707
   586
by (case_tac "neg z" 1);
wenzelm@7707
   587
by (auto_tac (claset(), simpset() addsimps [nat_less_iff]));
wenzelm@7707
   588
by (auto_tac (claset() addIs [zless_trans], 
wenzelm@7707
   589
	      simpset() addsimps [neg_eq_less_0, zle_def]));
wenzelm@7707
   590
qed "nat_less_eq_zless";
wenzelm@7707
   591
wenzelm@7707
   592
Goal "#0 < w | #0 <= z ==> (nat w <= nat z) = (w<=z)";
wenzelm@7707
   593
by (auto_tac (claset(), 
wenzelm@7707
   594
	      simpset() addsimps [linorder_not_less RS sym, 
wenzelm@7707
   595
				  zless_nat_conj]));
wenzelm@7707
   596
qed "nat_le_eq_zle";
wenzelm@7707
   597
wenzelm@7707
   598
(*Analogous to zadd_int, but more easily provable using the arithmetic in Bin*)
wenzelm@7707
   599
Goal "n<=m --> int m - int n = int (m-n)";
wenzelm@7707
   600
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
wenzelm@7707
   601
by Auto_tac;
wenzelm@7707
   602
qed_spec_mp "zdiff_int";
wenzelm@7707
   603
wenzelm@7707
   604
wenzelm@7707
   605
(** Products of signs **)
wenzelm@7707
   606
wenzelm@7707
   607
Goal "(m::int) < #0 ==> (#0 < m*n) = (n < #0)";
wenzelm@7707
   608
by Auto_tac;
wenzelm@7707
   609
by (force_tac (claset() addDs [zmult_zless_mono1_neg], simpset()) 2);
wenzelm@7707
   610
by (eres_inst_tac [("P", "#0 < m * n")] rev_mp 1);
wenzelm@7707
   611
by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
wenzelm@7707
   612
by (force_tac (claset() addDs [inst "k" "m" zmult_zless_mono1_neg], 
wenzelm@7707
   613
	       simpset()addsimps [order_le_less, zmult_commute]) 1);
wenzelm@7707
   614
qed "neg_imp_zmult_pos_iff";
wenzelm@7707
   615
wenzelm@7707
   616
Goal "(m::int) < #0 ==> (m*n < #0) = (#0 < n)";
wenzelm@7707
   617
by Auto_tac;
wenzelm@7707
   618
by (force_tac (claset() addDs [zmult_zless_mono1], simpset()) 2);
wenzelm@7707
   619
by (eres_inst_tac [("P", "m * n < #0")] rev_mp 1);
wenzelm@7707
   620
by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
wenzelm@7707
   621
by (force_tac (claset() addDs [zmult_zless_mono1_neg], 
wenzelm@7707
   622
	       simpset() addsimps [order_le_less]) 1);
wenzelm@7707
   623
qed "neg_imp_zmult_neg_iff";
wenzelm@7707
   624
wenzelm@7707
   625
Goal "#0 < (m::int) ==> (m*n < #0) = (n < #0)";
wenzelm@7707
   626
by Auto_tac;
wenzelm@7707
   627
by (force_tac (claset() addDs [zmult_zless_mono1_neg], simpset()) 2);
wenzelm@7707
   628
by (eres_inst_tac [("P", "m * n < #0")] rev_mp 1);
wenzelm@7707
   629
by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
wenzelm@7707
   630
by (force_tac (claset() addDs [zmult_zless_mono1], 
wenzelm@7707
   631
	       simpset() addsimps [order_le_less]) 1);
wenzelm@7707
   632
qed "pos_imp_zmult_neg_iff";
wenzelm@7707
   633
wenzelm@7707
   634
Goal "#0 < (m::int) ==> (#0 < m*n) = (#0 < n)";
wenzelm@7707
   635
by Auto_tac;
wenzelm@7707
   636
by (force_tac (claset() addDs [zmult_zless_mono1], simpset()) 2);
wenzelm@7707
   637
by (eres_inst_tac [("P", "#0 < m * n")] rev_mp 1);
wenzelm@7707
   638
by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
wenzelm@7707
   639
by (force_tac (claset() addDs [inst "k" "m" zmult_zless_mono1], 
wenzelm@7707
   640
	       simpset() addsimps [order_le_less, zmult_commute]) 1);
wenzelm@7707
   641
qed "pos_imp_zmult_pos_iff";
wenzelm@7707
   642
wenzelm@7707
   643
(** <= versions of the theorems above **)
wenzelm@7707
   644
wenzelm@7707
   645
Goal "(m::int) < #0 ==> (m*n <= #0) = (#0 <= n)";
wenzelm@7707
   646
by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
wenzelm@7707
   647
				      neg_imp_zmult_pos_iff]) 1);
wenzelm@7707
   648
qed "neg_imp_zmult_nonpos_iff";
wenzelm@7707
   649
wenzelm@7707
   650
Goal "(m::int) < #0 ==> (#0 <= m*n) = (n <= #0)";
wenzelm@7707
   651
by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
wenzelm@7707
   652
				      neg_imp_zmult_neg_iff]) 1);
wenzelm@7707
   653
qed "neg_imp_zmult_nonneg_iff";
wenzelm@7707
   654
wenzelm@7707
   655
Goal "#0 < (m::int) ==> (m*n <= #0) = (n <= #0)";
wenzelm@7707
   656
by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
wenzelm@7707
   657
				      pos_imp_zmult_pos_iff]) 1);
wenzelm@7707
   658
qed "pos_imp_zmult_nonpos_iff";
wenzelm@7707
   659
wenzelm@7707
   660
Goal "#0 < (m::int) ==> (#0 <= m*n) = (#0 <= n)";
wenzelm@7707
   661
by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
wenzelm@7707
   662
				      pos_imp_zmult_neg_iff]) 1);
wenzelm@7707
   663
qed "pos_imp_zmult_nonneg_iff";