author | paulson |
Thu, 10 Oct 1996 10:45:20 +0200 | |
changeset 2081 | c2da3ca231ab |
parent 2031 | 03a843f0f447 |
child 2099 | c5f004bfcbab |
permissions | -rw-r--r-- |
1465 | 1 |
(* Title: HOL/nat |
923 | 2 |
ID: $Id$ |
1465 | 3 |
Author: Tobias Nipkow, Cambridge University Computer Laboratory |
923 | 4 |
Copyright 1991 University of Cambridge |
5 |
||
6 |
For nat.thy. Type nat is defined as a set (Nat) over the type ind. |
|
7 |
*) |
|
8 |
||
9 |
open Nat; |
|
10 |
||
11 |
goal Nat.thy "mono(%X. {Zero_Rep} Un (Suc_Rep``X))"; |
|
12 |
by (REPEAT (ares_tac [monoI, subset_refl, image_mono, Un_mono] 1)); |
|
13 |
qed "Nat_fun_mono"; |
|
14 |
||
15 |
val Nat_unfold = Nat_fun_mono RS (Nat_def RS def_lfp_Tarski); |
|
16 |
||
17 |
(* Zero is a natural number -- this also justifies the type definition*) |
|
18 |
goal Nat.thy "Zero_Rep: Nat"; |
|
2031 | 19 |
by (stac Nat_unfold 1); |
923 | 20 |
by (rtac (singletonI RS UnI1) 1); |
21 |
qed "Zero_RepI"; |
|
22 |
||
23 |
val prems = goal Nat.thy "i: Nat ==> Suc_Rep(i) : Nat"; |
|
2031 | 24 |
by (stac Nat_unfold 1); |
923 | 25 |
by (rtac (imageI RS UnI2) 1); |
26 |
by (resolve_tac prems 1); |
|
27 |
qed "Suc_RepI"; |
|
28 |
||
29 |
(*** Induction ***) |
|
30 |
||
31 |
val major::prems = goal Nat.thy |
|
32 |
"[| i: Nat; P(Zero_Rep); \ |
|
33 |
\ !!j. [| j: Nat; P(j) |] ==> P(Suc_Rep(j)) |] ==> P(i)"; |
|
34 |
by (rtac ([Nat_def, Nat_fun_mono, major] MRS def_induct) 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
35 |
by (fast_tac (!claset addIs prems) 1); |
923 | 36 |
qed "Nat_induct"; |
37 |
||
38 |
val prems = goalw Nat.thy [Zero_def,Suc_def] |
|
39 |
"[| P(0); \ |
|
40 |
\ !!k. P(k) ==> P(Suc(k)) |] ==> P(n)"; |
|
41 |
by (rtac (Rep_Nat_inverse RS subst) 1); (*types force good instantiation*) |
|
42 |
by (rtac (Rep_Nat RS Nat_induct) 1); |
|
43 |
by (REPEAT (ares_tac prems 1 |
|
44 |
ORELSE eresolve_tac [Abs_Nat_inverse RS subst] 1)); |
|
45 |
qed "nat_induct"; |
|
46 |
||
47 |
(*Perform induction on n. *) |
|
48 |
fun nat_ind_tac a i = |
|
49 |
EVERY [res_inst_tac [("n",a)] nat_induct i, |
|
1465 | 50 |
rename_last_tac a ["1"] (i+1)]; |
923 | 51 |
|
52 |
(*A special form of induction for reasoning about m<n and m-n*) |
|
53 |
val prems = goal Nat.thy |
|
54 |
"[| !!x. P x 0; \ |
|
55 |
\ !!y. P 0 (Suc y); \ |
|
56 |
\ !!x y. [| P x y |] ==> P (Suc x) (Suc y) \ |
|
57 |
\ |] ==> P m n"; |
|
58 |
by (res_inst_tac [("x","m")] spec 1); |
|
59 |
by (nat_ind_tac "n" 1); |
|
60 |
by (rtac allI 2); |
|
61 |
by (nat_ind_tac "x" 2); |
|
62 |
by (REPEAT (ares_tac (prems@[allI]) 1 ORELSE etac spec 1)); |
|
63 |
qed "diff_induct"; |
|
64 |
||
65 |
(*Case analysis on the natural numbers*) |
|
66 |
val prems = goal Nat.thy |
|
67 |
"[| n=0 ==> P; !!x. n = Suc(x) ==> P |] ==> P"; |
|
68 |
by (subgoal_tac "n=0 | (EX x. n = Suc(x))" 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
69 |
by (fast_tac (!claset addSEs prems) 1); |
923 | 70 |
by (nat_ind_tac "n" 1); |
71 |
by (rtac (refl RS disjI1) 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
72 |
by (Fast_tac 1); |
923 | 73 |
qed "natE"; |
74 |
||
75 |
(*** Isomorphisms: Abs_Nat and Rep_Nat ***) |
|
76 |
||
77 |
(*We can't take these properties as axioms, or take Abs_Nat==Inv(Rep_Nat), |
|
78 |
since we assume the isomorphism equations will one day be given by Isabelle*) |
|
79 |
||
80 |
goal Nat.thy "inj(Rep_Nat)"; |
|
81 |
by (rtac inj_inverseI 1); |
|
82 |
by (rtac Rep_Nat_inverse 1); |
|
83 |
qed "inj_Rep_Nat"; |
|
84 |
||
85 |
goal Nat.thy "inj_onto Abs_Nat Nat"; |
|
86 |
by (rtac inj_onto_inverseI 1); |
|
87 |
by (etac Abs_Nat_inverse 1); |
|
88 |
qed "inj_onto_Abs_Nat"; |
|
89 |
||
90 |
(*** Distinctness of constructors ***) |
|
91 |
||
92 |
goalw Nat.thy [Zero_def,Suc_def] "Suc(m) ~= 0"; |
|
93 |
by (rtac (inj_onto_Abs_Nat RS inj_onto_contraD) 1); |
|
94 |
by (rtac Suc_Rep_not_Zero_Rep 1); |
|
95 |
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI, Zero_RepI] 1)); |
|
96 |
qed "Suc_not_Zero"; |
|
97 |
||
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
98 |
bind_thm ("Zero_not_Suc", Suc_not_Zero RS not_sym); |
923 | 99 |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
100 |
AddIffs [Suc_not_Zero,Zero_not_Suc]; |
1301 | 101 |
|
923 | 102 |
bind_thm ("Suc_neq_Zero", (Suc_not_Zero RS notE)); |
103 |
val Zero_neq_Suc = sym RS Suc_neq_Zero; |
|
104 |
||
105 |
(** Injectiveness of Suc **) |
|
106 |
||
107 |
goalw Nat.thy [Suc_def] "inj(Suc)"; |
|
108 |
by (rtac injI 1); |
|
109 |
by (dtac (inj_onto_Abs_Nat RS inj_ontoD) 1); |
|
110 |
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI] 1)); |
|
111 |
by (dtac (inj_Suc_Rep RS injD) 1); |
|
112 |
by (etac (inj_Rep_Nat RS injD) 1); |
|
113 |
qed "inj_Suc"; |
|
114 |
||
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1024
diff
changeset
|
115 |
val Suc_inject = inj_Suc RS injD; |
923 | 116 |
|
117 |
goal Nat.thy "(Suc(m)=Suc(n)) = (m=n)"; |
|
118 |
by (EVERY1 [rtac iffI, etac Suc_inject, etac arg_cong]); |
|
119 |
qed "Suc_Suc_eq"; |
|
120 |
||
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
121 |
AddIffs [Suc_Suc_eq]; |
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
122 |
|
923 | 123 |
goal Nat.thy "n ~= Suc(n)"; |
124 |
by (nat_ind_tac "n" 1); |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
125 |
by (ALLGOALS Asm_simp_tac); |
923 | 126 |
qed "n_not_Suc_n"; |
127 |
||
1618 | 128 |
bind_thm ("Suc_n_not_n", n_not_Suc_n RS not_sym); |
923 | 129 |
|
130 |
(*** nat_case -- the selection operator for nat ***) |
|
131 |
||
132 |
goalw Nat.thy [nat_case_def] "nat_case a f 0 = a"; |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
133 |
by (fast_tac (!claset addIs [select_equality]) 1); |
923 | 134 |
qed "nat_case_0"; |
135 |
||
136 |
goalw Nat.thy [nat_case_def] "nat_case a f (Suc k) = f(k)"; |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
137 |
by (fast_tac (!claset addIs [select_equality]) 1); |
923 | 138 |
qed "nat_case_Suc"; |
139 |
||
140 |
(** Introduction rules for 'pred_nat' **) |
|
141 |
||
972
e61b058d58d2
changed syntax of tuples from <..., ...> to (..., ...)
clasohm
parents:
962
diff
changeset
|
142 |
goalw Nat.thy [pred_nat_def] "(n, Suc(n)) : pred_nat"; |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
143 |
by (Fast_tac 1); |
923 | 144 |
qed "pred_natI"; |
145 |
||
146 |
val major::prems = goalw Nat.thy [pred_nat_def] |
|
972
e61b058d58d2
changed syntax of tuples from <..., ...> to (..., ...)
clasohm
parents:
962
diff
changeset
|
147 |
"[| p : pred_nat; !!x n. [| p = (n, Suc(n)) |] ==> R \ |
923 | 148 |
\ |] ==> R"; |
149 |
by (rtac (major RS CollectE) 1); |
|
150 |
by (REPEAT (eresolve_tac ([asm_rl,exE]@prems) 1)); |
|
151 |
qed "pred_natE"; |
|
152 |
||
153 |
goalw Nat.thy [wf_def] "wf(pred_nat)"; |
|
154 |
by (strip_tac 1); |
|
155 |
by (nat_ind_tac "x" 1); |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
156 |
by (fast_tac (!claset addSEs [mp, pred_natE]) 2); |
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
157 |
by (fast_tac (!claset addSEs [mp, pred_natE]) 1); |
923 | 158 |
qed "wf_pred_nat"; |
159 |
||
160 |
||
161 |
(*** nat_rec -- by wf recursion on pred_nat ***) |
|
162 |
||
1475 | 163 |
(* The unrolling rule for nat_rec *) |
164 |
goal Nat.thy |
|
1824 | 165 |
"(%n. nat_rec c d n) = wfrec pred_nat (%f. nat_case ?c (%m. ?d m (f m)))"; |
1475 | 166 |
by (simp_tac (HOL_ss addsimps [nat_rec_def]) 1); |
167 |
bind_thm("nat_rec_unfold", wf_pred_nat RS |
|
168 |
((result() RS eq_reflection) RS def_wfrec)); |
|
169 |
||
170 |
(*--------------------------------------------------------------------------- |
|
171 |
* Old: |
|
172 |
* bind_thm ("nat_rec_unfold", (wf_pred_nat RS (nat_rec_def RS def_wfrec))); |
|
173 |
*---------------------------------------------------------------------------*) |
|
923 | 174 |
|
175 |
(** conversion rules **) |
|
176 |
||
1824 | 177 |
goal Nat.thy "nat_rec c h 0 = c"; |
923 | 178 |
by (rtac (nat_rec_unfold RS trans) 1); |
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1024
diff
changeset
|
179 |
by (simp_tac (!simpset addsimps [nat_case_0]) 1); |
923 | 180 |
qed "nat_rec_0"; |
181 |
||
1824 | 182 |
goal Nat.thy "nat_rec c h (Suc n) = h n (nat_rec c h n)"; |
923 | 183 |
by (rtac (nat_rec_unfold RS trans) 1); |
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1024
diff
changeset
|
184 |
by (simp_tac (!simpset addsimps [nat_case_Suc, pred_natI, cut_apply]) 1); |
923 | 185 |
qed "nat_rec_Suc"; |
186 |
||
187 |
(*These 2 rules ease the use of primitive recursion. NOTE USE OF == *) |
|
188 |
val [rew] = goal Nat.thy |
|
1824 | 189 |
"[| !!n. f(n) == nat_rec c h n |] ==> f(0) = c"; |
923 | 190 |
by (rewtac rew); |
191 |
by (rtac nat_rec_0 1); |
|
192 |
qed "def_nat_rec_0"; |
|
193 |
||
194 |
val [rew] = goal Nat.thy |
|
1824 | 195 |
"[| !!n. f(n) == nat_rec c h n |] ==> f(Suc(n)) = h n (f n)"; |
923 | 196 |
by (rewtac rew); |
197 |
by (rtac nat_rec_Suc 1); |
|
198 |
qed "def_nat_rec_Suc"; |
|
199 |
||
200 |
fun nat_recs def = |
|
201 |
[standard (def RS def_nat_rec_0), |
|
202 |
standard (def RS def_nat_rec_Suc)]; |
|
203 |
||
204 |
||
205 |
(*** Basic properties of "less than" ***) |
|
206 |
||
207 |
(** Introduction properties **) |
|
208 |
||
209 |
val prems = goalw Nat.thy [less_def] "[| i<j; j<k |] ==> i<(k::nat)"; |
|
210 |
by (rtac (trans_trancl RS transD) 1); |
|
211 |
by (resolve_tac prems 1); |
|
212 |
by (resolve_tac prems 1); |
|
213 |
qed "less_trans"; |
|
214 |
||
215 |
goalw Nat.thy [less_def] "n < Suc(n)"; |
|
216 |
by (rtac (pred_natI RS r_into_trancl) 1); |
|
217 |
qed "lessI"; |
|
1301 | 218 |
Addsimps [lessI]; |
923 | 219 |
|
1618 | 220 |
(* i<j ==> i<Suc(j) *) |
923 | 221 |
val less_SucI = lessI RSN (2, less_trans); |
222 |
||
223 |
goal Nat.thy "0 < Suc(n)"; |
|
224 |
by (nat_ind_tac "n" 1); |
|
225 |
by (rtac lessI 1); |
|
226 |
by (etac less_trans 1); |
|
227 |
by (rtac lessI 1); |
|
228 |
qed "zero_less_Suc"; |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
229 |
AddIffs [zero_less_Suc]; |
923 | 230 |
|
231 |
(** Elimination properties **) |
|
232 |
||
233 |
val prems = goalw Nat.thy [less_def] "n<m ==> ~ m<(n::nat)"; |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
234 |
by (fast_tac (!claset addIs ([wf_pred_nat, wf_trancl RS wf_asym]@prems))1); |
923 | 235 |
qed "less_not_sym"; |
236 |
||
1931 | 237 |
(* [| n<m; m<n |] ==> R *) |
923 | 238 |
bind_thm ("less_asym", (less_not_sym RS notE)); |
239 |
||
240 |
goalw Nat.thy [less_def] "~ n<(n::nat)"; |
|
241 |
by (rtac notI 1); |
|
1618 | 242 |
by (etac (wf_pred_nat RS wf_trancl RS wf_irrefl) 1); |
923 | 243 |
qed "less_not_refl"; |
244 |
||
1817 | 245 |
(* n<n ==> R *) |
1618 | 246 |
bind_thm ("less_irrefl", (less_not_refl RS notE)); |
923 | 247 |
|
248 |
goal Nat.thy "!!m. n<m ==> m ~= (n::nat)"; |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
249 |
by (fast_tac (!claset addEs [less_irrefl]) 1); |
923 | 250 |
qed "less_not_refl2"; |
251 |
||
252 |
||
253 |
val major::prems = goalw Nat.thy [less_def] |
|
254 |
"[| i<k; k=Suc(i) ==> P; !!j. [| i<j; k=Suc(j) |] ==> P \ |
|
255 |
\ |] ==> P"; |
|
256 |
by (rtac (major RS tranclE) 1); |
|
1024 | 257 |
by (REPEAT_FIRST (bound_hyp_subst_tac ORELSE' |
1465 | 258 |
eresolve_tac (prems@[pred_natE, Pair_inject]))); |
1024 | 259 |
by (rtac refl 1); |
923 | 260 |
qed "lessE"; |
261 |
||
262 |
goal Nat.thy "~ n<0"; |
|
263 |
by (rtac notI 1); |
|
264 |
by (etac lessE 1); |
|
265 |
by (etac Zero_neq_Suc 1); |
|
266 |
by (etac Zero_neq_Suc 1); |
|
267 |
qed "not_less0"; |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
268 |
|
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
269 |
AddIffs [not_less0]; |
923 | 270 |
|
271 |
(* n<0 ==> R *) |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
272 |
bind_thm ("less_zeroE", not_less0 RS notE); |
923 | 273 |
|
274 |
val [major,less,eq] = goal Nat.thy |
|
275 |
"[| m < Suc(n); m<n ==> P; m=n ==> P |] ==> P"; |
|
276 |
by (rtac (major RS lessE) 1); |
|
277 |
by (rtac eq 1); |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
278 |
by (Fast_tac 1); |
923 | 279 |
by (rtac less 1); |
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
280 |
by (Fast_tac 1); |
923 | 281 |
qed "less_SucE"; |
282 |
||
283 |
goal Nat.thy "(m < Suc(n)) = (m < n | m = n)"; |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
284 |
by (fast_tac (!claset addSIs [lessI] |
1919 | 285 |
addEs [less_trans, less_SucE]) 1); |
923 | 286 |
qed "less_Suc_eq"; |
287 |
||
1301 | 288 |
val prems = goal Nat.thy "m<n ==> n ~= 0"; |
1552 | 289 |
by (res_inst_tac [("n","n")] natE 1); |
290 |
by (cut_facts_tac prems 1); |
|
291 |
by (ALLGOALS Asm_full_simp_tac); |
|
1301 | 292 |
qed "gr_implies_not0"; |
293 |
Addsimps [gr_implies_not0]; |
|
923 | 294 |
|
1660 | 295 |
qed_goal "zero_less_eq" Nat.thy "0 < n = (n ~= 0)" (fn _ => [ |
2031 | 296 |
rtac iffI 1, |
297 |
etac gr_implies_not0 1, |
|
298 |
rtac natE 1, |
|
299 |
contr_tac 1, |
|
300 |
etac ssubst 1, |
|
301 |
rtac zero_less_Suc 1]); |
|
1660 | 302 |
|
923 | 303 |
(** Inductive (?) properties **) |
304 |
||
305 |
val [prem] = goal Nat.thy "Suc(m) < n ==> m<n"; |
|
306 |
by (rtac (prem RS rev_mp) 1); |
|
307 |
by (nat_ind_tac "n" 1); |
|
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
308 |
by (ALLGOALS (fast_tac (!claset addSIs [lessI RS less_SucI] |
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
309 |
addEs [less_trans, lessE]))); |
923 | 310 |
qed "Suc_lessD"; |
311 |
||
312 |
val [major,minor] = goal Nat.thy |
|
313 |
"[| Suc(i)<k; !!j. [| i<j; k=Suc(j) |] ==> P \ |
|
314 |
\ |] ==> P"; |
|
315 |
by (rtac (major RS lessE) 1); |
|
316 |
by (etac (lessI RS minor) 1); |
|
317 |
by (etac (Suc_lessD RS minor) 1); |
|
318 |
by (assume_tac 1); |
|
319 |
qed "Suc_lessE"; |
|
320 |
||
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
321 |
goal Nat.thy "!!m n. Suc(m) < Suc(n) ==> m<n"; |
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
322 |
by (fast_tac (!claset addEs [lessE, Suc_lessD] addIs [lessI]) 1); |
923 | 323 |
qed "Suc_less_SucD"; |
324 |
||
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
325 |
goal Nat.thy "!!m n. m<n ==> Suc(m) < Suc(n)"; |
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
326 |
by (etac rev_mp 1); |
923 | 327 |
by (nat_ind_tac "n" 1); |
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
328 |
by (ALLGOALS (fast_tac (!claset addSIs [lessI] |
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
329 |
addEs [less_trans, lessE]))); |
923 | 330 |
qed "Suc_mono"; |
331 |
||
1672
2c109cd2fdd0
repaired critical proofs depending on the order inside non-confluent SimpSets,
oheimb
parents:
1660
diff
changeset
|
332 |
|
923 | 333 |
goal Nat.thy "(Suc(m) < Suc(n)) = (m<n)"; |
334 |
by (EVERY1 [rtac iffI, etac Suc_less_SucD, etac Suc_mono]); |
|
335 |
qed "Suc_less_eq"; |
|
1301 | 336 |
Addsimps [Suc_less_eq]; |
923 | 337 |
|
338 |
goal Nat.thy "~(Suc(n) < n)"; |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
339 |
by (fast_tac (!claset addEs [Suc_lessD RS less_irrefl]) 1); |
923 | 340 |
qed "not_Suc_n_less_n"; |
1301 | 341 |
Addsimps [not_Suc_n_less_n]; |
342 |
||
343 |
goal Nat.thy "!!i. i<j ==> j<k --> Suc i < k"; |
|
1552 | 344 |
by (nat_ind_tac "k" 1); |
1660 | 345 |
by (ALLGOALS (asm_simp_tac (!simpset))); |
346 |
by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
347 |
by (fast_tac (!claset addDs [Suc_lessD]) 1); |
1485
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
nipkow
parents:
1475
diff
changeset
|
348 |
qed_spec_mp "less_trans_Suc"; |
923 | 349 |
|
350 |
(*"Less than" is a linear ordering*) |
|
351 |
goal Nat.thy "m<n | m=n | n<(m::nat)"; |
|
352 |
by (nat_ind_tac "m" 1); |
|
353 |
by (nat_ind_tac "n" 1); |
|
354 |
by (rtac (refl RS disjI1 RS disjI2) 1); |
|
355 |
by (rtac (zero_less_Suc RS disjI1) 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
356 |
by (fast_tac (!claset addIs [lessI, Suc_mono, less_SucI] addEs [lessE]) 1); |
923 | 357 |
qed "less_linear"; |
358 |
||
1660 | 359 |
qed_goal "nat_less_cases" Nat.thy |
360 |
"[| (m::nat)<n ==> P n m; m=n ==> P n m; n<m ==> P n m |] ==> P n m" |
|
361 |
( fn prems => |
|
362 |
[ |
|
363 |
(res_inst_tac [("m1","n"),("n1","m")] (less_linear RS disjE) 1), |
|
364 |
(etac disjE 2), |
|
365 |
(etac (hd (tl (tl prems))) 1), |
|
366 |
(etac (sym RS hd (tl prems)) 1), |
|
367 |
(etac (hd prems) 1) |
|
368 |
]); |
|
369 |
||
923 | 370 |
(*Can be used with less_Suc_eq to get n=m | n<m *) |
371 |
goal Nat.thy "(~ m < n) = (n < Suc(m))"; |
|
372 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
1552 | 373 |
by (ALLGOALS Asm_simp_tac); |
923 | 374 |
qed "not_less_eq"; |
375 |
||
376 |
(*Complete induction, aka course-of-values induction*) |
|
377 |
val prems = goalw Nat.thy [less_def] |
|
378 |
"[| !!n. [| ! m::nat. m<n --> P(m) |] ==> P(n) |] ==> P(n)"; |
|
379 |
by (wf_ind_tac "n" [wf_pred_nat RS wf_trancl] 1); |
|
380 |
by (eresolve_tac prems 1); |
|
381 |
qed "less_induct"; |
|
382 |
||
383 |
||
384 |
(*** Properties of <= ***) |
|
385 |
||
1931 | 386 |
goalw Nat.thy [le_def] "(m <= n) = (m < Suc n)"; |
387 |
by (rtac not_less_eq 1); |
|
388 |
qed "le_eq_less_Suc"; |
|
389 |
||
923 | 390 |
goalw Nat.thy [le_def] "0 <= n"; |
391 |
by (rtac not_less0 1); |
|
392 |
qed "le0"; |
|
393 |
||
1301 | 394 |
goalw Nat.thy [le_def] "~ Suc n <= n"; |
1552 | 395 |
by (Simp_tac 1); |
1301 | 396 |
qed "Suc_n_not_le_n"; |
397 |
||
398 |
goalw Nat.thy [le_def] "(i <= 0) = (i = 0)"; |
|
1552 | 399 |
by (nat_ind_tac "i" 1); |
400 |
by (ALLGOALS Asm_simp_tac); |
|
1777
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
401 |
qed "le_0_eq"; |
1301 | 402 |
|
403 |
Addsimps [less_not_refl, |
|
1777
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
404 |
(*less_Suc_eq, makes simpset non-confluent*) le0, le_0_eq, |
1985
84cf16192e03
Tidied many proofs, using AddIffs to let equivalences take
paulson
parents:
1931
diff
changeset
|
405 |
Suc_n_not_le_n, |
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1024
diff
changeset
|
406 |
n_not_Suc_n, Suc_n_not_n, |
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1024
diff
changeset
|
407 |
nat_case_0, nat_case_Suc, nat_rec_0, nat_rec_Suc]; |
923 | 408 |
|
1777
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
409 |
(* |
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
410 |
goal Nat.thy "(Suc m < n | Suc m = n) = (m < n)"; |
2031 | 411 |
by (stac (less_Suc_eq RS sym) 1); |
412 |
by (rtac Suc_less_eq 1); |
|
1777
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
413 |
qed "Suc_le_eq"; |
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
414 |
|
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
415 |
this could make the simpset (with less_Suc_eq added again) more confluent, |
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
416 |
but less_Suc_eq makes additional problems with terms of the form 0 < Suc (...) |
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
417 |
*) |
47c47b7d7aa8
renamed le_0 to le_0_eq, to avoid confusion with le0,
oheimb
parents:
1760
diff
changeset
|
418 |
|
923 | 419 |
(*Prevents simplification of f and g: much faster*) |
420 |
qed_goal "nat_case_weak_cong" Nat.thy |
|
421 |
"m=n ==> nat_case a f m = nat_case a f n" |
|
422 |
(fn [prem] => [rtac (prem RS arg_cong) 1]); |
|
423 |
||
424 |
qed_goal "nat_rec_weak_cong" Nat.thy |
|
1824 | 425 |
"m=n ==> nat_rec a f m = nat_rec a f n" |
923 | 426 |
(fn [prem] => [rtac (prem RS arg_cong) 1]); |
427 |
||
1618 | 428 |
val prems = goalw Nat.thy [le_def] "~n<m ==> m<=(n::nat)"; |
923 | 429 |
by (resolve_tac prems 1); |
430 |
qed "leI"; |
|
431 |
||
1618 | 432 |
val prems = goalw Nat.thy [le_def] "m<=n ==> ~ n < (m::nat)"; |
923 | 433 |
by (resolve_tac prems 1); |
434 |
qed "leD"; |
|
435 |
||
436 |
val leE = make_elim leD; |
|
437 |
||
1618 | 438 |
goal Nat.thy "(~n<m) = (m<=(n::nat))"; |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
439 |
by (fast_tac (!claset addIs [leI] addEs [leE]) 1); |
1618 | 440 |
qed "not_less_iff_le"; |
441 |
||
923 | 442 |
goalw Nat.thy [le_def] "!!m. ~ m <= n ==> n<(m::nat)"; |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
443 |
by (Fast_tac 1); |
923 | 444 |
qed "not_leE"; |
445 |
||
446 |
goalw Nat.thy [le_def] "!!m. m < n ==> Suc(m) <= n"; |
|
1660 | 447 |
by (simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
448 |
by (fast_tac (!claset addEs [less_irrefl,less_asym]) 1); |
923 | 449 |
qed "lessD"; |
450 |
||
451 |
goalw Nat.thy [le_def] "!!m. Suc(m) <= n ==> m <= n"; |
|
1660 | 452 |
by (asm_full_simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
923 | 453 |
qed "Suc_leD"; |
454 |
||
1817 | 455 |
(* stronger version of Suc_leD *) |
456 |
goalw Nat.thy [le_def] |
|
457 |
"!!m. Suc m <= n ==> m < n"; |
|
458 |
by (asm_full_simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
|
459 |
by (cut_facts_tac [less_linear] 1); |
|
1823 | 460 |
by (Fast_tac 1); |
1817 | 461 |
qed "Suc_le_lessD"; |
462 |
||
463 |
goal Nat.thy "(Suc m <= n) = (m < n)"; |
|
464 |
by (fast_tac (!claset addIs [lessD, Suc_le_lessD]) 1); |
|
465 |
qed "Suc_le_eq"; |
|
466 |
||
1327
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
467 |
goalw Nat.thy [le_def] "!!m. m <= n ==> m <= Suc n"; |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
468 |
by (fast_tac (!claset addDs [Suc_lessD]) 1); |
1327
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
469 |
qed "le_SucI"; |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
470 |
Addsimps[le_SucI]; |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
471 |
|
923 | 472 |
goalw Nat.thy [le_def] "!!m. m < n ==> m <= (n::nat)"; |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
473 |
by (fast_tac (!claset addEs [less_asym]) 1); |
923 | 474 |
qed "less_imp_le"; |
475 |
||
476 |
goalw Nat.thy [le_def] "!!m. m <= n ==> m < n | m=(n::nat)"; |
|
477 |
by (cut_facts_tac [less_linear] 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
478 |
by (fast_tac (!claset addEs [less_irrefl,less_asym]) 1); |
923 | 479 |
qed "le_imp_less_or_eq"; |
480 |
||
481 |
goalw Nat.thy [le_def] "!!m. m<n | m=n ==> m <=(n::nat)"; |
|
482 |
by (cut_facts_tac [less_linear] 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
483 |
by (fast_tac (!claset addEs [less_irrefl,less_asym]) 1); |
923 | 484 |
by (flexflex_tac); |
485 |
qed "less_or_eq_imp_le"; |
|
486 |
||
487 |
goal Nat.thy "(x <= (y::nat)) = (x < y | x=y)"; |
|
488 |
by (REPEAT(ares_tac [iffI,less_or_eq_imp_le,le_imp_less_or_eq] 1)); |
|
489 |
qed "le_eq_less_or_eq"; |
|
490 |
||
491 |
goal Nat.thy "n <= (n::nat)"; |
|
1552 | 492 |
by (simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1); |
923 | 493 |
qed "le_refl"; |
494 |
||
495 |
val prems = goal Nat.thy "!!i. [| i <= j; j < k |] ==> i < (k::nat)"; |
|
496 |
by (dtac le_imp_less_or_eq 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
497 |
by (fast_tac (!claset addIs [less_trans]) 1); |
923 | 498 |
qed "le_less_trans"; |
499 |
||
500 |
goal Nat.thy "!!i. [| i < j; j <= k |] ==> i < (k::nat)"; |
|
501 |
by (dtac le_imp_less_or_eq 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
502 |
by (fast_tac (!claset addIs [less_trans]) 1); |
923 | 503 |
qed "less_le_trans"; |
504 |
||
505 |
goal Nat.thy "!!i. [| i <= j; j <= k |] ==> i <= (k::nat)"; |
|
506 |
by (EVERY1[dtac le_imp_less_or_eq, dtac le_imp_less_or_eq, |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
507 |
rtac less_or_eq_imp_le, fast_tac (!claset addIs [less_trans])]); |
923 | 508 |
qed "le_trans"; |
509 |
||
510 |
val prems = goal Nat.thy "!!m. [| m <= n; n <= m |] ==> m = (n::nat)"; |
|
511 |
by (EVERY1[dtac le_imp_less_or_eq, dtac le_imp_less_or_eq, |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
512 |
fast_tac (!claset addEs [less_irrefl,less_asym])]); |
923 | 513 |
qed "le_anti_sym"; |
514 |
||
515 |
goal Nat.thy "(Suc(n) <= Suc(m)) = (n <= m)"; |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1024
diff
changeset
|
516 |
by (simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1); |
923 | 517 |
qed "Suc_le_mono"; |
518 |
||
2009 | 519 |
AddIffs [le_refl,Suc_le_mono]; |
1531 | 520 |
|
521 |
||
522 |
(** LEAST -- the least number operator **) |
|
523 |
||
524 |
val [prem1,prem2] = goalw Nat.thy [Least_def] |
|
525 |
"[| P(k); !!x. x<k ==> ~P(x) |] ==> (LEAST x.P(x)) = k"; |
|
526 |
by (rtac select_equality 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
527 |
by (fast_tac (!claset addSIs [prem1,prem2]) 1); |
1531 | 528 |
by (cut_facts_tac [less_linear] 1); |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
529 |
by (fast_tac (!claset addSIs [prem1] addSDs [prem2]) 1); |
1531 | 530 |
qed "Least_equality"; |
531 |
||
532 |
val [prem] = goal Nat.thy "P(k) ==> P(LEAST x.P(x))"; |
|
533 |
by (rtac (prem RS rev_mp) 1); |
|
534 |
by (res_inst_tac [("n","k")] less_induct 1); |
|
535 |
by (rtac impI 1); |
|
536 |
by (rtac classical 1); |
|
537 |
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1); |
|
538 |
by (assume_tac 1); |
|
539 |
by (assume_tac 2); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
540 |
by (Fast_tac 1); |
1531 | 541 |
qed "LeastI"; |
542 |
||
543 |
(*Proof is almost identical to the one above!*) |
|
544 |
val [prem] = goal Nat.thy "P(k) ==> (LEAST x.P(x)) <= k"; |
|
545 |
by (rtac (prem RS rev_mp) 1); |
|
546 |
by (res_inst_tac [("n","k")] less_induct 1); |
|
547 |
by (rtac impI 1); |
|
548 |
by (rtac classical 1); |
|
549 |
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1); |
|
550 |
by (assume_tac 1); |
|
551 |
by (rtac le_refl 2); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1679
diff
changeset
|
552 |
by (fast_tac (!claset addIs [less_imp_le,le_trans]) 1); |
1531 | 553 |
qed "Least_le"; |
554 |
||
555 |
val [prem] = goal Nat.thy "k < (LEAST x.P(x)) ==> ~P(k)"; |
|
556 |
by (rtac notI 1); |
|
557 |
by (etac (rewrite_rule [le_def] Least_le RS notE) 1); |
|
558 |
by (rtac prem 1); |
|
559 |
qed "not_less_Least"; |
|
1660 | 560 |
|
561 |
qed_goalw "Least_Suc" Nat.thy [Least_def] |
|
1931 | 562 |
"!!P. [| ? n. P(Suc n); ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))" |
563 |
(fn _ => [ |
|
2031 | 564 |
rtac select_equality 1, |
565 |
fold_goals_tac [Least_def], |
|
566 |
safe_tac (!claset addSEs [LeastI]), |
|
567 |
res_inst_tac [("n","j")] natE 1, |
|
568 |
Fast_tac 1, |
|
569 |
fast_tac (!claset addDs [Suc_less_SucD, not_less_Least]) 1, |
|
570 |
res_inst_tac [("n","k")] natE 1, |
|
571 |
Fast_tac 1, |
|
572 |
hyp_subst_tac 1, |
|
573 |
rewtac Least_def, |
|
574 |
rtac (select_equality RS arg_cong RS sym) 1, |
|
575 |
safe_tac (!claset), |
|
576 |
dtac Suc_mono 1, |
|
577 |
Fast_tac 1, |
|
578 |
cut_facts_tac [less_linear] 1, |
|
579 |
safe_tac (!claset), |
|
580 |
atac 2, |
|
581 |
Fast_tac 2, |
|
582 |
dtac Suc_mono 1, |
|
583 |
Fast_tac 1]); |