src/HOL/Zorn.thy
 changeset 55018 2a526bd279ed parent 54552 5d57cbec0f0f child 55811 aa1acc25126b
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Zorn.thy	Thu Jan 16 16:33:19 2014 +0100
1.3 @@ -0,0 +1,712 @@
1.4 +(*  Title:      HOL/Zorn.thy
1.5 +    Author:     Jacques D. Fleuriot
1.6 +    Author:     Tobias Nipkow, TUM
1.7 +    Author:     Christian Sternagel, JAIST
1.8 +
1.9 +Zorn's Lemma (ported from Larry Paulson's Zorn.thy in ZF).
1.10 +The well-ordering theorem.
1.11 +*)
1.12 +
1.13 +header {* Zorn's Lemma *}
1.14 +
1.15 +theory Zorn
1.16 +imports Order_Relation Hilbert_Choice
1.17 +begin
1.18 +
1.19 +subsection {* Zorn's Lemma for the Subset Relation *}
1.20 +
1.21 +subsubsection {* Results that do not require an order *}
1.22 +
1.23 +text {*Let @{text P} be a binary predicate on the set @{text A}.*}
1.24 +locale pred_on =
1.25 +  fixes A :: "'a set"
1.26 +    and P :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<sqsubset>" 50)
1.27 +begin
1.28 +
1.29 +abbreviation Peq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<sqsubseteq>" 50) where
1.30 +  "x \<sqsubseteq> y \<equiv> P\<^sup>=\<^sup>= x y"
1.31 +
1.32 +text {*A chain is a totally ordered subset of @{term A}.*}
1.33 +definition chain :: "'a set \<Rightarrow> bool" where
1.34 +  "chain C \<longleftrightarrow> C \<subseteq> A \<and> (\<forall>x\<in>C. \<forall>y\<in>C. x \<sqsubseteq> y \<or> y \<sqsubseteq> x)"
1.35 +
1.36 +text {*We call a chain that is a proper superset of some set @{term X},
1.37 +but not necessarily a chain itself, a superchain of @{term X}.*}
1.38 +abbreviation superchain :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" (infix "<c" 50) where
1.39 +  "X <c C \<equiv> chain C \<and> X \<subset> C"
1.40 +
1.41 +text {*A maximal chain is a chain that does not have a superchain.*}
1.42 +definition maxchain :: "'a set \<Rightarrow> bool" where
1.43 +  "maxchain C \<longleftrightarrow> chain C \<and> \<not> (\<exists>S. C <c S)"
1.44 +
1.45 +text {*We define the successor of a set to be an arbitrary
1.46 +superchain, if such exists, or the set itself, otherwise.*}
1.47 +definition suc :: "'a set \<Rightarrow> 'a set" where
1.48 +  "suc C = (if \<not> chain C \<or> maxchain C then C else (SOME D. C <c D))"
1.49 +
1.50 +lemma chainI [Pure.intro?]:
1.51 +  "\<lbrakk>C \<subseteq> A; \<And>x y. \<lbrakk>x \<in> C; y \<in> C\<rbrakk> \<Longrightarrow> x \<sqsubseteq> y \<or> y \<sqsubseteq> x\<rbrakk> \<Longrightarrow> chain C"
1.52 +  unfolding chain_def by blast
1.53 +
1.54 +lemma chain_total:
1.55 +  "chain C \<Longrightarrow> x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
1.56 +  by (simp add: chain_def)
1.57 +
1.58 +lemma not_chain_suc [simp]: "\<not> chain X \<Longrightarrow> suc X = X"
1.59 +  by (simp add: suc_def)
1.60 +
1.61 +lemma maxchain_suc [simp]: "maxchain X \<Longrightarrow> suc X = X"
1.62 +  by (simp add: suc_def)
1.63 +
1.64 +lemma suc_subset: "X \<subseteq> suc X"
1.65 +  by (auto simp: suc_def maxchain_def intro: someI2)
1.66 +
1.67 +lemma chain_empty [simp]: "chain {}"
1.68 +  by (auto simp: chain_def)
1.69 +
1.70 +lemma not_maxchain_Some:
1.71 +  "chain C \<Longrightarrow> \<not> maxchain C \<Longrightarrow> C <c (SOME D. C <c D)"
1.72 +  by (rule someI_ex) (auto simp: maxchain_def)
1.73 +
1.74 +lemma suc_not_equals:
1.75 +  "chain C \<Longrightarrow> \<not> maxchain C \<Longrightarrow> suc C \<noteq> C"
1.76 +  by (auto simp: suc_def) (metis (no_types) less_irrefl not_maxchain_Some)
1.77 +
1.78 +lemma subset_suc:
1.79 +  assumes "X \<subseteq> Y" shows "X \<subseteq> suc Y"
1.80 +  using assms by (rule subset_trans) (rule suc_subset)
1.81 +
1.82 +text {*We build a set @{term \<C>} that is closed under applications
1.83 +of @{term suc} and contains the union of all its subsets.*}
1.84 +inductive_set suc_Union_closed ("\<C>") where
1.85 +  suc: "X \<in> \<C> \<Longrightarrow> suc X \<in> \<C>" |
1.86 +  Union [unfolded Pow_iff]: "X \<in> Pow \<C> \<Longrightarrow> \<Union>X \<in> \<C>"
1.87 +
1.88 +text {*Since the empty set as well as the set itself is a subset of
1.89 +every set, @{term \<C>} contains at least @{term "{} \<in> \<C>"} and
1.90 +@{term "\<Union>\<C> \<in> \<C>"}.*}
1.91 +lemma
1.92 +  suc_Union_closed_empty: "{} \<in> \<C>" and
1.93 +  suc_Union_closed_Union: "\<Union>\<C> \<in> \<C>"
1.94 +  using Union [of "{}"] and Union [of "\<C>"] by simp+
1.95 +text {*Thus closure under @{term suc} will hit a maximal chain
1.96 +eventually, as is shown below.*}
1.97 +
1.98 +lemma suc_Union_closed_induct [consumes 1, case_names suc Union,
1.99 +  induct pred: suc_Union_closed]:
1.100 +  assumes "X \<in> \<C>"
1.101 +    and "\<And>X. \<lbrakk>X \<in> \<C>; Q X\<rbrakk> \<Longrightarrow> Q (suc X)"
1.102 +    and "\<And>X. \<lbrakk>X \<subseteq> \<C>; \<forall>x\<in>X. Q x\<rbrakk> \<Longrightarrow> Q (\<Union>X)"
1.103 +  shows "Q X"
1.104 +  using assms by (induct) blast+
1.105 +
1.106 +lemma suc_Union_closed_cases [consumes 1, case_names suc Union,
1.107 +  cases pred: suc_Union_closed]:
1.108 +  assumes "X \<in> \<C>"
1.109 +    and "\<And>Y. \<lbrakk>X = suc Y; Y \<in> \<C>\<rbrakk> \<Longrightarrow> Q"
1.110 +    and "\<And>Y. \<lbrakk>X = \<Union>Y; Y \<subseteq> \<C>\<rbrakk> \<Longrightarrow> Q"
1.111 +  shows "Q"
1.112 +  using assms by (cases) simp+
1.113 +
1.114 +text {*On chains, @{term suc} yields a chain.*}
1.115 +lemma chain_suc:
1.116 +  assumes "chain X" shows "chain (suc X)"
1.117 +  using assms
1.118 +  by (cases "\<not> chain X \<or> maxchain X")
1.119 +     (force simp: suc_def dest: not_maxchain_Some)+
1.120 +
1.121 +lemma chain_sucD:
1.122 +  assumes "chain X" shows "suc X \<subseteq> A \<and> chain (suc X)"
1.123 +proof -
1.124 +  from `chain X` have *: "chain (suc X)" by (rule chain_suc)
1.125 +  then have "suc X \<subseteq> A" unfolding chain_def by blast
1.126 +  with * show ?thesis by blast
1.127 +qed
1.128 +
1.129 +lemma suc_Union_closed_total':
1.130 +  assumes "X \<in> \<C>" and "Y \<in> \<C>"
1.131 +    and *: "\<And>Z. Z \<in> \<C> \<Longrightarrow> Z \<subseteq> Y \<Longrightarrow> Z = Y \<or> suc Z \<subseteq> Y"
1.132 +  shows "X \<subseteq> Y \<or> suc Y \<subseteq> X"
1.133 +  using `X \<in> \<C>`
1.134 +proof (induct)
1.135 +  case (suc X)
1.136 +  with * show ?case by (blast del: subsetI intro: subset_suc)
1.137 +qed blast
1.138 +
1.139 +lemma suc_Union_closed_subsetD:
1.140 +  assumes "Y \<subseteq> X" and "X \<in> \<C>" and "Y \<in> \<C>"
1.141 +  shows "X = Y \<or> suc Y \<subseteq> X"
1.142 +  using assms(2-, 1)
1.143 +proof (induct arbitrary: Y)
1.144 +  case (suc X)
1.145 +  note * = `\<And>Y. \<lbrakk>Y \<in> \<C>; Y \<subseteq> X\<rbrakk> \<Longrightarrow> X = Y \<or> suc Y \<subseteq> X`
1.146 +  with suc_Union_closed_total' [OF `Y \<in> \<C>` `X \<in> \<C>`]
1.147 +    have "Y \<subseteq> X \<or> suc X \<subseteq> Y" by blast
1.148 +  then show ?case
1.149 +  proof
1.150 +    assume "Y \<subseteq> X"
1.151 +    with * and `Y \<in> \<C>` have "X = Y \<or> suc Y \<subseteq> X" by blast
1.152 +    then show ?thesis
1.153 +    proof
1.154 +      assume "X = Y" then show ?thesis by simp
1.155 +    next
1.156 +      assume "suc Y \<subseteq> X"
1.157 +      then have "suc Y \<subseteq> suc X" by (rule subset_suc)
1.158 +      then show ?thesis by simp
1.159 +    qed
1.160 +  next
1.161 +    assume "suc X \<subseteq> Y"
1.162 +    with `Y \<subseteq> suc X` show ?thesis by blast
1.163 +  qed
1.164 +next
1.165 +  case (Union X)
1.166 +  show ?case
1.167 +  proof (rule ccontr)
1.168 +    assume "\<not> ?thesis"
1.169 +    with `Y \<subseteq> \<Union>X` obtain x y z
1.170 +    where "\<not> suc Y \<subseteq> \<Union>X"
1.171 +      and "x \<in> X" and "y \<in> x" and "y \<notin> Y"
1.172 +      and "z \<in> suc Y" and "\<forall>x\<in>X. z \<notin> x" by blast
1.173 +    with `X \<subseteq> \<C>` have "x \<in> \<C>" by blast
1.174 +    from Union and `x \<in> X`
1.175 +      have *: "\<And>y. \<lbrakk>y \<in> \<C>; y \<subseteq> x\<rbrakk> \<Longrightarrow> x = y \<or> suc y \<subseteq> x" by blast
1.176 +    with suc_Union_closed_total' [OF `Y \<in> \<C>` `x \<in> \<C>`]
1.177 +      have "Y \<subseteq> x \<or> suc x \<subseteq> Y" by blast
1.178 +    then show False
1.179 +    proof
1.180 +      assume "Y \<subseteq> x"
1.181 +      with * [OF `Y \<in> \<C>`] have "x = Y \<or> suc Y \<subseteq> x" by blast
1.182 +      then show False
1.183 +      proof
1.184 +        assume "x = Y" with `y \<in> x` and `y \<notin> Y` show False by blast
1.185 +      next
1.186 +        assume "suc Y \<subseteq> x"
1.187 +        with `x \<in> X` have "suc Y \<subseteq> \<Union>X" by blast
1.188 +        with `\<not> suc Y \<subseteq> \<Union>X` show False by contradiction
1.189 +      qed
1.190 +    next
1.191 +      assume "suc x \<subseteq> Y"
1.192 +      moreover from suc_subset and `y \<in> x` have "y \<in> suc x" by blast
1.193 +      ultimately show False using `y \<notin> Y` by blast
1.194 +    qed
1.195 +  qed
1.196 +qed
1.197 +
1.198 +text {*The elements of @{term \<C>} are totally ordered by the subset relation.*}
1.199 +lemma suc_Union_closed_total:
1.200 +  assumes "X \<in> \<C>" and "Y \<in> \<C>"
1.201 +  shows "X \<subseteq> Y \<or> Y \<subseteq> X"
1.202 +proof (cases "\<forall>Z\<in>\<C>. Z \<subseteq> Y \<longrightarrow> Z = Y \<or> suc Z \<subseteq> Y")
1.203 +  case True
1.204 +  with suc_Union_closed_total' [OF assms]
1.205 +    have "X \<subseteq> Y \<or> suc Y \<subseteq> X" by blast
1.206 +  then show ?thesis using suc_subset [of Y] by blast
1.207 +next
1.208 +  case False
1.209 +  then obtain Z
1.210 +    where "Z \<in> \<C>" and "Z \<subseteq> Y" and "Z \<noteq> Y" and "\<not> suc Z \<subseteq> Y" by blast
1.211 +  with suc_Union_closed_subsetD and `Y \<in> \<C>` show ?thesis by blast
1.212 +qed
1.213 +
1.214 +text {*Once we hit a fixed point w.r.t. @{term suc}, all other elements
1.215 +of @{term \<C>} are subsets of this fixed point.*}
1.216 +lemma suc_Union_closed_suc:
1.217 +  assumes "X \<in> \<C>" and "Y \<in> \<C>" and "suc Y = Y"
1.218 +  shows "X \<subseteq> Y"
1.219 +using `X \<in> \<C>`
1.220 +proof (induct)
1.221 +  case (suc X)
1.222 +  with `Y \<in> \<C>` and suc_Union_closed_subsetD
1.223 +    have "X = Y \<or> suc X \<subseteq> Y" by blast
1.224 +  then show ?case by (auto simp: `suc Y = Y`)
1.225 +qed blast
1.226 +
1.227 +lemma eq_suc_Union:
1.228 +  assumes "X \<in> \<C>"
1.229 +  shows "suc X = X \<longleftrightarrow> X = \<Union>\<C>"
1.230 +proof
1.231 +  assume "suc X = X"
1.232 +  with suc_Union_closed_suc [OF suc_Union_closed_Union `X \<in> \<C>`]
1.233 +    have "\<Union>\<C> \<subseteq> X" .
1.234 +  with `X \<in> \<C>` show "X = \<Union>\<C>" by blast
1.235 +next
1.236 +  from `X \<in> \<C>` have "suc X \<in> \<C>" by (rule suc)
1.237 +  then have "suc X \<subseteq> \<Union>\<C>" by blast
1.238 +  moreover assume "X = \<Union>\<C>"
1.239 +  ultimately have "suc X \<subseteq> X" by simp
1.240 +  moreover have "X \<subseteq> suc X" by (rule suc_subset)
1.241 +  ultimately show "suc X = X" ..
1.242 +qed
1.243 +
1.244 +lemma suc_in_carrier:
1.245 +  assumes "X \<subseteq> A"
1.246 +  shows "suc X \<subseteq> A"
1.247 +  using assms
1.248 +  by (cases "\<not> chain X \<or> maxchain X")
1.249 +     (auto dest: chain_sucD)
1.250 +
1.251 +lemma suc_Union_closed_in_carrier:
1.252 +  assumes "X \<in> \<C>"
1.253 +  shows "X \<subseteq> A"
1.254 +  using assms
1.255 +  by (induct) (auto dest: suc_in_carrier)
1.256 +
1.257 +text {*All elements of @{term \<C>} are chains.*}
1.258 +lemma suc_Union_closed_chain:
1.259 +  assumes "X \<in> \<C>"
1.260 +  shows "chain X"
1.261 +using assms
1.262 +proof (induct)
1.263 +  case (suc X) then show ?case by (simp add: suc_def) (metis (no_types) not_maxchain_Some)
1.264 +next
1.265 +  case (Union X)
1.266 +  then have "\<Union>X \<subseteq> A" by (auto dest: suc_Union_closed_in_carrier)
1.267 +  moreover have "\<forall>x\<in>\<Union>X. \<forall>y\<in>\<Union>X. x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
1.268 +  proof (intro ballI)
1.269 +    fix x y
1.270 +    assume "x \<in> \<Union>X" and "y \<in> \<Union>X"
1.271 +    then obtain u v where "x \<in> u" and "u \<in> X" and "y \<in> v" and "v \<in> X" by blast
1.272 +    with Union have "u \<in> \<C>" and "v \<in> \<C>" and "chain u" and "chain v" by blast+
1.273 +    with suc_Union_closed_total have "u \<subseteq> v \<or> v \<subseteq> u" by blast
1.274 +    then show "x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
1.275 +    proof
1.276 +      assume "u \<subseteq> v"
1.277 +      from `chain v` show ?thesis
1.278 +      proof (rule chain_total)
1.279 +        show "y \<in> v" by fact
1.280 +        show "x \<in> v" using `u \<subseteq> v` and `x \<in> u` by blast
1.281 +      qed
1.282 +    next
1.283 +      assume "v \<subseteq> u"
1.284 +      from `chain u` show ?thesis
1.285 +      proof (rule chain_total)
1.286 +        show "x \<in> u" by fact
1.287 +        show "y \<in> u" using `v \<subseteq> u` and `y \<in> v` by blast
1.288 +      qed
1.289 +    qed
1.290 +  qed
1.291 +  ultimately show ?case unfolding chain_def ..
1.292 +qed
1.293 +
1.294 +subsubsection {* Hausdorff's Maximum Principle *}
1.295 +
1.296 +text {*There exists a maximal totally ordered subset of @{term A}. (Note that we do not
1.297 +require @{term A} to be partially ordered.)*}
1.298 +
1.299 +theorem Hausdorff: "\<exists>C. maxchain C"
1.300 +proof -
1.301 +  let ?M = "\<Union>\<C>"
1.302 +  have "maxchain ?M"
1.303 +  proof (rule ccontr)
1.304 +    assume "\<not> maxchain ?M"
1.305 +    then have "suc ?M \<noteq> ?M"
1.306 +      using suc_not_equals and
1.307 +      suc_Union_closed_chain [OF suc_Union_closed_Union] by simp
1.308 +    moreover have "suc ?M = ?M"
1.309 +      using eq_suc_Union [OF suc_Union_closed_Union] by simp
1.310 +    ultimately show False by contradiction
1.311 +  qed
1.312 +  then show ?thesis by blast
1.313 +qed
1.314 +
1.315 +text {*Make notation @{term \<C>} available again.*}
1.316 +no_notation suc_Union_closed ("\<C>")
1.317 +
1.318 +lemma chain_extend:
1.319 +  "chain C \<Longrightarrow> z \<in> A \<Longrightarrow> \<forall>x\<in>C. x \<sqsubseteq> z \<Longrightarrow> chain ({z} \<union> C)"
1.320 +  unfolding chain_def by blast
1.321 +
1.322 +lemma maxchain_imp_chain:
1.323 +  "maxchain C \<Longrightarrow> chain C"
1.324 +  by (simp add: maxchain_def)
1.325 +
1.326 +end
1.327 +
1.328 +text {*Hide constant @{const pred_on.suc_Union_closed}, which was just needed
1.329 +for the proof of Hausforff's maximum principle.*}
1.330 +hide_const pred_on.suc_Union_closed
1.331 +
1.332 +lemma chain_mono:
1.333 +  assumes "\<And>x y. \<lbrakk>x \<in> A; y \<in> A; P x y\<rbrakk> \<Longrightarrow> Q x y"
1.334 +    and "pred_on.chain A P C"
1.335 +  shows "pred_on.chain A Q C"
1.336 +  using assms unfolding pred_on.chain_def by blast
1.337 +
1.338 +subsubsection {* Results for the proper subset relation *}
1.339 +
1.340 +interpretation subset: pred_on "A" "op \<subset>" for A .
1.341 +
1.342 +lemma subset_maxchain_max:
1.343 +  assumes "subset.maxchain A C" and "X \<in> A" and "\<Union>C \<subseteq> X"
1.344 +  shows "\<Union>C = X"
1.345 +proof (rule ccontr)
1.346 +  let ?C = "{X} \<union> C"
1.347 +  from `subset.maxchain A C` have "subset.chain A C"
1.348 +    and *: "\<And>S. subset.chain A S \<Longrightarrow> \<not> C \<subset> S"
1.349 +    by (auto simp: subset.maxchain_def)
1.350 +  moreover have "\<forall>x\<in>C. x \<subseteq> X" using `\<Union>C \<subseteq> X` by auto
1.351 +  ultimately have "subset.chain A ?C"
1.352 +    using subset.chain_extend [of A C X] and `X \<in> A` by auto
1.353 +  moreover assume **: "\<Union>C \<noteq> X"
1.354 +  moreover from ** have "C \<subset> ?C" using `\<Union>C \<subseteq> X` by auto
1.355 +  ultimately show False using * by blast
1.356 +qed
1.357 +
1.358 +subsubsection {* Zorn's lemma *}
1.359 +
1.360 +text {*If every chain has an upper bound, then there is a maximal set.*}
1.361 +lemma subset_Zorn:
1.362 +  assumes "\<And>C. subset.chain A C \<Longrightarrow> \<exists>U\<in>A. \<forall>X\<in>C. X \<subseteq> U"
1.363 +  shows "\<exists>M\<in>A. \<forall>X\<in>A. M \<subseteq> X \<longrightarrow> X = M"
1.364 +proof -
1.365 +  from subset.Hausdorff [of A] obtain M where "subset.maxchain A M" ..
1.366 +  then have "subset.chain A M" by (rule subset.maxchain_imp_chain)
1.367 +  with assms obtain Y where "Y \<in> A" and "\<forall>X\<in>M. X \<subseteq> Y" by blast
1.368 +  moreover have "\<forall>X\<in>A. Y \<subseteq> X \<longrightarrow> Y = X"
1.369 +  proof (intro ballI impI)
1.370 +    fix X
1.371 +    assume "X \<in> A" and "Y \<subseteq> X"
1.372 +    show "Y = X"
1.373 +    proof (rule ccontr)
1.374 +      assume "Y \<noteq> X"
1.375 +      with `Y \<subseteq> X` have "\<not> X \<subseteq> Y" by blast
1.376 +      from subset.chain_extend [OF `subset.chain A M` `X \<in> A`] and `\<forall>X\<in>M. X \<subseteq> Y`
1.377 +        have "subset.chain A ({X} \<union> M)" using `Y \<subseteq> X` by auto
1.378 +      moreover have "M \<subset> {X} \<union> M" using `\<forall>X\<in>M. X \<subseteq> Y` and `\<not> X \<subseteq> Y` by auto
1.379 +      ultimately show False
1.380 +        using `subset.maxchain A M` by (auto simp: subset.maxchain_def)
1.381 +    qed
1.382 +  qed
1.383 +  ultimately show ?thesis by metis
1.384 +qed
1.385 +
1.386 +text{*Alternative version of Zorn's lemma for the subset relation.*}
1.387 +lemma subset_Zorn':
1.388 +  assumes "\<And>C. subset.chain A C \<Longrightarrow> \<Union>C \<in> A"
1.389 +  shows "\<exists>M\<in>A. \<forall>X\<in>A. M \<subseteq> X \<longrightarrow> X = M"
1.390 +proof -
1.391 +  from subset.Hausdorff [of A] obtain M where "subset.maxchain A M" ..
1.392 +  then have "subset.chain A M" by (rule subset.maxchain_imp_chain)
1.393 +  with assms have "\<Union>M \<in> A" .
1.394 +  moreover have "\<forall>Z\<in>A. \<Union>M \<subseteq> Z \<longrightarrow> \<Union>M = Z"
1.395 +  proof (intro ballI impI)
1.396 +    fix Z
1.397 +    assume "Z \<in> A" and "\<Union>M \<subseteq> Z"
1.398 +    with subset_maxchain_max [OF `subset.maxchain A M`]
1.399 +      show "\<Union>M = Z" .
1.400 +  qed
1.401 +  ultimately show ?thesis by blast
1.402 +qed
1.403 +
1.404 +
1.405 +subsection {* Zorn's Lemma for Partial Orders *}
1.406 +
1.407 +text {*Relate old to new definitions.*}
1.408 +
1.409 +(* Define globally? In Set.thy? *)
1.410 +definition chain_subset :: "'a set set \<Rightarrow> bool" ("chain\<^sub>\<subseteq>") where
1.411 +  "chain\<^sub>\<subseteq> C \<longleftrightarrow> (\<forall>A\<in>C. \<forall>B\<in>C. A \<subseteq> B \<or> B \<subseteq> A)"
1.412 +
1.413 +definition chains :: "'a set set \<Rightarrow> 'a set set set" where
1.414 +  "chains A = {C. C \<subseteq> A \<and> chain\<^sub>\<subseteq> C}"
1.415 +
1.416 +(* Define globally? In Relation.thy? *)
1.417 +definition Chains :: "('a \<times> 'a) set \<Rightarrow> 'a set set" where
1.418 +  "Chains r = {C. \<forall>a\<in>C. \<forall>b\<in>C. (a, b) \<in> r \<or> (b, a) \<in> r}"
1.419 +
1.420 +lemma chains_extend:
1.421 +  "[| c \<in> chains S; z \<in> S; \<forall>x \<in> c. x \<subseteq> (z:: 'a set) |] ==> {z} Un c \<in> chains S"
1.422 +  by (unfold chains_def chain_subset_def) blast
1.423 +
1.424 +lemma mono_Chains: "r \<subseteq> s \<Longrightarrow> Chains r \<subseteq> Chains s"
1.425 +  unfolding Chains_def by blast
1.426 +
1.427 +lemma chain_subset_alt_def: "chain\<^sub>\<subseteq> C = subset.chain UNIV C"
1.428 +  unfolding chain_subset_def subset.chain_def by fast
1.429 +
1.430 +lemma chains_alt_def: "chains A = {C. subset.chain A C}"
1.431 +  by (simp add: chains_def chain_subset_alt_def subset.chain_def)
1.432 +
1.433 +lemma Chains_subset:
1.434 +  "Chains r \<subseteq> {C. pred_on.chain UNIV (\<lambda>x y. (x, y) \<in> r) C}"
1.435 +  by (force simp add: Chains_def pred_on.chain_def)
1.436 +
1.437 +lemma Chains_subset':
1.438 +  assumes "refl r"
1.439 +  shows "{C. pred_on.chain UNIV (\<lambda>x y. (x, y) \<in> r) C} \<subseteq> Chains r"
1.440 +  using assms
1.441 +  by (auto simp add: Chains_def pred_on.chain_def refl_on_def)
1.442 +
1.443 +lemma Chains_alt_def:
1.444 +  assumes "refl r"
1.445 +  shows "Chains r = {C. pred_on.chain UNIV (\<lambda>x y. (x, y) \<in> r) C}"
1.446 +  using assms
1.447 +  by (metis Chains_subset Chains_subset' subset_antisym)
1.448 +
1.449 +lemma Zorn_Lemma:
1.450 +  "\<forall>C\<in>chains A. \<Union>C \<in> A \<Longrightarrow> \<exists>M\<in>A. \<forall>X\<in>A. M \<subseteq> X \<longrightarrow> X = M"
1.451 +  using subset_Zorn' [of A] by (force simp: chains_alt_def)
1.452 +
1.453 +lemma Zorn_Lemma2:
1.454 +  "\<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"
1.455 +  using subset_Zorn [of A] by (auto simp: chains_alt_def)
1.456 +
1.457 +text{*Various other lemmas*}
1.458 +
1.459 +lemma chainsD: "[| c \<in> chains S; x \<in> c; y \<in> c |] ==> x \<subseteq> y | y \<subseteq> x"
1.460 +by (unfold chains_def chain_subset_def) blast
1.461 +
1.462 +lemma chainsD2: "!!(c :: 'a set set). c \<in> chains S ==> c \<subseteq> S"
1.463 +by (unfold chains_def) blast
1.464 +
1.465 +lemma Zorns_po_lemma:
1.466 +  assumes po: "Partial_order r"
1.467 +    and u: "\<forall>C\<in>Chains r. \<exists>u\<in>Field r. \<forall>a\<in>C. (a, u) \<in> r"
1.468 +  shows "\<exists>m\<in>Field r. \<forall>a\<in>Field r. (m, a) \<in> r \<longrightarrow> a = m"
1.469 +proof -
1.470 +  have "Preorder r" using po by (simp add: partial_order_on_def)
1.471 +--{* Mirror r in the set of subsets below (wrt r) elements of A*}
1.472 +  let ?B = "%x. r\<inverse> `` {x}" let ?S = "?B ` Field r"
1.473 +  {
1.474 +    fix C assume 1: "C \<subseteq> ?S" and 2: "\<forall>A\<in>C. \<forall>B\<in>C. A \<subseteq> B \<or> B \<subseteq> A"
1.475 +    let ?A = "{x\<in>Field r. \<exists>M\<in>C. M = ?B x}"
1.476 +    have "C = ?B ` ?A" using 1 by (auto simp: image_def)
1.477 +    have "?A \<in> Chains r"
1.478 +    proof (simp add: Chains_def, intro allI impI, elim conjE)
1.479 +      fix a b
1.480 +      assume "a \<in> Field r" and "?B a \<in> C" and "b \<in> Field r" and "?B b \<in> C"
1.481 +      hence "?B a \<subseteq> ?B b \<or> ?B b \<subseteq> ?B a" using 2 by auto
1.482 +      thus "(a, b) \<in> r \<or> (b, a) \<in> r"
1.483 +        using `Preorder r` and `a \<in> Field r` and `b \<in> Field r`
1.485 +    qed
1.486 +    then obtain u where uA: "u \<in> Field r" "\<forall>a\<in>?A. (a, u) \<in> r" using u by auto
1.487 +    have "\<forall>A\<in>C. A \<subseteq> r\<inverse> `` {u}" (is "?P u")
1.488 +    proof auto
1.489 +      fix a B assume aB: "B \<in> C" "a \<in> B"
1.490 +      with 1 obtain x where "x \<in> Field r" and "B = r\<inverse> `` {x}" by auto
1.491 +      thus "(a, u) \<in> r" using uA and aB and `Preorder r`
1.492 +        unfolding preorder_on_def refl_on_def by simp (fast dest: transD)
1.493 +    qed
1.494 +    then have "\<exists>u\<in>Field r. ?P u" using `u \<in> Field r` by blast
1.495 +  }
1.496 +  then have "\<forall>C\<in>chains ?S. \<exists>U\<in>?S. \<forall>A\<in>C. A \<subseteq> U"
1.497 +    by (auto simp: chains_def chain_subset_def)
1.498 +  from Zorn_Lemma2 [OF this]
1.499 +  obtain m B where "m \<in> Field r" and "B = r\<inverse> `` {m}"
1.500 +    and "\<forall>x\<in>Field r. B \<subseteq> r\<inverse> `` {x} \<longrightarrow> r\<inverse> `` {x} = B"
1.501 +    by auto
1.502 +  hence "\<forall>a\<in>Field r. (m, a) \<in> r \<longrightarrow> a = m"
1.503 +    using po and `Preorder r` and `m \<in> Field r`
1.504 +    by (auto simp: subset_Image1_Image1_iff Partial_order_eq_Image1_Image1_iff)
1.505 +  thus ?thesis using `m \<in> Field r` by blast
1.506 +qed
1.507 +
1.508 +
1.509 +subsection {* The Well Ordering Theorem *}
1.510 +
1.511 +(* The initial segment of a relation appears generally useful.
1.512 +   Move to Relation.thy?
1.513 +   Definition correct/most general?
1.514 +   Naming?
1.515 +*)
1.516 +definition init_seg_of :: "(('a \<times> 'a) set \<times> ('a \<times> 'a) set) set" where
1.517 +  "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)}"
1.518 +
1.519 +abbreviation
1.520 +  initialSegmentOf :: "('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> bool" (infix "initial'_segment'_of" 55)
1.521 +where
1.522 +  "r initial_segment_of s \<equiv> (r, s) \<in> init_seg_of"
1.523 +
1.524 +lemma refl_on_init_seg_of [simp]: "r initial_segment_of r"
1.525 +  by (simp add: init_seg_of_def)
1.526 +
1.527 +lemma trans_init_seg_of:
1.528 +  "r initial_segment_of s \<Longrightarrow> s initial_segment_of t \<Longrightarrow> r initial_segment_of t"
1.529 +  by (simp (no_asm_use) add: init_seg_of_def) blast
1.530 +
1.531 +lemma antisym_init_seg_of:
1.532 +  "r initial_segment_of s \<Longrightarrow> s initial_segment_of r \<Longrightarrow> r = s"
1.533 +  unfolding init_seg_of_def by safe
1.534 +
1.535 +lemma Chains_init_seg_of_Union:
1.536 +  "R \<in> Chains init_seg_of \<Longrightarrow> r\<in>R \<Longrightarrow> r initial_segment_of \<Union>R"
1.537 +  by (auto simp: init_seg_of_def Ball_def Chains_def) blast
1.538 +
1.539 +lemma chain_subset_trans_Union:
1.540 +  "chain\<^sub>\<subseteq> R \<Longrightarrow> \<forall>r\<in>R. trans r \<Longrightarrow> trans (\<Union>R)"
1.541 +apply (auto simp add: chain_subset_def)
1.542 +apply (simp (no_asm_use) add: trans_def)
1.543 +by (metis subsetD)
1.544 +
1.545 +lemma chain_subset_antisym_Union:
1.546 +  "chain\<^sub>\<subseteq> R \<Longrightarrow> \<forall>r\<in>R. antisym r \<Longrightarrow> antisym (\<Union>R)"
1.547 +unfolding chain_subset_def antisym_def
1.548 +apply simp
1.549 +by (metis (no_types) subsetD)
1.550 +
1.551 +lemma chain_subset_Total_Union:
1.552 +  assumes "chain\<^sub>\<subseteq> R" and "\<forall>r\<in>R. Total r"
1.553 +  shows "Total (\<Union>R)"
1.554 +proof (simp add: total_on_def Ball_def, auto del: disjCI)
1.555 +  fix r s a b assume A: "r \<in> R" "s \<in> R" "a \<in> Field r" "b \<in> Field s" "a \<noteq> b"
1.556 +  from `chain\<^sub>\<subseteq> R` and `r \<in> R` and `s \<in> R` have "r \<subseteq> s \<or> s \<subseteq> r"
1.557 +    by (auto simp add: chain_subset_def)
1.558 +  thus "(\<exists>r\<in>R. (a, b) \<in> r) \<or> (\<exists>r\<in>R. (b, a) \<in> r)"
1.559 +  proof
1.560 +    assume "r \<subseteq> s" hence "(a, b) \<in> s \<or> (b, a) \<in> s" using assms(2) A
1.561 +      by (simp add: total_on_def) (metis (no_types) mono_Field subsetD)
1.562 +    thus ?thesis using `s \<in> R` by blast
1.563 +  next
1.564 +    assume "s \<subseteq> r" hence "(a, b) \<in> r \<or> (b, a) \<in> r" using assms(2) A
1.565 +      by (simp add: total_on_def) (metis (no_types) mono_Field subsetD)
1.566 +    thus ?thesis using `r \<in> R` by blast
1.567 +  qed
1.568 +qed
1.569 +
1.570 +lemma wf_Union_wf_init_segs:
1.571 +  assumes "R \<in> Chains init_seg_of" and "\<forall>r\<in>R. wf r"
1.572 +  shows "wf (\<Union>R)"
1.573 +proof(simp add: wf_iff_no_infinite_down_chain, rule ccontr, auto)
1.574 +  fix f assume 1: "\<forall>i. \<exists>r\<in>R. (f (Suc i), f i) \<in> r"
1.575 +  then obtain r where "r \<in> R" and "(f (Suc 0), f 0) \<in> r" by auto
1.576 +  { fix i have "(f (Suc i), f i) \<in> r"
1.577 +    proof (induct i)
1.578 +      case 0 show ?case by fact
1.579 +    next
1.580 +      case (Suc i)
1.581 +      then obtain s where s: "s \<in> R" "(f (Suc (Suc i)), f(Suc i)) \<in> s"
1.582 +        using 1 by auto
1.583 +      then have "s initial_segment_of r \<or> r initial_segment_of s"
1.584 +        using assms(1) `r \<in> R` by (simp add: Chains_def)
1.585 +      with Suc s show ?case by (simp add: init_seg_of_def) blast
1.586 +    qed
1.587 +  }
1.588 +  thus False using assms(2) and `r \<in> R`
1.589 +    by (simp add: wf_iff_no_infinite_down_chain) blast
1.590 +qed
1.591 +
1.592 +lemma initial_segment_of_Diff:
1.593 +  "p initial_segment_of q \<Longrightarrow> p - s initial_segment_of q - s"
1.594 +  unfolding init_seg_of_def by blast
1.595 +
1.596 +lemma Chains_inits_DiffI:
1.597 +  "R \<in> Chains init_seg_of \<Longrightarrow> {r - s |r. r \<in> R} \<in> Chains init_seg_of"
1.598 +  unfolding Chains_def by (blast intro: initial_segment_of_Diff)
1.599 +
1.600 +theorem well_ordering: "\<exists>r::'a rel. Well_order r \<and> Field r = UNIV"
1.601 +proof -
1.602 +-- {*The initial segment relation on well-orders: *}
1.603 +  let ?WO = "{r::'a rel. Well_order r}"
1.604 +  def I \<equiv> "init_seg_of \<inter> ?WO \<times> ?WO"
1.605 +  have I_init: "I \<subseteq> init_seg_of" by (auto simp: I_def)
1.606 +  hence subch: "\<And>R. R \<in> Chains I \<Longrightarrow> chain\<^sub>\<subseteq> R"
1.607 +    unfolding init_seg_of_def chain_subset_def Chains_def by blast
1.608 +  have Chains_wo: "\<And>R r. R \<in> Chains I \<Longrightarrow> r \<in> R \<Longrightarrow> Well_order r"
1.609 +    by (simp add: Chains_def I_def) blast
1.610 +  have FI: "Field I = ?WO" by (auto simp add: I_def init_seg_of_def Field_def)
1.611 +  hence 0: "Partial_order I"
1.612 +    by (auto simp: partial_order_on_def preorder_on_def antisym_def antisym_init_seg_of refl_on_def
1.613 +      trans_def I_def elim!: trans_init_seg_of)
1.614 +-- {*I-chains have upper bounds in ?WO wrt I: their Union*}
1.615 +  { fix R assume "R \<in> Chains I"
1.616 +    hence Ris: "R \<in> Chains init_seg_of" using mono_Chains [OF I_init] by blast
1.617 +    have subch: "chain\<^sub>\<subseteq> R" using `R : Chains I` I_init
1.618 +      by (auto simp: init_seg_of_def chain_subset_def Chains_def)
1.619 +    have "\<forall>r\<in>R. Refl r" and "\<forall>r\<in>R. trans r" and "\<forall>r\<in>R. antisym r"
1.620 +      and "\<forall>r\<in>R. Total r" and "\<forall>r\<in>R. wf (r - Id)"
1.621 +      using Chains_wo [OF `R \<in> Chains I`] by (simp_all add: order_on_defs)
1.622 +    have "Refl (\<Union>R)" using `\<forall>r\<in>R. Refl r` unfolding refl_on_def by fastforce
1.623 +    moreover have "trans (\<Union>R)"
1.624 +      by (rule chain_subset_trans_Union [OF subch `\<forall>r\<in>R. trans r`])
1.625 +    moreover have "antisym (\<Union>R)"
1.626 +      by (rule chain_subset_antisym_Union [OF subch `\<forall>r\<in>R. antisym r`])
1.627 +    moreover have "Total (\<Union>R)"
1.628 +      by (rule chain_subset_Total_Union [OF subch `\<forall>r\<in>R. Total r`])
1.629 +    moreover have "wf ((\<Union>R) - Id)"
1.630 +    proof -
1.631 +      have "(\<Union>R) - Id = \<Union>{r - Id | r. r \<in> R}" by blast
1.632 +      with `\<forall>r\<in>R. wf (r - Id)` and wf_Union_wf_init_segs [OF Chains_inits_DiffI [OF Ris]]
1.633 +      show ?thesis by fastforce
1.634 +    qed
1.635 +    ultimately have "Well_order (\<Union>R)" by(simp add:order_on_defs)
1.636 +    moreover have "\<forall>r \<in> R. r initial_segment_of \<Union>R" using Ris
1.638 +    ultimately have "\<Union>R \<in> ?WO \<and> (\<forall>r\<in>R. (r, \<Union>R) \<in> I)"
1.639 +      using mono_Chains [OF I_init] and `R \<in> Chains I`
1.640 +      by (simp (no_asm) add: I_def del: Field_Union) (metis Chains_wo)
1.641 +  }
1.642 +  hence 1: "\<forall>R \<in> Chains I. \<exists>u\<in>Field I. \<forall>r\<in>R. (r, u) \<in> I" by (subst FI) blast
1.643 +--{*Zorn's Lemma yields a maximal well-order m:*}
1.644 +  then obtain m::"'a rel" where "Well_order m" and
1.645 +    max: "\<forall>r. Well_order r \<and> (m, r) \<in> I \<longrightarrow> r = m"
1.646 +    using Zorns_po_lemma[OF 0 1] unfolding FI by fastforce
1.647 +--{*Now show by contradiction that m covers the whole type:*}
1.648 +  { fix x::'a assume "x \<notin> Field m"
1.649 +--{*We assume that x is not covered and extend m at the top with x*}
1.650 +    have "m \<noteq> {}"
1.651 +    proof
1.652 +      assume "m = {}"
1.653 +      moreover have "Well_order {(x, x)}"
1.654 +        by (simp add: order_on_defs refl_on_def trans_def antisym_def total_on_def Field_def)
1.655 +      ultimately show False using max
1.656 +        by (auto simp: I_def init_seg_of_def simp del: Field_insert)
1.657 +    qed
1.658 +    hence "Field m \<noteq> {}" by(auto simp:Field_def)
1.659 +    moreover have "wf (m - Id)" using `Well_order m`
1.660 +      by (simp add: well_order_on_def)
1.661 +--{*The extension of m by x:*}
1.662 +    let ?s = "{(a, x) | a. a \<in> Field m}"
1.663 +    let ?m = "insert (x, x) m \<union> ?s"
1.664 +    have Fm: "Field ?m = insert x (Field m)"
1.665 +      by (auto simp: Field_def)
1.666 +    have "Refl m" and "trans m" and "antisym m" and "Total m" and "wf (m - Id)"
1.667 +      using `Well_order m` by (simp_all add: order_on_defs)
1.668 +--{*We show that the extension is a well-order*}
1.669 +    have "Refl ?m" using `Refl m` Fm unfolding refl_on_def by blast
1.670 +    moreover have "trans ?m" using `trans m` and `x \<notin> Field m`
1.671 +      unfolding trans_def Field_def by blast
1.672 +    moreover have "antisym ?m" using `antisym m` and `x \<notin> Field m`
1.673 +      unfolding antisym_def Field_def by blast
1.674 +    moreover have "Total ?m" using `Total m` and Fm by (auto simp: total_on_def)
1.675 +    moreover have "wf (?m - Id)"
1.676 +    proof -
1.677 +      have "wf ?s" using `x \<notin> Field m`
1.678 +        by (auto simp add: wf_eq_minimal Field_def) metis
1.679 +      thus ?thesis using `wf (m - Id)` and `x \<notin> Field m`
1.680 +        wf_subset [OF `wf ?s` Diff_subset]
1.681 +        unfolding Un_Diff Field_def by (auto intro: wf_Un)
1.682 +    qed
1.683 +    ultimately have "Well_order ?m" by (simp add: order_on_defs)
1.684 +--{*We show that the extension is above m*}
1.685 +    moreover have "(m, ?m) \<in> I" using `Well_order ?m` and `Well_order m` and `x \<notin> Field m`
1.686 +      by (fastforce simp: I_def init_seg_of_def Field_def)
1.687 +    ultimately
1.688 +--{*This contradicts maximality of m:*}
1.689 +    have False using max and `x \<notin> Field m` unfolding Field_def by blast
1.690 +  }
1.691 +  hence "Field m = UNIV" by auto
1.692 +  with `Well_order m` show ?thesis by blast
1.693 +qed
1.694 +
1.695 +corollary well_order_on: "\<exists>r::'a rel. well_order_on A r"
1.696 +proof -
1.697 +  obtain r::"'a rel" where wo: "Well_order r" and univ: "Field r = UNIV"
1.698 +    using well_ordering [where 'a = "'a"] by blast
1.699 +  let ?r = "{(x, y). x \<in> A \<and> y \<in> A \<and> (x, y) \<in> r}"
1.700 +  have 1: "Field ?r = A" using wo univ
1.701 +    by (fastforce simp: Field_def order_on_defs refl_on_def)
1.702 +  have "Refl r" and "trans r" and "antisym r" and "Total r" and "wf (r - Id)"
1.703 +    using `Well_order r` by (simp_all add: order_on_defs)
1.704 +  have "Refl ?r" using `Refl r` by (auto simp: refl_on_def 1 univ)
1.705 +  moreover have "trans ?r" using `trans r`
1.706 +    unfolding trans_def by blast
1.707 +  moreover have "antisym ?r" using `antisym r`
1.708 +    unfolding antisym_def by blast
1.709 +  moreover have "Total ?r" using `Total r` by (simp add:total_on_def 1 univ)
1.710 +  moreover have "wf (?r - Id)" by (rule wf_subset [OF `wf (r - Id)`]) blast
1.711 +  ultimately have "Well_order ?r" by (simp add: order_on_defs)
1.712 +  with 1 show ?thesis by auto
1.713 +qed
1.714 +
1.715 +end
```