src/HOL/HOLCF/Universal.thy
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (20 months ago)
changeset 67003 49850a679c2c
parent 66453 cc19f7ca2ed6
child 67399 eab6ce8368fa
permissions -rw-r--r--
more robust sorted_entries;
wenzelm@42151
     1
(*  Title:      HOL/HOLCF/Universal.thy
huffman@27411
     2
    Author:     Brian Huffman
huffman@27411
     3
*)
huffman@27411
     4
wenzelm@62175
     5
section \<open>A universal bifinite domain\<close>
huffman@35794
     6
huffman@27411
     7
theory Universal
wenzelm@66453
     8
imports Bifinite Completion "HOL-Library.Nat_Bijection"
huffman@27411
     9
begin
huffman@27411
    10
wenzelm@65552
    11
no_notation binomial  (infixl "choose" 65)
wenzelm@65552
    12
wenzelm@62175
    13
subsection \<open>Basis for universal domain\<close>
huffman@39974
    14
wenzelm@62175
    15
subsubsection \<open>Basis datatype\<close>
huffman@27411
    16
huffman@41295
    17
type_synonym ubasis = nat
huffman@27411
    18
huffman@27411
    19
definition
huffman@27411
    20
  node :: "nat \<Rightarrow> ubasis \<Rightarrow> ubasis set \<Rightarrow> ubasis"
huffman@27411
    21
where
huffman@35701
    22
  "node i a S = Suc (prod_encode (i, prod_encode (a, set_encode S)))"
huffman@27411
    23
huffman@30505
    24
lemma node_not_0 [simp]: "node i a S \<noteq> 0"
huffman@27411
    25
unfolding node_def by simp
huffman@27411
    26
huffman@30505
    27
lemma node_gt_0 [simp]: "0 < node i a S"
huffman@27411
    28
unfolding node_def by simp
huffman@27411
    29
huffman@27411
    30
lemma node_inject [simp]:
huffman@30505
    31
  "\<lbrakk>finite S; finite T\<rbrakk>
huffman@30505
    32
    \<Longrightarrow> node i a S = node j b T \<longleftrightarrow> i = j \<and> a = b \<and> S = T"
huffman@35701
    33
unfolding node_def by (simp add: prod_encode_eq set_encode_eq)
huffman@27411
    34
huffman@30505
    35
lemma node_gt0: "i < node i a S"
huffman@27411
    36
unfolding node_def less_Suc_eq_le
huffman@35701
    37
by (rule le_prod_encode_1)
huffman@27411
    38
huffman@30505
    39
lemma node_gt1: "a < node i a S"
huffman@27411
    40
unfolding node_def less_Suc_eq_le
huffman@35701
    41
by (rule order_trans [OF le_prod_encode_1 le_prod_encode_2])
huffman@27411
    42
huffman@27411
    43
lemma nat_less_power2: "n < 2^n"
huffman@27411
    44
by (induct n) simp_all
huffman@27411
    45
huffman@30505
    46
lemma node_gt2: "\<lbrakk>finite S; b \<in> S\<rbrakk> \<Longrightarrow> b < node i a S"
huffman@35701
    47
unfolding node_def less_Suc_eq_le set_encode_def
huffman@35701
    48
apply (rule order_trans [OF _ le_prod_encode_2])
huffman@35701
    49
apply (rule order_trans [OF _ le_prod_encode_2])
nipkow@64267
    50
apply (rule order_trans [where y="sum (op ^ 2) {b}"])
huffman@27411
    51
apply (simp add: nat_less_power2 [THEN order_less_imp_le])
nipkow@64267
    52
apply (erule sum_mono2, simp, simp)
huffman@27411
    53
done
huffman@27411
    54
huffman@35701
    55
lemma eq_prod_encode_pairI:
huffman@35701
    56
  "\<lbrakk>fst (prod_decode x) = a; snd (prod_decode x) = b\<rbrakk> \<Longrightarrow> x = prod_encode (a, b)"
huffman@27411
    57
by (erule subst, erule subst, simp)
huffman@27411
    58
huffman@27411
    59
lemma node_cases:
huffman@27411
    60
  assumes 1: "x = 0 \<Longrightarrow> P"
huffman@30505
    61
  assumes 2: "\<And>i a S. \<lbrakk>finite S; x = node i a S\<rbrakk> \<Longrightarrow> P"
huffman@27411
    62
  shows "P"
huffman@27411
    63
 apply (cases x)
huffman@27411
    64
  apply (erule 1)
huffman@27411
    65
 apply (rule 2)
huffman@35701
    66
  apply (rule finite_set_decode)
huffman@27411
    67
 apply (simp add: node_def)
huffman@35701
    68
 apply (rule eq_prod_encode_pairI [OF refl])
huffman@35701
    69
 apply (rule eq_prod_encode_pairI [OF refl refl])
huffman@27411
    70
done
huffman@27411
    71
huffman@27411
    72
lemma node_induct:
huffman@27411
    73
  assumes 1: "P 0"
huffman@30505
    74
  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
    75
  shows "P x"
huffman@27411
    76
 apply (induct x rule: nat_less_induct)
huffman@27411
    77
 apply (case_tac n rule: node_cases)
huffman@27411
    78
  apply (simp add: 1)
huffman@27411
    79
 apply (simp add: 2 node_gt1 node_gt2)
huffman@27411
    80
done
huffman@27411
    81
wenzelm@62175
    82
subsubsection \<open>Basis ordering\<close>
huffman@27411
    83
huffman@27411
    84
inductive
huffman@27411
    85
  ubasis_le :: "nat \<Rightarrow> nat \<Rightarrow> bool"
huffman@27411
    86
where
huffman@30505
    87
  ubasis_le_refl: "ubasis_le a a"
huffman@27411
    88
| ubasis_le_trans:
huffman@30505
    89
    "\<lbrakk>ubasis_le a b; ubasis_le b c\<rbrakk> \<Longrightarrow> ubasis_le a c"
huffman@27411
    90
| ubasis_le_lower:
huffman@30505
    91
    "finite S \<Longrightarrow> ubasis_le a (node i a S)"
huffman@27411
    92
| ubasis_le_upper:
huffman@30505
    93
    "\<lbrakk>finite S; b \<in> S; ubasis_le a b\<rbrakk> \<Longrightarrow> ubasis_le (node i a S) b"
huffman@27411
    94
huffman@27411
    95
lemma ubasis_le_minimal: "ubasis_le 0 x"
huffman@27411
    96
apply (induct x rule: node_induct)
huffman@27411
    97
apply (rule ubasis_le_refl)
huffman@27411
    98
apply (erule ubasis_le_trans)
huffman@27411
    99
apply (erule ubasis_le_lower)
huffman@27411
   100
done
huffman@27411
   101
huffman@39974
   102
interpretation udom: preorder ubasis_le
wenzelm@61169
   103
apply standard
huffman@39974
   104
apply (rule ubasis_le_refl)
huffman@39974
   105
apply (erule (1) ubasis_le_trans)
huffman@39974
   106
done
huffman@39974
   107
wenzelm@62175
   108
subsubsection \<open>Generic take function\<close>
huffman@27411
   109
huffman@27411
   110
function
huffman@27411
   111
  ubasis_until :: "(ubasis \<Rightarrow> bool) \<Rightarrow> ubasis \<Rightarrow> ubasis"
huffman@27411
   112
where
huffman@27411
   113
  "ubasis_until P 0 = 0"
huffman@30505
   114
| "finite S \<Longrightarrow> ubasis_until P (node i a S) =
huffman@30505
   115
    (if P (node i a S) then node i a S else ubasis_until P a)"
nipkow@56073
   116
   apply clarify
nipkow@56073
   117
   apply (rule_tac x=b in node_cases)
huffman@27411
   118
    apply simp
huffman@27411
   119
   apply simp
nipkow@56073
   120
   apply fast
huffman@27411
   121
  apply simp
huffman@27411
   122
 apply simp
huffman@27411
   123
done
huffman@27411
   124
huffman@27411
   125
termination ubasis_until
huffman@27411
   126
