
wenzelm@35788: (*  Title:      HOL/Library/Quotient_List.thy
kaliszyk@35222:     Author:     Cezary Kaliszyk and Christian Urban
kaliszyk@35222: *)
wenzelm@35788: 
wenzelm@35788: header {* Quotient infrastructure for the list type *}
wenzelm@35788: 
kaliszyk@35222: theory Quotient_List
kaliszyk@35222: imports Main Quotient_Syntax
kaliszyk@35222: begin
kaliszyk@35222: 
kaliszyk@37492: declare [[map list = (map, list_all2)]]
kaliszyk@35222: 
kaliszyk@35222: lemma split_list_all:
kaliszyk@35222:   shows "(\<forall>x. P x) \<longleftrightarrow> P [] \<and> (\<forall>x xs. P (x#xs))"
kaliszyk@35222:   apply(auto)
kaliszyk@35222:   apply(case_tac x)
kaliszyk@35222:   apply(simp_all)
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@35222: lemma map_id[id_simps]:
kaliszyk@35222:   shows "map id = id"
nipkow@39302:   apply(simp add: fun_eq_iff)
kaliszyk@35222:   apply(rule allI)
kaliszyk@35222:   apply(induct_tac x)
kaliszyk@35222:   apply(simp_all)
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@37492: lemma list_all2_reflp:
kaliszyk@37492:   shows "equivp R \<Longrightarrow> list_all2 R xs xs"
kaliszyk@37492:   by (induct xs, simp_all add: equivp_reflp)
kaliszyk@35222: 
kaliszyk@37492: lemma list_all2_symp:
kaliszyk@35222:   assumes a: "equivp R"
kaliszyk@37492:   and b: "list_all2 R xs ys"
kaliszyk@37492:   shows "list_all2 R ys xs"
kaliszyk@37492:   using list_all2_lengthD[OF b] b
kaliszyk@37492:   apply(induct xs ys rule: list_induct2)
kaliszyk@35222:   apply(simp_all)
kaliszyk@35222:   apply(rule equivp_symp[OF a])
kaliszyk@35222:   apply(simp)
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@37492: thm list_induct3
kaliszyk@37492: 
kaliszyk@37492: lemma list_all2_transp:
kaliszyk@35222:   assumes a: "equivp R"
kaliszyk@37492:   and b: "list_all2 R xs1 xs2"
kaliszyk@37492:   and c: "list_all2 R xs2 xs3"
kaliszyk@37492:   shows "list_all2 R xs1 xs3"
kaliszyk@37492:   using list_all2_lengthD[OF b] list_all2_lengthD[OF c] b c
kaliszyk@37492:   apply(induct rule: list_induct3)
kaliszyk@37492:   apply(simp_all)
kaliszyk@37492:   apply(auto intro: equivp_transp[OF a])
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@35222: lemma list_equivp[quot_equiv]:
kaliszyk@35222:   assumes a: "equivp R"
kaliszyk@37492:   shows "equivp (list_all2 R)"
kaliszyk@37492:   apply (intro equivpI)
kaliszyk@35222:   unfolding reflp_def symp_def transp_def
kaliszyk@37492:   apply(simp add: list_all2_reflp[OF a])
kaliszyk@37492:   apply(blast intro: list_all2_symp[OF a])
kaliszyk@37492:   apply(blast intro: list_all2_transp[OF a])
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@37492: lemma list_all2_rel:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@37492:   shows "list_all2 R r s = (list_all2 R r r \<and> list_all2 R s s \<and> (map Abs r = map Abs s))"
kaliszyk@35222:   apply(induct r s rule: list_induct2')
kaliszyk@35222:   apply(simp_all)
kaliszyk@35222:   using Quotient_rel[OF q]
kaliszyk@35222:   apply(metis)
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@35222: lemma list_quotient[quot_thm]:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@37492:   shows "Quotient (list_all2 R) (map Abs) (map Rep)"
kaliszyk@35222:   unfolding Quotient_def
kaliszyk@35222:   apply(subst split_list_all)
kaliszyk@35222:   apply(simp add: Quotient_abs_rep[OF q] abs_o_rep[OF q] map_id)
kaliszyk@37492:   apply(intro conjI allI)
kaliszyk@35222:   apply(induct_tac a)
kaliszyk@37492:   apply(simp_all add: Quotient_rep_reflp[OF q])
kaliszyk@37492:   apply(rule list_all2_rel[OF q])
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@35222: lemma cons_prs_aux:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@35222:   shows "(map Abs) ((Rep h) # (map Rep t)) = h # t"
kaliszyk@35222:   by (induct t) (simp_all add: Quotient_abs_rep[OF q])
kaliszyk@35222: 
kaliszyk@35222: lemma cons_prs[quot_preserve]:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@35222:   shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
nipkow@39302:   by (simp only: fun_eq_iff fun_map_def cons_prs_aux[OF q])
kaliszyk@35222:      (simp)
kaliszyk@35222: 
kaliszyk@35222: lemma cons_rsp[quot_respect]:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@37492:   shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
kaliszyk@35222:   by (auto)
kaliszyk@35222: 
kaliszyk@35222: lemma nil_prs[quot_preserve]:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@35222:   shows "map Abs [] = []"
kaliszyk@35222:   by simp
kaliszyk@35222: 
kaliszyk@35222: lemma nil_rsp[quot_respect]:
kaliszyk@35222:   assumes q: "Quotient R Abs Rep"
kaliszyk@37492:   shows "list_all2 R [] []"
kaliszyk@35222:   by simp
kaliszyk@35222: 
kaliszyk@35222: lemma map_prs_aux:
kaliszyk@35222:   assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222:   and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222:   shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
kaliszyk@35222:   by (induct l)
kaliszyk@35222:      (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222: 
kaliszyk@35222: lemma map_prs[quot_preserve]:
kaliszyk@35222:   assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222:   and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222:   shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
kaliszyk@36216:   and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
nipkow@39302:   by (simp_all only: fun_eq_iff fun_map_def map_prs_aux[OF a b])
kaliszyk@36216:      (simp_all add: Quotient_abs_rep[OF a])
kaliszyk@35222: 
kaliszyk@35222: lemma map_rsp[quot_respect]:
kaliszyk@35222:   assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222:   and     q2: "Quotient 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"
kaliszyk@36216:   apply simp_all
kaliszyk@36216:   apply(rule_tac [!] allI)+
kaliszyk@36216:   apply(rule_tac [!] impI)
kaliszyk@36216:   apply(rule_tac [!] allI)+
kaliszyk@36216:   apply (induct_tac [!] xa ya rule: list_induct2')
kaliszyk@35222:   apply simp_all
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@35222: lemma foldr_prs_aux:
kaliszyk@35222:   assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222:   and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222:   shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
kaliszyk@35222:   by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222: 
kaliszyk@35222: lemma foldr_prs[quot_preserve]:
kaliszyk@35222:   assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222:   and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222:   shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
nipkow@39302:   by (simp only: fun_eq_iff fun_map_def foldr_prs_aux[OF a b])
kaliszyk@35222:      (simp)
kaliszyk@35222: 
kaliszyk@35222: lemma foldl_prs_aux:
kaliszyk@35222:   assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222:   and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222:   shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
kaliszyk@35222:   by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222: 
kaliszyk@35222: 
kaliszyk@35222: lemma foldl_prs[quot_preserve]:
kaliszyk@35222:   assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222:   and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222:   shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
nipkow@39302:   by (simp only: fun_eq_iff fun_map_def foldl_prs_aux[OF a b])
kaliszyk@35222:      (simp)
kaliszyk@35222: 
kaliszyk@37492: lemma list_all2_empty:
kaliszyk@37492:   shows "list_all2 R [] b \<Longrightarrow> length b = 0"
kaliszyk@35222:   by (induct b) (simp_all)
kaliszyk@35222: 
kaliszyk@35222: (* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
kaliszyk@35222: lemma foldl_rsp[quot_respect]:
kaliszyk@35222:   assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222:   and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@37492:   shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
kaliszyk@35222:   apply(auto)
kaliszyk@37492:   apply (subgoal_tac "R1 xa ya \<longrightarrow> list_all2 R2 xb yb \<longrightarrow> R1 (foldl x xa xb) (foldl y ya yb)")
kaliszyk@35222:   apply simp
kaliszyk@35222:   apply (rule_tac x="xa" in spec)
kaliszyk@35222:   apply (rule_tac x="ya" in spec)
kaliszyk@35222:   apply (rule_tac xs="xb" and ys="yb" in list_induct2)
kaliszyk@37492:   apply (rule list_all2_lengthD)
kaliszyk@35222:   apply (simp_all)
kaliszyk@35222:   done
kaliszyk@35222: 
kaliszyk@35222: lemma foldr_rsp[quot_respect]:
kaliszyk@35222:   assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222:   and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@37492:   shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
kaliszyk@35222:   apply auto
kaliszyk@37492:   apply(subgoal_tac "R2 xb yb \<longrightarrow> list_all2 R1 xa ya \<longrightarrow> R2 (foldr x xa xb) (foldr y ya yb)")
kaliszyk@35222:   apply simp
kaliszyk@35222:   apply (rule_tac xs="xa" and ys="ya" in list_induct2)
kaliszyk@37492:   apply (rule list_all2_lengthD)
kaliszyk@35222:   apply (simp_all)
kaliszyk@35222:   done
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"
kaliszyk@36154:   proof -
kaliszyk@37492:     have a: "length y = length x" by (rule list_all2_lengthD[OF l1, symmetric])
kaliszyk@37492:     have c: "length a = length b" by (rule list_all2_lengthD[OF l2])
kaliszyk@36154:     show ?thesis proof (cases "length x = length a")
kaliszyk@36154:       case True
kaliszyk@36154:       have b: "length x = length a" by fact
kaliszyk@36154:       show ?thesis using a b c r l1 l2 proof (induct rule: list_induct4)
kaliszyk@36154:         case Nil
kaliszyk@36154:         show ?case using assms by simp
kaliszyk@36154:       next
kaliszyk@36154:         case (Cons h t)
kaliszyk@36154:         then show ?case by auto
kaliszyk@36154:       qed
kaliszyk@36154:     next
kaliszyk@36154:       case False
kaliszyk@36154:       have d: "length x \<noteq> length a" by fact
kaliszyk@37492:       then have e: "\<not>list_all2 S x a" using list_all2_lengthD by auto
kaliszyk@36154:       have "length y \<noteq> length b" using d a c by simp
kaliszyk@37492:       then have "\<not>list_all2 T y b" using list_all2_lengthD by auto
kaliszyk@36154:       then show ?thesis using e by simp
kaliszyk@36154:     qed
kaliszyk@36154:   qed
kaliszyk@36154: 
kaliszyk@36154: lemma[quot_respect]:
kaliszyk@37492:   "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
kaliszyk@37492:   by (simp add: list_all2_rsp)
kaliszyk@36154: 
kaliszyk@36154: lemma[quot_preserve]:
kaliszyk@36154:   assumes a: "Quotient 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')
kaliszyk@36154:   apply (simp_all add: Quotient_abs_rep[OF a])
kaliszyk@36154:   done
kaliszyk@36154: 
kaliszyk@36154: lemma[quot_preserve]:
kaliszyk@36154:   assumes a: "Quotient R abs1 rep1"
kaliszyk@37492:   shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
kaliszyk@36154:   by (induct l m rule: list_induct2') (simp_all add: Quotient_rel_rep[OF a])
kaliszyk@36154: 
kaliszyk@37492: lemma list_all2_eq[id_simps]:
kaliszyk@37492:   shows "(list_all2 (op =)) = (op =)"
nipkow@39302:   unfolding fun_eq_iff
kaliszyk@35222:   apply(rule allI)+
kaliszyk@35222:   apply(induct_tac x xa rule: list_induct2')
kaliszyk@35222:   apply(simp_all)
kaliszyk@35222:   done
kaliszyk@35222: 
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)"
kaliszyk@36276: proof -
kaliszyk@37492:   have "length a = length b" using b by (rule list_all2_lengthD)
kaliszyk@36276:   then show ?thesis using a b by (induct a b rule: list_induct2) auto
kaliszyk@36276: qed
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




