src/HOL/HOLCF/Completion.thy
author haftmann
Sun Jun 23 21:16:07 2013 +0200 (2013-06-23)
changeset 52435 6646bb548c6b
parent 42151 4da4fc77664b
child 54863 82acc20ded73
permissions -rw-r--r--
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
wenzelm@42151
     1
(*  Title:      HOL/HOLCF/Completion.thy
huffman@27404
     2
    Author:     Brian Huffman
huffman@27404
     3
*)
huffman@27404
     4
huffman@39974
     5
header {* Defining algebraic domains by ideal completion *}
huffman@27404
     6
huffman@27404
     7
theory Completion
huffman@40502
     8
imports Plain_HOLCF
huffman@27404
     9
begin
huffman@27404
    10
huffman@27404
    11
subsection {* Ideals over a preorder *}
huffman@27404
    12
huffman@27404
    13
locale preorder =
huffman@27404
    14
  fixes r :: "'a::type \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<preceq>" 50)
huffman@27404
    15
  assumes r_refl: "x \<preceq> x"
huffman@27404
    16
  assumes r_trans: "\<lbrakk>x \<preceq> y; y \<preceq> z\<rbrakk> \<Longrightarrow> x \<preceq> z"
huffman@27404
    17
begin
huffman@27404
    18
huffman@27404
    19
definition
huffman@27404
    20
  ideal :: "'a set \<Rightarrow> bool" where
huffman@27404
    21
  "ideal A = ((\<exists>x. x \<in> A) \<and> (\<forall>x\<in>A. \<forall>y\<in>A. \<exists>z\<in>A. x \<preceq> z \<and> y \<preceq> z) \<and>
huffman@27404
    22
    (\<forall>x y. x \<preceq> y \<longrightarrow> y \<in> A \<longrightarrow> x \<in> A))"
huffman@27404
    23
huffman@27404
    24
lemma idealI:
huffman@27404
    25
  assumes "\<exists>x. x \<in> A"
huffman@27404
    26
  assumes "\<And>x y. \<lbrakk>x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> \<exists>z\<in>A. x \<preceq> z \<and> y \<preceq> z"
huffman@27404
    27
  assumes "\<And>x y. \<lbrakk>x \<preceq> y; y \<in> A\<rbrakk> \<Longrightarrow> x \<in> A"
huffman@27404
    28
  shows "ideal A"
wenzelm@41529
    29
unfolding ideal_def using assms by fast
huffman@27404
    30
huffman@27404
    31
lemma idealD1:
huffman@27404
    32
  "ideal A \<Longrightarrow> \<exists>x. x \<in> A"
huffman@27404
    33
unfolding ideal_def by fast
huffman@27404
    34
huffman@27404
    35
lemma idealD2:
huffman@27404
    36
  "\<lbrakk>ideal A; x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> \<exists>z\<in>A. x \<preceq> z \<and> y \<preceq> z"
huffman@27404
    37
unfolding ideal_def by fast
huffman@27404
    38
huffman@27404
    39
lemma idealD3:
huffman@27404
    40
  "\<lbrakk>ideal A; x \<preceq> y; y \<in> A\<rbrakk> \<Longrightarrow> x \<in> A"
huffman@27404
    41
unfolding ideal_def by fast
huffman@27404
    42
huffman@27404
    43
lemma ideal_principal: "ideal {x. x \<preceq> z}"
huffman@27404
    44
apply (rule idealI)
huffman@27404
    45
apply (rule_tac x=z in exI)
huffman@27404
    46
apply (fast intro: r_refl)
huffman@27404
    47
apply (rule_tac x=z in bexI, fast)
huffman@27404
    48
apply (fast intro: r_refl)
huffman@27404
    49
apply (fast intro: r_trans)
huffman@27404
    50
done
huffman@27404
    51
huffman@40888
    52
lemma ex_ideal: "\<exists>A. A \<in> {A. ideal A}"
huffman@40888
    53
by (fast intro: ideal_principal)
huffman@27404
    54
huffman@27404
    55
text {* The set of ideals is a cpo *}
huffman@27404
    56
huffman@27404
    57
lemma ideal_UN:
huffman@27404
    58
  fixes A :: "nat \<Rightarrow> 'a set"
huffman@27404
    59
  assumes ideal_A: "\<And>i. ideal (A i)"
