1(*  Title:       HOL/Zorn.thy
2    Author:      Jacques D. Fleuriot
3    Author:      Tobias Nipkow, TUM
4    Author:      Christian Sternagel, JAIST
5
6Zorn's Lemma (ported from Larry Paulson's Zorn.thy in ZF).
7The well-ordering theorem.
8*)
9
10section \<open>Zorn's Lemma\<close>
11
12theory Zorn
13  imports Order_Relation Hilbert_Choice
14begin
15
16subsection \<open>Zorn's Lemma for the Subset Relation\<close>
17
18subsubsection \<open>Results that do not require an order\<close>
19
20text \<open>Let \<open>P\<close> be a binary predicate on the set \<open>A\<close>.\<close>
21locale pred_on =
22  fixes A :: "'a set"
23    and P :: "'a \<Rightarrow> 'a \<Rightarrow> bool"  (infix "\<sqsubset>" 50)
24begin
25
26abbreviation Peq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"  (infix "\<sqsubseteq>" 50)
27  where "x \<sqsubseteq> y \<equiv> P\<^sup>=\<^sup>= x y"
28
29text \<open>A chain is a totally ordered subset of \<open>A\<close>.\<close>
30definition chain :: "'a set \<Rightarrow> bool"
31  where "chain C \<longleftrightarrow> C \<subseteq> A \<and> (\<forall>x\<in>C. \<forall>y\<in>C. x \<sqsubseteq> y \<or> y \<sqsubseteq> x)"
32
33text \<open>
34  We call a chain that is a proper superset of some set \<open>X\<close>,
35  but not necessarily a chain itself, a superchain of \<open>X\<close>.
36\<close>
37abbreviation superchain :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"  (infix "<c" 50)
38  where "X <c C \<equiv> chain C \<and> X \<subset> C"
39
40text \<open>A maximal chain is a chain that does not have a superchain.\<close>
41definition maxchain :: "'a set \<Rightarrow> bool"
42  where "maxchain C \<longleftrightarrow> chain C \<and> (\<nexists>S. C <c S)"
43
44text \<open>
45  We define the successor of a set to be an arbitrary
46  superchain, if such exists, or the set itself, otherwise.
47\<close>
48definition suc :: "'a set \<Rightarrow> 'a set"
49  where "suc C = (if \<not> chain C \<or> maxchain C then C else (SOME D. C <c D))"
50
51lemma chainI [Pure.intro?]: "C \<subseteq> A \<Longrightarrow> (\<And>x y. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> x \<sqsubseteq> y \<or> y \<sqsubseteq> x) \<Longrightarrow> chain C"
52  unfolding chain_def by blast
53
54lemma chain_total: "chain C \<Longrightarrow> x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
55  by (simp add: chain_def)
56
57lemma not_chain_suc [simp]: "\<not> chain X \<Longrightarrow> suc X = X"
58  by (simp add: suc_def)
59
60lemma maxchain_suc [simp]: "maxchain X \<Longrightarrow> suc X = X"
61  by (simp add: suc_def)
62
63lemma suc_subset: "X \<subseteq> suc X"
64  by (auto simp: suc_def maxchain_def intro: someI2)
65
66lemma chain_empty [simp]: "chain {}"
67  by (auto simp: chain_def)
68
69lemma not_maxchain_Some: "chain C \<Longrightarrow> \<not> maxchain C \<Longrightarrow> C <c (SOME D. C <c D)"
70  by (rule someI_ex) (auto simp: maxchain_def)
71
72lemma suc_not_equals: "chain C \<Longrightarrow> \<not> maxchain C \<Longrightarrow> suc C \<noteq> C"
73  using not_maxchain_Some by (auto simp: suc_def)
74
75lemma subset_suc:
76  assumes "X \<subseteq> Y"
77  shows "X \<subseteq> suc Y"
78  using assms by (rule subset_trans) (rule suc_subset)
79
80text \<open>
81  We build a set \<^term>\<open>\<C>\<close> that is closed under applications
82  of \<^term>\<open>suc\<close> and contains the union of all its subsets.
83\<close>
84inductive_set suc_Union_closed ("\<C>")
85  where
86    suc: "X \<in> \<C> \<Longrightarrow> suc X \<in> \<C>"
87  | Union [unfolded Pow_iff]: "X \<in> Pow \<C> \<Longrightarrow> \<Union>X \<in> \<C>"
88
89text \<open>
90  Since the empty set as well as the set itself is a subset of
91  every set, \<^term>\<open>\<C>\<close> contains at least \<^term>\<open>{} \<in> \<C>\<close> and
92  \<^term>\<open>\<Union>\<C> \<in> \<C>\<close>.
93\<close>
94lemma suc_Union_closed_empty: "{} \<in> \<C>"
95  and suc_Union_closed_Union: "\<Union>\<C> \<in> \<C>"
96  using Union [of "{}"] and Union [of "\<C>"] by simp_all
97
98text \<open>Thus closure under \<^term>\<open>suc\<close> will hit a maximal chain
99  eventually, as is shown below.\<close>
100
101lemma suc_Union_closed_induct [consumes 1, case_names suc Union, induct pred: suc_Union_closed]:
102  assumes "X \<in> \<C>"
103    and "\<And>X. X \<in> \<C> \<Longrightarrow> Q X \<Longrightarrow> Q (suc X)"
104    and "\<And>X. X \<subseteq> \<C> \<Longrightarrow> \<forall>x\<in>X. Q x \<Longrightarrow> Q (\<Union>X)"
105  shows "Q X"
106  using assms by induct blast+
107
108lemma suc_Union_closed_cases [consumes 1, case_names suc Union, cases pred: suc_Union_closed]:
109  assumes "X \<in> \<C>"
110    and "\<And>Y. X = suc Y \<Longrightarrow> Y \<in> \<C> \<Longrightarrow> Q"
111    and "\<And>Y. X = \<Union>Y \<Longrightarrow> Y \<subseteq> \<C> \<Longrightarrow> Q"
112  shows "Q"
113  using assms by cases simp_all
114
115text \<open>On chains, \<^term>\<open>suc\<close> yields a chain.\<close>
116lemma chain_suc:
117  assumes "chain X"
118  shows "chain (suc X)"
119  using assms
120  by (cases "\<not> chain X \<or> maxchain X") (force simp: suc_def dest: not_maxchain_Some)+
121
122lemma chain_sucD:
123  assumes "chain X"
124  shows "suc X \<subseteq> A \<and> chain (suc X)"
125proof -
126  from \<open>chain X\<close> have *: "chain (suc X)"
127    by (rule chain_suc)
128  then have "suc X \<subseteq> A"
129    unfolding chain_def by blast
130  with * show ?thesis by blast
131qed
132
133lemma suc_Union_closed_total':
134  assumes "X \<in> \<C>" and "Y \<in> \<C>"
135    and *: "\<And>Z. Z \<in> \<C> \<Longrightarrow> Z \<subseteq> Y \<Longrightarrow> Z = Y \<or> suc Z \<subseteq> Y"
136  shows "X \<subseteq> Y \<or> suc Y \<subseteq> X"
137  using \<open>X \<in> \<C>\<close>
138proof induct
139  case (suc X)
140  with * show ?case by (blast del: subsetI intro: subset_suc)
141next
142  case Union
143  then show ?case by blast
144qed
145
146lemma suc_Union_closed_subsetD:
147  assumes "Y \<subseteq> X" and "X \<in> \<C>" and "Y \<in> \<C>"
148  shows "X = Y \<or> suc Y \<subseteq> X"
149  using assms(2,3,1)
150proof (induct arbitrary: Y)
151  case (suc X)
152  note * = \<open>\<And>Y. Y \<in> \<C> \<Longrightarrow> Y \<subseteq> X \<Longrightarrow> X = Y \<or> suc Y \<subseteq> X\<close>
153  with suc_Union_closed_total' [OF \<open>Y \<in> \<C>\<close> \<open>X \<in> \<C>\<close>]
154  have "Y \<subseteq> X \<or> suc X \<subseteq> Y" by blast
155  then show ?case
156  proof
157    assume "Y \<subseteq> X"
158    with * and \<open>Y \<in> \<C>\<close> have "X = Y \<or> suc Y \<subseteq> X" by blast
159    then show ?thesis
160    proof
161      assume "X = Y"
162      then show ?thesis by simp
163    next
164      assume "suc Y \<subseteq> X"
165      then have "suc Y \<subseteq> suc X" by (rule subset_suc)
166      then show ?thesis by simp
167    qed
168  next
169    assume "suc X \<subseteq> Y"
170    with \<open>Y \<subseteq> suc X\<close> show ?thesis by blast
171  qed
172next
173  case (Union X)
174  show ?case
175  proof (rule ccontr)
176    assume "\<not> ?thesis"
177    with \<open>Y \<subseteq> \<Union>X\<close> obtain x y z
178      where "\<not> suc Y \<subseteq> \<Union>X"
179        and "x \<in> X" and "y \<in> x" and "y \<notin> Y"
180        and "z \<in> suc Y" and "\<forall>x\<in>X. z \<notin> x" by blast
181    with \<open>X \<subseteq> \<C>\<close> have "x \<in> \<C>" by blast
182    from Union and \<open>x \<in> X\<close> have *: "\<And>y. y \<in> \<C> \<Longrightarrow> y \<subseteq> x \<Longrightarrow> x = y \<or> suc y \<subseteq> x"
183      by blast
184    with suc_Union_closed_total' [OF \<open>Y \<in> \<C>\<close> \<open>x \<in> \<C>\<close>] have "Y \<subseteq> x \<or> suc x \<subseteq> Y"
185      by blast
186    then show False
187    proof
188      assume "Y \<subseteq> x"
189      with * [OF \<open>Y \<in> \<C>\<close>] have "x = Y \<or> suc Y \<subseteq> x" by blast
190      then show False
191      proof
192        assume "x = Y"
193        with \<open>y \<in> x\<close> and \<open>y \<notin> Y\<close> show False by blast
194      next
195        assume "suc Y \<subseteq> x"
196        with \<open>x \<in> X\<close> have "suc Y \<subseteq> \<Union>X" by blast
197        with \<open>\<not> suc Y \<subseteq> \<Union>X\<close> show False by contradiction
198      qed
199    next
200      assume "suc x \<subseteq> Y"
201      moreover from suc_subset and \<open>y \<in> x\<close> have "y \<in> suc x" by blast
202      ultimately show False using \<open>y \<notin> Y\<close> by blast
203    qed
204  qed
205qed
206
207text \<open>The elements of \<^term>\<open>\<C>\<close> are totally ordered by the subset relation.\<close>
208lemma suc_Union_closed_total:
209  assumes "X \<in> \<C>" and "Y \<in> \<C>"
210  shows "X \<subseteq> Y \<or> Y \<subseteq> X"
211proof (cases "\<forall>Z\<in>\<C>. Z \<subseteq> Y \<longrightarrow> Z = Y \<or> suc Z \<subseteq> Y")
212  case True
213  with suc_Union_closed_total' [OF assms]
214  have "X \<subseteq> Y \<or> suc Y \<subseteq> X" by blast
215  with suc_subset [of Y] show ?thesis by blast
216next
217  case False
218  then obtain Z where "Z \<in> \<C>" and "Z \<subseteq> Y" and "Z \<noteq> Y" and "\<not> suc Z \<subseteq> Y"
219    by blast
220  with suc_Union_closed_subsetD and \<open>Y \<in> \<C>\<close> show ?thesis
221    by blast
222qed
223
224text \<open>Once we hit a fixed point w.r.t. \<^term>\<open>suc\<close>, all other elements
225  of \<^term>\<open>\<C>\<close> are subsets of this fixed point.\<close>
226lemma suc_Union_closed_suc:
227  assumes "X \<in> \<C>" and "Y \<in> \<C>" and "suc Y = Y"
228  shows "X \<subseteq> Y"
229  using \<open>X \<in> \<C>\<close>
230proof induct
231  case (suc X)
232  with \<open>Y \<in> \<C>\<close> and suc_Union_closed_subsetD have "X = Y \<or> suc X \<subseteq> Y"
233    by blast
234  then show ?case
235    by (auto simp: \<open>suc Y = Y\<close>)
236next
237  case Union
238  then show ?case by blast
239qed
240
241lemma eq_suc_Union:
242  assumes "X \<in> \<C>"
243  shows "suc X = X \<longleftrightarrow> X = \<Union>\<C>"
244    (is "?lhs \<longleftrightarrow> ?rhs")
245proof
246  assume ?lhs
247  then have "\<Union>\<C> \<subseteq> X"
248    by (rule suc_Union_closed_suc [OF suc_Union_closed_Union \<open>X \<in> \<C>\<close>])
249  with \<open>X \<in> \<C>\<close> show ?rhs
250    by blast
251next
252  from \<open>X \<in> \<C>\<close> have "suc X \<in> \<C>" by (rule suc)
253  then have "suc X \<subseteq> \<Union>\<C>" by blast
254  moreover assume ?rhs
255  ultimately have "suc X \<subseteq> X" by simp
256  moreover have "X \<subseteq> suc X" by (rule suc_subset)
257  ultimately show ?lhs ..
258qed
259
260lemma suc_in_carrier:
261  assumes "X \<subseteq> A"
262  shows "suc X \<subseteq> A"
263  using assms
264  by (cases "\<not> chain X \<or> maxchain X") (auto dest: chain_sucD)
265
266lemma suc_Union_closed_in_carrier:
267  assumes "X \<in> \<C>"
268  shows "X \<subseteq> A"
269  using assms
270  by induct (auto dest: suc_in_carrier)
271
272text \<open>All elements of \<^term>\<open>\<C>\<close> are chains.\<close>
273lemma suc_Union_closed_chain:
274  assumes "X \<in> \<C>"
275  shows "chain X"
276  using assms
277proof induct
278  case (suc X)
279  then show ?case
280    using not_maxchain_Some by (simp add: suc_def)
281next
282  case (Union X)
283  then have "\<Union>X \<subseteq> A"
284    by (auto dest: suc_Union_closed_in_carrier)
285  moreover have "\<forall>x\<in>\<Union>X. \<forall>y\<in>\<Union>X. x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
286  proof (intro ballI)
287    fix x y
288    assume "x \<in> \<Union>X" and "y \<in> \<Union>X"
289    then obtain u v where "x \<in> u" and "u \<in> X" and "y \<in> v" and "v \<in> X"
290      by blast
291    with Union have "u \<in> \<C>" and "v \<in> \<C>" and "chain u" and "chain v"
292      by blast+
293    with suc_Union_closed_total have "u \<subseteq> v \<or> v \<subseteq> u"
294      by blast
295    then show "x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
296    proof
297      assume "u \<subseteq> v"
298      from \<open>chain v\<close> show ?thesis
299      proof (rule chain_total)
300        show "y \<in> v" by fact
301        show "x \<in> v" using \<open>u \<subseteq> v\<close> and \<open>x \<in> u\<close> by blast
302      qed
303    next
304      assume "v \<subseteq> u"
305      from \<open>chain u\<close> show ?thesis
306      proof (rule chain_total)
307        show "x \<in> u" by fact
308        show "y \<in> u" using \<open>v \<subseteq> u\<close> and \<open>y \<in> v\<close> by blast
309      qed
310    qed
311  qed
312  ultimately show ?case unfolding chain_def ..
313qed
314
315subsubsection \<open>Hausdorff's Maximum Principle\<close>
316
317text \<open>There exists a maximal totally ordered subset of \<open>A\<close>. (Note that we do not
318  require \<open>A\<close> to be partially ordered.)\<close>
319
320theorem Hausdorff: "\<exists>C. maxchain C"
321proof -
322  let ?M = "\<Union>\<C>"
323  have "maxchain ?M"
324  proof (rule ccontr)
325    assume "\<not> ?thesis"
326    then have "suc ?M \<noteq> ?M"
327      using suc_not_equals and suc_Union_closed_chain [OF suc_Union_closed_Union] by simp
328    moreover have "suc ?M = ?M"
329      using eq_suc_Union [OF suc_Union_closed_Union] by simp
330    ultimately show False by contradiction
331  qed
332  then show ?thesis by blast
333qed
334
335text \<open>Make notation \<^term>\<open>\<C>\<close> available again.\<close>
336no_notation suc_Union_closed  ("\<C>")
337
338lemma chain_extend: "chain C \<Longrightarrow> z \<in> A \<Longrightarrow> \<forall>x\<in>C. x \<sqsubseteq> z \<Longrightarrow> chain ({z} \<union> C)"
339  unfolding chain_def by blast
340
341lemma maxchain_imp_chain: "maxchain C \<Longrightarrow> chain C"
342  by (simp add: maxchain_def)
343
344end
345
346text \<open>Hide constant \<^const>\<open>pred_on.suc_Union_closed\<close>, which was just needed
347  for the proof of Hausforff's maximum principle.\<close>
348hide_const pred_on.suc_Union_closed
349
350lemma chain_mono:
351  assumes "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> P x y \<Longrightarrow> Q x y"
352    and "pred_on.chain A P C"
353  shows "pred_on.chain A Q C"
354  using assms unfolding pred_on.chain_def by blast
355
356
357subsubsection \<open>Results for the proper subset relation\<close>
358
359interpretation subset: pred_on "A" "(\<subset>)" for A .
360
361lemma subset_maxchain_max:
362  assumes "subset.maxchain A C"
363    and "X \<in> A"
364    and "\<Union>C \<subseteq> X"
365  shows "\<Union>C = X"
366proof (rule ccontr)
367  let ?C = "{X} \<union> C"
368  from \<open>subset.maxchain A C\<close> have "subset.chain A C"
369    and *: "\<And>S. subset.chain A S \<Longrightarrow> \<not> C \<subset> S"
370    by (auto simp: subset.maxchain_def)
371  moreover have "\<forall>x\<in>C. x \<subseteq> X" using \<open>\<Union>C \<subseteq> X\<close> by auto
372  ultimately have "subset.chain A ?C"
373    using subset.chain_extend [of A C X] and \<open>X \<in> A\<close> by auto
374  moreover assume **: "\<Union>C \<noteq> X"
375  moreover from ** have "C \<subset> ?C" using \<open>\<Union>C \<subseteq> X\<close> by auto
376  ultimately show False using * by blast
377qed
378
379lemma subset_chain_def: "\<And>\<A>. subset.chain \<A> \<C> = (\<C> \<subseteq> \<A> \<and> (\<forall>X\<in>\<C>. \<forall>Y\<in>\<C>. X \<subseteq> Y \<or> Y \<subseteq> X))"
380  by (auto simp: subset.chain_def)
381
382lemma subset_chain_insert:
383  "subset.chain \<A> (insert B \<B>) \<longleftrightarrow> B \<in> \<A> \<and> (\<forall>X\<in>\<B>. X \<subseteq> B \<or> B \<subseteq> X) \<and> subset.chain \<A> \<B>"
384  by (fastforce simp add: subset_chain_def)
385
386subsubsection \<open>Zorn's lemma\<close>
387
388text \<open>If every chain has an upper bound, then there is a maximal set.\<close>
389theorem subset_Zorn:
390  assumes "\<And>C. subset.chain A C \<Longrightarrow> \<exists>U\<in>A. \<forall>X\<in>C. X \<subseteq> U"
391  shows "\<exists>M\<in>A. \<forall>X\<in>A. M \<subseteq> X \<longrightarrow> X = M"
392proof -
393  from subset.Hausdorff [of A] obtain M where "subset.maxchain A M" ..
394  then have "subset.chain A M"
395    by (rule subset.maxchain_imp_chain)
396  with assms obtain Y where "Y \<in> A" and "\<forall>X\<in>M. X \<subseteq> Y"
397    by blast
398  moreover have "\<forall>X\<in>A. Y \<subseteq> X \<longrightarrow> Y = X"
399  proof (intro ballI impI)
400    fix X
401    assume "X \<in> A" and "Y \<subseteq> X"
402    show "Y = X"
403    proof (rule ccontr)
404      assume "\<not> ?thesis"
405      with \<open>Y \<subseteq> X\<close> have "\<not> X \<subseteq> Y" by blast
406      from subset.chain_extend [OF \<open>subset.chain A M\<close> \<open>X \<in> A\<close>] and \<open>\<forall>X\<in>M. X \<subseteq> Y\<close>
407      have "subset.chain A ({X} \<union> M)"
408        using \<open>Y \<subseteq> X\<close> by auto
409      moreover have "M \<subset> {X} \<union> M"
410        using \<open>\<forall>X\<in>M. X \<subseteq> Y\<close> and \<open>\<not> X \<subseteq> Y\<close> by auto
411      ultimately show False
412        using \<open>subset.maxchain A M\<close> by (auto simp: subset.maxchain_def)
413    qed
414  qed
415  ultimately show ?thesis by blast
416qed
417
418text \<open>Alternative version of Zorn's lemma for the subset relation.\<close>
419lemma subset_Zorn':
420  assumes "\<And>C. subset.chain A C \<Longrightarrow> \<Union>C \<in> A"
421  shows "\<exists>M\<in>A. \<forall>X\<in>A. M \<subseteq> X \<longrightarrow> X = M"
422proof -
423  from subset.Hausdorff [of A] obtain M where "subset.maxchain A M" ..
424  then have "subset.chain A M"
425    by (rule subset.maxchain_imp_chain)
426  with assms have "\<Union>M \<in> A" .
427  moreover have "\<forall>Z\<in>A. \<Union>M \<subseteq> Z \<longrightarrow> \<Union>M = Z"
428  proof (intro ballI impI)
429    fix Z
430    assume "Z \<in> A" and "\<Union>M \<subseteq> Z"
431    with subset_maxchain_max [OF \<open>subset.maxchain A M\<close>]
432      show "\<Union>M = Z" .
433  qed
434  ultimately show ?thesis by blast
435qed
436
437
438subsection \<open>Zorn's Lemma for Partial Orders\<close>
439
440text \<open>Relate old to new definitions.\<close>
441
442definition chain_subset :: "'a set set \<Rightarrow> bool"  ("chain\<^sub>\<subseteq>")  (* Define globally? In Set.thy? *)
443  where "chain\<^sub>\<subseteq> C \<longleftrightarrow> (\<forall>A\<in>C. \<forall>B\<in>C. A \<subseteq> B \<or> B \<subseteq> A)"
444
445definition chains :: "'a set set \<Rightarrow> 'a set set set"
446  where "chains A = {C. C \<subseteq> A \<and> chain\<^sub>\<subseteq> C}"
447
448definition Chains :: "('a \<times> 'a) set \<Rightarrow> 'a set set"  (* Define globally? In Relation.thy? *)
449  where "Chains r = {C. \<forall>a\<in>C. \<forall>b\<in>C. (a, b) \<in> r \<or> (b, a) \<in> r}"
450
451lemma chains_extend: "c \<in> chains S \<Longrightarrow> z \<in> S \<Longrightarrow> \<forall>x \<in> c. x \<subseteq> z \<Longrightarrow> {z} \<union> c \<in> chains S"
452  for z :: "'a set"
453  unfolding chains_def chain_subset_def by blast
454
455lemma mono_Chains: "r \<subseteq> s \<Longrightarrow> Chains r \<subseteq> Chains s"
456  unfolding Chains_def by blast
457
458lemma chain_subset_alt_def: "chain\<^sub>\<subseteq> C = subset.chain UNIV C"
459  unfolding chain_subset_def subset.chain_def by fast
460
461lemma chains_alt_def: "chains A = {C. subset.chain A C}"
462  by (simp add: chains_def chain_subset_alt_def subset.chain_def)
463
464lemma Chains_subset: "Chains r \<subseteq> {C. pred_on.chain UNIV (\<lambda>x y. (x, y) \<in> r) C}"
465  by (force simp add: Chains_def pred_on.chain_def)
466
467lemma Chains_subset':
468  assumes "refl r"
469  shows "{C. pred_on.chain UNIV (\<lambda>x y. (x, y) \<in> r) C} \<subseteq> Chains r"
470  using assms
471  by (auto simp add: Chains_def pred_on.chain_def refl_on_def)
472
473lemma Chains_alt_def:
474  assumes "refl r"
475  shows "Chains r = {C. pred_on.chain UNIV (\<lambda>x y. (x, y) \<in> r) C}"
476  using assms Chains_subset Chains_subset' by blast
477
478lemma Chains_relation_of:
479  assumes "C \<in> Chains (relation_of P A)" shows "C \<subseteq> A"
480  using assms unfolding Chains_def relation_of_def by auto
481
482lemma pairwise_chain_Union:
483  assumes P: "\<And>S. S \<in> \<C> \<Longrightarrow> pairwise R S" and "chain\<^sub>\<subseteq> \<C>"
484  shows "pairwise R (\<Union>\<C>)"
485  using \<open>chain\<^sub>\<subseteq> \<C>\<close> unfolding pairwise_def chain_subset_def
486  by (blast intro: P [unfolded pairwise_def, rule_format])
487
488lemma Zorn_Lemma: "\<forall>C\<in>chains A. \<Union>C \<in> A \<Longrightarrow> \<exists>M\<in>A. \<forall>X\<in>A. M \<subseteq> X \<longrightarrow> X = M"
489  using subset_Zorn' [of A] by (force simp: chains_alt_def)
490
491lemma Zorn_Lemma2: "\<forall>C\<in>chains A. \<exists>U\<in>A. \<forall>X\<in>C. X \<subseteq> U \<Longrightarrow> \<exists>M\<in>A. \<forall>X\<in>A. M \<subseteq> X \<longrightarrow> X = M"
492  using subset_Zorn [of A] by (auto simp: chains_alt_def)
493
494subsection \<open>Other variants of Zorn's Lemma\<close>
495
496lemma chainsD: "c \<in> chains S \<Longrightarrow> x \<in> c \<Longrightarrow> y \<in> c \<Longrightarrow> x \<subseteq> y \<or> y \<subseteq> x"
497  unfolding chains_def chain_subset_def by blast
498
499lemma chainsD2: "c \<in> chains S \<Longrightarrow> c \<subseteq> S"
500  unfolding chains_def by blast
501
502lemma Zorns_po_lemma:
503  assumes po: "Partial_order r"
504    and u: "\<And>C. C \<in> Chains r \<Longrightarrow> \<exists>u\<in>Field r. \<forall>a\<in>C. (a, u) \<in> r"
505  shows "\<exists>m\<in>Field r. \<forall>a\<in>Field r. (m, a) \<in> r \<longrightarrow> a = m"
506proof -
507  have "Preorder r"
508    using po by (simp add: partial_order_on_def)
509  txt \<open>Mirror \<open>r\<close> in the set of subsets below (wrt \<open>r\<close>) elements of \<open>A\<close>.\<close>
510  let ?B = "\<lambda>x. r\<inverse> `` {x}"
511  let ?S = "?B ` Field r"
512  have "\<exists>u\<in>Field r. \<forall>A\<in>C. A \<subseteq> r\<inverse> `` {u}"  (is "\<exists>u\<in>Field r. ?P u")
513    if 1: "C \<subseteq> ?S" and 2: "\<forall>A\<in>C. \<forall>B\<in>C. A \<subseteq> B \<or> B \<subseteq> A" for C
514  proof -
515    let ?A = "{x\<in>Field r. \<exists>M\<in>C. M = ?B x}"
516    from 1 have "C = ?B ` ?A" by (auto simp: image_def)
517    have "?A \<in> Chains r"
518    proof (simp add: Chains_def, intro allI impI, elim conjE)
519      fix a b
520      assume "a \<in> Field r" and "?B a \<in> C" and "b \<in> Field r" and "?B b \<in> C"
521      with 2 have "?B a \<subseteq> ?B b \<or> ?B b \<subseteq> ?B a" by auto
522      then show "(a, b) \<in> r \<or> (b, a) \<in> r"
523        using \<open>Preorder r\<close> and \<open>a \<in> Field r\<close> and \<open>b \<in> Field r\<close>
524        by (simp add:subset_Image1_Image1_iff)
525    qed
526    then obtain u where uA: "u \<in> Field r" "\<forall>a\<in>?A. (a, u) \<in> r"
527      by (auto simp: dest: u)
528    have "?P u"
529    proof auto
530      fix a B assume aB: "B \<in> C" "a \<in> B"
531      with 1 obtain x where "x \<in> Field r" and "B = r\<inverse> `` {x}" by auto
532      then show "(a, u) \<in> r"
533        using uA and aB and \<open>Preorder r\<close>
534        unfolding preorder_on_def refl_on_def by simp (fast dest: transD)
535    qed
536    then show ?thesis
537      using \<open>u \<in> Field r\<close> by blast
538  qed
539  then have "\<forall>C\<in>chains ?S. \<exists>U\<in>?S. \<forall>A\<in>C. A \<subseteq> U"
540    by (auto simp: chains_def chain_subset_def)
541  from Zorn_Lemma2 [OF this] obtain m B
542    where "m \<in> Field r"
543      and "B = r\<inverse> `` {m}"
544      and "\<forall>x\<in>Field r. B \<subseteq> r\<inverse> `` {x} \<longrightarrow> r\<inverse> `` {x} = B"
545    by auto
546  then have "\<forall>a\<in>Field r. (m, a) \<in> r \<longrightarrow> a = m"
547    using po and \<open>Preorder r\<close> and \<open>m \<in> Field r\<close>
548    by (auto simp: subset_Image1_Image1_iff Partial_order_eq_Image1_Image1_iff)
549  then show ?thesis
550    using \<open>m \<in> Field r\<close> by blast
551qed
552
553lemma predicate_Zorn:
554  assumes po: "partial_order_on A (relation_of P A)"
555    and ch: "\<And>C. C \<in> Chains (relation_of P A) \<Longrightarrow> \<exists>u \<in> A. \<forall>a \<in> C. P a u"
556  shows "\<exists>m \<in> A. \<forall>a \<in> A. P m a \<longrightarrow> a = m"
557proof -
558  have "a \<in> A" if "C \<in> Chains (relation_of P A)" and "a \<in> C" for C a
559    using that unfolding Chains_def relation_of_def by auto
560  moreover have "(a, u) \<in> relation_of P A" if "a \<in> A" and "u \<in> A" and "P a u" for a u
561    unfolding relation_of_def using that by auto
562  ultimately have "\<exists>m\<in>A. \<forall>a\<in>A. (m, a) \<in> relation_of P A \<longrightarrow> a = m"
563    using Zorns_po_lemma[OF Partial_order_relation_ofI[OF po], rule_format] ch
564    unfolding Field_relation_of[OF partial_order_onD(1)[OF po]] by blast
565  then show ?thesis
566    by (auto simp: relation_of_def)
567qed
568
569lemma Union_in_chain: "\<lbrakk>finite \<B>; \<B> \<noteq> {}; subset.chain \<A> \<B>\<rbrakk> \<Longrightarrow> \<Union>\<B> \<in> \<B>"
570proof (induction \<B> rule: finite_induct)
571  case (insert B \<B>)
572  show ?case
573  proof (cases "\<B> = {}")
574    case False
575    then show ?thesis
576      using insert sup.absorb2 by (auto simp: subset_chain_insert dest!: bspec [where x="\<Union>\<B>"])
577  qed auto
578qed simp
579
580lemma Inter_in_chain: "\<lbrakk>finite \<B>; \<B> \<noteq> {}; subset.chain \<A> \<B>\<rbrakk> \<Longrightarrow> \<Inter>\<B> \<in> \<B>"
581proof (induction \<B> rule: finite_induct)
582  case (insert B \<B>)
583  show ?case
584  proof (cases "\<B> = {}")
585    case False
586    then show ?thesis
587      using insert inf.absorb2 by (auto simp: subset_chain_insert dest!: bspec [where x="\<Inter>\<B>"])
588  qed auto
589qed simp
590
591lemma finite_subset_Union_chain:
592  assumes "finite A" "A \<subseteq> \<Union>\<B>" "\<B> \<noteq> {}" and sub: "subset.chain \<A> \<B>"
593  obtains B where "B \<in> \<B>" "A \<subseteq> B"
594proof -
595  obtain \<F> where \<F>: "finite \<F>" "\<F> \<subseteq> \<B>" "A \<subseteq> \<Union>\<F>"
596    using assms by (auto intro: finite_subset_Union)
597  show thesis
598  proof (cases "\<F> = {}")
599    case True
600    then show ?thesis
601      using \<open>A \<subseteq> \<Union>\<F>\<close> \<open>\<B> \<noteq> {}\<close> that by fastforce
602  next
603    case False
604    show ?thesis
605    proof
606      show "\<Union>\<F> \<in> \<B>"
607        using sub \<open>\<F> \<subseteq> \<B>\<close> \<open>finite \<F>\<close>
608        by (simp add: Union_in_chain False subset.chain_def subset_iff)
609      show "A \<subseteq> \<Union>\<F>"
610        using \<open>A \<subseteq> \<Union>\<F>\<close> by blast
611    qed
612  qed
613qed
614
615lemma subset_Zorn_nonempty:
616  assumes "\<A> \<noteq> {}" and ch: "\<And>\<C>. \<lbrakk>\<C>\<noteq>{}; subset.chain \<A> \<C>\<rbrakk> \<Longrightarrow> \<Union>\<C> \<in> \<A>"
617  shows "\<exists>M\<in>\<A>. \<forall>X\<in>\<A>. M \<subseteq> X \<longrightarrow> X = M"
618proof (rule subset_Zorn)
619  show "\<exists>U\<in>\<A>. \<forall>X\<in>\<C>. X \<subseteq> U" if "subset.chain \<A> \<C>" for \<C>
620  proof (cases "\<C> = {}")
621    case True
622    then show ?thesis
623      using \<open>\<A> \<noteq> {}\<close> by blast
624  next
625    case False
626    show ?thesis
627      by (blast intro!: ch False that Union_upper)
628  qed
629qed
630
631subsection \<open>The Well Ordering Theorem\<close>
632
633(* The initial segment of a relation appears generally useful.
634   Move to Relation.thy?
635   Definition correct/most general?
636   Naming?
637*)
638definition init_seg_of :: "(('a \<times> 'a) set \<times> ('a \<times> 'a) set) set"
639  where "init_seg_of = {(r, s). r \<subseteq> s \<and> (\<forall>a b c. (a, b) \<in> s \<and> (b, c) \<in> r \<longrightarrow> (a, b) \<in> r)}"
640
641abbreviation initial_segment_of_syntax :: "('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> bool"
642    (infix "initial'_segment'_of" 55)
643  where "r initial_segment_of s \<equiv> (r, s) \<in> init_seg_of"
644
645lemma refl_on_init_seg_of [simp]: "r initial_segment_of r"
646  by (simp add: init_seg_of_def)
647
648lemma trans_init_seg_of:
649  "r initial_segment_of s \<Longrightarrow> s initial_segment_of t \<Longrightarrow> r initial_segment_of t"
650  by (simp (no_asm_use) add: init_seg_of_def) blast
651
652lemma antisym_init_seg_of: "r initial_segment_of s \<Longrightarrow> s initial_segment_of r \<Longrightarrow> r = s"
653  unfolding init_seg_of_def by safe
654
655lemma Chains_init_seg_of_Union: "R \<in> Chains init_seg_of \<Longrightarrow> r\<in>R \<Longrightarrow> r initial_segment_of \<Union>R"
656  by (auto simp: init_seg_of_def Ball_def Chains_def) blast
657
658lemma chain_subset_trans_Union:
659  assumes "chain\<^sub>\<subseteq> R" "\<forall>r\<in>R. trans r"
660  shows "trans (\<Union>R)"
661proof (intro transI, elim UnionE)
662  fix S1 S2 :: "'a rel" and x y z :: 'a
663  assume "S1 \<in> R" "S2 \<in> R"
664  with assms(1) have "S1 \<subseteq> S2 \<or> S2 \<subseteq> S1"
665    unfolding chain_subset_def by blast
666  moreover assume "(x, y) \<in> S1" "(y, z) \<in> S2"
667  ultimately have "((x, y) \<in> S1 \<and> (y, z) \<in> S1) \<or> ((x, y) \<in> S2 \<and> (y, z) \<in> S2)"
668    by blast
669  with \<open>S1 \<in> R\<close> \<open>S2 \<in> R\<close> assms(2) show "(x, z) \<in> \<Union>R"
670    by (auto elim: transE)
671qed
672
673lemma chain_subset_antisym_Union:
674  assumes "chain\<^sub>\<subseteq> R" "\<forall>r\<in>R. antisym r"
675  shows "antisym (\<Union>R)"
676proof (intro antisymI, elim UnionE)
677  fix S1 S2 :: "'a rel" and x y :: 'a
678  assume "S1 \<in> R" "S2 \<in> R"
679  with assms(1) have "S1 \<subseteq> S2 \<or> S2 \<subseteq> S1"
680    unfolding chain_subset_def by blast
681  moreover assume "(x, y) \<in> S1" "(y, x) \<in> S2"
682  ultimately have "((x, y) \<in> S1 \<and> (y, x) \<in> S1) \<or> ((x, y) \<in> S2 \<and> (y, x) \<in> S2)"
683    by blast
684  with \<open>S1 \<in> R\<close> \<open>S2 \<in> R\<close> assms(2) show "x = y"
685    unfolding antisym_def by auto
686qed
687
688lemma chain_subset_Total_Union:
689  assumes "chain\<^sub>\<subseteq> R" and "\<forall>r\<in>R. Total r"
690  shows "Total (\<Union>R)"
691proof (simp add: total_on_def Ball_def, auto del: disjCI)
692  fix r s a b
693  assume A: "r \<in> R" "s \<in> R" "a \<in> Field r" "b \<in> Field s" "a \<noteq> b"
694  from \<open>chain\<^sub>\<subseteq> R\<close> and \<open>r \<in> R\<close> and \<open>s \<in> R\<close> have "r \<subseteq> s \<or> s \<subseteq> r"
695    by (auto simp add: chain_subset_def)
696  then show "(\<exists>r\<in>R. (a, b) \<in> r) \<or> (\<exists>r\<in>R. (b, a) \<in> r)"
697  proof
698    assume "r \<subseteq> s"
699    then have "(a, b) \<in> s \<or> (b, a) \<in> s"
700      using assms(2) A mono_Field[of r s]
701      by (auto simp add: total_on_def)
702    then show ?thesis
703      using \<open>s \<in> R\<close> by blast
704  next
705    assume "s \<subseteq> r"
706    then have "(a, b) \<in> r \<or> (b, a) \<in> r"
707      using assms(2) A mono_Field[of s r]
708      by (fastforce simp add: total_on_def)
709    then show ?thesis
710      using \<open>r \<in> R\<close> by blast
711  qed
712qed
713
714lemma wf_Union_wf_init_segs:
715  assumes "R \<in> Chains init_seg_of"
716    and "\<forall>r\<in>R. wf r"
717  shows "wf (\<Union>R)"
718proof (simp add: wf_iff_no_infinite_down_chain, rule ccontr, auto)
719  fix f
720  assume 1: "\<forall>i. \<exists>r\<in>R. (f (Suc i), f i) \<in> r"
721  then obtain r where "r \<in> R" and "(f (Suc 0), f 0) \<in> r" by auto
722  have "(f (Suc i), f i) \<in> r" for i
723  proof (induct i)
724    case 0
725    show ?case by fact
726  next
727    case (Suc i)
728    then obtain s where s: "s \<in> R" "(f (Suc (Suc i)), f(Suc i)) \<in> s"
729      using 1 by auto
730    then have "s initial_segment_of r \<or> r initial_segment_of s"
731      using assms(1) \<open>r \<in> R\<close> by (simp add: Chains_def)
732    with Suc s show ?case by (simp add: init_seg_of_def) blast
733  qed
734  then show False
735    using assms(2) and \<open>r \<in> R\<close>
736    by (simp add: wf_iff_no_infinite_down_chain) blast
737qed
738
739lemma initial_segment_of_Diff: "p initial_segment_of q \<Longrightarrow> p - s initial_segment_of q - s"
740  unfolding init_seg_of_def by blast
741
742lemma Chains_inits_DiffI: "R \<in> Chains init_seg_of \<Longrightarrow> {r - s |r. r \<in> R} \<in> Chains init_seg_of"
743  unfolding Chains_def by (blast intro: initial_segment_of_Diff)
744
745theorem well_ordering: "\<exists>r::'a rel. Well_order r \<and> Field r = UNIV"
746proof -
747\<comment> \<open>The initial segment relation on well-orders:\<close>
748  let ?WO = "{r::'a rel. Well_order r}"
749  define I where "I = init_seg_of \<inter> ?WO \<times> ?WO"
750  then have I_init: "I \<subseteq> init_seg_of" by simp
751  then have subch: "\<And>R. R \<in> Chains I \<Longrightarrow> chain\<^sub>\<subseteq> R"
752    unfolding init_seg_of_def chain_subset_def Chains_def by blast
753  have Chains_wo: "\<And>R r. R \<in> Chains I \<Longrightarrow> r \<in> R \<Longrightarrow> Well_order r"
754    by (simp add: Chains_def I_def) blast
755  have FI: "Field I = ?WO"
756    by (auto simp add: I_def init_seg_of_def Field_def)
757  then have 0: "Partial_order I"
758    by (auto simp: partial_order_on_def preorder_on_def antisym_def antisym_init_seg_of refl_on_def
759        trans_def I_def elim!: trans_init_seg_of)
760\<comment> \<open>\<open>I\<close>-chains have upper bounds in \<open>?WO\<close> wrt \<open>I\<close>: their Union\<close>
761  have "\<Union>R \<in> ?WO \<and> (\<forall>r\<in>R. (r, \<Union>R) \<in> I)" if "R \<in> Chains I" for R
762  proof -
763    from that have Ris: "R \<in> Chains init_seg_of"
764      using mono_Chains [OF I_init] by blast
765    have subch: "chain\<^sub>\<subseteq> R"
766      using \<open>R \<in> Chains I\<close> I_init by (auto simp: init_seg_of_def chain_subset_def Chains_def)
767    have "\<forall>r\<in>R. Refl r" and "\<forall>r\<in>R. trans r" and "\<forall>r\<in>R. antisym r"
768      and "\<forall>r\<in>R. Total r" and "\<forall>r\<in>R. wf (r - Id)"
769      using Chains_wo [OF \<open>R \<in> Chains I\<close>] by (simp_all add: order_on_defs)
770    have "Refl (\<Union>R)"
771      using \<open>\<forall>r\<in>R. Refl r\<close> unfolding refl_on_def by fastforce
772    moreover have "trans (\<Union>R)"
773      by (rule chain_subset_trans_Union [OF subch \<open>\<forall>r\<in>R. trans r\<close>])
774    moreover have "antisym (\<Union>R)"
775      by (rule chain_subset_antisym_Union [OF subch \<open>\<forall>r\<in>R. antisym r\<close>])
776    moreover have "Total (\<Union>R)"
777      by (rule chain_subset_Total_Union [OF subch \<open>\<forall>r\<in>R. Total r\<close>])
778    moreover have "wf ((\<Union>R) - Id)"
779    proof -
780      have "(\<Union>R) - Id = \<Union>{r - Id | r. r \<in> R}" by blast
781      with \<open>\<forall>r\<in>R. wf (r - Id)\<close> and wf_Union_wf_init_segs [OF Chains_inits_DiffI [OF Ris]]
782      show ?thesis by fastforce
783    qed
784    ultimately have "Well_order (\<Union>R)"
785      by (simp add:order_on_defs)
786    moreover have "\<forall>r \<in> R. r initial_segment_of \<Union>R"
787      using Ris by (simp add: Chains_init_seg_of_Union)
788    ultimately show ?thesis
789      using mono_Chains [OF I_init] Chains_wo[of R] and \<open>R \<in> Chains I\<close>
790      unfolding I_def by blast
791  qed
792  then have 1: "\<exists>u\<in>Field I. \<forall>r\<in>R. (r, u) \<in> I" if "R \<in> Chains I" for R
793    using that by (subst FI) blast
794\<comment> \<open>Zorn's Lemma yields a maximal well-order \<open>m\<close>:\<close>
795  then obtain m :: "'a rel"
796    where "Well_order m"
797      and max: "\<forall>r. Well_order r \<and> (m, r) \<in> I \<longrightarrow> r = m"
798    using Zorns_po_lemma[OF 0 1] unfolding FI by fastforce
799\<comment> \<open>Now show by contradiction that \<open>m\<close> covers the whole type:\<close>
800  have False if "x \<notin> Field m" for x :: 'a
801  proof -
802\<comment> \<open>Assuming that \<open>x\<close> is not covered and extend \<open>m\<close> at the top with \<open>x\<close>\<close>
803    have "m \<noteq> {}"
804    proof
805      assume "m = {}"
806      moreover have "Well_order {(x, x)}"
807        by (simp add: order_on_defs refl_on_def trans_def antisym_def total_on_def Field_def)
808      ultimately show False using max
809        by (auto simp: I_def init_seg_of_def simp del: Field_insert)
810    qed
811    then have "Field m \<noteq> {}" by (auto simp: Field_def)
812    moreover have "wf (m - Id)"
813      using \<open>Well_order m\<close> by (simp add: well_order_on_def)
814\<comment> \<open>The extension of \<open>m\<close> by \<open>x\<close>:\<close>
815    let ?s = "{(a, x) | a. a \<in> Field m}"
816    let ?m = "insert (x, x) m \<union> ?s"
817    have Fm: "Field ?m = insert x (Field m)"
818      by (auto simp: Field_def)
819    have "Refl m" and "trans m" and "antisym m" and "Total m" and "wf (m - Id)"
820      using \<open>Well_order m\<close> by (simp_all add: order_on_defs)
821\<comment> \<open>We show that the extension is a well-order\<close>
822    have "Refl ?m"
823      using \<open>Refl m\<close> Fm unfolding refl_on_def by blast
824    moreover have "trans ?m" using \<open>trans m\<close> and \<open>x \<notin> Field m\<close>
825      unfolding trans_def Field_def by blast
826    moreover have "antisym ?m"
827      using \<open>antisym m\<close> and \<open>x \<notin> Field m\<close> unfolding antisym_def Field_def by blast
828    moreover have "Total ?m"
829      using \<open>Total m\<close> and Fm by (auto simp: total_on_def)
830    moreover have "wf (?m - Id)"
831    proof -
832      have "wf ?s"
833        using \<open>x \<notin> Field m\<close> by (auto simp: wf_eq_minimal Field_def Bex_def)
834      then show ?thesis
835        using \<open>wf (m - Id)\<close> and \<open>x \<notin> Field m\<close> wf_subset [OF \<open>wf ?s\<close> Diff_subset]
836        by (auto simp: Un_Diff Field_def intro: wf_Un)
837    qed
838    ultimately have "Well_order ?m"
839      by (simp add: order_on_defs)
840\<comment> \<open>We show that the extension is above \<open>m\<close>\<close>
841    moreover have "(m, ?m) \<in> I"
842      using \<open>Well_order ?m\<close> and \<open>Well_order m\<close> and \<open>x \<notin> Field m\<close>
843      by (fastforce simp: I_def init_seg_of_def Field_def)
844    ultimately
845\<comment> \<open>This contradicts maximality of \<open>m\<close>:\<close>
846    show False
847      using max and \<open>x \<notin> Field m\<close> unfolding Field_def by blast
848  qed
849  then have "Field m = UNIV" by auto
850  with \<open>Well_order m\<close> show ?thesis by blast
851qed
852
853corollary well_order_on: "\<exists>r::'a rel. well_order_on A r"
854proof -
855  obtain r :: "'a rel" where wo: "Well_order r" and univ: "Field r = UNIV"
856    using well_ordering [where 'a = "'a"] by blast
857  let ?r = "{(x, y). x \<in> A \<and> y \<in> A \<and> (x, y) \<in> r}"
858  have 1: "Field ?r = A"
859    using wo univ by (fastforce simp: Field_def order_on_defs refl_on_def)
860  from \<open>Well_order r\<close> have "Refl r" "trans r" "antisym r" "Total r" "wf (r - Id)"
861    by (simp_all add: order_on_defs)
862  from \<open>Refl r\<close> have "Refl ?r"
863    by (auto simp: refl_on_def 1 univ)
864  moreover from \<open>trans r\<close> have "trans ?r"
865    unfolding trans_def by blast
866  moreover from \<open>antisym r\<close> have "antisym ?r"
867    unfolding antisym_def by blast
868  moreover from \<open>Total r\<close> have "Total ?r"
869    by (simp add:total_on_def 1 univ)
870  moreover have "wf (?r - Id)"
871    by (rule wf_subset [OF \<open>wf (r - Id)\<close>]) blast
872  ultimately have "Well_order ?r"
873    by (simp add: order_on_defs)
874  with 1 show ?thesis by auto
875qed
876
877(* Move this to Hilbert Choice and wfrec to Wellfounded*)
878
879lemma wfrec_def_adm: "f \<equiv> wfrec R F \<Longrightarrow> wf R \<Longrightarrow> adm_wf R F \<Longrightarrow> f = F f"
880  using wfrec_fixpoint by simp
881
882lemma dependent_wf_choice:
883  fixes P :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
884  assumes "wf R"
885    and adm: "\<And>f g x r. (\<And>z. (z, x) \<in> R \<Longrightarrow> f z = g z) \<Longrightarrow> P f x r = P g x r"
886    and P: "\<And>x f. (\<And>y. (y, x) \<in> R \<Longrightarrow> P f y (f y)) \<Longrightarrow> \<exists>r. P f x r"
887  shows "\<exists>f. \<forall>x. P f x (f x)"
888proof (intro exI allI)
889  fix x
890  define f where "f \<equiv> wfrec R (\<lambda>f x. SOME r. P f x r)"
891  from \<open>wf R\<close> show "P f x (f x)"
892  proof (induct x)
893    case (less x)
894    show "P f x (f x)"
895    proof (subst (2) wfrec_def_adm[OF f_def \<open>wf R\<close>])
896      show "adm_wf R (\<lambda>f x. SOME r. P f x r)"
897        by (auto simp add: adm_wf_def intro!: arg_cong[where f=Eps] ext adm)
898      show "P f x (Eps (P f x))"
899        using P by (rule someI_ex) fact
900    qed
901  qed
902qed
903
904lemma (in wellorder) dependent_wellorder_choice:
905  assumes "\<And>r f g x. (\<And>y. y < x \<Longrightarrow> f y = g y) \<Longrightarrow> P f x r = P g x r"
906    and P: "\<And>x f. (\<And>y. y < x \<Longrightarrow> P f y (f y)) \<Longrightarrow> \<exists>r. P f x r"
907  shows "\<exists>f. \<forall>x. P f x (f x)"
908  using wf by (rule dependent_wf_choice) (auto intro!: assms)
909
910end
911