# Theory Quotient_Sum

Up to index of Isabelle/HOL/HOL-Library

theory Quotient_Sum
imports Quotient_Syntax
`(*  Title:      HOL/Library/Quotient_Sum.thy    Author:     Cezary Kaliszyk, Christian Urban and Brian Huffman*)header {* Quotient infrastructure for the sum type *}theory Quotient_Sumimports Main Quotient_Syntaxbeginsubsection {* Relator for sum type *}fun  sum_rel :: "('a => 'c => bool) => ('b => 'd => bool) => 'a + 'b => 'c + 'd => bool"where  "sum_rel R1 R2 (Inl a1) (Inl b1) = R1 a1 b1"| "sum_rel R1 R2 (Inl a1) (Inr b2) = False"| "sum_rel R1 R2 (Inr a2) (Inl b1) = False"| "sum_rel R1 R2 (Inr a2) (Inr b2) = R2 a2 b2"lemma sum_rel_unfold:  "sum_rel R1 R2 x y = (case (x, y) of (Inl x, Inl y) => R1 x y    | (Inr x, Inr y) => R2 x y    | _ => False)"  by (cases x) (cases y, simp_all)+lemma sum_rel_map1:  "sum_rel R1 R2 (sum_map f1 f2 x) y <-> sum_rel (λx. R1 (f1 x)) (λx. R2 (f2 x)) x y"  by (simp add: sum_rel_unfold split: sum.split)lemma sum_rel_map2:  "sum_rel R1 R2 x (sum_map f1 f2 y) <-> sum_rel (λx y. R1 x (f1 y)) (λx y. R2 x (f2 y)) x y"  by (simp add: sum_rel_unfold split: sum.split)lemma sum_map_id [id_simps]:  "sum_map id id = id"  by (simp add: id_def sum_map.identity fun_eq_iff)lemma sum_rel_eq [id_simps, relator_eq]:  "sum_rel (op =) (op =) = (op =)"  by (simp add: sum_rel_unfold fun_eq_iff split: sum.split)lemma split_sum_all: "(∀x. P x) <-> (∀x. P (Inl x)) ∧ (∀x. P (Inr x))"  by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)lemma split_sum_ex: "(∃x. P x) <-> (∃x. P (Inl x)) ∨ (∃x. P (Inr x))"  by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)lemma sum_reflp[reflexivity_rule]:  "reflp R1 ==> reflp R2 ==> reflp (sum_rel R1 R2)"  unfolding reflp_def split_sum_all sum_rel.simps by fastlemma sum_left_total[reflexivity_rule]:  "left_total R1 ==> left_total R2 ==> left_total (sum_rel R1 R2)"  apply (intro left_totalI)  unfolding split_sum_ex   by (case_tac x) (auto elim: left_totalE)lemma sum_symp:  "symp R1 ==> symp R2 ==> symp (sum_rel R1 R2)"  unfolding symp_def split_sum_all sum_rel.simps by fastlemma sum_transp:  "transp R1 ==> transp R2 ==> transp (sum_rel R1 R2)"  unfolding transp_def split_sum_all sum_rel.simps by fastlemma sum_equivp [quot_equiv]:  "equivp R1 ==> equivp R2 ==> equivp (sum_rel R1 R2)"  by (blast intro: equivpI sum_reflp sum_symp sum_transp elim: equivpE)lemma right_total_sum_rel [transfer_rule]:  "right_total R1 ==> right_total R2 ==> right_total (sum_rel R1 R2)"  unfolding right_total_def split_sum_all split_sum_ex by simplemma right_unique_sum_rel [transfer_rule]:  "right_unique R1 ==> right_unique R2 ==> right_unique (sum_rel R1 R2)"  unfolding right_unique_def split_sum_all by simplemma bi_total_sum_rel [transfer_rule]:  "bi_total R1 ==> bi_total R2 ==> bi_total (sum_rel R1 R2)"  using assms unfolding bi_total_def split_sum_all split_sum_ex by simplemma bi_unique_sum_rel [transfer_rule]:  "bi_unique R1 ==> bi_unique R2 ==> bi_unique (sum_rel R1 R2)"  using assms unfolding bi_unique_def split_sum_all by simpsubsection {* Transfer rules for transfer package *}lemma Inl_transfer [transfer_rule]: "(A ===> sum_rel A B) Inl Inl"  unfolding fun_rel_def by simplemma Inr_transfer [transfer_rule]: "(B ===> sum_rel A B) Inr Inr"  unfolding fun_rel_def by simplemma sum_case_transfer [transfer_rule]:  "((A ===> C) ===> (B ===> C) ===> sum_rel A B ===> C) sum_case sum_case"  unfolding fun_rel_def sum_rel_unfold by (simp split: sum.split)subsection {* Setup for lifting package *}lemma Quotient_sum[quot_map]:  assumes "Quotient R1 Abs1 Rep1 T1"  assumes "Quotient R2 Abs2 Rep2 T2"  shows "Quotient (sum_rel R1 R2) (sum_map Abs1 Abs2)    (sum_map Rep1 Rep2) (sum_rel T1 T2)"  using assms unfolding Quotient_alt_def  by (simp add: split_sum_all)fun sum_pred :: "('a => bool) => ('b => bool) => 'a + 'b => bool"where  "sum_pred R1 R2 (Inl a) = R1 a"| "sum_pred R1 R2 (Inr a) = R2 a"lemma sum_invariant_commute [invariant_commute]:   "sum_rel (Lifting.invariant P1) (Lifting.invariant P2) = Lifting.invariant (sum_pred P1 P2)"  apply (simp add: fun_eq_iff Lifting.invariant_def)  apply (intro allI)   apply (case_tac x rule: sum.exhaust)  apply (case_tac xa rule: sum.exhaust)  apply auto[2]  apply (case_tac xa rule: sum.exhaust)  apply autodonesubsection {* Rules for quotient package *}lemma sum_quotient [quot_thm]:  assumes q1: "Quotient3 R1 Abs1 Rep1"  assumes q2: "Quotient3 R2 Abs2 Rep2"  shows "Quotient3 (sum_rel R1 R2) (sum_map Abs1 Abs2) (sum_map Rep1 Rep2)"  apply (rule Quotient3I)  apply (simp_all add: sum_map.compositionality comp_def sum_map.identity sum_rel_eq sum_rel_map1 sum_rel_map2    Quotient3_abs_rep [OF q1] Quotient3_rel_rep [OF q1] Quotient3_abs_rep [OF q2] Quotient3_rel_rep [OF q2])  using Quotient3_rel [OF q1] Quotient3_rel [OF q2]  apply (simp add: sum_rel_unfold comp_def split: sum.split)  donedeclare [[mapQ3 sum = (sum_rel, sum_quotient)]]lemma sum_Inl_rsp [quot_respect]:  assumes q1: "Quotient3 R1 Abs1 Rep1"  assumes q2: "Quotient3 R2 Abs2 Rep2"  shows "(R1 ===> sum_rel R1 R2) Inl Inl"  by autolemma sum_Inr_rsp [quot_respect]:  assumes q1: "Quotient3 R1 Abs1 Rep1"  assumes q2: "Quotient3 R2 Abs2 Rep2"  shows "(R2 ===> sum_rel R1 R2) Inr Inr"  by autolemma sum_Inl_prs [quot_preserve]:  assumes q1: "Quotient3 R1 Abs1 Rep1"  assumes q2: "Quotient3 R2 Abs2 Rep2"  shows "(Rep1 ---> sum_map Abs1 Abs2) Inl = Inl"  apply(simp add: fun_eq_iff)  apply(simp add: Quotient3_abs_rep[OF q1])  donelemma sum_Inr_prs [quot_preserve]:  assumes q1: "Quotient3 R1 Abs1 Rep1"  assumes q2: "Quotient3 R2 Abs2 Rep2"  shows "(Rep2 ---> sum_map Abs1 Abs2) Inr = Inr"  apply(simp add: fun_eq_iff)  apply(simp add: Quotient3_abs_rep[OF q2])  doneend`