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