3 Copyright 1992 University of Cambridge
6 header {*Well-founded Recursion*}
8 theory Wellfounded_Recursion
9 imports Transitive_Closure
13 wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => ('a * 'b) set"
15 inductive "wfrec_rel R F"
17 wfrecI: "ALL z. (z, x) : R --> (z, g z) : wfrec_rel R F ==>
18 (x, F g x) : wfrec_rel R F"
21 wf :: "('a * 'a)set => bool"
22 "wf(r) == (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))"
24 acyclic :: "('a*'a)set => bool"
25 "acyclic r == !x. (x,x) ~: r^+"
27 cut :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b"
28 "cut f r x == (%y. if (y,x):r then f y else arbitrary)"
30 adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool"
31 "adm_wf R F == ALL f g x.
32 (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
34 wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b"
35 "wfrec R F == %x. THE y. (x, y) : wfrec_rel R (%f x. F (cut f R x) x)"
37 axclass wellorder \<subseteq> linorder
38 wf: "wf {(x,y::'a::ord). x<y}"
42 "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"
43 by (unfold wf_def, blast)
45 text{*Restriction to domain @{term A}.
46 If @{term r} is well-founded over @{term A} then @{term "wf r"}*}
49 !!x P. [| ALL x. (ALL y. (y,x) : r --> P y) --> P x; x:A |] ==> P x |]
51 by (unfold wf_def, blast)
55 !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x)
57 by (unfold wf_def, blast)
59 lemmas wf_induct_rule = wf_induct [rule_format, consumes 1, case_names less, induct set: wf]
61 lemma wf_not_sym [rule_format]: "wf(r) ==> ALL x. (a,x):r --> (x,a)~:r"
62 by (erule_tac a=a in wf_induct, blast)
64 (* [| wf r; ~Z ==> (a,x) : r; (x,a) ~: r ==> Z |] ==> Z *)
65 lemmas wf_asym = wf_not_sym [elim_format]
67 lemma wf_not_refl [simp]: "wf(r) ==> (a,a) ~: r"
68 by (blast elim: wf_asym)
70 (* [| wf r; (a,a) ~: r ==> PROP W |] ==> PROP W *)
71 lemmas wf_irrefl = wf_not_refl [elim_format]
73 text{*transitive closure of a well-founded relation is well-founded! *}
74 lemma wf_trancl: "wf(r) ==> wf(r^+)"
75 apply (subst wf_def, clarify)
76 apply (rule allE, assumption)
77 --{*Retains the universal formula for later use!*}
79 apply (erule_tac a = x in wf_induct)
80 apply (blast elim: tranclE)
83 lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
84 apply (subst trancl_converse [symmetric])
85 apply (erule wf_trancl)
89 subsubsection{*Minimal-element characterization of well-foundedness*}
91 lemma lemma1: "wf r ==> x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)"
94 apply (erule mp [THEN spec], blast)
97 lemma lemma2: "(ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)) ==> wf r"
98 apply (unfold wf_def, clarify)
99 apply (drule_tac x = "{x. ~ P x}" in spec, blast)
102 lemma wf_eq_minimal: "wf r = (ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q))"
103 by (blast intro!: lemma1 lemma2)
105 subsubsection{*Other simple well-foundedness results*}
108 text{*Well-foundedness of subsets*}
109 lemma wf_subset: "[| wf(r); p<=r |] ==> wf(p)"
110 apply (simp (no_asm_use) add: wf_eq_minimal)
114 text{*Well-foundedness of the empty relation*}
115 lemma wf_empty [iff]: "wf({})"
116 by (simp add: wf_def)
118 text{*Well-foundedness of insert*}
119 lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
121 apply (blast elim: wf_trancl [THEN wf_irrefl]
122 intro: rtrancl_into_trancl1 wf_subset
123 rtrancl_mono [THEN [2] rev_subsetD])
124 apply (simp add: wf_eq_minimal, safe)
125 apply (rule allE, assumption, erule impE, blast)
127 apply (rename_tac "a", case_tac "a = x")
130 apply (case_tac "y:Q")
132 apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
134 apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl)
135 --{*essential for speed*}
136 txt{*Blast with new substOccur fails*}
137 apply (fast intro: converse_rtrancl_into_rtrancl)
140 text{*Well-foundedness of image*}
141 lemma wf_prod_fun_image: "[| wf r; inj f |] ==> wf(prod_fun f f ` r)"
142 apply (simp only: wf_eq_minimal, clarify)
143 apply (case_tac "EX p. f p : Q")
144 apply (erule_tac x = "{p. f p : Q}" in allE)
145 apply (fast dest: inj_onD, blast)
149 subsubsection{*Well-Foundedness Results for Unions*}
151 text{*Well-foundedness of indexed union with disjoint domains and ranges*}
153 lemma wf_UN: "[| ALL i:I. wf(r i);
154 ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}
155 |] ==> wf(UN i:I. r i)"
156 apply (simp only: wf_eq_minimal, clarify)
157 apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
161 apply (drule bspec, assumption)
162 apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
163 apply (blast elim!: allE)
168 ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}
170 apply (simp add: Union_def)
171 apply (blast intro: wf_UN)
174 (*Intuition: we find an (R u S)-min element of a nonempty subset A
176 1. There is a step a -R-> b with a,b : A.
177 Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
178 By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
179 subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
180 have an S-successor and is thus S-min in A as well.
181 2. There is no such step.
182 Pick an S-min element of A. In this case it must be an R-min
183 element of A as well.
187 "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
188 apply (simp only: wf_eq_minimal, clarify)
189 apply (rename_tac A a)
190 apply (case_tac "EX a:A. EX b:A. (b,a) : r")
193 apply (drule_tac x=A in spec)+
195 apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r) }" in allE)+
196 apply (blast elim!: allE)
199 subsubsection {*acyclic*}
201 lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
202 by (simp add: acyclic_def)
204 lemma wf_acyclic: "wf r ==> acyclic r"
205 apply (simp add: acyclic_def)
206 apply (blast elim: wf_trancl [THEN wf_irrefl])
209 lemma acyclic_insert [iff]:
210 "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
211 apply (simp add: acyclic_def trancl_insert)
212 apply (blast intro: rtrancl_trans)
215 lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
216 by (simp add: acyclic_def trancl_converse)
218 lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
219 apply (simp add: acyclic_def antisym_def)
220 apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
225 antisym = only self loops
226 Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
227 ==> antisym( r^* ) = acyclic(r - Id)";
230 lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
231 apply (simp add: acyclic_def)
232 apply (blast intro: trancl_mono)
236 subsection{*Well-Founded Recursion*}
240 lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
241 by (simp add: expand_fun_eq cut_def)
243 lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
244 by (simp add: cut_def)
246 text{*Inductive characterization of wfrec combinator; for details see:
247 John Harrison, "Inductive definitions: automation and application"*}
249 lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. (x, y) : wfrec_rel R F"
250 apply (simp add: adm_wf_def)
251 apply (erule_tac a=x in wf_induct)
253 apply (rule_tac g = "%x. THE y. (x, y) : wfrec_rel R F" in wfrec_rel.wfrecI)
254 apply (fast dest!: theI')
255 apply (erule wfrec_rel.cases, simp)
256 apply (erule allE, erule allE, erule allE, erule mp)
257 apply (fast intro: the_equality [symmetric])
260 lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
261 apply (simp add: adm_wf_def)
263 apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
267 lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
268 apply (simp add: wfrec_def)
269 apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
270 apply (rule wfrec_rel.wfrecI)
272 apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
276 text{** This form avoids giant explosions in proofs. NOTE USE OF ==*}
277 lemma def_wfrec: "[| f==wfrec r H; wf(r) |] ==> f(a) = H (cut f r a) a"
279 apply (blast intro: wfrec)
283 subsection {* Code generator setup *}
286 "wfrec" ("\<module>wfrec?")
288 fun wfrec f x = f (wfrec f) x;
293 fun wfrec f x = f (wfrec f) x;
296 wfrec f x = f (wfrec f) x
302 haskell ("{*wfrec*}?") *)
304 subsection{*Variants for TFL: the Recdef Package*}
306 lemma tfl_wf_induct: "ALL R. wf R -->
307 (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))"
309 apply (rule_tac r = R and P = P and a = x in wf_induct, assumption, blast)
312 lemma tfl_cut_apply: "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)"
314 apply (rule cut_apply, assumption)
318 "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)"
323 subsection {*LEAST and wellorderings*}
325 text{* See also @{text wf_linord_ex_has_least} and its consequences in
326 @{text Wellfounded_Relations.ML}*}
328 lemma wellorder_Least_lemma [rule_format]:
329 "P (k::'a::wellorder) --> P (LEAST x. P(x)) & (LEAST x. P(x)) <= k"
330 apply (rule_tac a = k in wf [THEN wf_induct])
332 apply (rule classical)
333 apply (rule_tac s = x in Least_equality [THEN ssubst], auto)
334 apply (auto simp add: linorder_not_less [symmetric])
337 lemmas LeastI = wellorder_Least_lemma [THEN conjunct1, standard]
338 lemmas Least_le = wellorder_Least_lemma [THEN conjunct2, standard]
340 -- "The following 3 lemmas are due to Brian Huffman"
341 lemma LeastI_ex: "EX x::'a::wellorder. P x ==> P (Least P)"
347 "[| P (a::'a::wellorder); !!x. P x ==> Q x |] ==> Q (Least P)"
348 by (blast intro: LeastI)
351 "[| EX a::'a::wellorder. P a; !!x. P x ==> Q x |] ==> Q (Least P)"
352 by (blast intro: LeastI_ex)
354 lemma not_less_Least: "[| k < (LEAST x. P x) |] ==> ~P (k::'a::wellorder)"
355 apply (simp (no_asm_use) add: linorder_not_le [symmetric])
356 apply (erule contrapos_nn)
357 apply (erule Least_le)
362 val wf_def = thm "wf_def";
363 val wfUNIVI = thm "wfUNIVI";
365 val wf_induct = thm "wf_induct";
366 val wf_not_sym = thm "wf_not_sym";
367 val wf_asym = thm "wf_asym";
368 val wf_not_refl = thm "wf_not_refl";
369 val wf_irrefl = thm "wf_irrefl";
370 val wf_trancl = thm "wf_trancl";
371 val wf_converse_trancl = thm "wf_converse_trancl";
372 val wf_eq_minimal = thm "wf_eq_minimal";
373 val wf_subset = thm "wf_subset";
374 val wf_empty = thm "wf_empty";
375 val wf_insert = thm "wf_insert";
376 val wf_UN = thm "wf_UN";
377 val wf_Union = thm "wf_Union";
378 val wf_Un = thm "wf_Un";
379 val wf_prod_fun_image = thm "wf_prod_fun_image";
380 val acyclicI = thm "acyclicI";
381 val wf_acyclic = thm "wf_acyclic";
382 val acyclic_insert = thm "acyclic_insert";
383 val acyclic_converse = thm "acyclic_converse";
384 val acyclic_impl_antisym_rtrancl = thm "acyclic_impl_antisym_rtrancl";
385 val acyclic_subset = thm "acyclic_subset";
386 val cuts_eq = thm "cuts_eq";
387 val cut_apply = thm "cut_apply";
388 val wfrec_unique = thm "wfrec_unique";
389 val wfrec = thm "wfrec";
390 val def_wfrec = thm "def_wfrec";
391 val tfl_wf_induct = thm "tfl_wf_induct";
392 val tfl_cut_apply = thm "tfl_cut_apply";
393 val tfl_wfrec = thm "tfl_wfrec";
394 val LeastI = thm "LeastI";
395 val Least_le = thm "Least_le";
396 val not_less_Least = thm "not_less_Least";