
wenzelm@47455: (*  Title:      HOL/Library/Quotient_List.thy
huffman@47641:     Author:     Cezary Kaliszyk, Christian Urban and Brian Huffman
kaliszyk@35222: *)
wenzelm@35788: 
wenzelm@35788: header {* Quotient infrastructure for the list type *}
wenzelm@35788: 
kaliszyk@35222: theory Quotient_List
huffman@47929: imports Main Quotient_Set Quotient_Product Quotient_Option
kaliszyk@35222: begin
kaliszyk@35222: 
huffman@47641: subsection {* Relator for list type *}
huffman@47641: 
haftmann@40820: lemma map_id [id_simps]:
haftmann@40820:   "map id = id"
haftmann@46663:   by (fact List.map.id)
kaliszyk@35222: 
huffman@47641: lemma list_all2_eq [id_simps, relator_eq]:
haftmann@40820:   "list_all2 (op =) = (op =)"
haftmann@40820: proof (rule ext)+
haftmann@40820:   fix xs ys
haftmann@40820:   show "list_all2 (op =) xs ys \<longleftrightarrow> xs = ys"
haftmann@40820:     by (induct xs ys rule: list_induct2') simp_all
haftmann@40820: qed
kaliszyk@35222: 
huffman@47660: lemma list_all2_OO: "list_all2 (A OO B) = list_all2 A OO list_all2 B"
huffman@47660: proof (intro ext iffI)
huffman@47660:   fix xs ys
huffman@47660:   assume "list_all2 (A OO B) xs ys"
huffman@47660:   thus "(list_all2 A OO list_all2 B) xs ys"
huffman@47660:     unfolding OO_def
huffman@47660:     by (induct, simp, simp add: list_all2_Cons1 list_all2_Cons2, fast)
huffman@47660: next
huffman@47660:   fix xs ys
huffman@47660:   assume "(list_all2 A OO list_all2 B) xs ys"
huffman@47660:   then obtain zs where "list_all2 A xs zs" and "list_all2 B zs ys" ..
huffman@47660:   thus "list_all2 (A OO B) xs ys"
huffman@47660:     by (induct arbitrary: ys, simp, clarsimp simp add: list_all2_Cons1, fast)
huffman@47660: qed
huffman@47660: 
kuncar@47982: lemma list_reflp[reflexivity_rule]:
haftmann@40820:   assumes "reflp R"
haftmann@40820:   shows "reflp (list_all2 R)"
haftmann@40820: proof (rule reflpI)
haftmann@40820:   from assms have *: "\<And>xs. R xs xs" by (rule reflpE)
haftmann@40820:   fix xs
haftmann@40820:   show "list_all2 R xs xs"
haftmann@40820:     by (induct xs) (simp_all add: *)
haftmann@40820: qed
kaliszyk@35222: 
kuncar@47982: lemma list_left_total[reflexivity_rule]:
kuncar@47982:   assumes "left_total R"
kuncar@47982:   shows "left_total (list_all2 R)"
kuncar@47982: proof (rule left_totalI)
kuncar@47982:   from assms have *: "\<And>xs. \<exists>ys. R xs ys" by (rule left_totalE)
kuncar@47982:   fix xs
kuncar@47982:   show "\<exists> ys. list_all2 R xs ys"
kuncar@47982:     by (induct xs) (simp_all add: * list_all2_Cons1)
kuncar@47982: qed
kuncar@47982: 
kuncar@47982: 
haftmann@40820: lemma list_symp:
haftmann@40820:   assumes "symp R"
haftmann@40820:   shows "symp (list_all2 R)"
haftmann@40820: proof (rule sympI)
haftmann@40820:   from assms have *: "\<And>xs ys. R xs ys \<Longrightarrow> R ys xs" by (rule sympE)
haftmann@40820:   fix xs ys
haftmann@40820:   assume "list_all2 R xs ys"
haftmann@40820:   then show "list_all2 R ys xs"
haftmann@40820:     by (induct xs ys rule: list_induct2') (simp_all add: *)
haftmann@40820: qed
kaliszyk@35222: 
haftmann@40820: lemma list_transp:
haftmann@40820:   assumes "transp R"
haftmann@40820:   shows "transp (list_all2 R)"
haftmann@40820: proof (rule transpI)
haftmann@40820:   from assms have *: "\<And>xs ys zs. R xs ys \<Longrightarrow> R ys zs \<Longrightarrow> R xs zs" by (rule transpE)
haftmann@40820:   fix xs ys zs
huffman@45803:   assume "list_all2 R xs ys" and "list_all2 R ys zs"
huffman@45803:   then show "list_all2 R xs zs"
huffman@45803:     by (induct arbitrary: zs) (auto simp: list_all2_Cons1 intro: *)
haftmann@40820: qed
kaliszyk@35222: 
haftmann@40820: lemma list_equivp [quot_equiv]:
haftmann@40820:   "equivp R \<Longrightarrow> equivp (list_all2 R)"
haftmann@40820:   by (blast intro: equivpI list_reflp list_symp list_transp elim: equivpE)
kaliszyk@35222: 
huffman@47641: lemma right_total_list_all2 [transfer_rule]:
huffman@47641:   "right_total R \<Longrightarrow> right_total (list_all2 R)"
huffman@47641:   unfolding right_total_def
huffman@47641:   by (rule allI, induct_tac y, simp, simp add: list_all2_Cons2)
huffman@47641: 
huffman@47641: lemma right_unique_list_all2 [transfer_rule]:
huffman@47641:   "right_unique R \<Longrightarrow> right_unique (list_all2 R)"
huffman@47641:   unfolding right_unique_def
huffman@47641:   apply (rule allI, rename_tac xs, induct_tac xs)
huffman@47641:   apply (auto simp add: list_all2_Cons1)
huffman@47641:   done
huffman@47641: 
huffman@47641: lemma bi_total_list_all2 [transfer_rule]:
huffman@47641:   "bi_total A \<Longrightarrow> bi_total (list_all2 A)"
huffman@47641:   unfolding bi_total_def
huffman@47641:   apply safe
huffman@47641:   apply (rename_tac xs, induct_tac xs, simp, simp add: list_all2_Cons1)
huffman@47641:   apply (rename_tac ys, induct_tac ys, simp, simp add: list_all2_Cons2)
huffman@47641:   done
huffman@47641: 
huffman@47641: lemma bi_unique_list_all2 [transfer_rule]:
huffman@47641:   "bi_unique A \<Longrightarrow> bi_unique (list_all2 A)"
huffman@47641:   unfolding bi_unique_def
huffman@47641:   apply (rule conjI)
huffman@47641:   apply (rule allI, rename_tac xs, induct_tac xs)
huffman@47641:   apply (simp, force simp add: list_all2_Cons1)
huffman@47641:   apply (subst (2) all_comm, subst (1) all_comm)
huffman@47641:   apply (rule allI, rename_tac xs, induct_tac xs)
huffman@47641:   apply (simp, force simp add: list_all2_Cons2)
huffman@47641:   done
huffman@47641: 
huffman@47641: subsection {* Transfer rules for transfer package *}
huffman@47641: 
huffman@47641: lemma Nil_transfer [transfer_rule]: "(list_all2 A) [] []"
huffman@47641:   by simp
huffman@47641: 
huffman@47641: lemma Cons_transfer [transfer_rule]:
huffman@47641:   "(A ===> list_all2 A ===> list_all2 A) Cons Cons"
huffman@47641:   unfolding fun_rel_def by simp
huffman@47641: 
huffman@47641: lemma list_case_transfer [transfer_rule]:
huffman@47641:   "(B ===> (A ===> list_all2 A ===> B) ===> list_all2 A ===> B)
huffman@47641:     list_case list_case"
huffman@47641:   unfolding fun_rel_def by (simp split: list.split)
huffman@47641: 
huffman@47641: lemma list_rec_transfer [transfer_rule]:
huffman@47641:   "(B ===> (A ===> list_all2 A ===> B ===> B) ===> list_all2 A ===> B)
huffman@47641:     list_rec list_rec"
huffman@47641:   unfolding fun_rel_def by (clarify, erule list_all2_induct, simp_all)
huffman@47641: 
huffman@47929: lemma tl_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> list_all2 A) tl tl"
huffman@47929:   unfolding tl_def by transfer_prover
huffman@47929: 
huffman@47929: lemma butlast_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> list_all2 A) butlast butlast"
huffman@47929:   by (rule fun_relI, erule list_all2_induct, auto)
huffman@47929: 
huffman@47929: lemma set_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> set_rel A) set set"
huffman@47929:   unfolding set_def by transfer_prover
huffman@47929: 
huffman@47641: lemma map_transfer [transfer_rule]:
huffman@47641:   "((A ===> B) ===> list_all2 A ===> list_all2 B) map map"
huffman@47641:   unfolding List.map_def by transfer_prover
huffman@47641: 
huffman@47641: lemma append_transfer [transfer_rule]:
huffman@47641:   "(list_all2 A ===> list_all2 A ===> list_all2 A) append append"
huffman@47641:   unfolding List.append_def by transfer_prover
huffman@47641: 
huffman@47929: lemma rev_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> list_all2 A) rev rev"
huffman@47929:   unfolding List.rev_def by transfer_prover
huffman@47929: 
huffman@47641: lemma filter_transfer [transfer_rule]:
huffman@47641:   "((A ===> op =) ===> list_all2 A ===> list_all2 A) filter filter"
huffman@47641:   unfolding List.filter_def by transfer_prover
huffman@47641: 
huffman@47929: lemma fold_transfer [transfer_rule]:
huffman@47929:   "((A ===> B ===> B) ===> list_all2 A ===> B ===> B) fold fold"
huffman@47929:   unfolding List.fold_def by transfer_prover
huffman@47929: 
huffman@47641: lemma foldr_transfer [transfer_rule]:
huffman@47641:   "((A ===> B ===> B) ===> list_all2 A ===> B ===> B) foldr foldr"
huffman@47641:   unfolding List.foldr_def by transfer_prover
huffman@47641: 
huffman@47641: lemma foldl_transfer [transfer_rule]:
huffman@47641:   "((B ===> A ===> B) ===> B ===> list_all2 A ===> B) foldl foldl"
huffman@47641:   unfolding List.foldl_def by transfer_prover
huffman@47641: 
huffman@47641: lemma concat_transfer [transfer_rule]:
huffman@47641:   "(list_all2 (list_all2 A) ===> list_all2 A) concat concat"
huffman@47641:   unfolding List.concat_def by transfer_prover
huffman@47641: 
huffman@47641: lemma drop_transfer [transfer_rule]:
huffman@47641:   "(op = ===> list_all2 A ===> list_all2 A) drop drop"
huffman@47641:   unfolding List.drop_def by transfer_prover
huffman@47641: 
huffman@47641: lemma take_transfer [transfer_rule]:
huffman@47641:   "(op = ===> list_all2 A ===> list_all2 A) take take"
huffman@47641:   unfolding List.take_def by transfer_prover
huffman@47641: 
huffman@47929: lemma list_update_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> op = ===> A ===> list_all2 A) list_update list_update"
huffman@47929:   unfolding list_update_def by transfer_prover
huffman@47929: 
huffman@47929: lemma takeWhile_transfer [transfer_rule]:
huffman@47929:   "((A ===> op =) ===> list_all2 A ===> list_all2 A) takeWhile takeWhile"
huffman@47929:   unfolding takeWhile_def by transfer_prover
huffman@47929: 
huffman@47929: lemma dropWhile_transfer [transfer_rule]:
huffman@47929:   "((A ===> op =) ===> list_all2 A ===> list_all2 A) dropWhile dropWhile"
huffman@47929:   unfolding dropWhile_def by transfer_prover
huffman@47929: 
huffman@47929: lemma zip_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> list_all2 B ===> list_all2 (prod_rel A B)) zip zip"
huffman@47929:   unfolding zip_def by transfer_prover
huffman@47929: 
huffman@47929: lemma insert_transfer [transfer_rule]:
huffman@47929:   assumes [transfer_rule]: "bi_unique A"
huffman@47929:   shows "(A ===> list_all2 A ===> list_all2 A) List.insert List.insert"
huffman@47929:   unfolding List.insert_def [abs_def] by transfer_prover
huffman@47929: 
huffman@47929: lemma find_transfer [transfer_rule]:
huffman@47929:   "((A ===> op =) ===> list_all2 A ===> option_rel A) List.find List.find"
huffman@47929:   unfolding List.find_def by transfer_prover
huffman@47929: 
huffman@47929: lemma remove1_transfer [transfer_rule]:
huffman@47929:   assumes [transfer_rule]: "bi_unique A"
huffman@47929:   shows "(A ===> list_all2 A ===> list_all2 A) remove1 remove1"
huffman@47929:   unfolding remove1_def by transfer_prover
huffman@47929: 
huffman@47929: lemma removeAll_transfer [transfer_rule]:
huffman@47929:   assumes [transfer_rule]: "bi_unique A"
huffman@47929:   shows "(A ===> list_all2 A ===> list_all2 A) removeAll removeAll"
huffman@47929:   unfolding removeAll_def by transfer_prover
huffman@47929: 
huffman@47929: lemma distinct_transfer [transfer_rule]:
huffman@47929:   assumes [transfer_rule]: "bi_unique A"
huffman@47929:   shows "(list_all2 A ===> op =) distinct distinct"
huffman@47929:   unfolding distinct_def by transfer_prover
huffman@47929: 
huffman@47929: lemma remdups_transfer [transfer_rule]:
huffman@47929:   assumes [transfer_rule]: "bi_unique A"
huffman@47929:   shows "(list_all2 A ===> list_all2 A) remdups remdups"
huffman@47929:   unfolding remdups_def by transfer_prover
huffman@47929: 
huffman@47929: lemma replicate_transfer [transfer_rule]:
huffman@47929:   "(op = ===> A ===> list_all2 A) replicate replicate"
huffman@47929:   unfolding replicate_def by transfer_prover
huffman@47929: 
huffman@47641: lemma length_transfer [transfer_rule]:
huffman@47641:   "(list_all2 A ===> op =) length length"
huffman@47641:   unfolding list_size_overloaded_def by transfer_prover
huffman@47641: 
huffman@47929: lemma rotate1_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> list_all2 A) rotate1 rotate1"
huffman@47929:   unfolding rotate1_def by transfer_prover
huffman@47929: 
huffman@47929: lemma funpow_transfer [transfer_rule]: (* FIXME: move to Transfer.thy *)
huffman@47929:   "(op = ===> (A ===> A) ===> (A ===> A)) compow compow"
huffman@47929:   unfolding funpow_def by transfer_prover
huffman@47929: 
huffman@47929: lemma rotate_transfer [transfer_rule]:
huffman@47929:   "(op = ===> list_all2 A ===> list_all2 A) rotate rotate"
huffman@47929:   unfolding rotate_def [abs_def] by transfer_prover
huffman@47641: 
huffman@47641: lemma list_all2_transfer [transfer_rule]:
huffman@47641:   "((A ===> B ===> op =) ===> list_all2 A ===> list_all2 B ===> op =)
huffman@47641:     list_all2 list_all2"
huffman@47929:   apply (subst (4) list_all2_def [abs_def])
huffman@47929:   apply (subst (3) list_all2_def [abs_def])
huffman@47929:   apply transfer_prover
huffman@47641:   done
huffman@47641: 
huffman@47929: lemma sublist_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> set_rel (op =) ===> list_all2 A) sublist sublist"
huffman@47929:   unfolding sublist_def [abs_def] by transfer_prover
huffman@47929: 
huffman@47929: lemma partition_transfer [transfer_rule]:
huffman@47929:   "((A ===> op =) ===> list_all2 A ===> prod_rel (list_all2 A) (list_all2 A))
huffman@47929:     partition partition"
huffman@47929:   unfolding partition_def by transfer_prover
huffman@47650: 
huffman@47923: lemma lists_transfer [transfer_rule]:
huffman@47923:   "(set_rel A ===> set_rel (list_all2 A)) lists lists"
huffman@47923:   apply (rule fun_relI, rule set_relI)
huffman@47923:   apply (erule lists.induct, simp)
huffman@47923:   apply (simp only: set_rel_def list_all2_Cons1, metis lists.Cons)
huffman@47923:   apply (erule lists.induct, simp)
huffman@47923:   apply (simp only: set_rel_def list_all2_Cons2, metis lists.Cons)
huffman@47923:   done
huffman@47923: 
huffman@47929: lemma set_Cons_transfer [transfer_rule]:
huffman@47929:   "(set_rel A ===> set_rel (list_all2 A) ===> set_rel (list_all2 A))
huffman@47929:     set_Cons set_Cons"
huffman@47929:   unfolding fun_rel_def set_rel_def set_Cons_def
huffman@47929:   apply safe
huffman@47929:   apply (simp add: list_all2_Cons1, fast)
huffman@47929:   apply (simp add: list_all2_Cons2, fast)
huffman@47929:   done
huffman@47929: 
huffman@47929: lemma listset_transfer [transfer_rule]:
huffman@47929:   "(list_all2 (set_rel A) ===> set_rel (list_all2 A)) listset listset"
huffman@47929:   unfolding listset_def by transfer_prover
huffman@47929: 
huffman@47929: lemma null_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> op =) List.null List.null"
huffman@47929:   unfolding fun_rel_def List.null_def by auto
huffman@47929: 
huffman@47929: lemma list_all_transfer [transfer_rule]:
huffman@47929:   "((A ===> op =) ===> list_all2 A ===> op =) list_all list_all"
huffman@47929:   unfolding list_all_iff [abs_def] by transfer_prover
huffman@47929: 
huffman@47929: lemma list_ex_transfer [transfer_rule]:
huffman@47929:   "((A ===> op =) ===> list_all2 A ===> op =) list_ex list_ex"
huffman@47929:   unfolding list_ex_iff [abs_def] by transfer_prover
huffman@47929: 
huffman@47929: lemma splice_transfer [transfer_rule]:
huffman@47929:   "(list_all2 A ===> list_all2 A ===> list_all2 A) splice splice"
huffman@47929:   apply (rule fun_relI, erule list_all2_induct, simp add: fun_rel_def, simp)
huffman@47929:   apply (rule fun_relI)
huffman@47929:   apply (erule_tac xs=x in list_all2_induct, simp, simp add: fun_rel_def)
huffman@47929:   done
huffman@47929: 
huffman@47641: subsection {* Setup for lifting package *}
huffman@47641: 
kuncar@47777: lemma Quotient_list[quot_map]:
huffman@47641:   assumes "Quotient R Abs Rep T"
huffman@47641:   shows "Quotient (list_all2 R) (map Abs) (map Rep) (list_all2 T)"
huffman@47641: proof (unfold Quotient_alt_def, intro conjI allI impI)
huffman@47641:   from assms have 1: "\<And>x y. T x y \<Longrightarrow> Abs x = y"
huffman@47641:     unfolding Quotient_alt_def by simp
huffman@47641:   fix xs ys assume "list_all2 T xs ys" thus "map Abs xs = ys"
huffman@47641:     by (induct, simp, simp add: 1)
huffman@47641: next
huffman@47641:   from assms have 2: "\<And>x. T (Rep x) x"
huffman@47641:     unfolding Quotient_alt_def by simp
huffman@47641:   fix xs show "list_all2 T (map Rep xs) xs"
huffman@47641:     by (induct xs, simp, simp add: 2)
huffman@47641: next
huffman@47641:   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"
huffman@47641:     unfolding Quotient_alt_def by simp
huffman@47641:   fix xs ys show "list_all2 R xs ys \<longleftrightarrow> list_all2 T xs (map Abs xs) \<and>
huffman@47641:     list_all2 T ys (map Abs ys) \<and> map Abs xs = map Abs ys"
huffman@47641:     by (induct xs ys rule: list_induct2', simp_all, metis 3)
huffman@47641: qed
huffman@47641: 
huffman@47641: lemma list_invariant_commute [invariant_commute]:
huffman@47641:   "list_all2 (Lifting.invariant P) = Lifting.invariant (list_all P)"
huffman@47641:   apply (simp add: fun_eq_iff list_all2_def list_all_iff Lifting.invariant_def Ball_def) 
huffman@47641:   apply (intro allI) 
huffman@47641:   apply (induct_tac rule: list_induct2') 
huffman@47641:   apply simp_all 
huffman@47641:   apply metis
huffman@47641: done
huffman@47641: 
huffman@47641: subsection {* Rules for quotient package *}
huffman@47641: 
kuncar@47308: lemma list_quotient3 [quot_thm]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
kuncar@47308:   shows "Quotient3 (list_all2 R) (map Abs) (map Rep)"
kuncar@47308: proof (rule Quotient3I)
kuncar@47308:   from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
haftmann@40820:   then show "\<And>xs. map Abs (map Rep xs) = xs" by (simp add: comp_def)
haftmann@40820: next
kuncar@47308:   from assms have "\<And>x y. R (Rep x) (Rep y) \<longleftrightarrow> x = y" by (rule Quotient3_rel_rep)
haftmann@40820:   then show "\<And>xs. list_all2 R (map Rep xs) (map Rep xs)"
haftmann@40820:     by (simp add: list_all2_map1 list_all2_map2 list_all2_eq)
haftmann@40820: next
haftmann@40820:   fix xs ys
kuncar@47308:   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)
haftmann@40820:   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"
haftmann@40820:     by (induct xs ys rule: list_induct2') auto
haftmann@40820: qed
kaliszyk@35222: 
kuncar@47308: declare [[mapQ3 list = (list_all2, list_quotient3)]]
kuncar@47094: 
haftmann@40820: lemma cons_prs [quot_preserve]:
kuncar@47308:   assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222:   shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
kuncar@47308:   by (auto simp add: fun_eq_iff comp_def Quotient3_abs_rep [OF q])
kaliszyk@35222: 
haftmann@40820: lemma cons_rsp [quot_respect]:
kuncar@47308:   assumes q: "Quotient3 R Abs Rep"
kaliszyk@37492:   shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
haftmann@40463:   by auto
kaliszyk@35222: 
haftmann@40820: lemma nil_prs [quot_preserve]:
kuncar@47308:   assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222:   shows "map Abs [] = []"
kaliszyk@35222:   by simp
kaliszyk@35222: 
haftmann@40820: lemma nil_rsp [quot_respect]:
kuncar@47308:   assumes q: "Quotient3 R Abs Rep"
kaliszyk@37492:   shows "list_all2 R [] []"
kaliszyk@35222:   by simp
kaliszyk@35222: 
kaliszyk@35222: lemma map_prs_aux:
kuncar@47308:   assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308:   and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222:   shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
kaliszyk@35222:   by (induct l)
kuncar@47308:      (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222: 
haftmann@40820: lemma map_prs [quot_preserve]:
kuncar@47308:   assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308:   and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222:   shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
kaliszyk@36216:   and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
haftmann@40463:   by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
kuncar@47308:     (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
haftmann@40463: 
haftmann@40820: lemma map_rsp [quot_respect]:
kuncar@47308:   assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308:   and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492:   shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
kaliszyk@37492:   and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
huffman@47641:   unfolding list_all2_eq [symmetric] by (rule map_transfer)+
kaliszyk@35222: 
kaliszyk@35222: lemma foldr_prs_aux:
kuncar@47308:   assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308:   and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222:   shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
kuncar@47308:   by (induct l) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222: 
haftmann@40820: lemma foldr_prs [quot_preserve]:
kuncar@47308:   assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308:   and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222:   shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
haftmann@40463:   apply (simp add: fun_eq_iff)
haftmann@40463:   by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
kaliszyk@35222:      (simp)
kaliszyk@35222: 
kaliszyk@35222: lemma foldl_prs_aux:
kuncar@47308:   assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308:   and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222:   shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
kuncar@47308:   by (induct l arbitrary:e) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222: 
haftmann@40820: lemma foldl_prs [quot_preserve]:
kuncar@47308:   assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308:   and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222:   shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
haftmann@40463:   by (simp add: fun_eq_iff foldl_prs_aux [OF a b])
kaliszyk@35222: 
kaliszyk@35222: (* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
kaliszyk@35222: lemma foldl_rsp[quot_respect]:
kuncar@47308:   assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308:   and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492:   shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
huffman@47641:   by (rule foldl_transfer)
kaliszyk@35222: 
kaliszyk@35222: lemma foldr_rsp[quot_respect]:
kuncar@47308:   assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308:   and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492:   shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
huffman@47641:   by (rule foldr_transfer)
kaliszyk@35222: 
kaliszyk@37492: lemma list_all2_rsp:
kaliszyk@36154:   assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
kaliszyk@37492:   and l1: "list_all2 R x y"
kaliszyk@37492:   and l2: "list_all2 R a b"
kaliszyk@37492:   shows "list_all2 S x a = list_all2 T y b"
huffman@45803:   using l1 l2
huffman@45803:   by (induct arbitrary: a b rule: list_all2_induct,
huffman@45803:     auto simp: list_all2_Cons1 list_all2_Cons2 r)
kaliszyk@36154: 
haftmann@40820: lemma [quot_respect]:
kaliszyk@37492:   "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
huffman@47641:   by (rule list_all2_transfer)
kaliszyk@36154: 
haftmann@40820: lemma [quot_preserve]:
kuncar@47308:   assumes a: "Quotient3 R abs1 rep1"
kaliszyk@37492:   shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
nipkow@39302:   apply (simp add: fun_eq_iff)
kaliszyk@36154:   apply clarify
kaliszyk@36154:   apply (induct_tac xa xb rule: list_induct2')
kuncar@47308:   apply (simp_all add: Quotient3_abs_rep[OF a])
kaliszyk@36154:   done
kaliszyk@36154: 
haftmann@40820: lemma [quot_preserve]:
kuncar@47308:   assumes a: "Quotient3 R abs1 rep1"
kaliszyk@37492:   shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
kuncar@47308:   by (induct l m rule: list_induct2') (simp_all add: Quotient3_rel_rep[OF a])
kaliszyk@36154: 
kaliszyk@37492: lemma list_all2_find_element:
kaliszyk@36276:   assumes a: "x \<in> set a"
kaliszyk@37492:   and b: "list_all2 R a b"
kaliszyk@36276:   shows "\<exists>y. (y \<in> set b \<and> R x y)"
huffman@45803:   using b a by induct auto
kaliszyk@36276: 
kaliszyk@37492: lemma list_all2_refl:
kaliszyk@35222:   assumes a: "\<And>x y. R x y = (R x = R y)"
kaliszyk@37492:   shows "list_all2 R x x"
kaliszyk@35222:   by (induct x) (auto simp add: a)
kaliszyk@35222: 
kaliszyk@35222: end




