1(*  Title:      HOL/HOLCF/Universal.thy
2    Author:     Brian Huffman
3*)
4
5section \<open>A universal bifinite domain\<close>
6
7theory Universal
8imports Bifinite Completion "HOL-Library.Nat_Bijection"
9begin
10
11no_notation binomial  (infixl "choose" 65)
12
13subsection \<open>Basis for universal domain\<close>
14
15subsubsection \<open>Basis datatype\<close>
16
17type_synonym ubasis = nat
18
19definition
20  node :: "nat \<Rightarrow> ubasis \<Rightarrow> ubasis set \<Rightarrow> ubasis"
21where
22  "node i a S = Suc (prod_encode (i, prod_encode (a, set_encode S)))"
23
24lemma node_not_0 [simp]: "node i a S \<noteq> 0"
25unfolding node_def by simp
26
27lemma node_gt_0 [simp]: "0 < node i a S"
28unfolding node_def by simp
29
30lemma node_inject [simp]:
31  "\<lbrakk>finite S; finite T\<rbrakk>
32    \<Longrightarrow> node i a S = node j b T \<longleftrightarrow> i = j \<and> a = b \<and> S = T"
33unfolding node_def by (simp add: prod_encode_eq set_encode_eq)
34
35lemma node_gt0: "i < node i a S"
36unfolding node_def less_Suc_eq_le
37by (rule le_prod_encode_1)
38
39lemma node_gt1: "a < node i a S"
40unfolding node_def less_Suc_eq_le
41by (rule order_trans [OF le_prod_encode_1 le_prod_encode_2])
42
43lemma nat_less_power2: "n < 2^n"
44by (induct n) simp_all
45
46lemma node_gt2: "\<lbrakk>finite S; b \<in> S\<rbrakk> \<Longrightarrow> b < node i a S"
47unfolding node_def less_Suc_eq_le set_encode_def
48apply (rule order_trans [OF _ le_prod_encode_2])
49apply (rule order_trans [OF _ le_prod_encode_2])
50apply (rule order_trans [where y="sum ((^) 2) {b}"])
51apply (simp add: nat_less_power2 [THEN order_less_imp_le])
52apply (erule sum_mono2, simp, simp)
53done
54
55lemma eq_prod_encode_pairI:
56  "\<lbrakk>fst (prod_decode x) = a; snd (prod_decode x) = b\<rbrakk> \<Longrightarrow> x = prod_encode (a, b)"
57by (erule subst, erule subst, simp)
58
59lemma node_cases:
60  assumes 1: "x = 0 \<Longrightarrow> P"
61  assumes 2: "\<And>i a S. \<lbrakk>finite S; x = node i a S\<rbrakk> \<Longrightarrow> P"
62  shows "P"
63 apply (cases x)
64  apply (erule 1)
65 apply (rule 2)
66  apply (rule finite_set_decode)
67 apply (simp add: node_def)
68 apply (rule eq_prod_encode_pairI [OF refl])
69 apply (rule eq_prod_encode_pairI [OF refl refl])
70done
71
72lemma node_induct:
73  assumes 1: "P 0"
74  assumes 2: "\<And>i a S. \<lbrakk>P a; finite S; \<forall>b\<in>S. P b\<rbrakk> \<Longrightarrow> P (node i a S)"
75  shows "P x"
76 apply (induct x rule: nat_less_induct)
77 apply (case_tac n rule: node_cases)
78  apply (simp add: 1)
79 apply (simp add: 2 node_gt1 node_gt2)
80done
81
82subsubsection \<open>Basis ordering\<close>
83
84inductive
85  ubasis_le :: "nat \<Rightarrow> nat \<Rightarrow> bool"
86where
87  ubasis_le_refl: "ubasis_le a a"
88| ubasis_le_trans:
89    "\<lbrakk>ubasis_le a b; ubasis_le b c\<rbrakk> \<Longrightarrow> ubasis_le a c"
90| ubasis_le_lower:
91    "finite S \<Longrightarrow> ubasis_le a (node i a S)"
92| ubasis_le_upper:
93    "\<lbrakk>finite S; b \<in> S; ubasis_le a b\<rbrakk> \<Longrightarrow> ubasis_le (node i a S) b"
94
95lemma ubasis_le_minimal: "ubasis_le 0 x"
96apply (induct x rule: node_induct)
97apply (rule ubasis_le_refl)
98apply (erule ubasis_le_trans)
99apply (erule ubasis_le_lower)
100done
101
102interpretation udom: preorder ubasis_le
103apply standard
104apply (rule ubasis_le_refl)
105apply (erule (1) ubasis_le_trans)
106done
107
108subsubsection \<open>Generic take function\<close>
109
110function
111  ubasis_until :: "(ubasis \<Rightarrow> bool) \<Rightarrow> ubasis \<Rightarrow> ubasis"
112where
113  "ubasis_until P 0 = 0"
114| "finite S \<Longrightarrow> ubasis_until P (node i a S) =
115    (if P (node i a S) then node i a S else ubasis_until P a)"
116   apply clarify
117   apply (rule_tac x=b in node_cases)
118    apply simp
119   apply simp
120   apply fast
121  apply simp
122 apply simp
123done
124
125termination ubasis_until
126apply (relation "measure snd")
127apply (rule wf_measure)
128apply (simp add: node_gt1)
129done
130
131lemma ubasis_until: "P 0 \<Longrightarrow> P (ubasis_until P x)"
132by (induct x rule: node_induct) simp_all
133
134lemma ubasis_until': "0 < ubasis_until P x \<Longrightarrow> P (ubasis_until P x)"
135by (induct x rule: node_induct) auto
136
137lemma ubasis_until_same: "P x \<Longrightarrow> ubasis_until P x = x"
138by (induct x rule: node_induct) simp_all
139
140lemma ubasis_until_idem:
141  "P 0 \<Longrightarrow> ubasis_until P (ubasis_until P x) = ubasis_until P x"
142by (rule ubasis_until_same [OF ubasis_until])
143
144lemma ubasis_until_0:
145  "\<forall>x. x \<noteq> 0 \<longrightarrow> \<not> P x \<Longrightarrow> ubasis_until P x = 0"
146by (induct x rule: node_induct) simp_all
147
148lemma ubasis_until_less: "ubasis_le (ubasis_until P x) x"
149apply (induct x rule: node_induct)
150apply (simp add: ubasis_le_refl)
151apply (simp add: ubasis_le_refl)
152apply (rule impI)
153apply (erule ubasis_le_trans)
154apply (erule ubasis_le_lower)
155done
156
157lemma ubasis_until_chain:
158  assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
159  shows "ubasis_le (ubasis_until P x) (ubasis_until Q x)"
160apply (induct x rule: node_induct)
161apply (simp add: ubasis_le_refl)
162apply (simp add: ubasis_le_refl)
163apply (simp add: PQ)
164apply clarify
165apply (rule ubasis_le_trans)
166apply (rule ubasis_until_less)
167apply (erule ubasis_le_lower)
168done
169
170lemma ubasis_until_mono:
171  assumes "\<And>i a S b. \<lbrakk>finite S; P (node i a S); b \<in> S; ubasis_le a b\<rbrakk> \<Longrightarrow> P b"
172  shows "ubasis_le a b \<Longrightarrow> ubasis_le (ubasis_until P a) (ubasis_until P b)"
173proof (induct set: ubasis_le)
174  case (ubasis_le_refl a) show ?case by (rule ubasis_le.ubasis_le_refl)
175next
176  case (ubasis_le_trans a b c) thus ?case by - (rule ubasis_le.ubasis_le_trans)
177next
178  case (ubasis_le_lower S a i) thus ?case
179    apply (clarsimp simp add: ubasis_le_refl)
180    apply (rule ubasis_le_trans [OF ubasis_until_less])
181    apply (erule ubasis_le.ubasis_le_lower)
182    done
183next
184  case (ubasis_le_upper S b a i) thus ?case
185    apply clarsimp
186    apply (subst ubasis_until_same)
187     apply (erule (3) assms)
188    apply (erule (2) ubasis_le.ubasis_le_upper)
189    done
190qed
191
192lemma finite_range_ubasis_until:
193  "finite {x. P x} \<Longrightarrow> finite (range (ubasis_until P))"
194apply (rule finite_subset [where B="insert 0 {x. P x}"])
195apply (clarsimp simp add: ubasis_until')
196apply simp
197done
198
199
200subsection \<open>Defining the universal domain by ideal completion\<close>
201
202typedef udom = "{S. udom.ideal S}"
203by (rule udom.ex_ideal)
204
205instantiation udom :: below
206begin
207
208definition
209  "x \<sqsubseteq> y \<longleftrightarrow> Rep_udom x \<subseteq> Rep_udom y"
210
211instance ..
212end
213
214instance udom :: po
215using type_definition_udom below_udom_def
216by (rule udom.typedef_ideal_po)
217
218instance udom :: cpo
219using type_definition_udom below_udom_def
220by (rule udom.typedef_ideal_cpo)
221
222definition
223  udom_principal :: "nat \<Rightarrow> udom" where
224  "udom_principal t = Abs_udom {u. ubasis_le u t}"
225
226lemma ubasis_countable: "\<exists>f::ubasis \<Rightarrow> nat. inj f"
227by (rule exI, rule inj_on_id)
228
229interpretation udom:
230  ideal_completion ubasis_le udom_principal Rep_udom
231using type_definition_udom below_udom_def
232using udom_principal_def ubasis_countable
233by (rule udom.typedef_ideal_completion)
234
235text \<open>Universal domain is pointed\<close>
236
237lemma udom_minimal: "udom_principal 0 \<sqsubseteq> x"
238apply (induct x rule: udom.principal_induct)
239apply (simp, simp add: ubasis_le_minimal)
240done
241
242instance udom :: pcpo
243by intro_classes (fast intro: udom_minimal)
244
245lemma inst_udom_pcpo: "\<bottom> = udom_principal 0"
246by (rule udom_minimal [THEN bottomI, symmetric])
247
248
249subsection \<open>Compact bases of domains\<close>
250
251typedef 'a compact_basis = "{x::'a::pcpo. compact x}"
252by auto
253
254lemma Rep_compact_basis' [simp]: "compact (Rep_compact_basis a)"
255by (rule Rep_compact_basis [unfolded mem_Collect_eq])
256
257lemma Abs_compact_basis_inverse' [simp]:
258   "compact x \<Longrightarrow> Rep_compact_basis (Abs_compact_basis x) = x"
259by (rule Abs_compact_basis_inverse [unfolded mem_Collect_eq])
260
261instantiation compact_basis :: (pcpo) below
262begin
263
264definition
265  compact_le_def:
266    "(\<sqsubseteq>) \<equiv> (\<lambda>x y. Rep_compact_basis x \<sqsubseteq> Rep_compact_basis y)"
267
268instance ..
269end
270
271instance compact_basis :: (pcpo) po
272using type_definition_compact_basis compact_le_def
273by (rule typedef_po)
274
275definition
276  approximants :: "'a \<Rightarrow> 'a compact_basis set" where
277  "approximants = (\<lambda>x. {a. Rep_compact_basis a \<sqsubseteq> x})"
278
279definition
280  compact_bot :: "'a::pcpo compact_basis" where
281  "compact_bot = Abs_compact_basis \<bottom>"
282
283lemma Rep_compact_bot [simp]: "Rep_compact_basis compact_bot = \<bottom>"
284unfolding compact_bot_def by simp
285
286lemma compact_bot_minimal [simp]: "compact_bot \<sqsubseteq> a"
287unfolding compact_le_def Rep_compact_bot by simp
288
289
290subsection \<open>Universality of \emph{udom}\<close>
291
292text \<open>We use a locale to parameterize the construction over a chain
293of approx functions on the type to be embedded.\<close>
294
295locale bifinite_approx_chain =
296  approx_chain approx for approx :: "nat \<Rightarrow> 'a::bifinite \<rightarrow> 'a"
297begin
298
299subsubsection \<open>Choosing a maximal element from a finite set\<close>
300
301lemma finite_has_maximal:
302  fixes A :: "'a compact_basis set"
303  shows "\<lbrakk>finite A; A \<noteq> {}\<rbrakk> \<Longrightarrow> \<exists>x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y"
304proof (induct rule: finite_ne_induct)
305  case (singleton x)
306    show ?case by simp
307next
308  case (insert a A)
309  from \<open>\<exists>x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y\<close>
310  obtain x where x: "x \<in> A"
311           and x_eq: "\<And>y. \<lbrakk>y \<in> A; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> x = y" by fast
312  show ?case
313  proof (intro bexI ballI impI)
314    fix y
315    assume "y \<in> insert a A" and "(if x \<sqsubseteq> a then a else x) \<sqsubseteq> y"
316    thus "(if x \<sqsubseteq> a then a else x) = y"
317      apply auto
318      apply (frule (1) below_trans)
319      apply (frule (1) x_eq)
320      apply (rule below_antisym, assumption)
321      apply simp
322      apply (erule (1) x_eq)
323      done
324  next
325    show "(if x \<sqsubseteq> a then a else x) \<in> insert a A"
326      by (simp add: x)
327  qed
328qed
329
330definition
331  choose :: "'a compact_basis set \<Rightarrow> 'a compact_basis"
332where
333  "choose A = (SOME x. x \<in> {x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y})"
334
335lemma choose_lemma:
336  "\<lbrakk>finite A; A \<noteq> {}\<rbrakk> \<Longrightarrow> choose A \<in> {x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y}"
337unfolding choose_def
338apply (rule someI_ex)
339apply (frule (1) finite_has_maximal, fast)
340done
341
342lemma maximal_choose:
343  "\<lbrakk>finite A; y \<in> A; choose A \<sqsubseteq> y\<rbrakk> \<Longrightarrow> choose A = y"
344apply (cases "A = {}", simp)
345apply (frule (1) choose_lemma, simp)
346done
347
348lemma choose_in: "\<lbrakk>finite A; A \<noteq> {}\<rbrakk> \<Longrightarrow> choose A \<in> A"
349by (frule (1) choose_lemma, simp)
350
351function
352  choose_pos :: "'a compact_basis set \<Rightarrow> 'a compact_basis \<Rightarrow> nat"
353where
354  "choose_pos A x =
355    (if finite A \<and> x \<in> A \<and> x \<noteq> choose A
356      then Suc (choose_pos (A - {choose A}) x) else 0)"
357by auto
358
359termination choose_pos
360apply (relation "measure (card \<circ> fst)", simp)
361apply clarsimp
362apply (rule card_Diff1_less)
363apply assumption
364apply (erule choose_in)
365apply clarsimp
366done
367
368declare choose_pos.simps [simp del]
369
370lemma choose_pos_choose: "finite A \<Longrightarrow> choose_pos A (choose A) = 0"
371by (simp add: choose_pos.simps)
372
373lemma inj_on_choose_pos [OF refl]:
374  "\<lbrakk>card A = n; finite A\<rbrakk> \<Longrightarrow> inj_on (choose_pos A) A"
375 apply (induct n arbitrary: A)
376  apply simp
377 apply (case_tac "A = {}", simp)
378 apply (frule (1) choose_in)
379 apply (rule inj_onI)
380 apply (drule_tac x="A - {choose A}" in meta_spec, simp)
381 apply (simp add: choose_pos.simps)
382 apply (simp split: if_split_asm)
383 apply (erule (1) inj_onD, simp, simp)
384done
385
386lemma choose_pos_bounded [OF refl]:
387  "\<lbrakk>card A = n; finite A; x \<in> A\<rbrakk> \<Longrightarrow> choose_pos A x < n"
388apply (induct n arbitrary: A)
389apply simp
390 apply (case_tac "A = {}", simp)
391 apply (frule (1) choose_in)
392apply (subst choose_pos.simps)
393apply simp
394done
395
396lemma choose_pos_lessD:
397  "\<lbrakk>choose_pos A x < choose_pos A y; finite A; x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> x \<notsqsubseteq> y"
398 apply (induct A x arbitrary: y rule: choose_pos.induct)
399 apply simp
400 apply (case_tac "x = choose A")
401  apply simp
402  apply (rule notI)
403  apply (frule (2) maximal_choose)
404  apply simp
405 apply (case_tac "y = choose A")
406  apply (simp add: choose_pos_choose)
407 apply (drule_tac x=y in meta_spec)
408 apply simp
409 apply (erule meta_mp)
410 apply (simp add: choose_pos.simps)
411done
412
413subsubsection \<open>Compact basis take function\<close>
414
415primrec
416  cb_take :: "nat \<Rightarrow> 'a compact_basis \<Rightarrow> 'a compact_basis" where
417  "cb_take 0 = (\<lambda>x. compact_bot)"
418| "cb_take (Suc n) = (\<lambda>a. Abs_compact_basis (approx n\<cdot>(Rep_compact_basis a)))"
419
420declare cb_take.simps [simp del]
421
422lemma cb_take_zero [simp]: "cb_take 0 a = compact_bot"
423by (simp only: cb_take.simps)
424
425lemma Rep_cb_take:
426  "Rep_compact_basis (cb_take (Suc n) a) = approx n\<cdot>(Rep_compact_basis a)"
427by (simp add: cb_take.simps(2))
428
429lemmas approx_Rep_compact_basis = Rep_cb_take [symmetric]
430
431lemma cb_take_covers: "\<exists>n. cb_take n x = x"
432apply (subgoal_tac "\<exists>n. cb_take (Suc n) x = x", fast)
433apply (simp add: Rep_compact_basis_inject [symmetric])
434apply (simp add: Rep_cb_take)
435apply (rule compact_eq_approx)
436apply (rule Rep_compact_basis')
437done
438
439lemma cb_take_less: "cb_take n x \<sqsubseteq> x"
440unfolding compact_le_def
441by (cases n, simp, simp add: Rep_cb_take approx_below)
442
443lemma cb_take_idem: "cb_take n (cb_take n x) = cb_take n x"
444unfolding Rep_compact_basis_inject [symmetric]
445by (cases n, simp, simp add: Rep_cb_take approx_idem)
446
447lemma cb_take_mono: "x \<sqsubseteq> y \<Longrightarrow> cb_take n x \<sqsubseteq> cb_take n y"
448unfolding compact_le_def
449by (cases n, simp, simp add: Rep_cb_take monofun_cfun_arg)
450
451lemma cb_take_chain_le: "m \<le> n \<Longrightarrow> cb_take m x \<sqsubseteq> cb_take n x"
452unfolding compact_le_def
453apply (cases m, simp, cases n, simp)
454apply (simp add: Rep_cb_take, rule chain_mono, simp, simp)
455done
456
457lemma finite_range_cb_take: "finite (range (cb_take n))"
458apply (cases n)
459apply (subgoal_tac "range (cb_take 0) = {compact_bot}", simp, force)
460apply (rule finite_imageD [where f="Rep_compact_basis"])
461apply (rule finite_subset [where B="range (\<lambda>x. approx (n - 1)\<cdot>x)"])
462apply (clarsimp simp add: Rep_cb_take)
463apply (rule finite_range_approx)
464apply (rule inj_onI, simp add: Rep_compact_basis_inject)
465done
466
467subsubsection \<open>Rank of basis elements\<close>
468
469definition
470  rank :: "'a compact_basis \<Rightarrow> nat"
471where
472  "rank x = (LEAST n. cb_take n x = x)"
473
474lemma compact_approx_rank: "cb_take (rank x) x = x"
475unfolding rank_def
476apply (rule LeastI_ex)
477apply (rule cb_take_covers)
478done
479
480lemma rank_leD: "rank x \<le> n \<Longrightarrow> cb_take n x = x"
481apply (rule below_antisym [OF cb_take_less])
482apply (subst compact_approx_rank [symmetric])
483apply (erule cb_take_chain_le)
484done
485
486lemma rank_leI: "cb_take n x = x \<Longrightarrow> rank x \<le> n"
487unfolding rank_def by (rule Least_le)
488
489lemma rank_le_iff: "rank x \<le> n \<longleftrightarrow> cb_take n x = x"
490by (rule iffI [OF rank_leD rank_leI])
491
492lemma rank_compact_bot [simp]: "rank compact_bot = 0"
493using rank_leI [of 0 compact_bot] by simp
494
495lemma rank_eq_0_iff [simp]: "rank x = 0 \<longleftrightarrow> x = compact_bot"
496using rank_le_iff [of x 0] by auto
497
498definition
499  rank_le :: "'a compact_basis \<Rightarrow> 'a compact_basis set"
500where
501  "rank_le x = {y. rank y \<le> rank x}"
502
503definition
504  rank_lt :: "'a compact_basis \<Rightarrow> 'a compact_basis set"
505where
506  "rank_lt x = {y. rank y < rank x}"
507
508definition
509  rank_eq :: "'a compact_basis \<Rightarrow> 'a compact_basis set"
510where
511  "rank_eq x = {y. rank y = rank x}"
512
513lemma rank_eq_cong: "rank x = rank y \<Longrightarrow> rank_eq x = rank_eq y"
514unfolding rank_eq_def by simp
515
516lemma rank_lt_cong: "rank x = rank y \<Longrightarrow> rank_lt x = rank_lt y"
517unfolding rank_lt_def by simp
518
519lemma rank_eq_subset: "rank_eq x \<subseteq> rank_le x"
520unfolding rank_eq_def rank_le_def by auto
521
522lemma rank_lt_subset: "rank_lt x \<subseteq> rank_le x"
523unfolding rank_lt_def rank_le_def by auto
524
525lemma finite_rank_le: "finite (rank_le x)"
526unfolding rank_le_def
527apply (rule finite_subset [where B="range (cb_take (rank x))"])
528apply clarify
529apply (rule range_eqI)
530apply (erule rank_leD [symmetric])
531apply (rule finite_range_cb_take)
532done
533
534lemma finite_rank_eq: "finite (rank_eq x)"
535by (rule finite_subset [OF rank_eq_subset finite_rank_le])
536
537lemma finite_rank_lt: "finite (rank_lt x)"
538by (rule finite_subset [OF rank_lt_subset finite_rank_le])
539
540lemma rank_lt_Int_rank_eq: "rank_lt x \<inter> rank_eq x = {}"
541unfolding rank_lt_def rank_eq_def rank_le_def by auto
542
543lemma rank_lt_Un_rank_eq: "rank_lt x \<union> rank_eq x = rank_le x"
544unfolding rank_lt_def rank_eq_def rank_le_def by auto
545
546subsubsection \<open>Sequencing basis elements\<close>
547
548definition
549  place :: "'a compact_basis \<Rightarrow> nat"
550where
551  "place x = card (rank_lt x) + choose_pos (rank_eq x) x"
552
553lemma place_bounded: "place x < card (rank_le x)"
554unfolding place_def
555 apply (rule ord_less_eq_trans)
556  apply (rule add_strict_left_mono)
557  apply (rule choose_pos_bounded)
558   apply (rule finite_rank_eq)
559  apply (simp add: rank_eq_def)
560 apply (subst card_Un_disjoint [symmetric])
561    apply (rule finite_rank_lt)
562   apply (rule finite_rank_eq)
563  apply (rule rank_lt_Int_rank_eq)
564 apply (simp add: rank_lt_Un_rank_eq)
565done
566
567lemma place_ge: "card (rank_lt x) \<le> place x"
568unfolding place_def by simp
569
570lemma place_rank_mono:
571  fixes x y :: "'a compact_basis"
572  shows "rank x < rank y \<Longrightarrow> place x < place y"
573apply (rule less_le_trans [OF place_bounded])
574apply (rule order_trans [OF _ place_ge])
575apply (rule card_mono)
576apply (rule finite_rank_lt)
577apply (simp add: rank_le_def rank_lt_def subset_eq)
578done
579
580lemma place_eqD: "place x = place y \<Longrightarrow> x = y"
581 apply (rule linorder_cases [where x="rank x" and y="rank y"])
582   apply (drule place_rank_mono, simp)
583  apply (simp add: place_def)
584  apply (rule inj_on_choose_pos [where A="rank_eq x", THEN inj_onD])
585     apply (rule finite_rank_eq)
586    apply (simp cong: rank_lt_cong rank_eq_cong)
587   apply (simp add: rank_eq_def)
588  apply (simp add: rank_eq_def)
589 apply (drule place_rank_mono, simp)
590done
591
592lemma inj_place: "inj place"
593by (rule inj_onI, erule place_eqD)
594
595subsubsection \<open>Embedding and projection on basis elements\<close>
596
597definition
598  sub :: "'a compact_basis \<Rightarrow> 'a compact_basis"
599where
600  "sub x = (case rank x of 0 \<Rightarrow> compact_bot | Suc k \<Rightarrow> cb_take k x)"
601
602lemma rank_sub_less: "x \<noteq> compact_bot \<Longrightarrow> rank (sub x) < rank x"
603unfolding sub_def
604apply (cases "rank x", simp)
605apply (simp add: less_Suc_eq_le)
606apply (rule rank_leI)
607apply (rule cb_take_idem)
608done
609
610lemma place_sub_less: "x \<noteq> compact_bot \<Longrightarrow> place (sub x) < place x"
611apply (rule place_rank_mono)
612apply (erule rank_sub_less)
613done
614
615lemma sub_below: "sub x \<sqsubseteq> x"
616unfolding sub_def by (cases "rank x", simp_all add: cb_take_less)
617
618lemma rank_less_imp_below_sub: "\<lbrakk>x \<sqsubseteq> y; rank x < rank y\<rbrakk> \<Longrightarrow> x \<sqsubseteq> sub y"
619unfolding sub_def
620apply (cases "rank y", simp)
621apply (simp add: less_Suc_eq_le)
622apply (subgoal_tac "cb_take nat x \<sqsubseteq> cb_take nat y")
623apply (simp add: rank_leD)
624apply (erule cb_take_mono)
625done
626
627function
628  basis_emb :: "'a compact_basis \<Rightarrow> ubasis"
629where
630  "basis_emb x = (if x = compact_bot then 0 else
631    node (place x) (basis_emb (sub x))
632      (basis_emb ` {y. place y < place x \<and> x \<sqsubseteq> y}))"
633by auto
634
635termination basis_emb
636apply (relation "measure place", simp)
637apply (simp add: place_sub_less)
638apply simp
639done
640
641declare basis_emb.simps [simp del]
642
643lemma basis_emb_compact_bot [simp]: "basis_emb compact_bot = 0"
644by (simp add: basis_emb.simps)
645
646lemma fin1: "finite {y. place y < place x \<and> x \<sqsubseteq> y}"
647apply (subst Collect_conj_eq)
648apply (rule finite_Int)
649apply (rule disjI1)
650apply (subgoal_tac "finite (place -` {n. n < place x})", simp)
651apply (rule finite_vimageI [OF _ inj_place])
652apply (simp add: lessThan_def [symmetric])
653done
654
655lemma fin2: "finite (basis_emb ` {y. place y < place x \<and> x \<sqsubseteq> y})"
656by (rule finite_imageI [OF fin1])
657
658lemma rank_place_mono:
659  "\<lbrakk>place x < place y; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> rank x < rank y"
660apply (rule linorder_cases, assumption)
661apply (simp add: place_def cong: rank_lt_cong rank_eq_cong)
662apply (drule choose_pos_lessD)
663apply (rule finite_rank_eq)
664apply (simp add: rank_eq_def)
665apply (simp add: rank_eq_def)
666apply simp
667apply (drule place_rank_mono, simp)
668done
669
670lemma basis_emb_mono:
671  "x \<sqsubseteq> y \<Longrightarrow> ubasis_le (basis_emb x) (basis_emb y)"
672proof (induct "max (place x) (place y)" arbitrary: x y rule: less_induct)
673  case less
674  show ?case proof (rule linorder_cases)
675    assume "place x < place y"
676    then have "rank x < rank y"
677      using \<open>x \<sqsubseteq> y\<close> by (rule rank_place_mono)
678    with \<open>place x < place y\<close> show ?case
679      apply (case_tac "y = compact_bot", simp)
680      apply (simp add: basis_emb.simps [of y])
681      apply (rule ubasis_le_trans [OF _ ubasis_le_lower [OF fin2]])
682      apply (rule less)
683       apply (simp add: less_max_iff_disj)
684       apply (erule place_sub_less)
685      apply (erule rank_less_imp_below_sub [OF \<open>x \<sqsubseteq> y\<close>])
686      done
687  next
688    assume "place x = place y"
689    hence "x = y" by (rule place_eqD)
690    thus ?case by (simp add: ubasis_le_refl)
691  next
692    assume "place x > place y"
693    with \<open>x \<sqsubseteq> y\<close> show ?case
694      apply (case_tac "x = compact_bot", simp add: ubasis_le_minimal)
695      apply (simp add: basis_emb.simps [of x])
696      apply (rule ubasis_le_upper [OF fin2], simp)
697      apply (rule less)
698       apply (simp add: less_max_iff_disj)
699       apply (erule place_sub_less)
700      apply (erule rev_below_trans)
701      apply (rule sub_below)
702      done
703  qed
704qed
705
706lemma inj_basis_emb: "inj basis_emb"
707 apply (rule inj_onI)
708 apply (case_tac "x = compact_bot")
709  apply (case_tac [!] "y = compact_bot")
710    apply simp
711   apply (simp add: basis_emb.simps)
712  apply (simp add: basis_emb.simps)
713 apply (simp add: basis_emb.simps)
714 apply (simp add: fin2 inj_eq [OF inj_place])
715done
716
717definition
718  basis_prj :: "ubasis \<Rightarrow> 'a compact_basis"
719where
720  "basis_prj x = inv basis_emb
721    (ubasis_until (\<lambda>x. x \<in> range (basis_emb :: 'a compact_basis \<Rightarrow> ubasis)) x)"
722
723lemma basis_prj_basis_emb: "\<And>x. basis_prj (basis_emb x) = x"
724unfolding basis_prj_def
725 apply (subst ubasis_until_same)
726  apply (rule rangeI)
727 apply (rule inv_f_f)
728 apply (rule inj_basis_emb)
729done
730
731lemma basis_prj_node:
732  "\<lbrakk>finite S; node i a S \<notin> range (basis_emb :: 'a compact_basis \<Rightarrow> nat)\<rbrakk>
733    \<Longrightarrow> basis_prj (node i a S) = (basis_prj a :: 'a compact_basis)"
734unfolding basis_prj_def by simp
735
736lemma basis_prj_0: "basis_prj 0 = compact_bot"
737apply (subst basis_emb_compact_bot [symmetric])
738apply (rule basis_prj_basis_emb)
739done
740
741lemma node_eq_basis_emb_iff:
742  "finite S \<Longrightarrow> node i a S = basis_emb x \<longleftrightarrow>
743    x \<noteq> compact_bot \<and> i = place x \<and> a = basis_emb (sub x) \<and>
744        S = basis_emb ` {y. place y < place x \<and> x \<sqsubseteq> y}"
745apply (cases "x = compact_bot", simp)
746apply (simp add: basis_emb.simps [of x])
747apply (simp add: fin2)
748done
749
750lemma basis_prj_mono: "ubasis_le a b \<Longrightarrow> basis_prj a \<sqsubseteq> basis_prj b"
751proof (induct a b rule: ubasis_le.induct)
752  case (ubasis_le_refl a) show ?case by (rule below_refl)
753next
754  case (ubasis_le_trans a b c) thus ?case by - (rule below_trans)
755next
756  case (ubasis_le_lower S a i) thus ?case
757    apply (cases "node i a S \<in> range (basis_emb :: 'a compact_basis \<Rightarrow> nat)")
758     apply (erule rangeE, rename_tac x)
759     apply (simp add: basis_prj_basis_emb)
760     apply (simp add: node_eq_basis_emb_iff)
761     apply (simp add: basis_prj_basis_emb)
762     apply (rule sub_below)
763    apply (simp add: basis_prj_node)
764    done
765next
766  case (ubasis_le_upper S b a i) thus ?case
767    apply (cases "node i a S \<in> range (basis_emb :: 'a compact_basis \<Rightarrow> nat)")
768     apply (erule rangeE, rename_tac x)
769     apply (simp add: basis_prj_basis_emb)
770     apply (clarsimp simp add: node_eq_basis_emb_iff)
771     apply (simp add: basis_prj_basis_emb)
772    apply (simp add: basis_prj_node)
773    done
774qed
775
776lemma basis_emb_prj_less: "ubasis_le (basis_emb (basis_prj x)) x"
777unfolding basis_prj_def
778 apply (subst f_inv_into_f [where f=basis_emb])
779  apply (rule ubasis_until)
780  apply (rule range_eqI [where x=compact_bot])
781  apply simp
782 apply (rule ubasis_until_less)
783done
784
785lemma ideal_completion:
786  "ideal_completion below Rep_compact_basis (approximants :: 'a \<Rightarrow> _)"
787proof
788  fix w :: "'a"
789  show "below.ideal (approximants w)"
790  proof (rule below.idealI)
791    have "Abs_compact_basis (approx 0\<cdot>w) \<in> approximants w"
792      by (simp add: approximants_def approx_below)
793    thus "\<exists>x. x \<in> approximants w" ..
794  next
795    fix x y :: "'a compact_basis"
796    assume x: "x \<in> approximants w" and y: "y \<in> approximants w"
797    obtain i where i: "approx i\<cdot>(Rep_compact_basis x) = Rep_compact_basis x"
798      using compact_eq_approx Rep_compact_basis' by fast
799    obtain j where j: "approx j\<cdot>(Rep_compact_basis y) = Rep_compact_basis y"
800      using compact_eq_approx Rep_compact_basis' by fast
801    let ?z = "Abs_compact_basis (approx (max i j)\<cdot>w)"
802    have "?z \<in> approximants w"
803      by (simp add: approximants_def approx_below)
804    moreover from x y have "x \<sqsubseteq> ?z \<and> y \<sqsubseteq> ?z"
805      by (simp add: approximants_def compact_le_def)
806         (metis i j monofun_cfun chain_mono chain_approx max.cobounded1 max.cobounded2)
807    ultimately show "\<exists>z \<in> approximants w. x \<sqsubseteq> z \<and> y \<sqsubseteq> z" ..
808  next
809    fix x y :: "'a compact_basis"
810    assume "x \<sqsubseteq> y" "y \<in> approximants w" thus "x \<in> approximants w"
811      unfolding approximants_def compact_le_def
812      by (auto elim: below_trans)
813  qed
814next
815  fix Y :: "nat \<Rightarrow> 'a"
816  assume "chain Y"
817  thus "approximants (\<Squnion>i. Y i) = (\<Union>i. approximants (Y i))"
818    unfolding approximants_def
819    by (auto simp add: compact_below_lub_iff)
820next
821  fix a :: "'a compact_basis"
822  show "approximants (Rep_compact_basis a) = {b. b \<sqsubseteq> a}"
823    unfolding approximants_def compact_le_def ..
824next
825  fix x y :: "'a"
826  assume "approximants x \<subseteq> approximants y"
827  hence "\<forall>z. compact z \<longrightarrow> z \<sqsubseteq> x \<longrightarrow> z \<sqsubseteq> y"
828    by (simp add: approximants_def subset_eq)
829       (metis Abs_compact_basis_inverse')
830  hence "(\<Squnion>i. approx i\<cdot>x) \<sqsubseteq> y"
831    by (simp add: lub_below approx_below)
832  thus "x \<sqsubseteq> y"
833    by (simp add: lub_distribs)
834next
835  show "\<exists>f::'a compact_basis \<Rightarrow> nat. inj f"
836    by (rule exI, rule inj_place)
837qed
838
839end
840
841interpretation compact_basis:
842  ideal_completion below Rep_compact_basis
843    "approximants :: 'a::bifinite \<Rightarrow> 'a compact_basis set"
844proof -
845  obtain a :: "nat \<Rightarrow> 'a \<rightarrow> 'a" where "approx_chain a"
846    using bifinite ..
847  hence "bifinite_approx_chain a"
848    unfolding bifinite_approx_chain_def .
849  thus "ideal_completion below Rep_compact_basis (approximants :: 'a \<Rightarrow> _)"
850    by (rule bifinite_approx_chain.ideal_completion)
851qed
852
853subsubsection \<open>EP-pair from any bifinite domain into \emph{udom}\<close>
854
855context bifinite_approx_chain begin
856
857definition
858  udom_emb :: "'a \<rightarrow> udom"
859where
860  "udom_emb = compact_basis.extension (\<lambda>x. udom_principal (basis_emb x))"
861
862definition
863  udom_prj :: "udom \<rightarrow> 'a"
864where
865  "udom_prj = udom.extension (\<lambda>x. Rep_compact_basis (basis_prj x))"
866
867lemma udom_emb_principal:
868  "udom_emb\<cdot>(Rep_compact_basis x) = udom_principal (basis_emb x)"
869unfolding udom_emb_def
870apply (rule compact_basis.extension_principal)
871apply (rule udom.principal_mono)
872apply (erule basis_emb_mono)
873done
874
875lemma udom_prj_principal:
876  "udom_prj\<cdot>(udom_principal x) = Rep_compact_basis (basis_prj x)"
877unfolding udom_prj_def
878apply (rule udom.extension_principal)
879apply (rule compact_basis.principal_mono)
880apply (erule basis_prj_mono)
881done
882
883lemma ep_pair_udom: "ep_pair udom_emb udom_prj"
884 apply standard
885  apply (rule compact_basis.principal_induct, simp)
886  apply (simp add: udom_emb_principal udom_prj_principal)
887  apply (simp add: basis_prj_basis_emb)
888 apply (rule udom.principal_induct, simp)
889 apply (simp add: udom_emb_principal udom_prj_principal)
890 apply (rule basis_emb_prj_less)
891done
892
893end
894
895abbreviation "udom_emb \<equiv> bifinite_approx_chain.udom_emb"
896abbreviation "udom_prj \<equiv> bifinite_approx_chain.udom_prj"
897
898lemmas ep_pair_udom =
899  bifinite_approx_chain.ep_pair_udom [unfolded bifinite_approx_chain_def]
900
901subsection \<open>Chain of approx functions for type \emph{udom}\<close>
902
903definition
904  udom_approx :: "nat \<Rightarrow> udom \<rightarrow> udom"
905where
906  "udom_approx i =
907    udom.extension (\<lambda>x. udom_principal (ubasis_until (\<lambda>y. y \<le> i) x))"
908
909lemma udom_approx_mono:
910  "ubasis_le a b \<Longrightarrow>
911    udom_principal (ubasis_until (\<lambda>y. y \<le> i) a) \<sqsubseteq>
912    udom_principal (ubasis_until (\<lambda>y. y \<le> i) b)"
913apply (rule udom.principal_mono)
914apply (rule ubasis_until_mono)
915apply (frule (2) order_less_le_trans [OF node_gt2])
916apply (erule order_less_imp_le)
917apply assumption
918done
919
920lemma adm_mem_finite: "\<lbrakk>cont f; finite S\<rbrakk> \<Longrightarrow> adm (\<lambda>x. f x \<in> S)"
921by (erule adm_subst, induct set: finite, simp_all)
922
923lemma udom_approx_principal:
924  "udom_approx i\<cdot>(udom_principal x) =
925    udom_principal (ubasis_until (\<lambda>y. y \<le> i) x)"
926unfolding udom_approx_def
927apply (rule udom.extension_principal)
928apply (erule udom_approx_mono)
929done
930
931lemma finite_deflation_udom_approx: "finite_deflation (udom_approx i)"
932proof
933  fix x show "udom_approx i\<cdot>(udom_approx i\<cdot>x) = udom_approx i\<cdot>x"
934    by (induct x rule: udom.principal_induct, simp)
935       (simp add: udom_approx_principal ubasis_until_idem)
936next
937  fix x show "udom_approx i\<cdot>x \<sqsubseteq> x"
938    by (induct x rule: udom.principal_induct, simp)
939       (simp add: udom_approx_principal ubasis_until_less)
940next
941  have *: "finite (range (\<lambda>x. udom_principal (ubasis_until (\<lambda>y. y \<le> i) x)))"
942    apply (subst range_composition [where f=udom_principal])
943    apply (simp add: finite_range_ubasis_until)
944    done
945  show "finite {x. udom_approx i\<cdot>x = x}"
946    apply (rule finite_range_imp_finite_fixes)
947    apply (rule rev_finite_subset [OF *])
948    apply (clarsimp, rename_tac x)
949    apply (induct_tac x rule: udom.principal_induct)
950    apply (simp add: adm_mem_finite *)
951    apply (simp add: udom_approx_principal)
952    done
953qed
954
955interpretation udom_approx: finite_deflation "udom_approx i"
956by (rule finite_deflation_udom_approx)
957
958lemma chain_udom_approx [simp]: "chain (\<lambda>i. udom_approx i)"
959unfolding udom_approx_def
960apply (rule chainI)
961apply (rule udom.extension_mono)
962apply (erule udom_approx_mono)
963apply (erule udom_approx_mono)
964apply (rule udom.principal_mono)
965apply (rule ubasis_until_chain, simp)
966done
967
968lemma lub_udom_approx [simp]: "(\<Squnion>i. udom_approx i) = ID"
969apply (rule cfun_eqI, simp add: contlub_cfun_fun)
970apply (rule below_antisym)
971apply (rule lub_below)
972apply (simp)
973apply (rule udom_approx.below)
974apply (rule_tac x=x in udom.principal_induct)
975apply (simp add: lub_distribs)
976apply (rule_tac i=a in below_lub)
977apply simp
978apply (simp add: udom_approx_principal)
979apply (simp add: ubasis_until_same ubasis_le_refl)
980done
981
982lemma udom_approx [simp]: "approx_chain udom_approx"
983proof
984  show "chain (\<lambda>i. udom_approx i)"
985    by (rule chain_udom_approx)
986  show "(\<Squnion>i. udom_approx i) = ID"
987    by (rule lub_udom_approx)
988qed
989
990instance udom :: bifinite
991  by standard (fast intro: udom_approx)
992
993hide_const (open) node
994
995notation binomial  (infixl "choose" 65)
996
997end
998