src/HOL/Library/Quotient_Sum.thy
author kuncar
Wed May 15 12:10:39 2013 +0200 (2013-05-15)
changeset 51994 82cc2aeb7d13
parent 51956 a4d81cdebf8b
child 53010 ec5e6f69bd65
permissions -rw-r--r--
stronger reflexivity prover
     1 (*  Title:      HOL/Library/Quotient_Sum.thy
     2     Author:     Cezary Kaliszyk, Christian Urban and Brian Huffman
     3 *)
     4 
     5 header {* Quotient infrastructure for the sum type *}
     6 
     7 theory Quotient_Sum
     8 imports Main Quotient_Syntax
     9 begin
    10 
    11 subsection {* Relator for sum type *}
    12 
    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"
    20 
    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)+
    26 
    27 fun sum_pred :: "('a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> 'a + 'b \<Rightarrow> bool"
    28 where
    29   "sum_pred P1 P2 (Inl a) = P1 a"
    30 | "sum_pred P1 P2 (Inr a) = P2 a"
    31 
    32 lemma sum_pred_unfold:
    33   "sum_pred P1 P2 x = (case x of Inl x \<Rightarrow> P1 x
    34     | Inr x \<Rightarrow> P2 x)"
    35 by (cases x) simp_all
    36 
    37 lemma sum_rel_map1:
    38   "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"
    39   by (simp add: sum_rel_unfold split: sum.split)
    40 
    41 lemma sum_rel_map2:
    42   "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"
    43   by (simp add: sum_rel_unfold split: sum.split)
    44 
    45 lemma sum_map_id [id_simps]:
    46   "sum_map id id = id"
    47   by (simp add: id_def sum_map.identity fun_eq_iff)
    48 
    49 lemma sum_rel_eq [id_simps, relator_eq]:
    50   "sum_rel (op =) (op =) = (op =)"
    51   by (simp add: sum_rel_unfold fun_eq_iff split: sum.split)
    52 
    53 lemma split_sum_all: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x. P (Inl x)) \<and> (\<forall>x. P (Inr x))"
    54   by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)
    55 
    56 lemma split_sum_ex: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. P (Inl x)) \<or> (\<exists>x. P (Inr x))"
    57   by (metis sum.exhaust) (* TODO: move to Sum_Type.thy *)
    58 
    59 lemma sum_rel_mono[relator_mono]:
    60   assumes "A \<le> C"
    61   assumes "B \<le> D"
    62   shows "(sum_rel A B) \<le> (sum_rel C D)"
    63 using assms by (auto simp: sum_rel_unfold split: sum.splits)
    64 
    65 lemma sum_rel_OO[relator_distr]:
    66   "(sum_rel A B) OO (sum_rel C D) = sum_rel (A OO C) (B OO D)"
    67 by (rule ext)+ (auto simp add: sum_rel_unfold OO_def split_sum_ex split: sum.split)
    68 
    69 lemma Domainp_sum[relator_domain]:
    70   assumes "Domainp R1 = P1"
    71   assumes "Domainp R2 = P2"
    72   shows "Domainp (sum_rel R1 R2) = (sum_pred P1 P2)"
    73 using assms
    74 by (auto simp add: Domainp_iff split_sum_ex sum_pred_unfold iff: fun_eq_iff split: sum.split)
    75 
    76 lemma reflp_sum_rel[reflexivity_rule]:
    77   "reflp R1 \<Longrightarrow> reflp R2 \<Longrightarrow> reflp (sum_rel R1 R2)"
    78   unfolding reflp_def split_sum_all sum_rel.simps by fast
    79 
    80 lemma left_total_sum_rel[reflexivity_rule]:
    81   "left_total R1 \<Longrightarrow> left_total R2 \<Longrightarrow> left_total (sum_rel R1 R2)"
    82   using assms unfolding left_total_def split_sum_all split_sum_ex by simp
    83 
    84 lemma left_unique_sum_rel [reflexivity_rule]:
    85   "left_unique R1 \<Longrightarrow> left_unique R2 \<Longrightarrow> left_unique (sum_rel R1 R2)"
    86   using assms unfolding left_unique_def split_sum_all by simp
    87 
    88 lemma sum_symp:
    89   "symp R1 \<Longrightarrow> symp R2 \<Longrightarrow> symp (sum_rel R1 R2)"
    90   unfolding symp_def split_sum_all sum_rel.simps by fast
    91 
    92 lemma sum_transp:
    93   "transp R1 \<Longrightarrow> transp R2 \<Longrightarrow> transp (sum_rel R1 R2)"
    94   unfolding transp_def split_sum_all sum_rel.simps by fast
    95 
    96 lemma sum_equivp [quot_equiv]:
    97   "equivp R1 \<Longrightarrow> equivp R2 \<Longrightarrow> equivp (sum_rel R1 R2)"
    98   by (blast intro: equivpI reflp_sum_rel sum_symp sum_transp elim: equivpE)
    99 
   100 lemma right_total_sum_rel [transfer_rule]:
   101   "right_total R1 \<Longrightarrow> right_total R2 \<Longrightarrow> right_total (sum_rel R1 R2)"
   102   unfolding right_total_def split_sum_all split_sum_ex by simp
   103 
   104 lemma right_unique_sum_rel [transfer_rule]:
   105   "right_unique R1 \<Longrightarrow> right_unique R2 \<Longrightarrow> right_unique (sum_rel R1 R2)"
   106   unfolding right_unique_def split_sum_all by simp
   107 
   108 lemma bi_total_sum_rel [transfer_rule]:
   109   "bi_total R1 \<Longrightarrow> bi_total R2 \<Longrightarrow> bi_total (sum_rel R1 R2)"
   110   using assms unfolding bi_total_def split_sum_all split_sum_ex by simp
   111 
   112 lemma bi_unique_sum_rel [transfer_rule]:
   113   "bi_unique R1 \<Longrightarrow> bi_unique R2 \<Longrightarrow> bi_unique (sum_rel R1 R2)"
   114   using assms unfolding bi_unique_def split_sum_all by simp
   115 
   116 subsection {* Transfer rules for transfer package *}
   117 
   118 lemma Inl_transfer [transfer_rule]: "(A ===> sum_rel A B) Inl Inl"
   119   unfolding fun_rel_def by simp
   120 
   121 lemma Inr_transfer [transfer_rule]: "(B ===> sum_rel A B) Inr Inr"
   122   unfolding fun_rel_def by simp
   123 
   124 lemma sum_case_transfer [transfer_rule]:
   125   "((A ===> C) ===> (B ===> C) ===> sum_rel A B ===> C) sum_case sum_case"
   126   unfolding fun_rel_def sum_rel_unfold by (simp split: sum.split)
   127 
   128 subsection {* Setup for lifting package *}
   129 
   130 lemma Quotient_sum[quot_map]:
   131   assumes "Quotient R1 Abs1 Rep1 T1"
   132   assumes "Quotient R2 Abs2 Rep2 T2"
   133   shows "Quotient (sum_rel R1 R2) (sum_map Abs1 Abs2)
   134     (sum_map Rep1 Rep2) (sum_rel T1 T2)"
   135   using assms unfolding Quotient_alt_def
   136   by (simp add: split_sum_all)
   137 
   138 lemma sum_invariant_commute [invariant_commute]: 
   139   "sum_rel (Lifting.invariant P1) (Lifting.invariant P2) = Lifting.invariant (sum_pred P1 P2)"
   140   by (auto simp add: fun_eq_iff Lifting.invariant_def sum_rel_unfold sum_pred_unfold split: sum.split)
   141 
   142 subsection {* Rules for quotient package *}
   143 
   144 lemma sum_quotient [quot_thm]:
   145   assumes q1: "Quotient3 R1 Abs1 Rep1"
   146   assumes q2: "Quotient3 R2 Abs2 Rep2"
   147   shows "Quotient3 (sum_rel R1 R2) (sum_map Abs1 Abs2) (sum_map Rep1 Rep2)"
   148   apply (rule Quotient3I)
   149   apply (simp_all add: sum_map.compositionality comp_def sum_map.identity sum_rel_eq sum_rel_map1 sum_rel_map2
   150     Quotient3_abs_rep [OF q1] Quotient3_rel_rep [OF q1] Quotient3_abs_rep [OF q2] Quotient3_rel_rep [OF q2])
   151   using Quotient3_rel [OF q1] Quotient3_rel [OF q2]
   152   apply (simp add: sum_rel_unfold comp_def split: sum.split)
   153   done
   154 
   155 declare [[mapQ3 sum = (sum_rel, sum_quotient)]]
   156 
   157 lemma sum_Inl_rsp [quot_respect]:
   158   assumes q1: "Quotient3 R1 Abs1 Rep1"
   159   assumes q2: "Quotient3 R2 Abs2 Rep2"
   160   shows "(R1 ===> sum_rel R1 R2) Inl Inl"
   161   by auto
   162 
   163 lemma sum_Inr_rsp [quot_respect]:
   164   assumes q1: "Quotient3 R1 Abs1 Rep1"
   165   assumes q2: "Quotient3 R2 Abs2 Rep2"
   166   shows "(R2 ===> sum_rel R1 R2) Inr Inr"
   167   by auto
   168 
   169 lemma sum_Inl_prs [quot_preserve]:
   170   assumes q1: "Quotient3 R1 Abs1 Rep1"
   171   assumes q2: "Quotient3 R2 Abs2 Rep2"
   172   shows "(Rep1 ---> sum_map Abs1 Abs2) Inl = Inl"
   173   apply(simp add: fun_eq_iff)
   174   apply(simp add: Quotient3_abs_rep[OF q1])
   175   done
   176 
   177 lemma sum_Inr_prs [quot_preserve]:
   178   assumes q1: "Quotient3 R1 Abs1 Rep1"
   179   assumes q2: "Quotient3 R2 Abs2 Rep2"
   180   shows "(Rep2 ---> sum_map Abs1 Abs2) Inr = Inr"
   181   apply(simp add: fun_eq_iff)
   182   apply(simp add: Quotient3_abs_rep[OF q2])
   183   done
   184 
   185 end