huffman@27404
    60
  assumes chain_A: "\<And>i j. i \<le> j \<Longrightarrow> A i \<subseteq> A j"
huffman@27404
    61
  shows "ideal (\<Union>i. A i)"
huffman@27404
    62
 apply (rule idealI)
huffman@27404
    63
   apply (cut_tac idealD1 [OF ideal_A], fast)
huffman@27404
    64
  apply (clarify, rename_tac i j)
huffman@27404
    65
  apply (drule subsetD [OF chain_A [OF le_maxI1]])
huffman@27404
    66
  apply (drule subsetD [OF chain_A [OF le_maxI2]])
huffman@27404
    67
  apply (drule (1) idealD2 [OF ideal_A])
huffman@27404
    68
  apply blast
huffman@27404
    69
 apply clarify
huffman@27404
    70
 apply (drule (1) idealD3 [OF ideal_A])
huffman@27404
    71
 apply fast
huffman@27404
    72
done
huffman@27404
    73
huffman@27404
    74
lemma typedef_ideal_po:
huffman@31076
    75
  fixes Abs :: "'a set \<Rightarrow> 'b::below"
huffman@27404
    76
  assumes type: "type_definition Rep Abs {S. ideal S}"
huffman@31076
    77
  assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
huffman@27404
    78
  shows "OFCLASS('b, po_class)"
huffman@31076
    79
 apply (intro_classes, unfold below)
huffman@27404
    80
   apply (rule subset_refl)
huffman@27404
    81
  apply (erule (1) subset_trans)
huffman@27404
    82
 apply (rule type_definition.Rep_inject [OF type, THEN iffD1])
huffman@27404
    83
 apply (erule (1) subset_antisym)
huffman@27404
    84
done
huffman@27404
    85
huffman@27404
    86
lemma
huffman@27404
    87
  fixes Abs :: "'a set \<Rightarrow> 'b::po"
huffman@27404
    88
  assumes type: "type_definition Rep Abs {S. ideal S}"
huffman@31076
    89
  assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
huffman@27404
    90
  assumes S: "chain S"
huffman@27404
    91
  shows typedef_ideal_lub: "range S <<| Abs (\<Union>i. Rep (S i))"
huffman@40769
    92
    and typedef_ideal_rep_lub: "Rep (\<Squnion>i. S i) = (\<Union>i. Rep (S i))"
huffman@27404
    93
proof -
huffman@27404
    94
  have 1: "ideal (\<Union>i. Rep (S i))"
huffman@27404
    95
    apply (rule ideal_UN)
huffman@27404
    96
     apply (rule type_definition.Rep [OF type, unfolded mem_Collect_eq])
huffman@31076
    97
    apply (subst below [symmetric])
huffman@27404
    98
    apply (erule chain_mono [OF S])
huffman@27404
    99
    done
huffman@27404
   100
  hence 2: "Rep (Abs (\<Union>i. Rep (S i))) = (\<Union>i. Rep (S i))"
huffman@27404
   101
    by (simp add: type_definition.Abs_inverse [OF type])
huffman@27404
   102
  show 3: "range S <<| Abs (\<Union>i. Rep (S i))"
huffman@27404
   103
    apply (rule is_lubI)
huffman@27404
   104
     apply (rule is_ubI)
huffman@31076
   105
     apply (simp add: below 2, fast)
huffman@31076
   106
    apply (simp add: below 2 is_ub_def, fast)
huffman@27404
   107
    done
huffman@27404
   108
  hence 4: "(\<Squnion>i. S i) = Abs (\<Union>i. Rep (S i))"
huffman@40771
   109
    by (rule lub_eqI)
huffman@27404
   110
  show 5: "Rep (\<Squnion>i. S i) = (\<Union>i. Rep (S i))"
huffman@27404
   111
    by (simp add: 4 2)
huffman@27404
   112
qed
huffman@27404
   113
huffman@27404
   114
lemma typedef_ideal_cpo:
huffman@27404
   115
  fixes Abs :: "'a set \<Rightarrow> 'b::po"
huffman@27404
   116
  assumes type: "type_definition Rep Abs {S. ideal S}"
huffman@31076
   117
  assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
huffman@27404
   118
  shows "OFCLASS('b, cpo_class)"
