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