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