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