src/HOL/Library/Quotient_List.thy
author kuncar
Fri Mar 23 14:20:09 2012 +0100 (2012-03-23)
changeset 47094 1a7ad2601cb5
parent 46663 7fe029e818c2
child 47308 9caab698dbe4
permissions -rw-r--r--
store the relational theorem for every relator
     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_Syntax
     9 begin
    10 
    11 lemma map_id [id_simps]:
    12   "map id = id"
    13   by (fact List.map.id)
    14 
    15 lemma list_all2_eq [id_simps]:
    16   "list_all2 (op =) = (op =)"
    17 proof (rule ext)+
    18   fix xs ys
    19   show "list_all2 (op =) xs ys \<longleftrightarrow> xs = ys"
    20     by (induct xs ys rule: list_induct2') simp_all
    21 qed
    22 
    23 lemma list_reflp:
    24   assumes "reflp R"
    25   shows "reflp (list_all2 R)"
    26 proof (rule reflpI)
    27   from assms have *: "\<And>xs. R xs xs" by (rule reflpE)
    28   fix xs
    29   show "list_all2 R xs xs"
    30     by (induct xs) (simp_all add: *)
    31 qed
    32 
    33 lemma list_symp:
    34   assumes "symp R"
    35   shows "symp (list_all2 R)"
    36 proof (rule sympI)
    37   from assms have *: "\<And>xs ys. R xs ys \<Longrightarrow> R ys xs" by (rule sympE)
    38   fix xs ys
    39   assume "list_all2 R xs ys"
    40   then show "list_all2 R ys xs"
    41     by (induct xs ys rule: list_induct2') (simp_all add: *)
    42 qed
    43 
    44 lemma list_transp:
    45   assumes "transp R"
    46   shows "transp (list_all2 R)"
    47 proof (rule transpI)
    48   from assms have *: "\<And>xs ys zs. R xs ys \<Longrightarrow> R ys zs \<Longrightarrow> R xs zs" by (rule transpE)
    49   fix xs ys zs
    50   assume "list_all2 R xs ys" and "list_all2 R ys zs"
    51   then show "list_all2 R xs zs"
    52     by (induct arbitrary: zs) (auto simp: list_all2_Cons1 intro: *)
    53 qed
    54 
    55 lemma list_equivp [quot_equiv]:
    56   "equivp R \<Longrightarrow> equivp (list_all2 R)"
    57   by (blast intro: equivpI list_reflp list_symp list_transp elim: equivpE)
    58 
    59 lemma list_quotient [quot_thm]:
    60   assumes "Quotient R Abs Rep"
    61   shows "Quotient (list_all2 R) (map Abs) (map Rep)"
    62 proof (rule QuotientI)
    63   from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient_abs_rep)
    64   then show "\<And>xs. map Abs (map Rep xs) = xs" by (simp add: comp_def)
    65 next
    66   from assms have "\<And>x y. R (Rep x) (Rep y) \<longleftrightarrow> x = y" by (rule Quotient_rel_rep)
    67   then show "\<And>xs. list_all2 R (map Rep xs) (map Rep xs)"
    68     by (simp add: list_all2_map1 list_all2_map2 list_all2_eq)
    69 next
    70   fix xs ys
    71   from assms have "\<And>x y. R x x \<and> R y y \<and> Abs x = Abs y \<longleftrightarrow> R x y" by (rule Quotient_rel)
    72   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"
    73     by (induct xs ys rule: list_induct2') auto
    74 qed
    75 
    76 declare [[map list = (list_all2, list_quotient)]]
    77 
    78 lemma cons_prs [quot_preserve]:
    79   assumes q: "Quotient R Abs Rep"
    80   shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
    81   by (auto simp add: fun_eq_iff comp_def Quotient_abs_rep [OF q])
    82 
    83 lemma cons_rsp [quot_respect]:
    84   assumes q: "Quotient R Abs Rep"
    85   shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
    86   by auto
    87 
    88 lemma nil_prs [quot_preserve]:
    89   assumes q: "Quotient R Abs Rep"
    90   shows "map Abs [] = []"
    91   by simp
    92 
    93 lemma nil_rsp [quot_respect]:
    94   assumes q: "Quotient R Abs Rep"
    95   shows "list_all2 R [] []"
    96   by simp
    97 
    98 lemma map_prs_aux:
    99   assumes a: "Quotient R1 abs1 rep1"
   100   and     b: "Quotient R2 abs2 rep2"
   101   shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
   102   by (induct l)
   103      (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   104 
   105 lemma map_prs [quot_preserve]:
   106   assumes a: "Quotient R1 abs1 rep1"
   107   and     b: "Quotient R2 abs2 rep2"
   108   shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
   109   and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
   110   by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
   111     (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   112 
   113 lemma map_rsp [quot_respect]:
   114   assumes q1: "Quotient R1 Abs1 Rep1"
   115   and     q2: "Quotient R2 Abs2 Rep2"
   116   shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
   117   and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
   118   apply (simp_all add: fun_rel_def)
   119   apply(rule_tac [!] allI)+
   120   apply(rule_tac [!] impI)
   121   apply(rule_tac [!] allI)+
   122   apply (induct_tac [!] xa ya rule: list_induct2')
   123   apply simp_all
   124   done
   125 
   126 lemma foldr_prs_aux:
   127   assumes a: "Quotient R1 abs1 rep1"
   128   and     b: "Quotient R2 abs2 rep2"
   129   shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
   130   by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   131 
   132 lemma foldr_prs [quot_preserve]:
   133   assumes a: "Quotient R1 abs1 rep1"
   134   and     b: "Quotient R2 abs2 rep2"
   135   shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
   136   apply (simp add: fun_eq_iff)
   137   by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
   138      (simp)
   139 
   140 lemma foldl_prs_aux:
   141   assumes a: "Quotient R1 abs1 rep1"
   142   and     b: "Quotient R2 abs2 rep2"
   143   shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
   144   by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   145 
   146 lemma foldl_prs [quot_preserve]:
   147   assumes a: "Quotient R1 abs1 rep1"
   148   and     b: "Quotient R2 abs2 rep2"
   149   shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
   150   by (simp add: fun_eq_iff foldl_prs_aux [OF a b])
   151 
   152 (* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
   153 lemma foldl_rsp[quot_respect]:
   154   assumes q1: "Quotient R1 Abs1 Rep1"
   155   and     q2: "Quotient R2 Abs2 Rep2"
   156   shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
   157   apply(auto simp add: fun_rel_def)
   158   apply (erule_tac P="R1 xa ya" in rev_mp)
   159   apply (rule_tac x="xa" in spec)
   160   apply (rule_tac x="ya" in spec)
   161   apply (erule list_all2_induct, simp_all)
   162   done
   163 
   164 lemma foldr_rsp[quot_respect]:
   165   assumes q1: "Quotient R1 Abs1 Rep1"
   166   and     q2: "Quotient R2 Abs2 Rep2"
   167   shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
   168   apply (auto simp add: fun_rel_def)
   169   apply (erule list_all2_induct, simp_all)
   170   done
   171 
   172 lemma list_all2_rsp:
   173   assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
   174   and l1: "list_all2 R x y"
   175   and l2: "list_all2 R a b"
   176   shows "list_all2 S x a = list_all2 T y b"
   177   using l1 l2
   178   by (induct arbitrary: a b rule: list_all2_induct,
   179     auto simp: list_all2_Cons1 list_all2_Cons2 r)
   180 
   181 lemma [quot_respect]:
   182   "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
   183   by (simp add: list_all2_rsp fun_rel_def)
   184 
   185 lemma [quot_preserve]:
   186   assumes a: "Quotient R abs1 rep1"
   187   shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
   188   apply (simp add: fun_eq_iff)
   189   apply clarify
   190   apply (induct_tac xa xb rule: list_induct2')
   191   apply (simp_all add: Quotient_abs_rep[OF a])
   192   done
   193 
   194 lemma [quot_preserve]:
   195   assumes a: "Quotient R abs1 rep1"
   196   shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
   197   by (induct l m rule: list_induct2') (simp_all add: Quotient_rel_rep[OF a])
   198 
   199 lemma list_all2_find_element:
   200   assumes a: "x \<in> set a"
   201   and b: "list_all2 R a b"
   202   shows "\<exists>y. (y \<in> set b \<and> R x y)"
   203   using b a by induct auto
   204 
   205 lemma list_all2_refl:
   206   assumes a: "\<And>x y. R x y = (R x = R y)"
   207   shows "list_all2 R x x"
   208   by (induct x) (auto simp add: a)
   209 
   210 end