huffman@31076
   119
by (default, rule exI, erule typedef_ideal_lub [OF type below])
huffman@27404
   120
huffman@27404
   121
end
huffman@27404
   122
huffman@31076
   123
interpretation below: preorder "below :: 'a::po \<Rightarrow> 'a \<Rightarrow> bool"
huffman@27404
   124
apply unfold_locales
huffman@31076
   125
apply (rule below_refl)
huffman@31076
   126
apply (erule (1) below_trans)
huffman@27404
   127
done
huffman@27404
   128
huffman@28133
   129
subsection {* Lemmas about least upper bounds *}
huffman@27404
   130
huffman@39974
   131
lemma is_ub_thelub_ex: "\<lbrakk>\<exists>u. S <<| u; x \<in> S\<rbrakk> \<Longrightarrow> x \<sqsubseteq> lub S"
huffman@40771
   132
apply (erule exE, drule is_lub_lub)
huffman@27404
   133
apply (drule is_lubD1)
huffman@27404
   134
apply (erule (1) is_ubD)
huffman@27404
   135
done
huffman@27404
   136
huffman@39974
   137
lemma is_lub_thelub_ex: "\<lbrakk>\<exists>u. S <<| u; S <| x\<rbrakk> \<Longrightarrow> lub S \<sqsubseteq> x"
huffman@40771
   138
by (erule exE, drule is_lub_lub, erule is_lubD2)
huffman@27404
   139
huffman@28133
   140
subsection {* Locale for ideal completion *}
huffman@28133
   141
huffman@39974
   142
locale ideal_completion = preorder +
huffman@27404
   143
  fixes principal :: "'a::type \<Rightarrow> 'b::cpo"
huffman@27404
   144
  fixes rep :: "'b::cpo \<Rightarrow> 'a::type set"
huffman@39974
   145
  assumes ideal_rep: "\<And>x. ideal (rep x)"
huffman@40769
   146
  assumes rep_lub: "\<And>Y. chain Y \<Longrightarrow> rep (\<Squnion>i. Y i) = (\<Union>i. rep (Y i))"
huffman@27404
   147
  assumes rep_principal: "\<And>a. rep (principal a) = {b. b \<preceq> a}"
huffman@41033
   148
  assumes belowI: "\<And>x y. rep x \<subseteq> rep y \<Longrightarrow> x \<sqsubseteq> y"
huffman@39974
   149
  assumes countable: "\<exists>f::'a \<Rightarrow> nat. inj f"
huffman@27404
   150
begin
huffman@27404
   151
huffman@28133
   152
lemma rep_mono: "x \<sqsubseteq> y \<Longrightarrow> rep x \<subseteq> rep y"
huffman@28133
   153
apply (frule bin_chain)
huffman@40769
   154
apply (drule rep_lub)
huffman@40771
   155
apply (simp only: lub_eqI [OF is_lub_bin_chain])
huffman@28133
   156
apply (rule subsetI, rule UN_I [where a=0], simp_all)
huffman@28133
   157
done
huffman@28133
   158
huffman@31076
   159
lemma below_def: "x \<sqsubseteq> y \<longleftrightarrow> rep x \<subseteq> rep y"
huffman@41033
   160
by (rule iffI [OF rep_mono belowI])
huffman@28133
   161
huffman@31076
   162
lemma principal_below_iff_mem_rep: "principal a \<sqsubseteq> x \<longleftrightarrow> a \<in> rep x"
huffman@41033
   163
unfolding below_def rep_principal
huffman@41033
   164
by (auto intro: r_refl elim: idealD3 [OF ideal_rep])
huffman@28133
   165
huffman@31076
   166
lemma principal_below_iff [simp]: "principal a \<sqsubseteq> principal b \<longleftrightarrow> a \<preceq> b"
huffman@31076
   167
by (simp add: principal_below_iff_mem_rep rep_principal)
huffman@28133
   168
huffman@28133
   169
lemma principal_eq_iff: "principal a = principal b \<longleftrightarrow> a \<preceq> b \<and> b \<preceq> a"
huffman@31076
   170
unfolding po_eq_conv [where 'a='b] principal_below_iff ..
huffman@28133
   171
huffman@39974
   172
lemma eq_iff: "x = y \<longleftrightarrow> rep x = rep y"
huffman@39974
   173
