author | berghofe |
Mon, 05 Aug 2002 14:27:42 +0200 | |
changeset 13449 | 43c9ec498291 |
parent 12338 | de0f4a63baa5 |
child 13585 | db4005b40cc6 |
permissions | -rw-r--r-- |
923 | 1 |
(* Title: HOL/Nat.thy |
2 |
ID: $Id$ |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
3 |
Author: Tobias Nipkow and Lawrence C Paulson |
923 | 4 |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
5 |
Type "nat" is a linear order, and a datatype; arithmetic operators + - |
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
6 |
and * (for div, mod and dvd, see theory Divides). |
923 | 7 |
*) |
8 |
||
13449 | 9 |
header {* Natural numbers *} |
10 |
||
11 |
theory Nat = Wellfounded_Recursion: |
|
12 |
||
13 |
subsection {* Type @{text ind} *} |
|
14 |
||
15 |
typedecl ind |
|
16 |
||
17 |
consts |
|
18 |
Zero_Rep :: ind |
|
19 |
Suc_Rep :: "ind => ind" |
|
20 |
||
21 |
axioms |
|
22 |
-- {* the axiom of infinity in 2 parts *} |
|
23 |
inj_Suc_Rep: "inj Suc_Rep" |
|
24 |
Suc_Rep_not_Zero_Rep: "Suc_Rep x ~= Zero_Rep" |
|
25 |
||
26 |
||
27 |
subsection {* Type nat *} |
|
28 |
||
29 |
text {* Type definition *} |
|
30 |
||
31 |
consts |
|
32 |
Nat :: "ind set" |
|
33 |
||
34 |
inductive Nat |
|
35 |
intros |
|
36 |
Zero_RepI: "Zero_Rep : Nat" |
|
37 |
Suc_RepI: "i : Nat ==> Suc_Rep i : Nat" |
|
38 |
||
39 |
global |
|
40 |
||
41 |
typedef (open Nat) |
|
42 |
nat = "Nat" by (rule exI, rule Nat.Zero_RepI) |
|
43 |
||
44 |
instance nat :: ord .. |
|
45 |
instance nat :: zero .. |
|
46 |
instance nat :: one .. |
|
47 |
||
48 |
||
49 |
text {* Abstract constants and syntax *} |
|
50 |
||
51 |
consts |
|
52 |
Suc :: "nat => nat" |
|
53 |
pred_nat :: "(nat * nat) set" |
|
54 |
||
55 |
local |
|
56 |
||
57 |
defs |
|
58 |
Zero_nat_def: "0 == Abs_Nat Zero_Rep" |
|
59 |
Suc_def: "Suc == (%n. Abs_Nat (Suc_Rep (Rep_Nat n)))" |
|
60 |
One_nat_def [simp]: "1 == Suc 0" |
|
61 |
||
62 |
-- {* nat operations *} |
|
63 |
pred_nat_def: "pred_nat == {(m, n). n = Suc m}" |
|
64 |
||
65 |
less_def: "m < n == (m, n) : trancl pred_nat" |
|
66 |
||
67 |
le_def: "m <= (n::nat) == ~ (n < m)" |
|
68 |
||
69 |
||
70 |
text {* Induction *} |
|
923 | 71 |
|
13449 | 72 |
theorem nat_induct: "P 0 ==> (!!n. P n ==> P (Suc n)) ==> P n" |
73 |
apply (unfold Zero_nat_def Suc_def) |
|
74 |
apply (rule Rep_Nat_inverse [THEN subst]) -- {* types force good instantiation *} |
|
75 |
apply (erule Rep_Nat [THEN Nat.induct]) |
|
76 |
apply (rules elim: Abs_Nat_inverse [THEN subst]) |
|
77 |
done |
|
78 |
||
79 |
||
80 |
text {* Isomorphisms: @{text Abs_Nat} and @{text Rep_Nat} *} |
|
81 |
||
82 |
lemma inj_Rep_Nat: "inj Rep_Nat" |
|
83 |
apply (rule inj_inverseI) |
|
84 |
apply (rule Rep_Nat_inverse) |
|
85 |
done |
|
86 |
||
87 |
lemma inj_on_Abs_Nat: "inj_on Abs_Nat Nat" |
|
88 |
apply (rule inj_on_inverseI) |
|
89 |
apply (erule Abs_Nat_inverse) |
|
90 |
done |
|
91 |
||
92 |
text {* Distinctness of constructors *} |
|
93 |
||
94 |
lemma Suc_not_Zero [iff]: "Suc m ~= 0" |
|
95 |
apply (unfold Zero_nat_def Suc_def) |
|
96 |
apply (rule inj_on_Abs_Nat [THEN inj_on_contraD]) |
|
97 |
apply (rule Suc_Rep_not_Zero_Rep) |
|
98 |
apply (rule Rep_Nat Nat.Suc_RepI Nat.Zero_RepI)+ |
|
99 |
done |
|
100 |
||
101 |
lemma Zero_not_Suc [iff]: "0 ~= Suc m" |
|
102 |
by (rule not_sym, rule Suc_not_Zero not_sym) |
|
103 |
||
104 |
lemma Suc_neq_Zero: "Suc m = 0 ==> R" |
|
105 |
by (rule notE, rule Suc_not_Zero) |
|
106 |
||
107 |
lemma Zero_neq_Suc: "0 = Suc m ==> R" |
|
108 |
by (rule Suc_neq_Zero, erule sym) |
|
109 |
||
110 |
text {* Injectiveness of @{term Suc} *} |
|
111 |
||
112 |
lemma inj_Suc: "inj Suc" |
|
113 |
apply (unfold Suc_def) |
|
114 |
apply (rule injI) |
|
115 |
apply (drule inj_on_Abs_Nat [THEN inj_onD]) |
|
116 |
apply (rule Rep_Nat Nat.Suc_RepI)+ |
|
117 |
apply (drule inj_Suc_Rep [THEN injD]) |
|
118 |
apply (erule inj_Rep_Nat [THEN injD]) |
|
119 |
done |
|
120 |
||
121 |
lemma Suc_inject: "Suc x = Suc y ==> x = y" |
|
122 |
by (rule inj_Suc [THEN injD]) |
|
123 |
||
124 |
lemma Suc_Suc_eq [iff]: "(Suc m = Suc n) = (m = n)" |
|
125 |
apply (rule iffI) |
|
126 |
apply (erule Suc_inject) |
|
127 |
apply (erule arg_cong) |
|
128 |
done |
|
129 |
||
130 |
lemma nat_not_singleton: "(ALL x. x = (0::nat)) = False" |
|
131 |
by auto |
|
132 |
||
133 |
text {* @{typ nat} is a datatype *} |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
134 |
|
5188
633ec5f6c155
Declaration of type 'nat' as a datatype (this allows usage of
berghofe
parents:
4640
diff
changeset
|
135 |
rep_datatype nat |
13449 | 136 |
distinct Suc_not_Zero Zero_not_Suc |
137 |
inject Suc_Suc_eq |
|
138 |
induction nat_induct |
|
139 |
||
140 |
lemma n_not_Suc_n: "n ~= Suc n" |
|
141 |
by (induct n) simp_all |
|
142 |
||
143 |
lemma Suc_n_not_n: "Suc t ~= t" |
|
144 |
by (rule not_sym, rule n_not_Suc_n) |
|
145 |
||
146 |
text {* A special form of induction for reasoning |
|
147 |
about @{term "m < n"} and @{term "m - n"} *} |
|
148 |
||
149 |
theorem diff_induct: "(!!x. P x 0) ==> (!!y. P 0 (Suc y)) ==> |
|
150 |
(!!x y. P x y ==> P (Suc x) (Suc y)) ==> P m n" |
|
151 |
apply (rule_tac x = "m" in spec) |
|
152 |
apply (induct_tac n) |
|
153 |
prefer 2 |
|
154 |
apply (rule allI) |
|
155 |
apply (induct_tac x) |
|
156 |
apply rules+ |
|
157 |
done |
|
158 |
||
159 |
subsection {* Basic properties of "less than" *} |
|
160 |
||
161 |
lemma wf_pred_nat: "wf pred_nat" |
|
162 |
apply (unfold wf_def pred_nat_def) |
|
163 |
apply clarify |
|
164 |
apply (induct_tac x) |
|
165 |
apply blast+ |
|
166 |
done |
|
167 |
||
168 |
lemma wf_less: "wf {(x, y::nat). x < y}" |
|
169 |
apply (unfold less_def) |
|
170 |
apply (rule wf_pred_nat [THEN wf_trancl, THEN wf_subset]) |
|
171 |
apply blast |
|
172 |
done |
|
173 |
||
174 |
lemma less_eq: "((m, n) : pred_nat^+) = (m < n)" |
|
175 |
apply (unfold less_def) |
|
176 |
apply (rule refl) |
|
177 |
done |
|
178 |
||
179 |
subsubsection {* Introduction properties *} |
|
180 |
||
181 |
lemma less_trans: "i < j ==> j < k ==> i < (k::nat)" |
|
182 |
apply (unfold less_def) |
|
183 |
apply (rule trans_trancl [THEN transD]) |
|
184 |
apply assumption+ |
|
185 |
done |
|
186 |
||
187 |
lemma lessI [iff]: "n < Suc n" |
|
188 |
apply (unfold less_def pred_nat_def) |
|
189 |
apply (simp add: r_into_trancl) |
|
190 |
done |
|
191 |
||
192 |
lemma less_SucI: "i < j ==> i < Suc j" |
|
193 |
apply (rule less_trans) |
|
194 |
apply assumption |
|
195 |
apply (rule lessI) |
|
196 |
done |
|
197 |
||
198 |
lemma zero_less_Suc [iff]: "0 < Suc n" |
|
199 |
apply (induct n) |
|
200 |
apply (rule lessI) |
|
201 |
apply (erule less_trans) |
|
202 |
apply (rule lessI) |
|
203 |
done |
|
204 |
||
205 |
subsubsection {* Elimination properties *} |
|
206 |
||
207 |
lemma less_not_sym: "n < m ==> ~ m < (n::nat)" |
|
208 |
apply (unfold less_def) |
|
209 |
apply (blast intro: wf_pred_nat wf_trancl [THEN wf_asym]) |
|
210 |
done |
|
211 |
||
212 |
lemma less_asym: |
|
213 |
assumes h1: "(n::nat) < m" and h2: "~ P ==> m < n" shows P |
|
214 |
apply (rule contrapos_np) |
|
215 |
apply (rule less_not_sym) |
|
216 |
apply (rule h1) |
|
217 |
apply (erule h2) |
|
218 |
done |
|
219 |
||
220 |
lemma less_not_refl: "~ n < (n::nat)" |
|
221 |
apply (unfold less_def) |
|
222 |
apply (rule wf_pred_nat [THEN wf_trancl, THEN wf_not_refl]) |
|
223 |
done |
|
224 |
||
225 |
lemma less_irrefl [elim!]: "(n::nat) < n ==> R" |
|
226 |
by (rule notE, rule less_not_refl) |
|
227 |
||
228 |
lemma less_not_refl2: "n < m ==> m ~= (n::nat)" by blast |
|
229 |
||
230 |
lemma less_not_refl3: "(s::nat) < t ==> s ~= t" |
|
231 |
by (rule not_sym, rule less_not_refl2) |
|
232 |
||
233 |
lemma lessE: |
|
234 |
assumes major: "i < k" |
|
235 |
and p1: "k = Suc i ==> P" and p2: "!!j. i < j ==> k = Suc j ==> P" |
|
236 |
shows P |
|
237 |
apply (rule major [unfolded less_def pred_nat_def, THEN tranclE]) |
|
238 |
apply simp_all |
|
239 |
apply (erule p1) |
|
240 |
apply (rule p2) |
|
241 |
apply (simp add: less_def pred_nat_def) |
|
242 |
apply assumption |
|
243 |
done |
|
244 |
||
245 |
lemma not_less0 [iff]: "~ n < (0::nat)" |
|
246 |
by (blast elim: lessE) |
|
247 |
||
248 |
lemma less_zeroE: "(n::nat) < 0 ==> R" |
|
249 |
by (rule notE, rule not_less0) |
|
250 |
||
251 |
lemma less_SucE: assumes major: "m < Suc n" |
|
252 |
and less: "m < n ==> P" and eq: "m = n ==> P" shows P |
|
253 |
apply (rule major [THEN lessE]) |
|
254 |
apply (rule eq) |
|
255 |
apply blast |
|
256 |
apply (rule less) |
|
257 |
apply blast |
|
258 |
done |
|
259 |
||
260 |
lemma less_Suc_eq: "(m < Suc n) = (m < n | m = n)" |
|
261 |
by (blast elim!: less_SucE intro: less_trans) |
|
262 |
||
263 |
lemma less_one [iff]: "(n < (1::nat)) = (n = 0)" |
|
264 |
by (simp add: less_Suc_eq) |
|
265 |
||
266 |
lemma less_Suc0 [iff]: "(n < Suc 0) = (n = 0)" |
|
267 |
by (simp add: less_Suc_eq) |
|
268 |
||
269 |
lemma Suc_mono: "m < n ==> Suc m < Suc n" |
|
270 |
by (induct n) (fast elim: less_trans lessE)+ |
|
271 |
||
272 |
text {* "Less than" is a linear ordering *} |
|
273 |
lemma less_linear: "m < n | m = n | n < (m::nat)" |
|
274 |
apply (induct_tac m) |
|
275 |
apply (induct_tac n) |
|
276 |
apply (rule refl [THEN disjI1, THEN disjI2]) |
|
277 |
apply (rule zero_less_Suc [THEN disjI1]) |
|
278 |
apply (blast intro: Suc_mono less_SucI elim: lessE) |
|
279 |
done |
|
280 |
||
281 |
lemma nat_neq_iff: "((m::nat) ~= n) = (m < n | n < m)" |
|
282 |
using less_linear by blast |
|
283 |
||
284 |
lemma nat_less_cases: assumes major: "(m::nat) < n ==> P n m" |
|
285 |
and eqCase: "m = n ==> P n m" and lessCase: "n<m ==> P n m" |
|
286 |
shows "P n m" |
|
287 |
apply (rule less_linear [THEN disjE]) |
|
288 |
apply (erule_tac [2] disjE) |
|
289 |
apply (erule lessCase) |
|
290 |
apply (erule sym [THEN eqCase]) |
|
291 |
apply (erule major) |
|
292 |
done |
|
293 |
||
294 |
||
295 |
subsubsection {* Inductive (?) properties *} |
|
296 |
||
297 |
lemma Suc_lessI: "m < n ==> Suc m ~= n ==> Suc m < n" |
|
298 |
apply (simp add: nat_neq_iff) |
|
299 |
apply (blast elim!: less_irrefl less_SucE elim: less_asym) |
|
300 |
done |
|
301 |
||
302 |
lemma Suc_lessD: "Suc m < n ==> m < n" |
|
303 |
apply (induct n) |
|
304 |
apply (fast intro!: lessI [THEN less_SucI] elim: less_trans lessE)+ |
|
305 |
done |
|
306 |
||
307 |
lemma Suc_lessE: assumes major: "Suc i < k" |
|
308 |
and minor: "!!j. i < j ==> k = Suc j ==> P" shows P |
|
309 |
apply (rule major [THEN lessE]) |
|
310 |
apply (erule lessI [THEN minor]) |
|
311 |
apply (erule Suc_lessD [THEN minor]) |
|
312 |
apply assumption |
|
313 |
done |
|
314 |
||
315 |
lemma Suc_less_SucD: "Suc m < Suc n ==> m < n" |
|
316 |
by (blast elim: lessE dest: Suc_lessD) |
|
4104 | 317 |
|
13449 | 318 |
lemma Suc_less_eq [iff]: "(Suc m < Suc n) = (m < n)" |
319 |
apply (rule iffI) |
|
320 |
apply (erule Suc_less_SucD) |
|
321 |
apply (erule Suc_mono) |
|
322 |
done |
|
323 |
||
324 |
lemma less_trans_Suc: |
|
325 |
assumes le: "i < j" shows "j < k ==> Suc i < k" |
|
326 |
apply (induct k) |
|
327 |
apply simp_all |
|
328 |
apply (insert le) |
|
329 |
apply (simp add: less_Suc_eq) |
|
330 |
apply (blast dest: Suc_lessD) |
|
331 |
done |
|
332 |
||
333 |
text {* Can be used with @{text less_Suc_eq} to get @{term "n = m | n < m"} *} |
|
334 |
lemma not_less_eq: "(~ m < n) = (n < Suc m)" |
|
335 |
apply (rule_tac m = "m" and n = "n" in diff_induct) |
|
336 |
apply simp_all |
|
337 |
done |
|
338 |
||
339 |
text {* Complete induction, aka course-of-values induction *} |
|
340 |
lemma nat_less_induct: |
|
341 |
assumes prem: "!!n. ALL m::nat. m < n --> P m ==> P n" shows "P n" |
|
342 |
apply (rule_tac a=n in wf_induct) |
|
343 |
apply (rule wf_pred_nat [THEN wf_trancl]) |
|
344 |
apply (rule prem) |
|
345 |
apply (unfold less_def) |
|
346 |
apply assumption |
|
347 |
done |
|
348 |
||
349 |
subsection {* Properties of "less or equal than" *} |
|
350 |
||
351 |
text {* Was @{text le_eq_less_Suc}, but this orientation is more useful *} |
|
352 |
lemma less_Suc_eq_le: "(m < Suc n) = (m <= n)" |
|
353 |
by (unfold le_def, rule not_less_eq [symmetric]) |
|
354 |
||
355 |
lemma le_imp_less_Suc: "m <= n ==> m < Suc n" |
|
356 |
by (rule less_Suc_eq_le [THEN iffD2]) |
|
357 |
||
358 |
lemma le0 [iff]: "(0::nat) <= n" |
|
359 |
by (unfold le_def, rule not_less0) |
|
360 |
||
361 |
lemma Suc_n_not_le_n: "~ Suc n <= n" |
|
362 |
by (simp add: le_def) |
|
363 |
||
364 |
lemma le_0_eq [iff]: "((i::nat) <= 0) = (i = 0)" |
|
365 |
by (induct i) (simp_all add: le_def) |
|
366 |
||
367 |
lemma le_Suc_eq: "(m <= Suc n) = (m <= n | m = Suc n)" |
|
368 |
by (simp del: less_Suc_eq_le add: less_Suc_eq_le [symmetric] less_Suc_eq) |
|
369 |
||
370 |
lemma le_SucE: "m <= Suc n ==> (m <= n ==> R) ==> (m = Suc n ==> R) ==> R" |
|
371 |
by (drule le_Suc_eq [THEN iffD1], rules+) |
|
372 |
||
373 |
lemma leI: "~ n < m ==> m <= (n::nat)" by (simp add: le_def) |
|
374 |
||
375 |
lemma leD: "m <= n ==> ~ n < (m::nat)" |
|
376 |
by (simp add: le_def) |
|
377 |
||
378 |
lemmas leE = leD [elim_format] |
|
379 |
||
380 |
lemma not_less_iff_le: "(~ n < m) = (m <= (n::nat))" |
|
381 |
by (blast intro: leI elim: leE) |
|
382 |
||
383 |
lemma not_leE: "~ m <= n ==> n<(m::nat)" |
|
384 |
by (simp add: le_def) |
|
385 |
||
386 |
lemma not_le_iff_less: "(~ n <= m) = (m < (n::nat))" |
|
387 |
by (simp add: le_def) |
|
388 |
||
389 |
lemma Suc_leI: "m < n ==> Suc(m) <= n" |
|
390 |
apply (simp add: le_def less_Suc_eq) |
|
391 |
apply (blast elim!: less_irrefl less_asym) |
|
392 |
done -- {* formerly called lessD *} |
|
393 |
||
394 |
lemma Suc_leD: "Suc(m) <= n ==> m <= n" |
|
395 |
by (simp add: le_def less_Suc_eq) |
|
396 |
||
397 |
text {* Stronger version of @{text Suc_leD} *} |
|
398 |
lemma Suc_le_lessD: "Suc m <= n ==> m < n" |
|
399 |
apply (simp add: le_def less_Suc_eq) |
|
400 |
using less_linear |
|
401 |
apply blast |
|
402 |
done |
|
403 |
||
404 |
lemma Suc_le_eq: "(Suc m <= n) = (m < n)" |
|
405 |
by (blast intro: Suc_leI Suc_le_lessD) |
|
406 |
||
407 |
lemma le_SucI: "m <= n ==> m <= Suc n" |
|
408 |
by (unfold le_def) (blast dest: Suc_lessD) |
|
409 |
||
410 |
lemma less_imp_le: "m < n ==> m <= (n::nat)" |
|
411 |
by (unfold le_def) (blast elim: less_asym) |
|
412 |
||
413 |
text {* For instance, @{text "(Suc m < Suc n) = (Suc m <= n) = (m < n)"} *} |
|
414 |
lemmas le_simps = less_imp_le less_Suc_eq_le Suc_le_eq |
|
415 |
||
416 |
||
417 |
text {* Equivalence of @{term "m <= n"} and @{term "m < n | m = n"} *} |
|
418 |
||
419 |
lemma le_imp_less_or_eq: "m <= n ==> m < n | m = (n::nat)" |
|
420 |
apply (unfold le_def) |
|
421 |
using less_linear |
|
422 |
apply (blast elim: less_irrefl less_asym) |
|
423 |
done |
|
424 |
||
425 |
lemma less_or_eq_imp_le: "m < n | m = n ==> m <= (n::nat)" |
|
426 |
apply (unfold le_def) |
|
427 |
using less_linear |
|
428 |
apply (blast elim!: less_irrefl elim: less_asym) |
|
429 |
done |
|
430 |
||
431 |
lemma le_eq_less_or_eq: "(m <= (n::nat)) = (m < n | m=n)" |
|
432 |
by (rules intro: less_or_eq_imp_le le_imp_less_or_eq) |
|
433 |
||
434 |
text {* Useful with @{text Blast}. *} |
|
435 |
lemma eq_imp_le: "(m::nat) = n ==> m <= n" |
|
436 |
by (rule less_or_eq_imp_le, rule disjI2) |
|
437 |
||
438 |
lemma le_refl: "n <= (n::nat)" |
|
439 |
by (simp add: le_eq_less_or_eq) |
|
440 |
||
441 |
lemma le_less_trans: "[| i <= j; j < k |] ==> i < (k::nat)" |
|
442 |
by (blast dest!: le_imp_less_or_eq intro: less_trans) |
|
443 |
||
444 |
lemma less_le_trans: "[| i < j; j <= k |] ==> i < (k::nat)" |
|
445 |
by (blast dest!: le_imp_less_or_eq intro: less_trans) |
|
446 |
||
447 |
lemma le_trans: "[| i <= j; j <= k |] ==> i <= (k::nat)" |
|
448 |
by (blast dest!: le_imp_less_or_eq intro: less_or_eq_imp_le less_trans) |
|
449 |
||
450 |
lemma le_anti_sym: "[| m <= n; n <= m |] ==> m = (n::nat)" |
|
451 |
-- {* @{text order_less_irrefl} could make this proof fail *} |
|
452 |
by (blast dest!: le_imp_less_or_eq elim!: less_irrefl elim: less_asym) |
|
453 |
||
454 |
lemma Suc_le_mono [iff]: "(Suc n <= Suc m) = (n <= m)" |
|
455 |
by (simp add: le_simps) |
|
456 |
||
457 |
text {* Axiom @{text order_less_le} of class @{text order}: *} |
|
458 |
lemma nat_less_le: "((m::nat) < n) = (m <= n & m ~= n)" |
|
459 |
by (simp add: le_def nat_neq_iff) (blast elim!: less_asym) |
|
460 |
||
461 |
lemma le_neq_implies_less: "(m::nat) <= n ==> m ~= n ==> m < n" |
|
462 |
by (rule iffD2, rule nat_less_le, rule conjI) |
|
463 |
||
464 |
text {* Axiom @{text linorder_linear} of class @{text linorder}: *} |
|
465 |
lemma nat_le_linear: "(m::nat) <= n | n <= m" |
|
466 |
apply (simp add: le_eq_less_or_eq) |
|
467 |
using less_linear |
|
468 |
apply blast |
|
469 |
done |
|
470 |
||
471 |
lemma not_less_less_Suc_eq: "~ n < m ==> (n < Suc m) = (n = m)" |
|
472 |
by (blast elim!: less_SucE) |
|
473 |
||
474 |
||
475 |
text {* |
|
476 |
Rewrite @{term "n < Suc m"} to @{term "n = m"} |
|
477 |
if @{term "~ n < m"} or @{term "m <= n"} hold. |
|
478 |
Not suitable as default simprules because they often lead to looping |
|
479 |
*} |
|
480 |
lemma le_less_Suc_eq: "m <= n ==> (n < Suc m) = (n = m)" |
|
481 |
by (rule not_less_less_Suc_eq, rule leD) |
|
482 |
||
483 |
lemmas not_less_simps = not_less_less_Suc_eq le_less_Suc_eq |
|
484 |
||
485 |
||
486 |
text {* |
|
487 |
Re-orientation of the equations @{text "0 = x"} and @{text "1 = x"}. |
|
488 |
No longer added as simprules (they loop) |
|
489 |
but via @{text reorient_simproc} in Bin |
|
490 |
*} |
|
491 |
||
492 |
text {* Polymorphic, not just for @{typ nat} *} |
|
493 |
lemma zero_reorient: "(0 = x) = (x = 0)" |
|
494 |
by auto |
|
495 |
||
496 |
lemma one_reorient: "(1 = x) = (x = 1)" |
|
497 |
by auto |
|
498 |
||
499 |
text {* Type {@typ nat} is a wellfounded linear order *} |
|
500 |
||
501 |
instance nat :: order by (intro_classes, |
|
502 |
(assumption | rule le_refl le_trans le_anti_sym nat_less_le)+) |
|
503 |
instance nat :: linorder by (intro_classes, rule nat_le_linear) |
|
504 |
instance nat :: wellorder by (intro_classes, rule wf_less) |
|
505 |
||
506 |
subsection {* Arithmetic operators *} |
|
1660 | 507 |
|
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
11451
diff
changeset
|
508 |
axclass power < type |
10435 | 509 |
|
3370
5c5fdce3a4e4
Overloading of "^" requires new type class "power", with types "nat" and
paulson
parents:
2608
diff
changeset
|
510 |
consts |
13449 | 511 |
power :: "('a::power) => nat => 'a" (infixr "^" 80) |
3370
5c5fdce3a4e4
Overloading of "^" requires new type class "power", with types "nat" and
paulson
parents:
2608
diff
changeset
|
512 |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
513 |
|
13449 | 514 |
text {* arithmetic operators @{text "+ -"} and @{text "*"} *} |
515 |
||
516 |
instance nat :: plus .. |
|
517 |
instance nat :: minus .. |
|
518 |
instance nat :: times .. |
|
519 |
instance nat :: power .. |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
520 |
|
13449 | 521 |
text {* size of a datatype value; overloaded *} |
522 |
consts size :: "'a => nat" |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
523 |
|
13449 | 524 |
primrec |
525 |
add_0: "0 + n = n" |
|
526 |
add_Suc: "Suc m + n = Suc (m + n)" |
|
527 |
||
528 |
primrec |
|
529 |
diff_0: "m - 0 = m" |
|
530 |
diff_Suc: "m - Suc n = (case m - n of 0 => 0 | Suc k => k)" |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
531 |
|
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
532 |
primrec |
13449 | 533 |
mult_0: "0 * n = 0" |
534 |
mult_Suc: "Suc m * n = n + (m * n)" |
|
535 |
||
536 |
text {* These 2 rules ease the use of primitive recursion. NOTE USE OF @{text "=="} *} |
|
537 |
lemma def_nat_rec_0: "(!!n. f n == nat_rec c h n) ==> f 0 = c" |
|
538 |
by simp |
|
539 |
||
540 |
lemma def_nat_rec_Suc: "(!!n. f n == nat_rec c h n) ==> f (Suc n) = h n (f n)" |
|
541 |
by simp |
|
542 |
||
543 |
lemma not0_implies_Suc: "n ~= 0 ==> EX m. n = Suc m" |
|
544 |
by (case_tac n) simp_all |
|
545 |
||
546 |
lemma gr_implies_not0: "!!n::nat. m<n ==> n ~= 0" |
|
547 |
by (case_tac n) simp_all |
|
548 |
||
549 |
lemma neq0_conv [iff]: "!!n::nat. (n ~= 0) = (0 < n)" |
|
550 |
by (case_tac n) simp_all |
|
551 |
||
552 |
text {* This theorem is useful with @{text blast} *} |
|
553 |
lemma gr0I: "((n::nat) = 0 ==> False) ==> 0 < n" |
|
554 |
by (rule iffD1, rule neq0_conv, rules) |
|
555 |
||
556 |
lemma gr0_conv_Suc: "(0 < n) = (EX m. n = Suc m)" |
|
557 |
by (fast intro: not0_implies_Suc) |
|
558 |
||
559 |
lemma not_gr0 [iff]: "!!n::nat. (~ (0 < n)) = (n = 0)" |
|
560 |
apply (rule iffI) |
|
561 |
apply (rule ccontr) |
|
562 |
apply simp_all |
|
563 |
done |
|
564 |
||
565 |
lemma Suc_le_D: "(Suc n <= m') ==> (? m. m' = Suc m)" |
|
566 |
by (induct m') simp_all |
|
567 |
||
568 |
text {* Useful in certain inductive arguments *} |
|
569 |
lemma less_Suc_eq_0_disj: "(m < Suc n) = (m = 0 | (EX j. m = Suc j & j < n))" |
|
570 |
by (case_tac m) simp_all |
|
571 |
||
572 |
lemma nat_induct2: "P 0 ==> P (Suc 0) ==> (!!k. P k ==> P (Suc (Suc k))) ==> P n" |
|
573 |
apply (rule nat_less_induct) |
|
574 |
apply (case_tac n) |
|
575 |
apply (case_tac [2] nat) |
|
576 |
apply (blast intro: less_trans)+ |
|
577 |
done |
|
578 |
||
579 |
subsection {* @{text LEAST} theorems for type @{typ nat} by specialization *} |
|
580 |
||
581 |
lemmas LeastI = wellorder_LeastI |
|
582 |
lemmas Least_le = wellorder_Least_le |
|
583 |
lemmas not_less_Least = wellorder_not_less_Least |
|
584 |
||
585 |
lemma Least_Suc: "[| P n; ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))" |
|
586 |
apply (case_tac "n") |
|
587 |
apply auto |
|
588 |
apply (frule LeastI) |
|
589 |
apply (drule_tac P = "%x. P (Suc x) " in LeastI) |
|
590 |
apply (subgoal_tac " (LEAST x. P x) <= Suc (LEAST x. P (Suc x))") |
|
591 |
apply (erule_tac [2] Least_le) |
|
592 |
apply (case_tac "LEAST x. P x") |
|
593 |
apply auto |
|
594 |
apply (drule_tac P = "%x. P (Suc x) " in Least_le) |
|
595 |
apply (blast intro: order_antisym) |
|
596 |
done |
|
597 |
||
598 |
lemma Least_Suc2: "[|P n; Q m; ~P 0; !k. P (Suc k) = Q k|] ==> Least P = Suc (Least Q)" |
|
599 |
apply (erule (1) Least_Suc [THEN ssubst]) |
|
600 |
apply simp |
|
601 |
done |
|
602 |
||
603 |
||
604 |
subsection {* @{term min} and @{term max} *} |
|
605 |
||
606 |
lemma min_0L [simp]: "min 0 n = (0::nat)" |
|
607 |
by (rule min_leastL) simp |
|
608 |
||
609 |
lemma min_0R [simp]: "min n 0 = (0::nat)" |
|
610 |
by (rule min_leastR) simp |
|
611 |
||
612 |
lemma min_Suc_Suc [simp]: "min (Suc m) (Suc n) = Suc (min m n)" |
|
613 |
by (simp add: min_of_mono) |
|
614 |
||
615 |
lemma max_0L [simp]: "max 0 n = (n::nat)" |
|
616 |
by (rule max_leastL) simp |
|
617 |
||
618 |
lemma max_0R [simp]: "max n 0 = (n::nat)" |
|
619 |
by (rule max_leastR) simp |
|
620 |
||
621 |
lemma max_Suc_Suc [simp]: "max (Suc m) (Suc n) = Suc(max m n)" |
|
622 |
by (simp add: max_of_mono) |
|
623 |
||
624 |
||
625 |
subsection {* Basic rewrite rules for the arithmetic operators *} |
|
626 |
||
627 |
text {* Difference *} |
|
628 |
||
629 |
lemma diff_0_eq_0 [simp]: "0 - n = (0::nat)" |
|
630 |
by (induct_tac n) simp_all |
|
631 |
||
632 |
lemma diff_Suc_Suc [simp]: "Suc(m) - Suc(n) = m - n" |
|
633 |
by (induct_tac n) simp_all |
|
634 |
||
635 |
||
636 |
text {* |
|
637 |
Could be (and is, below) generalized in various ways |
|
638 |
However, none of the generalizations are currently in the simpset, |
|
639 |
and I dread to think what happens if I put them in |
|
640 |
*} |
|
641 |
lemma Suc_pred [simp]: "0 < n ==> Suc (n - Suc 0) = n" |
|
642 |
by (simp split add: nat.split) |
|
643 |
||
644 |
declare diff_Suc [simp del] |
|
645 |
||
646 |
||
647 |
subsection {* Addition *} |
|
648 |
||
649 |
lemma add_0_right [simp]: "m + 0 = (m::nat)" |
|
650 |
by (induct m) simp_all |
|
651 |
||
652 |
lemma add_Suc_right [simp]: "m + Suc n = Suc (m + n)" |
|
653 |
by (induct m) simp_all |
|
654 |
||
655 |
||
656 |
text {* Associative law for addition *} |
|
657 |
lemma add_assoc: "(m + n) + k = m + ((n + k)::nat)" |
|
658 |
by (induct m) simp_all |
|
659 |
||
660 |
text {* Commutative law for addition *} |
|
661 |
lemma add_commute: "m + n = n + (m::nat)" |
|
662 |
by (induct m) simp_all |
|
663 |
||
664 |
lemma add_left_commute: "x + (y + z) = y + ((x + z)::nat)" |
|
665 |
apply (rule mk_left_commute [of "op +"]) |
|
666 |
apply (rule add_assoc) |
|
667 |
apply (rule add_commute) |
|
668 |
done |
|
669 |
||
670 |
text {* Addition is an AC-operator *} |
|
671 |
lemmas add_ac = add_assoc add_commute add_left_commute |
|
672 |
||
673 |
lemma add_left_cancel [simp]: "(k + m = k + n) = (m = (n::nat))" |
|
674 |
by (induct k) simp_all |
|
675 |
||
676 |
lemma add_right_cancel [simp]: "(m + k = n + k) = (m=(n::nat))" |
|
677 |
by (induct k) simp_all |
|
678 |
||
679 |
lemma add_left_cancel_le [simp]: "(k + m <= k + n) = (m<=(n::nat))" |
|
680 |
by (induct k) simp_all |
|
681 |
||
682 |
lemma add_left_cancel_less [simp]: "(k + m < k + n) = (m<(n::nat))" |
|
683 |
by (induct k) simp_all |
|
684 |
||
685 |
text {* Reasoning about @{text "m + 0 = 0"}, etc. *} |
|
686 |
||
687 |
lemma add_is_0 [iff]: "!!m::nat. (m + n = 0) = (m = 0 & n = 0)" |
|
688 |
by (case_tac m) simp_all |
|
689 |
||
690 |
lemma add_is_1: "(m+n= Suc 0) = (m= Suc 0 & n=0 | m=0 & n= Suc 0)" |
|
691 |
by (case_tac m) simp_all |
|
692 |
||
693 |
lemma one_is_add: "(Suc 0 = m + n) = (m = Suc 0 & n = 0 | m = 0 & n = Suc 0)" |
|
694 |
by (rule trans, rule eq_commute, rule add_is_1) |
|
695 |
||
696 |
lemma add_gr_0 [iff]: "!!m::nat. (0 < m + n) = (0 < m | 0 < n)" |
|
697 |
by (simp del: neq0_conv add: neq0_conv [symmetric]) |
|
698 |
||
699 |
lemma add_eq_self_zero: "!!m::nat. m + n = m ==> n = 0" |
|
700 |
apply (drule add_0_right [THEN ssubst]) |
|
701 |
apply (simp add: add_assoc del: add_0_right) |
|
702 |
done |
|
703 |
||
704 |
subsection {* Additional theorems about "less than" *} |
|
705 |
||
706 |
text {* Deleted @{text less_natE}; instead use @{text "less_imp_Suc_add RS exE"} *} |
|
707 |
lemma less_imp_Suc_add: "m < n ==> (EX k. n = Suc (m + k))" |
|
708 |
apply (induct n) |
|
709 |
apply (simp_all add: order_le_less) |
|
710 |
apply (blast elim!: less_SucE intro!: add_0_right [symmetric] add_Suc_right [symmetric]) |
|
711 |
done |
|
712 |
||
713 |
lemma le_add2: "n <= ((m + n)::nat)" |
|
714 |
apply (induct m) |
|
715 |
apply simp_all |
|
716 |
apply (erule le_SucI) |
|
717 |
done |
|
718 |
||
719 |
lemma le_add1: "n <= ((n + m)::nat)" |
|
720 |
apply (simp add: add_ac) |
|
721 |
apply (rule le_add2) |
|
722 |
done |
|
723 |
||
724 |
lemma less_add_Suc1: "i < Suc (i + m)" |
|
725 |
by (rule le_less_trans, rule le_add1, rule lessI) |
|
726 |
||
727 |
lemma less_add_Suc2: "i < Suc (m + i)" |
|
728 |
by (rule le_less_trans, rule le_add2, rule lessI) |
|
729 |
||
730 |
lemma less_iff_Suc_add: "(m < n) = (EX k. n = Suc (m + k))" |
|
731 |
by (rules intro!: less_add_Suc1 less_imp_Suc_add) |
|
732 |
||
733 |
||
734 |
lemma trans_le_add1: "(i::nat) <= j ==> i <= j + m" |
|
735 |
by (rule le_trans, assumption, rule le_add1) |
|
736 |
||
737 |
lemma trans_le_add2: "(i::nat) <= j ==> i <= m + j" |
|
738 |
by (rule le_trans, assumption, rule le_add2) |
|
739 |
||
740 |
lemma trans_less_add1: "(i::nat) < j ==> i < j + m" |
|
741 |
by (rule less_le_trans, assumption, rule le_add1) |
|
742 |
||
743 |
lemma trans_less_add2: "(i::nat) < j ==> i < m + j" |
|
744 |
by (rule less_le_trans, assumption, rule le_add2) |
|
745 |
||
746 |
lemma add_lessD1: "i + j < (k::nat) ==> i < k" |
|
747 |
apply (induct j) |
|
748 |
apply simp_all |
|
749 |
apply (blast dest: Suc_lessD) |
|
750 |
done |
|
751 |
||
752 |
lemma not_add_less1 [iff]: "~ (i + j < (i::nat))" |
|
753 |
apply (rule notI) |
|
754 |
apply (erule add_lessD1 [THEN less_irrefl]) |
|
755 |
done |
|
756 |
||
757 |
lemma not_add_less2 [iff]: "~ (j + i < (i::nat))" |
|
758 |
by (simp add: add_commute not_add_less1) |
|
759 |
||
760 |
lemma add_leD1: "m + k <= n ==> m <= (n::nat)" |
|
761 |
by (induct k) (simp_all add: le_simps) |
|
762 |
||
763 |
lemma add_leD2: "m + k <= n ==> k <= (n::nat)" |
|
764 |
apply (simp add: add_commute) |
|
765 |
apply (erule add_leD1) |
|
766 |
done |
|
767 |
||
768 |
lemma add_leE: "(m::nat) + k <= n ==> (m <= n ==> k <= n ==> R) ==> R" |
|
769 |
by (blast dest: add_leD1 add_leD2) |
|
770 |
||
771 |
text {* needs @{text "!!k"} for @{text add_ac} to work *} |
|
772 |
lemma less_add_eq_less: "!!k::nat. k < l ==> m + l = k + n ==> m < n" |
|
773 |
by (force simp del: add_Suc_right |
|
774 |
simp add: less_iff_Suc_add add_Suc_right [symmetric] add_ac) |
|
775 |
||
776 |
||
777 |
subsection {* Monotonicity of Addition *} |
|
778 |
||
779 |
text {* strict, in 1st argument *} |
|
780 |
lemma add_less_mono1: "i < j ==> i + k < j + (k::nat)" |
|
781 |
by (induct k) simp_all |
|
782 |
||
783 |
text {* strict, in both arguments *} |
|
784 |
lemma add_less_mono: "[|i < j; k < l|] ==> i + k < j + (l::nat)" |
|
785 |
apply (rule add_less_mono1 [THEN less_trans]) |
|
786 |
apply assumption+ |
|
787 |
apply (induct_tac j) |
|
788 |
apply simp_all |
|
789 |
done |
|
790 |
||
791 |
text {* A [clumsy] way of lifting @{text "<"} |
|
792 |
monotonicity to @{text "<="} monotonicity *} |
|
793 |
lemma less_mono_imp_le_mono: |
|
794 |
assumes lt_mono: "!!i j::nat. i < j ==> f i < f j" |
|
795 |
and le: "i <= j" shows "f i <= ((f j)::nat)" using le |
|
796 |
apply (simp add: order_le_less) |
|
797 |
apply (blast intro!: lt_mono) |
|
798 |
done |
|
799 |
||
800 |
text {* non-strict, in 1st argument *} |
|
801 |
lemma add_le_mono1: "i <= j ==> i + k <= j + (k::nat)" |
|
802 |
apply (rule_tac f = "%j. j + k" in less_mono_imp_le_mono) |
|
803 |
apply (erule add_less_mono1) |
|
804 |
apply assumption |
|
805 |
done |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
806 |
|
13449 | 807 |
text {* non-strict, in both arguments *} |
808 |
lemma add_le_mono: "[| i <= j; k <= l |] ==> i + k <= j + (l::nat)" |
|
809 |
apply (erule add_le_mono1 [THEN le_trans]) |
|
810 |
apply (simp add: add_commute) |
|
811 |
done |
|
812 |
||
813 |
||
814 |
subsection {* Multiplication *} |
|
815 |
||
816 |
text {* right annihilation in product *} |
|
817 |
lemma mult_0_right [simp]: "(m::nat) * 0 = 0" |
|
818 |
by (induct m) simp_all |
|
819 |
||
820 |
text {* right successor law for multiplication *} |
|
821 |
lemma mult_Suc_right [simp]: "m * Suc n = m + (m * n)" |
|
822 |
by (induct m) (simp_all add: add_ac) |
|
823 |
||
824 |
lemma mult_1: "(1::nat) * n = n" by simp |
|
825 |
||
826 |
lemma mult_1_right: "n * (1::nat) = n" by simp |
|
827 |
||
828 |
text {* Commutative law for multiplication *} |
|
829 |
lemma mult_commute: "m * n = n * (m::nat)" |
|
830 |
by (induct m) simp_all |
|
831 |
||
832 |
text {* addition distributes over multiplication *} |
|
833 |
lemma add_mult_distrib: "(m + n) * k = (m * k) + ((n * k)::nat)" |
|
834 |
by (induct m) (simp_all add: add_ac) |
|
835 |
||
836 |
lemma add_mult_distrib2: "k * (m + n) = (k * m) + ((k * n)::nat)" |
|
837 |
by (induct m) (simp_all add: add_ac) |
|
838 |
||
839 |
text {* Associative law for multiplication *} |
|
840 |
lemma mult_assoc: "(m * n) * k = m * ((n * k)::nat)" |
|
841 |
by (induct m) (simp_all add: add_mult_distrib) |
|
842 |
||
843 |
lemma mult_left_commute: "x * (y * z) = y * ((x * z)::nat)" |
|
844 |
apply (rule mk_left_commute [of "op *"]) |
|
845 |
apply (rule mult_assoc) |
|
846 |
apply (rule mult_commute) |
|
847 |
done |
|
848 |
||
849 |
lemmas mult_ac = mult_assoc mult_commute mult_left_commute |
|
850 |
||
851 |
lemma mult_is_0 [simp]: "((m::nat) * n = 0) = (m=0 | n=0)" |
|
852 |
apply (induct_tac m) |
|
853 |
apply (induct_tac [2] n) |
|
854 |
apply simp_all |
|
855 |
done |
|
856 |
||
857 |
||
858 |
subsection {* Difference *} |
|
859 |
||
860 |
lemma diff_self_eq_0 [simp]: "(m::nat) - m = 0" |
|
861 |
by (induct m) simp_all |
|
862 |
||
863 |
text {* Addition is the inverse of subtraction: |
|
864 |
if @{term "n <= m"} then @{term "n + (m - n) = m"}. *} |
|
865 |
lemma add_diff_inverse: "~ m < n ==> n + (m - n) = (m::nat)" |
|
866 |
by (induct m n rule: diff_induct) simp_all |
|
867 |
||
868 |
lemma le_add_diff_inverse [simp]: "n <= m ==> n + (m - n) = (m::nat)" |
|
869 |
by (simp add: add_diff_inverse not_less_iff_le) |
|
870 |
||
871 |
lemma le_add_diff_inverse2 [simp]: "n <= m ==> (m - n) + n = (m::nat)" |
|
872 |
by (simp add: le_add_diff_inverse add_commute) |
|
873 |
||
874 |
||
875 |
subsection {* More results about difference *} |
|
876 |
||
877 |
lemma Suc_diff_le: "n <= m ==> Suc m - n = Suc (m - n)" |
|
878 |
by (induct m n rule: diff_induct) simp_all |
|
879 |
||
880 |
lemma diff_less_Suc: "m - n < Suc m" |
|
881 |
apply (induct m n rule: diff_induct) |
|
882 |
apply (erule_tac [3] less_SucE) |
|
883 |
apply (simp_all add: less_Suc_eq) |
|
884 |
done |
|
885 |
||
886 |
lemma diff_le_self [simp]: "m - n <= (m::nat)" |
|
887 |
by (induct m n rule: diff_induct) (simp_all add: le_SucI) |
|
888 |
||
889 |
lemma less_imp_diff_less: "(j::nat) < k ==> j - n < k" |
|
890 |
by (rule le_less_trans, rule diff_le_self) |
|
891 |
||
892 |
lemma diff_diff_left: "(i::nat) - j - k = i - (j + k)" |
|
893 |
by (induct i j rule: diff_induct) simp_all |
|
894 |
||
895 |
lemma Suc_diff_diff [simp]: "(Suc m - n) - Suc k = m - n - k" |
|
896 |
by (simp add: diff_diff_left) |
|
897 |
||
898 |
lemma diff_Suc_less [simp]: "0<n ==> n - Suc i < n" |
|
899 |
apply (case_tac "n") |
|
900 |
apply safe |
|
901 |
apply (simp add: le_simps) |
|
902 |
done |
|
903 |
||
904 |
text {* This and the next few suggested by Florian Kammueller *} |
|
905 |
lemma diff_commute: "(i::nat) - j - k = i - k - j" |
|
906 |
by (simp add: diff_diff_left add_commute) |
|
907 |
||
908 |
lemma diff_add_assoc: "k <= (j::nat) ==> (i + j) - k = i + (j - k)" |
|
909 |
by (induct j k rule: diff_induct) simp_all |
|
910 |
||
911 |
lemma diff_add_assoc2: "k <= (j::nat) ==> (j + i) - k = (j - k) + i" |
|
912 |
by (simp add: add_commute diff_add_assoc) |
|
913 |
||
914 |
lemma diff_add_inverse: "(n + m) - n = (m::nat)" |
|
915 |
by (induct n) simp_all |
|
916 |
||
917 |
lemma diff_add_inverse2: "(m + n) - n = (m::nat)" |
|
918 |
by (simp add: diff_add_assoc) |
|
919 |
||
920 |
lemma le_imp_diff_is_add: "i <= (j::nat) ==> (j - i = k) = (j = k + i)" |
|
921 |
apply safe |
|
922 |
apply (simp_all add: diff_add_inverse2) |
|
923 |
done |
|
924 |
||
925 |
lemma diff_is_0_eq [simp]: "((m::nat) - n = 0) = (m <= n)" |
|
926 |
by (induct m n rule: diff_induct) simp_all |
|
927 |
||
928 |
lemma diff_is_0_eq' [simp]: "m <= n ==> (m::nat) - n = 0" |
|
929 |
by (rule iffD2, rule diff_is_0_eq) |
|
930 |
||
931 |
lemma zero_less_diff [simp]: "(0 < n - (m::nat)) = (m < n)" |
|
932 |
by (induct m n rule: diff_induct) simp_all |
|
933 |
||
934 |
lemma less_imp_add_positive: "i < j ==> EX k::nat. 0 < k & i + k = j" |
|
935 |
apply (rule_tac x = "j - i" in exI) |
|
936 |
apply (simp (no_asm_simp) add: add_diff_inverse less_not_sym) |
|
937 |
done |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
938 |
|
13449 | 939 |
lemma zero_induct_lemma: "P k ==> (!!n. P (Suc n) ==> P n) ==> P (k - i)" |
940 |
apply (induct k i rule: diff_induct) |
|
941 |
apply (simp_all (no_asm)) |
|
942 |
apply rules |
|
943 |
done |
|
944 |
||
945 |
lemma zero_induct: "P k ==> (!!n. P (Suc n) ==> P n) ==> P 0" |
|
946 |
apply (rule diff_self_eq_0 [THEN subst]) |
|
947 |
apply (rule zero_induct_lemma) |
|
948 |
apply rules+ |
|
949 |
done |
|
950 |
||
951 |
lemma diff_cancel: "(k + m) - (k + n) = m - (n::nat)" |
|
952 |
by (induct k) simp_all |
|
953 |
||
954 |
lemma diff_cancel2: "(m + k) - (n + k) = m - (n::nat)" |
|
955 |
by (simp add: diff_cancel add_commute) |
|
956 |
||
957 |
lemma diff_add_0: "n - (n + m) = (0::nat)" |
|
958 |
by (induct n) simp_all |
|
959 |
||
960 |
||
961 |
text {* Difference distributes over multiplication *} |
|
962 |
||
963 |
lemma diff_mult_distrib: "((m::nat) - n) * k = (m * k) - (n * k)" |
|
964 |
by (induct m n rule: diff_induct) (simp_all add: diff_cancel) |
|
965 |
||
966 |
lemma diff_mult_distrib2: "k * ((m::nat) - n) = (k * m) - (k * n)" |
|
967 |
by (simp add: diff_mult_distrib mult_commute [of k]) |
|
968 |
-- {* NOT added as rewrites, since sometimes they are used from right-to-left *} |
|
969 |
||
970 |
lemmas nat_distrib = |
|
971 |
add_mult_distrib add_mult_distrib2 diff_mult_distrib diff_mult_distrib2 |
|
972 |
||
973 |
||
974 |
subsection {* Monotonicity of Multiplication *} |
|
975 |
||
976 |
lemma mult_le_mono1: "i <= (j::nat) ==> i * k <= j * k" |
|
977 |
by (induct k) (simp_all add: add_le_mono) |
|
978 |
||
979 |
lemma mult_le_mono2: "i <= (j::nat) ==> k * i <= k * j" |
|
980 |
apply (drule mult_le_mono1) |
|
981 |
apply (simp add: mult_commute) |
|
982 |
done |
|
983 |
||
984 |
text {* @{text "<="} monotonicity, BOTH arguments *} |
|
985 |
lemma mult_le_mono: "i <= (j::nat) ==> k <= l ==> i * k <= j * l" |
|
986 |
apply (erule mult_le_mono1 [THEN le_trans]) |
|
987 |
apply (erule mult_le_mono2) |
|
988 |
done |
|
989 |
||
990 |
text {* strict, in 1st argument; proof is by induction on @{text "k > 0"} *} |
|
991 |
lemma mult_less_mono2: "(i::nat) < j ==> 0 < k ==> k * i < k * j" |
|
992 |
apply (erule_tac m1 = "0" in less_imp_Suc_add [THEN exE]) |
|
993 |
apply simp |
|
994 |
apply (induct_tac x) |
|
995 |
apply (simp_all add: add_less_mono) |
|
996 |
done |
|
997 |
||
998 |
lemma mult_less_mono1: "(i::nat) < j ==> 0 < k ==> i * k < j * k" |
|
999 |
by (drule mult_less_mono2) (simp_all add: mult_commute) |
|
1000 |
||
1001 |
lemma zero_less_mult_iff [simp]: "(0 < (m::nat) * n) = (0 < m & 0 < n)" |
|
1002 |
apply (induct m) |
|
1003 |
apply (case_tac [2] n) |
|
1004 |
apply simp_all |
|
1005 |
done |
|
1006 |
||
1007 |
lemma one_le_mult_iff [simp]: "(Suc 0 <= m * n) = (1 <= m & 1 <= n)" |
|
1008 |
apply (induct m) |
|
1009 |
apply (case_tac [2] n) |
|
1010 |
apply simp_all |
|
1011 |
done |
|
1012 |
||
1013 |
lemma mult_eq_1_iff [simp]: "(m * n = Suc 0) = (m = 1 & n = 1)" |
|
1014 |
apply (induct_tac m) |
|
1015 |
apply simp |
|
1016 |
apply (induct_tac n) |
|
1017 |
apply simp |
|
1018 |
apply fastsimp |
|
1019 |
done |
|
1020 |
||
1021 |
lemma one_eq_mult_iff [simp]: "(Suc 0 = m * n) = (m = 1 & n = 1)" |
|
1022 |
apply (rule trans) |
|
1023 |
apply (rule_tac [2] mult_eq_1_iff) |
|
1024 |
apply fastsimp |
|
1025 |
done |
|
1026 |
||
1027 |
lemma mult_less_cancel2: "((m::nat) * k < n * k) = (0 < k & m < n)" |
|
1028 |
apply (safe intro!: mult_less_mono1) |
|
1029 |
apply (case_tac k) |
|
1030 |
apply auto |
|
1031 |
apply (simp del: le_0_eq add: linorder_not_le [symmetric]) |
|
1032 |
apply (blast intro: mult_le_mono1) |
|
1033 |
done |
|
1034 |
||
1035 |
lemma mult_less_cancel1 [simp]: "(k * (m::nat) < k * n) = (0 < k & m < n)" |
|
1036 |
by (simp add: mult_less_cancel2 mult_commute [of k]) |
|
1037 |
||
1038 |
declare mult_less_cancel2 [simp] |
|
1039 |
||
1040 |
lemma mult_le_cancel1 [simp]: "(k * (m::nat) <= k * n) = (0 < k --> m <= n)" |
|
1041 |
apply (simp add: linorder_not_less [symmetric]) |
|
1042 |
apply auto |
|
1043 |
done |
|
1044 |
||
1045 |
lemma mult_le_cancel2 [simp]: "((m::nat) * k <= n * k) = (0 < k --> m <= n)" |
|
1046 |
apply (simp add: linorder_not_less [symmetric]) |
|
1047 |
apply auto |
|
1048 |
done |
|
1049 |
||
1050 |
lemma mult_cancel2: "(m * k = n * k) = (m = n | (k = (0::nat)))" |
|
1051 |
apply (cut_tac less_linear) |
|
1052 |
apply safe |
|
1053 |
apply auto |
|
1054 |
apply (drule mult_less_mono1, assumption, simp)+ |
|
1055 |
done |
|
1056 |
||
1057 |
lemma mult_cancel1 [simp]: "(k * m = k * n) = (m = n | (k = (0::nat)))" |
|
1058 |
by (simp add: mult_cancel2 mult_commute [of k]) |
|
1059 |
||
1060 |
declare mult_cancel2 [simp] |
|
1061 |
||
1062 |
lemma Suc_mult_less_cancel1: "(Suc k * m < Suc k * n) = (m < n)" |
|
1063 |
by (subst mult_less_cancel1) simp |
|
1064 |
||
1065 |
lemma Suc_mult_le_cancel1: "(Suc k * m <= Suc k * n) = (m <= n)" |
|
1066 |
by (subst mult_le_cancel1) simp |
|
1067 |
||
1068 |
lemma Suc_mult_cancel1: "(Suc k * m = Suc k * n) = (m = n)" |
|
1069 |
by (subst mult_cancel1) simp |
|
1070 |
||
1071 |
||
1072 |
text {* Lemma for @{text gcd} *} |
|
1073 |
lemma mult_eq_self_implies_10: "(m::nat) = m * n ==> n = 1 | m = 0" |
|
1074 |
apply (drule sym) |
|
1075 |
apply (rule disjCI) |
|
1076 |
apply (rule nat_less_cases, erule_tac [2] _) |
|
1077 |
apply (fastsimp elim!: less_SucE) |
|
1078 |
apply (fastsimp dest: mult_less_mono2) |
|
1079 |
done |
|
9436
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
wenzelm
parents:
7702
diff
changeset
|
1080 |
|
923 | 1081 |
end |