src/HOL/Equiv_Relations.thy
author haftmann
Mon Nov 29 13:44:54 2010 +0100 (2010-11-29)
changeset 40815 6e2d17cc0d1d
parent 40812 ff16e22e8776
child 40816 19c492929756
permissions -rw-r--r--
equivI has replaced equiv.intro
     1 (*  Authors:    Lawrence C Paulson, Cambridge University Computer Laboratory
     2     Copyright   1996  University of Cambridge
     3 *)
     4 
     5 header {* Equivalence Relations in Higher-Order Set Theory *}
     6 
     7 theory Equiv_Relations
     8 imports Big_Operators Relation Plain
     9 begin
    10 
    11 subsection {* Equivalence relations -- set version *}
    12 
    13 definition equiv :: "'a set \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> bool" where
    14   "equiv A r \<longleftrightarrow> refl_on A r \<and> sym r \<and> trans r"
    15 
    16 lemma equivI:
    17   "refl_on A r \<Longrightarrow> sym r \<Longrightarrow> trans r \<Longrightarrow> equiv A r"
    18   by (simp add: equiv_def)
    19 
    20 lemma equivE:
    21   assumes "equiv A r"
    22   obtains "refl_on A r" and "sym r" and "trans r"
    23   using assms by (simp add: equiv_def)
    24 
    25 text {*
    26   Suppes, Theorem 70: @{text r} is an equiv relation iff @{text "r\<inverse> O
    27   r = r"}.
    28 
    29   First half: @{text "equiv A r ==> r\<inverse> O r = r"}.
    30 *}
    31 
    32 lemma sym_trans_comp_subset:
    33     "sym r ==> trans r ==> r\<inverse> O r \<subseteq> r"
    34   by (unfold trans_def sym_def converse_def) blast
    35 
    36 lemma refl_on_comp_subset: "refl_on A r ==> r \<subseteq> r\<inverse> O r"
    37   by (unfold refl_on_def) blast
    38 
    39 lemma equiv_comp_eq: "equiv A r ==> r\<inverse> O r = r"
    40   apply (unfold equiv_def)
    41   apply clarify
    42   apply (rule equalityI)
    43    apply (iprover intro: sym_trans_comp_subset refl_on_comp_subset)+
    44   done
    45 
    46 text {* Second half. *}
    47 
    48 lemma comp_equivI:
    49     "r\<inverse> O r = r ==> Domain r = A ==> equiv A r"
    50   apply (unfold equiv_def refl_on_def sym_def trans_def)
    51   apply (erule equalityE)
    52   apply (subgoal_tac "\<forall>x y. (x, y) \<in> r --> (y, x) \<in> r")
    53    apply fast
    54   apply fast
    55   done
    56 
    57 
    58 subsection {* Equivalence classes *}
    59 
    60 lemma equiv_class_subset:
    61   "equiv A r ==> (a, b) \<in> r ==> r``{a} \<subseteq> r``{b}"
    62   -- {* lemma for the next result *}
    63   by (unfold equiv_def trans_def sym_def) blast
    64 
    65 theorem equiv_class_eq: "equiv A r ==> (a, b) \<in> r ==> r``{a} = r``{b}"
    66   apply (assumption | rule equalityI equiv_class_subset)+
    67   apply (unfold equiv_def sym_def)
    68   apply blast
    69   done
    70 
    71 lemma equiv_class_self: "equiv A r ==> a \<in> A ==> a \<in> r``{a}"
    72   by (unfold equiv_def refl_on_def) blast
    73 
    74 lemma subset_equiv_class:
    75     "equiv A r ==> r``{b} \<subseteq> r``{a} ==> b \<in> A ==> (a,b) \<in> r"
    76   -- {* lemma for the next result *}
    77   by (unfold equiv_def refl_on_def) blast
    78 
    79 lemma eq_equiv_class:
    80     "r``{a} = r``{b} ==> equiv A r ==> b \<in> A ==> (a, b) \<in> r"
    81   by (iprover intro: equalityD2 subset_equiv_class)
    82 
    83 lemma equiv_class_nondisjoint:
    84     "equiv A r ==> x \<in> (r``{a} \<inter> r``{b}) ==> (a, b) \<in> r"
    85   by (unfold equiv_def trans_def sym_def) blast
    86 
    87 lemma equiv_type: "equiv A r ==> r \<subseteq> A \<times> A"
    88   by (unfold equiv_def refl_on_def) blast
    89 
    90 theorem equiv_class_eq_iff:
    91   "equiv A r ==> ((x, y) \<in> r) = (r``{x} = r``{y} & x \<in> A & y \<in> A)"
    92   by (blast intro!: equiv_class_eq dest: eq_equiv_class equiv_type)
    93 
    94 theorem eq_equiv_class_iff:
    95   "equiv A r ==> x \<in> A ==> y \<in> A ==> (r``{x} = r``{y}) = ((x, y) \<in> r)"
    96   by (blast intro!: equiv_class_eq dest: eq_equiv_class equiv_type)
    97 
    98 
    99 subsection {* Quotients *}
   100 
   101 definition quotient :: "'a set \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> 'a set set"  (infixl "'/'/" 90) where
   102   "A//r = (\<Union>x \<in> A. {r``{x}})"  -- {* set of equiv classes *}
   103 
   104 lemma quotientI: "x \<in> A ==> r``{x} \<in> A//r"
   105   by (unfold quotient_def) blast
   106 
   107 lemma quotientE:
   108   "X \<in> A//r ==> (!!x. X = r``{x} ==> x \<in> A ==> P) ==> P"
   109   by (unfold quotient_def) blast
   110 
   111 lemma Union_quotient: "equiv A r ==> Union (A//r) = A"
   112   by (unfold equiv_def refl_on_def quotient_def) blast
   113 
   114 lemma quotient_disj:
   115   "equiv A r ==> X \<in> A//r ==> Y \<in> A//r ==> X = Y | (X \<inter> Y = {})"
   116   apply (unfold quotient_def)
   117   apply clarify
   118   apply (rule equiv_class_eq)
   119    apply assumption
   120   apply (unfold equiv_def trans_def sym_def)
   121   apply blast
   122   done
   123 
   124 lemma quotient_eqI:
   125   "[|equiv A r; X \<in> A//r; Y \<in> A//r; x \<in> X; y \<in> Y; (x,y) \<in> r|] ==> X = Y" 
   126   apply (clarify elim!: quotientE)
   127   apply (rule equiv_class_eq, assumption)
   128   apply (unfold equiv_def sym_def trans_def, blast)
   129   done
   130 
   131 lemma quotient_eq_iff:
   132   "[|equiv A r; X \<in> A//r; Y \<in> A//r; x \<in> X; y \<in> Y|] ==> (X = Y) = ((x,y) \<in> r)" 
   133   apply (rule iffI)  
   134    prefer 2 apply (blast del: equalityI intro: quotient_eqI) 
   135   apply (clarify elim!: quotientE)
   136   apply (unfold equiv_def sym_def trans_def, blast)
   137   done
   138 
   139 lemma eq_equiv_class_iff2:
   140   "\<lbrakk> equiv A r; x \<in> A; y \<in> A \<rbrakk> \<Longrightarrow> ({x}//r = {y}//r) = ((x,y) : r)"
   141 by(simp add:quotient_def eq_equiv_class_iff)
   142 
   143 
   144 lemma quotient_empty [simp]: "{}//r = {}"
   145 by(simp add: quotient_def)
   146 
   147 lemma quotient_is_empty [iff]: "(A//r = {}) = (A = {})"
   148 by(simp add: quotient_def)
   149 
   150 lemma quotient_is_empty2 [iff]: "({} = A//r) = (A = {})"
   151 by(simp add: quotient_def)
   152 
   153 
   154 lemma singleton_quotient: "{x}//r = {r `` {x}}"
   155 by(simp add:quotient_def)
   156 
   157 lemma quotient_diff1:
   158   "\<lbrakk> inj_on (%a. {a}//r) A; a \<in> A \<rbrakk> \<Longrightarrow> (A - {a})//r = A//r - {a}//r"
   159 apply(simp add:quotient_def inj_on_def)
   160 apply blast
   161 done
   162 
   163 subsection {* Defining unary operations upon equivalence classes *}
   164 
   165 text{*A congruence-preserving function*}
   166 locale congruent =
   167   fixes r and f
   168   assumes congruent: "(y,z) \<in> r ==> f y = f z"
   169 
   170 abbreviation
   171   RESPECTS :: "('a => 'b) => ('a * 'a) set => bool"
   172     (infixr "respects" 80) where
   173   "f respects r == congruent r f"
   174 
   175 
   176 lemma UN_constant_eq: "a \<in> A ==> \<forall>y \<in> A. f y = c ==> (\<Union>y \<in> A. f(y))=c"
   177   -- {* lemma required to prove @{text UN_equiv_class} *}
   178   by auto
   179 
   180 lemma UN_equiv_class:
   181   "equiv A r ==> f respects r ==> a \<in> A
   182     ==> (\<Union>x \<in> r``{a}. f x) = f a"
   183   -- {* Conversion rule *}
   184   apply (rule equiv_class_self [THEN UN_constant_eq], assumption+)
   185   apply (unfold equiv_def congruent_def sym_def)
   186   apply (blast del: equalityI)
   187   done
   188 
   189 lemma UN_equiv_class_type:
   190   "equiv A r ==> f respects r ==> X \<in> A//r ==>
   191     (!!x. x \<in> A ==> f x \<in> B) ==> (\<Union>x \<in> X. f x) \<in> B"
   192   apply (unfold quotient_def)
   193   apply clarify
   194   apply (subst UN_equiv_class)
   195      apply auto
   196   done
   197 
   198 text {*
   199   Sufficient conditions for injectiveness.  Could weaken premises!
   200   major premise could be an inclusion; bcong could be @{text "!!y. y \<in>
   201   A ==> f y \<in> B"}.
   202 *}
   203 
   204 lemma UN_equiv_class_inject:
   205   "equiv A r ==> f respects r ==>
   206     (\<Union>x \<in> X. f x) = (\<Union>y \<in> Y. f y) ==> X \<in> A//r ==> Y \<in> A//r
   207     ==> (!!x y. x \<in> A ==> y \<in> A ==> f x = f y ==> (x, y) \<in> r)
   208     ==> X = Y"
   209   apply (unfold quotient_def)
   210   apply clarify
   211   apply (rule equiv_class_eq)
   212    apply assumption
   213   apply (subgoal_tac "f x = f xa")
   214    apply blast
   215   apply (erule box_equals)
   216    apply (assumption | rule UN_equiv_class)+
   217   done
   218 
   219 
   220 subsection {* Defining binary operations upon equivalence classes *}
   221 
   222 text{*A congruence-preserving function of two arguments*}
   223 locale congruent2 =
   224   fixes r1 and r2 and f
   225   assumes congruent2:
   226     "(y1,z1) \<in> r1 ==> (y2,z2) \<in> r2 ==> f y1 y2 = f z1 z2"
   227 
   228 text{*Abbreviation for the common case where the relations are identical*}
   229 abbreviation
   230   RESPECTS2:: "['a => 'a => 'b, ('a * 'a) set] => bool"
   231     (infixr "respects2" 80) where
   232   "f respects2 r == congruent2 r r f"
   233 
   234 
   235 lemma congruent2_implies_congruent:
   236     "equiv A r1 ==> congruent2 r1 r2 f ==> a \<in> A ==> congruent r2 (f a)"
   237   by (unfold congruent_def congruent2_def equiv_def refl_on_def) blast
   238 
   239 lemma congruent2_implies_congruent_UN:
   240   "equiv A1 r1 ==> equiv A2 r2 ==> congruent2 r1 r2 f ==> a \<in> A2 ==>
   241     congruent r1 (\<lambda>x1. \<Union>x2 \<in> r2``{a}. f x1 x2)"
   242   apply (unfold congruent_def)
   243   apply clarify
   244   apply (rule equiv_type [THEN subsetD, THEN SigmaE2], assumption+)
   245   apply (simp add: UN_equiv_class congruent2_implies_congruent)
   246   apply (unfold congruent2_def equiv_def refl_on_def)
   247   apply (blast del: equalityI)
   248   done
   249 
   250 lemma UN_equiv_class2:
   251   "equiv A1 r1 ==> equiv A2 r2 ==> congruent2 r1 r2 f ==> a1 \<in> A1 ==> a2 \<in> A2
   252     ==> (\<Union>x1 \<in> r1``{a1}. \<Union>x2 \<in> r2``{a2}. f x1 x2) = f a1 a2"
   253   by (simp add: UN_equiv_class congruent2_implies_congruent
   254     congruent2_implies_congruent_UN)
   255 
   256 lemma UN_equiv_class_type2:
   257   "equiv A1 r1 ==> equiv A2 r2 ==> congruent2 r1 r2 f
   258     ==> X1 \<in> A1//r1 ==> X2 \<in> A2//r2
   259     ==> (!!x1 x2. x1 \<in> A1 ==> x2 \<in> A2 ==> f x1 x2 \<in> B)
   260     ==> (\<Union>x1 \<in> X1. \<Union>x2 \<in> X2. f x1 x2) \<in> B"
   261   apply (unfold quotient_def)
   262   apply clarify
   263   apply (blast intro: UN_equiv_class_type congruent2_implies_congruent_UN
   264     congruent2_implies_congruent quotientI)
   265   done
   266 
   267 lemma UN_UN_split_split_eq:
   268   "(\<Union>(x1, x2) \<in> X. \<Union>(y1, y2) \<in> Y. A x1 x2 y1 y2) =
   269     (\<Union>x \<in> X. \<Union>y \<in> Y. (\<lambda>(x1, x2). (\<lambda>(y1, y2). A x1 x2 y1 y2) y) x)"
   270   -- {* Allows a natural expression of binary operators, *}
   271   -- {* without explicit calls to @{text split} *}
   272   by auto
   273 
   274 lemma congruent2I:
   275   "equiv A1 r1 ==> equiv A2 r2
   276     ==> (!!y z w. w \<in> A2 ==> (y,z) \<in> r1 ==> f y w = f z w)
   277     ==> (!!y z w. w \<in> A1 ==> (y,z) \<in> r2 ==> f w y = f w z)
   278     ==> congruent2 r1 r2 f"
   279   -- {* Suggested by John Harrison -- the two subproofs may be *}
   280   -- {* \emph{much} simpler than the direct proof. *}
   281   apply (unfold congruent2_def equiv_def refl_on_def)
   282   apply clarify
   283   apply (blast intro: trans)
   284   done
   285 
   286 lemma congruent2_commuteI:
   287   assumes equivA: "equiv A r"
   288     and commute: "!!y z. y \<in> A ==> z \<in> A ==> f y z = f z y"
   289     and congt: "!!y z w. w \<in> A ==> (y,z) \<in> r ==> f w y = f w z"
   290   shows "f respects2 r"
   291   apply (rule congruent2I [OF equivA equivA])
   292    apply (rule commute [THEN trans])
   293      apply (rule_tac [3] commute [THEN trans, symmetric])
   294        apply (rule_tac [5] sym)
   295        apply (rule congt | assumption |
   296          erule equivA [THEN equiv_type, THEN subsetD, THEN SigmaE2])+
   297   done
   298 
   299 
   300 subsection {* Quotients and finiteness *}
   301 
   302 text {*Suggested by Florian Kammüller*}
   303 
   304 lemma finite_quotient: "finite A ==> r \<subseteq> A \<times> A ==> finite (A//r)"
   305   -- {* recall @{thm equiv_type} *}
   306   apply (rule finite_subset)
   307    apply (erule_tac [2] finite_Pow_iff [THEN iffD2])
   308   apply (unfold quotient_def)
   309   apply blast
   310   done
   311 
   312 lemma finite_equiv_class:
   313   "finite A ==> r \<subseteq> A \<times> A ==> X \<in> A//r ==> finite X"
   314   apply (unfold quotient_def)
   315   apply (rule finite_subset)
   316    prefer 2 apply assumption
   317   apply blast
   318   done
   319 
   320 lemma equiv_imp_dvd_card:
   321   "finite A ==> equiv A r ==> \<forall>X \<in> A//r. k dvd card X
   322     ==> k dvd card A"
   323   apply (rule Union_quotient [THEN subst [where P="\<lambda>A. k dvd card A"]])
   324    apply assumption
   325   apply (rule dvd_partition)
   326      prefer 3 apply (blast dest: quotient_disj)
   327     apply (simp_all add: Union_quotient equiv_type)
   328   done
   329 
   330 lemma card_quotient_disjoint:
   331  "\<lbrakk> finite A; inj_on (\<lambda>x. {x} // r) A \<rbrakk> \<Longrightarrow> card(A//r) = card A"
   332 apply(simp add:quotient_def)
   333 apply(subst card_UN_disjoint)
   334    apply assumption
   335   apply simp
   336  apply(fastsimp simp add:inj_on_def)
   337 apply simp
   338 done
   339 
   340 
   341 subsection {* Equivalence relations -- predicate version *}
   342 
   343 text {* Partial equivalences *}
   344 
   345 definition part_equivp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
   346   "part_equivp R \<longleftrightarrow> (\<exists>x. R x x) \<and> (\<forall>x y. R x y \<longleftrightarrow> R x x \<and> R y y \<and> R x = R y)"
   347     -- {* John-Harrison-style characterization *}
   348 
   349 lemma part_equivpI:
   350   "(\<exists>x. R x x) \<Longrightarrow> symp R \<Longrightarrow> transp R \<Longrightarrow> part_equivp R"
   351   by (auto simp add: part_equivp_def mem_def) (auto elim: sympE transpE)
   352 
   353 lemma part_equivpE:
   354   assumes "part_equivp R"
   355   obtains x where "R x x" and "symp R" and "transp R"
   356 proof -
   357   from assms have 1: "\<exists>x. R x x"
   358     and 2: "\<And>x y. R x y \<longleftrightarrow> R x x \<and> R y y \<and> R x = R y"
   359     by (unfold part_equivp_def) blast+
   360   from 1 obtain x where "R x x" ..
   361   moreover have "symp R"
   362   proof (rule sympI)
   363     fix x y
   364     assume "R x y"
   365     with 2 [of x y] show "R y x" by auto
   366   qed
   367   moreover have "transp R"
   368   proof (rule transpI)
   369     fix x y z
   370     assume "R x y" and "R y z"
   371     with 2 [of x y] 2 [of y z] show "R x z" by auto
   372   qed
   373   ultimately show thesis by (rule that)
   374 qed
   375 
   376 lemma part_equivp_refl_symp_transp:
   377   "part_equivp R \<longleftrightarrow> (\<exists>x. R x x) \<and> symp R \<and> transp R"
   378   by (auto intro: part_equivpI elim: part_equivpE)
   379 
   380 lemma part_equivp_symp:
   381   "part_equivp R \<Longrightarrow> R x y \<Longrightarrow> R y x"
   382   by (erule part_equivpE, erule sympE)
   383 
   384 lemma part_equivp_transp:
   385   "part_equivp R \<Longrightarrow> R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
   386   by (erule part_equivpE, erule transpE)
   387 
   388 lemma part_equivp_typedef:
   389   "part_equivp R \<Longrightarrow> \<exists>d. d \<in> (\<lambda>c. \<exists>x. R x x \<and> c = R x)"
   390   by (auto elim: part_equivpE simp add: mem_def)
   391 
   392 
   393 text {* Total equivalences *}
   394 
   395 definition equivp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
   396   "equivp R \<longleftrightarrow> (\<forall>x y. R x y = (R x = R y))" -- {* John-Harrison-style characterization *}
   397 
   398 lemma equivpI:
   399   "reflp R \<Longrightarrow> symp R \<Longrightarrow> transp R \<Longrightarrow> equivp R"
   400   by (auto elim: reflpE sympE transpE simp add: equivp_def mem_def)
   401 
   402 lemma equivpE:
   403   assumes "equivp R"
   404   obtains "reflp R" and "symp R" and "transp R"
   405   using assms by (auto intro!: that reflpI sympI transpI simp add: equivp_def)
   406 
   407 lemma equivp_implies_part_equivp:
   408   "equivp R \<Longrightarrow> part_equivp R"
   409   by (auto intro: part_equivpI elim: equivpE reflpE)
   410 
   411 lemma equivp_equiv:
   412   "equiv UNIV A \<longleftrightarrow> equivp (\<lambda>x y. (x, y) \<in> A)"
   413   by (auto intro: equivpI elim: equivpE simp add: equiv_def reflp_def symp_def transp_def)
   414 
   415 lemma equivp_reflp_symp_transp:
   416   shows "equivp R \<longleftrightarrow> reflp R \<and> symp R \<and> transp R"
   417   by (auto intro: equivpI elim: equivpE)
   418 
   419 lemma identity_equivp:
   420   "equivp (op =)"
   421   by (auto intro: equivpI reflpI sympI transpI)
   422 
   423 lemma equivp_reflp:
   424   "equivp R \<Longrightarrow> R x x"
   425   by (erule equivpE, erule reflpE)
   426 
   427 lemma equivp_symp:
   428   "equivp R \<Longrightarrow> R x y \<Longrightarrow> R y x"
   429   by (erule equivpE, erule sympE)
   430 
   431 lemma equivp_transp:
   432   "equivp R \<Longrightarrow> R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
   433   by (erule equivpE, erule transpE)
   434 
   435 end