src/HOLCF/Completion.thy
author huffman
Mon Oct 11 21:35:31 2010 -0700 (2010-10-11)
changeset 40002 c5b5f7a3a3b1
parent 39984 0300d5170622
child 40500 ee9c8d36318e
permissions -rw-r--r--
new theorem names: fun_below_iff, fun_belowI, cfun_eq_iff, cfun_eqI, cfun_below_iff, cfun_belowI
huffman@27404
     1
(*  Title:      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@39967
     8
imports Cfun
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"
huffman@27404
    29
unfolding ideal_def using prems 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@27404
    52
lemma ex_ideal: "\<exists>A. ideal A"
huffman@27404
    53
by (rule exI, rule ideal_principal)
huffman@27404
    54
huffman@27404
    55
lemma lub_image_principal:
huffman@27404
    56
  assumes f: "\<And>x y. x \<preceq> y \<Longrightarrow> f x \<sqsubseteq> f y"
huffman@27404
    57
  shows "(\<Squnion>x\<in>{x. x \<preceq> y}. f x) = f y"
huffman@27404
    58
apply (rule thelubI)
huffman@27404
    59
apply (rule is_lub_maximal)
huffman@27404
    60
apply (rule ub_imageI)
huffman@27404
    61
apply (simp add: f)
huffman@27404
    62
apply (rule imageI)
huffman@27404
    63
apply (simp add: r_refl)
huffman@27404
    64
done
huffman@27404
    65
huffman@27404
    66
text {* The set of ideals is a cpo *}
huffman@27404
    67
huffman@27404
    68
lemma ideal_UN:
huffman@27404
    69
  fixes A :: "nat \<Rightarrow> 'a set"
huffman@27404
    70
  assumes ideal_A: "\<And>i. ideal (A i)"
huffman@27404
    71
  assumes chain_A: "\<And>i j. i \<le> j \<Longrightarrow> A i \<subseteq> A j"
huffman@27404
    72
  shows "ideal (\<Union>i. A i)"
huffman@27404
    73
 apply (rule idealI)
huffman@27404
    74
   apply (cut_tac idealD1 [OF ideal_A], fast)
huffman@27404
    75
  apply (clarify, rename_tac i j)
huffman@27404
    76
  apply (drule subsetD [OF chain_A [OF le_maxI1]])
huffman@27404
    77
  apply (drule subsetD [OF chain_A [OF le_maxI2]])
huffman@27404
    78
  apply (drule (1) idealD2 [OF ideal_A])
huffman@27404
    79
  apply blast
huffman@27404
    80
 apply clarify
huffman@27404
    81
 apply (drule (1) idealD3 [OF ideal_A])
huffman@27404
    82
 apply fast
huffman@27404
    83
done
huffman@27404
    84
huffman@27404
    85
lemma typedef_ideal_po:
huffman@31076
    86
  fixes Abs :: "'a set \<Rightarrow> 'b::below"
huffman@27404
    87
  assumes type: "type_definition Rep Abs {S. ideal S}"
huffman@31076
    88
  assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
huffman@27404
    89
  shows "OFCLASS('b, po_class)"
huffman@31076
    90
 apply (intro_classes, unfold below)
huffman@27404
    91
   apply (rule subset_refl)
huffman@27404
    92
  apply (erule (1) subset_trans)
huffman@27404
    93
 apply (rule type_definition.Rep_inject [OF type, THEN iffD1])
huffman@27404
    94
 apply (erule (1) subset_antisym)
huffman@27404
    95
done
huffman@27404
    96
huffman@27404
    97
lemma
huffman@27404
    98
  fixes Abs :: "'a set \<Rightarrow> 'b::po"
huffman@27404
    99
  assumes type: "type_definition Rep Abs {S. ideal S}"
huffman@31076
   100
  assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
huffman@27404
   101
  assumes S: "chain S"
huffman@27404
   102
  shows typedef_ideal_lub: "range S <<| Abs (\<Union>i. Rep (S i))"
huffman@27404
   103
    and typedef_ideal_rep_contlub: "Rep (\<Squnion>i. S i) = (\<Union>i. Rep (S i))"
huffman@27404
   104
proof -
huffman@27404
   105
  have 1: "ideal (\<Union>i. Rep (S i))"
huffman@27404
   106
    apply (rule ideal_UN)
huffman@27404
   107
     apply (rule type_definition.Rep [OF type, unfolded mem_Collect_eq])
huffman@31076
   108
    apply (subst below [symmetric])
huffman@27404
   109
    apply (erule chain_mono [OF S])
huffman@27404
   110
    done
huffman@27404
   111
  hence 2: "Rep (Abs (\<Union>i. Rep (S i))) = (\<Union>i. Rep (S i))"
huffman@27404
   112
    by (simp add: type_definition.Abs_inverse [OF type])
huffman@27404
   113
  show 3: "range S <<| Abs (\<Union>i. Rep (S i))"
huffman@27404
   114
    apply (rule is_lubI)
huffman@27404
   115
     apply (rule is_ubI)
huffman@31076
   116
     apply (simp add: below 2, fast)
huffman@31076
   117
    apply (simp add: below 2 is_ub_def, fast)
huffman@27404
   118
    done
huffman@27404
   119
  hence 4: "(\<Squnion>i. S i) = Abs (\<Union>i. Rep (S i))"
huffman@27404
   120
    by (rule thelubI)
huffman@27404
   121
  show 5: "Rep (\<Squnion>i. S i) = (\<Union>i. Rep (S i))"
huffman@27404
   122
    by (simp add: 4 2)
huffman@27404
   123
qed
huffman@27404
   124
huffman@27404
   125
lemma typedef_ideal_cpo:
huffman@27404
   126
  fixes Abs :: "'a set \<Rightarrow> 'b::po"
huffman@27404
   127
  assumes type: "type_definition Rep Abs {S. ideal S}"
huffman@31076
   128
  assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
huffman@27404
   129
  shows "OFCLASS('b, cpo_class)"
huffman@31076
   130
by (default, rule exI, erule typedef_ideal_lub [OF type below])
huffman@27404
   131
huffman@27404
   132
end
huffman@27404
   133
huffman@31076
   134
interpretation below: preorder "below :: 'a::po \<Rightarrow> 'a \<Rightarrow> bool"
huffman@27404
   135
apply unfold_locales
huffman@31076
   136
apply (rule below_refl)
huffman@31076
   137
apply (erule (1) below_trans)
huffman@27404
   138
done
huffman@27404
   139
huffman@28133
   140
subsection {* Lemmas about least upper bounds *}
huffman@27404
   141
huffman@39974
   142
lemma is_ub_thelub_ex: "\<lbrakk>\<exists>u. S <<| u; x \<in> S\<rbrakk> \<Longrightarrow> x \<sqsubseteq> lub S"
huffman@27404
   143
apply (erule exE, drule lubI)
huffman@27404
   144
apply (drule is_lubD1)
huffman@27404
   145
apply (erule (1) is_ubD)
huffman@27404
   146
done
huffman@27404
   147
huffman@39974
   148
lemma is_lub_thelub_ex: "\<lbrakk>\<exists>u. S <<| u; S <| x\<rbrakk> \<Longrightarrow> lub S \<sqsubseteq> x"
huffman@27404
   149
by (erule exE, drule lubI, erule is_lub_lub)
huffman@27404
   150
huffman@28133
   151
subsection {* Locale for ideal completion *}
huffman@28133
   152
huffman@39974
   153
locale ideal_completion = preorder +
huffman@27404
   154
  fixes principal :: "'a::type \<Rightarrow> 'b::cpo"
huffman@27404
   155
  fixes rep :: "'b::cpo \<Rightarrow> 'a::type set"
huffman@39974
   156
  assumes ideal_rep: "\<And>x. ideal (rep x)"
huffman@27404
   157
  assumes rep_contlub: "\<And>Y. chain Y \<Longrightarrow> rep (\<Squnion>i. Y i) = (\<Union>i. rep (Y i))"
huffman@27404
   158
  assumes rep_principal: "\<And>a. rep (principal a) = {b. b \<preceq> a}"
huffman@27404
   159
  assumes subset_repD: "\<And>x y. rep x \<subseteq> rep y \<Longrightarrow> x \<sqsubseteq> y"
huffman@39974
   160
  assumes countable: "\<exists>f::'a \<Rightarrow> nat. inj f"
huffman@27404
   161
begin
huffman@27404
   162
huffman@28133
   163
lemma rep_mono: "x \<sqsubseteq> y \<Longrightarrow> rep x \<subseteq> rep y"
huffman@28133
   164
apply (frule bin_chain)
huffman@28133
   165
apply (drule rep_contlub)
huffman@28133
   166
apply (simp only: thelubI [OF lub_bin_chain])
huffman@28133
   167
apply (rule subsetI, rule UN_I [where a=0], simp_all)
huffman@28133
   168
done
huffman@28133
   169
huffman@31076
   170
lemma below_def: "x \<sqsubseteq> y \<longleftrightarrow> rep x \<subseteq> rep y"
huffman@28133
   171
by (rule iffI [OF rep_mono subset_repD])
huffman@28133
   172
huffman@28133
   173
lemma rep_eq: "rep x = {a. principal a \<sqsubseteq> x}"
huffman@31076
   174
unfolding below_def rep_principal
huffman@28133
   175
apply safe
huffman@28133
   176
apply (erule (1) idealD3 [OF ideal_rep])
huffman@28133
   177
apply (erule subsetD, simp add: r_refl)
huffman@28133
   178
done
huffman@28133
   179
huffman@31076
   180
lemma mem_rep_iff_principal_below: "a \<in> rep x \<longleftrightarrow> principal a \<sqsubseteq> x"
huffman@28133
   181
by (simp add: rep_eq)
huffman@28133
   182
huffman@31076
   183
lemma principal_below_iff_mem_rep: "principal a \<sqsubseteq> x \<longleftrightarrow> a \<in> rep x"
huffman@28133
   184
by (simp add: rep_eq)
huffman@28133
   185
huffman@31076
   186
lemma principal_below_iff [simp]: "principal a \<sqsubseteq> principal b \<longleftrightarrow> a \<preceq> b"
huffman@31076
   187
by (simp add: principal_below_iff_mem_rep rep_principal)
huffman@28133
   188
huffman@28133
   189
lemma principal_eq_iff: "principal a = principal b \<longleftrightarrow> a \<preceq> b \<and> b \<preceq> a"
huffman@31076
   190
unfolding po_eq_conv [where 'a='b] principal_below_iff ..
huffman@28133
   191
huffman@39974
   192
lemma eq_iff: "x = y \<longleftrightarrow> rep x = rep y"
huffman@39974
   193
unfolding po_eq_conv below_def by auto
huffman@39974
   194
huffman@28133
   195
lemma repD: "a \<in> rep x \<Longrightarrow> principal a \<sqsubseteq> x"
huffman@28133
   196
by (simp add: rep_eq)
huffman@28133
   197
huffman@28133
   198
lemma principal_mono: "a \<preceq> b \<Longrightarrow> principal a \<sqsubseteq> principal b"
huffman@31076
   199
by (simp only: principal_below_iff)
huffman@28133
   200
huffman@39974
   201
lemma ch2ch_principal [simp]:
huffman@39974
   202
  "\<forall>i. Y i \<preceq> Y (Suc i) \<Longrightarrow> chain (\<lambda>i. principal (Y i))"
huffman@39974
   203
by (simp add: chainI principal_mono)
huffman@39974
   204
huffman@28133
   205
lemma lub_principal_rep: "principal ` rep x <<| x"
huffman@28133
   206
apply (rule is_lubI)
huffman@28133
   207
apply (rule ub_imageI)
huffman@28133
   208
apply (erule repD)
huffman@31076
   209
apply (subst below_def)
huffman@28133
   210
apply (rule subsetI)
huffman@28133
   211
apply (drule (1) ub_imageD)
huffman@28133
   212
apply (simp add: rep_eq)
huffman@28133
   213
done
huffman@28133
   214
huffman@39974
   215
subsubsection {* Principal ideals approximate all elements *}
huffman@39974
   216
huffman@39974
   217
lemma compact_principal [simp]: "compact (principal a)"
huffman@39974
   218
by (rule compactI2, simp add: principal_below_iff_mem_rep rep_contlub)
huffman@39974
   219
huffman@39974
   220
text {* Construct a chain whose lub is the same as a given ideal *}
huffman@39974
   221
huffman@39974
   222
lemma obtain_principal_chain:
huffman@39974
   223
  obtains Y where "\<forall>i. Y i \<preceq> Y (Suc i)" and "x = (\<Squnion>i. principal (Y i))"
huffman@39974
   224
proof -
huffman@39974
   225
  obtain count :: "'a \<Rightarrow> nat" where inj: "inj count"
huffman@39974
   226
    using countable ..
huffman@39974
   227
  def enum \<equiv> "\<lambda>i. THE a. count a = i"
huffman@39974
   228
  have enum_count [simp]: "\<And>x. enum (count x) = x"
huffman@39974
   229
    unfolding enum_def by (simp add: inj_eq [OF inj])
huffman@39974
   230
  def a \<equiv> "LEAST i. enum i \<in> rep x"
huffman@39974
   231
  def b \<equiv> "\<lambda>i. LEAST j. enum j \<in> rep x \<and> \<not> enum j \<preceq> enum i"
huffman@39974
   232
  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
   233
  def P \<equiv> "\<lambda>i. \<exists>j. enum j \<in> rep x \<and> \<not> enum j \<preceq> enum i"
huffman@39974
   234
  def X \<equiv> "nat_rec a (\<lambda>n i. if P i then c i (b i) else i)"
huffman@39974
   235
  have X_0: "X 0 = a" unfolding X_def by simp
huffman@39974
   236
  have X_Suc: "\<And>n. X (Suc n) = (if P (X n) then c (X n) (b (X n)) else X n)"
huffman@39974
   237
    unfolding X_def by simp
huffman@39974
   238
  have a_mem: "enum a \<in> rep x"
huffman@39974
   239
    unfolding a_def
huffman@39974
   240
    apply (rule LeastI_ex)
huffman@39974
   241
    apply (cut_tac ideal_rep [of x])
huffman@39974
   242
    apply (drule idealD1)
huffman@39974
   243
    apply (clarify, rename_tac a)
huffman@39974
   244
    apply (rule_tac x="count a" in exI, simp)
huffman@39974
   245
    done
huffman@39974
   246
  have b: "\<And>i. P i \<Longrightarrow> enum i \<in> rep x
huffman@39974
   247
    \<Longrightarrow> enum (b i) \<in> rep x \<and> \<not> enum (b i) \<preceq> enum i"
huffman@39974
   248
    unfolding P_def b_def by (erule LeastI2_ex, simp)
huffman@39974
   249
  have c: "\<And>i j. enum i \<in> rep x \<Longrightarrow> enum j \<in> rep x
huffman@39974
   250
    \<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
   251
    unfolding c_def
huffman@39974
   252
    apply (drule (1) idealD2 [OF ideal_rep], clarify)
huffman@39974
   253
    apply (rule_tac a="count z" in LeastI2, simp, simp)
huffman@39974
   254
    done
huffman@39974
   255
  have X_mem: "\<And>n. enum (X n) \<in> rep x"
huffman@39974
   256
    apply (induct_tac n)
huffman@39974
   257
    apply (simp add: X_0 a_mem)
huffman@39974
   258
    apply (clarsimp simp add: X_Suc, rename_tac n)
huffman@39974
   259
    apply (simp add: b c)
huffman@39974
   260
    done
huffman@39974
   261
  have X_chain: "\<And>n. enum (X n) \<preceq> enum (X (Suc n))"
huffman@39974
   262
    apply (clarsimp simp add: X_Suc r_refl)
huffman@39974
   263
    apply (simp add: b c X_mem)
huffman@39974
   264
    done
huffman@39974
   265
  have less_b: "\<And>n i. n < b i \<Longrightarrow> enum n \<in> rep x \<Longrightarrow> enum n \<preceq> enum i"
huffman@39974
   266
    unfolding b_def by (drule not_less_Least, simp)
huffman@39974
   267
  have X_covers: "\<And>n. \<forall>k\<le>n. enum k \<in> rep x \<longrightarrow> enum k \<preceq> enum (X n)"
huffman@39974
   268
    apply (induct_tac n)
huffman@39974
   269
    apply (clarsimp simp add: X_0 a_def)
huffman@39974
   270
    apply (drule_tac k=0 in Least_le, simp add: r_refl)
huffman@39974
   271
    apply (clarsimp, rename_tac n k)
huffman@39974
   272
    apply (erule le_SucE)
huffman@39974
   273
    apply (rule r_trans [OF _ X_chain], simp)
huffman@39974
   274
    apply (case_tac "P (X n)", simp add: X_Suc)
huffman@39974
   275
    apply (rule_tac x="b (X n)" and y="Suc n" in linorder_cases)
huffman@39974
   276
    apply (simp only: less_Suc_eq_le)
huffman@39974
   277
    apply (drule spec, drule (1) mp, simp add: b X_mem)
huffman@39974
   278
    apply (simp add: c X_mem)
huffman@39974
   279
    apply (drule (1) less_b)
huffman@39974
   280
    apply (erule r_trans)
huffman@39974
   281
    apply (simp add: b c X_mem)
huffman@39974
   282
    apply (simp add: X_Suc)
huffman@39974
   283
    apply (simp add: P_def)
huffman@39974
   284
    done
huffman@39974
   285
  have 1: "\<forall>i. enum (X i) \<preceq> enum (X (Suc i))"
huffman@39974
   286
    by (simp add: X_chain)
huffman@39974
   287
  have 2: "x = (\<Squnion>n. principal (enum (X n)))"
huffman@39974
   288
    apply (simp add: eq_iff rep_contlub 1 rep_principal)
huffman@39974
   289
    apply (auto, rename_tac a)
huffman@39974
   290
    apply (subgoal_tac "\<exists>i. a = enum i", erule exE)
huffman@39974
   291
    apply (rule_tac x=i in exI, simp add: X_covers)
huffman@39974
   292
    apply (rule_tac x="count a" in exI, simp)
huffman@39974
   293
    apply (erule idealD3 [OF ideal_rep])
huffman@39974
   294
    apply (rule X_mem)
huffman@39974
   295
    done
huffman@39974
   296
  from 1 2 show ?thesis ..
huffman@39974
   297
qed
huffman@39974
   298
huffman@39974
   299
lemma principal_induct:
huffman@39974
   300
  assumes adm: "adm P"
huffman@39974
   301
  assumes P: "\<And>a. P (principal a)"
huffman@39974
   302
  shows "P x"
huffman@39974
   303
apply (rule obtain_principal_chain [of x])
huffman@39974
   304
apply (simp add: admD [OF adm] P)
huffman@39974
   305
done
huffman@39974
   306
huffman@39974
   307
lemma principal_induct2:
huffman@39974
   308
  "\<lbrakk>\<And>y. adm (\<lambda>x. P x y); \<And>x. adm (\<lambda>y. P x y);
huffman@39974
   309
    \<And>a b. P (principal a) (principal b)\<rbrakk> \<Longrightarrow> P x y"
huffman@39974
   310
apply (rule_tac x=y in spec)
huffman@39974
   311
apply (rule_tac x=x in principal_induct, simp)
huffman@39974
   312
apply (rule allI, rename_tac y)
huffman@39974
   313
apply (rule_tac x=y in principal_induct, simp)
huffman@39974
   314
apply simp
huffman@39974
   315
done
huffman@39974
   316
huffman@39974
   317
lemma compact_imp_principal: "compact x \<Longrightarrow> \<exists>a. x = principal a"
huffman@39974
   318
apply (rule obtain_principal_chain [of x])
huffman@39974
   319
apply (drule adm_compact_neq [OF _ cont_id])
huffman@39974
   320
apply (subgoal_tac "chain (\<lambda>i. principal (Y i))")
huffman@39974
   321
apply (drule (2) admD2, fast, simp)
huffman@39974
   322
done
huffman@39974
   323
huffman@39974
   324
lemma obtain_compact_chain:
huffman@39974
   325
  obtains Y :: "nat \<Rightarrow> 'b"
huffman@39974
   326
  where "chain Y" and "\<forall>i. compact (Y i)" and "x = (\<Squnion>i. Y i)"
huffman@39974
   327
apply (rule obtain_principal_chain [of x])
huffman@39974
   328
apply (rule_tac Y="\<lambda>i. principal (Y i)" in that, simp_all)
huffman@39974
   329
done
huffman@39974
   330
huffman@28133
   331
subsection {* Defining functions in terms of basis elements *}
huffman@28133
   332
huffman@28133
   333
definition
huffman@28133
   334
  basis_fun :: "('a::type \<Rightarrow> 'c::cpo) \<Rightarrow> 'b \<rightarrow> 'c" where
huffman@28133
   335
  "basis_fun = (\<lambda>f. (\<Lambda> x. lub (f ` rep x)))"
huffman@28133
   336
huffman@27404
   337
lemma basis_fun_lemma:
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@27404
   340
  shows "\<exists>u. f ` rep x <<| u"
huffman@39974
   341
proof -
huffman@39974
   342
  obtain Y where Y: "\<forall>i. Y i \<preceq> Y (Suc i)"
huffman@39974
   343
  and x: "x = (\<Squnion>i. principal (Y i))"
huffman@39974
   344
    by (rule obtain_principal_chain [of x])
huffman@39974
   345
  have chain: "chain (\<lambda>i. f (Y i))"
huffman@39974
   346
    by (rule chainI, simp add: f_mono Y)
huffman@39974
   347
  have rep_x: "rep x = (\<Union>n. {a. a \<preceq> Y n})"
huffman@39974
   348
    by (simp add: x rep_contlub Y rep_principal)
huffman@39974
   349
  have "f ` rep x <<| (\<Squnion>n. f (Y n))"
huffman@39974
   350
    apply (rule is_lubI)
huffman@39974
   351
    apply (rule ub_imageI, rename_tac a)
huffman@39974
   352
    apply (clarsimp simp add: rep_x)
huffman@39974
   353
    apply (drule f_mono)
huffman@39974
   354
    apply (erule below_trans)
huffman@39974
   355
    apply (rule is_ub_thelub [OF chain])
huffman@39974
   356
    apply (rule is_lub_thelub [OF chain])
huffman@39974
   357
    apply (rule ub_rangeI)
huffman@39974
   358
    apply (drule_tac x="Y i" in ub_imageD)
huffman@39974
   359
    apply (simp add: rep_x, fast intro: r_refl)
huffman@39974
   360
    apply assumption
huffman@39974
   361
    done
huffman@39974
   362
  thus ?thesis ..
huffman@39974
   363
qed
huffman@27404
   364
huffman@27404
   365
lemma basis_fun_beta:
huffman@27404
   366
  fixes f :: "'a::type \<Rightarrow> 'c::cpo"
huffman@27404
   367
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@27404
   368
  shows "basis_fun f\<cdot>x = lub (f ` rep x)"
huffman@27404
   369
unfolding basis_fun_def
huffman@27404
   370
proof (rule beta_cfun)
huffman@27404
   371
  have lub: "\<And>x. \<exists>u. f ` rep x <<| u"
huffman@27404
   372
    using f_mono by (rule basis_fun_lemma)
huffman@27404
   373
  show cont: "cont (\<lambda>x. lub (f ` rep x))"
huffman@27404
   374
    apply (rule contI2)
huffman@27404
   375
     apply (rule monofunI)
huffman@39974
   376
     apply (rule is_lub_thelub_ex [OF lub ub_imageI])
huffman@39974
   377
     apply (rule is_ub_thelub_ex [OF lub imageI])
huffman@27404
   378
     apply (erule (1) subsetD [OF rep_mono])
huffman@39974
   379
    apply (rule is_lub_thelub_ex [OF lub ub_imageI])
huffman@27404
   380
    apply (simp add: rep_contlub, clarify)
huffman@31076
   381
    apply (erule rev_below_trans [OF is_ub_thelub])
huffman@39974
   382
    apply (erule is_ub_thelub_ex [OF lub imageI])
huffman@27404
   383
    done
huffman@27404
   384
qed
huffman@27404
   385
huffman@27404
   386
lemma basis_fun_principal:
huffman@27404
   387
  fixes f :: "'a::type \<Rightarrow> 'c::cpo"
huffman@27404
   388
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@27404
   389
  shows "basis_fun f\<cdot>(principal a) = f a"
huffman@27404
   390
apply (subst basis_fun_beta, erule f_mono)
huffman@27404
   391
apply (subst rep_principal)
huffman@27404
   392
apply (rule lub_image_principal, erule f_mono)
huffman@27404
   393
done
huffman@27404
   394
huffman@27404
   395
lemma basis_fun_mono:
huffman@27404
   396
  assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
huffman@27404
   397
  assumes g_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> g a \<sqsubseteq> g b"
huffman@31076
   398
  assumes below: "\<And>a. f a \<sqsubseteq> g a"
huffman@27404
   399
  shows "basis_fun f \<sqsubseteq> basis_fun g"
huffman@40002
   400
 apply (rule cfun_belowI)
huffman@27404
   401
 apply (simp only: basis_fun_beta f_mono g_mono)
huffman@39974
   402
 apply (rule is_lub_thelub_ex)
huffman@27404
   403
  apply (rule basis_fun_lemma, erule f_mono)
huffman@27404
   404
 apply (rule ub_imageI, rename_tac a)
huffman@31076
   405
 apply (rule below_trans [OF below])
huffman@39974
   406
 apply (rule is_ub_thelub_ex)
huffman@27404
   407
  apply (rule basis_fun_lemma, erule g_mono)
huffman@27404
   408
 apply (erule imageI)
huffman@27404
   409
done
huffman@27404
   410
huffman@27404
   411
end
huffman@27404
   412
huffman@39984
   413
lemma (in preorder) typedef_ideal_completion:
huffman@39984
   414
  fixes Abs :: "'a set \<Rightarrow> 'b::cpo"
huffman@39984
   415
  assumes type: "type_definition Rep Abs {S. ideal S}"
huffman@39984
   416
  assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
huffman@39984
   417
  assumes principal: "\<And>a. principal a = Abs {b. b \<preceq> a}"
huffman@39984
   418
  assumes countable: "\<exists>f::'a \<Rightarrow> nat. inj f"
huffman@39984
   419
  shows "ideal_completion r principal Rep"
huffman@39984
   420
proof
huffman@39984
   421
  interpret type_definition Rep Abs "{S. ideal S}" by fact
huffman@39984
   422
  fix a b :: 'a and x y :: 'b and Y :: "nat \<Rightarrow> 'b"
huffman@39984
   423
  show "ideal (Rep x)"
huffman@39984
   424
    using Rep [of x] by simp
huffman@39984
   425
  show "chain Y \<Longrightarrow> Rep (\<Squnion>i. Y i) = (\<Union>i. Rep (Y i))"
huffman@39984
   426
    using type below by (rule typedef_ideal_rep_contlub)
huffman@39984
   427
  show "Rep (principal a) = {b. b \<preceq> a}"
huffman@39984
   428
    by (simp add: principal Abs_inverse ideal_principal)
huffman@39984
   429
  show "Rep x \<subseteq> Rep y \<Longrightarrow> x \<sqsubseteq> y"
huffman@39984
   430
    by (simp only: below)
huffman@39984
   431
  show "\<exists>f::'a \<Rightarrow> nat. inj f"
huffman@39984
   432
    by (rule countable)
huffman@39984
   433
qed
huffman@39984
   434
huffman@27404
   435
end