src/HOL/Library/Quotient_List.thy
author bulwahn
Fri Apr 08 16:31:14 2011 +0200 (2011-04-08)
changeset 42316 12635bb655fd
parent 40820 fd9c98ead9a9
child 45802 b16f976db515
child 45803 fe44c0b216ef
permissions -rw-r--r--
deactivating other compilations in quickcheck_exhaustive momentarily that only interesting for my benchmarks and experiments
     1 (*  Title:      HOL/Library/Quotient_List.thy
     2     Author:     Cezary Kaliszyk and Christian Urban
     3 *)
     4 
     5 header {* Quotient infrastructure for the list type *}
     6 
     7 theory Quotient_List
     8 imports Main Quotient_Syntax
     9 begin
    10 
    11 declare [[map list = (map, list_all2)]]
    12 
    13 lemma map_id [id_simps]:
    14   "map id = id"
    15   by (simp add: id_def fun_eq_iff map.identity)
    16 
    17 lemma list_all2_map1:
    18   "list_all2 R (map f xs) ys \<longleftrightarrow> list_all2 (\<lambda>x. R (f x)) xs ys"
    19   by (induct xs ys rule: list_induct2') simp_all
    20 
    21 lemma list_all2_map2:
    22   "list_all2 R xs (map f ys) \<longleftrightarrow> list_all2 (\<lambda>x y. R x (f y)) xs ys"
    23   by (induct xs ys rule: list_induct2') simp_all
    24 
    25 lemma list_all2_eq [id_simps]:
    26   "list_all2 (op =) = (op =)"
    27 proof (rule ext)+
    28   fix xs ys
    29   show "list_all2 (op =) xs ys \<longleftrightarrow> xs = ys"
    30     by (induct xs ys rule: list_induct2') simp_all
    31 qed
    32 
    33 lemma list_reflp:
    34   assumes "reflp R"
    35   shows "reflp (list_all2 R)"
    36 proof (rule reflpI)
    37   from assms have *: "\<And>xs. R xs xs" by (rule reflpE)
    38   fix xs
    39   show "list_all2 R xs xs"
    40     by (induct xs) (simp_all add: *)
    41 qed
    42 
    43 lemma list_symp:
    44   assumes "symp R"
    45   shows "symp (list_all2 R)"
    46 proof (rule sympI)
    47   from assms have *: "\<And>xs ys. R xs ys \<Longrightarrow> R ys xs" by (rule sympE)
    48   fix xs ys
    49   assume "list_all2 R xs ys"
    50   then show "list_all2 R ys xs"
    51     by (induct xs ys rule: list_induct2') (simp_all add: *)
    52 qed
    53 
    54 lemma list_transp:
    55   assumes "transp R"
    56   shows "transp (list_all2 R)"
    57 proof (rule transpI)
    58   from assms have *: "\<And>xs ys zs. R xs ys \<Longrightarrow> R ys zs \<Longrightarrow> R xs zs" by (rule transpE)
    59   fix xs ys zs
    60   assume A: "list_all2 R xs ys" "list_all2 R ys zs"
    61   then have "length xs = length ys" "length ys = length zs" by (blast dest: list_all2_lengthD)+
    62   then show "list_all2 R xs zs" using A
    63     by (induct xs ys zs rule: list_induct3) (auto intro: *)
    64 qed
    65 
    66 lemma list_equivp [quot_equiv]:
    67   "equivp R \<Longrightarrow> equivp (list_all2 R)"
    68   by (blast intro: equivpI list_reflp list_symp list_transp elim: equivpE)
    69 
    70 lemma list_quotient [quot_thm]:
    71   assumes "Quotient R Abs Rep"
    72   shows "Quotient (list_all2 R) (map Abs) (map Rep)"
    73 proof (rule QuotientI)
    74   from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient_abs_rep)
    75   then show "\<And>xs. map Abs (map Rep xs) = xs" by (simp add: comp_def)
    76 next
    77   from assms have "\<And>x y. R (Rep x) (Rep y) \<longleftrightarrow> x = y" by (rule Quotient_rel_rep)
    78   then show "\<And>xs. list_all2 R (map Rep xs) (map Rep xs)"
    79     by (simp add: list_all2_map1 list_all2_map2 list_all2_eq)
    80 next
    81   fix xs ys
    82   from assms have "\<And>x y. R x x \<and> R y y \<and> Abs x = Abs y \<longleftrightarrow> R x y" by (rule Quotient_rel)
    83   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"
    84     by (induct xs ys rule: list_induct2') auto
    85 qed
    86 
    87 lemma cons_prs [quot_preserve]:
    88   assumes q: "Quotient R Abs Rep"
    89   shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
    90   by (auto simp add: fun_eq_iff comp_def Quotient_abs_rep [OF q])
    91 
    92 lemma cons_rsp [quot_respect]:
    93   assumes q: "Quotient R Abs Rep"
    94   shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
    95   by auto
    96 
    97 lemma nil_prs [quot_preserve]:
    98   assumes q: "Quotient R Abs Rep"
    99   shows "map Abs [] = []"
   100   by simp
   101 
   102 lemma nil_rsp [quot_respect]:
   103   assumes q: "Quotient R Abs Rep"
   104   shows "list_all2 R [] []"
   105   by simp
   106 
   107 lemma map_prs_aux:
   108   assumes a: "Quotient R1 abs1 rep1"
   109   and     b: "Quotient R2 abs2 rep2"
   110   shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
   111   by (induct l)
   112      (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   113 
   114 lemma map_prs [quot_preserve]:
   115   assumes a: "Quotient R1 abs1 rep1"
   116   and     b: "Quotient R2 abs2 rep2"
   117   shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
   118   and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
   119   by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
   120     (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   121 
   122 lemma map_rsp [quot_respect]:
   123   assumes q1: "Quotient R1 Abs1 Rep1"
   124   and     q2: "Quotient R2 Abs2 Rep2"
   125   shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
   126   and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
   127   apply (simp_all add: fun_rel_def)
   128   apply(rule_tac [!] allI)+
   129   apply(rule_tac [!] impI)
   130   apply(rule_tac [!] allI)+
   131   apply (induct_tac [!] xa ya rule: list_induct2')
   132   apply simp_all
   133   done
   134 
   135 lemma foldr_prs_aux:
   136   assumes a: "Quotient R1 abs1 rep1"
   137   and     b: "Quotient R2 abs2 rep2"
   138   shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
   139   by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   140 
   141 lemma foldr_prs [quot_preserve]:
   142   assumes a: "Quotient R1 abs1 rep1"
   143   and     b: "Quotient R2 abs2 rep2"
   144   shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
   145   apply (simp add: fun_eq_iff)
   146   by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
   147      (simp)
   148 
   149 lemma foldl_prs_aux:
   150   assumes a: "Quotient R1 abs1 rep1"
   151   and     b: "Quotient R2 abs2 rep2"
   152   shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
   153   by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   154 
   155 lemma foldl_prs [quot_preserve]:
   156   assumes a: "Quotient R1 abs1 rep1"
   157   and     b: "Quotient R2 abs2 rep2"
   158   shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
   159   by (simp add: fun_eq_iff foldl_prs_aux [OF a b])
   160 
   161 lemma list_all2_empty:
   162   shows "list_all2 R [] b \<Longrightarrow> length b = 0"
   163   by (induct b) (simp_all)
   164 
   165 (* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
   166 lemma foldl_rsp[quot_respect]:
   167   assumes q1: "Quotient R1 Abs1 Rep1"
   168   and     q2: "Quotient R2 Abs2 Rep2"
   169   shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
   170   apply(auto simp add: fun_rel_def)
   171   apply (subgoal_tac "R1 xa ya \<longrightarrow> list_all2 R2 xb yb \<longrightarrow> R1 (foldl x xa xb) (foldl y ya yb)")
   172   apply simp
   173   apply (rule_tac x="xa" in spec)
   174   apply (rule_tac x="ya" in spec)
   175   apply (rule_tac xs="xb" and ys="yb" in list_induct2)
   176   apply (rule list_all2_lengthD)
   177   apply (simp_all)
   178   done
   179 
   180 lemma foldr_rsp[quot_respect]:
   181   assumes q1: "Quotient R1 Abs1 Rep1"
   182   and     q2: "Quotient R2 Abs2 Rep2"
   183   shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
   184   apply (auto simp add: fun_rel_def)
   185   apply(subgoal_tac "R2 xb yb \<longrightarrow> list_all2 R1 xa ya \<longrightarrow> R2 (foldr x xa xb) (foldr y ya yb)")
   186   apply simp
   187   apply (rule_tac xs="xa" and ys="ya" in list_induct2)
   188   apply (rule list_all2_lengthD)
   189   apply (simp_all)
   190   done
   191 
   192 lemma list_all2_rsp:
   193   assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
   194   and l1: "list_all2 R x y"
   195   and l2: "list_all2 R a b"
   196   shows "list_all2 S x a = list_all2 T y b"
   197   proof -
   198     have a: "length y = length x" by (rule list_all2_lengthD[OF l1, symmetric])
   199     have c: "length a = length b" by (rule list_all2_lengthD[OF l2])
   200     show ?thesis proof (cases "length x = length a")
   201       case True
   202       have b: "length x = length a" by fact
   203       show ?thesis using a b c r l1 l2 proof (induct rule: list_induct4)
   204         case Nil
   205         show ?case using assms by simp
   206       next
   207         case (Cons h t)
   208         then show ?case by auto
   209       qed
   210     next
   211       case False
   212       have d: "length x \<noteq> length a" by fact
   213       then have e: "\<not>list_all2 S x a" using list_all2_lengthD by auto
   214       have "length y \<noteq> length b" using d a c by simp
   215       then have "\<not>list_all2 T y b" using list_all2_lengthD by auto
   216       then show ?thesis using e by simp
   217     qed
   218   qed
   219 
   220 lemma [quot_respect]:
   221   "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
   222   by (simp add: list_all2_rsp fun_rel_def)
   223 
   224 lemma [quot_preserve]:
   225   assumes a: "Quotient R abs1 rep1"
   226   shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
   227   apply (simp add: fun_eq_iff)
   228   apply clarify
   229   apply (induct_tac xa xb rule: list_induct2')
   230   apply (simp_all add: Quotient_abs_rep[OF a])
   231   done
   232 
   233 lemma [quot_preserve]:
   234   assumes a: "Quotient R abs1 rep1"
   235   shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
   236   by (induct l m rule: list_induct2') (simp_all add: Quotient_rel_rep[OF a])
   237 
   238 lemma list_all2_find_element:
   239   assumes a: "x \<in> set a"
   240   and b: "list_all2 R a b"
   241   shows "\<exists>y. (y \<in> set b \<and> R x y)"
   242 proof -
   243   have "length a = length b" using b by (rule list_all2_lengthD)
   244   then show ?thesis using a b by (induct a b rule: list_induct2) auto
   245 qed
   246 
   247 lemma list_all2_refl:
   248   assumes a: "\<And>x y. R x y = (R x = R y)"
   249   shows "list_all2 R x x"
   250   by (induct x) (auto simp add: a)
   251 
   252 end