src/HOL/Lifting_Set.thy
author haftmann
Sun Mar 16 18:09:04 2014 +0100 (2014-03-16)
changeset 56166 9a241bc276cd
parent 55945 e96383acecf9
child 56212 3253aaf73a01
permissions -rw-r--r--
normalising simp rules for compound operators
     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   assumes "Domainp T = R"
    49   shows "Domainp (rel_set T) = (\<lambda>A. Ball A R)"
    50 using assms unfolding rel_set_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 left_total_rel_set[reflexivity_rule]: 
    58   "left_total A \<Longrightarrow> left_total (rel_set A)"
    59   unfolding left_total_def rel_set_def
    60   apply safe
    61   apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
    62 done
    63 
    64 lemma left_unique_rel_set[reflexivity_rule]: 
    65   "left_unique A \<Longrightarrow> left_unique (rel_set A)"
    66   unfolding left_unique_def rel_set_def
    67   by fast
    68 
    69 lemma right_total_rel_set [transfer_rule]:
    70   "right_total A \<Longrightarrow> right_total (rel_set A)"
    71 using left_total_rel_set[of "A\<inverse>\<inverse>"] by simp
    72 
    73 lemma right_unique_rel_set [transfer_rule]:
    74   "right_unique A \<Longrightarrow> right_unique (rel_set A)"
    75   unfolding right_unique_def rel_set_def by fast
    76 
    77 lemma bi_total_rel_set [transfer_rule]:
    78   "bi_total A \<Longrightarrow> bi_total (rel_set A)"
    79 by(simp add: bi_total_conv_left_right left_total_rel_set right_total_rel_set)
    80 
    81 lemma bi_unique_rel_set [transfer_rule]:
    82   "bi_unique A \<Longrightarrow> bi_unique (rel_set A)"
    83   unfolding bi_unique_def rel_set_def by fast
    84 
    85 lemma set_invariant_commute [invariant_commute]:
    86   "rel_set (Lifting.invariant P) = Lifting.invariant (\<lambda>A. Ball A P)"
    87   unfolding fun_eq_iff rel_set_def Lifting.invariant_def Ball_def by fast
    88 
    89 subsection {* Quotient theorem for the Lifting package *}
    90 
    91 lemma Quotient_set[quot_map]:
    92   assumes "Quotient R Abs Rep T"
    93   shows "Quotient (rel_set R) (image Abs) (image Rep) (rel_set T)"
    94   using assms unfolding Quotient_alt_def4
    95   apply (simp add: rel_set_OO[symmetric])
    96   apply (simp add: rel_set_def, fast)
    97   done
    98 
    99 subsection {* Transfer rules for the Transfer package *}
   100 
   101 subsubsection {* Unconditional transfer rules *}
   102 
   103 context
   104 begin
   105 interpretation lifting_syntax .
   106 
   107 lemma empty_transfer [transfer_rule]: "(rel_set A) {} {}"
   108   unfolding rel_set_def by simp
   109 
   110 lemma insert_transfer [transfer_rule]:
   111   "(A ===> rel_set A ===> rel_set A) insert insert"
   112   unfolding rel_fun_def rel_set_def by auto
   113 
   114 lemma union_transfer [transfer_rule]:
   115   "(rel_set A ===> rel_set A ===> rel_set A) union union"
   116   unfolding rel_fun_def rel_set_def by auto
   117 
   118 lemma Union_transfer [transfer_rule]:
   119   "(rel_set (rel_set A) ===> rel_set A) Union Union"
   120   unfolding rel_fun_def rel_set_def by simp fast
   121 
   122 lemma image_transfer [transfer_rule]:
   123   "((A ===> B) ===> rel_set A ===> rel_set B) image image"
   124   unfolding rel_fun_def rel_set_def by simp fast
   125 
   126 lemma UNION_transfer [transfer_rule]:
   127   "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) UNION UNION"
   128   unfolding Union_image_eq [symmetric, abs_def] by transfer_prover
   129 
   130 lemma Ball_transfer [transfer_rule]:
   131   "(rel_set A ===> (A ===> op =) ===> op =) Ball Ball"
   132   unfolding rel_set_def rel_fun_def by fast
   133 
   134 lemma Bex_transfer [transfer_rule]:
   135   "(rel_set A ===> (A ===> op =) ===> op =) Bex Bex"
   136   unfolding rel_set_def rel_fun_def by fast
   137 
   138 lemma Pow_transfer [transfer_rule]:
   139   "(rel_set A ===> rel_set (rel_set A)) Pow Pow"
   140   apply (rule rel_funI, rename_tac X Y, rule rel_setI)
   141   apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
   142   apply (simp add: rel_set_def, fast)
   143   apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
   144   apply (simp add: rel_set_def, fast)
   145   done
   146 
   147 lemma rel_set_transfer [transfer_rule]:
   148   "((A ===> B ===> op =) ===> rel_set A ===> rel_set B ===> op =)
   149     rel_set rel_set"
   150   unfolding rel_fun_def rel_set_def by fast
   151 
   152 lemma SUPR_parametric [transfer_rule]:
   153   "(rel_set R ===> (R ===> op =) ===> op =) SUPR (SUPR :: _ \<Rightarrow> _ \<Rightarrow> _::complete_lattice)"
   154 proof(rule rel_funI)+
   155   fix A B f and g :: "'b \<Rightarrow> 'c"
   156   assume AB: "rel_set R A B"
   157     and fg: "(R ===> op =) f g"
   158   show "SUPR A f = SUPR B g"
   159     by(rule SUPR_eq)(auto 4 4 dest: rel_setD1[OF AB] rel_setD2[OF AB] rel_funD[OF fg] intro: rev_bexI)
   160 qed
   161 
   162 lemma bind_transfer [transfer_rule]:
   163   "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) Set.bind Set.bind"
   164 unfolding bind_UNION[abs_def] by transfer_prover
   165 
   166 subsubsection {* Rules requiring bi-unique, bi-total or right-total relations *}
   167 
   168 lemma member_transfer [transfer_rule]:
   169   assumes "bi_unique A"
   170   shows "(A ===> rel_set A ===> op =) (op \<in>) (op \<in>)"
   171   using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
   172 
   173 lemma right_total_Collect_transfer[transfer_rule]:
   174   assumes "right_total A"
   175   shows "((A ===> op =) ===> rel_set A) (\<lambda>P. Collect (\<lambda>x. P x \<and> Domainp A x)) Collect"
   176   using assms unfolding right_total_def rel_set_def rel_fun_def Domainp_iff by fast
   177 
   178 lemma Collect_transfer [transfer_rule]:
   179   assumes "bi_total A"
   180   shows "((A ===> op =) ===> rel_set A) Collect Collect"
   181   using assms unfolding rel_fun_def rel_set_def bi_total_def by fast
   182 
   183 lemma inter_transfer [transfer_rule]:
   184   assumes "bi_unique A"
   185   shows "(rel_set A ===> rel_set A ===> rel_set A) inter inter"
   186   using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
   187 
   188 lemma Diff_transfer [transfer_rule]:
   189   assumes "bi_unique A"
   190   shows "(rel_set A ===> rel_set A ===> rel_set A) (op -) (op -)"
   191   using assms unfolding rel_fun_def rel_set_def bi_unique_def
   192   unfolding Ball_def Bex_def Diff_eq
   193   by (safe, simp, metis, simp, metis)
   194 
   195 lemma subset_transfer [transfer_rule]:
   196   assumes [transfer_rule]: "bi_unique A"
   197   shows "(rel_set A ===> rel_set A ===> op =) (op \<subseteq>) (op \<subseteq>)"
   198   unfolding subset_eq [abs_def] by transfer_prover
   199 
   200 lemma right_total_UNIV_transfer[transfer_rule]: 
   201   assumes "right_total A"
   202   shows "(rel_set A) (Collect (Domainp A)) UNIV"
   203   using assms unfolding right_total_def rel_set_def Domainp_iff by blast
   204 
   205 lemma UNIV_transfer [transfer_rule]:
   206   assumes "bi_total A"
   207   shows "(rel_set A) UNIV UNIV"
   208   using assms unfolding rel_set_def bi_total_def by simp
   209 
   210 lemma right_total_Compl_transfer [transfer_rule]:
   211   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
   212   shows "(rel_set A ===> rel_set A) (\<lambda>S. uminus S \<inter> Collect (Domainp A)) uminus"
   213   unfolding Compl_eq [abs_def]
   214   by (subst Collect_conj_eq[symmetric]) transfer_prover
   215 
   216 lemma Compl_transfer [transfer_rule]:
   217   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   218   shows "(rel_set A ===> rel_set A) uminus uminus"
   219   unfolding Compl_eq [abs_def] by transfer_prover
   220 
   221 lemma right_total_Inter_transfer [transfer_rule]:
   222   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
   223   shows "(rel_set (rel_set A) ===> rel_set A) (\<lambda>S. Inter S \<inter> Collect (Domainp A)) Inter"
   224   unfolding Inter_eq[abs_def]
   225   by (subst Collect_conj_eq[symmetric]) transfer_prover
   226 
   227 lemma Inter_transfer [transfer_rule]:
   228   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   229   shows "(rel_set (rel_set A) ===> rel_set A) Inter Inter"
   230   unfolding Inter_eq [abs_def] by transfer_prover
   231 
   232 lemma filter_transfer [transfer_rule]:
   233   assumes [transfer_rule]: "bi_unique A"
   234   shows "((A ===> op=) ===> rel_set A ===> rel_set A) Set.filter Set.filter"
   235   unfolding Set.filter_def[abs_def] rel_fun_def rel_set_def by blast
   236 
   237 lemma bi_unique_rel_set_lemma:
   238   assumes "bi_unique R" and "rel_set R X Y"
   239   obtains f where "Y = image f X" and "inj_on f X" and "\<forall>x\<in>X. R x (f x)"
   240 proof
   241   let ?f = "\<lambda>x. THE y. R x y"
   242   from assms show f: "\<forall>x\<in>X. R x (?f x)"
   243     apply (clarsimp simp add: rel_set_def)
   244     apply (drule (1) bspec, clarify)
   245     apply (rule theI2, assumption)
   246     apply (simp add: bi_unique_def)
   247     apply assumption
   248     done
   249   from assms show "Y = image ?f X"
   250     apply safe
   251     apply (clarsimp simp add: rel_set_def)
   252     apply (drule (1) bspec, clarify)
   253     apply (rule image_eqI)
   254     apply (rule the_equality [symmetric], assumption)
   255     apply (simp add: bi_unique_def)
   256     apply assumption
   257     apply (clarsimp simp add: rel_set_def)
   258     apply (frule (1) bspec, clarify)
   259     apply (rule theI2, assumption)
   260     apply (clarsimp simp add: bi_unique_def)
   261     apply (simp add: bi_unique_def, metis)
   262     done
   263   show "inj_on ?f X"
   264     apply (rule inj_onI)
   265     apply (drule f [rule_format])
   266     apply (drule f [rule_format])
   267     apply (simp add: assms(1) [unfolded bi_unique_def])
   268     done
   269 qed
   270 
   271 lemma finite_transfer [transfer_rule]:
   272   "bi_unique A \<Longrightarrow> (rel_set A ===> op =) finite finite"
   273   by (rule rel_funI, erule (1) bi_unique_rel_set_lemma,
   274     auto dest: finite_imageD)
   275 
   276 lemma card_transfer [transfer_rule]:
   277   "bi_unique A \<Longrightarrow> (rel_set A ===> op =) card card"
   278   by (rule rel_funI, erule (1) bi_unique_rel_set_lemma, simp add: card_image)
   279 
   280 lemma vimage_parametric [transfer_rule]:
   281   assumes [transfer_rule]: "bi_total A" "bi_unique B"
   282   shows "((A ===> B) ===> rel_set B ===> rel_set A) vimage vimage"
   283 unfolding vimage_def[abs_def] by transfer_prover
   284 
   285 lemma setsum_parametric [transfer_rule]:
   286   assumes "bi_unique A"
   287   shows "((A ===> op =) ===> rel_set A ===> op =) setsum setsum"
   288 proof(rule rel_funI)+
   289   fix f :: "'a \<Rightarrow> 'c" and g S T
   290   assume fg: "(A ===> op =) f g"
   291     and ST: "rel_set A S T"
   292   show "setsum f S = setsum g T"
   293   proof(rule setsum_reindex_cong)
   294     let ?f = "\<lambda>t. THE s. A s t"
   295     show "S = ?f ` T"
   296       by(blast dest: rel_setD1[OF ST] rel_setD2[OF ST] bi_uniqueDl[OF assms] 
   297            intro: rev_image_eqI the_equality[symmetric] subst[rotated, where P="\<lambda>x. x \<in> S"])
   298 
   299     show "inj_on ?f T"
   300     proof(rule inj_onI)
   301       fix t1 t2
   302       assume "t1 \<in> T" "t2 \<in> T" "?f t1 = ?f t2"
   303       from ST `t1 \<in> T` obtain s1 where "A s1 t1" "s1 \<in> S" by(auto dest: rel_setD2)
   304       hence "?f t1 = s1" by(auto dest: bi_uniqueDl[OF assms])
   305       moreover
   306       from ST `t2 \<in> T` obtain s2 where "A s2 t2" "s2 \<in> S" by(auto dest: rel_setD2)
   307       hence "?f t2 = s2" by(auto dest: bi_uniqueDl[OF assms])
   308       ultimately have "s1 = s2" using `?f t1 = ?f t2` by simp
   309       with `A s1 t1` `A s2 t2` show "t1 = t2" by(auto dest: bi_uniqueDr[OF assms])
   310     qed
   311 
   312     fix t
   313     assume "t \<in> T"
   314     with ST obtain s where "A s t" "s \<in> S" by(auto dest: rel_setD2)
   315     hence "?f t = s" by(auto dest: bi_uniqueDl[OF assms])
   316     moreover from fg `A s t` have "f s = g t" by(rule rel_funD)
   317     ultimately show "g t = f (?f t)" by simp
   318   qed
   319 qed
   320 
   321 end
   322 
   323 end