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