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