src/HOL/Lifting_Set.thy
author Andreas Lochbihler
Thu Sep 26 15:50:33 2013 +0200 (2013-09-26)
changeset 53927 abe2b313f0e5
parent 53012 cb82606b8215
child 53945 4191acef9d0e
permissions -rw-r--r--
add lemmas
     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 set_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
    14   where "set_rel 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 set_relI:
    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 "set_rel R A B"
    20   using assms unfolding set_rel_def by simp
    21 
    22 lemma set_relD1: "\<lbrakk> set_rel R A B; x \<in> A \<rbrakk> \<Longrightarrow> \<exists>y \<in> B. R x y"
    23   and set_relD2: "\<lbrakk> set_rel R A B; y \<in> B \<rbrakk> \<Longrightarrow> \<exists>x \<in> A. R x y"
    24 by(simp_all add: set_rel_def)
    25 
    26 lemma set_rel_conversep: "set_rel (conversep R) = conversep (set_rel R)"
    27   unfolding set_rel_def by auto
    28 
    29 lemma set_rel_eq [relator_eq]: "set_rel (op =) = (op =)"
    30   unfolding set_rel_def fun_eq_iff by auto
    31 
    32 lemma set_rel_mono[relator_mono]:
    33   assumes "A \<le> B"
    34   shows "set_rel A \<le> set_rel B"
    35 using assms unfolding set_rel_def by blast
    36 
    37 lemma set_rel_OO[relator_distr]: "set_rel R OO set_rel S = set_rel (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: set_rel_def, fast)
    43   apply (simp add: set_rel_def, fast)
    44   apply (simp add: set_rel_def, fast)
    45   done
    46 
    47 lemma Domainp_set[relator_domain]:
    48   assumes "Domainp T = R"
    49   shows "Domainp (set_rel T) = (\<lambda>A. Ball A R)"
    50 using assms unfolding set_rel_def Domainp_iff[abs_def]
    51 apply (intro ext)
    52 apply (rule iffI) 
    53 apply blast
    54 apply (rename_tac A, rule_tac x="{y. \<exists>x\<in>A. T x y}" in exI, fast)
    55 done
    56 
    57 lemma reflp_set_rel[reflexivity_rule]: "reflp R \<Longrightarrow> reflp (set_rel R)"
    58   unfolding reflp_def set_rel_def by fast
    59 
    60 lemma left_total_set_rel[reflexivity_rule]: 
    61   "left_total A \<Longrightarrow> left_total (set_rel A)"
    62   unfolding left_total_def set_rel_def
    63   apply safe
    64   apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
    65 done
    66 
    67 lemma left_unique_set_rel[reflexivity_rule]: 
    68   "left_unique A \<Longrightarrow> left_unique (set_rel A)"
    69   unfolding left_unique_def set_rel_def
    70   by fast
    71 
    72 lemma right_total_set_rel [transfer_rule]:
    73   "right_total A \<Longrightarrow> right_total (set_rel A)"
    74   unfolding right_total_def set_rel_def
    75   by (rule allI, rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
    76 
    77 lemma right_unique_set_rel [transfer_rule]:
    78   "right_unique A \<Longrightarrow> right_unique (set_rel A)"
    79   unfolding right_unique_def set_rel_def by fast
    80 
    81 lemma bi_total_set_rel [transfer_rule]:
    82   "bi_total A \<Longrightarrow> bi_total (set_rel A)"
    83   unfolding bi_total_def set_rel_def
    84   apply safe
    85   apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
    86   apply (rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
    87   done
    88 
    89 lemma bi_unique_set_rel [transfer_rule]:
    90   "bi_unique A \<Longrightarrow> bi_unique (set_rel A)"
    91   unfolding bi_unique_def set_rel_def by fast
    92 
    93 lemma set_invariant_commute [invariant_commute]:
    94   "set_rel (Lifting.invariant P) = Lifting.invariant (\<lambda>A. Ball A P)"
    95   unfolding fun_eq_iff set_rel_def Lifting.invariant_def Ball_def by fast
    96 
    97 subsection {* Quotient theorem for the Lifting package *}
    98 
    99 lemma Quotient_set[quot_map]:
   100   assumes "Quotient R Abs Rep T"
   101   shows "Quotient (set_rel R) (image Abs) (image Rep) (set_rel T)"
   102   using assms unfolding Quotient_alt_def4
   103   apply (simp add: set_rel_OO[symmetric] set_rel_conversep)
   104   apply (simp add: set_rel_def, fast)
   105   done
   106 
   107 subsection {* Transfer rules for the Transfer package *}
   108 
   109 subsubsection {* Unconditional transfer rules *}
   110 
   111 context
   112 begin
   113 interpretation lifting_syntax .
   114 
   115 lemma empty_transfer [transfer_rule]: "(set_rel A) {} {}"
   116   unfolding set_rel_def by simp
   117 
   118 lemma insert_transfer [transfer_rule]:
   119   "(A ===> set_rel A ===> set_rel A) insert insert"
   120   unfolding fun_rel_def set_rel_def by auto
   121 
   122 lemma union_transfer [transfer_rule]:
   123   "(set_rel A ===> set_rel A ===> set_rel A) union union"
   124   unfolding fun_rel_def set_rel_def by auto
   125 
   126 lemma Union_transfer [transfer_rule]:
   127   "(set_rel (set_rel A) ===> set_rel A) Union Union"
   128   unfolding fun_rel_def set_rel_def by simp fast
   129 
   130 lemma image_transfer [transfer_rule]:
   131   "((A ===> B) ===> set_rel A ===> set_rel B) image image"
   132   unfolding fun_rel_def set_rel_def by simp fast
   133 
   134 lemma UNION_transfer [transfer_rule]:
   135   "(set_rel A ===> (A ===> set_rel B) ===> set_rel B) UNION UNION"
   136   unfolding SUP_def [abs_def] by transfer_prover
   137 
   138 lemma Ball_transfer [transfer_rule]:
   139   "(set_rel A ===> (A ===> op =) ===> op =) Ball Ball"
   140   unfolding set_rel_def fun_rel_def by fast
   141 
   142 lemma Bex_transfer [transfer_rule]:
   143   "(set_rel A ===> (A ===> op =) ===> op =) Bex Bex"
   144   unfolding set_rel_def fun_rel_def by fast
   145 
   146 lemma Pow_transfer [transfer_rule]:
   147   "(set_rel A ===> set_rel (set_rel A)) Pow Pow"
   148   apply (rule fun_relI, rename_tac X Y, rule set_relI)
   149   apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
   150   apply (simp add: set_rel_def, fast)
   151   apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
   152   apply (simp add: set_rel_def, fast)
   153   done
   154 
   155 lemma set_rel_transfer [transfer_rule]:
   156   "((A ===> B ===> op =) ===> set_rel A ===> set_rel B ===> op =)
   157     set_rel set_rel"
   158   unfolding fun_rel_def set_rel_def by fast
   159 
   160 lemma SUPR_parametric [transfer_rule]:
   161   "(set_rel R ===> (R ===> op =) ===> op =) SUPR SUPR"
   162 proof(rule fun_relI)+
   163   fix A B f and g :: "'b \<Rightarrow> 'c"
   164   assume AB: "set_rel R A B"
   165     and fg: "(R ===> op =) f g"
   166   show "SUPR A f = SUPR B g"
   167     by(rule SUPR_eq)(auto 4 4 dest: set_relD1[OF AB] set_relD2[OF AB] fun_relD[OF fg] intro: rev_bexI)
   168 qed
   169 
   170 subsubsection {* Rules requiring bi-unique, bi-total or right-total relations *}
   171 
   172 lemma member_transfer [transfer_rule]:
   173   assumes "bi_unique A"
   174   shows "(A ===> set_rel A ===> op =) (op \<in>) (op \<in>)"
   175   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
   176 
   177 lemma right_total_Collect_transfer[transfer_rule]:
   178   assumes "right_total A"
   179   shows "((A ===> op =) ===> set_rel A) (\<lambda>P. Collect (\<lambda>x. P x \<and> Domainp A x)) Collect"
   180   using assms unfolding right_total_def set_rel_def fun_rel_def Domainp_iff by fast
   181 
   182 lemma Collect_transfer [transfer_rule]:
   183   assumes "bi_total A"
   184   shows "((A ===> op =) ===> set_rel A) Collect Collect"
   185   using assms unfolding fun_rel_def set_rel_def bi_total_def by fast
   186 
   187 lemma inter_transfer [transfer_rule]:
   188   assumes "bi_unique A"
   189   shows "(set_rel A ===> set_rel A ===> set_rel A) inter inter"
   190   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
   191 
   192 lemma Diff_transfer [transfer_rule]:
   193   assumes "bi_unique A"
   194   shows "(set_rel A ===> set_rel A ===> set_rel A) (op -) (op -)"
   195   using assms unfolding fun_rel_def set_rel_def bi_unique_def
   196   unfolding Ball_def Bex_def Diff_eq
   197   by (safe, simp, metis, simp, metis)
   198 
   199 lemma subset_transfer [transfer_rule]:
   200   assumes [transfer_rule]: "bi_unique A"
   201   shows "(set_rel A ===> set_rel A ===> op =) (op \<subseteq>) (op \<subseteq>)"
   202   unfolding subset_eq [abs_def] by transfer_prover
   203 
   204 lemma right_total_UNIV_transfer[transfer_rule]: 
   205   assumes "right_total A"
   206   shows "(set_rel A) (Collect (Domainp A)) UNIV"
   207   using assms unfolding right_total_def set_rel_def Domainp_iff by blast
   208 
   209 lemma UNIV_transfer [transfer_rule]:
   210   assumes "bi_total A"
   211   shows "(set_rel A) UNIV UNIV"
   212   using assms unfolding set_rel_def bi_total_def by simp
   213 
   214 lemma right_total_Compl_transfer [transfer_rule]:
   215   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
   216   shows "(set_rel A ===> set_rel A) (\<lambda>S. uminus S \<inter> Collect (Domainp A)) uminus"
   217   unfolding Compl_eq [abs_def]
   218   by (subst Collect_conj_eq[symmetric]) transfer_prover
   219 
   220 lemma Compl_transfer [transfer_rule]:
   221   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   222   shows "(set_rel A ===> set_rel A) uminus uminus"
   223   unfolding Compl_eq [abs_def] by transfer_prover
   224 
   225 lemma right_total_Inter_transfer [transfer_rule]:
   226   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
   227   shows "(set_rel (set_rel A) ===> set_rel A) (\<lambda>S. Inter S \<inter> Collect (Domainp A)) Inter"
   228   unfolding Inter_eq[abs_def]
   229   by (subst Collect_conj_eq[symmetric]) transfer_prover
   230 
   231 lemma Inter_transfer [transfer_rule]:
   232   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   233   shows "(set_rel (set_rel A) ===> set_rel A) Inter Inter"
   234   unfolding Inter_eq [abs_def] by transfer_prover
   235 
   236 lemma filter_transfer [transfer_rule]:
   237   assumes [transfer_rule]: "bi_unique A"
   238   shows "((A ===> op=) ===> set_rel A ===> set_rel A) Set.filter Set.filter"
   239   unfolding Set.filter_def[abs_def] fun_rel_def set_rel_def by blast
   240 
   241 lemma bi_unique_set_rel_lemma:
   242   assumes "bi_unique R" and "set_rel R X Y"
   243   obtains f where "Y = image f X" and "inj_on f X" and "\<forall>x\<in>X. R x (f x)"
   244 proof
   245   let ?f = "\<lambda>x. THE y. R x y"
   246   from assms show f: "\<forall>x\<in>X. R x (?f x)"
   247     apply (clarsimp simp add: set_rel_def)
   248     apply (drule (1) bspec, clarify)
   249     apply (rule theI2, assumption)
   250     apply (simp add: bi_unique_def)
   251     apply assumption
   252     done
   253   from assms show "Y = image ?f X"
   254     apply safe
   255     apply (clarsimp simp add: set_rel_def)
   256     apply (drule (1) bspec, clarify)
   257     apply (rule image_eqI)
   258     apply (rule the_equality [symmetric], assumption)
   259     apply (simp add: bi_unique_def)
   260     apply assumption
   261     apply (clarsimp simp add: set_rel_def)
   262     apply (frule (1) bspec, clarify)
   263     apply (rule theI2, assumption)
   264     apply (clarsimp simp add: bi_unique_def)
   265     apply (simp add: bi_unique_def, metis)
   266     done
   267   show "inj_on ?f X"
   268     apply (rule inj_onI)
   269     apply (drule f [rule_format])
   270     apply (drule f [rule_format])
   271     apply (simp add: assms(1) [unfolded bi_unique_def])
   272     done
   273 qed
   274 
   275 lemma finite_transfer [transfer_rule]:
   276   "bi_unique A \<Longrightarrow> (set_rel A ===> op =) finite finite"
   277   by (rule fun_relI, erule (1) bi_unique_set_rel_lemma,
   278     auto dest: finite_imageD)
   279 
   280 lemma card_transfer [transfer_rule]:
   281   "bi_unique A \<Longrightarrow> (set_rel A ===> op =) card card"
   282   by (rule fun_relI, erule (1) bi_unique_set_rel_lemma, simp add: card_image)
   283 
   284 lemma vimage_parametric [transfer_rule]:
   285   assumes [transfer_rule]: "bi_total A" "bi_unique B"
   286   shows "((A ===> B) ===> set_rel B ===> set_rel A) vimage vimage"
   287 unfolding vimage_def[abs_def] by transfer_prover
   288 
   289 lemma setsum_parametric [transfer_rule]:
   290   assumes "bi_unique A"
   291   shows "((A ===> op =) ===> set_rel A ===> op =) setsum setsum"
   292 proof(rule fun_relI)+
   293   fix f :: "'a \<Rightarrow> 'c" and g S T
   294   assume fg: "(A ===> op =) f g"
   295     and ST: "set_rel A S T"
   296   show "setsum f S = setsum g T"
   297   proof(rule setsum_reindex_cong)
   298     let ?f = "\<lambda>t. THE s. A s t"
   299     show "S = ?f ` T"
   300       by(blast dest: set_relD1[OF ST] set_relD2[OF ST] bi_uniqueDl[OF assms] 
   301            intro: rev_image_eqI the_equality[symmetric] subst[rotated, where P="\<lambda>x. x \<in> S"])
   302 
   303     show "inj_on ?f T"
   304     proof(rule inj_onI)
   305       fix t1 t2
   306       assume "t1 \<in> T" "t2 \<in> T" "?f t1 = ?f t2"
   307       from ST `t1 \<in> T` obtain s1 where "A s1 t1" "s1 \<in> S" by(auto dest: set_relD2)
   308       hence "?f t1 = s1" by(auto dest: bi_uniqueDl[OF assms])
   309       moreover
   310       from ST `t2 \<in> T` obtain s2 where "A s2 t2" "s2 \<in> S" by(auto dest: set_relD2)
   311       hence "?f t2 = s2" by(auto dest: bi_uniqueDl[OF assms])
   312       ultimately have "s1 = s2" using `?f t1 = ?f t2` by simp
   313       with `A s1 t1` `A s2 t2` show "t1 = t2" by(auto dest: bi_uniqueDr[OF assms])
   314     qed
   315 
   316     fix t
   317     assume "t \<in> T"
   318     with ST obtain s where "A s t" "s \<in> S" by(auto dest: set_relD2)
   319     hence "?f t = s" by(auto dest: bi_uniqueDl[OF assms])
   320     moreover from fg `A s t` have "f s = g t" by(rule fun_relD)
   321     ultimately show "g t = f (?f t)" by simp
   322   qed
   323 qed
   324 
   325 end
   326 
   327 end