apply (relation "measure snd")
huffman@27411
   127
apply (rule wf_measure)
huffman@27411
   128
apply (simp add: node_gt1)
huffman@27411
   129
done
huffman@27411
   130
huffman@27411
   131
lemma ubasis_until: "P 0 \<Longrightarrow> P (ubasis_until P x)"
huffman@27411
   132
by (induct x rule: node_induct) simp_all
huffman@27411
   133
huffman@27411
   134
lemma ubasis_until': "0 < ubasis_until P x \<Longrightarrow> P (ubasis_until P x)"
huffman@27411
   135
by (induct x rule: node_induct) auto
huffman@27411
   136
huffman@27411
   137
lemma ubasis_until_same: "P x \<Longrightarrow> ubasis_until P x = x"
huffman@27411
   138
by (induct x rule: node_induct) simp_all
huffman@27411
   139
huffman@27411
   140
lemma ubasis_until_idem:
huffman@27411
   141
  "P 0 \<Longrightarrow> ubasis_until P (ubasis_until P x) = ubasis_until P x"
huffman@27411
   142
by (rule ubasis_until_same [OF ubasis_until])
huffman@27411
   143
huffman@27411
   144
lemma ubasis_until_0:
huffman@27411
   145
  "\<forall>x. x \<noteq> 0 \<longrightarrow> \<not> P x \<Longrightarrow> ubasis_until P x = 0"
huffman@27411
   146
by (induct x rule: node_induct) simp_all
huffman@27411
   147
huffman@27411
   148
lemma ubasis_until_less: "ubasis_le (ubasis_until P x) 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 (rule impI)
huffman@27411
   153
apply (erule ubasis_le_trans)
huffman@27411
   154
apply (erule ubasis_le_lower)
huffman@27411
   155
done
huffman@27411
   156
huffman@27411
   157
lemma ubasis_until_chain:
huffman@27411
   158
  assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
huffman@27411
   159
  shows "ubasis_le (ubasis_until P x) (ubasis_until Q x)"
huffman@27411
   160
apply (induct x rule: node_induct)
huffman@27411
   161
apply (simp add: ubasis_le_refl)
huffman@27411
   162
apply (simp add: ubasis_le_refl)
huffman@27411
   163
apply (simp add: PQ)
huffman@27411
   164
apply clarify
huffman@27411
   165
apply (rule ubasis_le_trans)
huffman@27411
   166
apply (rule ubasis_until_less)
huffman@27411
   167
apply (erule ubasis_le_lower)
huffman@27411
   168
done
huffman@27411
   169
huffman@27411
   170
lemma ubasis_until_mono:
huffman@30505
   171
  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
   172
  shows "ubasis_le a b \<Longrightarrow> ubasis_le (ubasis_until P a) (ubasis_until P b)"
huffman@30561
   173
proof (induct set: ubasis_le)
huffman@30561
   174
  case (ubasis_le_refl a) show ?case by (rule ubasis_le.ubasis_le_refl)
huffman@30561
   175
next
huffman@30561
   176
  case (ubasis_le_trans a b c) thus ?case by - (rule ubasis_le.ubasis_le_trans)
huffman@30561
   177
next
huffman@30561
   178
  case (ubasis_le_lower S a i) thus ?case
huffman@30561
   179
    apply (clarsimp simp add: ubasis_le_refl)
huffman@30561
   180
    apply (rule ubasis_le_trans [OF ubasis_until_less])
huffman@30561
   181
    apply (erule ubasis_le.ubasis_le_lower)
huffman@30561
   182
    done
huffman@30561
   183
next
huffman@30561
   184
  case (ubasis_le_upper S b a i) thus ?case
huffman@30561
   185
    apply clarsimp
huffman@30561
   186
    apply (subst ubasis_until_same)
wenzelm@41529
   187
     apply (erule (3) assms)
huffman@30561
   188
    apply (erule (2) ubasis_le.ubasis_le_upper)
huffman@30561
   189
    done
huffman@30561
   190
qed
huffman@27411
   191
huffman@27411
   192
lemma finite_range_ubasis_until:
huffman@27411
   193
  "finite {x. P x} \<Longrightarrow> finite (range (ubasis_until P))"
huffman@27411
   194
apply (rule finite_subset [where B="insert 0 {x. P x}"])
huffman@27411
   195
apply (clarsimp simp add: ubasis_until')
huffman@27411
   196
apply simp
huffman@27411
   197
done
huffman@27411
   198
huffman@27411
   199
wenzelm@62175
   200
subsection \<open>Defining the universal domain by ideal completion\<close>
huffman@27411
   201
wenzelm@49834
   202
typedef udom = "{S. udom.ideal S}"
huffman@40888
   203
by (rule udom.ex_ideal)
huffman@27411
   204
huffman@31076
   205
instantiation udom :: below
huffman@27411
   206
begin
huffman@27411
   207
huffman@27411
   208
definition
huffman@27411
   209
  "x \<sqsubseteq> y \<longleftrightarrow> Rep_udom x \<subseteq> Rep_udom y"
huffman@27411
   210
huffman@27411
   211
instance ..
huffman@27411
   212
end
huffman@27411
   213
huffman@27411
   214
instance udom :: po
huffman@39974
   215
using type_definition_udom below_udom_def
huffman@39974
   216
by (rule udom.typedef_ideal_po)
huffman@27411
   217
huffman@27411
   218
instance udom :: cpo
huffman@39974
   219
using type_definition_udom below_udom_def
huffman@39974
   220
by (rule udom.typedef_ideal_cpo)
huffman@27411
   221
huffman@27411
   222
definition
huffman@27411
   223
  udom_principal :: "nat \<Rightarrow> udom" where
huffman@27411
   224
  "udom_principal t = Abs_udom {u. ubasis_le u t}"
huffman@27411
   225
huffman@39984
   226
lemma ubasis_countable: "\<exists>f::ubasis \<Rightarrow> nat. inj f"
huffman@39984
   227
by (rule exI, rule inj_on_id)
huffman@27411
   228
wenzelm@30729
   229
interpretation udom:
huffman@39974
   230
  ideal_completion ubasis_le udom_principal Rep_udom
huffman@39984
   231
using type_definition_udom below_udom_def
huffman@39984
   232
using udom_principal_def ubasis_countable
huffman@39984
   233
by (rule udom.typedef_ideal_completion)
huffman@27411
   234
wenzelm@62175
   235
text \<open>Universal domain is pointed\<close>
huffman@27411
   236
huffman@27411
   237
lemma udom_minimal: "udom_principal 0 \<sqsubseteq> x"
huffman@27411
   238
apply (induct x rule: udom.principal_induct)
huffman@27411
   239
apply (simp, simp add: ubasis_le_minimal)
huffman@27411
   240
done
huffman@27411
   241
huffman@27411
   242
instance udom :: pcpo
huffman@27411
   243
by intro_classes (fast intro: udom_minimal)
huffman@27411
   244
huffman@27411
   245
lemma inst_udom_pcpo: "\<bottom> = udom_principal 0"
huffman@41430
   246
by (rule udom_minimal [THEN bottomI, symmetric])
huffman@27411
   247
huffman@39974
   248
wenzelm@62175
   249
subsection \<open>Compact bases of domains\<close>
huffman@27411
   250
wenzelm@49834
   251
typedef 'a compact_basis = "{x::'a::pcpo. compact x}"
huffman@39974
   252
by auto
huffman@39974
   253
huffman@41370
   254
lemma Rep_compact_basis' [simp]: "compact (Rep_compact_basis a)"
huffman@39974
   255
by (rule Rep_compact_basis [unfolded mem_Collect_eq])
huffman@39974
   256
huffman@41370
   257
lemma Abs_compact_basis_inverse' [simp]:
huffman@41370
   258
   "compact x \<Longrightarrow> Rep_compact_basis (Abs_compact_basis x) = x"
huffman@41370
   259
by (rule Abs_compact_basis_inverse [unfolded mem_Collect_eq])
huffman@41370
   260
huffman@39974
   261
instantiation compact_basis :: (pcpo) below
huffman@27411
   262
begin
huffman@27411
   263
