src/HOL/Lifting_Set.thy
author kuncar
Thu Apr 10 17:48:15 2014 +0200 (2014-04-10)
changeset 56520 3373f5d1e074
parent 56519 c1048f5bbb45
child 56524 f4ba736040fa
permissions -rw-r--r--
abstract Domainp in relator_domain rules => more natural statement of the rule
     1 (*  Title:      HOL/Lifting_Set.thy
     2     Author:     Brian Huffman and Ondrej Kuncar
     3 *)
     4 
     5 header {* Setup for Lifting/Transfer for the set type *}
     6 
     7 theory Lifting_Set
     8 imports Lifting
     9 begin
    10 
    11 subsection {* Relator and predicator properties *}
    12 
    13 definition rel_set :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
    14   where "rel_set R = (\<lambda>A B. (\<forall>x\<in>A. \<exists>y\<in>B. R x y) \<and> (\<forall>y\<in>B. \<exists>x\<in>A. R x y))"
    15 
    16 lemma rel_setI:
    17   assumes "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. R x y"
    18   assumes "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. R x y"
    19   shows "rel_set R A B"
    20   using assms unfolding rel_set_def by simp
    21 
    22 lemma rel_setD1: "\<lbrakk> rel_set R A B; x \<in> A \<rbrakk> \<Longrightarrow> \<exists>y \<in> B. R x y"
    23   and rel_setD2: "\<lbrakk> rel_set R A B; y \<in> B \<rbrakk> \<Longrightarrow> \<exists>x \<in> A. R x y"
    24 by(simp_all add: rel_set_def)
    25 
    26 lemma rel_set_conversep [simp]: "rel_set A\<inverse>\<inverse> = (rel_set A)\<inverse>\<inverse>"
    27   unfolding rel_set_def by auto
    28 
    29 lemma rel_set_eq [relator_eq]: "rel_set (op =) = (op =)"
    30   unfolding rel_set_def fun_eq_iff by auto
    31 
    32 lemma rel_set_mono[relator_mono]:
    33   assumes "A \<le> B"
    34   shows "rel_set A \<le> rel_set B"
    35 using assms unfolding rel_set_def by blast
    36 
    37 lemma rel_set_OO[relator_distr]: "rel_set R OO rel_set S = rel_set (R OO S)"
    38   apply (rule sym)
    39   apply (intro ext, rename_tac X Z)
    40   apply (rule iffI)
    41   apply (rule_tac b="{y. (\<exists>x\<in>X. R x y) \<and> (\<exists>z\<in>Z. S y z)}" in relcomppI)
    42   apply (simp add: rel_set_def, fast)
    43   apply (simp add: rel_set_def, fast)
    44   apply (simp add: rel_set_def, fast)
    45   done
    46 
    47 lemma Domainp_set[relator_domain]:
    48   "Domainp (rel_set T) = (\<lambda>A. Ball A (Domainp T))"
    49 unfolding rel_set_def Domainp_iff[abs_def]
    50 apply (intro ext)
    51 apply (rule iffI) 
    52 apply blast
    53 apply (rename_tac A, rule_tac x="{y. \<exists>x\<in>A. T x y}" in exI, fast)
    54 done
    55 
    56 lemma left_total_rel_set[transfer_rule]: 
    57   "left_total A \<Longrightarrow> left_total (rel_set A)"
    58   unfolding left_total_def rel_set_def
    59   apply safe
    60   apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
    61 done
    62 
    63 lemma left_unique_rel_set[transfer_rule]: 
    64   "left_unique A \<Longrightarrow> left_unique (rel_set A)"
    65   unfolding left_unique_def rel_set_def
    66   by fast
    67 
    68 lemma right_total_rel_set [transfer_rule]:
    69   "right_total A \<Longrightarrow> right_total (rel_set A)"
    70 using left_total_rel_set[of "A\<inverse>\<inverse>"] by simp
    71 
    72 lemma right_unique_rel_set [transfer_rule]:
    73   "right_unique A \<Longrightarrow> right_unique (rel_set A)"
    74   unfolding right_unique_def rel_set_def by fast
    75 
    76 lemma bi_total_rel_set [transfer_rule]:
    77   "bi_total A \<Longrightarrow> bi_total (rel_set A)"
    78 by(simp add: bi_total_conv_left_right left_total_rel_set right_total_rel_set)
    79 
    80 lemma bi_unique_rel_set [transfer_rule]:
    81   "bi_unique A \<Longrightarrow> bi_unique (rel_set A)"
    82   unfolding bi_unique_def rel_set_def by fast
    83 
    84 lemma set_relator_eq_onp [relator_eq_onp]:
    85   "rel_set (eq_onp P) = eq_onp (\<lambda>A. Ball A P)"
    86   unfolding fun_eq_iff rel_set_def eq_onp_def Ball_def by fast
    87 
    88 subsection {* Quotient theorem for the Lifting package *}
    89 
    90 lemma Quotient_set[quot_map]:
    91   assumes "Quotient R Abs Rep T"
    92   shows "Quotient (rel_set R) (image Abs) (image Rep) (rel_set T)"
    93   using assms unfolding Quotient_alt_def4
    94   apply (simp add: rel_set_OO[symmetric])
    95   apply (simp add: rel_set_def, fast)
    96   done
    97 
    98 subsection {* Transfer rules for the Transfer package *}
    99 
   100 subsubsection {* Unconditional transfer rules *}
   101 
   102 context
   103 begin
   104 interpretation lifting_syntax .
   105 
   106 lemma empty_transfer [transfer_rule]: "(rel_set A) {} {}"
   107   unfolding rel_set_def by simp
   108 
   109 lemma insert_transfer [transfer_rule]:
   110   "(A ===> rel_set A ===> rel_set A) insert insert"
   111   unfolding rel_fun_def rel_set_def by auto
   112 
   113 lemma union_transfer [transfer_rule]:
   114   "(rel_set A ===> rel_set A ===> rel_set A) union union"
   115   unfolding rel_fun_def rel_set_def by auto
   116 
   117 lemma Union_transfer [transfer_rule]:
   118   "(rel_set (rel_set A) ===> rel_set A) Union Union"
   119   unfolding rel_fun_def rel_set_def by simp fast
   120 
   121 lemma image_transfer [transfer_rule]:
   122   "((A ===> B) ===> rel_set A ===> rel_set B) image image"
   123   unfolding rel_fun_def rel_set_def by simp fast
   124 
   125 lemma UNION_transfer [transfer_rule]:
   126   "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) UNION UNION"
   127   unfolding Union_image_eq [symmetric, abs_def] by transfer_prover
   128 
   129 lemma Ball_transfer [transfer_rule]:
   130   "(rel_set A ===> (A ===> op =) ===> op =) Ball Ball"
   131   unfolding rel_set_def rel_fun_def by fast
   132 
   133 lemma Bex_transfer [transfer_rule]:
   134   "(rel_set A ===> (A ===> op =) ===> op =) Bex Bex"
   135   unfolding rel_set_def rel_fun_def by fast
   136 
   137 lemma Pow_transfer [transfer_rule]:
   138   "(rel_set A ===> rel_set (rel_set A)) Pow Pow"
   139   apply (rule rel_funI, rename_tac X Y, rule rel_setI)
   140   apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
   141   apply (simp add: rel_set_def, fast)
   142   apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
   143   apply (simp add: rel_set_def, fast)
   144   done
   145 
   146 lemma rel_set_transfer [transfer_rule]:
   147   "((A ===> B ===> op =) ===> rel_set A ===> rel_set B ===> op =) rel_set rel_set"
   148   unfolding rel_fun_def rel_set_def by fast
   149 
   150 lemma bind_transfer [transfer_rule]:
   151   "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) Set.bind Set.bind"
   152   unfolding bind_UNION [abs_def] by transfer_prover
   153 
   154 lemma INF_parametric [transfer_rule]:
   155   "(rel_set A ===> (A ===> HOL.eq) ===> HOL.eq) INFIMUM INFIMUM"
   156   unfolding INF_def [abs_def] by transfer_prover
   157 
   158 lemma SUP_parametric [transfer_rule]:
   159   "(rel_set R ===> (R ===> HOL.eq) ===> HOL.eq) SUPREMUM SUPREMUM"
   160   unfolding SUP_def [abs_def] by transfer_prover
   161 
   162 
   163 subsubsection {* Rules requiring bi-unique, bi-total or right-total relations *}
   164 
   165 lemma member_transfer [transfer_rule]:
   166   assumes "bi_unique A"
   167   shows "(A ===> rel_set A ===> op =) (op \<in>) (op \<in>)"
   168   using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
   169 
   170 lemma right_total_Collect_transfer[transfer_rule]:
   171   assumes "right_total A"
   172   shows "((A ===> op =) ===> rel_set A) (\<lambda>P. Collect (\<lambda>x. P x \<and> Domainp A x)) Collect"
   173   using assms unfolding right_total_def rel_set_def rel_fun_def Domainp_iff by fast
   174 
   175 lemma Collect_transfer [transfer_rule]:
   176   assumes "bi_total A"
   177   shows "((A ===> op =) ===> rel_set A) Collect Collect"
   178   using assms unfolding rel_fun_def rel_set_def bi_total_def by fast
   179 
   180 lemma inter_transfer [transfer_rule]:
   181   assumes "bi_unique A"
   182   shows "(rel_set A ===> rel_set A ===> rel_set A) inter inter"
   183   using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
   184 
   185 lemma Diff_transfer [transfer_rule]:
   186   assumes "bi_unique A"
   187   shows "(rel_set A ===> rel_set A ===> rel_set A) (op -) (op -)"
   188   using assms unfolding rel_fun_def rel_set_def bi_unique_def
   189   unfolding Ball_def Bex_def Diff_eq
   190   by (safe, simp, metis, simp, metis)
   191 
   192 lemma subset_transfer [transfer_rule]:
   193   assumes [transfer_rule]: "bi_unique A"
   194   shows "(rel_set A ===> rel_set A ===> op =) (op \<subseteq>) (op \<subseteq>)"
   195   unfolding subset_eq [abs_def] by transfer_prover
   196 
   197 lemma right_total_UNIV_transfer[transfer_rule]: 
   198   assumes "right_total A"
   199   shows "(rel_set A) (Collect (Domainp A)) UNIV"
   200   using assms unfolding right_total_def rel_set_def Domainp_iff by blast
   201 
   202 lemma UNIV_transfer [transfer_rule]:
   203   assumes "bi_total A"
   204   shows "(rel_set A) UNIV UNIV"
   205   using assms unfolding rel_set_def bi_total_def by simp
   206 
   207 lemma right_total_Compl_transfer [transfer_rule]:
   208   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
   209   shows "(rel_set A ===> rel_set A) (\<lambda>S. uminus S \<inter> Collect (Domainp A)) uminus"
   210   unfolding Compl_eq [abs_def]
   211   by (subst Collect_conj_eq[symmetric]) transfer_prover
   212 
   213 lemma Compl_transfer [transfer_rule]:
   214   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   215   shows "(rel_set A ===> rel_set A) uminus uminus"
   216   unfolding Compl_eq [abs_def] by transfer_prover
   217 
   218 lemma right_total_Inter_transfer [transfer_rule]:
   219   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
   220   shows "(rel_set (rel_set A) ===> rel_set A) (\<lambda>S. Inter S \<inter> Collect (Domainp A)) Inter"
   221   unfolding Inter_eq[abs_def]
   222   by (subst Collect_conj_eq[symmetric]) transfer_prover
   223 
   224 lemma Inter_transfer [transfer_rule]:
   225   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   226   shows "(rel_set (rel_set A) ===> rel_set A) Inter Inter"
   227   unfolding Inter_eq [abs_def] by transfer_prover
   228 
   229 lemma filter_transfer [transfer_rule]:
   230   assumes [transfer_rule]: "bi_unique A"
   231   shows "((A ===> op=) ===> rel_set A ===> rel_set A) Set.filter Set.filter"
   232   unfolding Set.filter_def[abs_def] rel_fun_def rel_set_def by blast
   233 
   234 lemma bi_unique_rel_set_lemma:
   235   assumes "bi_unique R" and "rel_set R X Y"
   236   obtains f where "Y = image f X" and "inj_on f X" and "\<forall>x\<in>X. R x (f x)"
   237 proof
   238   let ?f = "\<lambda>x. THE y. R x y"
   239   from assms show f: "\<forall>x\<in>X. R x (?f x)"
   240     apply (clarsimp simp add: rel_set_def)
   241     apply (drule (1) bspec, clarify)
   242     apply (rule theI2, assumption)
   243     apply (simp add: bi_unique_def)
   244     apply assumption
   245     done
   246   from assms show "Y = image ?f X"
   247     apply safe
   248     apply (clarsimp simp add: rel_set_def)
   249     apply (drule (1) bspec, clarify)
   250     apply (rule image_eqI)
   251     apply (rule the_equality [symmetric], assumption)
   252     apply (simp add: bi_unique_def)
   253     apply assumption
   254     apply (clarsimp simp add: rel_set_def)
   255     apply (frule (1) bspec, clarify)
   256     apply (rule theI2, assumption)
   257     apply (clarsimp simp add: bi_unique_def)
   258     apply (simp add: bi_unique_def, metis)
   259     done
   260   show "inj_on ?f X"
   261     apply (rule inj_onI)
   262     apply (drule f [rule_format])
   263     apply (drule f [rule_format])
   264     apply (simp add: assms(1) [unfolded bi_unique_def])
   265     done
   266 qed
   267 
   268 lemma finite_transfer [transfer_rule]:
   269   "bi_unique A \<Longrightarrow> (rel_set A ===> op =) finite finite"
   270   by (rule rel_funI, erule (1) bi_unique_rel_set_lemma,
   271     auto dest: finite_imageD)
   272 
   273 lemma card_transfer [transfer_rule]:
   274   "bi_unique A \<Longrightarrow> (rel_set A ===> op =) card card"
   275   by (rule rel_funI, erule (1) bi_unique_rel_set_lemma, simp add: card_image)
   276 
   277 lemma vimage_parametric [transfer_rule]:
   278   assumes [transfer_rule]: "bi_total A" "bi_unique B"
   279   shows "((A ===> B) ===> rel_set B ===> rel_set A) vimage vimage"
   280 unfolding vimage_def[abs_def] by transfer_prover
   281 
   282 lemma setsum_parametric [transfer_rule]:
   283   assumes "bi_unique A"
   284   shows "((A ===> op =) ===> rel_set A ===> op =) setsum setsum"
   285 proof(rule rel_funI)+
   286   fix f :: "'a \<Rightarrow> 'c" and g S T
   287   assume fg: "(A ===> op =) f g"
   288     and ST: "rel_set A S T"
   289   show "setsum f S = setsum g T"
   290   proof(rule setsum_reindex_cong)
   291     let ?f = "\<lambda>t. THE s. A s t"
   292     show "S = ?f ` T"
   293       by(blast dest: rel_setD1[OF ST] rel_setD2[OF ST] bi_uniqueDl[OF assms] 
   294            intro: rev_image_eqI the_equality[symmetric] subst[rotated, where P="\<lambda>x. x \<in> S"])
   295 
   296     show "inj_on ?f T"
   297     proof(rule inj_onI)
   298       fix t1 t2
   299       assume "t1 \<in> T" "t2 \<in> T" "?f t1 = ?f t2"
   300       from ST `t1 \<in> T` obtain s1 where "A s1 t1" "s1 \<in> S" by(auto dest: rel_setD2)
   301       hence "?f t1 = s1" by(auto dest: bi_uniqueDl[OF assms])
   302       moreover
   303       from ST `t2 \<in> T` obtain s2 where "A s2 t2" "s2 \<in> S" by(auto dest: rel_setD2)
   304       hence "?f t2 = s2" by(auto dest: bi_uniqueDl[OF assms])
   305       ultimately have "s1 = s2" using `?f t1 = ?f t2` by simp
   306       with `A s1 t1` `A s2 t2` show "t1 = t2" by(auto dest: bi_uniqueDr[OF assms])
   307     qed
   308 
   309     fix t
   310     assume "t \<in> T"
   311     with ST obtain s where "A s t" "s \<in> S" by(auto dest: rel_setD2)
   312     hence "?f t = s" by(auto dest: bi_uniqueDl[OF assms])
   313     moreover from fg `A s t` have "f s = g t" by(rule rel_funD)
   314     ultimately show "g t = f (?f t)" by simp
   315   qed
   316 qed
   317 
   318 end
   319 
   320 end