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