src/HOL/Library/Quotient_List.thy
author nipkow
Mon Sep 13 11:13:15 2010 +0200 (2010-09-13)
changeset 39302 d7728f65b353
parent 39198 f967a16dfcdd
child 40032 5f78dfb2fa7d
permissions -rw-r--r--
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
     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 thm list_induct3
    44 
    45 lemma list_all2_transp:
    46   assumes a: "equivp R"
    47   and b: "list_all2 R xs1 xs2"
    48   and c: "list_all2 R xs2 xs3"
    49   shows "list_all2 R xs1 xs3"
    50   using list_all2_lengthD[OF b] list_all2_lengthD[OF c] b c
    51   apply(induct rule: list_induct3)
    52   apply(simp_all)
    53   apply(auto intro: equivp_transp[OF a])
    54   done
    55 
    56 lemma list_equivp[quot_equiv]:
    57   assumes a: "equivp R"
    58   shows "equivp (list_all2 R)"
    59   apply (intro equivpI)
    60   unfolding reflp_def symp_def transp_def
    61   apply(simp add: list_all2_reflp[OF a])
    62   apply(blast intro: list_all2_symp[OF a])
    63   apply(blast intro: list_all2_transp[OF a])
    64   done
    65 
    66 lemma list_all2_rel:
    67   assumes q: "Quotient R Abs Rep"
    68   shows "list_all2 R r s = (list_all2 R r r \<and> list_all2 R s s \<and> (map Abs r = map Abs s))"
    69   apply(induct r s rule: list_induct2')
    70   apply(simp_all)
    71   using Quotient_rel[OF q]
    72   apply(metis)
    73   done
    74 
    75 lemma list_quotient[quot_thm]:
    76   assumes q: "Quotient R Abs Rep"
    77   shows "Quotient (list_all2 R) (map Abs) (map Rep)"
    78   unfolding Quotient_def
    79   apply(subst split_list_all)
    80   apply(simp add: Quotient_abs_rep[OF q] abs_o_rep[OF q] map_id)
    81   apply(intro conjI allI)
    82   apply(induct_tac a)
    83   apply(simp_all add: Quotient_rep_reflp[OF q])
    84   apply(rule list_all2_rel[OF q])
    85   done
    86 
    87 lemma cons_prs_aux:
    88   assumes q: "Quotient R Abs Rep"
    89   shows "(map Abs) ((Rep h) # (map Rep t)) = h # t"
    90   by (induct t) (simp_all add: Quotient_abs_rep[OF q])
    91 
    92 lemma cons_prs[quot_preserve]:
    93   assumes q: "Quotient R Abs Rep"
    94   shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
    95   by (simp only: fun_eq_iff fun_map_def cons_prs_aux[OF q])
    96      (simp)
    97 
    98 lemma cons_rsp[quot_respect]:
    99   assumes q: "Quotient R Abs Rep"
   100   shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
   101   by (auto)
   102 
   103 lemma nil_prs[quot_preserve]:
   104   assumes q: "Quotient R Abs Rep"
   105   shows "map Abs [] = []"
   106   by simp
   107 
   108 lemma nil_rsp[quot_respect]:
   109   assumes q: "Quotient R Abs Rep"
   110   shows "list_all2 R [] []"
   111   by simp
   112 
   113 lemma map_prs_aux:
   114   assumes a: "Quotient R1 abs1 rep1"
   115   and     b: "Quotient R2 abs2 rep2"
   116   shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
   117   by (induct l)
   118      (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   119 
   120 lemma map_prs[quot_preserve]:
   121   assumes a: "Quotient R1 abs1 rep1"
   122   and     b: "Quotient R2 abs2 rep2"
   123   shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
   124   and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
   125   by (simp_all only: fun_eq_iff fun_map_def map_prs_aux[OF a b])
   126      (simp_all add: Quotient_abs_rep[OF a])
   127 
   128 lemma map_rsp[quot_respect]:
   129   assumes q1: "Quotient R1 Abs1 Rep1"
   130   and     q2: "Quotient R2 Abs2 Rep2"
   131   shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
   132   and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
   133   apply simp_all
   134   apply(rule_tac [!] allI)+
   135   apply(rule_tac [!] impI)
   136   apply(rule_tac [!] allI)+
   137   apply (induct_tac [!] xa ya rule: list_induct2')
   138   apply simp_all
   139   done
   140 
   141 lemma foldr_prs_aux:
   142   assumes a: "Quotient R1 abs1 rep1"
   143   and     b: "Quotient R2 abs2 rep2"
   144   shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
   145   by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   146 
   147 lemma foldr_prs[quot_preserve]:
   148   assumes a: "Quotient R1 abs1 rep1"
   149   and     b: "Quotient R2 abs2 rep2"
   150   shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
   151   by (simp only: fun_eq_iff fun_map_def foldr_prs_aux[OF a b])
   152      (simp)
   153 
   154 lemma foldl_prs_aux:
   155   assumes a: "Quotient R1 abs1 rep1"
   156   and     b: "Quotient R2 abs2 rep2"
   157   shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
   158   by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
   159 
   160 
   161 lemma foldl_prs[quot_preserve]:
   162   assumes a: "Quotient R1 abs1 rep1"
   163   and     b: "Quotient R2 abs2 rep2"
   164   shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
   165   by (simp only: fun_eq_iff fun_map_def foldl_prs_aux[OF a b])
   166      (simp)
   167 
   168 lemma list_all2_empty:
   169   shows "list_all2 R [] b \<Longrightarrow> length b = 0"
   170   by (induct b) (simp_all)
   171 
   172 (* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
   173 lemma foldl_rsp[quot_respect]:
   174   assumes q1: "Quotient R1 Abs1 Rep1"
   175   and     q2: "Quotient R2 Abs2 Rep2"
   176   shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
   177   apply(auto)
   178   apply (subgoal_tac "R1 xa ya \<longrightarrow> list_all2 R2 xb yb \<longrightarrow> R1 (foldl x xa xb) (foldl y ya yb)")
   179   apply simp
   180   apply (rule_tac x="xa" in spec)
   181   apply (rule_tac x="ya" in spec)
   182   apply (rule_tac xs="xb" and ys="yb" in list_induct2)
   183   apply (rule list_all2_lengthD)
   184   apply (simp_all)
   185   done
   186 
   187 lemma foldr_rsp[quot_respect]:
   188   assumes q1: "Quotient R1 Abs1 Rep1"
   189   and     q2: "Quotient R2 Abs2 Rep2"
   190   shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
   191   apply auto
   192   apply(subgoal_tac "R2 xb yb \<longrightarrow> list_all2 R1 xa ya \<longrightarrow> R2 (foldr x xa xb) (foldr y ya yb)")
   193   apply simp
   194   apply (rule_tac xs="xa" and ys="ya" in list_induct2)
   195   apply (rule list_all2_lengthD)
   196   apply (simp_all)
   197   done
   198 
   199 lemma list_all2_rsp:
   200   assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
   201   and l1: "list_all2 R x y"
   202   and l2: "list_all2 R a b"
   203   shows "list_all2 S x a = list_all2 T y b"
   204   proof -
   205     have a: "length y = length x" by (rule list_all2_lengthD[OF l1, symmetric])
   206     have c: "length a = length b" by (rule list_all2_lengthD[OF l2])
   207     show ?thesis proof (cases "length x = length a")
   208       case True
   209       have b: "length x = length a" by fact
   210       show ?thesis using a b c r l1 l2 proof (induct rule: list_induct4)
   211         case Nil
   212         show ?case using assms by simp
   213       next
   214         case (Cons h t)
   215         then show ?case by auto
   216       qed
   217     next
   218       case False
   219       have d: "length x \<noteq> length a" by fact
   220       then have e: "\<not>list_all2 S x a" using list_all2_lengthD by auto
   221       have "length y \<noteq> length b" using d a c by simp
   222       then have "\<not>list_all2 T y b" using list_all2_lengthD by auto
   223       then show ?thesis using e by simp
   224     qed
   225   qed
   226 
   227 lemma[quot_respect]:
   228   "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
   229   by (simp add: list_all2_rsp)
   230 
   231 lemma[quot_preserve]:
   232   assumes a: "Quotient R abs1 rep1"
   233   shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
   234   apply (simp add: fun_eq_iff)
   235   apply clarify
   236   apply (induct_tac xa xb rule: list_induct2')
   237   apply (simp_all add: Quotient_abs_rep[OF a])
   238   done
   239 
   240 lemma[quot_preserve]:
   241   assumes a: "Quotient R abs1 rep1"
   242   shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
   243   by (induct l m rule: list_induct2') (simp_all add: Quotient_rel_rep[OF a])
   244 
   245 lemma list_all2_eq[id_simps]:
   246   shows "(list_all2 (op =)) = (op =)"
   247   unfolding fun_eq_iff
   248   apply(rule allI)+
   249   apply(induct_tac x xa rule: list_induct2')
   250   apply(simp_all)
   251   done
   252 
   253 lemma list_all2_find_element:
   254   assumes a: "x \<in> set a"
   255   and b: "list_all2 R a b"
   256   shows "\<exists>y. (y \<in> set b \<and> R x y)"
   257 proof -
   258   have "length a = length b" using b by (rule list_all2_lengthD)
   259   then show ?thesis using a b by (induct a b rule: list_induct2) auto
   260 qed
   261 
   262 lemma list_all2_refl:
   263   assumes a: "\<And>x y. R x y = (R x = R y)"
   264   shows "list_all2 R x x"
   265   by (induct x) (auto simp add: a)
   266 
   267 end