unfolding po_eq_conv below_def by auto
huffman@39974
   174
huffman@28133
   175
lemma principal_mono: "a \<preceq> b \<Longrightarrow> principal a \<sqsubseteq> principal b"
huffman@31076
   176
by (simp only: principal_below_iff)
huffman@28133
   177
huffman@39974
   178
lemma ch2ch_principal [simp]:
huffman@39974
   179
  "\<forall>i. Y i \<preceq> Y (Suc i) \<Longrightarrow> chain (\<lambda>i. principal (Y i))"
huffman@39974
   180
by (simp add: chainI principal_mono)
huffman@39974
   181
huffman@39974
   182
subsubsection {* Principal ideals approximate all elements *}
huffman@39974
   183
huffman@39974
   184
lemma compact_principal [simp]: "compact (principal a)"
huffman@40769
   185
by (rule compactI2, simp add: principal_below_iff_mem_rep rep_lub)
huffman@39974
   186
huffman@39974
   187
text {* Construct a chain whose lub is the same as a given ideal *}
huffman@39974
   188
huffman@39974
   189
lemma obtain_principal_chain:
huffman@39974
   190
  obtains Y where "\<forall>i. Y i \<preceq> Y (Suc i)" and "x = (\<Squnion>i. principal (Y i))"
huffman@39974
   191
proof -
huffman@39974
   192
  obtain count :: "'a \<Rightarrow> nat" where inj: "inj count"
huffman@39974
   193
    using countable ..
huffman@39974
   194
  def enum \<equiv> "\<lambda>i. THE a. count a = i"
huffman@39974
   195
  have enum_count [simp]: "\<And>x. enum (count x) = x"
huffman@39974
   196
    unfolding enum_def by (simp add: inj_eq [OF inj])
huffman@39974
   197
  def a \<equiv> "LEAST i. enum i \<in> rep x"
huffman@39974
   198
  def b \<equiv> "\<lambda>i. LEAST j. enum j \<in> rep x \<and> \<not> enum j \<preceq> enum i"
huffman@39974
   199
  def c \<equiv> "\<lambda>i j. LEAST k. enum k \<in> rep x \<and> enum i \<preceq> enum k \<and> enum j \<preceq> enum k"
huffman@39974
   200
  def P \<equiv> "\<lambda>i. \<exists>j. enum j \<in> rep x \<and> \<not> enum j \<preceq> enum i"
huffman@39974
   201
  def X \<equiv> "nat_rec a (\<lambda>n i. if P i then c i (b i) else i)"
huffman@39974
   202
  have X_0: "X 0 = a" unfolding X_def by simp
huffman@39974
   203
  have X_Suc: "\<And>n. X (Suc n) = (if P (X n) then c (X n) (b (X n)) else X n)"
huffman@39974
   204
    unfolding X_def by simp
huffman@39974
   205
  have a_mem: "enum a \<in> rep x"
huffman@39974
   206
    unfolding a_def
huffman@39974
   207
    apply (rule LeastI_ex)
huffman@39974
   208
    apply (cut_tac ideal_rep [of x])
huffman@39974
   209
    apply (drule idealD1)
huffman@39974
   210
    apply (clarify, rename_tac a)
huffman@39974
   211
    apply (rule_tac x="count a" in exI, simp)
huffman@39974
   212
    done
huffman@39974
   213
  have b: "\<And>i. P i \<Longrightarrow> enum i \<in> rep x
huffman@39974
   214
    \<Longrightarrow> enum (b i) \<in> rep x \<and> \<not> enum (b i) \<preceq> enum i"
huffman@39974
   215
    unfolding P_def b_def by (erule LeastI2_ex, simp)
huffman@39974
   216
  have c: "\<And>i j. enum i \<in> rep x \<Longrightarrow> enum j \<in> rep x
huffman@39974
   217
    \<Longrightarrow> enum (c i j) \<in> rep x \<and> enum i \<preceq> enum (c i j) \<and> enum j \<preceq> enum (c i j)"
huffman@39974
   218
    unfolding c_def
huffman@39974
   219
    apply (drule (1) idealD2 [OF ideal_rep], clarify)
huffman@39974
   220
    apply (rule_tac a="count z" in LeastI2, simp, simp)
huffman@39974
   221
    done
