src/HOL/Library/Quotient_List.thy
author kuncar
Tue Aug 13 15:59:22 2013 +0200 (2013-08-13)
changeset 53012 cb82606b8215
parent 52308 299b35e3054b
child 55564 e81ee43ab290
permissions -rw-r--r--
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
     1 (*  Title:      HOL/Library/Quotient_List.thy
     2     Author:     Cezary Kaliszyk and Christian Urban
     3 *)
     4 
     5 header {* Quotient infrastructure for the list type *}
     6 
     7 theory Quotient_List
     8 imports Main Quotient_Set Quotient_Product Quotient_Option
     9 begin
    10 
    11 subsection {* Rules for the Quotient package *}
    12 
    13 lemma map_id [id_simps]:
    14   "map id = id"
    15   by (fact List.map.id)
    16 
    17 lemma list_all2_eq [id_simps]:
    18   "list_all2 (op =) = (op =)"
    19 proof (rule ext)+
    20   fix xs ys
    21   show "list_all2 (op =) xs ys \<longleftrightarrow> xs = ys"
    22     by (induct xs ys rule: list_induct2') simp_all
    23 qed
    24 
    25 lemma list_symp:
    26   assumes "symp R"
    27   shows "symp (list_all2 R)"
    28 proof (rule sympI)
    29   from assms have *: "\<And>xs ys. R xs ys \<Longrightarrow> R ys xs" by (rule sympE)
    30   fix xs ys
    31   assume "list_all2 R xs ys"
    32   then show "list_all2 R ys xs"
    33     by (induct xs ys rule: list_induct2') (simp_all add: *)
    34 qed
    35 
    36 lemma list_transp:
    37   assumes "transp R"
    38   shows "transp (list_all2 R)"
    39 proof (rule transpI)
    40   from assms have *: "\<And>xs ys zs. R xs ys \<Longrightarrow> R ys zs \<Longrightarrow> R xs zs" by (rule transpE)
    41   fix xs ys zs
    42   assume "list_all2 R xs ys" and "list_all2 R ys zs"
    43   then show "list_all2 R xs zs"
    44     by (induct arbitrary: zs) (auto simp: list_all2_Cons1 intro: *)
    45 qed
    46 
    47 lemma list_equivp [quot_equiv]:
    48   "equivp R \<Longrightarrow> equivp (list_all2 R)"
    49   by (blast intro: equivpI reflp_list_all2 list_symp list_transp elim: equivpE)
    50 
    51 lemma list_quotient3 [quot_thm]:
    52   assumes "Quotient3 R Abs Rep"
    53   shows "Quotient3 (list_all2 R) (map Abs) (map Rep)"
    54 proof (rule Quotient3I)
    55   from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
    56   then show "\<And>xs. map Abs (map Rep xs) = xs" by (simp add: comp_def)
    57 next
    58   from assms have "\<And>x y. R (Rep x) (Rep y) \<longleftrightarrow> x = y" by (rule Quotient3_rel_rep)
    59   then show "\<And>xs. list_all2 R (map Rep xs) (map Rep xs)"
    60     by (simp add: list_all2_map1 list_all2_map2 list_all2_eq)
    61 next
    62   fix xs ys
    63   from assms have "\<And>x y. R x x \<and> R y y \<and> Abs x = Abs y \<longleftrightarrow> R x y" by (rule Quotient3_rel)
    64   then show "list_all2 R xs ys \<longleftrightarrow> list_all2 R xs xs \<and> list_all2 R ys ys \<and> map Abs xs = map Abs ys"
    65     by (induct xs ys rule: list_induct2') auto
    66 qed
    67 
    68 declare [[mapQ3 list = (list_all2, list_quotient3)]]
    69 
    70 lemma cons_prs [quot_preserve]:
    71   assumes q: "Quotient3 R Abs Rep"
    72   shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
    73   by (auto simp add: fun_eq_iff comp_def Quotient3_abs_rep [OF q])
    74 
    75 lemma cons_rsp [quot_respect]:
    76   assumes q: "Quotient3 R Abs Rep"
    77   shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
    78   by auto
    79 
    80 lemma nil_prs [quot_preserve]:
    81   assumes q: "Quotient3 R Abs Rep"
    82   shows "map Abs [] = []"
    83   by simp
    84 
    85 lemma nil_rsp [quot_respect]:
    86   assumes q: "Quotient3 R Abs Rep"
    87   shows "list_all2 R [] []"
    88   by simp
    89 
    90 lemma map_prs_aux:
    91   assumes a: "Quotient3 R1 abs1 rep1"
    92   and     b: "Quotient3 R2 abs2 rep2"
    93   shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
    94   by (induct l)
    95      (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
    96 
    97 lemma map_prs [quot_preserve]:
    98   assumes a: "Quotient3 R1 abs1 rep1"
    99   and     b: "Quotient3 R2 abs2 rep2"
   100   shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
   101   and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
   102   by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
   103     (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
   104 
   105 lemma map_rsp [quot_respect]:
   106   assumes q1: "Quotient3 R1 Abs1 Rep1"
   107   and     q2: "Quotient3 R2 Abs2 Rep2"
   108   shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
   109   and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
   110   unfolding list_all2_eq [symmetric] by (rule map_transfer)+
   111 
   112 lemma foldr_prs_aux:
   113   assumes a: "Quotient3 R1 abs1 rep1"
   114   and     b: "Quotient3 R2 abs2 rep2"
   115   shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
   116   by (induct l) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
   117 
   118 lemma foldr_prs [quot_preserve]:
   119   assumes a: "Quotient3 R1 abs1 rep1"
   120   and     b: "Quotient3 R2 abs2 rep2"
   121   shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
   122   apply (simp add: fun_eq_iff)
   123   by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
   124      (simp)
   125 
   126 lemma foldl_prs_aux:
   127   assumes a: "Quotient3 R1 abs1 rep1"
   128   and     b: "Quotient3 R2 abs2 rep2"
   129   shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
   130   by (induct l arbitrary:e) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
   131 
   132 lemma foldl_prs [quot_preserve]:
   133   assumes a: "Quotient3 R1 abs1 rep1"
   134   and     b: "Quotient3 R2 abs2 rep2"
   135   shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
   136   by (simp add: fun_eq_iff foldl_prs_aux [OF a b])
   137 
   138 (* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
   139 lemma foldl_rsp[quot_respect]:
   140   assumes q1: "Quotient3 R1 Abs1 Rep1"
   141   and     q2: "Quotient3 R2 Abs2 Rep2"
   142   shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
   143   by (rule foldl_transfer)
   144 
   145 lemma foldr_rsp[quot_respect]:
   146   assumes q1: "Quotient3 R1 Abs1 Rep1"
   147   and     q2: "Quotient3 R2 Abs2 Rep2"
   148   shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
   149   by (rule foldr_transfer)
   150 
   151 lemma list_all2_rsp:
   152   assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
   153   and l1: "list_all2 R x y"
   154   and l2: "list_all2 R a b"
   155   shows "list_all2 S x a = list_all2 T y b"
   156   using l1 l2
   157   by (induct arbitrary: a b rule: list_all2_induct,
   158     auto simp: list_all2_Cons1 list_all2_Cons2 r)
   159 
   160 lemma [quot_respect]:
   161   "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
   162   by (rule list_all2_transfer)
   163 
   164 lemma [quot_preserve]:
   165   assumes a: "Quotient3 R abs1 rep1"
   166   shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
   167   apply (simp add: fun_eq_iff)
   168   apply clarify
   169   apply (induct_tac xa xb rule: list_induct2')
   170   apply (simp_all add: Quotient3_abs_rep[OF a])
   171   done
   172 
   173 lemma [quot_preserve]:
   174   assumes a: "Quotient3 R abs1 rep1"
   175   shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
   176   by (induct l m rule: list_induct2') (simp_all add: Quotient3_rel_rep[OF a])
   177 
   178 lemma list_all2_find_element:
   179   assumes a: "x \<in> set a"
   180   and b: "list_all2 R a b"
   181   shows "\<exists>y. (y \<in> set b \<and> R x y)"
   182   using b a by induct auto
   183 
   184 lemma list_all2_refl:
   185   assumes a: "\<And>x y. R x y = (R x = R y)"
   186   shows "list_all2 R x x"
   187   by (induct x) (auto simp add: a)
   188 
   189 end