huffman@27411
   264
definition
huffman@39974
   265
  compact_le_def:
huffman@39974
   266
    "(op \<sqsubseteq>) \<equiv> (\<lambda>x y. Rep_compact_basis x \<sqsubseteq> Rep_compact_basis y)"
huffman@27411
   267
huffman@39974
   268
instance ..
huffman@27411
   269
end
huffman@27411
   270
huffman@39974
   271
instance compact_basis :: (pcpo) po
huffman@39974
   272
using type_definition_compact_basis compact_le_def
huffman@39974
   273
by (rule typedef_po)
huffman@39974
   274
huffman@39974
   275
definition
huffman@39974
   276
  approximants :: "'a \<Rightarrow> 'a compact_basis set" where
huffman@39974
   277
  "approximants = (\<lambda>x. {a. Rep_compact_basis a \<sqsubseteq> x})"
huffman@27411
   278
huffman@39974
   279
definition
huffman@39974
   280
  compact_bot :: "'a::pcpo compact_basis" where
huffman@39974
   281
  "compact_bot = Abs_compact_basis \<bottom>"
huffman@39974
   282
huffman@39974
   283
lemma Rep_compact_bot [simp]: "Rep_compact_basis compact_bot = \<bottom>"
huffman@41370
   284
unfolding compact_bot_def by simp
huffman@39974
   285
huffman@39974
   286
lemma compact_bot_minimal [simp]: "compact_bot \<sqsubseteq> a"
huffman@39974
   287
unfolding compact_le_def Rep_compact_bot by simp
huffman@27411
   288
huffman@27411
   289
wenzelm@62175
   290
subsection \<open>Universality of \emph{udom}\<close>
huffman@27411
   291
wenzelm@62175
   292
text \<open>We use a locale to parameterize the construction over a chain
wenzelm@62175
   293
of approx functions on the type to be embedded.\<close>
huffman@39974
   294
wenzelm@46868
   295
locale bifinite_approx_chain =
wenzelm@46868
   296
  approx_chain approx for approx :: "nat \<Rightarrow> 'a::bifinite \<rightarrow> 'a"
huffman@39974
   297
begin
huffman@27411
   298
wenzelm@62175
   299
subsubsection \<open>Choosing a maximal element from a finite set\<close>
huffman@27411
   300
huffman@27411
   301
lemma finite_has_maximal:
huffman@39974
   302
  fixes A :: "'a compact_basis set"
huffman@27411
   303
  shows "\<lbrakk>finite A; A \<noteq> {}\<rbrakk> \<Longrightarrow> \<exists>x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y"
huffman@27411
   304
proof (induct rule: finite_ne_induct)
huffman@27411
   305
  case (singleton x)
huffman@27411
   306
    show ?case by simp
huffman@27411
   307
next
huffman@27411
   308
  case (insert a A)
wenzelm@62175
   309
  from \<open>\<exists>x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y\<close>
huffman@27411
   310
  obtain x where x: "x \<in> A"
huffman@27411
   311
           and x_eq: "\<And>y. \<lbrakk>y \<in> A; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> x = y" by fast
huffman@27411
   312
  show ?case
huffman@27411
   313
  proof (intro bexI ballI impI)
huffman@27411
   314
    fix y
huffman@27411
   315
    assume "y \<in> insert a A" and "(if x \<sqsubseteq> a then a else x) \<sqsubseteq> y"
huffman@27411
   316
    thus "(if x \<sqsubseteq> a then a else x) = y"
huffman@27411
   317
      apply auto
huffman@31076
   318
      apply (frule (1) below_trans)
huffman@27411
   319
      apply (frule (1) x_eq)
huffman@31076
   320
      apply (rule below_antisym, assumption)
huffman@27411
   321
      apply simp
huffman@27411
   322
      apply (erule (1) x_eq)
huffman@27411
   323
      done
huffman@27411
   324
  next
huffman@27411
   325
    show "(if x \<sqsubseteq> a then a else x) \<in> insert a A"
huffman@27411
   326
      by (simp add: x)
huffman@27411
   327
  qed
huffman@27411
   328
qed
huffman@27411
   329
huffman@27411
   330
definition
huffman@27411
   331
  choose :: "'a compact_basis set \<Rightarrow> 'a compact_basis"
huffman@27411
   332
where
huffman@27411
   333
  "choose A = (SOME x. x \<in> {x\<in>A. \<forall>y\<in>A. x \<sqsubseteq> y \<longrightarrow> x = y})"
huffman@27411
   334
huffman@27411
   335
lemma choose_lemma:
huffman@27411
   336
  "\<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
   337
unfolding choose_def
huffman@27411
   338
apply (rule someI_ex)
huffman@27411
   339
apply (frule (1) finite_has_maximal, fast)
huffman@27411
   340
done
huffman@27411
   341
huffman@27411
   342
lemma maximal_choose:
huffman@27411
   343
  "\<lbrakk>finite A; y \<in> A; choose A \<sqsubseteq> y\<rbrakk> \<Longrightarrow> choose A = y"
huffman@27411
   344
apply (cases "A = {}", simp)
huffman@27411
   345
apply (frule (1) choose_lemma, simp)
huffman@27411
   346
done
huffman@27411
   347
huffman@27411
   348
lemma choose_in: "\<lbrakk>finite A; A \<noteq> {}\<rbrakk> \<Longrightarrow> choose A \<in> A"
huffman@27411
   349
by (frule (1) choose_lemma, simp)
huffman@27411
   350
huffman@27411
   351
function
huffman@27411
   352
  choose_pos :: "'a compact_basis set \<Rightarrow> 'a compact_basis \<Rightarrow> nat"
huffman@27411
   353
where
huffman@27411
   354
  "choose_pos A x =
huffman@27411
   355
    (if finite A \<and> x \<in> A \<and> x \<noteq> choose A
huffman@27411
   356
      then Suc (choose_pos (A - {choose A}) x) else 0)"
huffman@27411
   357
by auto
huffman@27411
   358
huffman@27411
   359
termination choose_pos
huffman@27411
   360
apply (relation "measure (card \<circ> fst)", simp)
huffman@27411
   361
apply clarsimp
huffman@27411
   362
apply (rule card_Diff1_less)
huffman@27411
   363
apply assumption
huffman@27411
   364
apply (erule choose_in)
huffman@27411
   365
apply clarsimp
huffman@27411
   366
done
huffman@27411
   367
huffman@27411
   368
declare choose_pos.simps [simp del]
huffman@27411
   369
huffman@27411
   370
lemma choose_pos_choose: "finite A \<Longrightarrow> choose_pos A (choose A) = 0"
huffman@27411
   371
by (simp add: choose_pos.simps)
huffman@27411
   372
huffman@27411
   373
lemma inj_on_choose_pos [OF refl]:
huffman@27411
   374
  "\<lbrakk>card A = n; finite A\<rbrakk> \<Longrightarrow> inj_on (choose_pos A) A"
huffman@27411
   375
 apply (induct n arbitrary: A)
huffman@27411
   376
  apply simp
huffman@27411
   377
 apply (case_tac "A = {}", simp)
huffman@27411
   378
 apply (frule (1) choose_in)
huffman@27411
   379
 apply (rule inj_onI)
huffman@27411
   380
 apply (drule_tac x="A - {choose A}" in meta_spec, simp)
huffman@27411
   381
 apply (simp add: choose_pos.simps)
nipkow@62390
   382
 apply (simp split: if_split_asm)
huffman@27411
   383
 apply (erule (1) inj_onD, simp, simp)
huffman@27411
   384
done
huffman@27411
   385
huffman@27411
   386
lemma choose_pos_bounded [OF refl]:
huffman@27411
   387
  "\<lbrakk>card A = n; finite A; x \<in> A\<rbrakk> \<Longrightarrow> choose_pos A x < n"
huffman@27411
   388
apply (induct n arbitrary: A)
huffman@27411
   389
apply simp
huffman@27411
   390
 apply (case_tac "A = {}", simp)
huffman@27411
   391
 apply (frule (1) choose_in)
huffman@27411
   392
