src/HOL/Library/Quotient_Sum.thy
 author huffman Fri Apr 20 14:57:19 2012 +0200 (2012-04-20) changeset 47624 16d433895d2e parent 47455 26315a545e26 child 47634 091bcd569441 permissions -rw-r--r--
add new transfer rules and setup for lifting package
1 (*  Title:      HOL/Library/Quotient_Sum.thy
2     Author:     Cezary Kaliszyk, Christian Urban and Brian Huffman
3 *)
5 header {* Quotient infrastructure for the sum type *}
7 theory Quotient_Sum
8 imports Main Quotient_Syntax
9 begin
11 subsection {* Relator for sum type *}
13 fun
14   sum_rel :: "('a \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> 'a + 'b \<Rightarrow> 'c + 'd \<Rightarrow> bool"
15 where
16   "sum_rel R1 R2 (Inl a1) (Inl b1) = R1 a1 b1"
17 | "sum_rel R1 R2 (Inl a1) (Inr b2) = False"
18 | "sum_rel R1 R2 (Inr a2) (Inl b1) = False"
19 | "sum_rel R1 R2 (Inr a2) (Inr b2) = R2 a2 b2"
21 lemma sum_rel_unfold:
22   "sum_rel R1 R2 x y = (case (x, y) of (Inl x, Inl y) \<Rightarrow> R1 x y
23     | (Inr x, Inr y) \<Rightarrow> R2 x y
24     | _ \<Rightarrow> False)"
25   by (cases x) (cases y, simp_all)+
27 lemma sum_rel_map1:
28   "sum_rel R1 R2 (sum_map f1 f2 x) y \<longleftrightarrow> sum_rel (\<lambda>x. R1 (f1 x)) (\<lambda>x. R2 (f2 x)) x y"
29   by (simp add: sum_rel_unfold split: sum.split)
31 lemma sum_rel_map2:
32   "sum_rel R1 R2 x (sum_map f1 f2 y) \<longleftrightarrow> sum_rel (\<lambda>x y. R1 x (f1 y)) (\<lambda>x y. R2 x (f2 y)) x y"
33   by (simp add: sum_rel_unfold split: sum.split)
35 lemma sum_map_id [id_simps]:
36   "sum_map id id = id"
37   by (simp add: id_def sum_map.identity fun_eq_iff)
39 lemma sum_rel_eq [id_simps, relator_eq]:
40   "sum_rel (op =) (op =) = (op =)"
41   by (simp add: sum_rel_unfold fun_eq_iff split: sum.split)
43 lemma split_sum_all: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P (Inl x)) \<and> (\<forall>x. P (Inr x))"
44   by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)
46 lemma split_sum_ex: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P (Inl x)) \<or> (\<exists>x. P (Inr x))"
47   by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)
49 lemma sum_reflp:
50   "reflp R1 \<Longrightarrow> reflp R2 \<Longrightarrow> reflp (sum_rel R1 R2)"
51   unfolding reflp_def split_sum_all sum_rel.simps by fast
53 lemma sum_symp:
54   "symp R1 \<Longrightarrow> symp R2 \<Longrightarrow> symp (sum_rel R1 R2)"
55   unfolding symp_def split_sum_all sum_rel.simps by fast
57 lemma sum_transp:
58   "transp R1 \<Longrightarrow> transp R2 \<Longrightarrow> transp (sum_rel R1 R2)"
59   unfolding transp_def split_sum_all sum_rel.simps by fast
61 lemma sum_equivp [quot_equiv]:
62   "equivp R1 \<Longrightarrow> equivp R2 \<Longrightarrow> equivp (sum_rel R1 R2)"
63   by (blast intro: equivpI sum_reflp sum_symp sum_transp elim: equivpE)
65 lemma right_total_sum_rel [transfer_rule]:
66   "right_total R1 \<Longrightarrow> right_total R2 \<Longrightarrow> right_total (sum_rel R1 R2)"
67   unfolding right_total_def split_sum_all split_sum_ex by simp
69 lemma right_unique_sum_rel [transfer_rule]:
70   "right_unique R1 \<Longrightarrow> right_unique R2 \<Longrightarrow> right_unique (sum_rel R1 R2)"
71   unfolding right_unique_def split_sum_all by simp
73 lemma bi_total_sum_rel [transfer_rule]:
74   "bi_total R1 \<Longrightarrow> bi_total R2 \<Longrightarrow> bi_total (sum_rel R1 R2)"
75   using assms unfolding bi_total_def split_sum_all split_sum_ex by simp
77 lemma bi_unique_sum_rel [transfer_rule]:
78   "bi_unique R1 \<Longrightarrow> bi_unique R2 \<Longrightarrow> bi_unique (sum_rel R1 R2)"
79   using assms unfolding bi_unique_def split_sum_all by simp
81 subsection {* Correspondence rules for transfer package *}
83 lemma Inl_transfer [transfer_rule]: "(A ===> sum_rel A B) Inl Inl"
84   unfolding fun_rel_def by simp
86 lemma Inr_transfer [transfer_rule]: "(B ===> sum_rel A B) Inr Inr"
87   unfolding fun_rel_def by simp
89 lemma sum_case_transfer [transfer_rule]:
90   "((A ===> C) ===> (B ===> C) ===> sum_rel A B ===> C) sum_case sum_case"
91   unfolding fun_rel_def sum_rel_unfold by (simp split: sum.split)
93 subsection {* Setup for lifting package *}
95 lemma Quotient_sum:
96   assumes "Quotient R1 Abs1 Rep1 T1"
97   assumes "Quotient R2 Abs2 Rep2 T2"
98   shows "Quotient (sum_rel R1 R2) (sum_map Abs1 Abs2)
99     (sum_map Rep1 Rep2) (sum_rel T1 T2)"
100   using assms unfolding Quotient_alt_def
103 declare [[map sum = (sum_rel, Quotient_sum)]]
105 subsection {* Rules for quotient package *}
107 lemma sum_quotient [quot_thm]:
108   assumes q1: "Quotient3 R1 Abs1 Rep1"
109   assumes q2: "Quotient3 R2 Abs2 Rep2"
110   shows "Quotient3 (sum_rel R1 R2) (sum_map Abs1 Abs2) (sum_map Rep1 Rep2)"
111   apply (rule Quotient3I)
112   apply (simp_all add: sum_map.compositionality comp_def sum_map.identity sum_rel_eq sum_rel_map1 sum_rel_map2
113     Quotient3_abs_rep [OF q1] Quotient3_rel_rep [OF q1] Quotient3_abs_rep [OF q2] Quotient3_rel_rep [OF q2])
114   using Quotient3_rel [OF q1] Quotient3_rel [OF q2]
115   apply (simp add: sum_rel_unfold comp_def split: sum.split)
116   done
118 declare [[mapQ3 sum = (sum_rel, sum_quotient)]]
120 lemma sum_Inl_rsp [quot_respect]:
121   assumes q1: "Quotient3 R1 Abs1 Rep1"
122   assumes q2: "Quotient3 R2 Abs2 Rep2"
123   shows "(R1 ===> sum_rel R1 R2) Inl Inl"
124   by auto
126 lemma sum_Inr_rsp [quot_respect]:
127   assumes q1: "Quotient3 R1 Abs1 Rep1"
128   assumes q2: "Quotient3 R2 Abs2 Rep2"
129   shows "(R2 ===> sum_rel R1 R2) Inr Inr"
130   by auto
132 lemma sum_Inl_prs [quot_preserve]:
133   assumes q1: "Quotient3 R1 Abs1 Rep1"
134   assumes q2: "Quotient3 R2 Abs2 Rep2"
135   shows "(Rep1 ---> sum_map Abs1 Abs2) Inl = Inl"