src/HOL/Library/Quotient_List.thy
author huffman
Sat Apr 21 11:04:21 2012 +0200 (2012-04-21)
changeset 47649 df687f0797fb
parent 47641 2cddc27a881f
child 47650 33ecf14d5ee9
permissions -rw-r--r--
remove duplicate of lemma id_transfer
     1 (*  Title:      HOL/Library/Quotient_List.thy
     2     Author:     Cezary Kaliszyk, Christian Urban and Brian Huffman
     3 *)
     4 
     5 header {* Quotient infrastructure for the list type *}
     6 
     7 theory Quotient_List
     8 imports Main Quotient_Syntax
     9 begin
    10 
    11 subsection {* Relator for list type *}
    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, relator_eq]:
    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_reflp:
    26   assumes "reflp R"
    27   shows "reflp (list_all2 R)"
    28 proof (rule reflpI)
    29   from assms have *: "\<And>xs. R xs xs" by (rule reflpE)
    30   fix xs
    31   show "list_all2 R xs xs"
    32     by (induct xs) (simp_all add: *)
    33 qed
    34 
    35 lemma list_symp:
    36   assumes "symp R"
    37   shows "symp (list_all2 R)"
    38 proof (rule sympI)
    39   from assms have *: "\<And>xs ys. R xs ys \<Longrightarrow> R ys xs" by (rule sympE)
    40   fix xs ys
    41   assume "list_all2 R xs ys"
    42   then show "list_all2 R ys xs"
    43     by (induct xs ys rule: list_induct2') (simp_all add: *)
    44 qed
    45 
    46 lemma list_transp:
    47   assumes "transp R"
    48   shows "transp (list_all2 R)"
    49 proof (rule transpI)
    50   from assms have *: "\<And>xs ys zs. R xs ys \<Longrightarrow> R ys zs \<Longrightarrow> R xs zs" by (rule transpE)
    51   fix xs ys zs
    52   assume "list_all2 R xs ys" and "list_all2 R ys zs"
    53   then show "list_all2 R xs zs"
    54     by (induct arbitrary: zs) (auto simp: list_all2_Cons1 intro: *)
    55 qed
    56 
    57 lemma list_equivp [quot_equiv]:
    58   "equivp R \<Longrightarrow> equivp (list_all2 R)"
    59   by (blast intro: equivpI list_reflp list_symp list_transp elim: equivpE)
    60 
    61 lemma right_total_list_all2 [transfer_rule]:
    62   "right_total R \<Longrightarrow> right_total (list_all2 R)"
    63   unfolding right_total_def
    64   by (rule allI, induct_tac y, simp, simp add: list_all2_Cons2)
    65 
    66 lemma right_unique_list_all2 [transfer_rule]:
    67   "right_unique R \<Longrightarrow> right_unique (list_all2 R)"
    68   unfolding right_unique_def
    69   apply (rule allI, rename_tac xs, induct_tac xs)
    70   apply (auto simp add: list_all2_Cons1)
    71   done
    72 
    73 lemma bi_total_list_all2 [transfer_rule]:
    74   "bi_total A \<Longrightarrow> bi_total (list_all2 A)"
    75   unfolding bi_total_def
    76   apply safe
    77   apply (rename_tac xs, induct_tac xs, simp, simp add: list_all2_Cons1)
    78   apply (rename_tac ys, induct_tac ys, simp, simp add: list_all2_Cons2)
    79   done
    80 
    81 lemma bi_unique_list_all2 [transfer_rule]:
    82   "bi_unique A \<Longrightarrow> bi_unique (list_all2 A)"
    83   unfolding bi_unique_def
    84   apply (rule conjI)
    85   apply (rule allI, rename_tac xs, induct_tac xs)
    86   apply (simp, force simp add: list_all2_Cons1)
    87   apply (subst (2) all_comm, subst (1) all_comm)
    88   apply (rule allI, rename_tac xs, induct_tac xs)
    89   apply (simp, force simp add: list_all2_Cons2)
    90   done
    91 
    92 subsection {* Transfer rules for transfer package *}
    93 
    94 lemma Nil_transfer [transfer_rule]: "(list_all2 A) [] []"
    95   by simp
    96 
    97 lemma Cons_transfer [transfer_rule]:
    98   "(A ===> list_all2 A ===> list_all2 A) Cons Cons"
    99   unfolding fun_rel_def by simp
   100 
   101 lemma list_case_transfer [transfer_rule]:
   102   "(B ===> (A ===> list_all2 A ===> B) ===> list_all2 A ===> B)
   103     list_case list_case"
   104   unfolding fun_rel_def by (simp split: list.split)
   105 
   106 lemma list_rec_transfer [transfer_rule]:
   107   "(B ===> (A ===> list_all2 A ===> B ===> B) ===> list_all2 A ===> B)
   108     list_rec list_rec"
   109   unfolding fun_rel_def by (clarify, erule list_all2_induct, simp_all)
   110 
   111 lemma map_transfer [transfer_rule]:
   112   "((A ===> B) ===> list_all2 A ===> list_all2 B) map map"
   113   unfolding List.map_def by transfer_prover
   114 
   115 lemma append_transfer [transfer_rule]:
   116   "(list_all2 A ===> list_all2 A ===> list_all2 A) append append"
   117   unfolding List.append_def by transfer_prover
   118 
   119 lemma filter_transfer [transfer_rule]:
   120   "((A ===> op =) ===> list_all2 A ===> list_all2 A) filter filter"
   121   unfolding List.filter_def by transfer_prover
   122 
   123 lemma foldr_transfer [transfer_rule]:
   124   "((A ===> B ===> B) ===> list_all2 A ===> B ===> B) foldr foldr"
   125   unfolding List.foldr_def by transfer_prover
   126 
   127 lemma foldl_transfer [transfer_rule]:
   128   "((B ===> A ===> B) ===> B ===> list_all2 A ===> B) foldl foldl"
   129   unfolding List.foldl_def by transfer_prover
   130 
   131 lemma concat_transfer [transfer_rule]:
   132   "(list_all2 (list_all2 A) ===> list_all2 A) concat concat"
   133   unfolding List.concat_def by transfer_prover
   134 
   135 lemma drop_transfer [transfer_rule]:
   136   "(op = ===> list_all2 A ===> list_all2 A) drop drop"
   137   unfolding List.drop_def by transfer_prover
   138 
   139 lemma take_transfer [transfer_rule]:
   140   "(op = ===> list_all2 A ===> list_all2 A) take take"
   141   unfolding List.take_def by transfer_prover
   142 
   143 lemma length_transfer [transfer_rule]:
   144   "(list_all2 A ===> op =) length length"
   145   unfolding list_size_overloaded_def by transfer_prover
   146 
   147 lemma list_all_transfer [transfer_rule]:
   148   "((A ===> op =) ===> list_all2 A ===> op =) list_all list_all"
   149   unfolding fun_rel_def by (clarify, erule list_all2_induct, simp_all)
   150 
   151 lemma list_all2_transfer [transfer_rule]:
   152   "((A ===> B ===> op =) ===> list_all2 A ===> list_all2 B ===> op =)
   153     list_all2 list_all2"
   154   apply (rule fun_relI, rule fun_relI, erule list_all2_induct)
   155   apply (rule fun_relI, erule list_all2_induct, simp, simp)
   156   apply (rule fun_relI, erule list_all2_induct [of B])
   157   apply (simp, simp add: fun_rel_def)
   158   done
   159 
   160 subsection {* Setup for lifting package *}
   161 
   162 lemma Quotient_list:
   163   assumes "Quotient R Abs Rep T"
   164   shows "Quotient (list_all2 R) (map Abs) (map Rep) (list_all2 T)"
   165 proof (unfold Quotient_alt_def, intro conjI allI impI)
   166   from assms have 1: "\<And>x y. T x y \<Longrightarrow> Abs x = y"
   167     unfolding Quotient_alt_def by simp
   168   fix xs ys assume "list_all2 T xs ys" thus "map Abs xs = ys"
   169     by (induct, simp, simp add: 1)
   170 next
   171   from assms have 2: "\<And>x. T (Rep x) x"
   172     unfolding Quotient_alt_def by simp
   173   fix xs show "list_all2 T (map Rep xs) xs"
   174     by (induct xs, simp, simp add: 2)
   175 next
   176   from assms have 3: "\<And>x y. R x y \<longleftrightarrow> T x (Abs x) \<and> T y (Abs y) \<and> Abs x = Abs y"
   177     unfolding Quotient_alt_def by simp
   178   fix xs ys show "list_all2 R xs ys \<longleftrightarrow> list_all2 T xs (map Abs xs) \<and>
   179     list_all2 T ys (map Abs ys) \<and> map Abs xs = map Abs ys"
   180     by (induct xs ys rule: list_induct2', simp_all, metis 3)
   181 qed
   182 
   183 declare [[map list = (list_all2, Quotient_list)]]
   184 
   185 lemma list_invariant_commute [invariant_commute]:
   186   "list_all2 (Lifting.invariant P) = Lifting.invariant (list_all P)"
   187   apply (simp add: fun_eq_iff list_all2_def list_all_iff Lifting.invariant_def Ball_def) 
   188   apply (intro allI) 
   189   apply (induct_tac rule: list_induct2') 
   190   apply simp_all 
   191   apply metis
   192 done
   193 
   194 subsection {* Rules for quotient package *}
   195 
   196 lemma list_quotient3 [quot_thm]:
   197   assumes "Quotient3 R Abs Rep"
   198   shows "Quotient3 (list_all2 R) (map Abs) (map Rep)"
   199 proof (rule Quotient3I)
   200   from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
   201   then show "\<And>xs. map Abs (map Rep xs) = xs" by (simp add: comp_def)
   202 next
   203   from assms have "\<And>x y. R (Rep x) (Rep y) \<longleftrightarrow> x = y" by (rule Quotient3_rel_rep)
   204   then show "\<And>xs. list_all2 R (map Rep xs) (map Rep xs)"
   205     by (simp add: list_all2_map1 list_all2_map2 list_all2_eq)
   206 next
   207   fix xs ys
   208   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)
   209   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"
   210     by (induct xs ys rule: list_induct2') auto
   211 qed
   212 
   213 declare [[mapQ3 list = (list_all2, list_quotient3)]]
   214 
   215 lemma cons_prs [quot_preserve]:
   216   assumes q: "Quotient3 R Abs Rep"
   217   shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
   218   by (auto simp add: fun_eq_iff comp_def Quotient3_abs_rep [OF q])
   219 
   220 lemma cons_rsp [quot_respect]:
   221   assumes q: "Quotient3 R Abs Rep"
   222   shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
   223   by auto
   224 
   225 lemma nil_prs [quot_preserve]:
   226   assumes q: "Quotient3 R Abs Rep"
   227   shows "map Abs [] = []"
   228   by simp
   229 
   230 lemma nil_rsp [quot_respect]:
   231   assumes q: "Quotient3 R Abs Rep"
   232   shows "list_all2 R [] []"
   233   by simp
   234 
   235 lemma map_prs_aux:
   236   assumes a: "Quotient3 R1 abs1 rep1"
   237   and     b: "Quotient3 R2 abs2 rep2"
   238   shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
   239   by (induct l)
   240      (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
   241 
   242 lemma map_prs [quot_preserve]:
   243   assumes a: "Quotient3 R1 abs1 rep1"
   244   and     b: "Quotient3 R2 abs2 rep2"
   245   shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
   246   and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
   247   by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
   248     (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
   249 
   250 lemma map_rsp [quot_respect]:
   251   assumes q1: "Quotient3 R1 Abs1 Rep1"
   252   and     q2: "Quotient3 R2 Abs2 Rep2"
   253   shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
   254   and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
   255   unfolding list_all2_eq [symmetric] by (rule map_transfer)+
   256 
   257 lemma foldr_prs_aux:
   258   assumes a: "Quotient3 R1 abs1 rep1"
   259   and     b: "Quotient3 R2 abs2 rep2"
   260   shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
   261   by (induct l) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
   262 
   263 lemma foldr_prs [quot_preserve]:
   264   assumes a: "Quotient3 R1 abs1 rep1"
   265   and     b: "Quotient3 R2 abs2 rep2"
   266   shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
   267   apply (simp add: fun_eq_iff)
   268   by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
   269      (simp)
   270 
   271 lemma foldl_prs_aux:
   272   assumes a: "Quotient3 R1 abs1 rep1"
   273   and     b: "Quotient3 R2 abs2 rep2"
   274   shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
   275   by (induct l arbitrary:e) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
   276 
   277 lemma foldl_prs [quot_preserve]:
   278   assumes a: "Quotient3 R1 abs1 rep1"
   279   and     b: "Quotient3 R2 abs2 rep2"
   280   shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
   281   by (simp add: fun_eq_iff foldl_prs_aux [OF a b])
   282 
   283 (* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
   284 lemma foldl_rsp[quot_respect]:
   285   assumes q1: "Quotient3 R1 Abs1 Rep1"
   286   and     q2: "Quotient3 R2 Abs2 Rep2"
   287   shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
   288   by (rule foldl_transfer)
   289 
   290 lemma foldr_rsp[quot_respect]:
   291   assumes q1: "Quotient3 R1 Abs1 Rep1"
   292   and     q2: "Quotient3 R2 Abs2 Rep2"
   293   shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
   294   by (rule foldr_transfer)
   295 
   296 lemma list_all2_rsp:
   297   assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
   298   and l1: "list_all2 R x y"
   299   and l2: "list_all2 R a b"
   300   shows "list_all2 S x a = list_all2 T y b"
   301   using l1 l2
   302   by (induct arbitrary: a b rule: list_all2_induct,
   303     auto simp: list_all2_Cons1 list_all2_Cons2 r)
   304 
   305 lemma [quot_respect]:
   306   "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
   307   by (rule list_all2_transfer)
   308 
   309 lemma [quot_preserve]:
   310   assumes a: "Quotient3 R abs1 rep1"
   311   shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
   312   apply (simp add: fun_eq_iff)
   313   apply clarify
   314   apply (induct_tac xa xb rule: list_induct2')
   315   apply (simp_all add: Quotient3_abs_rep[OF a])
   316   done
   317 
   318 lemma [quot_preserve]:
   319   assumes a: "Quotient3 R abs1 rep1"
   320   shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
   321   by (induct l m rule: list_induct2') (simp_all add: Quotient3_rel_rep[OF a])
   322 
   323 lemma list_all2_find_element:
   324   assumes a: "x \<in> set a"
   325   and b: "list_all2 R a b"
   326   shows "\<exists>y. (y \<in> set b \<and> R x y)"
   327   using b a by induct auto
   328 
   329 lemma list_all2_refl:
   330   assumes a: "\<And>x y. R x y = (R x = R y)"
   331   shows "list_all2 R x x"
   332   by (induct x) (auto simp add: a)
   333 
   334 end