huffman@39974
   222
  have X_mem: "\<And>n. enum (X n) \<in> rep x"
huffman@39974
   223
    apply (induct_tac n)
huffman@39974
   224
    apply (simp add: X_0 a_mem)
huffman@39974
   225
    apply (clarsimp simp add: X_Suc, rename_tac n)
huffman@39974
   226
    apply (simp add: b c)
huffman@39974
   227
    done
huffman@39974
   228
  have X_chain: "\<And>n. enum (X n) \<preceq> enum (X (Suc n))"
huffman@39974
   229
    apply (clarsimp simp add: X_Suc r_refl)
huffman@39974
   230
    apply (simp add: b c X_mem)
huffman@39974
   231
    done
huffman@39974
   232
  have less_b: "\<And>n i. n < b i \<Longrightarrow> enum n \<in> rep x \<Longrightarrow> enum n \<preceq> enum i"
huffman@39974
   233
    unfolding b_def by (drule not_less_Least, simp)
huffman@39974
   234
  have X_covers: "\<And>n. \<forall>k\<le>n. enum k \<in> rep x \<longrightarrow> enum k \<preceq> enum (X n)"
huffman@39974
   235
    apply (induct_tac n)
huffman@39974
   236
    apply (clarsimp simp add: X_0 a_def)
huffman@39974
   237
    apply (drule_tac k=0 in Least_le, simp add: r_refl)
huffman@39974
   238
    apply (clarsimp, rename_tac n k)
huffman@39974
   239
    apply (erule le_SucE)
huffman@39974
   240
    apply (rule r_trans [OF _ X_chain], simp)
huffman@39974
   241
    apply (case_tac "P (X n)", simp add: X_Suc)
huffman@39974
   242
    apply (rule_tac x="b (X n)" and y="Suc n" in linorder_cases)
huffman@39974
   243
    apply (simp only: less_Suc_eq_le)
huffman@39974
   244
    apply (drule spec, drule (1) mp, simp add: b X_mem)
huffman@39974
   245
    apply (simp add: c X_mem)
huffman@39974
   246
    apply (drule (1) less_b)
huffman@39974
   247
    apply (erule r_trans)
huffman@39974
   248
    apply (simp add: b c X_mem)
huffman@39974
   249
    apply (simp add: X_Suc)
huffman@39974
   250
    apply (simp add: P_def)
huffman@39974
   251
    done
huffman@39974
   252
  have 1: "\<forall>i. enum (X i) \<preceq> enum (X (Suc i))"
huffman@39974
   253
    by (simp add: X_chain)
huffman@39974
   254
  have 2: "x = (\<Squnion>n. principal (enum (X n)))"
huffman@40769
   255
    apply (simp add: eq_iff rep_lub 1 rep_principal)
huffman@39974
   256
    apply (auto, rename_tac a)
huffman@39974
   257
    apply (subgoal_tac "\<exists>i. a = enum i", erule exE)
huffman@39974
   258
    apply (rule_tac x=i in exI, simp add: X_covers)
huffman@39974
   259
    apply (rule_tac x="count a" in exI, simp)
huffman@39974
   260
    apply (erule idealD3 [OF ideal_rep])
huffman@39974
   261
    apply (rule X_mem)
huffman@39974
   262
    done
huffman@39974
   263
  from 1 2 show ?thesis ..
huffman@39974
   264
qed
huffman@39974
   265
huffman@39974
   266
lemma principal_induct:
huffman@39974
   267
  assumes adm: "adm P"
huffman@39974
   268
  assumes P: "\<And>a. P (principal a)"
huffman@39974
   269
  shows "P x"
huffman@39974
   270
apply (rule obtain_principal_chain [of x])
huffman@39974
   271
apply (simp add: admD [OF adm] P)
huffman@39974
   272
done
huffman@39974
   273
huffman@39974
   274
lemma compact_imp_principal: "compact x \<Longrightarrow> \<exists>a. x = principal a"
huffman@39974
   275
apply (rule obtain_principal_chain [of x])
huffman@39974
   276
apply (drule adm_compact_neq [OF _ cont_id])
huffman@39974
   277
apply (subgoal_tac "chain (\<lambda>i. principal (Y i))")
huffman@39974
   278
apply (drule (2) admD2, fast, simp)
huffman@39974
   279
