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