|
1 (* Title: HOL/NatBin.thy |
|
2 ID: $Id$ |
|
3 Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 Copyright 1999 University of Cambridge |
|
5 *) |
|
6 |
|
7 header {* Binary arithmetic for the natural numbers *} |
|
8 |
|
9 theory NatBin |
|
10 imports IntDiv |
|
11 begin |
|
12 |
|
13 text {* |
|
14 Arithmetic for naturals is reduced to that for the non-negative integers. |
|
15 *} |
|
16 |
|
17 instance nat :: number |
|
18 nat_number_of_def [code inline]: "number_of v == nat (number_of (v\<Colon>int))" .. |
|
19 |
|
20 abbreviation (xsymbols) |
|
21 square :: "'a::power => 'a" ("(_\<twosuperior>)" [1000] 999) where |
|
22 "x\<twosuperior> == x^2" |
|
23 |
|
24 notation (latex output) |
|
25 square ("(_\<twosuperior>)" [1000] 999) |
|
26 |
|
27 notation (HTML output) |
|
28 square ("(_\<twosuperior>)" [1000] 999) |
|
29 |
|
30 |
|
31 subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*} |
|
32 |
|
33 declare nat_0 [simp] nat_1 [simp] |
|
34 |
|
35 lemma nat_number_of [simp]: "nat (number_of w) = number_of w" |
|
36 by (simp add: nat_number_of_def) |
|
37 |
|
38 lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)" |
|
39 by (simp add: nat_number_of_def) |
|
40 |
|
41 lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)" |
|
42 by (simp add: nat_1 nat_number_of_def) |
|
43 |
|
44 lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0" |
|
45 by (simp add: nat_numeral_1_eq_1) |
|
46 |
|
47 lemma numeral_2_eq_2: "2 = Suc (Suc 0)" |
|
48 apply (unfold nat_number_of_def) |
|
49 apply (rule nat_2) |
|
50 done |
|
51 |
|
52 |
|
53 text{*Distributive laws for type @{text nat}. The others are in theory |
|
54 @{text IntArith}, but these require div and mod to be defined for type |
|
55 "int". They also need some of the lemmas proved above.*} |
|
56 |
|
57 lemma nat_div_distrib: "(0::int) <= z ==> nat (z div z') = nat z div nat z'" |
|
58 apply (case_tac "0 <= z'") |
|
59 apply (auto simp add: div_nonneg_neg_le0 DIVISION_BY_ZERO_DIV) |
|
60 apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO) |
|
61 apply (auto elim!: nonneg_eq_int) |
|
62 apply (rename_tac m m') |
|
63 apply (subgoal_tac "0 <= int m div int m'") |
|
64 prefer 2 apply (simp add: nat_numeral_0_eq_0 pos_imp_zdiv_nonneg_iff) |
|
65 apply (rule inj_int [THEN injD], simp) |
|
66 apply (rule_tac r = "int (m mod m') " in quorem_div) |
|
67 prefer 2 apply force |
|
68 apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0 zadd_int |
|
69 zmult_int) |
|
70 done |
|
71 |
|
72 (*Fails if z'<0: the LHS collapses to (nat z) but the RHS doesn't*) |
|
73 lemma nat_mod_distrib: |
|
74 "[| (0::int) <= z; 0 <= z' |] ==> nat (z mod z') = nat z mod nat z'" |
|
75 apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO) |
|
76 apply (auto elim!: nonneg_eq_int) |
|
77 apply (rename_tac m m') |
|
78 apply (subgoal_tac "0 <= int m mod int m'") |
|
79 prefer 2 apply (simp add: nat_less_iff nat_numeral_0_eq_0 pos_mod_sign) |
|
80 apply (rule inj_int [THEN injD], simp) |
|
81 apply (rule_tac q = "int (m div m') " in quorem_mod) |
|
82 prefer 2 apply force |
|
83 apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0 zadd_int zmult_int) |
|
84 done |
|
85 |
|
86 text{*Suggested by Matthias Daum*} |
|
87 lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)" |
|
88 apply (subgoal_tac "nat x div nat k < nat x") |
|
89 apply (simp (asm_lr) add: nat_div_distrib [symmetric]) |
|
90 apply (rule Divides.div_less_dividend, simp_all) |
|
91 done |
|
92 |
|
93 subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*} |
|
94 |
|
95 (*"neg" is used in rewrite rules for binary comparisons*) |
|
96 lemma int_nat_number_of [simp]: |
|
97 "int (number_of v :: nat) = |
|
98 (if neg (number_of v :: int) then 0 |
|
99 else (number_of v :: int))" |
|
100 by (simp del: nat_number_of |
|
101 add: neg_nat nat_number_of_def not_neg_nat add_assoc) |
|
102 |
|
103 |
|
104 subsubsection{*Successor *} |
|
105 |
|
106 lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)" |
|
107 apply (rule sym) |
|
108 apply (simp add: nat_eq_iff int_Suc) |
|
109 done |
|
110 |
|
111 lemma Suc_nat_number_of_add: |
|
112 "Suc (number_of v + n) = |
|
113 (if neg (number_of v :: int) then 1+n else number_of (Numeral.succ v) + n)" |
|
114 by (simp del: nat_number_of |
|
115 add: nat_number_of_def neg_nat |
|
116 Suc_nat_eq_nat_zadd1 number_of_succ) |
|
117 |
|
118 lemma Suc_nat_number_of [simp]: |
|
119 "Suc (number_of v) = |
|
120 (if neg (number_of v :: int) then 1 else number_of (Numeral.succ v))" |
|
121 apply (cut_tac n = 0 in Suc_nat_number_of_add) |
|
122 apply (simp cong del: if_weak_cong) |
|
123 done |
|
124 |
|
125 |
|
126 subsubsection{*Addition *} |
|
127 |
|
128 (*"neg" is used in rewrite rules for binary comparisons*) |
|
129 lemma add_nat_number_of [simp]: |
|
130 "(number_of v :: nat) + number_of v' = |
|
131 (if neg (number_of v :: int) then number_of v' |
|
132 else if neg (number_of v' :: int) then number_of v |
|
133 else number_of (v + v'))" |
|
134 by (force dest!: neg_nat |
|
135 simp del: nat_number_of |
|
136 simp add: nat_number_of_def nat_add_distrib [symmetric]) |
|
137 |
|
138 |
|
139 subsubsection{*Subtraction *} |
|
140 |
|
141 lemma diff_nat_eq_if: |
|
142 "nat z - nat z' = |
|
143 (if neg z' then nat z |
|
144 else let d = z-z' in |
|
145 if neg d then 0 else nat d)" |
|
146 apply (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0) |
|
147 done |
|
148 |
|
149 lemma diff_nat_number_of [simp]: |
|
150 "(number_of v :: nat) - number_of v' = |
|
151 (if neg (number_of v' :: int) then number_of v |
|
152 else let d = number_of (v + uminus v') in |
|
153 if neg d then 0 else nat d)" |
|
154 by (simp del: nat_number_of add: diff_nat_eq_if nat_number_of_def) |
|
155 |
|
156 |
|
157 |
|
158 subsubsection{*Multiplication *} |
|
159 |
|
160 lemma mult_nat_number_of [simp]: |
|
161 "(number_of v :: nat) * number_of v' = |
|
162 (if neg (number_of v :: int) then 0 else number_of (v * v'))" |
|
163 by (force dest!: neg_nat |
|
164 simp del: nat_number_of |
|
165 simp add: nat_number_of_def nat_mult_distrib [symmetric]) |
|
166 |
|
167 |
|
168 |
|
169 subsubsection{*Quotient *} |
|
170 |
|
171 lemma div_nat_number_of [simp]: |
|
172 "(number_of v :: nat) div number_of v' = |
|
173 (if neg (number_of v :: int) then 0 |
|
174 else nat (number_of v div number_of v'))" |
|
175 by (force dest!: neg_nat |
|
176 simp del: nat_number_of |
|
177 simp add: nat_number_of_def nat_div_distrib [symmetric]) |
|
178 |
|
179 lemma one_div_nat_number_of [simp]: |
|
180 "(Suc 0) div number_of v' = (nat (1 div number_of v'))" |
|
181 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) |
|
182 |
|
183 |
|
184 subsubsection{*Remainder *} |
|
185 |
|
186 lemma mod_nat_number_of [simp]: |
|
187 "(number_of v :: nat) mod number_of v' = |
|
188 (if neg (number_of v :: int) then 0 |
|
189 else if neg (number_of v' :: int) then number_of v |
|
190 else nat (number_of v mod number_of v'))" |
|
191 by (force dest!: neg_nat |
|
192 simp del: nat_number_of |
|
193 simp add: nat_number_of_def nat_mod_distrib [symmetric]) |
|
194 |
|
195 lemma one_mod_nat_number_of [simp]: |
|
196 "(Suc 0) mod number_of v' = |
|
197 (if neg (number_of v' :: int) then Suc 0 |
|
198 else nat (1 mod number_of v'))" |
|
199 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) |
|
200 |
|
201 |
|
202 subsubsection{* Divisibility *} |
|
203 |
|
204 lemmas dvd_eq_mod_eq_0_number_of = |
|
205 dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard] |
|
206 |
|
207 declare dvd_eq_mod_eq_0_number_of [simp] |
|
208 |
|
209 ML |
|
210 {* |
|
211 val nat_number_of_def = thm"nat_number_of_def"; |
|
212 |
|
213 val nat_number_of = thm"nat_number_of"; |
|
214 val nat_numeral_0_eq_0 = thm"nat_numeral_0_eq_0"; |
|
215 val nat_numeral_1_eq_1 = thm"nat_numeral_1_eq_1"; |
|
216 val numeral_1_eq_Suc_0 = thm"numeral_1_eq_Suc_0"; |
|
217 val numeral_2_eq_2 = thm"numeral_2_eq_2"; |
|
218 val nat_div_distrib = thm"nat_div_distrib"; |
|
219 val nat_mod_distrib = thm"nat_mod_distrib"; |
|
220 val int_nat_number_of = thm"int_nat_number_of"; |
|
221 val Suc_nat_eq_nat_zadd1 = thm"Suc_nat_eq_nat_zadd1"; |
|
222 val Suc_nat_number_of_add = thm"Suc_nat_number_of_add"; |
|
223 val Suc_nat_number_of = thm"Suc_nat_number_of"; |
|
224 val add_nat_number_of = thm"add_nat_number_of"; |
|
225 val diff_nat_eq_if = thm"diff_nat_eq_if"; |
|
226 val diff_nat_number_of = thm"diff_nat_number_of"; |
|
227 val mult_nat_number_of = thm"mult_nat_number_of"; |
|
228 val div_nat_number_of = thm"div_nat_number_of"; |
|
229 val mod_nat_number_of = thm"mod_nat_number_of"; |
|
230 *} |
|
231 |
|
232 |
|
233 subsection{*Comparisons*} |
|
234 |
|
235 subsubsection{*Equals (=) *} |
|
236 |
|
237 lemma eq_nat_nat_iff: |
|
238 "[| (0::int) <= z; 0 <= z' |] ==> (nat z = nat z') = (z=z')" |
|
239 by (auto elim!: nonneg_eq_int) |
|
240 |
|
241 (*"neg" is used in rewrite rules for binary comparisons*) |
|
242 lemma eq_nat_number_of [simp]: |
|
243 "((number_of v :: nat) = number_of v') = |
|
244 (if neg (number_of v :: int) then (iszero (number_of v' :: int) | neg (number_of v' :: int)) |
|
245 else if neg (number_of v' :: int) then iszero (number_of v :: int) |
|
246 else iszero (number_of (v + uminus v') :: int))" |
|
247 apply (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def |
|
248 eq_nat_nat_iff eq_number_of_eq nat_0 iszero_def |
|
249 split add: split_if cong add: imp_cong) |
|
250 apply (simp only: nat_eq_iff nat_eq_iff2) |
|
251 apply (simp add: not_neg_eq_ge_0 [symmetric]) |
|
252 done |
|
253 |
|
254 |
|
255 subsubsection{*Less-than (<) *} |
|
256 |
|
257 (*"neg" is used in rewrite rules for binary comparisons*) |
|
258 lemma less_nat_number_of [simp]: |
|
259 "((number_of v :: nat) < number_of v') = |
|
260 (if neg (number_of v :: int) then neg (number_of (uminus v') :: int) |
|
261 else neg (number_of (v + uminus v') :: int))" |
|
262 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def |
|
263 nat_less_eq_zless less_number_of_eq_neg zless_nat_eq_int_zless |
|
264 cong add: imp_cong, simp add: Pls_def) |
|
265 |
|
266 |
|
267 (*Maps #n to n for n = 0, 1, 2*) |
|
268 lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2 |
|
269 |
|
270 |
|
271 subsection{*Powers with Numeric Exponents*} |
|
272 |
|
273 text{*We cannot refer to the number @{term 2} in @{text Ring_and_Field.thy}. |
|
274 We cannot prove general results about the numeral @{term "-1"}, so we have to |
|
275 use @{term "- 1"} instead.*} |
|
276 |
|
277 lemma power2_eq_square: "(a::'a::{comm_semiring_1_cancel,recpower})\<twosuperior> = a * a" |
|
278 by (simp add: numeral_2_eq_2 Power.power_Suc) |
|
279 |
|
280 lemma zero_power2 [simp]: "(0::'a::{comm_semiring_1_cancel,recpower})\<twosuperior> = 0" |
|
281 by (simp add: power2_eq_square) |
|
282 |
|
283 lemma one_power2 [simp]: "(1::'a::{comm_semiring_1_cancel,recpower})\<twosuperior> = 1" |
|
284 by (simp add: power2_eq_square) |
|
285 |
|
286 lemma power3_eq_cube: "(x::'a::recpower) ^ 3 = x * x * x" |
|
287 apply (subgoal_tac "3 = Suc (Suc (Suc 0))") |
|
288 apply (erule ssubst) |
|
289 apply (simp add: power_Suc mult_ac) |
|
290 apply (unfold nat_number_of_def) |
|
291 apply (subst nat_eq_iff) |
|
292 apply simp |
|
293 done |
|
294 |
|
295 text{*Squares of literal numerals will be evaluated.*} |
|
296 lemmas power2_eq_square_number_of = |
|
297 power2_eq_square [of "number_of w", standard] |
|
298 declare power2_eq_square_number_of [simp] |
|
299 |
|
300 |
|
301 lemma zero_le_power2[simp]: "0 \<le> (a\<twosuperior>::'a::{ordered_idom,recpower})" |
|
302 by (simp add: power2_eq_square) |
|
303 |
|
304 lemma zero_less_power2[simp]: |
|
305 "(0 < a\<twosuperior>) = (a \<noteq> (0::'a::{ordered_idom,recpower}))" |
|
306 by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff) |
|
307 |
|
308 lemma power2_less_0[simp]: |
|
309 fixes a :: "'a::{ordered_idom,recpower}" |
|
310 shows "~ (a\<twosuperior> < 0)" |
|
311 by (force simp add: power2_eq_square mult_less_0_iff) |
|
312 |
|
313 lemma zero_eq_power2[simp]: |
|
314 "(a\<twosuperior> = 0) = (a = (0::'a::{ordered_idom,recpower}))" |
|
315 by (force simp add: power2_eq_square mult_eq_0_iff) |
|
316 |
|
317 lemma abs_power2[simp]: |
|
318 "abs(a\<twosuperior>) = (a\<twosuperior>::'a::{ordered_idom,recpower})" |
|
319 by (simp add: power2_eq_square abs_mult abs_mult_self) |
|
320 |
|
321 lemma power2_abs[simp]: |
|
322 "(abs a)\<twosuperior> = (a\<twosuperior>::'a::{ordered_idom,recpower})" |
|
323 by (simp add: power2_eq_square abs_mult_self) |
|
324 |
|
325 lemma power2_minus[simp]: |
|
326 "(- a)\<twosuperior> = (a\<twosuperior>::'a::{comm_ring_1,recpower})" |
|
327 by (simp add: power2_eq_square) |
|
328 |
|
329 lemma power2_le_imp_le: |
|
330 fixes x y :: "'a::{ordered_semidom,recpower}" |
|
331 shows "\<lbrakk>x\<twosuperior> \<le> y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x \<le> y" |
|
332 unfolding numeral_2_eq_2 by (rule power_le_imp_le_base) |
|
333 |
|
334 lemma power2_less_imp_less: |
|
335 fixes x y :: "'a::{ordered_semidom,recpower}" |
|
336 shows "\<lbrakk>x\<twosuperior> < y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x < y" |
|
337 by (rule power_less_imp_less_base) |
|
338 |
|
339 lemma power2_eq_imp_eq: |
|
340 fixes x y :: "'a::{ordered_semidom,recpower}" |
|
341 shows "\<lbrakk>x\<twosuperior> = y\<twosuperior>; 0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> x = y" |
|
342 unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base, simp) |
|
343 |
|
344 lemma power_minus1_even[simp]: "(- 1) ^ (2*n) = (1::'a::{comm_ring_1,recpower})" |
|
345 apply (induct "n") |
|
346 apply (auto simp add: power_Suc power_add) |
|
347 done |
|
348 |
|
349 lemma power_even_eq: "(a::'a::recpower) ^ (2*n) = (a^n)^2" |
|
350 by (subst mult_commute) (simp add: power_mult) |
|
351 |
|
352 lemma power_odd_eq: "(a::int) ^ Suc(2*n) = a * (a^n)^2" |
|
353 by (simp add: power_even_eq) |
|
354 |
|
355 lemma power_minus_even [simp]: |
|
356 "(-a) ^ (2*n) = (a::'a::{comm_ring_1,recpower}) ^ (2*n)" |
|
357 by (simp add: power_minus1_even power_minus [of a]) |
|
358 |
|
359 lemma zero_le_even_power'[simp]: |
|
360 "0 \<le> (a::'a::{ordered_idom,recpower}) ^ (2*n)" |
|
361 proof (induct "n") |
|
362 case 0 |
|
363 show ?case by (simp add: zero_le_one) |
|
364 next |
|
365 case (Suc n) |
|
366 have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" |
|
367 by (simp add: mult_ac power_add power2_eq_square) |
|
368 thus ?case |
|
369 by (simp add: prems zero_le_mult_iff) |
|
370 qed |
|
371 |
|
372 lemma odd_power_less_zero: |
|
373 "(a::'a::{ordered_idom,recpower}) < 0 ==> a ^ Suc(2*n) < 0" |
|
374 proof (induct "n") |
|
375 case 0 |
|
376 show ?case by (simp add: Power.power_Suc) |
|
377 next |
|
378 case (Suc n) |
|
379 have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)" |
|
380 by (simp add: mult_ac power_add power2_eq_square Power.power_Suc) |
|
381 thus ?case |
|
382 by (simp add: prems mult_less_0_iff mult_neg_neg) |
|
383 qed |
|
384 |
|
385 lemma odd_0_le_power_imp_0_le: |
|
386 "0 \<le> a ^ Suc(2*n) ==> 0 \<le> (a::'a::{ordered_idom,recpower})" |
|
387 apply (insert odd_power_less_zero [of a n]) |
|
388 apply (force simp add: linorder_not_less [symmetric]) |
|
389 done |
|
390 |
|
391 text{*Simprules for comparisons where common factors can be cancelled.*} |
|
392 lemmas zero_compare_simps = |
|
393 add_strict_increasing add_strict_increasing2 add_increasing |
|
394 zero_le_mult_iff zero_le_divide_iff |
|
395 zero_less_mult_iff zero_less_divide_iff |
|
396 mult_le_0_iff divide_le_0_iff |
|
397 mult_less_0_iff divide_less_0_iff |
|
398 zero_le_power2 power2_less_0 |
|
399 |
|
400 subsubsection{*Nat *} |
|
401 |
|
402 lemma Suc_pred': "0 < n ==> n = Suc(n - 1)" |
|
403 by (simp add: numerals) |
|
404 |
|
405 (*Expresses a natural number constant as the Suc of another one. |
|
406 NOT suitable for rewriting because n recurs in the condition.*) |
|
407 lemmas expand_Suc = Suc_pred' [of "number_of v", standard] |
|
408 |
|
409 subsubsection{*Arith *} |
|
410 |
|
411 lemma Suc_eq_add_numeral_1: "Suc n = n + 1" |
|
412 by (simp add: numerals) |
|
413 |
|
414 lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n" |
|
415 by (simp add: numerals) |
|
416 |
|
417 (* These two can be useful when m = number_of... *) |
|
418 |
|
419 lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))" |
|
420 apply (case_tac "m") |
|
421 apply (simp_all add: numerals) |
|
422 done |
|
423 |
|
424 lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))" |
|
425 apply (case_tac "m") |
|
426 apply (simp_all add: numerals) |
|
427 done |
|
428 |
|
429 lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))" |
|
430 apply (case_tac "m") |
|
431 apply (simp_all add: numerals) |
|
432 done |
|
433 |
|
434 |
|
435 subsection{*Comparisons involving (0::nat) *} |
|
436 |
|
437 text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*} |
|
438 |
|
439 lemma eq_number_of_0 [simp]: |
|
440 "(number_of v = (0::nat)) = |
|
441 (if neg (number_of v :: int) then True else iszero (number_of v :: int))" |
|
442 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0) |
|
443 |
|
444 lemma eq_0_number_of [simp]: |
|
445 "((0::nat) = number_of v) = |
|
446 (if neg (number_of v :: int) then True else iszero (number_of v :: int))" |
|
447 by (rule trans [OF eq_sym_conv eq_number_of_0]) |
|
448 |
|
449 lemma less_0_number_of [simp]: |
|
450 "((0::nat) < number_of v) = neg (number_of (uminus v) :: int)" |
|
451 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] Pls_def) |
|
452 |
|
453 |
|
454 lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)" |
|
455 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0) |
|
456 |
|
457 |
|
458 |
|
459 subsection{*Comparisons involving @{term Suc} *} |
|
460 |
|
461 lemma eq_number_of_Suc [simp]: |
|
462 "(number_of v = Suc n) = |
|
463 (let pv = number_of (Numeral.pred v) in |
|
464 if neg pv then False else nat pv = n)" |
|
465 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less |
|
466 number_of_pred nat_number_of_def |
|
467 split add: split_if) |
|
468 apply (rule_tac x = "number_of v" in spec) |
|
469 apply (auto simp add: nat_eq_iff) |
|
470 done |
|
471 |
|
472 lemma Suc_eq_number_of [simp]: |
|
473 "(Suc n = number_of v) = |
|
474 (let pv = number_of (Numeral.pred v) in |
|
475 if neg pv then False else nat pv = n)" |
|
476 by (rule trans [OF eq_sym_conv eq_number_of_Suc]) |
|
477 |
|
478 lemma less_number_of_Suc [simp]: |
|
479 "(number_of v < Suc n) = |
|
480 (let pv = number_of (Numeral.pred v) in |
|
481 if neg pv then True else nat pv < n)" |
|
482 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less |
|
483 number_of_pred nat_number_of_def |
|
484 split add: split_if) |
|
485 apply (rule_tac x = "number_of v" in spec) |
|
486 apply (auto simp add: nat_less_iff) |
|
487 done |
|
488 |
|
489 lemma less_Suc_number_of [simp]: |
|
490 "(Suc n < number_of v) = |
|
491 (let pv = number_of (Numeral.pred v) in |
|
492 if neg pv then False else n < nat pv)" |
|
493 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less |
|
494 number_of_pred nat_number_of_def |
|
495 split add: split_if) |
|
496 apply (rule_tac x = "number_of v" in spec) |
|
497 apply (auto simp add: zless_nat_eq_int_zless) |
|
498 done |
|
499 |
|
500 lemma le_number_of_Suc [simp]: |
|
501 "(number_of v <= Suc n) = |
|
502 (let pv = number_of (Numeral.pred v) in |
|
503 if neg pv then True else nat pv <= n)" |
|
504 by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric]) |
|
505 |
|
506 lemma le_Suc_number_of [simp]: |
|
507 "(Suc n <= number_of v) = |
|
508 (let pv = number_of (Numeral.pred v) in |
|
509 if neg pv then False else n <= nat pv)" |
|
510 by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric]) |
|
511 |
|
512 |
|
513 (* Push int(.) inwards: *) |
|
514 declare zadd_int [symmetric, simp] |
|
515 |
|
516 lemma lemma1: "(m+m = n+n) = (m = (n::int))" |
|
517 by auto |
|
518 |
|
519 lemma lemma2: "m+m ~= (1::int) + (n + n)" |
|
520 apply auto |
|
521 apply (drule_tac f = "%x. x mod 2" in arg_cong) |
|
522 apply (simp add: zmod_zadd1_eq) |
|
523 done |
|
524 |
|
525 lemma eq_number_of_BIT_BIT: |
|
526 "((number_of (v BIT x) ::int) = number_of (w BIT y)) = |
|
527 (x=y & (((number_of v) ::int) = number_of w))" |
|
528 apply (simp only: number_of_BIT lemma1 lemma2 eq_commute |
|
529 OrderedGroup.add_left_cancel add_assoc OrderedGroup.add_0_left |
|
530 split add: bit.split) |
|
531 apply simp |
|
532 done |
|
533 |
|
534 lemma eq_number_of_BIT_Pls: |
|
535 "((number_of (v BIT x) ::int) = Numeral0) = |
|
536 (x=bit.B0 & (((number_of v) ::int) = Numeral0))" |
|
537 apply (simp only: simp_thms add: number_of_BIT number_of_Pls eq_commute |
|
538 split add: bit.split cong: imp_cong) |
|
539 apply (rule_tac x = "number_of v" in spec, safe) |
|
540 apply (simp_all (no_asm_use)) |
|
541 apply (drule_tac f = "%x. x mod 2" in arg_cong) |
|
542 apply (simp add: zmod_zadd1_eq) |
|
543 done |
|
544 |
|
545 lemma eq_number_of_BIT_Min: |
|
546 "((number_of (v BIT x) ::int) = number_of Numeral.Min) = |
|
547 (x=bit.B1 & (((number_of v) ::int) = number_of Numeral.Min))" |
|
548 apply (simp only: simp_thms add: number_of_BIT number_of_Min eq_commute |
|
549 split add: bit.split cong: imp_cong) |
|
550 apply (rule_tac x = "number_of v" in spec, auto) |
|
551 apply (drule_tac f = "%x. x mod 2" in arg_cong, auto) |
|
552 done |
|
553 |
|
554 lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Numeral.Min" |
|
555 by auto |
|
556 |
|
557 |
|
558 |
|
559 subsection{*Max and Min Combined with @{term Suc} *} |
|
560 |
|
561 lemma max_number_of_Suc [simp]: |
|
562 "max (Suc n) (number_of v) = |
|
563 (let pv = number_of (Numeral.pred v) in |
|
564 if neg pv then Suc n else Suc(max n (nat pv)))" |
|
565 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def |
|
566 split add: split_if nat.split) |
|
567 apply (rule_tac x = "number_of v" in spec) |
|
568 apply auto |
|
569 done |
|
570 |
|
571 lemma max_Suc_number_of [simp]: |
|
572 "max (number_of v) (Suc n) = |
|
573 (let pv = number_of (Numeral.pred v) in |
|
574 if neg pv then Suc n else Suc(max (nat pv) n))" |
|
575 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def |
|
576 split add: split_if nat.split) |
|
577 apply (rule_tac x = "number_of v" in spec) |
|
578 apply auto |
|
579 done |
|
580 |
|
581 lemma min_number_of_Suc [simp]: |
|
582 "min (Suc n) (number_of v) = |
|
583 (let pv = number_of (Numeral.pred v) in |
|
584 if neg pv then 0 else Suc(min n (nat pv)))" |
|
585 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def |
|
586 split add: split_if nat.split) |
|
587 apply (rule_tac x = "number_of v" in spec) |
|
588 apply auto |
|
589 done |
|
590 |
|
591 lemma min_Suc_number_of [simp]: |
|
592 "min (number_of v) (Suc n) = |
|
593 (let pv = number_of (Numeral.pred v) in |
|
594 if neg pv then 0 else Suc(min (nat pv) n))" |
|
595 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def |
|
596 split add: split_if nat.split) |
|
597 apply (rule_tac x = "number_of v" in spec) |
|
598 apply auto |
|
599 done |
|
600 |
|
601 subsection{*Literal arithmetic involving powers*} |
|
602 |
|
603 lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n" |
|
604 apply (induct "n") |
|
605 apply (simp_all (no_asm_simp) add: nat_mult_distrib) |
|
606 done |
|
607 |
|
608 lemma power_nat_number_of: |
|
609 "(number_of v :: nat) ^ n = |
|
610 (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))" |
|
611 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq |
|
612 split add: split_if cong: imp_cong) |
|
613 |
|
614 |
|
615 lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard] |
|
616 declare power_nat_number_of_number_of [simp] |
|
617 |
|
618 |
|
619 |
|
620 text{*For the integers*} |
|
621 |
|
622 lemma zpower_number_of_even: |
|
623 "(z::int) ^ number_of (w BIT bit.B0) = (let w = z ^ (number_of w) in w * w)" |
|
624 unfolding Let_def nat_number_of_def number_of_BIT bit.cases |
|
625 apply (rule_tac x = "number_of w" in spec, clarify) |
|
626 apply (case_tac " (0::int) <= x") |
|
627 apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square) |
|
628 done |
|
629 |
|
630 lemma zpower_number_of_odd: |
|
631 "(z::int) ^ number_of (w BIT bit.B1) = (if (0::int) <= number_of w |
|
632 then (let w = z ^ (number_of w) in z * w * w) else 1)" |
|
633 unfolding Let_def nat_number_of_def number_of_BIT bit.cases |
|
634 apply (rule_tac x = "number_of w" in spec, auto) |
|
635 apply (simp only: nat_add_distrib nat_mult_distrib) |
|
636 apply simp |
|
637 apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat) |
|
638 done |
|
639 |
|
640 lemmas zpower_number_of_even_number_of = |
|
641 zpower_number_of_even [of "number_of v", standard] |
|
642 declare zpower_number_of_even_number_of [simp] |
|
643 |
|
644 lemmas zpower_number_of_odd_number_of = |
|
645 zpower_number_of_odd [of "number_of v", standard] |
|
646 declare zpower_number_of_odd_number_of [simp] |
|
647 |
|
648 |
|
649 |
|
650 |
|
651 ML |
|
652 {* |
|
653 val numerals = thms"numerals"; |
|
654 val numeral_ss = simpset() addsimps numerals; |
|
655 |
|
656 val nat_bin_arith_setup = |
|
657 Fast_Arith.map_data |
|
658 (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} => |
|
659 {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, |
|
660 inj_thms = inj_thms, |
|
661 lessD = lessD, neqE = neqE, |
|
662 simpset = simpset addsimps [Suc_nat_number_of, int_nat_number_of, |
|
663 not_neg_number_of_Pls, |
|
664 neg_number_of_Min,neg_number_of_BIT]}) |
|
665 *} |
|
666 |
|
667 setup nat_bin_arith_setup |
|
668 |
|
669 (* Enable arith to deal with div/mod k where k is a numeral: *) |
|
670 declare split_div[of _ _ "number_of k", standard, arith_split] |
|
671 declare split_mod[of _ _ "number_of k", standard, arith_split] |
|
672 |
|
673 lemma nat_number_of_Pls: "Numeral0 = (0::nat)" |
|
674 by (simp add: number_of_Pls nat_number_of_def) |
|
675 |
|
676 lemma nat_number_of_Min: "number_of Numeral.Min = (0::nat)" |
|
677 apply (simp only: number_of_Min nat_number_of_def nat_zminus_int) |
|
678 done |
|
679 |
|
680 lemma nat_number_of_BIT_1: |
|
681 "number_of (w BIT bit.B1) = |
|
682 (if neg (number_of w :: int) then 0 |
|
683 else let n = number_of w in Suc (n + n))" |
|
684 apply (simp only: nat_number_of_def Let_def split: split_if) |
|
685 apply (intro conjI impI) |
|
686 apply (simp add: neg_nat neg_number_of_BIT) |
|
687 apply (rule int_int_eq [THEN iffD1]) |
|
688 apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms) |
|
689 apply (simp only: number_of_BIT zadd_assoc split: bit.split) |
|
690 apply simp |
|
691 done |
|
692 |
|
693 lemma nat_number_of_BIT_0: |
|
694 "number_of (w BIT bit.B0) = (let n::nat = number_of w in n + n)" |
|
695 apply (simp only: nat_number_of_def Let_def) |
|
696 apply (cases "neg (number_of w :: int)") |
|
697 apply (simp add: neg_nat neg_number_of_BIT) |
|
698 apply (rule int_int_eq [THEN iffD1]) |
|
699 apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms) |
|
700 apply (simp only: number_of_BIT zadd_assoc) |
|
701 apply simp |
|
702 done |
|
703 |
|
704 lemmas nat_number = |
|
705 nat_number_of_Pls nat_number_of_Min |
|
706 nat_number_of_BIT_1 nat_number_of_BIT_0 |
|
707 |
|
708 lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)" |
|
709 by (simp add: Let_def) |
|
710 |
|
711 lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring,recpower})" |
|
712 by (simp add: power_mult); |
|
713 |
|
714 lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring,recpower})" |
|
715 by (simp add: power_mult power_Suc); |
|
716 |
|
717 |
|
718 subsection{*Literal arithmetic and @{term of_nat}*} |
|
719 |
|
720 lemma of_nat_double: |
|
721 "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)" |
|
722 by (simp only: mult_2 nat_add_distrib of_nat_add) |
|
723 |
|
724 lemma nat_numeral_m1_eq_0: "-1 = (0::nat)" |
|
725 by (simp only: nat_number_of_def) |
|
726 |
|
727 lemma of_nat_number_of_lemma: |
|
728 "of_nat (number_of v :: nat) = |
|
729 (if 0 \<le> (number_of v :: int) |
|
730 then (number_of v :: 'a :: number_ring) |
|
731 else 0)" |
|
732 by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat); |
|
733 |
|
734 lemma of_nat_number_of_eq [simp]: |
|
735 "of_nat (number_of v :: nat) = |
|
736 (if neg (number_of v :: int) then 0 |
|
737 else (number_of v :: 'a :: number_ring))" |
|
738 by (simp only: of_nat_number_of_lemma neg_def, simp) |
|
739 |
|
740 |
|
741 subsection {*Lemmas for the Combination and Cancellation Simprocs*} |
|
742 |
|
743 lemma nat_number_of_add_left: |
|
744 "number_of v + (number_of v' + (k::nat)) = |
|
745 (if neg (number_of v :: int) then number_of v' + k |
|
746 else if neg (number_of v' :: int) then number_of v + k |
|
747 else number_of (v + v') + k)" |
|
748 by simp |
|
749 |
|
750 lemma nat_number_of_mult_left: |
|
751 "number_of v * (number_of v' * (k::nat)) = |
|
752 (if neg (number_of v :: int) then 0 |
|
753 else number_of (v * v') * k)" |
|
754 by simp |
|
755 |
|
756 |
|
757 subsubsection{*For @{text combine_numerals}*} |
|
758 |
|
759 lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)" |
|
760 by (simp add: add_mult_distrib) |
|
761 |
|
762 |
|
763 subsubsection{*For @{text cancel_numerals}*} |
|
764 |
|
765 lemma nat_diff_add_eq1: |
|
766 "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)" |
|
767 by (simp split add: nat_diff_split add: add_mult_distrib) |
|
768 |
|
769 lemma nat_diff_add_eq2: |
|
770 "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))" |
|
771 by (simp split add: nat_diff_split add: add_mult_distrib) |
|
772 |
|
773 lemma nat_eq_add_iff1: |
|
774 "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)" |
|
775 by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
776 |
|
777 lemma nat_eq_add_iff2: |
|
778 "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)" |
|
779 by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
780 |
|
781 lemma nat_less_add_iff1: |
|
782 "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)" |
|
783 by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
784 |
|
785 lemma nat_less_add_iff2: |
|
786 "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)" |
|
787 by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
788 |
|
789 lemma nat_le_add_iff1: |
|
790 "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)" |
|
791 by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
792 |
|
793 lemma nat_le_add_iff2: |
|
794 "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)" |
|
795 by (auto split add: nat_diff_split simp add: add_mult_distrib) |
|
796 |
|
797 |
|
798 subsubsection{*For @{text cancel_numeral_factors} *} |
|
799 |
|
800 lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)" |
|
801 by auto |
|
802 |
|
803 lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)" |
|
804 by auto |
|
805 |
|
806 lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)" |
|
807 by auto |
|
808 |
|
809 lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)" |
|
810 by auto |
|
811 |
|
812 |
|
813 subsubsection{*For @{text cancel_factor} *} |
|
814 |
|
815 lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)" |
|
816 by auto |
|
817 |
|
818 lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)" |
|
819 by auto |
|
820 |
|
821 lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)" |
|
822 by auto |
|
823 |
|
824 lemma nat_mult_div_cancel_disj: |
|
825 "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)" |
|
826 by (simp add: nat_mult_div_cancel1) |
|
827 |
|
828 |
|
829 subsection {* legacy ML bindings *} |
|
830 |
|
831 ML |
|
832 {* |
|
833 val eq_nat_nat_iff = thm"eq_nat_nat_iff"; |
|
834 val eq_nat_number_of = thm"eq_nat_number_of"; |
|
835 val less_nat_number_of = thm"less_nat_number_of"; |
|
836 val power2_eq_square = thm "power2_eq_square"; |
|
837 val zero_le_power2 = thm "zero_le_power2"; |
|
838 val zero_less_power2 = thm "zero_less_power2"; |
|
839 val zero_eq_power2 = thm "zero_eq_power2"; |
|
840 val abs_power2 = thm "abs_power2"; |
|
841 val power2_abs = thm "power2_abs"; |
|
842 val power2_minus = thm "power2_minus"; |
|
843 val power_minus1_even = thm "power_minus1_even"; |
|
844 val power_minus_even = thm "power_minus_even"; |
|
845 val odd_power_less_zero = thm "odd_power_less_zero"; |
|
846 val odd_0_le_power_imp_0_le = thm "odd_0_le_power_imp_0_le"; |
|
847 |
|
848 val Suc_pred' = thm"Suc_pred'"; |
|
849 val expand_Suc = thm"expand_Suc"; |
|
850 val Suc_eq_add_numeral_1 = thm"Suc_eq_add_numeral_1"; |
|
851 val Suc_eq_add_numeral_1_left = thm"Suc_eq_add_numeral_1_left"; |
|
852 val add_eq_if = thm"add_eq_if"; |
|
853 val mult_eq_if = thm"mult_eq_if"; |
|
854 val power_eq_if = thm"power_eq_if"; |
|
855 val eq_number_of_0 = thm"eq_number_of_0"; |
|
856 val eq_0_number_of = thm"eq_0_number_of"; |
|
857 val less_0_number_of = thm"less_0_number_of"; |
|
858 val neg_imp_number_of_eq_0 = thm"neg_imp_number_of_eq_0"; |
|
859 val eq_number_of_Suc = thm"eq_number_of_Suc"; |
|
860 val Suc_eq_number_of = thm"Suc_eq_number_of"; |
|
861 val less_number_of_Suc = thm"less_number_of_Suc"; |
|
862 val less_Suc_number_of = thm"less_Suc_number_of"; |
|
863 val le_number_of_Suc = thm"le_number_of_Suc"; |
|
864 val le_Suc_number_of = thm"le_Suc_number_of"; |
|
865 val eq_number_of_BIT_BIT = thm"eq_number_of_BIT_BIT"; |
|
866 val eq_number_of_BIT_Pls = thm"eq_number_of_BIT_Pls"; |
|
867 val eq_number_of_BIT_Min = thm"eq_number_of_BIT_Min"; |
|
868 val eq_number_of_Pls_Min = thm"eq_number_of_Pls_Min"; |
|
869 val of_nat_number_of_eq = thm"of_nat_number_of_eq"; |
|
870 val nat_power_eq = thm"nat_power_eq"; |
|
871 val power_nat_number_of = thm"power_nat_number_of"; |
|
872 val zpower_number_of_even = thm"zpower_number_of_even"; |
|
873 val zpower_number_of_odd = thm"zpower_number_of_odd"; |
|
874 val nat_number_of_Pls = thm"nat_number_of_Pls"; |
|
875 val nat_number_of_Min = thm"nat_number_of_Min"; |
|
876 val Let_Suc = thm"Let_Suc"; |
|
877 |
|
878 val nat_number = thms"nat_number"; |
|
879 |
|
880 val nat_number_of_add_left = thm"nat_number_of_add_left"; |
|
881 val nat_number_of_mult_left = thm"nat_number_of_mult_left"; |
|
882 val left_add_mult_distrib = thm"left_add_mult_distrib"; |
|
883 val nat_diff_add_eq1 = thm"nat_diff_add_eq1"; |
|
884 val nat_diff_add_eq2 = thm"nat_diff_add_eq2"; |
|
885 val nat_eq_add_iff1 = thm"nat_eq_add_iff1"; |
|
886 val nat_eq_add_iff2 = thm"nat_eq_add_iff2"; |
|
887 val nat_less_add_iff1 = thm"nat_less_add_iff1"; |
|
888 val nat_less_add_iff2 = thm"nat_less_add_iff2"; |
|
889 val nat_le_add_iff1 = thm"nat_le_add_iff1"; |
|
890 val nat_le_add_iff2 = thm"nat_le_add_iff2"; |
|
891 val nat_mult_le_cancel1 = thm"nat_mult_le_cancel1"; |
|
892 val nat_mult_less_cancel1 = thm"nat_mult_less_cancel1"; |
|
893 val nat_mult_eq_cancel1 = thm"nat_mult_eq_cancel1"; |
|
894 val nat_mult_div_cancel1 = thm"nat_mult_div_cancel1"; |
|
895 val nat_mult_le_cancel_disj = thm"nat_mult_le_cancel_disj"; |
|
896 val nat_mult_less_cancel_disj = thm"nat_mult_less_cancel_disj"; |
|
897 val nat_mult_eq_cancel_disj = thm"nat_mult_eq_cancel_disj"; |
|
898 val nat_mult_div_cancel_disj = thm"nat_mult_div_cancel_disj"; |
|
899 |
|
900 val power_minus_even = thm"power_minus_even"; |
|
901 *} |
|
902 |
|
903 end |