src/HOL/HOLCF/Completion.thy
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (22 months ago)
changeset 66695 91500c024c7f
parent 65380 ae93953746fc
child 68383 93a42bd62ede
permissions -rw-r--r--
tuned;
wenzelm@42151
     1
(*  Title:      HOL/HOLCF/Completion.thy
huffman@27404
     2
    Author:     Brian Huffman
huffman@27404
     3
*)
huffman@27404
     4
wenzelm@62175
     5
section \<open>Defining algebraic domains by ideal completion\<close>
huffman@27404
     6
huffman@27404
     7
theory Completion
wenzelm@65380
     8
imports Cfun
huffman@27404
     9
begin
huffman@27404
    10
wenzelm@62175
    11
subsection \<open>Ideals over a preorder\<close>
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
wenzelm@62175
    55
text \<open>The set of ideals is a cpo\<close>
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)
haftmann@54863
    65
  apply (drule subsetD [OF chain_A [OF max.cobounded1]])
haftmann@54863
    66
  apply (drule subsetD [OF chain_A [OF max.cobounded2]])
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)"
wenzelm@61169
   119
  by standard (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
wenzelm@62175
   129
subsection \<open>Lemmas about least upper bounds\<close>
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
wenzelm@65379
   140
wenzelm@62175
   141
subsection \<open>Locale for ideal completion\<close>
huffman@28133
   142
wenzelm@65379
   143
hide_const (open) Filter.principal
wenzelm@65379
   144
huffman@39974
   145
locale ideal_completion = preorder +
huffman@27404
   146
  fixes principal :: "'a::type \<Rightarrow> 'b::cpo"
huffman@27404
   147
  fixes rep :: "'b::cpo \<Rightarrow> 'a::type set"
huffman@39974
   148
  assumes ideal_rep: "\<And>x. ideal (rep x)"
huffman@40769
   149
  assumes rep_lub: "\<And>Y. chain Y \<Longrightarrow> rep (\<Squnion>i. Y i) = (\<Union>i. rep (Y i))"
huffman@27404
   150
  assumes rep_principal: "\<And>a. rep (principal a) = {b. b \<preceq> a}"
huffman@41033
   151
  assumes belowI: "\<And>x y. rep x \<subseteq> rep y \<Longrightarrow> x \<sqsubseteq> y"
huffman@39974
   152
  assumes countable: "\<exists>f::'a \<Rightarrow> nat. inj f"
huffman@27404
   153
begin
huffman@27404
   154
huffman@28133
   155
lemma rep_mono: "x \<sqsubseteq> y \<Longrightarrow> rep x \<subseteq> rep y"
huffman@28133
   156
apply (frule bin_chain)
huffman@40769
   157
apply (drule rep_lub)
huffman@40771
   158
apply (simp only: lub_eqI [OF is_lub_bin_chain])
huffman@28133
   159
apply (rule subsetI, rule UN_I [where a=0], simp_all)
huffman@28133
   160
done
huffman@28133
   161
huffman@31076
   162
lemma below_def: "x \<sqsubseteq> y \<longleftrightarrow> rep x \<subseteq> rep y"
huffman@41033
   163
by (rule iffI [OF rep_mono belowI])
huffman@28133
   164
huffman@31076
   165
lemma principal_below_iff_mem_rep: "principal a \<sqsubseteq> x \<longleftrightarrow> a \<in> rep x"
huffman@41033
   166
unfolding below_def rep_principal
huffman@41033
   167
by (auto intro: r_refl elim: idealD3 [OF ideal_rep])
huffman@28133
   168
huffman@31076
   169
lemma principal_below_iff [simp]: "principal a \<sqsubseteq> principal b \<longleftrightarrow> a \<preceq> b"
huffman@31076
   170
by (simp add: principal_below_iff_mem_rep rep_principal)
huffman@28133
   171
huffman@28133
   172
lemma principal_eq_iff: "principal a = principal b \<longleftrightarrow> a \<preceq> b \<and> b \<preceq> a"
huffman@31076
   173
unfolding po_eq_conv [where 'a='b] principal_below_iff ..
huffman@28133
   174
huffman@39974
   175
lemma eq_iff: "x = y \<longleftrightarrow> rep x = rep y"
huffman@39974
   176
unfolding po_eq_conv below_def by auto
huffman@39974
   177
huffman@28133
   178
lemma principal_mono: "a \<preceq> b \<Longrightarrow> principal a \<sqsubseteq> principal b"
huffman@31076
   179
by (simp only: principal_below_iff)
huffman@28133
   180
huffman@39974
   181
lemma ch2ch_principal [simp]:
huffman@39974
   182
  "\<forall>i. Y i \<preceq> Y (Suc i) \<Longrightarrow> chain (\<lambda>i. principal (Y i))"
huffman@39974
   183
by (simp add: chainI principal_mono)
huffman@39974
   184
wenzelm@62175
   185
subsubsection \<open>Principal ideals approximate all elements\<close>
huffman@39974
   186
huffman@39974
   187
lemma compact_principal [simp]: "compact (principal a)"
huffman@40769
   188
by (rule compactI2, simp add: principal_below_iff_mem_rep rep_lub)
huffman@39974
   189
wenzelm@62175
   190
text \<open>Construct a chain whose lub is the same as a given ideal\<close>
huffman@39974
   191
huffman@39974
   192
lemma obtain_principal_chain:
huffman@39974
   193
  obtains Y where "\<forall>i. Y i \<preceq> Y (Suc i)" and "x = (\<Squnion>i. principal (Y i))"
huffman@39974
   194
proof -
huffman@39974
   195
  obtain count :: "'a \<Rightarrow> nat" where inj: "inj count"
huffman@39974
   196
    using countable ..
wenzelm@63040
   197
  define enum where "enum i = (THE a. count a = i)" for i
huffman@39974
   198
  have enum_count [simp]: "\<And>x. enum (count x) = x"
huffman@39974
   199
    unfolding enum_def by (simp add: inj_eq [OF inj])
wenzelm@63040
   200
  define a where "a = (LEAST i. enum i \<in> rep x)"
wenzelm@63040
   201
  define b where "b i = (LEAST j. enum j \<in> rep x \<and> \<not> enum j \<preceq> enum i)" for i
wenzelm@63040
   202
  define c where "c i j = (LEAST k. enum k \<in> rep x \<and> enum i \<preceq> enum k \<and> enum j \<preceq> enum k)" for i j
wenzelm@63040
   203
  define P where "P i \<longleftrightarrow> (\<exists>j. enum j \<in> rep x \<and> \<not> enum j \<preceq> enum i)" for i
wenzelm@63040
   204
  define X where "X = rec_nat a (\<lambda>n i. if P i then c i (b i) else i)"
huffman@39974
   205
  have X_0: "X 0 = a" unfolding X_def by simp
huffman@39974
   206
  have X_Suc: "\<And>n. X (Suc n) = (if P (X n) then c (X n) (b (X n)) else X n)"
huffman@39974
   207
    unfolding X_def by simp
huffman@39974
   208
  have a_mem: "enum a \<in> rep x"
huffman@39974
   209
    unfolding a_def
huffman@39974
   210
    apply (rule LeastI_ex)
huffman@39974
   211
    apply (cut_tac ideal_rep [of x])
huffman@39974
   212
    apply (drule idealD1)
huffman@39974
   213
    apply (clarify, rename_tac a)
huffman@39974
   214
    apply (rule_tac x="count a" in exI, simp)
huffman@39974
   215
    done
huffman@39974
   216
  have b: "\<And>i. P i \<Longrightarrow> enum i \<in> rep x
huffman@39974
   217
    \<Longrightarrow> enum (b i) \<in> rep x \<and> \<not> enum (b i) \<preceq> enum i"
huffman@39974
   218
    unfolding P_def b_def by (erule LeastI2_ex, simp)
huffman@39974
   219
  have c: "\<And>i j. enum i \<in> rep x \<Longrightarrow> enum j \<in> rep x
huffman@39974
   220
    \<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
   221
    unfolding c_def
huffman@39974
   222
    apply (drule (1) idealD2 [OF ideal_rep], clarify)
huffman@39974
   223
    apply (rule_tac a="count z" in LeastI2, simp, simp)
huffman@39974
   224
    done
huffman@39974
   225
  have X_mem: "\<And>n. enum (X n) \<in> rep x"
huffman@39974
   226
    apply (induct_tac n)
huffman@39974
   227
    apply (simp add: X_0 a_mem)
huffman@39974
   228
    apply (clarsimp simp add: X_Suc, rename_tac n)
huffman@39974
   229
    apply (simp add: b c)
huffman@39974
   230
    done
huffman@39974
   231
  have X_chain: "\<And>n. enum (X n) \<preceq> enum (X (Suc n))"
huffman@39974
   232
    apply (clarsimp simp add: X_Suc r_refl)
huffman@39974
   233
    apply (simp add: b c X_mem)
huffman@39974
   234
    done
huffman@39974
   235
  have less_b: "\<And>n i. n < b i \<Longrightarrow> enum n \<in> rep x \<Longrightarrow> enum n \<preceq> enum i"
huffman@39974
   236
    unfolding b_def by (drule not_less_Least, simp)
huffman@39974
   237
  have X_covers: "\<And>n. \<forall>k\<le>n. enum k \<in> rep x \<longrightarrow> enum k \<preceq> enum (X n)"
huffman@39974
   238
    apply (induct_tac n)
huffman@39974
   239
    apply (clarsimp simp add: X_0 a_def)
huffman@39974
   240
    apply (drule_tac k=0 in Least_le, simp add: r_refl)
huffman@39974
   241
    apply (clarsimp, rename_tac n k)
huffman@39974
   242
    apply (erule le_SucE)
huffman@39974
   243
    apply (rule r_trans [OF _ X_chain], simp)
huffman@39974
   244
    apply (case_tac "P (X n)", simp add: X_Suc)
huffman@39974
   245
    apply (rule_tac x="b (X n)" and y="Suc n" in linorder_cases)
huffman@39974
   246
    apply (simp only: less_Suc_eq_le)
huffman@39974
   247
    apply (drule spec, drule (1) mp, simp add: b X_mem)
huffman@39974
   248
    apply (simp add: c X_mem)
huffman@39974
   249
    apply (drule (1) less_b)
huffman@39974
   250
    apply (erule r_trans)
huffman@39974
   251
    apply (simp add: b c X_mem)
huffman@39974
   252
    apply (simp add: X_Suc)
huffman@39974
   253
    apply (simp add: P_def)
huffman@39974
   254
    done
huffman@39974
   255
  have 1: "\<forall>i. enum (X i) \<preceq> enum (X (Suc i))"
huffman@39974
   256
    by (simp add: X_chain)
huffman@39974
   257
  have 2: "x = (\<Squnion>n. principal (enum (X n)))"
huffman@40769
   258
    apply (simp add: eq_iff rep_lub 1 rep_principal)
huffman@39974
   259
    apply (auto, rename_tac a)
huffman@39974
   260
    apply (subgoal_tac "\<exists>i. a = enum i", erule exE)
huffman@39974
   261
    apply (rule_tac x=i in exI, simp add: X_covers)
huffman@39974
   262
    apply (rule_tac x="count a" in exI, simp)
huffman@39974
   263
    apply (erule idealD3 [OF ideal_rep])
huffman@39974
   264
    apply (rule X_mem)
huffman@39974
   265
    done
huffman@39974
   266
  from 1 2 show ?thesis ..
huffman@39974
   267
qed
huffman@39974
   268
huffman@39974
   269
lemma principal_induct:
huffman@39974
   270
  assumes adm: "adm P"
huffman@39974
   271
  assumes P: "\<And>a. P (principal a)"
huffman@39974
   272
  shows "P x"
huffman@39974
   273
apply (rule obtain_principal_chain [of x])
huffman@39974
   274
apply (simp add: admD [OF adm] P)
huffman@39974
   275
done
huffman@39974
   276
huffman@39974
   277
lemma compact_imp_principal: "compact x \<Longrightarrow> \<exists>a. x = principal a"
huffman@39974
   278
apply (rule obtain_principal_chain [of x])
huffman@39974
   279
apply (drule adm_compact_neq [OF _ cont_id])
huffman@39974
   280
apply (subgoal_tac "chain (\<lambda>i. principal (Y i))")
huffman@39974
   281
apply (drule (2) admD2, fast, simp)
huffman@39974
   282
done
huffman@39974
   283
wenzelm@62175
   284
subsection \<open>Defining functions in terms of basis elements\<close>
huffman@28133
   285
huffman@28133
   286
definition
huffman@41394
   287
  extension :: "('a::type \<Rightarrow> 'c::cpo) \<Rightarrow> 'b \<rightarrow> 'c" where
huffman@41394
   288
  "extension = (\<lambda>f. (\<Lambda> x. lub (f ` rep x)))"
huffman@28133
   289
huffman@41394
   290
lemma extension_lemma:
huffman@27404
   291
  fixes f :: "'a::type \<Rightarrow> 'c::cpo"
huffman@27404
   292
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@27404
   293
  shows "\<exists>u. f ` rep x <<| u"
huffman@39974
   294
proof -
huffman@39974
   295
  obtain Y where Y: "\<forall>i. Y i \<preceq> Y (Suc i)"
huffman@39974
   296
  and x: "x = (\<Squnion>i. principal (Y i))"
huffman@39974
   297
    by (rule obtain_principal_chain [of x])
huffman@39974
   298
  have chain: "chain (\<lambda>i. f (Y i))"
huffman@39974
   299
    by (rule chainI, simp add: f_mono Y)
huffman@39974
   300
  have rep_x: "rep x = (\<Union>n. {a. a \<preceq> Y n})"
huffman@40769
   301
    by (simp add: x rep_lub Y rep_principal)
huffman@39974
   302
  have "f ` rep x <<| (\<Squnion>n. f (Y n))"
huffman@39974
   303
    apply (rule is_lubI)
huffman@39974
   304
    apply (rule ub_imageI, rename_tac a)
huffman@39974
   305
    apply (clarsimp simp add: rep_x)
huffman@39974
   306
    apply (drule f_mono)
huffman@40500
   307
    apply (erule below_lub [OF chain])
huffman@40500
   308
    apply (rule lub_below [OF chain])
huffman@40500
   309
    apply (drule_tac x="Y n" in ub_imageD)
huffman@39974
   310
    apply (simp add: rep_x, fast intro: r_refl)
huffman@39974
   311
    apply assumption
huffman@39974
   312
    done
huffman@39974
   313
  thus ?thesis ..
huffman@39974
   314
qed
huffman@27404
   315
huffman@41394
   316
lemma extension_beta:
huffman@27404
   317
  fixes f :: "'a::type \<Rightarrow> 'c::cpo"
huffman@27404
   318
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@41394
   319
  shows "extension f\<cdot>x = lub (f ` rep x)"
huffman@41394
   320
unfolding extension_def
huffman@27404
   321
proof (rule beta_cfun)
huffman@27404
   322
  have lub: "\<And>x. \<exists>u. f ` rep x <<| u"
huffman@41394
   323
    using f_mono by (rule extension_lemma)
huffman@27404
   324
  show cont: "cont (\<lambda>x. lub (f ` rep x))"
huffman@27404
   325
    apply (rule contI2)
huffman@27404
   326
     apply (rule monofunI)
huffman@39974
   327
     apply (rule is_lub_thelub_ex [OF lub ub_imageI])
huffman@39974
   328
     apply (rule is_ub_thelub_ex [OF lub imageI])
huffman@27404
   329
     apply (erule (1) subsetD [OF rep_mono])
huffman@39974
   330
    apply (rule is_lub_thelub_ex [OF lub ub_imageI])
huffman@40769
   331
    apply (simp add: rep_lub, clarify)
huffman@31076
   332
    apply (erule rev_below_trans [OF is_ub_thelub])
huffman@39974
   333
    apply (erule is_ub_thelub_ex [OF lub imageI])
huffman@27404
   334
    done
huffman@27404
   335
qed
huffman@27404
   336
huffman@41394
   337
lemma extension_principal:
huffman@27404
   338
  fixes f :: "'a::type \<Rightarrow> 'c::cpo"
huffman@27404
   339
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@41394
   340
  shows "extension f\<cdot>(principal a) = f a"
huffman@41394
   341
apply (subst extension_beta, erule f_mono)
huffman@27404
   342
apply (subst rep_principal)
huffman@41033
   343
apply (rule lub_eqI)
huffman@41033
   344
apply (rule is_lub_maximal)
huffman@41033
   345
apply (rule ub_imageI)
huffman@41033
   346
apply (simp add: f_mono)
huffman@41033
   347
apply (rule imageI)
huffman@41033
   348
apply (simp add: r_refl)
huffman@27404
   349
done
huffman@27404
   350
huffman@41394
   351
lemma extension_mono:
huffman@27404
   352
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@27404
   353
  assumes g_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> g a \<sqsubseteq> g b"
huffman@31076
   354
  assumes below: "\<And>a. f a \<sqsubseteq> g a"
huffman@41394
   355
  shows "extension f \<sqsubseteq> extension g"
huffman@40002
   356
 apply (rule cfun_belowI)
huffman@41394
   357
 apply (simp only: extension_beta f_mono g_mono)
huffman@39974
   358
 apply (rule is_lub_thelub_ex)
huffman@41394
   359
  apply (rule extension_lemma, erule f_mono)
huffman@27404
   360
 apply (rule ub_imageI, rename_tac a)
huffman@31076
   361
 apply (rule below_trans [OF below])
huffman@39974
   362
 apply (rule is_ub_thelub_ex)
huffman@41394
   363
  apply (rule extension_lemma, erule g_mono)
huffman@27404
   364
 apply (erule imageI)
huffman@27404
   365
done
huffman@27404
   366
huffman@41394
   367
lemma cont_extension:
huffman@41182
   368
  assumes f_mono: "\<And>a b x. a \<preceq> b \<Longrightarrow> f x a \<sqsubseteq> f x b"
huffman@41182
   369
  assumes f_cont: "\<And>a. cont (\<lambda>x. f x a)"
huffman@41394
   370
  shows "cont (\<lambda>x. extension (\<lambda>a. f x a))"
huffman@41182
   371
 apply (rule contI2)
huffman@41182
   372
  apply (rule monofunI)
huffman@41394
   373
  apply (rule extension_mono, erule f_mono, erule f_mono)
huffman@41182
   374
  apply (erule cont2monofunE [OF f_cont])
huffman@41182
   375
 apply (rule cfun_belowI)
huffman@41182
   376
 apply (rule principal_induct, simp)
huffman@41182
   377
 apply (simp only: contlub_cfun_fun)
huffman@41394
   378
 apply (simp only: extension_principal f_mono)
huffman@41182
   379
 apply (simp add: cont2contlubE [OF f_cont])
huffman@41182
   380
done
huffman@41182
   381
huffman@27404
   382
end
huffman@27404
   383
huffman@39984
   384
lemma (in preorder) typedef_ideal_completion:
huffman@39984
   385
  fixes Abs :: "'a set \<Rightarrow> 'b::cpo"
huffman@39984
   386
  assumes type: "type_definition Rep Abs {S. ideal S}"
huffman@39984
   387
  assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
huffman@39984
   388
  assumes principal: "\<And>a. principal a = Abs {b. b \<preceq> a}"
huffman@39984
   389
  assumes countable: "\<exists>f::'a \<Rightarrow> nat. inj f"
huffman@39984
   390
  shows "ideal_completion r principal Rep"
huffman@39984
   391
proof
huffman@39984
   392
  interpret type_definition Rep Abs "{S. ideal S}" by fact
huffman@39984
   393
  fix a b :: 'a and x y :: 'b and Y :: "nat \<Rightarrow> 'b"
huffman@39984
   394
  show "ideal (Rep x)"
huffman@39984
   395
    using Rep [of x] by simp
huffman@39984
   396
  show "chain Y \<Longrightarrow> Rep (\<Squnion>i. Y i) = (\<Union>i. Rep (Y i))"
huffman@40769
   397
    using type below by (rule typedef_ideal_rep_lub)
huffman@39984
   398
  show "Rep (principal a) = {b. b \<preceq> a}"
huffman@39984
   399
    by (simp add: principal Abs_inverse ideal_principal)
huffman@39984
   400
  show "Rep x \<subseteq> Rep y \<Longrightarrow> x \<sqsubseteq> y"
huffman@39984
   401
    by (simp only: below)
huffman@39984
   402
  show "\<exists>f::'a \<Rightarrow> nat. inj f"
huffman@39984
   403
    by (rule countable)
huffman@39984
   404
qed
huffman@39984
   405
huffman@27404
   406
end