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