src/HOL/Library/Quotient_List.thy
changeset 35222 4f1fba00f66d
child 35788 f1deaca15ca3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Quotient_List.thy	Fri Feb 19 13:54:19 2010 +0100
     1.3 @@ -0,0 +1,232 @@
     1.4 +(*  Title:      Quotient_List.thy
     1.5 +    Author:     Cezary Kaliszyk and Christian Urban
     1.6 +*)
     1.7 +theory Quotient_List
     1.8 +imports Main Quotient_Syntax
     1.9 +begin
    1.10 +
    1.11 +section {* Quotient infrastructure for the list type. *}
    1.12 +
    1.13 +fun
    1.14 +  list_rel
    1.15 +where
    1.16 +  "list_rel R [] [] = True"
    1.17 +| "list_rel R (x#xs) [] = False"
    1.18 +| "list_rel R [] (x#xs) = False"
    1.19 +| "list_rel R (x#xs) (y#ys) = (R x y \<and> list_rel R xs ys)"
    1.20 +
    1.21 +declare [[map list = (map, list_rel)]]
    1.22 +
    1.23 +lemma split_list_all:
    1.24 +  shows "(\<forall>x. P x) \<longleftrightarrow> P [] \<and> (\<forall>x xs. P (x#xs))"
    1.25 +  apply(auto)
    1.26 +  apply(case_tac x)
    1.27 +  apply(simp_all)
    1.28 +  done
    1.29 +
    1.30 +lemma map_id[id_simps]:
    1.31 +  shows "map id = id"
    1.32 +  apply(simp add: expand_fun_eq)
    1.33 +  apply(rule allI)
    1.34 +  apply(induct_tac x)
    1.35 +  apply(simp_all)
    1.36 +  done
    1.37 +
    1.38 +
    1.39 +lemma list_rel_reflp:
    1.40 +  shows "equivp R \<Longrightarrow> list_rel R xs xs"
    1.41 +  apply(induct xs)
    1.42 +  apply(simp_all add: equivp_reflp)
    1.43 +  done
    1.44 +
    1.45 +lemma list_rel_symp:
    1.46 +  assumes a: "equivp R"
    1.47 +  shows "list_rel R xs ys \<Longrightarrow> list_rel R ys xs"
    1.48 +  apply(induct xs ys rule: list_induct2')
    1.49 +  apply(simp_all)
    1.50 +  apply(rule equivp_symp[OF a])
    1.51 +  apply(simp)
    1.52 +  done
    1.53 +
    1.54 +lemma list_rel_transp:
    1.55 +  assumes a: "equivp R"
    1.56 +  shows "list_rel R xs1 xs2 \<Longrightarrow> list_rel R xs2 xs3 \<Longrightarrow> list_rel R xs1 xs3"
    1.57 +  apply(induct xs1 xs2 arbitrary: xs3 rule: list_induct2')
    1.58 +  apply(simp_all)
    1.59 +  apply(case_tac xs3)
    1.60 +  apply(simp_all)
    1.61 +  apply(rule equivp_transp[OF a])
    1.62 +  apply(auto)
    1.63 +  done
    1.64 +
    1.65 +lemma list_equivp[quot_equiv]:
    1.66 +  assumes a: "equivp R"
    1.67 +  shows "equivp (list_rel R)"
    1.68 +  apply(rule equivpI)
    1.69 +  unfolding reflp_def symp_def transp_def
    1.70 +  apply(subst split_list_all)
    1.71 +  apply(simp add: equivp_reflp[OF a] list_rel_reflp[OF a])
    1.72 +  apply(blast intro: list_rel_symp[OF a])
    1.73 +  apply(blast intro: list_rel_transp[OF a])
    1.74 +  done
    1.75 +
    1.76 +lemma list_rel_rel:
    1.77 +  assumes q: "Quotient R Abs Rep"
    1.78 +  shows "list_rel R r s = (list_rel R r r \<and> list_rel R s s \<and> (map Abs r = map Abs s))"
    1.79 +  apply(induct r s rule: list_induct2')
    1.80 +  apply(simp_all)
    1.81 +  using Quotient_rel[OF q]
    1.82 +  apply(metis)
    1.83 +  done
    1.84 +
    1.85 +lemma list_quotient[quot_thm]:
    1.86 +  assumes q: "Quotient R Abs Rep"
    1.87 +  shows "Quotient (list_rel R) (map Abs) (map Rep)"
    1.88 +  unfolding Quotient_def
    1.89 +  apply(subst split_list_all)
    1.90 +  apply(simp add: Quotient_abs_rep[OF q] abs_o_rep[OF q] map_id)
    1.91 +  apply(rule conjI)
    1.92 +  apply(rule allI)
    1.93 +  apply(induct_tac a)
    1.94 +  apply(simp)
    1.95 +  apply(simp)
    1.96 +  apply(simp add: Quotient_rep_reflp[OF q])
    1.97 +  apply(rule allI)+
    1.98 +  apply(rule list_rel_rel[OF q])
    1.99 +  done
   1.100 +
   1.101 +
   1.102 +lemma cons_prs_aux:
   1.103 +  assumes q: "Quotient R Abs Rep"
   1.104 +  shows "(map Abs) ((Rep h) # (map Rep t)) = h # t"
   1.105 +  by (induct t) (simp_all add: Quotient_abs_rep[OF q])
   1.106 +
   1.107 +lemma cons_prs[quot_preserve]:
   1.108 +  assumes q: "Quotient R Abs Rep"
   1.109 +  shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
   1.110 +  by (simp only: expand_fun_eq fun_map_def cons_prs_aux[OF q])
   1.111 +     (simp)
   1.112 +
   1.113 +lemma cons_rsp[quot_respect]:
   1.114 +  assumes q: "Quotient R Abs Rep"
   1.115 +  shows "(R ===> list_rel R ===> list_rel R) (op #) (op #)"
   1.116 +  by (auto)
   1.117 +
   1.118 +lemma nil_prs[quot_preserve]:
   1.119 +  assumes q: "Quotient R Abs Rep"
   1.120 +  shows "map Abs [] = []"
   1.121 +  by simp
   1.122 +
   1.123 +lemma nil_rsp[quot_respect]:
   1.124 +  assumes q: "Quotient R Abs Rep"
   1.125 +  shows "list_rel R [] []"
   1.126 +  by simp
   1.127 +
   1.128 +lemma map_prs_aux:
   1.129 +  assumes a: "Quotient R1 abs1 rep1"
   1.130 +  and     b: "Quotient R2 abs2 rep2"
   1.131 +  shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
   1.132 +  by (induct l)
   1.133 +     (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   1.134 +
   1.135 +
   1.136 +lemma map_prs[quot_preserve]:
   1.137 +  assumes a: "Quotient R1 abs1 rep1"
   1.138 +  and     b: "Quotient R2 abs2 rep2"
   1.139 +  shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
   1.140 +  by (simp only: expand_fun_eq fun_map_def map_prs_aux[OF a b])
   1.141 +     (simp)
   1.142 +
   1.143 +
   1.144 +lemma map_rsp[quot_respect]:
   1.145 +  assumes q1: "Quotient R1 Abs1 Rep1"
   1.146 +  and     q2: "Quotient R2 Abs2 Rep2"
   1.147 +  shows "((R1 ===> R2) ===> (list_rel R1) ===> list_rel R2) map map"
   1.148 +  apply(simp)
   1.149 +  apply(rule allI)+
   1.150 +  apply(rule impI)
   1.151 +  apply(rule allI)+
   1.152 +  apply (induct_tac xa ya rule: list_induct2')
   1.153 +  apply simp_all
   1.154 +  done
   1.155 +
   1.156 +lemma foldr_prs_aux:
   1.157 +  assumes a: "Quotient R1 abs1 rep1"
   1.158 +  and     b: "Quotient R2 abs2 rep2"
   1.159 +  shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
   1.160 +  by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   1.161 +
   1.162 +lemma foldr_prs[quot_preserve]:
   1.163 +  assumes a: "Quotient R1 abs1 rep1"
   1.164 +  and     b: "Quotient R2 abs2 rep2"
   1.165 +  shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
   1.166 +  by (simp only: expand_fun_eq fun_map_def foldr_prs_aux[OF a b])
   1.167 +     (simp)
   1.168 +
   1.169 +lemma foldl_prs_aux:
   1.170 +  assumes a: "Quotient R1 abs1 rep1"
   1.171 +  and     b: "Quotient R2 abs2 rep2"
   1.172 +  shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
   1.173 +  by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   1.174 +
   1.175 +
   1.176 +lemma foldl_prs[quot_preserve]:
   1.177 +  assumes a: "Quotient R1 abs1 rep1"
   1.178 +  and     b: "Quotient R2 abs2 rep2"
   1.179 +  shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
   1.180 +  by (simp only: expand_fun_eq fun_map_def foldl_prs_aux[OF a b])
   1.181 +     (simp)
   1.182 +
   1.183 +lemma list_rel_empty:
   1.184 +  shows "list_rel R [] b \<Longrightarrow> length b = 0"
   1.185 +  by (induct b) (simp_all)
   1.186 +
   1.187 +lemma list_rel_len:
   1.188 +  shows "list_rel R a b \<Longrightarrow> length a = length b"
   1.189 +  apply (induct a arbitrary: b)
   1.190 +  apply (simp add: list_rel_empty)
   1.191 +  apply (case_tac b)
   1.192 +  apply simp_all
   1.193 +  done
   1.194 +
   1.195 +(* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
   1.196 +lemma foldl_rsp[quot_respect]:
   1.197 +  assumes q1: "Quotient R1 Abs1 Rep1"
   1.198 +  and     q2: "Quotient R2 Abs2 Rep2"
   1.199 +  shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_rel R2 ===> R1) foldl foldl"
   1.200 +  apply(auto)
   1.201 +  apply (subgoal_tac "R1 xa ya \<longrightarrow> list_rel R2 xb yb \<longrightarrow> R1 (foldl x xa xb) (foldl y ya yb)")
   1.202 +  apply simp
   1.203 +  apply (rule_tac x="xa" in spec)
   1.204 +  apply (rule_tac x="ya" in spec)
   1.205 +  apply (rule_tac xs="xb" and ys="yb" in list_induct2)
   1.206 +  apply (rule list_rel_len)
   1.207 +  apply (simp_all)
   1.208 +  done
   1.209 +
   1.210 +lemma foldr_rsp[quot_respect]:
   1.211 +  assumes q1: "Quotient R1 Abs1 Rep1"
   1.212 +  and     q2: "Quotient R2 Abs2 Rep2"
   1.213 +  shows "((R1 ===> R2 ===> R2) ===> list_rel R1 ===> R2 ===> R2) foldr foldr"
   1.214 +  apply auto
   1.215 +  apply(subgoal_tac "R2 xb yb \<longrightarrow> list_rel R1 xa ya \<longrightarrow> R2 (foldr x xa xb) (foldr y ya yb)")
   1.216 +  apply simp
   1.217 +  apply (rule_tac xs="xa" and ys="ya" in list_induct2)
   1.218 +  apply (rule list_rel_len)
   1.219 +  apply (simp_all)
   1.220 +  done
   1.221 +
   1.222 +lemma list_rel_eq[id_simps]:
   1.223 +  shows "(list_rel (op =)) = (op =)"
   1.224 +  unfolding expand_fun_eq
   1.225 +  apply(rule allI)+
   1.226 +  apply(induct_tac x xa rule: list_induct2')
   1.227 +  apply(simp_all)
   1.228 +  done
   1.229 +
   1.230 +lemma list_rel_refl:
   1.231 +  assumes a: "\<And>x y. R x y = (R x = R y)"
   1.232 +  shows "list_rel R x x"
   1.233 +  by (induct x) (auto simp add: a)
   1.234 +
   1.235 +end