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