done
huffman@39974
   280
huffman@28133
   281
subsection {* Defining functions in terms of basis elements *}
huffman@28133
   282
huffman@28133
   283
definition
huffman@41394
   284
  extension :: "('a::type \<Rightarrow> 'c::cpo) \<Rightarrow> 'b \<rightarrow> 'c" where
huffman@41394
   285
  "extension = (\<lambda>f. (\<Lambda> x. lub (f ` rep x)))"
huffman@28133
   286
huffman@41394
   287
lemma extension_lemma:
huffman@27404
   288
  fixes f :: "'a::type \<Rightarrow> 'c::cpo"
huffman@27404
   289
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@27404
   290
  shows "\<exists>u. f ` rep x <<| u"
huffman@39974
   291
proof -
huffman@39974
   292
  obtain Y where Y: "\<forall>i. Y i \<preceq> Y (Suc i)"
huffman@39974
   293
  and x: "x = (\<Squnion>i. principal (Y i))"
huffman@39974
   294
    by (rule obtain_principal_chain [of x])
huffman@39974
   295
  have chain: "chain (\<lambda>i. f (Y i))"
huffman@39974
   296
    by (rule chainI, simp add: f_mono Y)
huffman@39974
   297
  have rep_x: "rep x = (\<Union>n. {a. a \<preceq> Y n})"
huffman@40769
   298
    by (simp add: x rep_lub Y rep_principal)
huffman@39974
   299
  have "f ` rep x <<| (\<Squnion>n. f (Y n))"
huffman@39974
   300
    apply (rule is_lubI)
huffman@39974
   301
    apply (rule ub_imageI, rename_tac a)
huffman@39974
   302
    apply (clarsimp simp add: rep_x)
huffman@39974
   303
    apply (drule f_mono)
huffman@40500
   304
    apply (erule below_lub [OF chain])
huffman@40500
   305
    apply (rule lub_below [OF chain])
huffman@40500
   306
    apply (drule_tac x="Y n" in ub_imageD)
huffman@39974
   307
    apply (simp add: rep_x, fast intro: r_refl)
huffman@39974
   308
    apply assumption
huffman@39974
   309
    done
huffman@39974
   310
  thus ?thesis ..
huffman@39974
   311
qed
huffman@27404
   312
huffman@41394
   313
lemma extension_beta:
huffman@27404
   314
  fixes f :: "'a::type \<Rightarrow> 'c::cpo"
huffman@27404
   315
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@41394
   316
  shows "extension f\<cdot>x = lub (f ` rep x)"
huffman@41394
   317
unfolding extension_def
huffman@27404
   318
proof (rule beta_cfun)
huffman@27404
   319
  have lub: "\<And>x. \<exists>u. f ` rep x <<| u"
huffman@41394
   320
    using f_mono by (rule extension_lemma)
huffman@27404
   321
  show cont: "cont (\<lambda>x. lub (f ` rep x))"
huffman@27404
   322
    apply (rule contI2)
huffman@27404
   323
     apply (rule monofunI)
huffman@39974
   324
     apply (rule is_lub_thelub_ex [OF lub ub_imageI])
huffman@39974
   325
     apply (rule is_ub_thelub_ex [OF lub imageI])
huffman@27404
   326
     apply (erule (1) subsetD [OF rep_mono])
huffman@39974
   327
    apply (rule is_lub_thelub_ex [OF lub ub_imageI])
huffman@40769
   328
    apply (simp add: rep_lub, clarify)
huffman@31076
   329
    apply (erule rev_below_trans [OF is_ub_thelub])
huffman@39974
   330
    apply (erule is_ub_thelub_ex [OF lub imageI])
huffman@27404
   331
    done
huffman@27404
   332
qed
huffman@27404
   333
huffman@41394
   334
lemma extension_principal:
huffman@27404
   335
  fixes f :: "'a::type \<Rightarrow> 'c::cpo"
huffman@27404
   336
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@41394
   337
  shows "extension f\<cdot>(principal a) = f a"
huffman@41394
   338
apply (subst extension_beta, erule f_mono)
huffman@27404
   339
apply (subst rep_principal)
huffman@41033
   340
apply (rule lub_eqI)
huffman@41033
   341
apply (rule is_lub_maximal)
huffman@41033
   342
