author | nipkow |
Thu, 30 Oct 1997 09:45:03 +0100 | |
changeset 4032 | 4b1c69d8b767 |
parent 4000 | 0614fdf0db20 |
child 4089 | 96fba19bcbe2 |
permissions | -rw-r--r-- |
2608 | 1 |
(* Title: HOL/NatDef.ML |
2 |
ID: $Id$ |
|
3 |
Author: Tobias Nipkow, Cambridge University Computer Laboratory |
|
4 |
Copyright 1991 University of Cambridge |
|
5 |
*) |
|
6 |
||
7 |
goal thy "mono(%X. {Zero_Rep} Un (Suc_Rep``X))"; |
|
8 |
by (REPEAT (ares_tac [monoI, subset_refl, image_mono, Un_mono] 1)); |
|
9 |
qed "Nat_fun_mono"; |
|
10 |
||
11 |
val Nat_unfold = Nat_fun_mono RS (Nat_def RS def_lfp_Tarski); |
|
12 |
||
13 |
(* Zero is a natural number -- this also justifies the type definition*) |
|
14 |
goal thy "Zero_Rep: Nat"; |
|
15 |
by (stac Nat_unfold 1); |
|
16 |
by (rtac (singletonI RS UnI1) 1); |
|
17 |
qed "Zero_RepI"; |
|
18 |
||
19 |
val prems = goal thy "i: Nat ==> Suc_Rep(i) : Nat"; |
|
20 |
by (stac Nat_unfold 1); |
|
21 |
by (rtac (imageI RS UnI2) 1); |
|
22 |
by (resolve_tac prems 1); |
|
23 |
qed "Suc_RepI"; |
|
24 |
||
25 |
(*** Induction ***) |
|
26 |
||
27 |
val major::prems = goal thy |
|
28 |
"[| i: Nat; P(Zero_Rep); \ |
|
29 |
\ !!j. [| j: Nat; P(j) |] ==> P(Suc_Rep(j)) |] ==> P(i)"; |
|
30 |
by (rtac ([Nat_def, Nat_fun_mono, major] MRS def_induct) 1); |
|
2891 | 31 |
by (blast_tac (!claset addIs prems) 1); |
2608 | 32 |
qed "Nat_induct"; |
33 |
||
34 |
val prems = goalw thy [Zero_def,Suc_def] |
|
35 |
"[| P(0); \ |
|
3040
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
3023
diff
changeset
|
36 |
\ !!n. P(n) ==> P(Suc(n)) |] ==> P(n)"; |
2608 | 37 |
by (rtac (Rep_Nat_inverse RS subst) 1); (*types force good instantiation*) |
38 |
by (rtac (Rep_Nat RS Nat_induct) 1); |
|
39 |
by (REPEAT (ares_tac prems 1 |
|
40 |
ORELSE eresolve_tac [Abs_Nat_inverse RS subst] 1)); |
|
41 |
qed "nat_induct"; |
|
42 |
||
43 |
(*Perform induction on n. *) |
|
3563 | 44 |
local fun raw_nat_ind_tac a i = |
45 |
res_inst_tac [("n",a)] nat_induct i THEN rename_last_tac a [""] (i+1) |
|
46 |
in |
|
47 |
val nat_ind_tac = Datatype.occs_in_prems raw_nat_ind_tac |
|
48 |
end; |
|
3040
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
3023
diff
changeset
|
49 |
|
2608 | 50 |
(*A special form of induction for reasoning about m<n and m-n*) |
51 |
val prems = goal thy |
|
52 |
"[| !!x. P x 0; \ |
|
53 |
\ !!y. P 0 (Suc y); \ |
|
54 |
\ !!x y. [| P x y |] ==> P (Suc x) (Suc y) \ |
|
55 |
\ |] ==> P m n"; |
|
56 |
by (res_inst_tac [("x","m")] spec 1); |
|
57 |
by (nat_ind_tac "n" 1); |
|
58 |
by (rtac allI 2); |
|
59 |
by (nat_ind_tac "x" 2); |
|
60 |
by (REPEAT (ares_tac (prems@[allI]) 1 ORELSE etac spec 1)); |
|
61 |
qed "diff_induct"; |
|
62 |
||
63 |
(*Case analysis on the natural numbers*) |
|
64 |
val prems = goal thy |
|
65 |
"[| n=0 ==> P; !!x. n = Suc(x) ==> P |] ==> P"; |
|
66 |
by (subgoal_tac "n=0 | (EX x. n = Suc(x))" 1); |
|
67 |
by (fast_tac (!claset addSEs prems) 1); |
|
68 |
by (nat_ind_tac "n" 1); |
|
69 |
by (rtac (refl RS disjI1) 1); |
|
2891 | 70 |
by (Blast_tac 1); |
2608 | 71 |
qed "natE"; |
72 |
||
3282
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3236
diff
changeset
|
73 |
(*Install 'automatic' induction tactic, pretending nat is a datatype *) |
3292 | 74 |
(*except for induct_tac and exhaust_tac, everything is dummy*) |
3282
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3236
diff
changeset
|
75 |
datatypes := [("nat",{case_const = Bound 0, case_rewrites = [], |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3236
diff
changeset
|
76 |
constructors = [], induct_tac = nat_ind_tac, |
3292 | 77 |
exhaustion = natE, |
78 |
exhaust_tac = fn v => ALLNEWSUBGOALS (res_inst_tac [("n",v)] natE) |
|
79 |
(rotate_tac ~1), |
|
4000 | 80 |
nchotomy = ProtoPure.flexpair_def, case_cong = ProtoPure.flexpair_def})]; |
3282
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3236
diff
changeset
|
81 |
|
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3236
diff
changeset
|
82 |
|
2608 | 83 |
(*** Isomorphisms: Abs_Nat and Rep_Nat ***) |
84 |
||
85 |
(*We can't take these properties as axioms, or take Abs_Nat==Inv(Rep_Nat), |
|
86 |
since we assume the isomorphism equations will one day be given by Isabelle*) |
|
87 |
||
88 |
goal thy "inj(Rep_Nat)"; |
|
89 |
by (rtac inj_inverseI 1); |
|
90 |
by (rtac Rep_Nat_inverse 1); |
|
91 |
qed "inj_Rep_Nat"; |
|
92 |
||
93 |
goal thy "inj_onto Abs_Nat Nat"; |
|
94 |
by (rtac inj_onto_inverseI 1); |
|
95 |
by (etac Abs_Nat_inverse 1); |
|
96 |
qed "inj_onto_Abs_Nat"; |
|
97 |
||
98 |
(*** Distinctness of constructors ***) |
|
99 |
||
100 |
goalw thy [Zero_def,Suc_def] "Suc(m) ~= 0"; |
|
101 |
by (rtac (inj_onto_Abs_Nat RS inj_onto_contraD) 1); |
|
102 |
by (rtac Suc_Rep_not_Zero_Rep 1); |
|
103 |
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI, Zero_RepI] 1)); |
|
104 |
qed "Suc_not_Zero"; |
|
105 |
||
106 |
bind_thm ("Zero_not_Suc", Suc_not_Zero RS not_sym); |
|
107 |
||
108 |
AddIffs [Suc_not_Zero,Zero_not_Suc]; |
|
109 |
||
110 |
bind_thm ("Suc_neq_Zero", (Suc_not_Zero RS notE)); |
|
111 |
val Zero_neq_Suc = sym RS Suc_neq_Zero; |
|
112 |
||
113 |
(** Injectiveness of Suc **) |
|
114 |
||
115 |
goalw thy [Suc_def] "inj(Suc)"; |
|
116 |
by (rtac injI 1); |
|
117 |
by (dtac (inj_onto_Abs_Nat RS inj_ontoD) 1); |
|
118 |
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI] 1)); |
|
119 |
by (dtac (inj_Suc_Rep RS injD) 1); |
|
120 |
by (etac (inj_Rep_Nat RS injD) 1); |
|
121 |
qed "inj_Suc"; |
|
122 |
||
123 |
val Suc_inject = inj_Suc RS injD; |
|
124 |
||
125 |
goal thy "(Suc(m)=Suc(n)) = (m=n)"; |
|
126 |
by (EVERY1 [rtac iffI, etac Suc_inject, etac arg_cong]); |
|
127 |
qed "Suc_Suc_eq"; |
|
128 |
||
129 |
AddIffs [Suc_Suc_eq]; |
|
130 |
||
131 |
goal thy "n ~= Suc(n)"; |
|
132 |
by (nat_ind_tac "n" 1); |
|
133 |
by (ALLGOALS Asm_simp_tac); |
|
134 |
qed "n_not_Suc_n"; |
|
135 |
||
136 |
bind_thm ("Suc_n_not_n", n_not_Suc_n RS not_sym); |
|
137 |
||
3236 | 138 |
goal thy "!!n. n ~= 0 ==> EX m. n = Suc m"; |
3457 | 139 |
by (rtac natE 1); |
3236 | 140 |
by (REPEAT (Blast_tac 1)); |
141 |
qed "not0_implies_Suc"; |
|
142 |
||
143 |
||
2608 | 144 |
(*** nat_case -- the selection operator for nat ***) |
145 |
||
146 |
goalw thy [nat_case_def] "nat_case a f 0 = a"; |
|
2891 | 147 |
by (blast_tac (!claset addIs [select_equality]) 1); |
2608 | 148 |
qed "nat_case_0"; |
149 |
||
150 |
goalw thy [nat_case_def] "nat_case a f (Suc k) = f(k)"; |
|
2891 | 151 |
by (blast_tac (!claset addIs [select_equality]) 1); |
2608 | 152 |
qed "nat_case_Suc"; |
153 |
||
3236 | 154 |
goalw thy [wf_def, pred_nat_def] "wf(pred_nat)"; |
3718 | 155 |
by (Clarify_tac 1); |
2608 | 156 |
by (nat_ind_tac "x" 1); |
3236 | 157 |
by (ALLGOALS Blast_tac); |
2608 | 158 |
qed "wf_pred_nat"; |
159 |
||
160 |
||
161 |
(*** nat_rec -- by wf recursion on pred_nat ***) |
|
162 |
||
163 |
(* The unrolling rule for nat_rec *) |
|
164 |
goal thy |
|
165 |
"(%n. nat_rec c d n) = wfrec pred_nat (%f. nat_case ?c (%m. ?d m (f m)))"; |
|
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 |
*---------------------------------------------------------------------------*) |
|
174 |
||
175 |
(** conversion rules **) |
|
176 |
||
177 |
goal thy "nat_rec c h 0 = c"; |
|
178 |
by (rtac (nat_rec_unfold RS trans) 1); |
|
179 |
by (simp_tac (!simpset addsimps [nat_case_0]) 1); |
|
180 |
qed "nat_rec_0"; |
|
181 |
||
182 |
goal thy "nat_rec c h (Suc n) = h n (nat_rec c h n)"; |
|
183 |
by (rtac (nat_rec_unfold RS trans) 1); |
|
3236 | 184 |
by (simp_tac (!simpset addsimps [nat_case_Suc, pred_nat_def, cut_apply]) 1); |
2608 | 185 |
qed "nat_rec_Suc"; |
186 |
||
187 |
(*These 2 rules ease the use of primitive recursion. NOTE USE OF == *) |
|
188 |
val [rew] = goal thy |
|
189 |
"[| !!n. f(n) == nat_rec c h n |] ==> f(0) = c"; |
|
190 |
by (rewtac rew); |
|
191 |
by (rtac nat_rec_0 1); |
|
192 |
qed "def_nat_rec_0"; |
|
193 |
||
194 |
val [rew] = goal thy |
|
195 |
"[| !!n. f(n) == nat_rec c h n |] ==> f(Suc(n)) = h n (f n)"; |
|
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 |
||
3378 | 207 |
(*Used in TFL/post.sml*) |
208 |
goalw thy [less_def] "(m,n) : pred_nat^+ = (m<n)"; |
|
209 |
by (rtac refl 1); |
|
210 |
qed "less_eq"; |
|
211 |
||
2608 | 212 |
(** Introduction properties **) |
213 |
||
214 |
val prems = goalw thy [less_def] "[| i<j; j<k |] ==> i<(k::nat)"; |
|
215 |
by (rtac (trans_trancl RS transD) 1); |
|
216 |
by (resolve_tac prems 1); |
|
217 |
by (resolve_tac prems 1); |
|
218 |
qed "less_trans"; |
|
219 |
||
3236 | 220 |
goalw thy [less_def, pred_nat_def] "n < Suc(n)"; |
221 |
by (simp_tac (!simpset addsimps [r_into_trancl]) 1); |
|
2608 | 222 |
qed "lessI"; |
223 |
AddIffs [lessI]; |
|
224 |
||
225 |
(* i<j ==> i<Suc(j) *) |
|
226 |
bind_thm("less_SucI", lessI RSN (2, less_trans)); |
|
227 |
Addsimps [less_SucI]; |
|
228 |
||
229 |
goal thy "0 < Suc(n)"; |
|
230 |
by (nat_ind_tac "n" 1); |
|
231 |
by (rtac lessI 1); |
|
232 |
by (etac less_trans 1); |
|
233 |
by (rtac lessI 1); |
|
234 |
qed "zero_less_Suc"; |
|
235 |
AddIffs [zero_less_Suc]; |
|
236 |
||
237 |
(** Elimination properties **) |
|
238 |
||
239 |
val prems = goalw thy [less_def] "n<m ==> ~ m<(n::nat)"; |
|
2891 | 240 |
by (blast_tac (!claset addIs ([wf_pred_nat, wf_trancl RS wf_asym]@prems))1); |
2608 | 241 |
qed "less_not_sym"; |
242 |
||
243 |
(* [| n<m; m<n |] ==> R *) |
|
244 |
bind_thm ("less_asym", (less_not_sym RS notE)); |
|
245 |
||
246 |
goalw thy [less_def] "~ n<(n::nat)"; |
|
247 |
by (rtac notI 1); |
|
248 |
by (etac (wf_pred_nat RS wf_trancl RS wf_irrefl) 1); |
|
249 |
qed "less_not_refl"; |
|
250 |
||
251 |
(* n<n ==> R *) |
|
252 |
bind_thm ("less_irrefl", (less_not_refl RS notE)); |
|
253 |
||
254 |
goal thy "!!m. n<m ==> m ~= (n::nat)"; |
|
3085 | 255 |
by (blast_tac (!claset addSEs [less_irrefl]) 1); |
2608 | 256 |
qed "less_not_refl2"; |
257 |
||
258 |
||
3236 | 259 |
val major::prems = goalw thy [less_def, pred_nat_def] |
2608 | 260 |
"[| i<k; k=Suc(i) ==> P; !!j. [| i<j; k=Suc(j) |] ==> P \ |
261 |
\ |] ==> P"; |
|
262 |
by (rtac (major RS tranclE) 1); |
|
3236 | 263 |
by (ALLGOALS Full_simp_tac); |
2608 | 264 |
by (REPEAT_FIRST (bound_hyp_subst_tac ORELSE' |
3236 | 265 |
eresolve_tac (prems@[asm_rl, Pair_inject]))); |
2608 | 266 |
qed "lessE"; |
267 |
||
268 |
goal thy "~ n<0"; |
|
269 |
by (rtac notI 1); |
|
270 |
by (etac lessE 1); |
|
271 |
by (etac Zero_neq_Suc 1); |
|
272 |
by (etac Zero_neq_Suc 1); |
|
273 |
qed "not_less0"; |
|
274 |
||
275 |
AddIffs [not_less0]; |
|
276 |
||
277 |
(* n<0 ==> R *) |
|
278 |
bind_thm ("less_zeroE", not_less0 RS notE); |
|
279 |
||
280 |
val [major,less,eq] = goal thy |
|
281 |
"[| m < Suc(n); m<n ==> P; m=n ==> P |] ==> P"; |
|
282 |
by (rtac (major RS lessE) 1); |
|
283 |
by (rtac eq 1); |
|
2891 | 284 |
by (Blast_tac 1); |
2608 | 285 |
by (rtac less 1); |
2891 | 286 |
by (Blast_tac 1); |
2608 | 287 |
qed "less_SucE"; |
288 |
||
289 |
goal thy "(m < Suc(n)) = (m < n | m = n)"; |
|
2935 | 290 |
by (blast_tac (!claset addSEs [less_SucE] addIs [less_trans]) 1); |
2608 | 291 |
qed "less_Suc_eq"; |
292 |
||
3484
1e93eb09ebb9
Added the following lemmas tp Divides and a few others to Arith and NatDef:
nipkow
parents:
3457
diff
changeset
|
293 |
goal thy "(n<1) = (n=0)"; |
1e93eb09ebb9
Added the following lemmas tp Divides and a few others to Arith and NatDef:
nipkow
parents:
3457
diff
changeset
|
294 |
by (simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
1e93eb09ebb9
Added the following lemmas tp Divides and a few others to Arith and NatDef:
nipkow
parents:
3457
diff
changeset
|
295 |
qed "less_one"; |
1e93eb09ebb9
Added the following lemmas tp Divides and a few others to Arith and NatDef:
nipkow
parents:
3457
diff
changeset
|
296 |
AddIffs [less_one]; |
1e93eb09ebb9
Added the following lemmas tp Divides and a few others to Arith and NatDef:
nipkow
parents:
3457
diff
changeset
|
297 |
|
2608 | 298 |
val prems = goal thy "m<n ==> n ~= 0"; |
299 |
by (res_inst_tac [("n","n")] natE 1); |
|
300 |
by (cut_facts_tac prems 1); |
|
301 |
by (ALLGOALS Asm_full_simp_tac); |
|
302 |
qed "gr_implies_not0"; |
|
303 |
Addsimps [gr_implies_not0]; |
|
304 |
||
305 |
qed_goal "zero_less_eq" thy "0 < n = (n ~= 0)" (fn _ => [ |
|
306 |
rtac iffI 1, |
|
307 |
etac gr_implies_not0 1, |
|
308 |
rtac natE 1, |
|
309 |
contr_tac 1, |
|
310 |
etac ssubst 1, |
|
311 |
rtac zero_less_Suc 1]); |
|
312 |
||
313 |
(** Inductive (?) properties **) |
|
314 |
||
315 |
val [prem] = goal thy "Suc(m) < n ==> m<n"; |
|
316 |
by (rtac (prem RS rev_mp) 1); |
|
317 |
by (nat_ind_tac "n" 1); |
|
318 |
by (ALLGOALS (fast_tac (!claset addSIs [lessI RS less_SucI] |
|
319 |
addEs [less_trans, lessE]))); |
|
320 |
qed "Suc_lessD"; |
|
321 |
||
322 |
val [major,minor] = goal thy |
|
323 |
"[| Suc(i)<k; !!j. [| i<j; k=Suc(j) |] ==> P \ |
|
324 |
\ |] ==> P"; |
|
325 |
by (rtac (major RS lessE) 1); |
|
326 |
by (etac (lessI RS minor) 1); |
|
327 |
by (etac (Suc_lessD RS minor) 1); |
|
328 |
by (assume_tac 1); |
|
329 |
qed "Suc_lessE"; |
|
330 |
||
331 |
goal thy "!!m n. Suc(m) < Suc(n) ==> m<n"; |
|
2891 | 332 |
by (blast_tac (!claset addEs [lessE, make_elim Suc_lessD]) 1); |
2608 | 333 |
qed "Suc_less_SucD"; |
334 |
||
335 |
goal thy "!!m n. m<n ==> Suc(m) < Suc(n)"; |
|
336 |
by (etac rev_mp 1); |
|
337 |
by (nat_ind_tac "n" 1); |
|
2891 | 338 |
by (ALLGOALS (fast_tac (!claset addEs [less_trans, lessE]))); |
2608 | 339 |
qed "Suc_mono"; |
340 |
||
341 |
||
342 |
goal thy "(Suc(m) < Suc(n)) = (m<n)"; |
|
343 |
by (EVERY1 [rtac iffI, etac Suc_less_SucD, etac Suc_mono]); |
|
344 |
qed "Suc_less_eq"; |
|
345 |
Addsimps [Suc_less_eq]; |
|
346 |
||
347 |
goal thy "~(Suc(n) < n)"; |
|
2891 | 348 |
by (blast_tac (!claset addEs [Suc_lessD RS less_irrefl]) 1); |
2608 | 349 |
qed "not_Suc_n_less_n"; |
350 |
Addsimps [not_Suc_n_less_n]; |
|
351 |
||
352 |
goal thy "!!i. i<j ==> j<k --> Suc i < k"; |
|
353 |
by (nat_ind_tac "k" 1); |
|
354 |
by (ALLGOALS (asm_simp_tac (!simpset))); |
|
355 |
by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
|
2891 | 356 |
by (blast_tac (!claset addDs [Suc_lessD]) 1); |
2608 | 357 |
qed_spec_mp "less_trans_Suc"; |
358 |
||
359 |
(*"Less than" is a linear ordering*) |
|
360 |
goal thy "m<n | m=n | n<(m::nat)"; |
|
361 |
by (nat_ind_tac "m" 1); |
|
362 |
by (nat_ind_tac "n" 1); |
|
363 |
by (rtac (refl RS disjI1 RS disjI2) 1); |
|
364 |
by (rtac (zero_less_Suc RS disjI1) 1); |
|
2935 | 365 |
by (blast_tac (!claset addIs [Suc_mono, less_SucI] addEs [lessE]) 1); |
2608 | 366 |
qed "less_linear"; |
367 |
||
368 |
qed_goal "nat_less_cases" thy |
|
369 |
"[| (m::nat)<n ==> P n m; m=n ==> P n m; n<m ==> P n m |] ==> P n m" |
|
2935 | 370 |
( fn [major,eqCase,lessCase] => |
2608 | 371 |
[ |
2935 | 372 |
(rtac (less_linear RS disjE) 1), |
2608 | 373 |
(etac disjE 2), |
2935 | 374 |
(etac lessCase 1), |
375 |
(etac (sym RS eqCase) 1), |
|
376 |
(etac major 1) |
|
2608 | 377 |
]); |
378 |
||
379 |
(*Can be used with less_Suc_eq to get n=m | n<m *) |
|
380 |
goal thy "(~ m < n) = (n < Suc(m))"; |
|
381 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
382 |
by (ALLGOALS Asm_simp_tac); |
|
383 |
qed "not_less_eq"; |
|
384 |
||
385 |
(*Complete induction, aka course-of-values induction*) |
|
386 |
val prems = goalw thy [less_def] |
|
387 |
"[| !!n. [| ! m::nat. m<n --> P(m) |] ==> P(n) |] ==> P(n)"; |
|
388 |
by (wf_ind_tac "n" [wf_pred_nat RS wf_trancl] 1); |
|
389 |
by (eresolve_tac prems 1); |
|
390 |
qed "less_induct"; |
|
391 |
||
392 |
qed_goal "nat_induct2" thy |
|
393 |
"[| P 0; P 1; !!k. P k ==> P (Suc (Suc k)) |] ==> P n" (fn prems => [ |
|
3023 | 394 |
cut_facts_tac prems 1, |
395 |
rtac less_induct 1, |
|
396 |
res_inst_tac [("n","n")] natE 1, |
|
397 |
hyp_subst_tac 1, |
|
398 |
atac 1, |
|
399 |
hyp_subst_tac 1, |
|
400 |
res_inst_tac [("n","x")] natE 1, |
|
401 |
hyp_subst_tac 1, |
|
402 |
atac 1, |
|
403 |
hyp_subst_tac 1, |
|
404 |
resolve_tac prems 1, |
|
405 |
dtac spec 1, |
|
406 |
etac mp 1, |
|
407 |
rtac (lessI RS less_trans) 1, |
|
408 |
rtac (lessI RS Suc_mono) 1]); |
|
2608 | 409 |
|
410 |
(*** Properties of <= ***) |
|
411 |
||
412 |
goalw thy [le_def] "(m <= n) = (m < Suc n)"; |
|
413 |
by (rtac not_less_eq 1); |
|
414 |
qed "le_eq_less_Suc"; |
|
415 |
||
3343 | 416 |
(* m<=n ==> m < Suc n *) |
417 |
bind_thm ("le_imp_less_Suc", le_eq_less_Suc RS iffD1); |
|
418 |
||
2608 | 419 |
goalw thy [le_def] "0 <= n"; |
420 |
by (rtac not_less0 1); |
|
421 |
qed "le0"; |
|
422 |
||
423 |
goalw thy [le_def] "~ Suc n <= n"; |
|
424 |
by (Simp_tac 1); |
|
425 |
qed "Suc_n_not_le_n"; |
|
426 |
||
427 |
goalw thy [le_def] "(i <= 0) = (i = 0)"; |
|
428 |
by (nat_ind_tac "i" 1); |
|
429 |
by (ALLGOALS Asm_simp_tac); |
|
430 |
qed "le_0_eq"; |
|
431 |
||
432 |
Addsimps [(*less_Suc_eq, makes simpset non-confluent*) le0, le_0_eq, |
|
433 |
Suc_n_not_le_n, |
|
434 |
n_not_Suc_n, Suc_n_not_n, |
|
435 |
nat_case_0, nat_case_Suc, nat_rec_0, nat_rec_Suc]; |
|
436 |
||
3355 | 437 |
goal thy "!!m. (m <= Suc(n)) = (m<=n | m = Suc n)"; |
438 |
by (simp_tac (!simpset addsimps [le_eq_less_Suc]) 1); |
|
439 |
by (blast_tac (!claset addSEs [less_SucE] addIs [less_SucI]) 1); |
|
440 |
qed "le_Suc_eq"; |
|
441 |
||
2608 | 442 |
(* |
443 |
goal thy "(Suc m < n | Suc m = n) = (m < n)"; |
|
444 |
by (stac (less_Suc_eq RS sym) 1); |
|
445 |
by (rtac Suc_less_eq 1); |
|
446 |
qed "Suc_le_eq"; |
|
447 |
||
448 |
this could make the simpset (with less_Suc_eq added again) more confluent, |
|
449 |
but less_Suc_eq makes additional problems with terms of the form 0 < Suc (...) |
|
450 |
*) |
|
451 |
||
452 |
(*Prevents simplification of f and g: much faster*) |
|
453 |
qed_goal "nat_case_weak_cong" thy |
|
454 |
"m=n ==> nat_case a f m = nat_case a f n" |
|
455 |
(fn [prem] => [rtac (prem RS arg_cong) 1]); |
|
456 |
||
457 |
qed_goal "nat_rec_weak_cong" thy |
|
458 |
"m=n ==> nat_rec a f m = nat_rec a f n" |
|
459 |
(fn [prem] => [rtac (prem RS arg_cong) 1]); |
|
460 |
||
461 |
qed_goal "expand_nat_case" thy |
|
462 |
"P(nat_case z s n) = ((n=0 --> P z) & (!m. n = Suc m --> P(s m)))" |
|
463 |
(fn _ => [nat_ind_tac "n" 1, ALLGOALS Asm_simp_tac]); |
|
464 |
||
465 |
val prems = goalw thy [le_def] "~n<m ==> m<=(n::nat)"; |
|
466 |
by (resolve_tac prems 1); |
|
467 |
qed "leI"; |
|
468 |
||
469 |
val prems = goalw thy [le_def] "m<=n ==> ~ n < (m::nat)"; |
|
470 |
by (resolve_tac prems 1); |
|
471 |
qed "leD"; |
|
472 |
||
473 |
val leE = make_elim leD; |
|
474 |
||
475 |
goal thy "(~n<m) = (m<=(n::nat))"; |
|
2891 | 476 |
by (blast_tac (!claset addIs [leI] addEs [leE]) 1); |
2608 | 477 |
qed "not_less_iff_le"; |
478 |
||
479 |
goalw thy [le_def] "!!m. ~ m <= n ==> n<(m::nat)"; |
|
2891 | 480 |
by (Blast_tac 1); |
2608 | 481 |
qed "not_leE"; |
482 |
||
483 |
goalw thy [le_def] "!!m. m < n ==> Suc(m) <= n"; |
|
484 |
by (simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
|
3085 | 485 |
by (blast_tac (!claset addSEs [less_irrefl,less_asym]) 1); |
3343 | 486 |
qed "Suc_leI"; (*formerly called lessD*) |
2608 | 487 |
|
488 |
goalw thy [le_def] "!!m. Suc(m) <= n ==> m <= n"; |
|
489 |
by (asm_full_simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
|
490 |
qed "Suc_leD"; |
|
491 |
||
492 |
(* stronger version of Suc_leD *) |
|
493 |
goalw thy [le_def] |
|
494 |
"!!m. Suc m <= n ==> m < n"; |
|
495 |
by (asm_full_simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
|
496 |
by (cut_facts_tac [less_linear] 1); |
|
2891 | 497 |
by (Blast_tac 1); |
2608 | 498 |
qed "Suc_le_lessD"; |
499 |
||
500 |
goal thy "(Suc m <= n) = (m < n)"; |
|
3343 | 501 |
by (blast_tac (!claset addIs [Suc_leI, Suc_le_lessD]) 1); |
2608 | 502 |
qed "Suc_le_eq"; |
503 |
||
504 |
goalw thy [le_def] "!!m. m <= n ==> m <= Suc n"; |
|
2891 | 505 |
by (blast_tac (!claset addDs [Suc_lessD]) 1); |
2608 | 506 |
qed "le_SucI"; |
507 |
Addsimps[le_SucI]; |
|
508 |
||
509 |
bind_thm ("le_Suc", not_Suc_n_less_n RS leI); |
|
510 |
||
511 |
goalw thy [le_def] "!!m. m < n ==> m <= (n::nat)"; |
|
2891 | 512 |
by (blast_tac (!claset addEs [less_asym]) 1); |
2608 | 513 |
qed "less_imp_le"; |
514 |
||
3343 | 515 |
(** Equivalence of m<=n and m<n | m=n **) |
516 |
||
2608 | 517 |
goalw thy [le_def] "!!m. m <= n ==> m < n | m=(n::nat)"; |
518 |
by (cut_facts_tac [less_linear] 1); |
|
2891 | 519 |
by (blast_tac (!claset addEs [less_irrefl,less_asym]) 1); |
2608 | 520 |
qed "le_imp_less_or_eq"; |
521 |
||
522 |
goalw thy [le_def] "!!m. m<n | m=n ==> m <=(n::nat)"; |
|
523 |
by (cut_facts_tac [less_linear] 1); |
|
3085 | 524 |
by (blast_tac (!claset addSEs [less_irrefl] addEs [less_asym]) 1); |
2608 | 525 |
qed "less_or_eq_imp_le"; |
526 |
||
3343 | 527 |
goal thy "(m <= (n::nat)) = (m < n | m=n)"; |
2608 | 528 |
by (REPEAT(ares_tac [iffI,less_or_eq_imp_le,le_imp_less_or_eq] 1)); |
529 |
qed "le_eq_less_or_eq"; |
|
530 |
||
531 |
goal thy "n <= (n::nat)"; |
|
532 |
by (simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1); |
|
533 |
qed "le_refl"; |
|
534 |
||
535 |
val prems = goal thy "!!i. [| i <= j; j < k |] ==> i < (k::nat)"; |
|
536 |
by (dtac le_imp_less_or_eq 1); |
|
2935 | 537 |
by (blast_tac (!claset addIs [less_trans]) 1); |
2608 | 538 |
qed "le_less_trans"; |
539 |
||
540 |
goal thy "!!i. [| i < j; j <= k |] ==> i < (k::nat)"; |
|
541 |
by (dtac le_imp_less_or_eq 1); |
|
2935 | 542 |
by (blast_tac (!claset addIs [less_trans]) 1); |
2608 | 543 |
qed "less_le_trans"; |
544 |
||
545 |
goal thy "!!i. [| i <= j; j <= k |] ==> i <= (k::nat)"; |
|
2891 | 546 |
by (EVERY1[dtac le_imp_less_or_eq, |
3023 | 547 |
dtac le_imp_less_or_eq, |
548 |
rtac less_or_eq_imp_le, |
|
549 |
blast_tac (!claset addIs [less_trans])]); |
|
2608 | 550 |
qed "le_trans"; |
551 |
||
2891 | 552 |
goal thy "!!m. [| m <= n; n <= m |] ==> m = (n::nat)"; |
553 |
by (EVERY1[dtac le_imp_less_or_eq, |
|
3023 | 554 |
dtac le_imp_less_or_eq, |
555 |
blast_tac (!claset addEs [less_irrefl,less_asym])]); |
|
2608 | 556 |
qed "le_anti_sym"; |
557 |
||
558 |
goal thy "(Suc(n) <= Suc(m)) = (n <= m)"; |
|
559 |
by (simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1); |
|
560 |
qed "Suc_le_mono"; |
|
561 |
||
562 |
AddIffs [Suc_le_mono]; |
|
563 |
||
564 |
(* Axiom 'order_le_less' of class 'order': *) |
|
565 |
goal thy "(m::nat) < n = (m <= n & m ~= n)"; |
|
3023 | 566 |
by (rtac iffI 1); |
567 |
by (rtac conjI 1); |
|
568 |
by (etac less_imp_le 1); |
|
569 |
by (etac (less_not_refl2 RS not_sym) 1); |
|
570 |
by (blast_tac (!claset addSDs [le_imp_less_or_eq]) 1); |
|
2608 | 571 |
qed "nat_less_le"; |
572 |
||
573 |
(** LEAST -- the least number operator **) |
|
574 |
||
3143
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
575 |
goal thy "(! m::nat. P m --> n <= m) = (! m. m < n --> ~ P m)"; |
3457 | 576 |
by (blast_tac (!claset addIs [leI] addEs [leE]) 1); |
3143
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
577 |
val lemma = result(); |
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
578 |
|
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
579 |
(* This is an old def of Least for nat, which is derived for compatibility *) |
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
580 |
goalw thy [Least_def] |
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
581 |
"(LEAST n::nat. P n) == (@n. P(n) & (ALL m. m < n --> ~P(m)))"; |
3457 | 582 |
by (simp_tac (!simpset addsimps [lemma]) 1); |
583 |
by (rtac eq_reflection 1); |
|
584 |
by (rtac refl 1); |
|
3143
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
585 |
qed "Least_nat_def"; |
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
586 |
|
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
587 |
val [prem1,prem2] = goalw thy [Least_nat_def] |
3842 | 588 |
"[| P(k::nat); !!x. x<k ==> ~P(x) |] ==> (LEAST x. P(x)) = k"; |
2608 | 589 |
by (rtac select_equality 1); |
2891 | 590 |
by (blast_tac (!claset addSIs [prem1,prem2]) 1); |
2608 | 591 |
by (cut_facts_tac [less_linear] 1); |
2891 | 592 |
by (blast_tac (!claset addSIs [prem1] addSDs [prem2]) 1); |
2608 | 593 |
qed "Least_equality"; |
594 |
||
3842 | 595 |
val [prem] = goal thy "P(k::nat) ==> P(LEAST x. P(x))"; |
2608 | 596 |
by (rtac (prem RS rev_mp) 1); |
597 |
by (res_inst_tac [("n","k")] less_induct 1); |
|
598 |
by (rtac impI 1); |
|
599 |
by (rtac classical 1); |
|
600 |
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1); |
|
601 |
by (assume_tac 1); |
|
602 |
by (assume_tac 2); |
|
2891 | 603 |
by (Blast_tac 1); |
2608 | 604 |
qed "LeastI"; |
605 |
||
606 |
(*Proof is almost identical to the one above!*) |
|
3842 | 607 |
val [prem] = goal thy "P(k::nat) ==> (LEAST x. P(x)) <= k"; |
2608 | 608 |
by (rtac (prem RS rev_mp) 1); |
609 |
by (res_inst_tac [("n","k")] less_induct 1); |
|
610 |
by (rtac impI 1); |
|
611 |
by (rtac classical 1); |
|
612 |
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1); |
|
613 |
by (assume_tac 1); |
|
614 |
by (rtac le_refl 2); |
|
2891 | 615 |
by (blast_tac (!claset addIs [less_imp_le,le_trans]) 1); |
2608 | 616 |
qed "Least_le"; |
617 |
||
3842 | 618 |
val [prem] = goal thy "k < (LEAST x. P(x)) ==> ~P(k::nat)"; |
2608 | 619 |
by (rtac notI 1); |
620 |
by (etac (rewrite_rule [le_def] Least_le RS notE) 1); |
|
621 |
by (rtac prem 1); |
|
622 |
qed "not_less_Least"; |
|
623 |
||
3143
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
624 |
qed_goalw "Least_Suc" thy [Least_nat_def] |
2608 | 625 |
"!!P. [| ? n. P(Suc n); ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))" |
626 |
(fn _ => [ |
|
627 |
rtac select_equality 1, |
|
3143
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
628 |
fold_goals_tac [Least_nat_def], |
2608 | 629 |
safe_tac (!claset addSEs [LeastI]), |
630 |
rename_tac "j" 1, |
|
631 |
res_inst_tac [("n","j")] natE 1, |
|
2891 | 632 |
Blast_tac 1, |
633 |
blast_tac (!claset addDs [Suc_less_SucD, not_less_Least]) 1, |
|
2608 | 634 |
rename_tac "k n" 1, |
635 |
res_inst_tac [("n","k")] natE 1, |
|
2891 | 636 |
Blast_tac 1, |
2608 | 637 |
hyp_subst_tac 1, |
3143
d60e49b86c6a
Modified def of Least, which, as Markus correctly complained, looked like
nipkow
parents:
3085
diff
changeset
|
638 |
rewtac Least_nat_def, |
2608 | 639 |
rtac (select_equality RS arg_cong RS sym) 1, |
640 |
safe_tac (!claset), |
|
641 |
dtac Suc_mono 1, |
|
2891 | 642 |
Blast_tac 1, |
2608 | 643 |
cut_facts_tac [less_linear] 1, |
644 |
safe_tac (!claset), |
|
645 |
atac 2, |
|
2891 | 646 |
Blast_tac 2, |
2608 | 647 |
dtac Suc_mono 1, |
2891 | 648 |
Blast_tac 1]); |
2608 | 649 |
|
650 |
||
651 |
(*** Instantiation of transitivity prover ***) |
|
652 |
||
653 |
structure Less_Arith = |
|
654 |
struct |
|
655 |
val nat_leI = leI; |
|
656 |
val nat_leD = leD; |
|
657 |
val lessI = lessI; |
|
658 |
val zero_less_Suc = zero_less_Suc; |
|
659 |
val less_reflE = less_irrefl; |
|
660 |
val less_zeroE = less_zeroE; |
|
661 |
val less_incr = Suc_mono; |
|
662 |
val less_decr = Suc_less_SucD; |
|
663 |
val less_incr_rhs = Suc_mono RS Suc_lessD; |
|
664 |
val less_decr_lhs = Suc_lessD; |
|
665 |
val less_trans_Suc = less_trans_Suc; |
|
3343 | 666 |
val leI = Suc_leI RS (Suc_le_mono RS iffD1); |
2608 | 667 |
val not_lessI = leI RS leD |
668 |
val not_leI = prove_goal thy "!!m::nat. n < m ==> ~ m <= n" |
|
669 |
(fn _ => [etac swap2 1, etac leD 1]); |
|
670 |
val eqI = prove_goal thy "!!m. [| m < Suc n; n < Suc m |] ==> m=n" |
|
671 |
(fn _ => [etac less_SucE 1, |
|
2935 | 672 |
blast_tac (!claset addSDs [Suc_less_SucD] addSEs [less_irrefl] |
2891 | 673 |
addDs [less_trans_Suc]) 1, |
2935 | 674 |
assume_tac 1]); |
2608 | 675 |
val leD = le_eq_less_Suc RS iffD1; |
676 |
val not_lessD = nat_leI RS leD; |
|
677 |
val not_leD = not_leE |
|
678 |
val eqD1 = prove_goal thy "!!n. m = n ==> m < Suc n" |
|
679 |
(fn _ => [etac subst 1, rtac lessI 1]); |
|
680 |
val eqD2 = sym RS eqD1; |
|
681 |
||
682 |
fun is_zero(t) = t = Const("0",Type("nat",[])); |
|
683 |
||
684 |
fun nnb T = T = Type("fun",[Type("nat",[]), |
|
685 |
Type("fun",[Type("nat",[]), |
|
686 |
Type("bool",[])])]) |
|
687 |
||
688 |
fun decomp_Suc(Const("Suc",_)$t) = let val (a,i) = decomp_Suc t in (a,i+1) end |
|
689 |
| decomp_Suc t = (t,0); |
|
690 |
||
691 |
fun decomp2(rel,T,lhs,rhs) = |
|
692 |
if not(nnb T) then None else |
|
693 |
let val (x,i) = decomp_Suc lhs |
|
694 |
val (y,j) = decomp_Suc rhs |
|
695 |
in case rel of |
|
696 |
"op <" => Some(x,i,"<",y,j) |
|
697 |
| "op <=" => Some(x,i,"<=",y,j) |
|
698 |
| "op =" => Some(x,i,"=",y,j) |
|
699 |
| _ => None |
|
700 |
end; |
|
701 |
||
702 |
fun negate(Some(x,i,rel,y,j)) = Some(x,i,"~"^rel,y,j) |
|
703 |
| negate None = None; |
|
704 |
||
705 |
fun decomp(_$(Const(rel,T)$lhs$rhs)) = decomp2(rel,T,lhs,rhs) |
|
2718 | 706 |
| decomp(_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) = |
2608 | 707 |
negate(decomp2(rel,T,lhs,rhs)) |
708 |
| decomp _ = None |
|
709 |
||
710 |
end; |
|
711 |
||
712 |
structure Trans_Tac = Trans_Tac_Fun(Less_Arith); |
|
713 |
||
714 |
open Trans_Tac; |
|
715 |
||
716 |
(*** eliminates ~= in premises, which trans_tac cannot deal with ***) |
|
717 |
qed_goal "nat_neqE" thy |
|
718 |
"[| (m::nat) ~= n; m < n ==> P; n < m ==> P |] ==> P" |
|
719 |
(fn major::prems => [cut_facts_tac [less_linear] 1, |
|
720 |
REPEAT(eresolve_tac ([disjE,major RS notE]@prems) 1)]); |
|
2680
20fa49e610ca
function nat_add_primrec added to allow primrec definitions over nat
pusch
parents:
2608
diff
changeset
|
721 |
|
20fa49e610ca
function nat_add_primrec added to allow primrec definitions over nat
pusch
parents:
2608
diff
changeset
|
722 |
|
20fa49e610ca
function nat_add_primrec added to allow primrec definitions over nat
pusch
parents:
2608
diff
changeset
|
723 |
|
20fa49e610ca
function nat_add_primrec added to allow primrec definitions over nat
pusch
parents:
2608
diff
changeset
|
724 |
(* add function nat_add_primrec *) |
4032
4b1c69d8b767
For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents:
4000
diff
changeset
|
725 |
val (_, nat_add_primrec, _, _) = Datatype.add_datatype |
3308
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
726 |
([], "nat", [("0", [], Mixfix ("0", [], max_pri)), ("Suc", [dtTyp ([], |
3768 | 727 |
"nat")], NoSyn)]) (Theory.add_name "Arith" HOL.thy); |
728 |
(*pretend Arith is part of the basic theory to fool package*) |