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