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