apply (subst choose_pos.simps)
huffman@27411
   393
apply simp
huffman@27411
   394
done
huffman@27411
   395
huffman@27411
   396
lemma choose_pos_lessD:
huffman@41182
   397
  "\<lbrakk>choose_pos A x < choose_pos A y; finite A; x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> x \<notsqsubseteq> y"
huffman@27411
   398
 apply (induct A x arbitrary: y rule: choose_pos.induct)
huffman@27411
   399
 apply simp
huffman@27411
   400
 apply (case_tac "x = choose A")
huffman@27411
   401
  apply simp
huffman@27411
   402
  apply (rule notI)
huffman@27411
   403
  apply (frule (2) maximal_choose)
huffman@27411
   404
  apply simp
huffman@27411
   405
 apply (case_tac "y = choose A")
huffman@27411
   406
  apply (simp add: choose_pos_choose)
huffman@27411
   407
 apply (drule_tac x=y in meta_spec)
huffman@27411
   408
 apply simp
huffman@27411
   409
 apply (erule meta_mp)
huffman@27411
   410
 apply (simp add: choose_pos.simps)
huffman@27411
   411
done
huffman@27411
   412
wenzelm@62175
   413
subsubsection \<open>Compact basis take function\<close>
huffman@27411
   414
huffman@27411
   415
primrec
huffman@39974
   416
  cb_take :: "nat \<Rightarrow> 'a compact_basis \<Rightarrow> 'a compact_basis" where
huffman@27411
   417
  "cb_take 0 = (\<lambda>x. compact_bot)"
huffman@39974
   418
| "cb_take (Suc n) = (\<lambda>a. Abs_compact_basis (approx n\<cdot>(Rep_compact_basis a)))"
huffman@39974
   419
huffman@39974
   420
declare cb_take.simps [simp del]
huffman@39974
   421
huffman@39974
   422
lemma cb_take_zero [simp]: "cb_take 0 a = compact_bot"
huffman@39974
   423
by (simp only: cb_take.simps)
huffman@39974
   424
huffman@39974
   425
lemma Rep_cb_take:
huffman@39974
   426
  "Rep_compact_basis (cb_take (Suc n) a) = approx n\<cdot>(Rep_compact_basis a)"
huffman@41370
   427
by (simp add: cb_take.simps(2))
huffman@39974
   428
huffman@39974
   429
lemmas approx_Rep_compact_basis = Rep_cb_take [symmetric]
huffman@27411
   430
huffman@27411
   431
lemma cb_take_covers: "\<exists>n. cb_take n x = x"
huffman@39974
   432
apply (subgoal_tac "\<exists>n. cb_take (Suc n) x = x", fast)
huffman@39974
   433
apply (simp add: Rep_compact_basis_inject [symmetric])
huffman@39974
   434
apply (simp add: Rep_cb_take)
huffman@39974
   435
apply (rule compact_eq_approx)
huffman@41370
   436
