src/HOL/Library/Quotient_Sum.thy
author Christian Sternagel
Wed Aug 29 12:23:14 2012 +0900 (2012-08-29)
changeset 49083 01081bca31b6
parent 47982 7aa35601ff65
child 51377 7da251a6c16e
permissions -rw-r--r--
dropped ord and bot instance for list prefixes (use locale interpretation instead, which allows users to decide what order to use on lists)
     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 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)
    30 
    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)
    34 
    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)
    38 
    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)
    42 
    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 *)
    45 
    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 *)
    48 
    49 lemma sum_reflp[reflexivity_rule]:
    50   "reflp R1 \<Longrightarrow> reflp R2 \<Longrightarrow> reflp (sum_rel R1 R2)"
    51   unfolding reflp_def split_sum_all sum_rel.simps by fast
    52 
    53 lemma sum_left_total[reflexivity_rule]:
    54   "left_total R1 \<Longrightarrow> left_total R2 \<Longrightarrow> left_total (sum_rel R1 R2)"
    55   apply (intro left_totalI)
    56   unfolding split_sum_ex 
    57   by (case_tac x) (auto elim: left_totalE)
    58 
    59 lemma sum_symp:
    60   "symp R1 \<Longrightarrow> symp R2 \<Longrightarrow> symp (sum_rel R1 R2)"
    61   unfolding symp_def split_sum_all sum_rel.simps by fast
    62 
    63 lemma sum_transp:
    64   "transp R1 \<Longrightarrow> transp R2 \<Longrightarrow> transp (sum_rel R1 R2)"
    65   unfolding transp_def split_sum_all sum_rel.simps by fast
    66 
    67 lemma sum_equivp [quot_equiv]:
    68   "equivp R1 \<Longrightarrow> equivp R2 \<Longrightarrow> equivp (sum_rel R1 R2)"
    69   by (blast intro: equivpI sum_reflp sum_symp sum_transp elim: equivpE)
    70 
    71 lemma right_total_sum_rel [transfer_rule]:
    72   "right_total R1 \<Longrightarrow> right_total R2 \<Longrightarrow> right_total (sum_rel R1 R2)"
    73   unfolding right_total_def split_sum_all split_sum_ex by simp
    74 
    75 lemma right_unique_sum_rel [transfer_rule]:
    76   "right_unique R1 \<Longrightarrow> right_unique R2 \<Longrightarrow> right_unique (sum_rel R1 R2)"
    77   unfolding right_unique_def split_sum_all by simp
    78 
    79 lemma bi_total_sum_rel [transfer_rule]:
    80   "bi_total R1 \<Longrightarrow> bi_total R2 \<Longrightarrow> bi_total (sum_rel R1 R2)"
    81   using assms unfolding bi_total_def split_sum_all split_sum_ex by simp
    82 
    83 lemma bi_unique_sum_rel [transfer_rule]:
    84   "bi_unique R1 \<Longrightarrow> bi_unique R2 \<Longrightarrow> bi_unique (sum_rel R1 R2)"
    85   using assms unfolding bi_unique_def split_sum_all by simp
    86 
    87 subsection {* Transfer rules for transfer package *}
    88 
    89 lemma Inl_transfer [transfer_rule]: "(A ===> sum_rel A B) Inl Inl"
    90   unfolding fun_rel_def by simp
    91 
    92 lemma Inr_transfer [transfer_rule]: "(B ===> sum_rel A B) Inr Inr"
    93   unfolding fun_rel_def by simp
    94 
    95 lemma sum_case_transfer [transfer_rule]:
    96   "((A ===> C) ===> (B ===> C) ===> sum_rel A B ===> C) sum_case sum_case"
    97   unfolding fun_rel_def sum_rel_unfold by (simp split: sum.split)
    98 
    99 subsection {* Setup for lifting package *}
   100 
   101 lemma Quotient_sum[quot_map]:
   102   assumes "Quotient R1 Abs1 Rep1 T1"
   103   assumes "Quotient R2 Abs2 Rep2 T2"
   104   shows "Quotient (sum_rel R1 R2) (sum_map Abs1 Abs2)
   105     (sum_map Rep1 Rep2) (sum_rel T1 T2)"
   106   using assms unfolding Quotient_alt_def
   107   by (simp add: split_sum_all)
   108 
   109 fun sum_pred :: "('a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> 'a + 'b \<Rightarrow> bool"
   110 where
   111   "sum_pred R1 R2 (Inl a) = R1 a"
   112 | "sum_pred R1 R2 (Inr a) = R2 a"
   113 
   114 lemma sum_invariant_commute [invariant_commute]: 
   115   "sum_rel (Lifting.invariant P1) (Lifting.invariant P2) = Lifting.invariant (sum_pred P1 P2)"
   116   apply (simp add: fun_eq_iff Lifting.invariant_def)
   117   apply (intro allI) 
   118   apply (case_tac x rule: sum.exhaust)
   119   apply (case_tac xa rule: sum.exhaust)
   120   apply auto[2]
   121   apply (case_tac xa rule: sum.exhaust)
   122   apply auto
   123 done
   124 
   125 subsection {* Rules for quotient package *}
   126 
   127 lemma sum_quotient [quot_thm]:
   128   assumes q1: "Quotient3 R1 Abs1 Rep1"
   129   assumes q2: "Quotient3 R2 Abs2 Rep2"
   130   shows "Quotient3 (sum_rel R1 R2) (sum_map Abs1 Abs2) (sum_map Rep1 Rep2)"
   131   apply (rule Quotient3I)
   132   apply (simp_all add: sum_map.compositionality comp_def sum_map.identity sum_rel_eq sum_rel_map1 sum_rel_map2
   133     Quotient3_abs_rep [OF q1] Quotient3_rel_rep [OF q1] Quotient3_abs_rep [OF q2] Quotient3_rel_rep [OF q2])
   134   using Quotient3_rel [OF q1] Quotient3_rel [OF q2]
   135   apply (simp add: sum_rel_unfold comp_def split: sum.split)
   136   done
   137 
   138 declare [[mapQ3 sum = (sum_rel, sum_quotient)]]
   139 
   140 lemma sum_Inl_rsp [quot_respect]:
   141   assumes q1: "Quotient3 R1 Abs1 Rep1"
   142   assumes q2: "Quotient3 R2 Abs2 Rep2"
   143   shows "(R1 ===> sum_rel R1 R2) Inl Inl"
   144   by auto
   145 
   146 lemma sum_Inr_rsp [quot_respect]:
   147   assumes q1: "Quotient3 R1 Abs1 Rep1"
   148   assumes q2: "Quotient3 R2 Abs2 Rep2"
   149   shows "(R2 ===> sum_rel R1 R2) Inr Inr"
   150   by auto
   151 
   152 lemma sum_Inl_prs [quot_preserve]:
   153   assumes q1: "Quotient3 R1 Abs1 Rep1"
   154   assumes q2: "Quotient3 R2 Abs2 Rep2"
   155   shows "(Rep1 ---> sum_map Abs1 Abs2) Inl = Inl"
   156   apply(simp add: fun_eq_iff)
   157   apply(simp add: Quotient3_abs_rep[OF q1])
   158   done
   159 
   160 lemma sum_Inr_prs [quot_preserve]:
   161   assumes q1: "Quotient3 R1 Abs1 Rep1"
   162   assumes q2: "Quotient3 R2 Abs2 Rep2"
   163   shows "(Rep2 ---> sum_map Abs1 Abs2) Inr = Inr"
   164   apply(simp add: fun_eq_iff)
   165   apply(simp add: Quotient3_abs_rep[OF q2])
   166   done
   167 
   168 end