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