src/HOLCF/Completion.thy
author huffman
Sat, 27 Nov 2010 12:38:02 -0800
changeset 41017 3af9b0df3521
parent 40737 8e92772bc0e8
child 41019 1c6f7d4b110e
permissions -rw-r--r--
rename rep_contlub lemmas to rep_lub
     1 (*  Title:      HOLCF/Completion.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 header {* Defining algebraic domains by ideal completion *}
     6 
     7 theory Completion
     8 imports Plain_HOLCF
     9 begin
    10 
    11 subsection {* Ideals over a preorder *}
    12 
    13 locale preorder =
    14   fixes r :: "'a::type \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<preceq>" 50)
    15   assumes r_refl: "x \<preceq> x"
    16   assumes r_trans: "\<lbrakk>x \<preceq> y; y \<preceq> z\<rbrakk> \<Longrightarrow> x \<preceq> z"
    17 begin
    18 
    19 definition
    20   ideal :: "'a set \<Rightarrow> bool" where
    21   "ideal A = ((\<exists>x. x \<in> A) \<and> (\<forall>x\<in>A. \<forall>y\<in>A. \<exists>z\<in>A. x \<preceq> z \<and> y \<preceq> z) \<and>
    22     (\<forall>x y. x \<preceq> y \<longrightarrow> y \<in> A \<longrightarrow> x \<in> A))"
    23 
    24 lemma idealI:
    25   assumes "\<exists>x. x \<in> A"
    26   assumes "\<And>x y. \<lbrakk>x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> \<exists>z\<in>A. x \<preceq> z \<and> y \<preceq> z"
    27   assumes "\<And>x y. \<lbrakk>x \<preceq> y; y \<in> A\<rbrakk> \<Longrightarrow> x \<in> A"
    28   shows "ideal A"
    29 unfolding ideal_def using prems by fast
    30 
    31 lemma idealD1:
    32   "ideal A \<Longrightarrow> \<exists>x. x \<in> A"
    33 unfolding ideal_def by fast
    34 
    35 lemma idealD2:
    36   "\<lbrakk>ideal A; x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> \<exists>z\<in>A. x \<preceq> z \<and> y \<preceq> z"
    37 unfolding ideal_def by fast
    38 
    39 lemma idealD3:
    40   "\<lbrakk>ideal A; x \<preceq> y; y \<in> A\<rbrakk> \<Longrightarrow> x \<in> A"
    41 unfolding ideal_def by fast
    42 
    43 lemma ideal_principal: "ideal {x. x \<preceq> z}"
    44 apply (rule idealI)
    45 apply (rule_tac x=z in exI)
    46 apply (fast intro: r_refl)
    47 apply (rule_tac x=z in bexI, fast)
    48 apply (fast intro: r_refl)
    49 apply (fast intro: r_trans)
    50 done
    51 
    52 lemma ex_ideal: "\<exists>A. ideal A"
    53 by (rule exI, rule ideal_principal)
    54 
    55 lemma lub_image_principal:
    56   assumes f: "\<And>x y. x \<preceq> y \<Longrightarrow> f x \<sqsubseteq> f y"
    57   shows "(\<Squnion>x\<in>{x. x \<preceq> y}. f x) = f y"
    58 apply (rule thelubI)
    59 apply (rule is_lub_maximal)
    60 apply (rule ub_imageI)
    61 apply (simp add: f)
    62 apply (rule imageI)
    63 apply (simp add: r_refl)
    64 done
    65 
    66 text {* The set of ideals is a cpo *}
    67 
    68 lemma ideal_UN:
    69   fixes A :: "nat \<Rightarrow> 'a set"
    70   assumes ideal_A: "\<And>i. ideal (A i)"
    71   assumes chain_A: "\<And>i j. i \<le> j \<Longrightarrow> A i \<subseteq> A j"
    72   shows "ideal (\<Union>i. A i)"
    73  apply (rule idealI)
    74    apply (cut_tac idealD1 [OF ideal_A], fast)
    75   apply (clarify, rename_tac i j)
    76   apply (drule subsetD [OF chain_A [OF le_maxI1]])
    77   apply (drule subsetD [OF chain_A [OF le_maxI2]])
    78   apply (drule (1) idealD2 [OF ideal_A])
    79   apply blast
    80  apply clarify
    81  apply (drule (1) idealD3 [OF ideal_A])
    82  apply fast
    83 done
    84 
    85 lemma typedef_ideal_po:
    86   fixes Abs :: "'a set \<Rightarrow> 'b::below"
    87   assumes type: "type_definition Rep Abs {S. ideal S}"
    88   assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
    89   shows "OFCLASS('b, po_class)"
    90  apply (intro_classes, unfold below)
    91    apply (rule subset_refl)
    92   apply (erule (1) subset_trans)
    93  apply (rule type_definition.Rep_inject [OF type, THEN iffD1])
    94  apply (erule (1) subset_antisym)
    95 done
    96 
    97 lemma
    98   fixes Abs :: "'a set \<Rightarrow> 'b::po"
    99   assumes type: "type_definition Rep Abs {S. ideal S}"
   100   assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
   101   assumes S: "chain S"
   102   shows typedef_ideal_lub: "range S <<| Abs (\<Union>i. Rep (S i))"
   103     and typedef_ideal_rep_lub: "Rep (\<Squnion>i. S i) = (\<Union>i. Rep (S i))"
   104 proof -
   105   have 1: "ideal (\<Union>i. Rep (S i))"
   106     apply (rule ideal_UN)
   107      apply (rule type_definition.Rep [OF type, unfolded mem_Collect_eq])
   108     apply (subst below [symmetric])
   109     apply (erule chain_mono [OF S])
   110     done
   111   hence 2: "Rep (Abs (\<Union>i. Rep (S i))) = (\<Union>i. Rep (S i))"
   112     by (simp add: type_definition.Abs_inverse [OF type])
   113   show 3: "range S <<| Abs (\<Union>i. Rep (S i))"
   114     apply (rule is_lubI)
   115      apply (rule is_ubI)
   116      apply (simp add: below 2, fast)
   117     apply (simp add: below 2 is_ub_def, fast)
   118     done
   119   hence 4: "(\<Squnion>i. S i) = Abs (\<Union>i. Rep (S i))"
   120     by (rule thelubI)
   121   show 5: "Rep (\<Squnion>i. S i) = (\<Union>i. Rep (S i))"
   122     by (simp add: 4 2)
   123 qed
   124 
   125 lemma typedef_ideal_cpo:
   126   fixes Abs :: "'a set \<Rightarrow> 'b::po"
   127   assumes type: "type_definition Rep Abs {S. ideal S}"
   128   assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
   129   shows "OFCLASS('b, cpo_class)"
   130 by (default, rule exI, erule typedef_ideal_lub [OF type below])
   131 
   132 end
   133 
   134 interpretation below: preorder "below :: 'a::po \<Rightarrow> 'a \<Rightarrow> bool"
   135 apply unfold_locales
   136 apply (rule below_refl)
   137 apply (erule (1) below_trans)
   138 done
   139 
   140 subsection {* Lemmas about least upper bounds *}
   141 
   142 lemma is_ub_thelub_ex: "\<lbrakk>\<exists>u. S <<| u; x \<in> S\<rbrakk> \<Longrightarrow> x \<sqsubseteq> lub S"
   143 apply (erule exE, drule lubI)
   144 apply (drule is_lubD1)
   145 apply (erule (1) is_ubD)
   146 done
   147 
   148 lemma is_lub_thelub_ex: "\<lbrakk>\<exists>u. S <<| u; S <| x\<rbrakk> \<Longrightarrow> lub S \<sqsubseteq> x"
   149 by (erule exE, drule lubI, erule is_lub_lub)
   150 
   151 subsection {* Locale for ideal completion *}
   152 
   153 locale ideal_completion = preorder +
   154   fixes principal :: "'a::type \<Rightarrow> 'b::cpo"
   155   fixes rep :: "'b::cpo \<Rightarrow> 'a::type set"
   156   assumes ideal_rep: "\<And>x. ideal (rep x)"
   157   assumes rep_lub: "\<And>Y. chain Y \<Longrightarrow> rep (\<Squnion>i. Y i) = (\<Union>i. rep (Y i))"
   158   assumes rep_principal: "\<And>a. rep (principal a) = {b. b \<preceq> a}"
   159   assumes subset_repD: "\<And>x y. rep x \<subseteq> rep y \<Longrightarrow> x \<sqsubseteq> y"
   160   assumes countable: "\<exists>f::'a \<Rightarrow> nat. inj f"
   161 begin
   162 
   163 lemma rep_mono: "x \<sqsubseteq> y \<Longrightarrow> rep x \<subseteq> rep y"
   164 apply (frule bin_chain)
   165 apply (drule rep_lub)
   166 apply (simp only: thelubI [OF lub_bin_chain])
   167 apply (rule subsetI, rule UN_I [where a=0], simp_all)
   168 done
   169 
   170 lemma below_def: "x \<sqsubseteq> y \<longleftrightarrow> rep x \<subseteq> rep y"
   171 by (rule iffI [OF rep_mono subset_repD])
   172 
   173 lemma rep_eq: "rep x = {a. principal a \<sqsubseteq> x}"
   174 unfolding below_def rep_principal
   175 apply safe
   176 apply (erule (1) idealD3 [OF ideal_rep])
   177 apply (erule subsetD, simp add: r_refl)
   178 done
   179 
   180 lemma mem_rep_iff_principal_below: "a \<in> rep x \<longleftrightarrow> principal a \<sqsubseteq> x"
   181 by (simp add: rep_eq)
   182 
   183 lemma principal_below_iff_mem_rep: "principal a \<sqsubseteq> x \<longleftrightarrow> a \<in> rep x"
   184 by (simp add: rep_eq)
   185 
   186 lemma principal_below_iff [simp]: "principal a \<sqsubseteq> principal b \<longleftrightarrow> a \<preceq> b"
   187 by (simp add: principal_below_iff_mem_rep rep_principal)
   188 
   189 lemma principal_eq_iff: "principal a = principal b \<longleftrightarrow> a \<preceq> b \<and> b \<preceq> a"
   190 unfolding po_eq_conv [where 'a='b] principal_below_iff ..
   191 
   192 lemma eq_iff: "x = y \<longleftrightarrow> rep x = rep y"
   193 unfolding po_eq_conv below_def by auto
   194 
   195 lemma repD: "a \<in> rep x \<Longrightarrow> principal a \<sqsubseteq> x"
   196 by (simp add: rep_eq)
   197 
   198 lemma principal_mono: "a \<preceq> b \<Longrightarrow> principal a \<sqsubseteq> principal b"
   199 by (simp only: principal_below_iff)
   200 
   201 lemma ch2ch_principal [simp]:
   202   "\<forall>i. Y i \<preceq> Y (Suc i) \<Longrightarrow> chain (\<lambda>i. principal (Y i))"
   203 by (simp add: chainI principal_mono)
   204 
   205 lemma lub_principal_rep: "principal ` rep x <<| x"
   206 apply (rule is_lubI)
   207 apply (rule ub_imageI)
   208 apply (erule repD)
   209 apply (subst below_def)
   210 apply (rule subsetI)
   211 apply (drule (1) ub_imageD)
   212 apply (simp add: rep_eq)
   213 done
   214 
   215 subsubsection {* Principal ideals approximate all elements *}
   216 
   217 lemma compact_principal [simp]: "compact (principal a)"
   218 by (rule compactI2, simp add: principal_below_iff_mem_rep rep_lub)
   219 
   220 text {* Construct a chain whose lub is the same as a given ideal *}
   221 
   222 lemma obtain_principal_chain:
   223   obtains Y where "\<forall>i. Y i \<preceq> Y (Suc i)" and "x = (\<Squnion>i. principal (Y i))"
   224 proof -
   225   obtain count :: "'a \<Rightarrow> nat" where inj: "inj count"
   226     using countable ..
   227   def enum \<equiv> "\<lambda>i. THE a. count a = i"
   228   have enum_count [simp]: "\<And>x. enum (count x) = x"
   229     unfolding enum_def by (simp add: inj_eq [OF inj])
   230   def a \<equiv> "LEAST i. enum i \<in> rep x"
   231   def b \<equiv> "\<lambda>i. LEAST j. enum j \<in> rep x \<and> \<not> enum j \<preceq> enum i"
   232   def c \<equiv> "\<lambda>i j. LEAST k. enum k \<in> rep x \<and> enum i \<preceq> enum k \<and> enum j \<preceq> enum k"
   233   def P \<equiv> "\<lambda>i. \<exists>j. enum j \<in> rep x \<and> \<not> enum j \<preceq> enum i"
   234   def X \<equiv> "nat_rec a (\<lambda>n i. if P i then c i (b i) else i)"
   235   have X_0: "X 0 = a" unfolding X_def by simp
   236   have X_Suc: "\<And>n. X (Suc n) = (if P (X n) then c (X n) (b (X n)) else X n)"
   237     unfolding X_def by simp
   238   have a_mem: "enum a \<in> rep x"
   239     unfolding a_def
   240     apply (rule LeastI_ex)
   241     apply (cut_tac ideal_rep [of x])
   242     apply (drule idealD1)
   243     apply (clarify, rename_tac a)
   244     apply (rule_tac x="count a" in exI, simp)
   245     done
   246   have b: "\<And>i. P i \<Longrightarrow> enum i \<in> rep x
   247     \<Longrightarrow> enum (b i) \<in> rep x \<and> \<not> enum (b i) \<preceq> enum i"
   248     unfolding P_def b_def by (erule LeastI2_ex, simp)
   249   have c: "\<And>i j. enum i \<in> rep x \<Longrightarrow> enum j \<in> rep x
   250     \<Longrightarrow> enum (c i j) \<in> rep x \<and> enum i \<preceq> enum (c i j) \<and> enum j \<preceq> enum (c i j)"
   251     unfolding c_def
   252     apply (drule (1) idealD2 [OF ideal_rep], clarify)
   253     apply (rule_tac a="count z" in LeastI2, simp, simp)
   254     done
   255   have X_mem: "\<And>n. enum (X n) \<in> rep x"
   256     apply (induct_tac n)
   257     apply (simp add: X_0 a_mem)
   258     apply (clarsimp simp add: X_Suc, rename_tac n)
   259     apply (simp add: b c)
   260     done
   261   have X_chain: "\<And>n. enum (X n) \<preceq> enum (X (Suc n))"
   262     apply (clarsimp simp add: X_Suc r_refl)
   263     apply (simp add: b c X_mem)
   264     done
   265   have less_b: "\<And>n i. n < b i \<Longrightarrow> enum n \<in> rep x \<Longrightarrow> enum n \<preceq> enum i"
   266     unfolding b_def by (drule not_less_Least, simp)
   267   have X_covers: "\<And>n. \<forall>k\<le>n. enum k \<in> rep x \<longrightarrow> enum k \<preceq> enum (X n)"
   268     apply (induct_tac n)
   269     apply (clarsimp simp add: X_0 a_def)
   270     apply (drule_tac k=0 in Least_le, simp add: r_refl)
   271     apply (clarsimp, rename_tac n k)
   272     apply (erule le_SucE)
   273     apply (rule r_trans [OF _ X_chain], simp)
   274     apply (case_tac "P (X n)", simp add: X_Suc)
   275     apply (rule_tac x="b (X n)" and y="Suc n" in linorder_cases)
   276     apply (simp only: less_Suc_eq_le)
   277     apply (drule spec, drule (1) mp, simp add: b X_mem)
   278     apply (simp add: c X_mem)
   279     apply (drule (1) less_b)
   280     apply (erule r_trans)
   281     apply (simp add: b c X_mem)
   282     apply (simp add: X_Suc)
   283     apply (simp add: P_def)
   284     done
   285   have 1: "\<forall>i. enum (X i) \<preceq> enum (X (Suc i))"
   286     by (simp add: X_chain)
   287   have 2: "x = (\<Squnion>n. principal (enum (X n)))"
   288     apply (simp add: eq_iff rep_lub 1 rep_principal)
   289     apply (auto, rename_tac a)
   290     apply (subgoal_tac "\<exists>i. a = enum i", erule exE)
   291     apply (rule_tac x=i in exI, simp add: X_covers)
   292     apply (rule_tac x="count a" in exI, simp)
   293     apply (erule idealD3 [OF ideal_rep])
   294     apply (rule X_mem)
   295     done
   296   from 1 2 show ?thesis ..
   297 qed
   298 
   299 lemma principal_induct:
   300   assumes adm: "adm P"
   301   assumes P: "\<And>a. P (principal a)"
   302   shows "P x"
   303 apply (rule obtain_principal_chain [of x])
   304 apply (simp add: admD [OF adm] P)
   305 done
   306 
   307 lemma principal_induct2:
   308   "\<lbrakk>\<And>y. adm (\<lambda>x. P x y); \<And>x. adm (\<lambda>y. P x y);
   309     \<And>a b. P (principal a) (principal b)\<rbrakk> \<Longrightarrow> P x y"
   310 apply (rule_tac x=y in spec)
   311 apply (rule_tac x=x in principal_induct, simp)
   312 apply (rule allI, rename_tac y)
   313 apply (rule_tac x=y in principal_induct, simp)
   314 apply simp
   315 done
   316 
   317 lemma compact_imp_principal: "compact x \<Longrightarrow> \<exists>a. x = principal a"
   318 apply (rule obtain_principal_chain [of x])
   319 apply (drule adm_compact_neq [OF _ cont_id])
   320 apply (subgoal_tac "chain (\<lambda>i. principal (Y i))")
   321 apply (drule (2) admD2, fast, simp)
   322 done
   323 
   324 lemma obtain_compact_chain:
   325   obtains Y :: "nat \<Rightarrow> 'b"
   326   where "chain Y" and "\<forall>i. compact (Y i)" and "x = (\<Squnion>i. Y i)"
   327 apply (rule obtain_principal_chain [of x])
   328 apply (rule_tac Y="\<lambda>i. principal (Y i)" in that, simp_all)
   329 done
   330 
   331 subsection {* Defining functions in terms of basis elements *}
   332 
   333 definition
   334   basis_fun :: "('a::type \<Rightarrow> 'c::cpo) \<Rightarrow> 'b \<rightarrow> 'c" where
   335   "basis_fun = (\<lambda>f. (\<Lambda> x. lub (f ` rep x)))"
   336 
   337 lemma basis_fun_lemma:
   338   fixes f :: "'a::type \<Rightarrow> 'c::cpo"
   339   assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
   340   shows "\<exists>u. f ` rep x <<| u"
   341 proof -
   342   obtain Y where Y: "\<forall>i. Y i \<preceq> Y (Suc i)"
   343   and x: "x = (\<Squnion>i. principal (Y i))"
   344     by (rule obtain_principal_chain [of x])
   345   have chain: "chain (\<lambda>i. f (Y i))"
   346     by (rule chainI, simp add: f_mono Y)
   347   have rep_x: "rep x = (\<Union>n. {a. a \<preceq> Y n})"
   348     by (simp add: x rep_lub Y rep_principal)
   349   have "f ` rep x <<| (\<Squnion>n. f (Y n))"
   350     apply (rule is_lubI)
   351     apply (rule ub_imageI, rename_tac a)
   352     apply (clarsimp simp add: rep_x)
   353     apply (drule f_mono)
   354     apply (erule below_lub [OF chain])
   355     apply (rule lub_below [OF chain])
   356     apply (drule_tac x="Y n" in ub_imageD)
   357     apply (simp add: rep_x, fast intro: r_refl)
   358     apply assumption
   359     done
   360   thus ?thesis ..
   361 qed
   362 
   363 lemma basis_fun_beta:
   364   fixes f :: "'a::type \<Rightarrow> 'c::cpo"
   365   assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
   366   shows "basis_fun f\<cdot>x = lub (f ` rep x)"
   367 unfolding basis_fun_def
   368 proof (rule beta_cfun)
   369   have lub: "\<And>x. \<exists>u. f ` rep x <<| u"
   370     using f_mono by (rule basis_fun_lemma)
   371   show cont: "cont (\<lambda>x. lub (f ` rep x))"
   372     apply (rule contI2)
   373      apply (rule monofunI)
   374      apply (rule is_lub_thelub_ex [OF lub ub_imageI])
   375      apply (rule is_ub_thelub_ex [OF lub imageI])
   376      apply (erule (1) subsetD [OF rep_mono])
   377     apply (rule is_lub_thelub_ex [OF lub ub_imageI])
   378     apply (simp add: rep_lub, clarify)
   379     apply (erule rev_below_trans [OF is_ub_thelub])
   380     apply (erule is_ub_thelub_ex [OF lub imageI])
   381     done
   382 qed
   383 
   384 lemma basis_fun_principal:
   385   fixes f :: "'a::type \<Rightarrow> 'c::cpo"
   386   assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
   387   shows "basis_fun f\<cdot>(principal a) = f a"
   388 apply (subst basis_fun_beta, erule f_mono)
   389 apply (subst rep_principal)
   390 apply (rule lub_image_principal, erule f_mono)
   391 done
   392 
   393 lemma basis_fun_mono:
   394   assumes f_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> f a \<sqsubseteq> f b"
   395   assumes g_mono: "\<And>a b. a \<preceq> b \<Longrightarrow> g a \<sqsubseteq> g b"
   396   assumes below: "\<And>a. f a \<sqsubseteq> g a"
   397   shows "basis_fun f \<sqsubseteq> basis_fun g"
   398  apply (rule cfun_belowI)
   399  apply (simp only: basis_fun_beta f_mono g_mono)
   400  apply (rule is_lub_thelub_ex)
   401   apply (rule basis_fun_lemma, erule f_mono)
   402  apply (rule ub_imageI, rename_tac a)
   403  apply (rule below_trans [OF below])
   404  apply (rule is_ub_thelub_ex)
   405   apply (rule basis_fun_lemma, erule g_mono)
   406  apply (erule imageI)
   407 done
   408 
   409 end
   410 
   411 lemma (in preorder) typedef_ideal_completion:
   412   fixes Abs :: "'a set \<Rightarrow> 'b::cpo"
   413   assumes type: "type_definition Rep Abs {S. ideal S}"
   414   assumes below: "\<And>x y. x \<sqsubseteq> y \<longleftrightarrow> Rep x \<subseteq> Rep y"
   415   assumes principal: "\<And>a. principal a = Abs {b. b \<preceq> a}"
   416   assumes countable: "\<exists>f::'a \<Rightarrow> nat. inj f"
   417   shows "ideal_completion r principal Rep"
   418 proof
   419   interpret type_definition Rep Abs "{S. ideal S}" by fact
   420   fix a b :: 'a and x y :: 'b and Y :: "nat \<Rightarrow> 'b"
   421   show "ideal (Rep x)"
   422     using Rep [of x] by simp
   423   show "chain Y \<Longrightarrow> Rep (\<Squnion>i. Y i) = (\<Union>i. Rep (Y i))"
   424     using type below by (rule typedef_ideal_rep_lub)
   425   show "Rep (principal a) = {b. b \<preceq> a}"
   426     by (simp add: principal Abs_inverse ideal_principal)
   427   show "Rep x \<subseteq> Rep y \<Longrightarrow> x \<sqsubseteq> y"
   428     by (simp only: below)
   429   show "\<exists>f::'a \<Rightarrow> nat. inj f"
   430     by (rule countable)
   431 qed
   432 
   433 end