apply (rule ub_imageI)
huffman@41033
   343
apply (simp add: f_mono)
huffman@41033
   344
apply (rule imageI)
huffman@41033
   345
apply (simp add: r_refl)
huffman@27404
   346
done
huffman@27404
   347
huffman@41394
   348
lemma extension_mono:
huffman@27404
   349
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@27404
   350
  assumes g_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> g a \<sqsubseteq> g b"
huffman@31076
   351
  assumes below: "\<And>a. f a \<sqsubseteq> g a"
huffman@41394
   352
  shows "extension f \<sqsubseteq> extension g"
huffman@40002
   353
 apply (rule cfun_belowI)
huffman@41394
   354
 apply (simp only: extension_beta f_mono g_mono)
huffman@39974
   355
 apply (rule is_lub_thelub_ex)
huffman@41394
   356
  apply (rule extension_lemma, erule f_mono)
huffman@27404
   357
 apply (rule ub_imageI, rename_tac a)
huffman@31076
   358
 apply (rule below_trans [OF below])
huffman@39974
   359
 apply (rule is_ub_thelub_ex)
huffman@41394
   360
  apply (rule extension_lemma, erule g_mono)
huffman@27404
   361
 apply (erule imageI)
huffman@27404
   362
done
huffman@27404
   363
huffman@41394
   364
lemma cont_extension:
huffman@41182
   365
  assumes f_mono: "\<And>a b x. a \<preceq> b \<Longrightarrow> f x a \<sqsubseteq> f x b"
huffman@41182
   366
  assumes f_cont: "\<And>a. cont (\<lambda>x. f x a)"
huffman@41394
   367
  shows "cont (\<lambda>x. extension (\<lambda>a. f x a))"
huffman@41182
   368
 apply (rule contI2)
huffman@41182
   369
  apply (rule monofunI)
huffman@41394
   370
  apply (rule extension_mono, erule f_mono, erule f_mono)
huffman@41182
   371
  apply (erule cont2monofunE [OF f_cont])
huffman@41182
   372
 apply (rule cfun_belowI)
huffman@41182
   373
 apply (rule principal_induct, simp)
huffman@41182
   374
 apply (simp only: contlub_cfun_fun)
huffman@41394
   375
 apply (simp only: extension_principal f_mono)
huffman@41182
   376
 apply (simp add: cont2contlubE [OF f_cont])
huffman@41182
   377
done
huffman@41182
   378
huffman@27404
   379
end
huffman@27404
   380
huffman@39984
   381
lemma (in preorder) typedef_ideal_completion:
huffman@39984
   382
  fixes Abs :: "'a set \<Rightarrow> 'b::cpo"
huffman@39984
   383
  assumes type: "type_definition Rep Abs {S. ideal S}"
huffman@39984
   384
  assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
huffman@39984
   385
  assumes principal: "\<And>a. principal a = Abs {b. b \<preceq> a}"
huffman@39984
   386
  assumes countable: "\<exists>f::'a \<Rightarrow> nat. inj f"
huffman@39984
   387
  shows "ideal_completion r principal Rep"
huffman@39984
   388
proof
huffman@39984
   389
  interpret type_definition Rep Abs "{S. ideal S}" by fact
huffman@39984
   390
  fix a b :: 'a and x y :: 'b and Y :: "nat \<Rightarrow> 'b"
huffman@39984
   391
  show "ideal (Rep x)"
huffman@39984
   392
    using Rep [of x] by simp
huffman@39984
   393
  show "chain Y \<Longrightarrow> Rep (\<Squnion>i. Y i) = (\<Union>i. Rep (Y i))"
huffman@40769
   394
    using type below by (rule typedef_ideal_rep_lub)
huffman@39984
   395
  show "Rep (principal a) = {b. b \<preceq> a}"
huffman@39984
   396
    by (simp add: principal Abs_inverse ideal_principal)
huffman@39984
   397
  show "Rep x \<subseteq> Rep y \<Longrightarrow> x \<sqsubseteq> y"
huffman@39984
   398
    by (simp only: below)
huffman@39984
   399
  show "\<exists>f::'a \<Rightarrow> nat. inj f"
huffman@39984
   400
    by (rule countable)
huffman@39984
   401
qed
huffman@39984
   402
huffman@27404
   403
end