src/HOLCF/Universal.thy
author huffman
Mon Mar 22 12:52:51 2010 -0700 (2010-03-22)
changeset 35900 aa5dfb03eb1e
parent 35794 8cd7134275cc
child 36176 3fe7e97ccca8
permissions -rw-r--r--
remove LaTeX hyperref warnings by avoiding antiquotations within section headings
     1 (*  Title:      HOLCF/Universal.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 header {* A universal bifinite domain *}
     6 
     7 theory Universal
     8 imports CompactBasis Nat_Bijection
     9 begin
    10 
    11 subsection {* Basis datatype *}
    12 
    13 types ubasis = nat
    14 
    15 definition
    16   node :: "nat \<Rightarrow> ubasis \<Rightarrow> ubasis set \<Rightarrow> ubasis"
    17 where
    18   "node i a S = Suc (prod_encode (i, prod_encode (a, set_encode S)))"
    19 
    20 lemma node_not_0 [simp]: "node i a S \<noteq> 0"
    21 unfolding node_def by simp
    22 
    23 lemma node_gt_0 [simp]: "0 < node i a S"
    24 unfolding node_def by simp
    25 
    26 lemma node_inject [simp]:
    27   "\<lbrakk>finite S; finite T\<rbrakk>
    28     \<Longrightarrow> node i a S = node j b T \<longleftrightarrow> i = j \<and> a = b \<and> S = T"
    29 unfolding node_def by (simp add: prod_encode_eq set_encode_eq)
    30 
    31 lemma node_gt0: "i < node i a S"
    32 unfolding node_def less_Suc_eq_le
    33 by (rule le_prod_encode_1)
    34 
    35 lemma node_gt1: "a < node i a S"
    36 unfolding node_def less_Suc_eq_le
    37 by (rule order_trans [OF le_prod_encode_1 le_prod_encode_2])
    38 
    39 lemma nat_less_power2: "n < 2^n"
    40 by (induct n) simp_all
    41 
    42 lemma node_gt2: "\<lbrakk>finite S; b \<in> S\<rbrakk> \<Longrightarrow> b < node i a S"
    43 unfolding node_def less_Suc_eq_le set_encode_def
    44 apply (rule order_trans [OF _ le_prod_encode_2])
    45 apply (rule order_trans [OF _ le_prod_encode_2])
    46 apply (rule order_trans [where y="setsum (op ^ 2) {b}"])
    47 apply (simp add: nat_less_power2 [THEN order_less_imp_le])
    48 apply (erule setsum_mono2, simp, simp)
    49 done
    50 
    51 lemma eq_prod_encode_pairI:
    52   "\<lbrakk>fst (prod_decode x) = a; snd (prod_decode x) = b\<rbrakk> \<Longrightarrow> x = prod_encode (a, b)"
    53 by (erule subst, erule subst, simp)
    54 
    55 lemma node_cases:
    56   assumes 1: "x = 0 \<Longrightarrow> P"
    57   assumes 2: "\<And>i a S. \<lbrakk>finite S; x = node i a S\<rbrakk> \<Longrightarrow> P"
    58   shows "P"
    59  apply (cases x)
    60   apply (erule 1)
    61  apply (rule 2)
    62   apply (rule finite_set_decode)
    63  apply (simp add: node_def)
    64  apply (rule eq_prod_encode_pairI [OF refl])
    65  apply (rule eq_prod_encode_pairI [OF refl refl])
    66 done
    67 
    68 lemma node_induct:
    69   assumes 1: "P 0"
    70   assumes 2: "\<And>i a S. \<lbrakk>P a; finite S; \<forall>b\<in>S. P b\<rbrakk> \<Longrightarrow> P (node i a S)"
    71   shows "P x"
    72  apply (induct x rule: nat_less_induct)
    73  apply (case_tac n rule: node_cases)
    74   apply (simp add: 1)
    75  apply (simp add: 2 node_gt1 node_gt2)
    76 done
    77 
    78 subsection {* Basis ordering *}
    79 
    80 inductive
    81   ubasis_le :: "nat \<Rightarrow> nat \<Rightarrow> bool"
    82 where
    83   ubasis_le_refl: "ubasis_le a a"
    84 | ubasis_le_trans:
    85     "\<lbrakk>ubasis_le a b; ubasis_le b c\<rbrakk> \<Longrightarrow> ubasis_le a c"
    86 | ubasis_le_lower:
    87     "finite S \<Longrightarrow> ubasis_le a (node i a S)"
    88 | ubasis_le_upper:
    89     "\<lbrakk>finite S; b \<in> S; ubasis_le a b\<rbrakk> \<Longrightarrow> ubasis_le (node i a S) b"
    90 
    91 lemma ubasis_le_minimal: "ubasis_le 0 x"
    92 apply (induct x rule: node_induct)
    93 apply (rule ubasis_le_refl)
    94 apply (erule ubasis_le_trans)
    95 apply (erule ubasis_le_lower)
    96 done
    97 
    98 subsubsection {* Generic take function *}
    99 
   100 function
   101   ubasis_until :: "(ubasis \<Rightarrow> bool) \<Rightarrow> ubasis \<Rightarrow> ubasis"
   102 where
   103   "ubasis_until P 0 = 0"
   104 | "finite S \<Longrightarrow> ubasis_until P (node i a S) =
   105     (if P (node i a S) then node i a S else ubasis_until P a)"
   106     apply clarify
   107     apply (rule_tac x=b in node_cases)
   108      apply simp
   109     apply simp
   110     apply fast
   111    apply simp
   112   apply simp
   113  apply simp
   114 done
   115 
   116 termination ubasis_until
   117 apply (relation "measure snd")
   118 apply (rule wf_measure)
   119 apply (simp add: node_gt1)
   120 done
   121 
   122 lemma ubasis_until: "P 0 \<Longrightarrow> P (ubasis_until P x)"
   123 by (induct x rule: node_induct) simp_all
   124 
   125 lemma ubasis_until': "0 < ubasis_until P x \<Longrightarrow> P (ubasis_until P x)"
   126 by (induct x rule: node_induct) auto
   127 
   128 lemma ubasis_until_same: "P x \<Longrightarrow> ubasis_until P x = x"
   129 by (induct x rule: node_induct) simp_all
   130 
   131 lemma ubasis_until_idem:
   132   "P 0 \<Longrightarrow> ubasis_until P (ubasis_until P x) = ubasis_until P x"
   133 by (rule ubasis_until_same [OF ubasis_until])
   134 
   135 lemma ubasis_until_0:
   136   "\<forall>x. x \<noteq> 0 \<longrightarrow> \<not> P x \<Longrightarrow> ubasis_until P x = 0"
   137 by (induct x rule: node_induct) simp_all
   138 
   139 lemma ubasis_until_less: "ubasis_le (ubasis_until P x) x"
   140 apply (induct x rule: node_induct)
   141 apply (simp add: ubasis_le_refl)
   142 apply (simp add: ubasis_le_refl)
   143 apply (rule impI)
   144 apply (erule ubasis_le_trans)
   145 apply (erule ubasis_le_lower)
   146 done
   147 
   148 lemma ubasis_until_chain:
   149   assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
   150   shows "ubasis_le (ubasis_until P x) (ubasis_until Q x)"
   151 apply (induct x rule: node_induct)
   152 apply (simp add: ubasis_le_refl)
   153 apply (simp add: ubasis_le_refl)
   154 apply (simp add: PQ)
   155 apply clarify
   156 apply (rule ubasis_le_trans)
   157 apply (rule ubasis_until_less)
   158 apply (erule ubasis_le_lower)
   159 done
   160 
   161 lemma ubasis_until_mono:
   162   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"
   163   shows "ubasis_le a b \<Longrightarrow> ubasis_le (ubasis_until P a) (ubasis_until P b)"
   164 proof (induct set: ubasis_le)
   165   case (ubasis_le_refl a) show ?case by (rule ubasis_le.ubasis_le_refl)
   166 next
   167   case (ubasis_le_trans a b c) thus ?case by - (rule ubasis_le.ubasis_le_trans)
   168 next
   169   case (ubasis_le_lower S a i) thus ?case
   170     apply (clarsimp simp add: ubasis_le_refl)
   171     apply (rule ubasis_le_trans [OF ubasis_until_less])
   172     apply (erule ubasis_le.ubasis_le_lower)
   173     done
   174 next
   175   case (ubasis_le_upper S b a i) thus ?case
   176     apply clarsimp
   177     apply (subst ubasis_until_same)
   178      apply (erule (3) prems)
   179     apply (erule (2) ubasis_le.ubasis_le_upper)
   180     done
   181 qed
   182 
   183 lemma finite_range_ubasis_until:
   184   "finite {x. P x} \<Longrightarrow> finite (range (ubasis_until P))"
   185 apply (rule finite_subset [where B="insert 0 {x. P x}"])
   186 apply (clarsimp simp add: ubasis_until')
   187 apply simp
   188 done
   189 
   190 subsubsection {* Take function for \emph{ubasis} *}
   191 
   192 definition
   193   ubasis_take :: "nat \<Rightarrow> ubasis \<Rightarrow> ubasis"
   194 where
   195   "ubasis_take n = ubasis_until (\<lambda>x. x \<le> n)"
   196 
   197 lemma ubasis_take_le: "ubasis_take n x \<le> n"
   198 unfolding ubasis_take_def by (rule ubasis_until, rule le0)
   199 
   200 lemma ubasis_take_same: "x \<le> n \<Longrightarrow> ubasis_take n x = x"
   201 unfolding ubasis_take_def by (rule ubasis_until_same)
   202 
   203 lemma ubasis_take_idem: "ubasis_take n (ubasis_take n x) = ubasis_take n x"
   204 by (rule ubasis_take_same [OF ubasis_take_le])
   205 
   206 lemma ubasis_take_0 [simp]: "ubasis_take 0 x = 0"
   207 unfolding ubasis_take_def by (simp add: ubasis_until_0)
   208 
   209 lemma ubasis_take_less: "ubasis_le (ubasis_take n x) x"
   210 unfolding ubasis_take_def by (rule ubasis_until_less)
   211 
   212 lemma ubasis_take_chain: "ubasis_le (ubasis_take n x) (ubasis_take (Suc n) x)"
   213 unfolding ubasis_take_def by (rule ubasis_until_chain) simp
   214 
   215 lemma ubasis_take_mono:
   216   assumes "ubasis_le x y"
   217   shows "ubasis_le (ubasis_take n x) (ubasis_take n y)"
   218 unfolding ubasis_take_def
   219  apply (rule ubasis_until_mono [OF _ prems])
   220  apply (frule (2) order_less_le_trans [OF node_gt2])
   221  apply (erule order_less_imp_le)
   222 done
   223 
   224 lemma finite_range_ubasis_take: "finite (range (ubasis_take n))"
   225 apply (rule finite_subset [where B="{..n}"])
   226 apply (simp add: subset_eq ubasis_take_le)
   227 apply simp
   228 done
   229 
   230 lemma ubasis_take_covers: "\<exists>n. ubasis_take n x = x"
   231 apply (rule exI [where x=x])
   232 apply (simp add: ubasis_take_same)
   233 done
   234 
   235 interpretation udom: preorder ubasis_le
   236 apply default
   237 apply (rule ubasis_le_refl)
   238 apply (erule (1) ubasis_le_trans)
   239 done
   240 
   241 interpretation udom: basis_take ubasis_le ubasis_take
   242 apply default
   243 apply (rule ubasis_take_less)
   244 apply (rule ubasis_take_idem)
   245 apply (erule ubasis_take_mono)
   246 apply (rule ubasis_take_chain)
   247 apply (rule finite_range_ubasis_take)
   248 apply (rule ubasis_take_covers)
   249 done
   250 
   251 subsection {* Defining the universal domain by ideal completion *}
   252 
   253 typedef (open) udom = "{S. udom.ideal S}"
   254 by (fast intro: udom.ideal_principal)
   255 
   256 instantiation udom :: below
   257 begin
   258 
   259 definition
   260   "x \<sqsubseteq> y \<longleftrightarrow> Rep_udom x \<subseteq> Rep_udom y"
   261 
   262 instance ..
   263 end
   264 
   265 instance udom :: po
   266 by (rule udom.typedef_ideal_po
   267     [OF type_definition_udom below_udom_def])
   268 
   269 instance udom :: cpo
   270 by (rule udom.typedef_ideal_cpo
   271     [OF type_definition_udom below_udom_def])
   272 
   273 lemma Rep_udom_lub:
   274   "chain Y \<Longrightarrow> Rep_udom (\<Squnion>i. Y i) = (\<Union>i. Rep_udom (Y i))"
   275 by (rule udom.typedef_ideal_rep_contlub
   276     [OF type_definition_udom below_udom_def])
   277 
   278 lemma ideal_Rep_udom: "udom.ideal (Rep_udom xs)"
   279 by (rule Rep_udom [unfolded mem_Collect_eq])
   280 
   281 definition
   282   udom_principal :: "nat \<Rightarrow> udom" where
   283   "udom_principal t = Abs_udom {u. ubasis_le u t}"
   284 
   285 lemma Rep_udom_principal:
   286   "Rep_udom (udom_principal t) = {u. ubasis_le u t}"
   287 unfolding udom_principal_def
   288 by (simp add: Abs_udom_inverse udom.ideal_principal)
   289 
   290 interpretation udom:
   291   ideal_completion ubasis_le ubasis_take udom_principal Rep_udom
   292 apply unfold_locales
   293 apply (rule ideal_Rep_udom)
   294 apply (erule Rep_udom_lub)
   295 apply (rule Rep_udom_principal)
   296 apply (simp only: below_udom_def)
   297 done
   298 
   299 text {* Universal domain is pointed *}
   300 
   301 lemma udom_minimal: "udom_principal 0 \<sqsubseteq> x"
   302 apply (induct x rule: udom.principal_induct)
   303 apply (simp, simp add: ubasis_le_minimal)
   304 done
   305 
   306 instance udom :: pcpo
   307 by intro_classes (fast intro: udom_minimal)
   308 
   309 lemma inst_udom_pcpo: "\<bottom> = udom_principal 0"
   310 by (rule udom_minimal [THEN UU_I, symmetric])
   311 
   312 text {* Universal domain is bifinite *}
   313 
   314 instantiation udom :: bifinite
   315 begin
   316 
   317 definition
   318   approx_udom_def: "approx = udom.completion_approx"
   319 
   320 instance
   321 apply (intro_classes, unfold approx_udom_def)
   322 apply (rule udom.chain_completion_approx)
   323 apply (rule udom.lub_completion_approx)
   324 apply (rule udom.completion_approx_idem)
   325 apply (rule udom.finite_fixes_completion_approx)
   326 done
   327 
   328 end
   329 
   330 lemma approx_udom_principal [simp]:
   331   "approx n\<cdot>(udom_principal x) = udom_principal (ubasis_take n x)"
   332 unfolding approx_udom_def
   333 by (rule udom.completion_approx_principal)
   334 
   335 lemma approx_eq_udom_principal:
   336   "\<exists>a\<in>Rep_udom x. approx n\<cdot>x = udom_principal (ubasis_take n a)"
   337 unfolding approx_udom_def
   338 by (rule udom.completion_approx_eq_principal)
   339 
   340 
   341 subsection {* Universality of \emph{udom} *}
   342 
   343 defaultsort bifinite
   344 
   345 subsubsection {* Choosing a maximal element from a finite set *}
   346 
   347 lemma finite_has_maximal:
   348   fixes A :: "'a::po set"
   349   shows "\<lbrakk>finite A; A \<noteq> {}\<rbrakk> \<Longrightarrow> \<exists>x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y"
   350 proof (induct rule: finite_ne_induct)
   351   case (singleton x)
   352     show ?case by simp
   353 next
   354   case (insert a A)
   355   from `\<exists>x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y`
   356   obtain x where x: "x \<in> A"
   357            and x_eq: "\<And>y. \<lbrakk>y \<in> A; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> x = y" by fast
   358   show ?case
   359   proof (intro bexI ballI impI)
   360     fix y
   361     assume "y \<in> insert a A" and "(if x \<sqsubseteq> a then a else x) \<sqsubseteq> y"
   362     thus "(if x \<sqsubseteq> a then a else x) = y"
   363       apply auto
   364       apply (frule (1) below_trans)
   365       apply (frule (1) x_eq)
   366       apply (rule below_antisym, assumption)
   367       apply simp
   368       apply (erule (1) x_eq)
   369       done
   370   next
   371     show "(if x \<sqsubseteq> a then a else x) \<in> insert a A"
   372       by (simp add: x)
   373   qed
   374 qed
   375 
   376 definition
   377   choose :: "'a compact_basis set \<Rightarrow> 'a compact_basis"
   378 where
   379   "choose A = (SOME x. x \<in> {x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y})"
   380 
   381 lemma choose_lemma:
   382   "\<lbrakk>finite A; A \<noteq> {}\<rbrakk> \<Longrightarrow> choose A \<in> {x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y}"
   383 unfolding choose_def
   384 apply (rule someI_ex)
   385 apply (frule (1) finite_has_maximal, fast)
   386 done
   387 
   388 lemma maximal_choose:
   389   "\<lbrakk>finite A; y \<in> A; choose A \<sqsubseteq> y\<rbrakk> \<Longrightarrow> choose A = y"
   390 apply (cases "A = {}", simp)
   391 apply (frule (1) choose_lemma, simp)
   392 done
   393 
   394 lemma choose_in: "\<lbrakk>finite A; A \<noteq> {}\<rbrakk> \<Longrightarrow> choose A \<in> A"
   395 by (frule (1) choose_lemma, simp)
   396 
   397 function
   398   choose_pos :: "'a compact_basis set \<Rightarrow> 'a compact_basis \<Rightarrow> nat"
   399 where
   400   "choose_pos A x =
   401     (if finite A \<and> x \<in> A \<and> x \<noteq> choose A
   402       then Suc (choose_pos (A - {choose A}) x) else 0)"
   403 by auto
   404 
   405 termination choose_pos
   406 apply (relation "measure (card \<circ> fst)", simp)
   407 apply clarsimp
   408 apply (rule card_Diff1_less)
   409 apply assumption
   410 apply (erule choose_in)
   411 apply clarsimp
   412 done
   413 
   414 declare choose_pos.simps [simp del]
   415 
   416 lemma choose_pos_choose: "finite A \<Longrightarrow> choose_pos A (choose A) = 0"
   417 by (simp add: choose_pos.simps)
   418 
   419 lemma inj_on_choose_pos [OF refl]:
   420   "\<lbrakk>card A = n; finite A\<rbrakk> \<Longrightarrow> inj_on (choose_pos A) A"
   421  apply (induct n arbitrary: A)
   422   apply simp
   423  apply (case_tac "A = {}", simp)
   424  apply (frule (1) choose_in)
   425  apply (rule inj_onI)
   426  apply (drule_tac x="A - {choose A}" in meta_spec, simp)
   427  apply (simp add: choose_pos.simps)
   428  apply (simp split: split_if_asm)
   429  apply (erule (1) inj_onD, simp, simp)
   430 done
   431 
   432 lemma choose_pos_bounded [OF refl]:
   433   "\<lbrakk>card A = n; finite A; x \<in> A\<rbrakk> \<Longrightarrow> choose_pos A x < n"
   434 apply (induct n arbitrary: A)
   435 apply simp
   436  apply (case_tac "A = {}", simp)
   437  apply (frule (1) choose_in)
   438 apply (subst choose_pos.simps)
   439 apply simp
   440 done
   441 
   442 lemma choose_pos_lessD:
   443   "\<lbrakk>choose_pos A x < choose_pos A y; finite A; x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> \<not> x \<sqsubseteq> y"
   444  apply (induct A x arbitrary: y rule: choose_pos.induct)
   445  apply simp
   446  apply (case_tac "x = choose A")
   447   apply simp
   448   apply (rule notI)
   449   apply (frule (2) maximal_choose)
   450   apply simp
   451  apply (case_tac "y = choose A")
   452   apply (simp add: choose_pos_choose)
   453  apply (drule_tac x=y in meta_spec)
   454  apply simp
   455  apply (erule meta_mp)
   456  apply (simp add: choose_pos.simps)
   457 done
   458 
   459 subsubsection {* Rank of basis elements *}
   460 
   461 primrec
   462   cb_take :: "nat \<Rightarrow> 'a compact_basis \<Rightarrow> 'a compact_basis"
   463 where
   464   "cb_take 0 = (\<lambda>x. compact_bot)"
   465 | "cb_take (Suc n) = compact_take n"
   466 
   467 lemma cb_take_covers: "\<exists>n. cb_take n x = x"
   468 apply (rule exE [OF compact_basis.take_covers [where a=x]])
   469 apply (rename_tac n, rule_tac x="Suc n" in exI, simp)
   470 done
   471 
   472 lemma cb_take_less: "cb_take n x \<sqsubseteq> x"
   473 by (cases n, simp, simp add: compact_basis.take_less)
   474 
   475 lemma cb_take_idem: "cb_take n (cb_take n x) = cb_take n x"
   476 by (cases n, simp, simp add: compact_basis.take_take)
   477 
   478 lemma cb_take_mono: "x \<sqsubseteq> y \<Longrightarrow> cb_take n x \<sqsubseteq> cb_take n y"
   479 by (cases n, simp, simp add: compact_basis.take_mono)
   480 
   481 lemma cb_take_chain_le: "m \<le> n \<Longrightarrow> cb_take m x \<sqsubseteq> cb_take n x"
   482 apply (cases m, simp)
   483 apply (cases n, simp)
   484 apply (simp add: compact_basis.take_chain_le)
   485 done
   486 
   487 lemma range_const: "range (\<lambda>x. c) = {c}"
   488 by auto
   489 
   490 lemma finite_range_cb_take: "finite (range (cb_take n))"
   491 apply (cases n)
   492 apply (simp add: range_const)
   493 apply (simp add: compact_basis.finite_range_take)
   494 done
   495 
   496 definition
   497   rank :: "'a compact_basis \<Rightarrow> nat"
   498 where
   499   "rank x = (LEAST n. cb_take n x = x)"
   500 
   501 lemma compact_approx_rank: "cb_take (rank x) x = x"
   502 unfolding rank_def
   503 apply (rule LeastI_ex)
   504 apply (rule cb_take_covers)
   505 done
   506 
   507 lemma rank_leD: "rank x \<le> n \<Longrightarrow> cb_take n x = x"
   508 apply (rule below_antisym [OF cb_take_less])
   509 apply (subst compact_approx_rank [symmetric])
   510 apply (erule cb_take_chain_le)
   511 done
   512 
   513 lemma rank_leI: "cb_take n x = x \<Longrightarrow> rank x \<le> n"
   514 unfolding rank_def by (rule Least_le)
   515 
   516 lemma rank_le_iff: "rank x \<le> n \<longleftrightarrow> cb_take n x = x"
   517 by (rule iffI [OF rank_leD rank_leI])
   518 
   519 lemma rank_compact_bot [simp]: "rank compact_bot = 0"
   520 using rank_leI [of 0 compact_bot] by simp
   521 
   522 lemma rank_eq_0_iff [simp]: "rank x = 0 \<longleftrightarrow> x = compact_bot"
   523 using rank_le_iff [of x 0] by auto
   524 
   525 definition
   526   rank_le :: "'a compact_basis \<Rightarrow> 'a compact_basis set"
   527 where
   528   "rank_le x = {y. rank y \<le> rank x}"
   529 
   530 definition
   531   rank_lt :: "'a compact_basis \<Rightarrow> 'a compact_basis set"
   532 where
   533   "rank_lt x = {y. rank y < rank x}"
   534 
   535 definition
   536   rank_eq :: "'a compact_basis \<Rightarrow> 'a compact_basis set"
   537 where
   538   "rank_eq x = {y. rank y = rank x}"
   539 
   540 lemma rank_eq_cong: "rank x = rank y \<Longrightarrow> rank_eq x = rank_eq y"
   541 unfolding rank_eq_def by simp
   542 
   543 lemma rank_lt_cong: "rank x = rank y \<Longrightarrow> rank_lt x = rank_lt y"
   544 unfolding rank_lt_def by simp
   545 
   546 lemma rank_eq_subset: "rank_eq x \<subseteq> rank_le x"
   547 unfolding rank_eq_def rank_le_def by auto
   548 
   549 lemma rank_lt_subset: "rank_lt x \<subseteq> rank_le x"
   550 unfolding rank_lt_def rank_le_def by auto
   551 
   552 lemma finite_rank_le: "finite (rank_le x)"
   553 unfolding rank_le_def
   554 apply (rule finite_subset [where B="range (cb_take (rank x))"])
   555 apply clarify
   556 apply (rule range_eqI)
   557 apply (erule rank_leD [symmetric])
   558 apply (rule finite_range_cb_take)
   559 done
   560 
   561 lemma finite_rank_eq: "finite (rank_eq x)"
   562 by (rule finite_subset [OF rank_eq_subset finite_rank_le])
   563 
   564 lemma finite_rank_lt: "finite (rank_lt x)"
   565 by (rule finite_subset [OF rank_lt_subset finite_rank_le])
   566 
   567 lemma rank_lt_Int_rank_eq: "rank_lt x \<inter> rank_eq x = {}"
   568 unfolding rank_lt_def rank_eq_def rank_le_def by auto
   569 
   570 lemma rank_lt_Un_rank_eq: "rank_lt x \<union> rank_eq x = rank_le x"
   571 unfolding rank_lt_def rank_eq_def rank_le_def by auto
   572 
   573 subsubsection {* Sequencing basis elements *}
   574 
   575 definition
   576   place :: "'a compact_basis \<Rightarrow> nat"
   577 where
   578   "place x = card (rank_lt x) + choose_pos (rank_eq x) x"
   579 
   580 lemma place_bounded: "place x < card (rank_le x)"
   581 unfolding place_def
   582  apply (rule ord_less_eq_trans)
   583   apply (rule add_strict_left_mono)
   584   apply (rule choose_pos_bounded)
   585    apply (rule finite_rank_eq)
   586   apply (simp add: rank_eq_def)
   587  apply (subst card_Un_disjoint [symmetric])
   588     apply (rule finite_rank_lt)
   589    apply (rule finite_rank_eq)
   590   apply (rule rank_lt_Int_rank_eq)
   591  apply (simp add: rank_lt_Un_rank_eq)
   592 done
   593 
   594 lemma place_ge: "card (rank_lt x) \<le> place x"
   595 unfolding place_def by simp
   596 
   597 lemma place_rank_mono:
   598   fixes x y :: "'a compact_basis"
   599   shows "rank x < rank y \<Longrightarrow> place x < place y"
   600 apply (rule less_le_trans [OF place_bounded])
   601 apply (rule order_trans [OF _ place_ge])
   602 apply (rule card_mono)
   603 apply (rule finite_rank_lt)
   604 apply (simp add: rank_le_def rank_lt_def subset_eq)
   605 done
   606 
   607 lemma place_eqD: "place x = place y \<Longrightarrow> x = y"
   608  apply (rule linorder_cases [where x="rank x" and y="rank y"])
   609    apply (drule place_rank_mono, simp)
   610   apply (simp add: place_def)
   611   apply (rule inj_on_choose_pos [where A="rank_eq x", THEN inj_onD])
   612      apply (rule finite_rank_eq)
   613     apply (simp cong: rank_lt_cong rank_eq_cong)
   614    apply (simp add: rank_eq_def)
   615   apply (simp add: rank_eq_def)
   616  apply (drule place_rank_mono, simp)
   617 done
   618 
   619 lemma inj_place: "inj place"
   620 by (rule inj_onI, erule place_eqD)
   621 
   622 subsubsection {* Embedding and projection on basis elements *}
   623 
   624 definition
   625   sub :: "'a compact_basis \<Rightarrow> 'a compact_basis"
   626 where
   627   "sub x = (case rank x of 0 \<Rightarrow> compact_bot | Suc k \<Rightarrow> cb_take k x)"
   628 
   629 lemma rank_sub_less: "x \<noteq> compact_bot \<Longrightarrow> rank (sub x) < rank x"
   630 unfolding sub_def
   631 apply (cases "rank x", simp)
   632 apply (simp add: less_Suc_eq_le)
   633 apply (rule rank_leI)
   634 apply (rule cb_take_idem)
   635 done
   636 
   637 lemma place_sub_less: "x \<noteq> compact_bot \<Longrightarrow> place (sub x) < place x"
   638 apply (rule place_rank_mono)
   639 apply (erule rank_sub_less)
   640 done
   641 
   642 lemma sub_below: "sub x \<sqsubseteq> x"
   643 unfolding sub_def by (cases "rank x", simp_all add: cb_take_less)
   644 
   645 lemma rank_less_imp_below_sub: "\<lbrakk>x \<sqsubseteq> y; rank x < rank y\<rbrakk> \<Longrightarrow> x \<sqsubseteq> sub y"
   646 unfolding sub_def
   647 apply (cases "rank y", simp)
   648 apply (simp add: less_Suc_eq_le)
   649 apply (subgoal_tac "cb_take nat x \<sqsubseteq> cb_take nat y")
   650 apply (simp add: rank_leD)
   651 apply (erule cb_take_mono)
   652 done
   653 
   654 function
   655   basis_emb :: "'a compact_basis \<Rightarrow> ubasis"
   656 where
   657   "basis_emb x = (if x = compact_bot then 0 else
   658     node (place x) (basis_emb (sub x))
   659       (basis_emb ` {y. place y < place x \<and> x \<sqsubseteq> y}))"
   660 by auto
   661 
   662 termination basis_emb
   663 apply (relation "measure place", simp)
   664 apply (simp add: place_sub_less)
   665 apply simp
   666 done
   667 
   668 declare basis_emb.simps [simp del]
   669 
   670 lemma basis_emb_compact_bot [simp]: "basis_emb compact_bot = 0"
   671 by (simp add: basis_emb.simps)
   672 
   673 lemma fin1: "finite {y. place y < place x \<and> x \<sqsubseteq> y}"
   674 apply (subst Collect_conj_eq)
   675 apply (rule finite_Int)
   676 apply (rule disjI1)
   677 apply (subgoal_tac "finite (place -` {n. n < place x})", simp)
   678 apply (rule finite_vimageI [OF _ inj_place])
   679 apply (simp add: lessThan_def [symmetric])
   680 done
   681 
   682 lemma fin2: "finite (basis_emb ` {y. place y < place x \<and> x \<sqsubseteq> y})"
   683 by (rule finite_imageI [OF fin1])
   684 
   685 lemma rank_place_mono:
   686   "\<lbrakk>place x < place y; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> rank x < rank y"
   687 apply (rule linorder_cases, assumption)
   688 apply (simp add: place_def cong: rank_lt_cong rank_eq_cong)
   689 apply (drule choose_pos_lessD)
   690 apply (rule finite_rank_eq)
   691 apply (simp add: rank_eq_def)
   692 apply (simp add: rank_eq_def)
   693 apply simp
   694 apply (drule place_rank_mono, simp)
   695 done
   696 
   697 lemma basis_emb_mono:
   698   "x \<sqsubseteq> y \<Longrightarrow> ubasis_le (basis_emb x) (basis_emb y)"
   699 proof (induct "max (place x) (place y)" arbitrary: x y rule: less_induct)
   700   case less
   701   show ?case proof (rule linorder_cases)
   702     assume "place x < place y"
   703     then have "rank x < rank y"
   704       using `x \<sqsubseteq> y` by (rule rank_place_mono)
   705     with `place x < place y` show ?case
   706       apply (case_tac "y = compact_bot", simp)
   707       apply (simp add: basis_emb.simps [of y])
   708       apply (rule ubasis_le_trans [OF _ ubasis_le_lower [OF fin2]])
   709       apply (rule less)
   710        apply (simp add: less_max_iff_disj)
   711        apply (erule place_sub_less)
   712       apply (erule rank_less_imp_below_sub [OF `x \<sqsubseteq> y`])
   713       done
   714   next
   715     assume "place x = place y"
   716     hence "x = y" by (rule place_eqD)
   717     thus ?case by (simp add: ubasis_le_refl)
   718   next
   719     assume "place x > place y"
   720     with `x \<sqsubseteq> y` show ?case
   721       apply (case_tac "x = compact_bot", simp add: ubasis_le_minimal)
   722       apply (simp add: basis_emb.simps [of x])
   723       apply (rule ubasis_le_upper [OF fin2], simp)
   724       apply (rule less)
   725        apply (simp add: less_max_iff_disj)
   726        apply (erule place_sub_less)
   727       apply (erule rev_below_trans)
   728       apply (rule sub_below)
   729       done
   730   qed
   731 qed
   732 
   733 lemma inj_basis_emb: "inj basis_emb"
   734  apply (rule inj_onI)
   735  apply (case_tac "x = compact_bot")
   736   apply (case_tac [!] "y = compact_bot")
   737     apply simp
   738    apply (simp add: basis_emb.simps)
   739   apply (simp add: basis_emb.simps)
   740  apply (simp add: basis_emb.simps)
   741  apply (simp add: fin2 inj_eq [OF inj_place])
   742 done
   743 
   744 definition
   745   basis_prj :: "ubasis \<Rightarrow> 'a compact_basis"
   746 where
   747   "basis_prj x = inv basis_emb
   748     (ubasis_until (\<lambda>x. x \<in> range (basis_emb :: 'a compact_basis \<Rightarrow> ubasis)) x)"
   749 
   750 lemma basis_prj_basis_emb: "\<And>x. basis_prj (basis_emb x) = x"
   751 unfolding basis_prj_def
   752  apply (subst ubasis_until_same)
   753   apply (rule rangeI)
   754  apply (rule inv_f_f)
   755  apply (rule inj_basis_emb)
   756 done
   757 
   758 lemma basis_prj_node:
   759   "\<lbrakk>finite S; node i a S \<notin> range (basis_emb :: 'a compact_basis \<Rightarrow> nat)\<rbrakk>
   760     \<Longrightarrow> basis_prj (node i a S) = (basis_prj a :: 'a compact_basis)"
   761 unfolding basis_prj_def by simp
   762 
   763 lemma basis_prj_0: "basis_prj 0 = compact_bot"
   764 apply (subst basis_emb_compact_bot [symmetric])
   765 apply (rule basis_prj_basis_emb)
   766 done
   767 
   768 lemma node_eq_basis_emb_iff:
   769   "finite S \<Longrightarrow> node i a S = basis_emb x \<longleftrightarrow>
   770     x \<noteq> compact_bot \<and> i = place x \<and> a = basis_emb (sub x) \<and>
   771         S = basis_emb ` {y. place y < place x \<and> x \<sqsubseteq> y}"
   772 apply (cases "x = compact_bot", simp)
   773 apply (simp add: basis_emb.simps [of x])
   774 apply (simp add: fin2)
   775 done
   776 
   777 lemma basis_prj_mono: "ubasis_le a b \<Longrightarrow> basis_prj a \<sqsubseteq> basis_prj b"
   778 proof (induct a b rule: ubasis_le.induct)
   779   case (ubasis_le_refl a) show ?case by (rule below_refl)
   780 next
   781   case (ubasis_le_trans a b c) thus ?case by - (rule below_trans)
   782 next
   783   case (ubasis_le_lower S a i) thus ?case
   784     apply (cases "node i a S \<in> range (basis_emb :: 'a compact_basis \<Rightarrow> nat)")
   785      apply (erule rangeE, rename_tac x)
   786      apply (simp add: basis_prj_basis_emb)
   787      apply (simp add: node_eq_basis_emb_iff)
   788      apply (simp add: basis_prj_basis_emb)
   789      apply (rule sub_below)
   790     apply (simp add: basis_prj_node)
   791     done
   792 next
   793   case (ubasis_le_upper S b a i) thus ?case
   794     apply (cases "node i a S \<in> range (basis_emb :: 'a compact_basis \<Rightarrow> nat)")
   795      apply (erule rangeE, rename_tac x)
   796      apply (simp add: basis_prj_basis_emb)
   797      apply (clarsimp simp add: node_eq_basis_emb_iff)
   798      apply (simp add: basis_prj_basis_emb)
   799     apply (simp add: basis_prj_node)
   800     done
   801 qed
   802 
   803 lemma basis_emb_prj_less: "ubasis_le (basis_emb (basis_prj x)) x"
   804 unfolding basis_prj_def
   805  apply (subst f_inv_into_f [where f=basis_emb])
   806   apply (rule ubasis_until)
   807   apply (rule range_eqI [where x=compact_bot])
   808   apply simp
   809  apply (rule ubasis_until_less)
   810 done
   811 
   812 hide (open) const
   813   node
   814   choose
   815   choose_pos
   816   place
   817   sub
   818 
   819 subsubsection {* EP-pair from any bifinite domain into \emph{udom} *}
   820 
   821 definition
   822   udom_emb :: "'a::bifinite \<rightarrow> udom"
   823 where
   824   "udom_emb = compact_basis.basis_fun (\<lambda>x. udom_principal (basis_emb x))"
   825 
   826 definition
   827   udom_prj :: "udom \<rightarrow> 'a::bifinite"
   828 where
   829   "udom_prj = udom.basis_fun (\<lambda>x. Rep_compact_basis (basis_prj x))"
   830 
   831 lemma udom_emb_principal:
   832   "udom_emb\<cdot>(Rep_compact_basis x) = udom_principal (basis_emb x)"
   833 unfolding udom_emb_def
   834 apply (rule compact_basis.basis_fun_principal)
   835 apply (rule udom.principal_mono)
   836 apply (erule basis_emb_mono)
   837 done
   838 
   839 lemma udom_prj_principal:
   840   "udom_prj\<cdot>(udom_principal x) = Rep_compact_basis (basis_prj x)"
   841 unfolding udom_prj_def
   842 apply (rule udom.basis_fun_principal)
   843 apply (rule compact_basis.principal_mono)
   844 apply (erule basis_prj_mono)
   845 done
   846 
   847 lemma ep_pair_udom: "ep_pair udom_emb udom_prj"
   848  apply default
   849   apply (rule compact_basis.principal_induct, simp)
   850   apply (simp add: udom_emb_principal udom_prj_principal)
   851   apply (simp add: basis_prj_basis_emb)
   852  apply (rule udom.principal_induct, simp)
   853  apply (simp add: udom_emb_principal udom_prj_principal)
   854  apply (rule basis_emb_prj_less)
   855 done
   856 
   857 end