|
1 (* Title: ZF/Nat.thy |
|
2 ID: $Id$ |
|
3 Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 Copyright 1994 University of Cambridge |
|
5 |
|
6 *) |
|
7 |
|
8 header{*The Natural numbers As a Least Fixed Point*} |
|
9 |
|
10 theory Nat_ZF imports OrdQuant Bool begin |
|
11 |
|
12 definition |
|
13 nat :: i where |
|
14 "nat == lfp(Inf, %X. {0} Un {succ(i). i:X})" |
|
15 |
|
16 definition |
|
17 quasinat :: "i => o" where |
|
18 "quasinat(n) == n=0 | (\<exists>m. n = succ(m))" |
|
19 |
|
20 definition |
|
21 (*Has an unconditional succ case, which is used in "recursor" below.*) |
|
22 nat_case :: "[i, i=>i, i]=>i" where |
|
23 "nat_case(a,b,k) == THE y. k=0 & y=a | (EX x. k=succ(x) & y=b(x))" |
|
24 |
|
25 definition |
|
26 nat_rec :: "[i, i, [i,i]=>i]=>i" where |
|
27 "nat_rec(k,a,b) == |
|
28 wfrec(Memrel(nat), k, %n f. nat_case(a, %m. b(m, f`m), n))" |
|
29 |
|
30 (*Internalized relations on the naturals*) |
|
31 |
|
32 definition |
|
33 Le :: i where |
|
34 "Le == {<x,y>:nat*nat. x le y}" |
|
35 |
|
36 definition |
|
37 Lt :: i where |
|
38 "Lt == {<x, y>:nat*nat. x < y}" |
|
39 |
|
40 definition |
|
41 Ge :: i where |
|
42 "Ge == {<x,y>:nat*nat. y le x}" |
|
43 |
|
44 definition |
|
45 Gt :: i where |
|
46 "Gt == {<x,y>:nat*nat. y < x}" |
|
47 |
|
48 definition |
|
49 greater_than :: "i=>i" where |
|
50 "greater_than(n) == {i:nat. n < i}" |
|
51 |
|
52 text{*No need for a less-than operator: a natural number is its list of |
|
53 predecessors!*} |
|
54 |
|
55 |
|
56 lemma nat_bnd_mono: "bnd_mono(Inf, %X. {0} Un {succ(i). i:X})" |
|
57 apply (rule bnd_monoI) |
|
58 apply (cut_tac infinity, blast, blast) |
|
59 done |
|
60 |
|
61 (* nat = {0} Un {succ(x). x:nat} *) |
|
62 lemmas nat_unfold = nat_bnd_mono [THEN nat_def [THEN def_lfp_unfold], standard] |
|
63 |
|
64 (** Type checking of 0 and successor **) |
|
65 |
|
66 lemma nat_0I [iff,TC]: "0 : nat" |
|
67 apply (subst nat_unfold) |
|
68 apply (rule singletonI [THEN UnI1]) |
|
69 done |
|
70 |
|
71 lemma nat_succI [intro!,TC]: "n : nat ==> succ(n) : nat" |
|
72 apply (subst nat_unfold) |
|
73 apply (erule RepFunI [THEN UnI2]) |
|
74 done |
|
75 |
|
76 lemma nat_1I [iff,TC]: "1 : nat" |
|
77 by (rule nat_0I [THEN nat_succI]) |
|
78 |
|
79 lemma nat_2I [iff,TC]: "2 : nat" |
|
80 by (rule nat_1I [THEN nat_succI]) |
|
81 |
|
82 lemma bool_subset_nat: "bool <= nat" |
|
83 by (blast elim!: boolE) |
|
84 |
|
85 lemmas bool_into_nat = bool_subset_nat [THEN subsetD, standard] |
|
86 |
|
87 |
|
88 subsection{*Injectivity Properties and Induction*} |
|
89 |
|
90 (*Mathematical induction*) |
|
91 lemma nat_induct [case_names 0 succ, induct set: nat]: |
|
92 "[| n: nat; P(0); !!x. [| x: nat; P(x) |] ==> P(succ(x)) |] ==> P(n)" |
|
93 by (erule def_induct [OF nat_def nat_bnd_mono], blast) |
|
94 |
|
95 lemma natE: |
|
96 "[| n: nat; n=0 ==> P; !!x. [| x: nat; n=succ(x) |] ==> P |] ==> P" |
|
97 by (erule nat_unfold [THEN equalityD1, THEN subsetD, THEN UnE], auto) |
|
98 |
|
99 lemma nat_into_Ord [simp]: "n: nat ==> Ord(n)" |
|
100 by (erule nat_induct, auto) |
|
101 |
|
102 (* i: nat ==> 0 le i; same thing as 0<succ(i) *) |
|
103 lemmas nat_0_le = nat_into_Ord [THEN Ord_0_le, standard] |
|
104 |
|
105 (* i: nat ==> i le i; same thing as i<succ(i) *) |
|
106 lemmas nat_le_refl = nat_into_Ord [THEN le_refl, standard] |
|
107 |
|
108 lemma Ord_nat [iff]: "Ord(nat)" |
|
109 apply (rule OrdI) |
|
110 apply (erule_tac [2] nat_into_Ord [THEN Ord_is_Transset]) |
|
111 apply (unfold Transset_def) |
|
112 apply (rule ballI) |
|
113 apply (erule nat_induct, auto) |
|
114 done |
|
115 |
|
116 lemma Limit_nat [iff]: "Limit(nat)" |
|
117 apply (unfold Limit_def) |
|
118 apply (safe intro!: ltI Ord_nat) |
|
119 apply (erule ltD) |
|
120 done |
|
121 |
|
122 lemma naturals_not_limit: "a \<in> nat ==> ~ Limit(a)" |
|
123 by (induct a rule: nat_induct, auto) |
|
124 |
|
125 lemma succ_natD: "succ(i): nat ==> i: nat" |
|
126 by (rule Ord_trans [OF succI1], auto) |
|
127 |
|
128 lemma nat_succ_iff [iff]: "succ(n): nat <-> n: nat" |
|
129 by (blast dest!: succ_natD) |
|
130 |
|
131 lemma nat_le_Limit: "Limit(i) ==> nat le i" |
|
132 apply (rule subset_imp_le) |
|
133 apply (simp_all add: Limit_is_Ord) |
|
134 apply (rule subsetI) |
|
135 apply (erule nat_induct) |
|
136 apply (erule Limit_has_0 [THEN ltD]) |
|
137 apply (blast intro: Limit_has_succ [THEN ltD] ltI Limit_is_Ord) |
|
138 done |
|
139 |
|
140 (* [| succ(i): k; k: nat |] ==> i: k *) |
|
141 lemmas succ_in_naturalD = Ord_trans [OF succI1 _ nat_into_Ord] |
|
142 |
|
143 lemma lt_nat_in_nat: "[| m<n; n: nat |] ==> m: nat" |
|
144 apply (erule ltE) |
|
145 apply (erule Ord_trans, assumption, simp) |
|
146 done |
|
147 |
|
148 lemma le_in_nat: "[| m le n; n:nat |] ==> m:nat" |
|
149 by (blast dest!: lt_nat_in_nat) |
|
150 |
|
151 |
|
152 subsection{*Variations on Mathematical Induction*} |
|
153 |
|
154 (*complete induction*) |
|
155 |
|
156 lemmas complete_induct = Ord_induct [OF _ Ord_nat, case_names less, consumes 1] |
|
157 |
|
158 lemmas complete_induct_rule = |
|
159 complete_induct [rule_format, case_names less, consumes 1] |
|
160 |
|
161 |
|
162 lemma nat_induct_from_lemma [rule_format]: |
|
163 "[| n: nat; m: nat; |
|
164 !!x. [| x: nat; m le x; P(x) |] ==> P(succ(x)) |] |
|
165 ==> m le n --> P(m) --> P(n)" |
|
166 apply (erule nat_induct) |
|
167 apply (simp_all add: distrib_simps le0_iff le_succ_iff) |
|
168 done |
|
169 |
|
170 (*Induction starting from m rather than 0*) |
|
171 lemma nat_induct_from: |
|
172 "[| m le n; m: nat; n: nat; |
|
173 P(m); |
|
174 !!x. [| x: nat; m le x; P(x) |] ==> P(succ(x)) |] |
|
175 ==> P(n)" |
|
176 apply (blast intro: nat_induct_from_lemma) |
|
177 done |
|
178 |
|
179 (*Induction suitable for subtraction and less-than*) |
|
180 lemma diff_induct [case_names 0 0_succ succ_succ, consumes 2]: |
|
181 "[| m: nat; n: nat; |
|
182 !!x. x: nat ==> P(x,0); |
|
183 !!y. y: nat ==> P(0,succ(y)); |
|
184 !!x y. [| x: nat; y: nat; P(x,y) |] ==> P(succ(x),succ(y)) |] |
|
185 ==> P(m,n)" |
|
186 apply (erule_tac x = m in rev_bspec) |
|
187 apply (erule nat_induct, simp) |
|
188 apply (rule ballI) |
|
189 apply (rename_tac i j) |
|
190 apply (erule_tac n=j in nat_induct, auto) |
|
191 done |
|
192 |
|
193 |
|
194 (** Induction principle analogous to trancl_induct **) |
|
195 |
|
196 lemma succ_lt_induct_lemma [rule_format]: |
|
197 "m: nat ==> P(m,succ(m)) --> (ALL x: nat. P(m,x) --> P(m,succ(x))) --> |
|
198 (ALL n:nat. m<n --> P(m,n))" |
|
199 apply (erule nat_induct) |
|
200 apply (intro impI, rule nat_induct [THEN ballI]) |
|
201 prefer 4 apply (intro impI, rule nat_induct [THEN ballI]) |
|
202 apply (auto simp add: le_iff) |
|
203 done |
|
204 |
|
205 lemma succ_lt_induct: |
|
206 "[| m<n; n: nat; |
|
207 P(m,succ(m)); |
|
208 !!x. [| x: nat; P(m,x) |] ==> P(m,succ(x)) |] |
|
209 ==> P(m,n)" |
|
210 by (blast intro: succ_lt_induct_lemma lt_nat_in_nat) |
|
211 |
|
212 subsection{*quasinat: to allow a case-split rule for @{term nat_case}*} |
|
213 |
|
214 text{*True if the argument is zero or any successor*} |
|
215 lemma [iff]: "quasinat(0)" |
|
216 by (simp add: quasinat_def) |
|
217 |
|
218 lemma [iff]: "quasinat(succ(x))" |
|
219 by (simp add: quasinat_def) |
|
220 |
|
221 lemma nat_imp_quasinat: "n \<in> nat ==> quasinat(n)" |
|
222 by (erule natE, simp_all) |
|
223 |
|
224 lemma non_nat_case: "~ quasinat(x) ==> nat_case(a,b,x) = 0" |
|
225 by (simp add: quasinat_def nat_case_def) |
|
226 |
|
227 lemma nat_cases_disj: "k=0 | (\<exists>y. k = succ(y)) | ~ quasinat(k)" |
|
228 apply (case_tac "k=0", simp) |
|
229 apply (case_tac "\<exists>m. k = succ(m)") |
|
230 apply (simp_all add: quasinat_def) |
|
231 done |
|
232 |
|
233 lemma nat_cases: |
|
234 "[|k=0 ==> P; !!y. k = succ(y) ==> P; ~ quasinat(k) ==> P|] ==> P" |
|
235 by (insert nat_cases_disj [of k], blast) |
|
236 |
|
237 (** nat_case **) |
|
238 |
|
239 lemma nat_case_0 [simp]: "nat_case(a,b,0) = a" |
|
240 by (simp add: nat_case_def) |
|
241 |
|
242 lemma nat_case_succ [simp]: "nat_case(a,b,succ(n)) = b(n)" |
|
243 by (simp add: nat_case_def) |
|
244 |
|
245 lemma nat_case_type [TC]: |
|
246 "[| n: nat; a: C(0); !!m. m: nat ==> b(m): C(succ(m)) |] |
|
247 ==> nat_case(a,b,n) : C(n)"; |
|
248 by (erule nat_induct, auto) |
|
249 |
|
250 lemma split_nat_case: |
|
251 "P(nat_case(a,b,k)) <-> |
|
252 ((k=0 --> P(a)) & (\<forall>x. k=succ(x) --> P(b(x))) & (~ quasinat(k) \<longrightarrow> P(0)))" |
|
253 apply (rule nat_cases [of k]) |
|
254 apply (auto simp add: non_nat_case) |
|
255 done |
|
256 |
|
257 |
|
258 subsection{*Recursion on the Natural Numbers*} |
|
259 |
|
260 (** nat_rec is used to define eclose and transrec, then becomes obsolete. |
|
261 The operator rec, from arith.thy, has fewer typing conditions **) |
|
262 |
|
263 lemma nat_rec_0: "nat_rec(0,a,b) = a" |
|
264 apply (rule nat_rec_def [THEN def_wfrec, THEN trans]) |
|
265 apply (rule wf_Memrel) |
|
266 apply (rule nat_case_0) |
|
267 done |
|
268 |
|
269 lemma nat_rec_succ: "m: nat ==> nat_rec(succ(m),a,b) = b(m, nat_rec(m,a,b))" |
|
270 apply (rule nat_rec_def [THEN def_wfrec, THEN trans]) |
|
271 apply (rule wf_Memrel) |
|
272 apply (simp add: vimage_singleton_iff) |
|
273 done |
|
274 |
|
275 (** The union of two natural numbers is a natural number -- their maximum **) |
|
276 |
|
277 lemma Un_nat_type [TC]: "[| i: nat; j: nat |] ==> i Un j: nat" |
|
278 apply (rule Un_least_lt [THEN ltD]) |
|
279 apply (simp_all add: lt_def) |
|
280 done |
|
281 |
|
282 lemma Int_nat_type [TC]: "[| i: nat; j: nat |] ==> i Int j: nat" |
|
283 apply (rule Int_greatest_lt [THEN ltD]) |
|
284 apply (simp_all add: lt_def) |
|
285 done |
|
286 |
|
287 (*needed to simplify unions over nat*) |
|
288 lemma nat_nonempty [simp]: "nat ~= 0" |
|
289 by blast |
|
290 |
|
291 text{*A natural number is the set of its predecessors*} |
|
292 lemma nat_eq_Collect_lt: "i \<in> nat ==> {j\<in>nat. j<i} = i" |
|
293 apply (rule equalityI) |
|
294 apply (blast dest: ltD) |
|
295 apply (auto simp add: Ord_mem_iff_lt) |
|
296 apply (blast intro: lt_trans) |
|
297 done |
|
298 |
|
299 lemma Le_iff [iff]: "<x,y> : Le <-> x le y & x : nat & y : nat" |
|
300 by (force simp add: Le_def) |
|
301 |
|
302 end |