src/HOL/Lifting_Set.thy
 author haftmann Wed Apr 09 10:04:31 2014 +0200 (2014-04-09) changeset 56482 39ac12b655ab parent 56218 1c3f1f2431f9 child 56518 beb3b6851665 permissions -rw-r--r--
parametricity transfer rule for INFIMUM, SUPREMUM
1 (*  Title:      HOL/Lifting_Set.thy
2     Author:     Brian Huffman and Ondrej Kuncar
3 *)
5 header {* Setup for Lifting/Transfer for the set type *}
7 theory Lifting_Set
8 imports Lifting
9 begin
11 subsection {* Relator and predicator properties *}
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))"
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
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)
26 lemma rel_set_conversep [simp]: "rel_set A\<inverse>\<inverse> = (rel_set A)\<inverse>\<inverse>"
27   unfolding rel_set_def by auto
29 lemma rel_set_eq [relator_eq]: "rel_set (op =) = (op =)"
30   unfolding rel_set_def fun_eq_iff by auto
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
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
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
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
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
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
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
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)
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
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
89 subsection {* Quotient theorem for the Lifting package *}
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
99 subsection {* Transfer rules for the Transfer package *}
101 subsubsection {* Unconditional transfer rules *}
103 context
104 begin
105 interpretation lifting_syntax .
107 lemma empty_transfer [transfer_rule]: "(rel_set A) {} {}"
108   unfolding rel_set_def by simp
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
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
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
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
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
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
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
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
147 lemma rel_set_transfer [transfer_rule]:
148   "((A ===> B ===> op =) ===> rel_set A ===> rel_set B ===> op =) rel_set rel_set"
149   unfolding rel_fun_def rel_set_def by fast
151 lemma bind_transfer [transfer_rule]:
152   "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) Set.bind Set.bind"
153   unfolding bind_UNION [abs_def] by transfer_prover
155 lemma INF_parametric [transfer_rule]:
156   "(rel_set A ===> (A ===> HOL.eq) ===> HOL.eq) INFIMUM INFIMUM"
157   unfolding INF_def [abs_def] by transfer_prover
159 lemma SUP_parametric [transfer_rule]:
160   "(rel_set R ===> (R ===> HOL.eq) ===> HOL.eq) SUPREMUM SUPREMUM"
161   unfolding SUP_def [abs_def] by transfer_prover
164 subsubsection {* Rules requiring bi-unique, bi-total or right-total relations *}
166 lemma member_transfer [transfer_rule]:
167   assumes "bi_unique A"
168   shows "(A ===> rel_set A ===> op =) (op \<in>) (op \<in>)"
169   using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
171 lemma right_total_Collect_transfer[transfer_rule]:
172   assumes "right_total A"
173   shows "((A ===> op =) ===> rel_set A) (\<lambda>P. Collect (\<lambda>x. P x \<and> Domainp A x)) Collect"
174   using assms unfolding right_total_def rel_set_def rel_fun_def Domainp_iff by fast
176 lemma Collect_transfer [transfer_rule]:
177   assumes "bi_total A"
178   shows "((A ===> op =) ===> rel_set A) Collect Collect"
179   using assms unfolding rel_fun_def rel_set_def bi_total_def by fast
181 lemma inter_transfer [transfer_rule]:
182   assumes "bi_unique A"
183   shows "(rel_set A ===> rel_set A ===> rel_set A) inter inter"
184   using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
186 lemma Diff_transfer [transfer_rule]:
187   assumes "bi_unique A"
188   shows "(rel_set A ===> rel_set A ===> rel_set A) (op -) (op -)"
189   using assms unfolding rel_fun_def rel_set_def bi_unique_def
190   unfolding Ball_def Bex_def Diff_eq
191   by (safe, simp, metis, simp, metis)
193 lemma subset_transfer [transfer_rule]:
194   assumes [transfer_rule]: "bi_unique A"
195   shows "(rel_set A ===> rel_set A ===> op =) (op \<subseteq>) (op \<subseteq>)"
196   unfolding subset_eq [abs_def] by transfer_prover
198 lemma right_total_UNIV_transfer[transfer_rule]:
199   assumes "right_total A"
200   shows "(rel_set A) (Collect (Domainp A)) UNIV"
201   using assms unfolding right_total_def rel_set_def Domainp_iff by blast
203 lemma UNIV_transfer [transfer_rule]:
204   assumes "bi_total A"
205   shows "(rel_set A) UNIV UNIV"
206   using assms unfolding rel_set_def bi_total_def by simp
208 lemma right_total_Compl_transfer [transfer_rule]:
209   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
210   shows "(rel_set A ===> rel_set A) (\<lambda>S. uminus S \<inter> Collect (Domainp A)) uminus"
211   unfolding Compl_eq [abs_def]
212   by (subst Collect_conj_eq[symmetric]) transfer_prover
214 lemma Compl_transfer [transfer_rule]:
215   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
216   shows "(rel_set A ===> rel_set A) uminus uminus"
217   unfolding Compl_eq [abs_def] by transfer_prover
219 lemma right_total_Inter_transfer [transfer_rule]:
220   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
221   shows "(rel_set (rel_set A) ===> rel_set A) (\<lambda>S. Inter S \<inter> Collect (Domainp A)) Inter"
222   unfolding Inter_eq[abs_def]
223   by (subst Collect_conj_eq[symmetric]) transfer_prover
225 lemma Inter_transfer [transfer_rule]:
226   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
227   shows "(rel_set (rel_set A) ===> rel_set A) Inter Inter"
228   unfolding Inter_eq [abs_def] by transfer_prover
230 lemma filter_transfer [transfer_rule]:
231   assumes [transfer_rule]: "bi_unique A"
232   shows "((A ===> op=) ===> rel_set A ===> rel_set A) Set.filter Set.filter"
233   unfolding Set.filter_def[abs_def] rel_fun_def rel_set_def by blast
235 lemma bi_unique_rel_set_lemma:
236   assumes "bi_unique R" and "rel_set R X Y"
237   obtains f where "Y = image f X" and "inj_on f X" and "\<forall>x\<in>X. R x (f x)"
238 proof
239   let ?f = "\<lambda>x. THE y. R x y"
240   from assms show f: "\<forall>x\<in>X. R x (?f x)"
241     apply (clarsimp simp add: rel_set_def)
242     apply (drule (1) bspec, clarify)
243     apply (rule theI2, assumption)
244     apply (simp add: bi_unique_def)
245     apply assumption
246     done
247   from assms show "Y = image ?f X"
248     apply safe
249     apply (clarsimp simp add: rel_set_def)
250     apply (drule (1) bspec, clarify)
251     apply (rule image_eqI)
252     apply (rule the_equality [symmetric], assumption)
253     apply (simp add: bi_unique_def)
254     apply assumption
255     apply (clarsimp simp add: rel_set_def)
256     apply (frule (1) bspec, clarify)
257     apply (rule theI2, assumption)
258     apply (clarsimp simp add: bi_unique_def)
259     apply (simp add: bi_unique_def, metis)
260     done
261   show "inj_on ?f X"
262     apply (rule inj_onI)
263     apply (drule f [rule_format])
264     apply (drule f [rule_format])
265     apply (simp add: assms(1) [unfolded bi_unique_def])
266     done
267 qed
269 lemma finite_transfer [transfer_rule]:
270   "bi_unique A \<Longrightarrow> (rel_set A ===> op =) finite finite"
271   by (rule rel_funI, erule (1) bi_unique_rel_set_lemma,
272     auto dest: finite_imageD)
274 lemma card_transfer [transfer_rule]:
275   "bi_unique A \<Longrightarrow> (rel_set A ===> op =) card card"
276   by (rule rel_funI, erule (1) bi_unique_rel_set_lemma, simp add: card_image)
278 lemma vimage_parametric [transfer_rule]:
279   assumes [transfer_rule]: "bi_total A" "bi_unique B"
280   shows "((A ===> B) ===> rel_set B ===> rel_set A) vimage vimage"
281 unfolding vimage_def[abs_def] by transfer_prover
283 lemma setsum_parametric [transfer_rule]:
284   assumes "bi_unique A"
285   shows "((A ===> op =) ===> rel_set A ===> op =) setsum setsum"
286 proof(rule rel_funI)+
287   fix f :: "'a \<Rightarrow> 'c" and g S T
288   assume fg: "(A ===> op =) f g"
289     and ST: "rel_set A S T"
290   show "setsum f S = setsum g T"
291   proof(rule setsum_reindex_cong)
292     let ?f = "\<lambda>t. THE s. A s t"
293     show "S = ?f ` T"
294       by(blast dest: rel_setD1[OF ST] rel_setD2[OF ST] bi_uniqueDl[OF assms]
295            intro: rev_image_eqI the_equality[symmetric] subst[rotated, where P="\<lambda>x. x \<in> S"])
297     show "inj_on ?f T"
298     proof(rule inj_onI)
299       fix t1 t2
300       assume "t1 \<in> T" "t2 \<in> T" "?f t1 = ?f t2"
301       from ST `t1 \<in> T` obtain s1 where "A s1 t1" "s1 \<in> S" by(auto dest: rel_setD2)
302       hence "?f t1 = s1" by(auto dest: bi_uniqueDl[OF assms])
303       moreover
304       from ST `t2 \<in> T` obtain s2 where "A s2 t2" "s2 \<in> S" by(auto dest: rel_setD2)
305       hence "?f t2 = s2" by(auto dest: bi_uniqueDl[OF assms])
306       ultimately have "s1 = s2" using `?f t1 = ?f t2` by simp
307       with `A s1 t1` `A s2 t2` show "t1 = t2" by(auto dest: bi_uniqueDr[OF assms])
308     qed
310     fix t
311     assume "t \<in> T"
312     with ST obtain s where "A s t" "s \<in> S" by(auto dest: rel_setD2)
313     hence "?f t = s" by(auto dest: bi_uniqueDl[OF assms])
314     moreover from fg `A s t` have "f s = g t" by(rule rel_funD)
315     ultimately show "g t = f (?f t)" by simp
316   qed
317 qed
319 end
321 end