src/HOL/Transfer.thy
 author kuncar Thu Apr 10 17:48:14 2014 +0200 (2014-04-10) changeset 56518 beb3b6851665 parent 56085 3d11892ea537 child 56520 3373f5d1e074 permissions -rw-r--r--
left_total and left_unique rules are now transfer rules (cleaner solution, reflexvity_rule attribute not needed anymore)
1 (*  Title:      HOL/Transfer.thy
2     Author:     Brian Huffman, TU Muenchen
3     Author:     Ondrej Kuncar, TU Muenchen
4 *)
6 header {* Generic theorem transfer using relations *}
8 theory Transfer
9 imports Hilbert_Choice Basic_BNFs Metis
10 begin
12 subsection {* Relator for function space *}
14 locale lifting_syntax
15 begin
16   notation rel_fun (infixr "===>" 55)
17   notation map_fun (infixr "--->" 55)
18 end
20 context
21 begin
22 interpretation lifting_syntax .
24 lemma rel_funD2:
25   assumes "rel_fun A B f g" and "A x x"
26   shows "B (f x) (g x)"
27   using assms by (rule rel_funD)
29 lemma rel_funE:
30   assumes "rel_fun A B f g" and "A x y"
31   obtains "B (f x) (g y)"
32   using assms by (simp add: rel_fun_def)
34 lemmas rel_fun_eq = fun.rel_eq
36 lemma rel_fun_eq_rel:
37 shows "rel_fun (op =) R = (\<lambda>f g. \<forall>x. R (f x) (g x))"
41 subsection {* Transfer method *}
43 text {* Explicit tag for relation membership allows for
44   backward proof methods. *}
46 definition Rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
47   where "Rel r \<equiv> r"
49 text {* Handling of equality relations *}
51 definition is_equality :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
52   where "is_equality R \<longleftrightarrow> R = (op =)"
54 lemma is_equality_eq: "is_equality (op =)"
55   unfolding is_equality_def by simp
57 text {* Reverse implication for monotonicity rules *}
59 definition rev_implies where
60   "rev_implies x y \<longleftrightarrow> (y \<longrightarrow> x)"
62 text {* Handling of meta-logic connectives *}
64 definition transfer_forall where
65   "transfer_forall \<equiv> All"
67 definition transfer_implies where
68   "transfer_implies \<equiv> op \<longrightarrow>"
70 definition transfer_bforall :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
71   where "transfer_bforall \<equiv> (\<lambda>P Q. \<forall>x. P x \<longrightarrow> Q x)"
73 lemma transfer_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (transfer_forall (\<lambda>x. P x))"
74   unfolding atomize_all transfer_forall_def ..
76 lemma transfer_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (transfer_implies A B)"
77   unfolding atomize_imp transfer_implies_def ..
79 lemma transfer_bforall_unfold:
80   "Trueprop (transfer_bforall P (\<lambda>x. Q x)) \<equiv> (\<And>x. P x \<Longrightarrow> Q x)"
81   unfolding transfer_bforall_def atomize_imp atomize_all ..
83 lemma transfer_start: "\<lbrakk>P; Rel (op =) P Q\<rbrakk> \<Longrightarrow> Q"
84   unfolding Rel_def by simp
86 lemma transfer_start': "\<lbrakk>P; Rel (op \<longrightarrow>) P Q\<rbrakk> \<Longrightarrow> Q"
87   unfolding Rel_def by simp
89 lemma transfer_prover_start: "\<lbrakk>x = x'; Rel R x' y\<rbrakk> \<Longrightarrow> Rel R x y"
90   by simp
92 lemma untransfer_start: "\<lbrakk>Q; Rel (op =) P Q\<rbrakk> \<Longrightarrow> P"
93   unfolding Rel_def by simp
95 lemma Rel_eq_refl: "Rel (op =) x x"
96   unfolding Rel_def ..
98 lemma Rel_app:
99   assumes "Rel (A ===> B) f g" and "Rel A x y"
100   shows "Rel B (f x) (g y)"
101   using assms unfolding Rel_def rel_fun_def by fast
103 lemma Rel_abs:
104   assumes "\<And>x y. Rel A x y \<Longrightarrow> Rel B (f x) (g y)"
105   shows "Rel (A ===> B) (\<lambda>x. f x) (\<lambda>y. g y)"
106   using assms unfolding Rel_def rel_fun_def by fast
108 end
110 ML_file "Tools/transfer.ML"
111 setup Transfer.setup
113 declare refl [transfer_rule]
115 declare rel_fun_eq [relator_eq]
117 hide_const (open) Rel
119 context
120 begin
121 interpretation lifting_syntax .
123 text {* Handling of domains *}
125 lemma Domainp_iff: "Domainp T x \<longleftrightarrow> (\<exists>y. T x y)"
126   by auto
128 lemma Domaimp_refl[transfer_domain_rule]:
129   "Domainp T = Domainp T" ..
131 lemma Domainp_prod_fun_eq[transfer_domain_rule]:
132   assumes "Domainp T = P"
133   shows "Domainp (op= ===> T) = (\<lambda>f. \<forall>x. P (f x))"
134 by (auto intro: choice simp: assms[symmetric] Domainp_iff rel_fun_def fun_eq_iff)
136 subsection {* Predicates on relations, i.e. ``class constraints'' *}
138 definition left_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
139   where "left_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y)"
141 definition left_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
142   where "left_unique R \<longleftrightarrow> (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
144 definition right_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
145   where "right_total R \<longleftrightarrow> (\<forall>y. \<exists>x. R x y)"
147 definition right_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
148   where "right_unique R \<longleftrightarrow> (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z)"
150 definition bi_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
151   where "bi_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y) \<and> (\<forall>y. \<exists>x. R x y)"
153 definition bi_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
154   where "bi_unique R \<longleftrightarrow>
155     (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z) \<and>
156     (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
158 lemma left_uniqueI: "(\<And>x y z. \<lbrakk> A x z; A y z \<rbrakk> \<Longrightarrow> x = y) \<Longrightarrow> left_unique A"
159 unfolding left_unique_def by blast
161 lemma left_uniqueD: "\<lbrakk> left_unique A; A x z; A y z \<rbrakk> \<Longrightarrow> x = y"
162 unfolding left_unique_def by blast
164 lemma left_totalI:
165   "(\<And>x. \<exists>y. R x y) \<Longrightarrow> left_total R"
166 unfolding left_total_def by blast
168 lemma left_totalE:
169   assumes "left_total R"
170   obtains "(\<And>x. \<exists>y. R x y)"
171 using assms unfolding left_total_def by blast
173 lemma bi_uniqueDr: "\<lbrakk> bi_unique A; A x y; A x z \<rbrakk> \<Longrightarrow> y = z"
176 lemma bi_uniqueDl: "\<lbrakk> bi_unique A; A x y; A z y \<rbrakk> \<Longrightarrow> x = z"
179 lemma right_uniqueI: "(\<And>x y z. \<lbrakk> A x y; A x z \<rbrakk> \<Longrightarrow> y = z) \<Longrightarrow> right_unique A"
180 unfolding right_unique_def by fast
182 lemma right_uniqueD: "\<lbrakk> right_unique A; A x y; A x z \<rbrakk> \<Longrightarrow> y = z"
183 unfolding right_unique_def by fast
185 lemma right_total_alt_def:
186   "right_total R \<longleftrightarrow> ((R ===> op \<longrightarrow>) ===> op \<longrightarrow>) All All"
187   unfolding right_total_def rel_fun_def
188   apply (rule iffI, fast)
189   apply (rule allI)
190   apply (drule_tac x="\<lambda>x. True" in spec)
191   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
192   apply fast
193   done
195 lemma right_unique_alt_def:
196   "right_unique R \<longleftrightarrow> (R ===> R ===> op \<longrightarrow>) (op =) (op =)"
197   unfolding right_unique_def rel_fun_def by auto
199 lemma bi_total_alt_def:
200   "bi_total R \<longleftrightarrow> ((R ===> op =) ===> op =) All All"
201   unfolding bi_total_def rel_fun_def
202   apply (rule iffI, fast)
203   apply safe
204   apply (drule_tac x="\<lambda>x. \<exists>y. R x y" in spec)
205   apply (drule_tac x="\<lambda>y. True" in spec)
206   apply fast
207   apply (drule_tac x="\<lambda>x. True" in spec)
208   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
209   apply fast
210   done
212 lemma bi_unique_alt_def:
213   "bi_unique R \<longleftrightarrow> (R ===> R ===> op =) (op =) (op =)"
214   unfolding bi_unique_def rel_fun_def by auto
216 lemma [simp]:
217   shows left_unique_conversep: "left_unique A\<inverse>\<inverse> \<longleftrightarrow> right_unique A"
218   and right_unique_conversep: "right_unique A\<inverse>\<inverse> \<longleftrightarrow> left_unique A"
219 by(auto simp add: left_unique_def right_unique_def)
221 lemma [simp]:
222   shows left_total_conversep: "left_total A\<inverse>\<inverse> \<longleftrightarrow> right_total A"
223   and right_total_conversep: "right_total A\<inverse>\<inverse> \<longleftrightarrow> left_total A"
226 lemma bi_unique_conversep [simp]: "bi_unique R\<inverse>\<inverse> = bi_unique R"
229 lemma bi_total_conversep [simp]: "bi_total R\<inverse>\<inverse> = bi_total R"
232 lemma bi_total_iff: "bi_total A = (right_total A \<and> left_total A)"
233 unfolding left_total_def right_total_def bi_total_def by blast
235 lemma bi_total_conv_left_right: "bi_total R \<longleftrightarrow> left_total R \<and> right_total R"
236 by(simp add: left_total_def right_total_def bi_total_def)
238 lemma bi_unique_iff: "bi_unique A  \<longleftrightarrow> right_unique A \<and> left_unique A"
239 unfolding left_unique_def right_unique_def bi_unique_def by blast
241 lemma bi_unique_conv_left_right: "bi_unique R \<longleftrightarrow> left_unique R \<and> right_unique R"
242 by(auto simp add: left_unique_def right_unique_def bi_unique_def)
244 lemma bi_totalI: "left_total R \<Longrightarrow> right_total R \<Longrightarrow> bi_total R"
245 unfolding bi_total_iff ..
247 lemma bi_uniqueI: "left_unique R \<Longrightarrow> right_unique R \<Longrightarrow> bi_unique R"
248 unfolding bi_unique_iff ..
251 text {* Properties are preserved by relation composition. *}
253 lemma OO_def: "R OO S = (\<lambda>x z. \<exists>y. R x y \<and> S y z)"
254   by auto
256 lemma bi_total_OO: "\<lbrakk>bi_total A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A OO B)"
257   unfolding bi_total_def OO_def by fast
259 lemma bi_unique_OO: "\<lbrakk>bi_unique A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A OO B)"
260   unfolding bi_unique_def OO_def by blast
262 lemma right_total_OO:
263   "\<lbrakk>right_total A; right_total B\<rbrakk> \<Longrightarrow> right_total (A OO B)"
264   unfolding right_total_def OO_def by fast
266 lemma right_unique_OO:
267   "\<lbrakk>right_unique A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A OO B)"
268   unfolding right_unique_def OO_def by fast
270 lemma left_total_OO: "left_total R \<Longrightarrow> left_total S \<Longrightarrow> left_total (R OO S)"
271 unfolding left_total_def OO_def by fast
273 lemma left_unique_OO: "left_unique R \<Longrightarrow> left_unique S \<Longrightarrow> left_unique (R OO S)"
274 unfolding left_unique_def OO_def by blast
277 subsection {* Properties of relators *}
279 lemma left_total_eq[transfer_rule]: "left_total op="
280   unfolding left_total_def by blast
282 lemma left_unique_eq[transfer_rule]: "left_unique op="
283   unfolding left_unique_def by blast
285 lemma right_total_eq [transfer_rule]: "right_total op="
286   unfolding right_total_def by simp
288 lemma right_unique_eq [transfer_rule]: "right_unique op="
289   unfolding right_unique_def by simp
291 lemma bi_total_eq[transfer_rule]: "bi_total (op =)"
292   unfolding bi_total_def by simp
294 lemma bi_unique_eq[transfer_rule]: "bi_unique (op =)"
295   unfolding bi_unique_def by simp
297 lemma left_total_fun[transfer_rule]:
298   "\<lbrakk>left_unique A; left_total B\<rbrakk> \<Longrightarrow> left_total (A ===> B)"
299   unfolding left_total_def rel_fun_def
300   apply (rule allI, rename_tac f)
301   apply (rule_tac x="\<lambda>y. SOME z. B (f (THE x. A x y)) z" in exI)
302   apply clarify
303   apply (subgoal_tac "(THE x. A x y) = x", simp)
304   apply (rule someI_ex)
305   apply (simp)
306   apply (rule the_equality)
307   apply assumption
309   done
311 lemma left_unique_fun[transfer_rule]:
312   "\<lbrakk>left_total A; left_unique B\<rbrakk> \<Longrightarrow> left_unique (A ===> B)"
313   unfolding left_total_def left_unique_def rel_fun_def
314   by (clarify, rule ext, fast)
316 lemma right_total_fun [transfer_rule]:
317   "\<lbrakk>right_unique A; right_total B\<rbrakk> \<Longrightarrow> right_total (A ===> B)"
318   unfolding right_total_def rel_fun_def
319   apply (rule allI, rename_tac g)
320   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
321   apply clarify
322   apply (subgoal_tac "(THE y. A x y) = y", simp)
323   apply (rule someI_ex)
324   apply (simp)
325   apply (rule the_equality)
326   apply assumption
328   done
330 lemma right_unique_fun [transfer_rule]:
331   "\<lbrakk>right_total A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A ===> B)"
332   unfolding right_total_def right_unique_def rel_fun_def
333   by (clarify, rule ext, fast)
335 lemma bi_total_fun[transfer_rule]:
336   "\<lbrakk>bi_unique A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A ===> B)"
337   unfolding bi_unique_iff bi_total_iff
338   by (blast intro: right_total_fun left_total_fun)
340 lemma bi_unique_fun[transfer_rule]:
341   "\<lbrakk>bi_total A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A ===> B)"
342   unfolding bi_unique_iff bi_total_iff
343   by (blast intro: right_unique_fun left_unique_fun)
345 subsection {* Transfer rules *}
347 lemma Domainp_forall_transfer [transfer_rule]:
348   assumes "right_total A"
349   shows "((A ===> op =) ===> op =)
350     (transfer_bforall (Domainp A)) transfer_forall"
351   using assms unfolding right_total_def
352   unfolding transfer_forall_def transfer_bforall_def rel_fun_def Domainp_iff
353   by fast
355 text {* Transfer rules using implication instead of equality on booleans. *}
357 lemma transfer_forall_transfer [transfer_rule]:
358   "bi_total A \<Longrightarrow> ((A ===> op =) ===> op =) transfer_forall transfer_forall"
359   "right_total A \<Longrightarrow> ((A ===> op =) ===> implies) transfer_forall transfer_forall"
360   "right_total A \<Longrightarrow> ((A ===> implies) ===> implies) transfer_forall transfer_forall"
361   "bi_total A \<Longrightarrow> ((A ===> op =) ===> rev_implies) transfer_forall transfer_forall"
362   "bi_total A \<Longrightarrow> ((A ===> rev_implies) ===> rev_implies) transfer_forall transfer_forall"
363   unfolding transfer_forall_def rev_implies_def rel_fun_def right_total_def bi_total_def
364   by fast+
366 lemma transfer_implies_transfer [transfer_rule]:
367   "(op =        ===> op =        ===> op =       ) transfer_implies transfer_implies"
368   "(rev_implies ===> implies     ===> implies    ) transfer_implies transfer_implies"
369   "(rev_implies ===> op =        ===> implies    ) transfer_implies transfer_implies"
370   "(op =        ===> implies     ===> implies    ) transfer_implies transfer_implies"
371   "(op =        ===> op =        ===> implies    ) transfer_implies transfer_implies"
372   "(implies     ===> rev_implies ===> rev_implies) transfer_implies transfer_implies"
373   "(implies     ===> op =        ===> rev_implies) transfer_implies transfer_implies"
374   "(op =        ===> rev_implies ===> rev_implies) transfer_implies transfer_implies"
375   "(op =        ===> op =        ===> rev_implies) transfer_implies transfer_implies"
376   unfolding transfer_implies_def rev_implies_def rel_fun_def by auto
378 lemma eq_imp_transfer [transfer_rule]:
379   "right_unique A \<Longrightarrow> (A ===> A ===> op \<longrightarrow>) (op =) (op =)"
380   unfolding right_unique_alt_def .
382 text {* Transfer rules using equality. *}
384 lemma left_unique_transfer [transfer_rule]:
385   assumes "right_total A"
386   assumes "right_total B"
387   assumes "bi_unique A"
388   shows "((A ===> B ===> op=) ===> implies) left_unique left_unique"
389 using assms unfolding left_unique_def[abs_def] right_total_def bi_unique_def rel_fun_def
390 by metis
392 lemma eq_transfer [transfer_rule]:
393   assumes "bi_unique A"
394   shows "(A ===> A ===> op =) (op =) (op =)"
395   using assms unfolding bi_unique_def rel_fun_def by auto
397 lemma right_total_Ex_transfer[transfer_rule]:
398   assumes "right_total A"
399   shows "((A ===> op=) ===> op=) (Bex (Collect (Domainp A))) Ex"
400 using assms unfolding right_total_def Bex_def rel_fun_def Domainp_iff[abs_def]
401 by fast
403 lemma right_total_All_transfer[transfer_rule]:
404   assumes "right_total A"
405   shows "((A ===> op =) ===> op =) (Ball (Collect (Domainp A))) All"
406 using assms unfolding right_total_def Ball_def rel_fun_def Domainp_iff[abs_def]
407 by fast
409 lemma All_transfer [transfer_rule]:
410   assumes "bi_total A"
411   shows "((A ===> op =) ===> op =) All All"
412   using assms unfolding bi_total_def rel_fun_def by fast
414 lemma Ex_transfer [transfer_rule]:
415   assumes "bi_total A"
416   shows "((A ===> op =) ===> op =) Ex Ex"
417   using assms unfolding bi_total_def rel_fun_def by fast
419 lemma If_transfer [transfer_rule]: "(op = ===> A ===> A ===> A) If If"
420   unfolding rel_fun_def by simp
422 lemma Let_transfer [transfer_rule]: "(A ===> (A ===> B) ===> B) Let Let"
423   unfolding rel_fun_def by simp
425 lemma id_transfer [transfer_rule]: "(A ===> A) id id"
426   unfolding rel_fun_def by simp
428 lemma comp_transfer [transfer_rule]:
429   "((B ===> C) ===> (A ===> B) ===> (A ===> C)) (op \<circ>) (op \<circ>)"
430   unfolding rel_fun_def by simp
432 lemma fun_upd_transfer [transfer_rule]:
433   assumes [transfer_rule]: "bi_unique A"
434   shows "((A ===> B) ===> A ===> B ===> A ===> B) fun_upd fun_upd"
435   unfolding fun_upd_def [abs_def] by transfer_prover
437 lemma case_nat_transfer [transfer_rule]:
438   "(A ===> (op = ===> A) ===> op = ===> A) case_nat case_nat"
439   unfolding rel_fun_def by (simp split: nat.split)
441 lemma rec_nat_transfer [transfer_rule]:
442   "(A ===> (op = ===> A ===> A) ===> op = ===> A) rec_nat rec_nat"
443   unfolding rel_fun_def by (clarsimp, rename_tac n, induct_tac n, simp_all)
445 lemma funpow_transfer [transfer_rule]:
446   "(op = ===> (A ===> A) ===> (A ===> A)) compow compow"
447   unfolding funpow_def by transfer_prover
449 lemma mono_transfer[transfer_rule]:
450   assumes [transfer_rule]: "bi_total A"
451   assumes [transfer_rule]: "(A ===> A ===> op=) op\<le> op\<le>"
452   assumes [transfer_rule]: "(B ===> B ===> op=) op\<le> op\<le>"
453   shows "((A ===> B) ===> op=) mono mono"
454 unfolding mono_def[abs_def] by transfer_prover
456 lemma right_total_relcompp_transfer[transfer_rule]:
457   assumes [transfer_rule]: "right_total B"
458   shows "((A ===> B ===> op=) ===> (B ===> C ===> op=) ===> A ===> C ===> op=)
459     (\<lambda>R S x z. \<exists>y\<in>Collect (Domainp B). R x y \<and> S y z) op OO"
460 unfolding OO_def[abs_def] by transfer_prover
462 lemma relcompp_transfer[transfer_rule]:
463   assumes [transfer_rule]: "bi_total B"
464   shows "((A ===> B ===> op=) ===> (B ===> C ===> op=) ===> A ===> C ===> op=) op OO op OO"
465 unfolding OO_def[abs_def] by transfer_prover
467 lemma right_total_Domainp_transfer[transfer_rule]:
468   assumes [transfer_rule]: "right_total B"
469   shows "((A ===> B ===> op=) ===> A ===> op=) (\<lambda>T x. \<exists>y\<in>Collect(Domainp B). T x y) Domainp"
470 apply(subst(2) Domainp_iff[abs_def]) by transfer_prover
472 lemma Domainp_transfer[transfer_rule]:
473   assumes [transfer_rule]: "bi_total B"
474   shows "((A ===> B ===> op=) ===> A ===> op=) Domainp Domainp"
475 unfolding Domainp_iff[abs_def] by transfer_prover
477 lemma reflp_transfer[transfer_rule]:
478   "bi_total A \<Longrightarrow> ((A ===> A ===> op=) ===> op=) reflp reflp"
479   "right_total A \<Longrightarrow> ((A ===> A ===> implies) ===> implies) reflp reflp"
480   "right_total A \<Longrightarrow> ((A ===> A ===> op=) ===> implies) reflp reflp"
481   "bi_total A \<Longrightarrow> ((A ===> A ===> rev_implies) ===> rev_implies) reflp reflp"
482   "bi_total A \<Longrightarrow> ((A ===> A ===> op=) ===> rev_implies) reflp reflp"
483 using assms unfolding reflp_def[abs_def] rev_implies_def bi_total_def right_total_def rel_fun_def
484 by fast+
486 lemma right_unique_transfer [transfer_rule]:
487   assumes [transfer_rule]: "right_total A"
488   assumes [transfer_rule]: "right_total B"
489   assumes [transfer_rule]: "bi_unique B"
490   shows "((A ===> B ===> op=) ===> implies) right_unique right_unique"
491 using assms unfolding right_unique_def[abs_def] right_total_def bi_unique_def rel_fun_def
492 by metis
494 end
496 end