src/HOL/Integ/NatSimprocs.ML
author paulson
Tue May 02 18:42:48 2000 +0200 (2000-05-02)
changeset 8776 60821dbc9f18
parent 8766 1ef6e77e12ee
child 8785 00cff9d083df
permissions -rw-r--r--
now with combine_numerals
paulson@8759
     1
(*  Title:      HOL/NatSimprocs.ML
paulson@8759
     2
    ID:         $Id$
paulson@8759
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@8759
     4
    Copyright   2000  University of Cambridge
paulson@8759
     5
paulson@8759
     6
Simprocs for nat numerals
paulson@8759
     7
*)
paulson@8759
     8
paulson@8759
     9
Goal "number_of v + (number_of v' + (k::nat)) = \
paulson@8759
    10
\        (if neg (number_of v) then number_of v' + k \
paulson@8759
    11
\         else if neg (number_of v') then number_of v + k \
paulson@8759
    12
\         else number_of (bin_add v v') + k)";
paulson@8759
    13
by (Simp_tac 1);
paulson@8766
    14
qed "nat_number_of_add_left";
paulson@8759
    15
paulson@8759
    16
paulson@8776
    17
(** For combine_numerals **)
paulson@8776
    18
paulson@8776
    19
Goal "i*u + (j*u + k) = (i+j)*u + (k::nat)";
paulson@8776
    20
by (asm_simp_tac (simpset() addsimps [add_mult_distrib]) 1);
paulson@8776
    21
qed "left_add_mult_distrib";
paulson@8776
    22
paulson@8776
    23
paulson@8759
    24
(** For cancel_numerals **)
paulson@8759
    25
paulson@8759
    26
Goal "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)";
paulson@8759
    27
by (asm_simp_tac (simpset() addsplits [nat_diff_split'] 
paulson@8759
    28
		            addsimps [add_mult_distrib]) 1);
paulson@8766
    29
qed "nat_diff_add_eq1";
paulson@8759
    30
paulson@8759
    31
Goal "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))";
paulson@8759
    32
by (asm_simp_tac (simpset() addsplits [nat_diff_split'] 
paulson@8759
    33
		            addsimps [add_mult_distrib]) 1);
paulson@8766
    34
qed "nat_diff_add_eq2";
paulson@8759
    35
paulson@8759
    36
Goal "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)";
paulson@8759
    37
by (auto_tac (claset(), simpset() addsplits [nat_diff_split'] 
paulson@8759
    38
                                  addsimps [add_mult_distrib]));
paulson@8766
    39
qed "nat_eq_add_iff1";
paulson@8759
    40
paulson@8759
    41
Goal "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)";
paulson@8759
    42
by (auto_tac (claset(), simpset() addsplits [nat_diff_split'] 
paulson@8759
    43
                                  addsimps [add_mult_distrib]));
paulson@8766
    44
qed "nat_eq_add_iff2";
paulson@8759
    45
paulson@8759
    46
Goal "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)";
paulson@8759
    47
by (auto_tac (claset(), simpset() addsplits [nat_diff_split'] 
paulson@8759
    48
                                  addsimps [add_mult_distrib]));
paulson@8766
    49
qed "nat_less_add_iff1";
paulson@8759
    50
paulson@8759
    51
Goal "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)";
paulson@8759
    52
by (auto_tac (claset(), simpset() addsplits [nat_diff_split'] 
paulson@8759
    53
                                  addsimps [add_mult_distrib]));
paulson@8766
    54
qed "nat_less_add_iff2";
paulson@8759
    55
paulson@8759
    56
Goal "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)";
paulson@8759
    57
by (auto_tac (claset(), simpset() addsplits [nat_diff_split'] 
paulson@8759
    58
                                  addsimps [add_mult_distrib]));
paulson@8766
    59
qed "nat_le_add_iff1";
paulson@8759
    60
paulson@8759
    61
Goal "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)";
paulson@8759
    62
by (auto_tac (claset(), simpset() addsplits [nat_diff_split'] 
paulson@8759
    63
                                  addsimps [add_mult_distrib]));
paulson@8766
    64
qed "nat_le_add_iff2";
paulson@8759
    65
paulson@8776
    66
paulson@8759
    67
structure Nat_Numeral_Simprocs =
paulson@8759
    68
struct
paulson@8759
    69
paulson@8766
    70
(*Utilities*)
paulson@8759
    71
paulson@8766
    72
fun mk_numeral n = HOLogic.number_of_const HOLogic.natT $ 
paulson@8766
    73
                   NumeralSyntax.mk_bin n;
paulson@8759
    74
paulson@8759
    75
(*Decodes a unary or binary numeral to a NATURAL NUMBER*)
paulson@8759
    76
fun dest_numeral (Const ("0", _)) = 0
paulson@8759
    77
  | dest_numeral (Const ("Suc", _) $ t) = 1 + dest_numeral t
paulson@8759
    78
  | dest_numeral (Const("Numeral.number_of", _) $ w) = 
paulson@8776
    79
        (BasisLibrary.Int.max (0, NumeralSyntax.dest_bin w)
paulson@8776
    80
	 handle Match => raise TERM("dest_numeral:1", [w]))
paulson@8776
    81
  | dest_numeral t = raise TERM("dest_numeral:2", [t]);
paulson@8759
    82
paulson@8759
    83
fun find_first_numeral past (t::terms) =
paulson@8759
    84
	((dest_numeral t, t, rev past @ terms)
paulson@8759
    85
	 handle TERM _ => find_first_numeral (t::past) terms)
paulson@8759
    86
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
paulson@8759
    87
paulson@8759
    88
val zero = mk_numeral 0;
paulson@8759
    89
val mk_plus = HOLogic.mk_binop "op +";
paulson@8759
    90
paulson@8766
    91
(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
paulson@8759
    92
fun mk_sum []        = zero
paulson@8766
    93
  | mk_sum [t,u]     = mk_plus (t, u)
paulson@8759
    94
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
paulson@8759
    95
paulson@8776
    96
(*this version ALWAYS includes a trailing zero*)
paulson@8776
    97
fun long_mk_sum []        = zero
paulson@8776
    98
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
paulson@8776
    99
paulson@8759
   100
val dest_plus = HOLogic.dest_bin "op +" HOLogic.natT;
paulson@8759
   101
paulson@8759
   102
(*extract the outer Sucs from a term and convert them to a binary numeral*)
paulson@8759
   103
fun dest_Sucs (k, Const ("Suc", _) $ t) = dest_Sucs (k+1, t)
paulson@8759
   104
  | dest_Sucs (0, t) = t
paulson@8759
   105
  | dest_Sucs (k, t) = mk_plus (mk_numeral k, t);
paulson@8759
   106
paulson@8759
   107
fun dest_sum t =
paulson@8759
   108
      let val (t,u) = dest_plus t 
paulson@8759
   109
      in  dest_sum t @ dest_sum u  end
paulson@8759
   110
      handle TERM _ => [t];
paulson@8759
   111
paulson@8759
   112
fun dest_Sucs_sum t = dest_sum (dest_Sucs (0,t));
paulson@8759
   113
paulson@8776
   114
val subst_tac = Int_Numeral_Simprocs.subst_tac;
paulson@8759
   115
paulson@8776
   116
val prove_conv = Int_Numeral_Simprocs.prove_conv;
paulson@8759
   117
paulson@8766
   118
val bin_simps = [add_nat_number_of, nat_number_of_add_left,
paulson@8766
   119
		 diff_nat_number_of, le_nat_number_of_eq_not_less, 
paulson@8766
   120
		 less_nat_number_of, Let_number_of, nat_number_of] @ 
paulson@8766
   121
                bin_arith_simps @ bin_rel_simps;
paulson@8759
   122
paulson@8759
   123
fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
paulson@8759
   124
fun prep_pat s = Thm.read_cterm (Theory.sign_of Arith.thy) (s, HOLogic.termT);
paulson@8759
   125
val prep_pats = map prep_pat;
paulson@8759
   126
paulson@8759
   127
paulson@8776
   128
(*** CancelNumerals simprocs ***)
paulson@8759
   129
paulson@8759
   130
val one = mk_numeral 1;
paulson@8759
   131
val mk_times = HOLogic.mk_binop "op *";
paulson@8759
   132
paulson@8759
   133
fun mk_prod [] = one
paulson@8759
   134
  | mk_prod [t] = t
paulson@8759
   135
  | mk_prod (t :: ts) = if t = one then mk_prod ts
paulson@8759
   136
                        else mk_times (t, mk_prod ts);
paulson@8759
   137
paulson@8759
   138
val dest_times = HOLogic.dest_bin "op *" HOLogic.natT;
paulson@8759
   139
paulson@8759
   140
fun dest_prod t =
paulson@8759
   141
      let val (t,u) = dest_times t 
paulson@8759
   142
      in  dest_prod t @ dest_prod u  end
paulson@8759
   143
      handle TERM _ => [t];
paulson@8759
   144
paulson@8759
   145
(*DON'T do the obvious simplifications; that would create special cases*) 
paulson@8759
   146
fun mk_coeff (k, ts) = mk_times (mk_numeral k, ts);
paulson@8759
   147
paulson@8759
   148
(*Express t as a product of (possibly) a numeral with other sorted terms*)
paulson@8759
   149
fun dest_coeff t =
paulson@8759
   150
    let val ts = sort Term.term_ord (dest_prod t)
paulson@8759
   151
	val (n, _, ts') = find_first_numeral [] ts
paulson@8759
   152
                          handle TERM _ => (1, one, ts)
paulson@8759
   153
    in (n, mk_prod ts') end;
paulson@8759
   154
paulson@8759
   155
(*Find first coefficient-term THAT MATCHES u*)
paulson@8759
   156
fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) 
paulson@8759
   157
  | find_first_coeff past u (t::terms) =
paulson@8759
   158
	let val (n,u') = dest_coeff t
paulson@8759
   159
	in  if u aconv u' then (n, rev past @ terms)
paulson@8759
   160
			  else find_first_coeff (t::past) u terms
paulson@8759
   161
	end
paulson@8759
   162
	handle TERM _ => find_first_coeff (t::past) u terms;
paulson@8759
   163
paulson@8759
   164
paulson@8759
   165
(*Simplify #1*n and n*#1 to n*)
paulson@8759
   166
val add_0s = map (rename_numerals NatBin.thy) [add_0, add_0_right];
paulson@8759
   167
val mult_1s = map (rename_numerals NatBin.thy) [mult_1, mult_1_right];
paulson@8759
   168
paulson@8759
   169
structure CancelNumeralsCommon =
paulson@8759
   170
  struct
paulson@8759
   171
  val mk_sum    	= mk_sum
paulson@8759
   172
  val dest_sum		= dest_Sucs_sum
paulson@8759
   173
  val mk_coeff		= mk_coeff
paulson@8759
   174
  val dest_coeff	= dest_coeff
paulson@8759
   175
  val find_first_coeff	= find_first_coeff []
paulson@8776
   176
  val subst_tac          = subst_tac
paulson@8759
   177
  val norm_tac = ALLGOALS
paulson@8759
   178
                   (simp_tac (HOL_ss addsimps add_0s@mult_1s@bin_simps@
paulson@8776
   179
                                       [add_0, Suc_eq_add_numeral_1]@add_ac))
paulson@8776
   180
                 THEN ALLGOALS (simp_tac (HOL_ss addsimps mult_ac@add_ac))
paulson@8766
   181
  val numeral_simp_tac	= ALLGOALS
paulson@8766
   182
                (simp_tac (HOL_ss addsimps [numeral_0_eq_0 RS sym]@add_0s@bin_simps))
paulson@8759
   183
  end;
paulson@8759
   184
paulson@8759
   185
paulson@8759
   186
structure EqCancelNumerals = CancelNumeralsFun
paulson@8759
   187
 (open CancelNumeralsCommon
paulson@8776
   188
  val prove_conv = prove_conv "nateq_cancel_numerals"
paulson@8759
   189
  val mk_bal   = HOLogic.mk_eq
paulson@8759
   190
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT
paulson@8776
   191
  val bal_add1 = nat_eq_add_iff1 RS trans
paulson@8776
   192
  val bal_add2 = nat_eq_add_iff2 RS trans
paulson@8759
   193
);
paulson@8759
   194
paulson@8759
   195
structure LessCancelNumerals = CancelNumeralsFun
paulson@8759
   196
 (open CancelNumeralsCommon
paulson@8776
   197
  val prove_conv = prove_conv "natless_cancel_numerals"
paulson@8759
   198
  val mk_bal   = HOLogic.mk_binrel "op <"
paulson@8759
   199
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT
paulson@8776
   200
  val bal_add1 = nat_less_add_iff1 RS trans
paulson@8776
   201
  val bal_add2 = nat_less_add_iff2 RS trans
paulson@8759
   202
);
paulson@8759
   203
paulson@8759
   204
structure LeCancelNumerals = CancelNumeralsFun
paulson@8759
   205
 (open CancelNumeralsCommon
paulson@8776
   206
  val prove_conv = prove_conv "natle_cancel_numerals"
paulson@8759
   207
  val mk_bal   = HOLogic.mk_binrel "op <="
paulson@8759
   208
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT
paulson@8776
   209
  val bal_add1 = nat_le_add_iff1 RS trans
paulson@8776
   210
  val bal_add2 = nat_le_add_iff2 RS trans
paulson@8759
   211
);
paulson@8759
   212
paulson@8759
   213
structure DiffCancelNumerals = CancelNumeralsFun
paulson@8759
   214
 (open CancelNumeralsCommon
paulson@8776
   215
  val prove_conv = prove_conv "natdiff_cancel_numerals"
paulson@8759
   216
  val mk_bal   = HOLogic.mk_binop "op -"
paulson@8759
   217
  val dest_bal = HOLogic.dest_bin "op -" HOLogic.natT
paulson@8776
   218
  val bal_add1 = nat_diff_add_eq1 RS trans
paulson@8776
   219
  val bal_add2 = nat_diff_add_eq2 RS trans
paulson@8759
   220
);
paulson@8759
   221
paulson@8759
   222
paulson@8759
   223
val cancel_numerals = 
paulson@8759
   224
  map prep_simproc
paulson@8759
   225
   [("nateq_cancel_numerals",
paulson@8759
   226
     prep_pats ["(l::nat) + m = n", "(l::nat) = m + n", 
paulson@8759
   227
		"(l::nat) * m = n", "(l::nat) = m * n", 
paulson@8759
   228
		"Suc m = n", "m = Suc n"], 
paulson@8759
   229
     EqCancelNumerals.proc),
paulson@8759
   230
    ("natless_cancel_numerals", 
paulson@8759
   231
     prep_pats ["(l::nat) + m < n", "(l::nat) < m + n", 
paulson@8759
   232
		"(l::nat) * m < n", "(l::nat) < m * n", 
paulson@8759
   233
		"Suc m < n", "m < Suc n"], 
paulson@8759
   234
     LessCancelNumerals.proc),
paulson@8759
   235
    ("natle_cancel_numerals", 
paulson@8759
   236
     prep_pats ["(l::nat) + m <= n", "(l::nat) <= m + n", 
paulson@8759
   237
		"(l::nat) * m <= n", "(l::nat) <= m * n", 
paulson@8759
   238
		"Suc m <= n", "m <= Suc n"], 
paulson@8759
   239
     LeCancelNumerals.proc),
paulson@8759
   240
    ("natdiff_cancel_numerals", 
paulson@8759
   241
     prep_pats ["((l::nat) + m) - n", "(l::nat) - (m + n)", 
paulson@8759
   242
		"(l::nat) * m - n", "(l::nat) - m * n", 
paulson@8759
   243
		"Suc m - n", "m - Suc n"], 
paulson@8759
   244
     DiffCancelNumerals.proc)];
paulson@8759
   245
paulson@8759
   246
paulson@8776
   247
structure CombineNumeralsData =
paulson@8776
   248
  struct
paulson@8776
   249
  val mk_sum    	= long_mk_sum    (*to work for e.g. #2*x + #3*x *)
paulson@8776
   250
  val dest_sum		= dest_Sucs_sum
paulson@8776
   251
  val mk_coeff		= mk_coeff
paulson@8776
   252
  val dest_coeff	= dest_coeff
paulson@8776
   253
  val left_distrib	= left_add_mult_distrib RS trans
paulson@8776
   254
  val prove_conv	= prove_conv "nat_combine_numerals"
paulson@8776
   255
  val subst_tac          = subst_tac
paulson@8776
   256
  val norm_tac = ALLGOALS
paulson@8776
   257
                   (simp_tac (HOL_ss addsimps add_0s@mult_1s@bin_simps@
paulson@8776
   258
                                       [add_0, Suc_eq_add_numeral_1]@add_ac))
paulson@8776
   259
                 THEN ALLGOALS (simp_tac (HOL_ss addsimps mult_ac@add_ac))
paulson@8776
   260
  val numeral_simp_tac	= ALLGOALS
paulson@8776
   261
                (simp_tac (HOL_ss addsimps [numeral_0_eq_0 RS sym]@add_0s@bin_simps))
paulson@8776
   262
  end;
paulson@8776
   263
paulson@8776
   264
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
paulson@8776
   265
  
paulson@8776
   266
val combine_numerals = 
paulson@8776
   267
    prep_simproc ("nat_combine_numerals",
paulson@8776
   268
		  prep_pats ["(i::nat) + (j+k)", "(i::nat) + (j*k)", 
paulson@8776
   269
			     "(j+k) + (i::nat)", "(j*k) + (i::nat)", 
paulson@8776
   270
			     "Suc (i + j)"],
paulson@8776
   271
		  CombineNumerals.proc);
paulson@8776
   272
paulson@8759
   273
end;
paulson@8759
   274
paulson@8759
   275
paulson@8759
   276
Addsimprocs Nat_Numeral_Simprocs.cancel_numerals;
paulson@8776
   277
Addsimprocs [Nat_Numeral_Simprocs.combine_numerals];
paulson@8759
   278
paulson@8759
   279
(*examples:
paulson@8759
   280
print_depth 22;
paulson@8759
   281
set proof_timing;
paulson@8759
   282
set trace_simp;
paulson@8759
   283
fun test s = (Goal s; by (Simp_tac 1)); 
paulson@8759
   284
paulson@8759
   285
(*cancel_numerals*)
paulson@8759
   286
test "(#2*length xs < #2*length xs + j)";
paulson@8759
   287
test "(#2*length xs < length xs * #2 + j)";
paulson@8759
   288
test "#2*u = (u::nat)";
paulson@8759
   289
test "#2*u = Suc (u)";
paulson@8759
   290
test "(i + j + #12 + (k::nat)) - #15 = y";
paulson@8759
   291
test "(i + j + #12 + (k::nat)) - #5 = y";
paulson@8759
   292
test "Suc u - #2 = y";
paulson@8759
   293
test "Suc (Suc (Suc u)) - #2 = y";
paulson@8759
   294
(*Unary*)
paulson@8759
   295
test "(i + j + #2 + (k::nat)) - 1 = y";
paulson@8759
   296
test "(i + j + #1 + (k::nat)) - 2 = y";
paulson@8759
   297
paulson@8759
   298
test "(#2*x + (u*v) + y) - v*#3*u = (w::nat)";
paulson@8759
   299
test "(#2*x*u*v + (u*v)*#4 + y) - v*u*#4 = (w::nat)";
paulson@8759
   300
test "(#2*x*u*v + (u*v)*#4 + y) - v*u = (w::nat)";
paulson@8759
   301
test "Suc (Suc (#2*x*u*v + u*#4 + y)) - u = w";
paulson@8759
   302
test "Suc ((u*v)*#4) - v*#3*u = w";
paulson@8759
   303
test "Suc (Suc ((u*v)*#3)) - v*#3*u = w";
paulson@8759
   304
paulson@8759
   305
test "(i + j + #12 + (k::nat)) = u + #15 + y";
paulson@8759
   306
test "(i + j + #32 + (k::nat)) - (u + #15 + y) = zz";
paulson@8759
   307
test "(i + j + #12 + (k::nat)) = u + #5 + y";
paulson@8759
   308
(*Suc*)
paulson@8759
   309
test "(i + j + #12 + k) = Suc (u + y)";
paulson@8759
   310
test "Suc (Suc (Suc (Suc (Suc (u + y))))) <= ((i + j) + #41 + k)";
paulson@8759
   311
test "(i + j + #5 + k) < Suc (Suc (Suc (Suc (Suc (u + y)))))";
paulson@8759
   312
test "Suc (Suc (Suc (Suc (Suc (u + y))))) - #5 = v";
paulson@8759
   313
test "(i + j + #5 + k) = Suc (Suc (Suc (Suc (Suc (Suc (Suc (u + y)))))))";
paulson@8759
   314
test "#2*y + #3*z + #2*u = Suc (u)";
paulson@8759
   315
test "#2*y + #3*z + #6*w + #2*y + #3*z + #2*u = Suc (u)";
paulson@8759
   316
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::nat)";
paulson@8759
   317
test "#6 + #2*y + #3*z + #4*u = Suc (vv + #2*u + z)";
paulson@8776
   318
test "(#2*n*m) < (#3*(m*n)) + (u::nat)";
paulson@8759
   319
paulson@8759
   320
(*negative numerals: FAIL*)
paulson@8759
   321
test "(i + j + #-23 + (k::nat)) < u + #15 + y";
paulson@8759
   322
test "(i + j + #3 + (k::nat)) < u + #-15 + y";
paulson@8759
   323
test "(i + j + #-12 + (k::nat)) - #15 = y";
paulson@8759
   324
test "(i + j + #12 + (k::nat)) - #-15 = y";
paulson@8759
   325
test "(i + j + #-12 + (k::nat)) - #-15 = y";
paulson@8759
   326
paulson@8776
   327
(*combine_numerals*)
paulson@8776
   328
test "k + #3*k = (u::nat)";
paulson@8776
   329
test "Suc (i + #3) = u";
paulson@8759
   330
test "Suc (i + j + #3 + k) = u";
paulson@8776
   331
test "k + j + #3*k + j = (u::nat)";
paulson@8776
   332
test "Suc (j*i + i + k + #5 + #3*k + i*j*#4) = (u::nat)";
paulson@8776
   333
test "(#2*n*m) + (#3*(m*n)) = (u::nat)";
paulson@8776
   334
(*negative numerals: FAIL*)
paulson@8759
   335
test "Suc (i + j + #-3 + k) = u";
paulson@8759
   336
*)
paulson@8759
   337
paulson@8759
   338
paulson@8759
   339
(*** Prepare linear arithmetic for nat numerals ***)
paulson@8759
   340
paulson@8759
   341
let
paulson@8759
   342
paulson@8759
   343
(* reduce contradictory <= to False *)
paulson@8759
   344
val add_rules =
paulson@8759
   345
  [add_nat_number_of, diff_nat_number_of, mult_nat_number_of,
paulson@8759
   346
   eq_nat_number_of, less_nat_number_of, le_nat_number_of_eq_not_less,
paulson@8759
   347
   le_Suc_number_of,le_number_of_Suc,
paulson@8759
   348
   less_Suc_number_of,less_number_of_Suc,
paulson@8759
   349
   Suc_eq_number_of,eq_number_of_Suc,
paulson@8759
   350
   eq_number_of_0, eq_0_number_of, less_0_number_of,
paulson@8759
   351
   nat_number_of, Let_number_of, if_True, if_False];
paulson@8759
   352
paulson@8776
   353
val simprocs = [Nat_Times_Assoc.conv,
paulson@8776
   354
		Nat_Numeral_Simprocs.combine_numerals]@ 
paulson@8776
   355
		Nat_Numeral_Simprocs.cancel_numerals;
paulson@8759
   356
paulson@8759
   357
in
paulson@8776
   358
LA_Data_Ref.ss_ref := !LA_Data_Ref.ss_ref addsimps add_rules 
paulson@8776
   359
                                          addsimps basic_renamed_arith_simps
paulson@8776
   360
                                          addsimprocs simprocs
paulson@8759
   361
end;
paulson@8759
   362
paulson@8776
   363
Delsimprocs [Nat_Plus_Assoc.conv];  (*combine_numerals makes it redundant*)
paulson@8776
   364
paulson@8759
   365
paulson@8759
   366
paulson@8759
   367
(** For simplifying  Suc m - #n **)
paulson@8759
   368
paulson@8759
   369
Goal "#0 < n ==> Suc m - n = m - (n - #1)";
paulson@8759
   370
by (asm_full_simp_tac (numeral_ss addsplits [nat_diff_split']) 1);
paulson@8759
   371
qed "Suc_diff_eq_diff_pred";
paulson@8759
   372
paulson@8759
   373
(*Now just instantiating n to (number_of v) does the right simplification,
paulson@8759
   374
  but with some redundant inequality tests.*)
paulson@8759
   375
paulson@8759
   376
Goal "neg (number_of (bin_pred v)) = (number_of v = 0)";
paulson@8759
   377
by (subgoal_tac "neg (number_of (bin_pred v)) = (number_of v < 1)" 1);
paulson@8759
   378
by (Asm_simp_tac 1);
paulson@8759
   379
by (stac less_number_of_Suc 1);
paulson@8759
   380
by (Simp_tac 1);
paulson@8759
   381
qed "neg_number_of_bin_pred_iff_0";
paulson@8759
   382
paulson@8759
   383
Goal "neg (number_of (bin_minus v)) ==> \
paulson@8759
   384
\     Suc m - (number_of v) = m - (number_of (bin_pred v))";
paulson@8759
   385
by (stac Suc_diff_eq_diff_pred 1);
paulson@8759
   386
by (Simp_tac 1);
paulson@8759
   387
by (Simp_tac 1);
paulson@8759
   388
by (asm_full_simp_tac
paulson@8759
   389
    (simpset_of Int.thy addsimps [less_0_number_of RS sym, 
paulson@8759
   390
				  neg_number_of_bin_pred_iff_0]) 1);
paulson@8759
   391
qed "Suc_diff_number_of";
paulson@8759
   392
paulson@8759
   393
(* now redundant because of the inverse_fold simproc
paulson@8759
   394
    Addsimps [Suc_diff_number_of]; *)
paulson@8759
   395
paulson@8759
   396
paulson@8759
   397
(** For simplifying  #m - Suc n **)
paulson@8759
   398
paulson@8759
   399
Goal "m - Suc n = (m - #1) - n";
paulson@8759
   400
by (simp_tac (numeral_ss addsplits [nat_diff_split']) 1);
paulson@8759
   401
qed "diff_Suc_eq_diff_pred";
paulson@8759
   402
paulson@8759
   403
Addsimps [inst "m" "number_of ?v" diff_Suc_eq_diff_pred];
paulson@8776
   404
paulson@8776
   405
paulson@8776
   406
(** Evens and Odds, for Mutilated Chess Board **)
paulson@8776
   407
paulson@8776
   408
(*Case analysis on b<#2*)
paulson@8776
   409
Goal "(n::nat) < #2 ==> n = #0 | n = #1";
paulson@8776
   410
by (arith_tac 1);
paulson@8776
   411
qed "less_2_cases";
paulson@8776
   412
paulson@8776
   413
Goal "Suc(Suc(m)) mod #2 = m mod #2";
paulson@8776
   414
by (subgoal_tac "m mod #2 < #2" 1);
paulson@8776
   415
by (Asm_simp_tac 2);
paulson@8776
   416
be (less_2_cases RS disjE) 1;
paulson@8776
   417
by (ALLGOALS (asm_simp_tac (simpset() addsimps [mod_Suc])));
paulson@8776
   418
qed "mod2_Suc_Suc";
paulson@8776
   419
Addsimps [mod2_Suc_Suc];
paulson@8776
   420
paulson@8776
   421
Goal "(0 < m mod #2) = (m mod #2 = #1)";
paulson@8776
   422
by (subgoal_tac "m mod #2 < #2" 1);
paulson@8776
   423
by (Asm_simp_tac 2);
paulson@8776
   424
by (auto_tac (claset(), simpset() delsimps [mod_less_divisor]));
paulson@8776
   425
qed "mod2_gr_0";
paulson@8776
   426
Addsimps [mod2_gr_0, rename_numerals thy mod2_gr_0];
paulson@8776
   427