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