src/HOLCF/Representable.thy
author huffman
Wed Nov 10 17:56:08 2010 -0800 (2010-11-10)
changeset 40502 8e92772bc0e8
parent 40497 d2e876d6da8c
permissions -rw-r--r--
move map functions to new theory file Map_Functions; add theory file Plain_HOLCF
     1 (*  Title:      HOLCF/Representable.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 header {* Representable Types *}
     6 
     7 theory Representable
     8 imports Algebraic Bifinite Universal Ssum One Fixrec Domain_Aux
     9 uses
    10   ("Tools/repdef.ML")
    11   ("Tools/Domain/domain_isomorphism.ML")
    12 begin
    13 
    14 default_sort "domain"
    15 
    16 subsection {* Representations of types *}
    17 
    18 lemma emb_prj: "emb\<cdot>((prj\<cdot>x)::'a) = cast\<cdot>DEFL('a)\<cdot>x"
    19 by (simp add: cast_DEFL)
    20 
    21 lemma emb_prj_emb:
    22   fixes x :: "'a"
    23   assumes "DEFL('a) \<sqsubseteq> DEFL('b)"
    24   shows "emb\<cdot>(prj\<cdot>(emb\<cdot>x) :: 'b) = emb\<cdot>x"
    25 unfolding emb_prj
    26 apply (rule cast.belowD)
    27 apply (rule monofun_cfun_arg [OF assms])
    28 apply (simp add: cast_DEFL)
    29 done
    30 
    31 lemma prj_emb_prj:
    32   assumes "DEFL('a) \<sqsubseteq> DEFL('b)"
    33   shows "prj\<cdot>(emb\<cdot>(prj\<cdot>x :: 'b)) = (prj\<cdot>x :: 'a)"
    34  apply (rule emb_eq_iff [THEN iffD1])
    35  apply (simp only: emb_prj)
    36  apply (rule deflation_below_comp1)
    37    apply (rule deflation_cast)
    38   apply (rule deflation_cast)
    39  apply (rule monofun_cfun_arg [OF assms])
    40 done
    41 
    42 text {* Isomorphism lemmas used internally by the domain package: *}
    43 
    44 lemma domain_abs_iso:
    45   fixes abs and rep
    46   assumes DEFL: "DEFL('b) = DEFL('a)"
    47   assumes abs_def: "(abs :: 'a \<rightarrow> 'b) \<equiv> prj oo emb"
    48   assumes rep_def: "(rep :: 'b \<rightarrow> 'a) \<equiv> prj oo emb"
    49   shows "rep\<cdot>(abs\<cdot>x) = x"
    50 unfolding abs_def rep_def
    51 by (simp add: emb_prj_emb DEFL)
    52 
    53 lemma domain_rep_iso:
    54   fixes abs and rep
    55   assumes DEFL: "DEFL('b) = DEFL('a)"
    56   assumes abs_def: "(abs :: 'a \<rightarrow> 'b) \<equiv> prj oo emb"
    57   assumes rep_def: "(rep :: 'b \<rightarrow> 'a) \<equiv> prj oo emb"
    58   shows "abs\<cdot>(rep\<cdot>x) = x"
    59 unfolding abs_def rep_def
    60 by (simp add: emb_prj_emb DEFL)
    61 
    62 subsection {* Deflations as sets *}
    63 
    64 definition defl_set :: "defl \<Rightarrow> udom set"
    65 where "defl_set A = {x. cast\<cdot>A\<cdot>x = x}"
    66 
    67 lemma adm_defl_set: "adm (\<lambda>x. x \<in> defl_set A)"
    68 unfolding defl_set_def by simp
    69 
    70 lemma defl_set_bottom: "\<bottom> \<in> defl_set A"
    71 unfolding defl_set_def by simp
    72 
    73 lemma defl_set_cast [simp]: "cast\<cdot>A\<cdot>x \<in> defl_set A"
    74 unfolding defl_set_def by simp
    75 
    76 lemma defl_set_subset_iff: "defl_set A \<subseteq> defl_set B \<longleftrightarrow> A \<sqsubseteq> B"
    77 apply (simp add: defl_set_def subset_eq cast_below_cast [symmetric])
    78 apply (auto simp add: cast.belowI cast.belowD)
    79 done
    80 
    81 subsection {* Proving a subtype is representable *}
    82 
    83 text {* Temporarily relax type constraints. *}
    84 
    85 setup {*
    86   fold Sign.add_const_constraint
    87   [ (@{const_name defl}, SOME @{typ "'a::pcpo itself \<Rightarrow> defl"})
    88   , (@{const_name emb}, SOME @{typ "'a::pcpo \<rightarrow> udom"})
    89   , (@{const_name prj}, SOME @{typ "udom \<rightarrow> 'a::pcpo"})
    90   , (@{const_name liftdefl}, SOME @{typ "'a::pcpo itself \<Rightarrow> defl"})
    91   , (@{const_name liftemb}, SOME @{typ "'a::pcpo u \<rightarrow> udom"})
    92   , (@{const_name liftprj}, SOME @{typ "udom \<rightarrow> 'a::pcpo u"}) ]
    93 *}
    94 
    95 lemma typedef_rep_class:
    96   fixes Rep :: "'a::pcpo \<Rightarrow> udom"
    97   fixes Abs :: "udom \<Rightarrow> 'a::pcpo"
    98   fixes t :: defl
    99   assumes type: "type_definition Rep Abs (defl_set t)"
   100   assumes below: "op \<sqsubseteq> \<equiv> \<lambda>x y. Rep x \<sqsubseteq> Rep y"
   101   assumes emb: "emb \<equiv> (\<Lambda> x. Rep x)"
   102   assumes prj: "prj \<equiv> (\<Lambda> x. Abs (cast\<cdot>t\<cdot>x))"
   103   assumes defl: "defl \<equiv> (\<lambda> a::'a itself. t)"
   104   assumes liftemb: "(liftemb :: 'a u \<rightarrow> udom) \<equiv> udom_emb u_approx oo u_map\<cdot>emb"
   105   assumes liftprj: "(liftprj :: udom \<rightarrow> 'a u) \<equiv> u_map\<cdot>prj oo udom_prj u_approx"
   106   assumes liftdefl: "(liftdefl :: 'a itself \<Rightarrow> defl) \<equiv> (\<lambda>t. u_defl\<cdot>DEFL('a))"
   107   shows "OFCLASS('a, liftdomain_class)"
   108 using liftemb [THEN meta_eq_to_obj_eq]
   109 using liftprj [THEN meta_eq_to_obj_eq]
   110 proof (rule liftdomain_class_intro)
   111   have emb_beta: "\<And>x. emb\<cdot>x = Rep x"
   112     unfolding emb
   113     apply (rule beta_cfun)
   114     apply (rule typedef_cont_Rep [OF type below adm_defl_set])
   115     done
   116   have prj_beta: "\<And>y. prj\<cdot>y = Abs (cast\<cdot>t\<cdot>y)"
   117     unfolding prj
   118     apply (rule beta_cfun)
   119     apply (rule typedef_cont_Abs [OF type below adm_defl_set])
   120     apply simp_all
   121     done
   122   have prj_emb: "\<And>x::'a. prj\<cdot>(emb\<cdot>x) = x"
   123     using type_definition.Rep [OF type]
   124     unfolding prj_beta emb_beta defl_set_def
   125     by (simp add: type_definition.Rep_inverse [OF type])
   126   have emb_prj: "\<And>y. emb\<cdot>(prj\<cdot>y :: 'a) = cast\<cdot>t\<cdot>y"
   127     unfolding prj_beta emb_beta
   128     by (simp add: type_definition.Abs_inverse [OF type])
   129   show "ep_pair (emb :: 'a \<rightarrow> udom) prj"
   130     apply default
   131     apply (simp add: prj_emb)
   132     apply (simp add: emb_prj cast.below)
   133     done
   134   show "cast\<cdot>DEFL('a) = emb oo (prj :: udom \<rightarrow> 'a)"
   135     by (rule cfun_eqI, simp add: defl emb_prj)
   136   show "LIFTDEFL('a) = u_defl\<cdot>DEFL('a)"
   137     unfolding liftdefl ..
   138 qed
   139 
   140 lemma typedef_DEFL:
   141   assumes "defl \<equiv> (\<lambda>a::'a::pcpo itself. t)"
   142   shows "DEFL('a::pcpo) = t"
   143 unfolding assms ..
   144 
   145 text {* Restore original typing constraints. *}
   146 
   147 setup {*
   148   fold Sign.add_const_constraint
   149   [ (@{const_name defl}, SOME @{typ "'a::domain itself \<Rightarrow> defl"})
   150   , (@{const_name emb}, SOME @{typ "'a::domain \<rightarrow> udom"})
   151   , (@{const_name prj}, SOME @{typ "udom \<rightarrow> 'a::domain"})
   152   , (@{const_name liftdefl}, SOME @{typ "'a::predomain itself \<Rightarrow> defl"})
   153   , (@{const_name liftemb}, SOME @{typ "'a::predomain u \<rightarrow> udom"})
   154   , (@{const_name liftprj}, SOME @{typ "udom \<rightarrow> 'a::predomain u"}) ]
   155 *}
   156 
   157 use "Tools/repdef.ML"
   158 
   159 subsection {* Isomorphic deflations *}
   160 
   161 definition
   162   isodefl :: "('a \<rightarrow> 'a) \<Rightarrow> defl \<Rightarrow> bool"
   163 where
   164   "isodefl d t \<longleftrightarrow> cast\<cdot>t = emb oo d oo prj"
   165 
   166 lemma isodeflI: "(\<And>x. cast\<cdot>t\<cdot>x = emb\<cdot>(d\<cdot>(prj\<cdot>x))) \<Longrightarrow> isodefl d t"
   167 unfolding isodefl_def by (simp add: cfun_eqI)
   168 
   169 lemma cast_isodefl: "isodefl d t \<Longrightarrow> cast\<cdot>t = (\<Lambda> x. emb\<cdot>(d\<cdot>(prj\<cdot>x)))"
   170 unfolding isodefl_def by (simp add: cfun_eqI)
   171 
   172 lemma isodefl_strict: "isodefl d t \<Longrightarrow> d\<cdot>\<bottom> = \<bottom>"
   173 unfolding isodefl_def
   174 by (drule cfun_fun_cong [where x="\<bottom>"], simp)
   175 
   176 lemma isodefl_imp_deflation:
   177   fixes d :: "'a \<rightarrow> 'a"
   178   assumes "isodefl d t" shows "deflation d"
   179 proof
   180   note assms [unfolded isodefl_def, simp]
   181   fix x :: 'a
   182   show "d\<cdot>(d\<cdot>x) = d\<cdot>x"
   183     using cast.idem [of t "emb\<cdot>x"] by simp
   184   show "d\<cdot>x \<sqsubseteq> x"
   185     using cast.below [of t "emb\<cdot>x"] by simp
   186 qed
   187 
   188 lemma isodefl_ID_DEFL: "isodefl (ID :: 'a \<rightarrow> 'a) DEFL('a)"
   189 unfolding isodefl_def by (simp add: cast_DEFL)
   190 
   191 lemma isodefl_LIFTDEFL:
   192   "isodefl (u_map\<cdot>(ID :: 'a \<rightarrow> 'a)) LIFTDEFL('a::predomain)"
   193 unfolding u_map_ID DEFL_u [symmetric]
   194 by (rule isodefl_ID_DEFL)
   195 
   196 lemma isodefl_DEFL_imp_ID: "isodefl (d :: 'a \<rightarrow> 'a) DEFL('a) \<Longrightarrow> d = ID"
   197 unfolding isodefl_def
   198 apply (simp add: cast_DEFL)
   199 apply (simp add: cfun_eq_iff)
   200 apply (rule allI)
   201 apply (drule_tac x="emb\<cdot>x" in spec)
   202 apply simp
   203 done
   204 
   205 lemma isodefl_bottom: "isodefl \<bottom> \<bottom>"
   206 unfolding isodefl_def by (simp add: cfun_eq_iff)
   207 
   208 lemma adm_isodefl:
   209   "cont f \<Longrightarrow> cont g \<Longrightarrow> adm (\<lambda>x. isodefl (f x) (g x))"
   210 unfolding isodefl_def by simp
   211 
   212 lemma isodefl_lub:
   213   assumes "chain d" and "chain t"
   214   assumes "\<And>i. isodefl (d i) (t i)"
   215   shows "isodefl (\<Squnion>i. d i) (\<Squnion>i. t i)"
   216 using prems unfolding isodefl_def
   217 by (simp add: contlub_cfun_arg contlub_cfun_fun)
   218 
   219 lemma isodefl_fix:
   220   assumes "\<And>d t. isodefl d t \<Longrightarrow> isodefl (f\<cdot>d) (g\<cdot>t)"
   221   shows "isodefl (fix\<cdot>f) (fix\<cdot>g)"
   222 unfolding fix_def2
   223 apply (rule isodefl_lub, simp, simp)
   224 apply (induct_tac i)
   225 apply (simp add: isodefl_bottom)
   226 apply (simp add: assms)
   227 done
   228 
   229 lemma isodefl_abs_rep:
   230   fixes abs and rep and d
   231   assumes DEFL: "DEFL('b) = DEFL('a)"
   232   assumes abs_def: "(abs :: 'a \<rightarrow> 'b) \<equiv> prj oo emb"
   233   assumes rep_def: "(rep :: 'b \<rightarrow> 'a) \<equiv> prj oo emb"
   234   shows "isodefl d t \<Longrightarrow> isodefl (abs oo d oo rep) t"
   235 unfolding isodefl_def
   236 by (simp add: cfun_eq_iff assms prj_emb_prj emb_prj_emb)
   237 
   238 lemma isodefl_cfun:
   239   "isodefl d1 t1 \<Longrightarrow> isodefl d2 t2 \<Longrightarrow>
   240     isodefl (cfun_map\<cdot>d1\<cdot>d2) (cfun_defl\<cdot>t1\<cdot>t2)"
   241 apply (rule isodeflI)
   242 apply (simp add: cast_cfun_defl cast_isodefl)
   243 apply (simp add: emb_cfun_def prj_cfun_def)
   244 apply (simp add: cfun_map_map cfcomp1)
   245 done
   246 
   247 lemma isodefl_ssum:
   248   "isodefl d1 t1 \<Longrightarrow> isodefl d2 t2 \<Longrightarrow>
   249     isodefl (ssum_map\<cdot>d1\<cdot>d2) (ssum_defl\<cdot>t1\<cdot>t2)"
   250 apply (rule isodeflI)
   251 apply (simp add: cast_ssum_defl cast_isodefl)
   252 apply (simp add: emb_ssum_def prj_ssum_def)
   253 apply (simp add: ssum_map_map isodefl_strict)
   254 done
   255 
   256 lemma isodefl_sprod:
   257   "isodefl d1 t1 \<Longrightarrow> isodefl d2 t2 \<Longrightarrow>
   258     isodefl (sprod_map\<cdot>d1\<cdot>d2) (sprod_defl\<cdot>t1\<cdot>t2)"
   259 apply (rule isodeflI)
   260 apply (simp add: cast_sprod_defl cast_isodefl)
   261 apply (simp add: emb_sprod_def prj_sprod_def)
   262 apply (simp add: sprod_map_map isodefl_strict)
   263 done
   264 
   265 lemma isodefl_cprod:
   266   "isodefl d1 t1 \<Longrightarrow> isodefl d2 t2 \<Longrightarrow>
   267     isodefl (cprod_map\<cdot>d1\<cdot>d2) (prod_defl\<cdot>t1\<cdot>t2)"
   268 apply (rule isodeflI)
   269 apply (simp add: cast_prod_defl cast_isodefl)
   270 apply (simp add: emb_prod_def prj_prod_def)
   271 apply (simp add: cprod_map_map cfcomp1)
   272 done
   273 
   274 lemma isodefl_u:
   275   fixes d :: "'a::liftdomain \<rightarrow> 'a"
   276   shows "isodefl (d :: 'a \<rightarrow> 'a) t \<Longrightarrow> isodefl (u_map\<cdot>d) (u_defl\<cdot>t)"
   277 apply (rule isodeflI)
   278 apply (simp add: cast_u_defl cast_isodefl)
   279 apply (simp add: emb_u_def prj_u_def liftemb_eq liftprj_eq)
   280 apply (simp add: u_map_map)
   281 done
   282 
   283 lemma encode_prod_u_map:
   284   "encode_prod_u\<cdot>(u_map\<cdot>(cprod_map\<cdot>f\<cdot>g)\<cdot>(decode_prod_u\<cdot>x))
   285     = sprod_map\<cdot>(u_map\<cdot>f)\<cdot>(u_map\<cdot>g)\<cdot>x"
   286 unfolding encode_prod_u_def decode_prod_u_def
   287 apply (case_tac x, simp, rename_tac a b)
   288 apply (case_tac a, simp, case_tac b, simp, simp)
   289 done
   290 
   291 lemma isodefl_cprod_u:
   292   assumes "isodefl (u_map\<cdot>d1) t1" and "isodefl (u_map\<cdot>d2) t2"
   293   shows "isodefl (u_map\<cdot>(cprod_map\<cdot>d1\<cdot>d2)) (sprod_defl\<cdot>t1\<cdot>t2)"
   294 using assms unfolding isodefl_def
   295 apply (simp add: emb_u_def prj_u_def liftemb_prod_def liftprj_prod_def)
   296 apply (simp add: emb_u_def [symmetric] prj_u_def [symmetric])
   297 apply (simp add: cfcomp1 encode_prod_u_map cast_sprod_defl sprod_map_map)
   298 done
   299 
   300 subsection {* Constructing Domain Isomorphisms *}
   301 
   302 use "Tools/Domain/domain_isomorphism.ML"
   303 
   304 setup Domain_Isomorphism.setup
   305 
   306 lemmas [domain_defl_simps] =
   307   DEFL_cfun DEFL_ssum DEFL_sprod DEFL_prod DEFL_u
   308   liftdefl_eq LIFTDEFL_prod
   309 
   310 lemmas [domain_map_ID] =
   311   cfun_map_ID ssum_map_ID sprod_map_ID cprod_map_ID u_map_ID
   312 
   313 lemmas [domain_isodefl] =
   314   isodefl_u isodefl_cfun isodefl_ssum isodefl_sprod
   315   isodefl_cprod isodefl_cprod_u
   316 
   317 lemmas [domain_deflation] =
   318   deflation_cfun_map deflation_ssum_map deflation_sprod_map
   319   deflation_cprod_map deflation_u_map
   320 
   321 setup {*
   322   fold Domain_Take_Proofs.add_map_function
   323     [(@{type_name cfun}, @{const_name cfun_map}, [true, true]),
   324      (@{type_name ssum}, @{const_name ssum_map}, [true, true]),
   325      (@{type_name sprod}, @{const_name sprod_map}, [true, true]),
   326      (@{type_name prod}, @{const_name cprod_map}, [true, true]),
   327      (@{type_name "u"}, @{const_name u_map}, [true])]
   328 *}
   329 
   330 end