src/HOL/Library/Quotient_List.thy
author haftmann
Tue Nov 09 14:02:12 2010 +0100 (2010-11-09)
changeset 40463 75e544159549
parent 40032 5f78dfb2fa7d
child 40820 fd9c98ead9a9
permissions -rw-r--r--
fun_rel_def is no simp rule by default
     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 split_list_all:
    14   shows "(\<forall>x. P x) \<longleftrightarrow> P [] \<and> (\<forall>x xs. P (x#xs))"
    15   apply(auto)
    16   apply(case_tac x)
    17   apply(simp_all)
    18   done
    19 
    20 lemma map_id[id_simps]:
    21   shows "map id = id"
    22   apply(simp add: fun_eq_iff)
    23   apply(rule allI)
    24   apply(induct_tac x)
    25   apply(simp_all)
    26   done
    27 
    28 lemma list_all2_reflp:
    29   shows "equivp R \<Longrightarrow> list_all2 R xs xs"
    30   by (induct xs, simp_all add: equivp_reflp)
    31 
    32 lemma list_all2_symp:
    33   assumes a: "equivp R"
    34   and b: "list_all2 R xs ys"
    35   shows "list_all2 R ys xs"
    36   using list_all2_lengthD[OF b] b
    37   apply(induct xs ys rule: list_induct2)
    38   apply(simp_all)
    39   apply(rule equivp_symp[OF a])
    40   apply(simp)
    41   done
    42 
    43 lemma list_all2_transp:
    44   assumes a: "equivp R"
    45   and b: "list_all2 R xs1 xs2"
    46   and c: "list_all2 R xs2 xs3"
    47   shows "list_all2 R xs1 xs3"
    48   using list_all2_lengthD[OF b] list_all2_lengthD[OF c] b c
    49   apply(induct rule: list_induct3)
    50   apply(simp_all)
    51   apply(auto intro: equivp_transp[OF a])
    52   done
    53 
    54 lemma list_equivp[quot_equiv]:
    55   assumes a: "equivp R"
    56   shows "equivp (list_all2 R)"
    57   apply (intro equivpI)
    58   unfolding reflp_def symp_def transp_def
    59   apply(simp add: list_all2_reflp[OF a])
    60   apply(blast intro: list_all2_symp[OF a])
    61   apply(blast intro: list_all2_transp[OF a])
    62   done
    63 
    64 lemma list_all2_rel:
    65   assumes q: "Quotient R Abs Rep"
    66   shows "list_all2 R r s = (list_all2 R r r \<and> list_all2 R s s \<and> (map Abs r = map Abs s))"
    67   apply(induct r s rule: list_induct2')
    68   apply(simp_all)
    69   using Quotient_rel[OF q]
    70   apply(metis)
    71   done
    72 
    73 lemma list_quotient[quot_thm]:
    74   assumes q: "Quotient R Abs Rep"
    75   shows "Quotient (list_all2 R) (map Abs) (map Rep)"
    76   unfolding Quotient_def
    77   apply(subst split_list_all)
    78   apply(simp add: Quotient_abs_rep[OF q] abs_o_rep[OF q] map_id)
    79   apply(intro conjI allI)
    80   apply(induct_tac a)
    81   apply(simp_all add: Quotient_rep_reflp[OF q])
    82   apply(rule list_all2_rel[OF q])
    83   done
    84 
    85 lemma cons_prs[quot_preserve]:
    86   assumes q: "Quotient R Abs Rep"
    87   shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
    88   by (auto simp add: fun_eq_iff comp_def Quotient_abs_rep [OF q])
    89 
    90 lemma cons_rsp[quot_respect]:
    91   assumes q: "Quotient R Abs Rep"
    92   shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
    93   by auto
    94 
    95 lemma nil_prs[quot_preserve]:
    96   assumes q: "Quotient R Abs Rep"
    97   shows "map Abs [] = []"
    98   by simp
    99 
   100 lemma nil_rsp[quot_respect]:
   101   assumes q: "Quotient R Abs Rep"
   102   shows "list_all2 R [] []"
   103   by simp
   104 
   105 lemma map_prs_aux:
   106   assumes a: "Quotient R1 abs1 rep1"
   107   and     b: "Quotient R2 abs2 rep2"
   108   shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
   109   by (induct l)
   110      (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   111 
   112 lemma map_prs[quot_preserve]:
   113   assumes a: "Quotient R1 abs1 rep1"
   114   and     b: "Quotient R2 abs2 rep2"
   115   shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
   116   and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
   117   by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
   118     (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   119 
   120 
   121 lemma map_rsp[quot_respect]:
   122   assumes q1: "Quotient R1 Abs1 Rep1"
   123   and     q2: "Quotient R2 Abs2 Rep2"
   124   shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
   125   and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
   126   apply (simp_all add: fun_rel_def)
   127   apply(rule_tac [!] allI)+
   128   apply(rule_tac [!] impI)
   129   apply(rule_tac [!] allI)+
   130   apply (induct_tac [!] xa ya rule: list_induct2')
   131   apply simp_all
   132   done
   133 
   134 lemma foldr_prs_aux:
   135   assumes a: "Quotient R1 abs1 rep1"
   136   and     b: "Quotient R2 abs2 rep2"
   137   shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
   138   by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   139 
   140 lemma foldr_prs[quot_preserve]:
   141   assumes a: "Quotient R1 abs1 rep1"
   142   and     b: "Quotient R2 abs2 rep2"
   143   shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
   144   apply (simp add: fun_eq_iff)
   145   by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
   146      (simp)
   147 
   148 lemma foldl_prs_aux:
   149   assumes a: "Quotient R1 abs1 rep1"
   150   and     b: "Quotient R2 abs2 rep2"
   151   shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
   152   by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   153 
   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_eq[id_simps]:
   239   shows "(list_all2 (op =)) = (op =)"
   240   unfolding fun_eq_iff
   241   apply(rule allI)+
   242   apply(induct_tac x xa rule: list_induct2')
   243   apply(simp_all)
   244   done
   245 
   246 lemma list_all2_find_element:
   247   assumes a: "x \<in> set a"
   248   and b: "list_all2 R a b"
   249   shows "\<exists>y. (y \<in> set b \<and> R x y)"
   250 proof -
   251   have "length a = length b" using b by (rule list_all2_lengthD)
   252   then show ?thesis using a b by (induct a b rule: list_induct2) auto
   253 qed
   254 
   255 lemma list_all2_refl:
   256   assumes a: "\<And>x y. R x y = (R x = R y)"
   257   shows "list_all2 R x x"
   258   by (induct x) (auto simp add: a)
   259 
   260 end