apply (rule Rep_compact_basis')
huffman@27411
   437
done
huffman@27411
   438
huffman@27411
   439
lemma cb_take_less: "cb_take n x \<sqsubseteq> x"
huffman@39974
   440
unfolding compact_le_def
huffman@39974
   441
by (cases n, simp, simp add: Rep_cb_take approx_below)
huffman@27411
   442
huffman@27411
   443
lemma cb_take_idem: "cb_take n (cb_take n x) = cb_take n x"
huffman@39974
   444
unfolding Rep_compact_basis_inject [symmetric]
huffman@39974
   445
by (cases n, simp, simp add: Rep_cb_take approx_idem)
huffman@27411
   446
huffman@27411
   447
lemma cb_take_mono: "x \<sqsubseteq> y \<Longrightarrow> cb_take n x \<sqsubseteq> cb_take n y"
huffman@39974
   448
unfolding compact_le_def
huffman@39974
   449
by (cases n, simp, simp add: Rep_cb_take monofun_cfun_arg)
huffman@27411
   450
huffman@27411
   451
lemma cb_take_chain_le: "m \<le> n \<Longrightarrow> cb_take m x \<sqsubseteq> cb_take n x"
huffman@39974
   452
unfolding compact_le_def
huffman@39974
   453
apply (cases m, simp, cases n, simp)
huffman@39974
   454
apply (simp add: Rep_cb_take, rule chain_mono, simp, simp)
huffman@27411
   455
done
huffman@27411
   456
huffman@27411
   457
lemma finite_range_cb_take: "finite (range (cb_take n))"
huffman@27411
   458
apply (cases n)
huffman@39974
   459
apply (subgoal_tac "range (cb_take 0) = {compact_bot}", simp, force)
huffman@39974
   460
apply (rule finite_imageD [where f="Rep_compact_basis"])
huffman@39974
   461
apply (rule finite_subset [where B="range (\<lambda>x. approx (n - 1)\<cdot>x)"])
huffman@39974
   462
apply (clarsimp simp add: Rep_cb_take)
huffman@39974
   463
apply (rule finite_range_approx)
huffman@39974
   464
apply (rule inj_onI, simp add: Rep_compact_basis_inject)
huffman@27411
   465
done
huffman@27411
   466
wenzelm@62175
   467
subsubsection \<open>Rank of basis elements\<close>
huffman@39974
   468
huffman@27411
   469
definition
huffman@27411
   470
  rank :: "'a compact_basis \<Rightarrow> nat"
huffman@27411
   471
where
huffman@27411
   472
  "rank x = (LEAST n. cb_take n x = x)"
huffman@27411
   473
huffman@27411
   474
lemma compact_approx_rank: "cb_take (rank x) x = x"
huffman@27411
   475
unfolding rank_def
huffman@27411
   476
apply (rule LeastI_ex)
huffman@27411
   477
apply (rule cb_take_covers)
huffman@27411
   478
done
huffman@27411
   479
huffman@27411
   480
lemma rank_leD: "rank x \<le> n \<Longrightarrow> cb_take n x = x"
huffman@31076
   481
apply (rule below_antisym [OF cb_take_less])
huffman@27411
   482
apply (subst compact_approx_rank [symmetric])
huffman@27411
   483
apply (erule cb_take_chain_le)
huffman@27411
   484
done
huffman@27411
   485
huffman@27411
   486
lemma rank_leI: "cb_take n x = x \<Longrightarrow> rank x \<le> n"
huffman@27411
   487
unfolding rank_def by (rule Least_le)
huffman@27411
   488
huffman@27411
   489
lemma rank_le_iff: "rank x \<le> n \<longleftrightarrow> cb_take n x = x"
huffman@27411
   490
by (rule iffI [OF rank_leD rank_leI])
huffman@27411
   491
huffman@30505
   492
lemma rank_compact_bot [simp]: "rank compact_bot = 0"
huffman@30505
   493
using rank_leI [of 0 compact_bot] by simp
huffman@30505
   494
huffman@30505
   495
lemma rank_eq_0_iff [simp]: "rank x = 0 \<longleftrightarrow> x = compact_bot"
huffman@30505
   496
using rank_le_iff [of x 0] by auto
huffman@30505
   497
huffman@27411
   498
definition
huffman@27411
   499
  rank_le :: "'a compact_basis \<Rightarrow> 'a compact_basis set"
huffman@27411
   500
where
huffman@27411
   501
  "rank_le x = {y. rank y \<le> rank x}"
huffman@27411
   502
huffman@27411
   503
definition
huffman@27411
   504
  rank_lt :: "'a compact_basis \<Rightarrow> 'a compact_basis set"
huffman@27411
   505
where
huffman@27411
   506
  "rank_lt x = {y. rank y < rank x}"
huffman@27411
   507
huffman@27411
   508
definition
huffman@27411
   509
  rank_eq :: "'a compact_basis \<Rightarrow> 'a compact_basis set"
huffman@27411
   510
where
huffman@27411
   511
  "rank_eq x = {y. rank y = rank x}"
huffman@27411
   512
huffman@27411
   513
lemma rank_eq_cong: "rank x = rank y \<Longrightarrow> rank_eq x = rank_eq y"
huffman@27411
   514
unfolding rank_eq_def by simp
huffman@27411
   515
huffman@27411
   516
lemma rank_lt_cong: "rank x = rank y \<Longrightarrow> rank_lt x = rank_lt y"
huffman@27411
   517
unfolding rank_lt_def by simp
huffman@27411
   518
huffman@27411
   519
lemma rank_eq_subset: "rank_eq x \<subseteq> rank_le x"
huffman@27411
   520
unfolding rank_eq_def rank_le_def by auto
huffman@27411
   521
huffman@27411
   522
lemma rank_lt_subset: "rank_lt x \<subseteq> rank_le x"
huffman@27411
   523
unfolding rank_lt_def rank_le_def by auto
huffman@27411
   524
huffman@27411
   525
lemma finite_rank_le: "finite (rank_le x)"
huffman@27411
   526
unfolding rank_le_def
huffman@27411
   527
apply (rule finite_subset [where B="range (cb_take (rank x))"])
huffman@27411
   528
apply clarify
huffman@27411
   529
apply (rule range_eqI)
huffman@27411
   530
apply (erule rank_leD [symmetric])
huffman@27411
   531
apply (rule finite_range_cb_take)
huffman@27411
   532
done
huffman@27411
   533
huffman@27411
   534
lemma finite_rank_eq: "finite (rank_eq x)"
huffman@27411
   535
by (rule finite_subset [OF rank_eq_subset finite_rank_le])
huffman@27411
   536
huffman@27411
   537
lemma finite_rank_lt: "finite (rank_lt x)"
huffman@27411
   538
by (rule finite_subset [OF rank_lt_subset finite_rank_le])
huffman@27411
   539
huffman@27411
   540
lemma rank_lt_Int_rank_eq: "rank_lt x \<inter> rank_eq x = {}"
huffman@27411
   541
unfolding rank_lt_def rank_eq_def rank_le_def by auto
huffman@27411
   542
huffman@27411
   543
lemma rank_lt_Un_rank_eq: "rank_lt x \<union> rank_eq x = rank_le x"
huffman@27411
   544
unfolding rank_lt_def rank_eq_def rank_le_def by auto
huffman@27411
   545
wenzelm@62175
   546
subsubsection \<open>Sequencing basis elements\<close>
huffman@27411
   547
huffman@27411
   548
definition
huffman@30505
   549
  place :: "'a compact_basis \<Rightarrow> nat"
huffman@27411
   550
where
huffman@30505
   551
  "place x = card (rank_lt x) + choose_pos (rank_eq x) x"
huffman@27411
   552
huffman@30505
   553
lemma place_bounded: "place x < card (rank_le x)"
huffman@30505
   554
unfolding place_def
huffman@27411
   555
 apply (rule ord_less_eq_trans)
huffman@27411
   556
  apply (rule add_strict_left_mono)
huffman@27411
   557
  apply (rule choose_pos_bounded)
huffman@27411
   558
   apply (rule finite_rank_eq)
huffman@27411
   559
  apply (simp add: rank_eq_def)
huffman@27411
   560
 apply (subst card_Un_disjoint [symmetric])
huffman@27411
   561
    apply (rule finite_rank_lt)
huffman@27411
   562
   apply (rule finite_rank_eq)
huffman@27411
   563
  apply (rule rank_lt_Int_rank_eq)
huffman@27411
   564
 apply (simp add: rank_lt_Un_rank_eq)
huffman@27411
   565
done
huffman@27411
   566
huffman@30505
   567
lemma place_ge: "card (rank_lt x) \<le> place x"
huffman@30505
   568
unfolding place_def by simp
huffman@27411
   569
huffman@30505
   570
lemma place_rank_mono:
huffman@27411
   571
  fixes x y :: "'a compact_basis"
huffman@30505
   572
  shows "rank x < rank y \<Longrightarrow> place x < place y"
huffman@30505
   573
apply (rule less_le_trans [OF place_bounded])
huffman@30505
   574
apply (rule order_trans [OF _ place_ge])
huffman@27411
   575
apply (rule card_mono)
huffman@27411
   576
apply (rule finite_rank_lt)
huffman@27411
   577
apply (simp add: rank_le_def rank_lt_def subset_eq)
huffman@27411
   578
done
huffman@27411
   579
huffman@30505
   580
lemma place_eqD: "place x = place y \<Longrightarrow> x = y"
huffman@27411
   581
 apply (rule linorder_cases [where x="rank x" and y="rank y"])
huffman@30505
   582
   apply (drule place_rank_mono, simp)
huffman@30505
   583
  apply (simp add: place_def)
huffman@27411
   584
  apply (rule inj_on_choose_pos [where A="rank_eq x", THEN inj_onD])
huffman@27411
   585
     apply (rule finite_rank_eq)
huffman@27411
   586
    apply (simp cong: rank_lt_cong rank_eq_cong)
huffman@27411
   587
   apply (simp add: rank_eq_def)
huffman@27411
   588
  apply (simp add: rank_eq_def)
huffman@30505
   589
 apply (drule place_rank_mono, simp)
huffman@27411
   590
done
huffman@27411
   591
huffman@30505
   592
lemma inj_place: "inj place"
huffman@30505
   593
by (rule inj_onI, erule place_eqD)
huffman@27411
   594
wenzelm@62175
   595
subsubsection \<open>Embedding and projection on basis elements\<close>
huffman@27411
   596
huffman@30505
   597
definition
huffman@30505
   598
  sub :: "'a compact_basis \<Rightarrow> 'a compact_basis"
huffman@30505
   599
where
huffman@30505
   600
  "sub x = (case rank x of 0 \<Rightarrow> compact_bot | Suc k \<Rightarrow> cb_take k x)"
huffman@30505
   601
huffman@30505
   602
lemma rank_sub_less: "x \<noteq> compact_bot \<Longrightarrow> rank (sub x) < rank x"
huffman@30505
   603
unfolding sub_def
huffman@30505
   604
apply (cases "rank x", simp)
huffman@30505
   605
apply (simp add: less_Suc_eq_le)
huffman@30505
   606
apply (rule rank_leI)
huffman@30505
   607
apply (rule cb_take_idem)
huffman@30505
   608
done
huffman@30505
   609
huffman@30505
   610
lemma place_sub_less: "x \<noteq> compact_bot \<Longrightarrow> place (sub x) < place x"
huffman@30505
   611
apply (rule place_rank_mono)
huffman@30505
   612
apply (erule rank_sub_less)
huffman@30505
   613
done
huffman@30505
   614
huffman@30505
   615
lemma sub_below: "sub x \<sqsubseteq> x"
huffman@30505
   616
unfolding sub_def by (cases "rank x", simp_all add: cb_take_less)
huffman@30505
   617
huffman@30505
   618
lemma rank_less_imp_below_sub: "\<lbrakk>x \<sqsubseteq> y; rank x < rank y\<rbrakk> \<Longrightarrow> x \<sqsubseteq> sub y"
huffman@30505
   619
unfolding sub_def
huffman@30505
   620
apply (cases "rank y", simp)
huffman@30505
   621
apply (simp add: less_Suc_eq_le)
huffman@30505
   622
apply (subgoal_tac "cb_take nat x \<sqsubseteq> cb_take nat y")
huffman@30505
   623
apply (simp add: rank_leD)
huffman@30505
   624
apply (erule cb_take_mono)
huffman@30505
   625
done
huffman@30505
   626
huffman@27411
   627
function
huffman@27411
   628
  basis_emb :: "'a compact_basis \<Rightarrow> ubasis"
huffman@27411
   629
where
huffman@27411
   630
  "basis_emb x = (if x = compact_bot then 0 else
huffman@30505
   631
    node (place x) (basis_emb (sub x))
huffman@30505
   632
      (basis_emb ` {y. place y < place x \<and> x \<sqsubseteq> y}))"
huffman@27411
   633
by auto
huffman@27411
   634
huffman@27411
   635
termination basis_emb
huffman@30505
   636
apply (relation "measure place", simp)
huffman@30505
   637
apply (simp add: place_sub_less)
huffman@27411
   638
apply simp
huffman@27411
   639
done
huffman@27411
   640
huffman@27411
   641
declare basis_emb.simps [simp del]
huffman@27411
   642
huffman@27411
   643
lemma basis_emb_compact_bot [simp]: "basis_emb compact_bot = 0"
huffman@27411
   644
by (simp add: basis_emb.simps)
huffman@27411
   645
huffman@30505
   646
lemma fin1: "finite {y. place y < place x \<and> x \<sqsubseteq> y}"
huffman@27411
   647
apply (subst Collect_conj_eq)
huffman@27411
   648
apply (rule finite_Int)
huffman@27411
   649
apply (rule disjI1)
huffman@30505
   650
apply (subgoal_tac "finite (place -` {n. n < place x})", simp)
huffman@30505
   651
apply (rule finite_vimageI [OF _ inj_place])
huffman@27411
   652
apply (simp add: lessThan_def [symmetric])
huffman@27411
   653
done
huffman@27411
   654
huffman@30505
   655
lemma fin2: "finite (basis_emb ` {y. place y < place x \<and> x \<sqsubseteq> y})"
huffman@27411
   656
by (rule finite_imageI [OF fin1])
huffman@27411
   657
huffman@30505
   658
lemma rank_place_mono:
huffman@30505
   659
  "\<lbrakk>place x < place y; x \<sqsubseteq> y\<rbrakk> \<Longrightarrow> rank x < rank y"
huffman@30505
   660
apply (rule linorder_cases, assumption)
huffman@30505
   661
apply (simp add: place_def cong: rank_lt_cong rank_eq_cong)
huffman@30505
   662
apply (drule choose_pos_lessD)
huffman@30505
   663
apply (rule finite_rank_eq)
huffman@30505
   664
apply (simp add: rank_eq_def)
huffman@30505
   665
apply (simp add: rank_eq_def)
huffman@30505
   666
apply simp
huffman@30505
   667
apply (drule place_rank_mono, simp)
huffman@30505
   668
done
huffman@30505
   669
huffman@30505
   670
lemma basis_emb_mono:
huffman@30505
   671
  "x \<sqsubseteq> y \<Longrightarrow> ubasis_le (basis_emb x) (basis_emb y)"
berghofe@34915
   672
proof (induct "max (place x) (place y)" arbitrary: x y rule: less_induct)
berghofe@34915
   673
  case less
huffman@30505
   674
  show ?case proof (rule linorder_cases)
huffman@30505
   675
    assume "place x < place y"
huffman@30505
   676
    then have "rank x < rank y"
wenzelm@62175
   677
      using \<open>x \<sqsubseteq> y\<close> by (rule rank_place_mono)
wenzelm@62175
   678
    with \<open>place x < place y\<close> show ?case
huffman@30505
   679
      apply (case_tac "y = compact_bot", simp)
huffman@30505
   680
      apply (simp add: basis_emb.simps [of y])
huffman@30505
   681
      apply (rule ubasis_le_trans [OF _ ubasis_le_lower [OF fin2]])
berghofe@34915
   682
      apply (rule less)
huffman@30505
   683
       apply (simp add: less_max_iff_disj)
huffman@30505
   684
       apply (erule place_sub_less)
wenzelm@62175
   685
      apply (erule rank_less_imp_below_sub [OF \<open>x \<sqsubseteq> y\<close>])
huffman@27411
   686
      done
huffman@30505
   687
  next
huffman@30505
   688
    assume "place x = place y"
huffman@30505
   689
    hence "x = y" by (rule place_eqD)
huffman@30505
   690
    thus ?case by (simp add: ubasis_le_refl)
huffman@30505
   691
  next
huffman@30505
   692
    assume "place x > place y"
wenzelm@62175
   693
    with \<open>x \<sqsubseteq> y\<close> show ?case
huffman@30505
   694
      apply (case_tac "x = compact_bot", simp add: ubasis_le_minimal)
huffman@30505
   695
      apply (simp add: basis_emb.simps [of x])
huffman@30505
   696
      apply (rule ubasis_le_upper [OF fin2], simp)
berghofe@34915
   697
      apply (rule less)
huffman@30505
   698
       apply (simp add: less_max_iff_disj)
huffman@30505
   699
       apply (erule place_sub_less)
huffman@31076
   700
      apply (erule rev_below_trans)
huffman@30505
   701
      apply (rule sub_below)
huffman@30505
   702
      done
huffman@27411
   703
  qed
huffman@27411
   704
qed
huffman@27411
   705
huffman@27411
   706
lemma inj_basis_emb: "inj basis_emb"
huffman@27411
   707
 apply (rule inj_onI)
huffman@27411
   708
 apply (case_tac "x = compact_bot")
huffman@27411
   709
  apply (case_tac [!] "y = compact_bot")
huffman@27411
   710
    apply simp
huffman@27411
   711
   apply (simp add: basis_emb.simps)
huffman@27411
   712
  apply (simp add: basis_emb.simps)
huffman@27411
   713
 apply (simp add: basis_emb.simps)
huffman@30505
   714
 apply (simp add: fin2 inj_eq [OF inj_place])
huffman@27411
   715
done
huffman@27411
   716
huffman@27411
   717
definition
huffman@30505
   718
  basis_prj :: "ubasis \<Rightarrow> 'a compact_basis"
huffman@27411
   719
where
huffman@27411
   720
  "basis_prj x = inv basis_emb
huffman@30505
   721
    (ubasis_until (\<lambda>x. x \<in> range (basis_emb :: 'a compact_basis \<Rightarrow> ubasis)) x)"
huffman@27411
   722
huffman@27411
   723
lemma basis_prj_basis_emb: "\<And>x. basis_prj (basis_emb x) = x"
huffman@27411
   724
unfolding basis_prj_def
huffman@27411
   725
 apply (subst ubasis_until_same)
huffman@27411
   726
  apply (rule rangeI)
huffman@27411
   727
 apply (rule inv_f_f)
huffman@27411
   728
 apply (rule inj_basis_emb)
huffman@27411
   729
done
huffman@27411
   730
huffman@27411
   731
lemma basis_prj_node:
huffman@30505
   732
  "\<lbrakk>finite S; node i a S \<notin> range (basis_emb :: 'a compact_basis \<Rightarrow> nat)\<rbrakk>
huffman@30505
   733
    \<Longrightarrow> basis_prj (node i a S) = (basis_prj a :: 'a compact_basis)"
huffman@27411
   734
unfolding basis_prj_def by simp
huffman@27411
   735
huffman@27411
   736
lemma basis_prj_0: "basis_prj 0 = compact_bot"
huffman@27411
   737
apply (subst basis_emb_compact_bot [symmetric])
huffman@27411
   738
apply (rule basis_prj_basis_emb)
huffman@27411
   739
done
huffman@27411
   740
huffman@30505
   741
lemma node_eq_basis_emb_iff:
huffman@30505
   742
  "finite S \<Longrightarrow> node i a S = basis_emb x \<longleftrightarrow>
huffman@30505
   743
    x \<noteq> compact_bot \<and> i = place x \<and> a = basis_emb (sub x) \<and>
huffman@30505
   744
        S = basis_emb ` {y. place y < place x \<and> x \<sqsubseteq> y}"
huffman@30505
   745
apply (cases "x = compact_bot", simp)
huffman@30505
   746
apply (simp add: basis_emb.simps [of x])
huffman@30505
   747
apply (simp add: fin2)
huffman@27411
   748
done
huffman@27411
   749
huffman@30505
   750
lemma basis_prj_mono: "ubasis_le a b \<Longrightarrow> basis_prj a \<sqsubseteq> basis_prj b"
huffman@30505
   751
proof (induct a b rule: ubasis_le.induct)
huffman@31076
   752
  case (ubasis_le_refl a) show ?case by (rule below_refl)
huffman@30505
   753
next
huffman@31076
   754
  case (ubasis_le_trans a b c) thus ?case by - (rule below_trans)
huffman@30505
   755
next
huffman@30505
   756
  case (ubasis_le_lower S a i) thus ?case
huffman@30561
   757
    apply (cases "node i a S \<in> range (basis_emb :: 'a compact_basis \<Rightarrow> nat)")
huffman@30505
   758
     apply (erule rangeE, rename_tac x)
huffman@30505
   759
     apply (simp add: basis_prj_basis_emb)
huffman@30505
   760
     apply (simp add: node_eq_basis_emb_iff)
huffman@30505
   761
     apply (simp add: basis_prj_basis_emb)
huffman@30505
   762
     apply (rule sub_below)
huffman@30505
   763
    apply (simp add: basis_prj_node)
huffman@30505
   764
    done
huffman@30505
   765
next
huffman@30505
   766
  case (ubasis_le_upper S b a i) thus ?case
huffman@30561
   767
    apply (cases "node i a S \<in> range (basis_emb :: 'a compact_basis \<Rightarrow> nat)")
huffman@30505
   768
     apply (erule rangeE, rename_tac x)
huffman@30505
   769
     apply (simp add: basis_prj_basis_emb)
huffman@30505
   770
     apply (clarsimp simp add: node_eq_basis_emb_iff)
huffman@30505
   771
     apply (simp add: basis_prj_basis_emb)
huffman@30505
   772
    apply (simp add: basis_prj_node)
huffman@30505
   773
    done
huffman@30505
   774
qed
huffman@30505
   775
huffman@27411
   776
lemma basis_emb_prj_less: "ubasis_le (basis_emb (basis_prj x)) x"
huffman@27411
   777
unfolding basis_prj_def
wenzelm@33071
   778
 apply (subst f_inv_into_f [where f=basis_emb])
huffman@27411
   779
  apply (rule ubasis_until)
huffman@27411
   780
  apply (rule range_eqI [where x=compact_bot])
huffman@27411
   781
  apply simp
huffman@27411
   782
 apply (rule ubasis_until_less)
huffman@27411
   783
done
huffman@27411
   784
huffman@41286
   785
lemma ideal_completion:
huffman@41286
   786
  "ideal_completion below Rep_compact_basis (approximants :: 'a \<Rightarrow> _)"
huffman@39974
   787
proof
huffman@39974
   788
  fix w :: "'a"
huffman@39974
   789
  show "below.ideal (approximants w)"
huffman@39974
   790
  proof (rule below.idealI)
huffman@41370
   791
    have "Abs_compact_basis (approx 0\<cdot>w) \<in> approximants w"
huffman@41370
   792
      by (simp add: approximants_def approx_below)
huffman@41370
   793
    thus "\<exists>x. x \<in> approximants w" ..
huffman@39974
   794
  next
huffman@39974
   795
    fix x y :: "'a compact_basis"
huffman@41370
   796
    assume x: "x \<in> approximants w" and y: "y \<in> approximants w"
huffman@41370
   797
    obtain i where i: "approx i\<cdot>(Rep_compact_basis x) = Rep_compact_basis x"
huffman@41370
   798
      using compact_eq_approx Rep_compact_basis' by fast
huffman@41370
   799
    obtain j where j: "approx j\<cdot>(Rep_compact_basis y) = Rep_compact_basis y"
huffman@41370
   800
      using compact_eq_approx Rep_compact_basis' by fast
huffman@41370
   801
    let ?z = "Abs_compact_basis (approx (max i j)\<cdot>w)"
huffman@41370
   802
    have "?z \<in> approximants w"
huffman@41370
   803
      by (simp add: approximants_def approx_below)
huffman@41370
   804
    moreover from x y have "x \<sqsubseteq> ?z \<and> y \<sqsubseteq> ?z"
huffman@41370
   805
      by (simp add: approximants_def compact_le_def)
haftmann@54863
   806
         (metis i j monofun_cfun chain_mono chain_approx max.cobounded1 max.cobounded2)
huffman@41370
   807
    ultimately show "\<exists>z \<in> approximants w. x \<sqsubseteq> z \<and> y \<sqsubseteq> z" ..
huffman@39974
   808
  next
huffman@39974
   809
    fix x y :: "'a compact_basis"
huffman@39974
   810
    assume "x \<sqsubseteq> y" "y \<in> approximants w" thus "x \<in> approximants w"
huffman@41370
   811
      unfolding approximants_def compact_le_def
huffman@41370
   812
      by (auto elim: below_trans)
huffman@39974
   813
  qed
huffman@39974
   814
next
huffman@39974
   815
  fix Y :: "nat \<Rightarrow> 'a"
huffman@41370
   816
  assume "chain Y"
huffman@41370
   817
  thus "approximants (\<Squnion>i. Y i) = (\<Union>i. approximants (Y i))"
huffman@39974
   818
    unfolding approximants_def
huffman@41370
   819
    by (auto simp add: compact_below_lub_iff)
huffman@39974
   820
next
huffman@39974
   821
  fix a :: "'a compact_basis"
huffman@39974
   822
  show "approximants (Rep_compact_basis a) = {b. b \<sqsubseteq> a}"
huffman@39974
   823
    unfolding approximants_def compact_le_def ..
huffman@39974
   824
next
huffman@39974
   825
  fix x y :: "'a"
huffman@41370
   826
  assume "approximants x \<subseteq> approximants y"
huffman@41370
   827
  hence "\<forall>z. compact z \<longrightarrow> z \<sqsubseteq> x \<longrightarrow> z \<sqsubseteq> y"
huffman@41370
   828
    by (simp add: approximants_def subset_eq)
huffman@41370
   829
       (metis Abs_compact_basis_inverse')
huffman@41370
   830
  hence "(\<Squnion>i. approx i\<cdot>x) \<sqsubseteq> y"
huffman@41370
   831
    by (simp add: lub_below approx_below)
huffman@41370
   832
  thus "x \<sqsubseteq> y"
huffman@41370
   833
    by (simp add: lub_distribs)
huffman@39974
   834
next
huffman@39974
   835
  show "\<exists>f::'a compact_basis \<Rightarrow> nat. inj f"
huffman@39974
   836
    by (rule exI, rule inj_place)
huffman@39974
   837
qed
huffman@27411
   838
huffman@41286
   839
end
huffman@41286
   840
wenzelm@61605
   841
interpretation compact_basis:
huffman@41286
   842
  ideal_completion below Rep_compact_basis
huffman@41286
   843
    "approximants :: 'a::bifinite \<Rightarrow> 'a compact_basis set"
huffman@41286
   844
proof -
huffman@41286
   845
  obtain a :: "nat \<Rightarrow> 'a \<rightarrow> 'a" where "approx_chain a"
huffman@41286
   846
    using bifinite ..
huffman@41286
   847
  hence "bifinite_approx_chain a"
huffman@41286
   848
    unfolding bifinite_approx_chain_def .
huffman@41286
   849
  thus "ideal_completion below Rep_compact_basis (approximants :: 'a \<Rightarrow> _)"
huffman@41286
   850
    by (rule bifinite_approx_chain.ideal_completion)
huffman@41286
   851
qed
huffman@41286
   852
wenzelm@62175
   853
subsubsection \<open>EP-pair from any bifinite domain into \emph{udom}\<close>
huffman@27411
   854
huffman@41286
   855
context bifinite_approx_chain begin
huffman@39974
   856
huffman@27411
   857
definition
huffman@39974
   858
  udom_emb :: "'a \<rightarrow> udom"
huffman@27411
   859
where
huffman@41394
   860
  "udom_emb = compact_basis.extension (\<lambda>x. udom_principal (basis_emb x))"
huffman@27411
   861
huffman@27411
   862
definition
huffman@39974
   863
  udom_prj :: "udom \<rightarrow> 'a"
huffman@27411
   864
where
huffman@41394
   865
  "udom_prj = udom.extension (\<lambda>x. Rep_compact_basis (basis_prj x))"
huffman@27411
   866
huffman@27411
   867
lemma udom_emb_principal:
huffman@27411
   868
  "udom_emb\<cdot>(Rep_compact_basis x) = udom_principal (basis_emb x)"
huffman@27411
   869
unfolding udom_emb_def
huffman@41394
   870
apply (rule compact_basis.extension_principal)
huffman@27411
   871
apply (rule udom.principal_mono)
huffman@27411
   872
apply (erule basis_emb_mono)
huffman@27411
   873
done
huffman@27411
   874
huffman@27411
   875
lemma udom_prj_principal:
huffman@27411
   876
  "udom_prj\<cdot>(udom_principal x) = Rep_compact_basis (basis_prj x)"
huffman@27411
   877
unfolding udom_prj_def
huffman@41394
   878
apply (rule udom.extension_principal)
huffman@27411
   879
apply (rule compact_basis.principal_mono)
huffman@27411
   880
apply (erule basis_prj_mono)
huffman@27411
   881
done
huffman@27411
   882
huffman@27411
   883
lemma ep_pair_udom: "ep_pair udom_emb udom_prj"
wenzelm@61169
   884
 apply standard
huffman@27411
   885
  apply (rule compact_basis.principal_induct, simp)
huffman@27411
   886
  apply (simp add: udom_emb_principal udom_prj_principal)
huffman@27411
   887
  apply (simp add: basis_prj_basis_emb)
huffman@27411
   888
 apply (rule udom.principal_induct, simp)
huffman@27411
   889
 apply (simp add: udom_emb_principal udom_prj_principal)
huffman@27411
   890
 apply (rule basis_emb_prj_less)
huffman@27411
   891
done
huffman@27411
   892
huffman@27411
   893
end
huffman@39974
   894
huffman@41286
   895
abbreviation "udom_emb \<equiv> bifinite_approx_chain.udom_emb"
huffman@41286
   896
abbreviation "udom_prj \<equiv> bifinite_approx_chain.udom_prj"
huffman@39974
   897
huffman@41286
   898
lemmas ep_pair_udom =
huffman@41286
   899
  bifinite_approx_chain.ep_pair_udom [unfolded bifinite_approx_chain_def]
huffman@39974
   900
wenzelm@62175
   901
subsection \<open>Chain of approx functions for type \emph{udom}\<close>
huffman@39974
   902
huffman@39974
   903
definition
huffman@39974
   904
  udom_approx :: "nat \<Rightarrow> udom \<rightarrow> udom"
huffman@39974
   905
where
huffman@39974
   906
  "udom_approx i =
huffman@41394
   907
    udom.extension (\<lambda>x. udom_principal (ubasis_until (\<lambda>y. y \<le> i) x))"
huffman@39974
   908
huffman@39974
   909
lemma udom_approx_mono:
huffman@39974
   910
  "ubasis_le a b \<Longrightarrow>
huffman@39974
   911
    udom_principal (ubasis_until (\<lambda>y. y \<le> i) a) \<sqsubseteq>
huffman@39974
   912
    udom_principal (ubasis_until (\<lambda>y. y \<le> i) b)"
huffman@39974
   913
apply (rule udom.principal_mono)
huffman@39974
   914
apply (rule ubasis_until_mono)
huffman@39974
   915
apply (frule (2) order_less_le_trans [OF node_gt2])
huffman@39974
   916
apply (erule order_less_imp_le)
huffman@39974
   917
apply assumption
huffman@39974
   918
done
huffman@39974
   919
huffman@39974
   920
lemma adm_mem_finite: "\<lbrakk>cont f; finite S\<rbrakk> \<Longrightarrow> adm (\<lambda>x. f x \<in> S)"
huffman@39974
   921
by (erule adm_subst, induct set: finite, simp_all)
huffman@39974
   922
huffman@39974
   923
lemma udom_approx_principal:
huffman@39974
   924
  "udom_approx i\<cdot>(udom_principal x) =
huffman@39974
   925
    udom_principal (ubasis_until (\<lambda>y. y \<le> i) x)"
huffman@39974
   926
unfolding udom_approx_def
huffman@41394
   927
apply (rule udom.extension_principal)
huffman@39974
   928
apply (erule udom_approx_mono)
huffman@39974
   929
done
huffman@39974
   930
huffman@39974
   931
lemma finite_deflation_udom_approx: "finite_deflation (udom_approx i)"
huffman@39974
   932
proof
huffman@39974
   933
  fix x show "udom_approx i\<cdot>(udom_approx i\<cdot>x) = udom_approx i\<cdot>x"
huffman@39974
   934
    by (induct x rule: udom.principal_induct, simp)
huffman@39974
   935
       (simp add: udom_approx_principal ubasis_until_idem)
huffman@39974
   936
next
huffman@39974
   937
  fix x show "udom_approx i\<cdot>x \<sqsubseteq> x"
huffman@39974
   938
    by (induct x rule: udom.principal_induct, simp)
huffman@39974
   939
       (simp add: udom_approx_principal ubasis_until_less)
huffman@39974
   940
next
huffman@39974
   941
  have *: "finite (range (\<lambda>x. udom_principal (ubasis_until (\<lambda>y. y \<le> i) x)))"
huffman@39974
   942
    apply (subst range_composition [where f=udom_principal])
huffman@39974
   943
    apply (simp add: finite_range_ubasis_until)
huffman@39974
   944
    done
huffman@39974
   945
  show "finite {x. udom_approx i\<cdot>x = x}"
huffman@39974
   946
    apply (rule finite_range_imp_finite_fixes)
huffman@39974
   947
    apply (rule rev_finite_subset [OF *])
huffman@39974
   948
    apply (clarsimp, rename_tac x)
huffman@39974
   949
    apply (induct_tac x rule: udom.principal_induct)
huffman@39974
   950
    apply (simp add: adm_mem_finite *)
huffman@39974
   951
    apply (simp add: udom_approx_principal)
huffman@39974
   952
    done
huffman@39974
   953
qed
huffman@39974
   954
huffman@39974
   955
interpretation udom_approx: finite_deflation "udom_approx i"
huffman@39974
   956
by (rule finite_deflation_udom_approx)
huffman@39974
   957
huffman@39974
   958
lemma chain_udom_approx [simp]: "chain (\<lambda>i. udom_approx i)"
huffman@39974
   959
unfolding udom_approx_def
huffman@39974
   960
apply (rule chainI)
huffman@41394
   961
apply (rule udom.extension_mono)
huffman@39974
   962
apply (erule udom_approx_mono)
huffman@39974
   963
apply (erule udom_approx_mono)
huffman@39974
   964
apply (rule udom.principal_mono)
huffman@39974
   965
apply (rule ubasis_until_chain, simp)
huffman@39974
   966
done
huffman@39974
   967
huffman@39974
   968
lemma lub_udom_approx [simp]: "(\<Squnion>i. udom_approx i) = ID"
huffman@40002
   969
apply (rule cfun_eqI, simp add: contlub_cfun_fun)
huffman@39974
   970
apply (rule below_antisym)
huffman@40500
   971
apply (rule lub_below)
huffman@39974
   972
apply (simp)
huffman@39974
   973
apply (rule udom_approx.below)
huffman@39974
   974
apply (rule_tac x=x in udom.principal_induct)
huffman@39974
   975
apply (simp add: lub_distribs)
huffman@40500
   976
apply (rule_tac i=a in below_lub)
huffman@39974
   977
apply simp
huffman@39974
   978
apply (simp add: udom_approx_principal)
huffman@39974
   979
apply (simp add: ubasis_until_same ubasis_le_refl)
huffman@39974
   980
done
wenzelm@65552
   981
huffman@41286
   982
lemma udom_approx [simp]: "approx_chain udom_approx"
huffman@39974
   983
proof
huffman@39974
   984
  show "chain (\<lambda>i. udom_approx i)"
huffman@39974
   985
    by (rule chain_udom_approx)
huffman@39974
   986
  show "(\<Squnion>i. udom_approx i) = ID"
huffman@39974
   987
    by (rule lub_udom_approx)
huffman@39974
   988
qed
huffman@39974
   989
huffman@41286
   990
instance udom :: bifinite
wenzelm@61169
   991
  by standard (fast intro: udom_approx)
huffman@41286
   992
huffman@39974
   993
hide_const (open) node
huffman@39974
   994
wenzelm@65552
   995
notation binomial  (infixl "choose" 65)
wenzelm@65552
   996
huffman@39974
   997
end