src/HOL/Equiv_Relations.thy
 author haftmann Fri Jan 02 08:12:46 2009 +0100 (2009-01-02) changeset 29332 edc1e2a56398 parent 28562 4e74209f113e child 29655 ac31940cfb69 permissions -rw-r--r--
named code theorem for Fract_norm
     1 (*  ID:         $Id$

     2     Authors:    Lawrence C Paulson, Cambridge University Computer Laboratory

     3     Copyright   1996  University of Cambridge

     4 *)

     5

     6 header {* Equivalence Relations in Higher-Order Set Theory *}

     7

     8 theory Equiv_Relations

     9 imports Finite_Set Relation

    10 begin

    11

    12 subsection {* Equivalence relations *}

    13

    14 locale equiv =

    15   fixes A and r

    16   assumes refl: "refl A r"

    17     and sym: "sym r"

    18     and trans: "trans r"

    19

    20 text {*

    21   Suppes, Theorem 70: @{text r} is an equiv relation iff @{text "r\<inverse> O

    22   r = r"}.

    23

    24   First half: @{text "equiv A r ==> r\<inverse> O r = r"}.

    25 *}

    26

    27 lemma sym_trans_comp_subset:

    28     "sym r ==> trans r ==> r\<inverse> O r \<subseteq> r"

    29   by (unfold trans_def sym_def converse_def) blast

    30

    31 lemma refl_comp_subset: "refl A r ==> r \<subseteq> r\<inverse> O r"

    32   by (unfold refl_def) blast

    33

    34 lemma equiv_comp_eq: "equiv A r ==> r\<inverse> O r = r"

    35   apply (unfold equiv_def)

    36   apply clarify

    37   apply (rule equalityI)

    38    apply (iprover intro: sym_trans_comp_subset refl_comp_subset)+

    39   done

    40

    41 text {* Second half. *}

    42

    43 lemma comp_equivI:

    44     "r\<inverse> O r = r ==> Domain r = A ==> equiv A r"

    45   apply (unfold equiv_def refl_def sym_def trans_def)

    46   apply (erule equalityE)

    47   apply (subgoal_tac "\<forall>x y. (x, y) \<in> r --> (y, x) \<in> r")

    48    apply fast

    49   apply fast

    50   done

    51

    52

    53 subsection {* Equivalence classes *}

    54

    55 lemma equiv_class_subset:

    56   "equiv A r ==> (a, b) \<in> r ==> r{a} \<subseteq> r{b}"

    57   -- {* lemma for the next result *}

    58   by (unfold equiv_def trans_def sym_def) blast

    59

    60 theorem equiv_class_eq: "equiv A r ==> (a, b) \<in> r ==> r{a} = r{b}"

    61   apply (assumption | rule equalityI equiv_class_subset)+

    62   apply (unfold equiv_def sym_def)

    63   apply blast

    64   done

    65

    66 lemma equiv_class_self: "equiv A r ==> a \<in> A ==> a \<in> r{a}"

    67   by (unfold equiv_def refl_def) blast

    68

    69 lemma subset_equiv_class:

    70     "equiv A r ==> r{b} \<subseteq> r{a} ==> b \<in> A ==> (a,b) \<in> r"

    71   -- {* lemma for the next result *}

    72   by (unfold equiv_def refl_def) blast

    73

    74 lemma eq_equiv_class:

    75     "r{a} = r{b} ==> equiv A r ==> b \<in> A ==> (a, b) \<in> r"

    76   by (iprover intro: equalityD2 subset_equiv_class)

    77

    78 lemma equiv_class_nondisjoint:

    79     "equiv A r ==> x \<in> (r{a} \<inter> r{b}) ==> (a, b) \<in> r"

    80   by (unfold equiv_def trans_def sym_def) blast

    81

    82 lemma equiv_type: "equiv A r ==> r \<subseteq> A \<times> A"

    83   by (unfold equiv_def refl_def) blast

    84

    85 theorem equiv_class_eq_iff:

    86   "equiv A r ==> ((x, y) \<in> r) = (r{x} = r{y} & x \<in> A & y \<in> A)"

    87   by (blast intro!: equiv_class_eq dest: eq_equiv_class equiv_type)

    88

    89 theorem eq_equiv_class_iff:

    90   "equiv A r ==> x \<in> A ==> y \<in> A ==> (r{x} = r{y}) = ((x, y) \<in> r)"

    91   by (blast intro!: equiv_class_eq dest: eq_equiv_class equiv_type)

    92

    93

    94 subsection {* Quotients *}

    95

    96 definition quotient :: "'a set \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> 'a set set"  (infixl "'/'/" 90) where

    97   [code del]: "A//r = (\<Union>x \<in> A. {r{x}})"  -- {* set of equiv classes *}

    98

    99 lemma quotientI: "x \<in> A ==> r{x} \<in> A//r"

   100   by (unfold quotient_def) blast

   101

   102 lemma quotientE:

   103   "X \<in> A//r ==> (!!x. X = r{x} ==> x \<in> A ==> P) ==> P"

   104   by (unfold quotient_def) blast

   105

   106 lemma Union_quotient: "equiv A r ==> Union (A//r) = A"

   107   by (unfold equiv_def refl_def quotient_def) blast

   108

   109 lemma quotient_disj:

   110   "equiv A r ==> X \<in> A//r ==> Y \<in> A//r ==> X = Y | (X \<inter> Y = {})"

   111   apply (unfold quotient_def)

   112   apply clarify

   113   apply (rule equiv_class_eq)

   114    apply assumption

   115   apply (unfold equiv_def trans_def sym_def)

   116   apply blast

   117   done

   118

   119 lemma quotient_eqI:

   120   "[|equiv A r; X \<in> A//r; Y \<in> A//r; x \<in> X; y \<in> Y; (x,y) \<in> r|] ==> X = Y"

   121   apply (clarify elim!: quotientE)

   122   apply (rule equiv_class_eq, assumption)

   123   apply (unfold equiv_def sym_def trans_def, blast)

   124   done

   125

   126 lemma quotient_eq_iff:

   127   "[|equiv A r; X \<in> A//r; Y \<in> A//r; x \<in> X; y \<in> Y|] ==> (X = Y) = ((x,y) \<in> r)"

   128   apply (rule iffI)

   129    prefer 2 apply (blast del: equalityI intro: quotient_eqI)

   130   apply (clarify elim!: quotientE)

   131   apply (unfold equiv_def sym_def trans_def, blast)

   132   done

   133

   134 lemma eq_equiv_class_iff2:

   135   "\<lbrakk> equiv A r; x \<in> A; y \<in> A \<rbrakk> \<Longrightarrow> ({x}//r = {y}//r) = ((x,y) : r)"

   136 by(simp add:quotient_def eq_equiv_class_iff)

   137

   138

   139 lemma quotient_empty [simp]: "{}//r = {}"

   140 by(simp add: quotient_def)

   141

   142 lemma quotient_is_empty [iff]: "(A//r = {}) = (A = {})"

   143 by(simp add: quotient_def)

   144

   145 lemma quotient_is_empty2 [iff]: "({} = A//r) = (A = {})"

   146 by(simp add: quotient_def)

   147

   148

   149 lemma singleton_quotient: "{x}//r = {r  {x}}"

   150 by(simp add:quotient_def)

   151

   152 lemma quotient_diff1:

   153   "\<lbrakk> inj_on (%a. {a}//r) A; a \<in> A \<rbrakk> \<Longrightarrow> (A - {a})//r = A//r - {a}//r"

   154 apply(simp add:quotient_def inj_on_def)

   155 apply blast

   156 done

   157

   158 subsection {* Defining unary operations upon equivalence classes *}

   159

   160 text{*A congruence-preserving function*}

   161 locale congruent =

   162   fixes r and f

   163   assumes congruent: "(y,z) \<in> r ==> f y = f z"

   164

   165 abbreviation

   166   RESPECTS :: "('a => 'b) => ('a * 'a) set => bool"

   167     (infixr "respects" 80) where

   168   "f respects r == congruent r f"

   169

   170

   171 lemma UN_constant_eq: "a \<in> A ==> \<forall>y \<in> A. f y = c ==> (\<Union>y \<in> A. f(y))=c"

   172   -- {* lemma required to prove @{text UN_equiv_class} *}

   173   by auto

   174

   175 lemma UN_equiv_class:

   176   "equiv A r ==> f respects r ==> a \<in> A

   177     ==> (\<Union>x \<in> r{a}. f x) = f a"

   178   -- {* Conversion rule *}

   179   apply (rule equiv_class_self [THEN UN_constant_eq], assumption+)

   180   apply (unfold equiv_def congruent_def sym_def)

   181   apply (blast del: equalityI)

   182   done

   183

   184 lemma UN_equiv_class_type:

   185   "equiv A r ==> f respects r ==> X \<in> A//r ==>

   186     (!!x. x \<in> A ==> f x \<in> B) ==> (\<Union>x \<in> X. f x) \<in> B"

   187   apply (unfold quotient_def)

   188   apply clarify

   189   apply (subst UN_equiv_class)

   190      apply auto

   191   done

   192

   193 text {*

   194   Sufficient conditions for injectiveness.  Could weaken premises!

   195   major premise could be an inclusion; bcong could be @{text "!!y. y \<in>

   196   A ==> f y \<in> B"}.

   197 *}

   198

   199 lemma UN_equiv_class_inject:

   200   "equiv A r ==> f respects r ==>

   201     (\<Union>x \<in> X. f x) = (\<Union>y \<in> Y. f y) ==> X \<in> A//r ==> Y \<in> A//r

   202     ==> (!!x y. x \<in> A ==> y \<in> A ==> f x = f y ==> (x, y) \<in> r)

   203     ==> X = Y"

   204   apply (unfold quotient_def)

   205   apply clarify

   206   apply (rule equiv_class_eq)

   207    apply assumption

   208   apply (subgoal_tac "f x = f xa")

   209    apply blast

   210   apply (erule box_equals)

   211    apply (assumption | rule UN_equiv_class)+

   212   done

   213

   214

   215 subsection {* Defining binary operations upon equivalence classes *}

   216

   217 text{*A congruence-preserving function of two arguments*}

   218 locale congruent2 =

   219   fixes r1 and r2 and f

   220   assumes congruent2:

   221     "(y1,z1) \<in> r1 ==> (y2,z2) \<in> r2 ==> f y1 y2 = f z1 z2"

   222

   223 text{*Abbreviation for the common case where the relations are identical*}

   224 abbreviation

   225   RESPECTS2:: "['a => 'a => 'b, ('a * 'a) set] => bool"

   226     (infixr "respects2" 80) where

   227   "f respects2 r == congruent2 r r f"

   228

   229

   230 lemma congruent2_implies_congruent:

   231     "equiv A r1 ==> congruent2 r1 r2 f ==> a \<in> A ==> congruent r2 (f a)"

   232   by (unfold congruent_def congruent2_def equiv_def refl_def) blast

   233

   234 lemma congruent2_implies_congruent_UN:

   235   "equiv A1 r1 ==> equiv A2 r2 ==> congruent2 r1 r2 f ==> a \<in> A2 ==>

   236     congruent r1 (\<lambda>x1. \<Union>x2 \<in> r2{a}. f x1 x2)"

   237   apply (unfold congruent_def)

   238   apply clarify

   239   apply (rule equiv_type [THEN subsetD, THEN SigmaE2], assumption+)

   240   apply (simp add: UN_equiv_class congruent2_implies_congruent)

   241   apply (unfold congruent2_def equiv_def refl_def)

   242   apply (blast del: equalityI)

   243   done

   244

   245 lemma UN_equiv_class2:

   246   "equiv A1 r1 ==> equiv A2 r2 ==> congruent2 r1 r2 f ==> a1 \<in> A1 ==> a2 \<in> A2

   247     ==> (\<Union>x1 \<in> r1{a1}. \<Union>x2 \<in> r2{a2}. f x1 x2) = f a1 a2"

   248   by (simp add: UN_equiv_class congruent2_implies_congruent

   249     congruent2_implies_congruent_UN)

   250

   251 lemma UN_equiv_class_type2:

   252   "equiv A1 r1 ==> equiv A2 r2 ==> congruent2 r1 r2 f

   253     ==> X1 \<in> A1//r1 ==> X2 \<in> A2//r2

   254     ==> (!!x1 x2. x1 \<in> A1 ==> x2 \<in> A2 ==> f x1 x2 \<in> B)

   255     ==> (\<Union>x1 \<in> X1. \<Union>x2 \<in> X2. f x1 x2) \<in> B"

   256   apply (unfold quotient_def)

   257   apply clarify

   258   apply (blast intro: UN_equiv_class_type congruent2_implies_congruent_UN

   259     congruent2_implies_congruent quotientI)

   260   done

   261

   262 lemma UN_UN_split_split_eq:

   263   "(\<Union>(x1, x2) \<in> X. \<Union>(y1, y2) \<in> Y. A x1 x2 y1 y2) =

   264     (\<Union>x \<in> X. \<Union>y \<in> Y. (\<lambda>(x1, x2). (\<lambda>(y1, y2). A x1 x2 y1 y2) y) x)"

   265   -- {* Allows a natural expression of binary operators, *}

   266   -- {* without explicit calls to @{text split} *}

   267   by auto

   268

   269 lemma congruent2I:

   270   "equiv A1 r1 ==> equiv A2 r2

   271     ==> (!!y z w. w \<in> A2 ==> (y,z) \<in> r1 ==> f y w = f z w)

   272     ==> (!!y z w. w \<in> A1 ==> (y,z) \<in> r2 ==> f w y = f w z)

   273     ==> congruent2 r1 r2 f"

   274   -- {* Suggested by John Harrison -- the two subproofs may be *}

   275   -- {* \emph{much} simpler than the direct proof. *}

   276   apply (unfold congruent2_def equiv_def refl_def)

   277   apply clarify

   278   apply (blast intro: trans)

   279   done

   280

   281 lemma congruent2_commuteI:

   282   assumes equivA: "equiv A r"

   283     and commute: "!!y z. y \<in> A ==> z \<in> A ==> f y z = f z y"

   284     and congt: "!!y z w. w \<in> A ==> (y,z) \<in> r ==> f w y = f w z"

   285   shows "f respects2 r"

   286   apply (rule congruent2I [OF equivA equivA])

   287    apply (rule commute [THEN trans])

   288      apply (rule_tac  commute [THEN trans, symmetric])

   289        apply (rule_tac  sym)

   290        apply (rule congt | assumption |

   291          erule equivA [THEN equiv_type, THEN subsetD, THEN SigmaE2])+

   292   done

   293

   294

   295 subsection {* Quotients and finiteness *}

   296

   297 text {*Suggested by Florian Kamm�ller*}

   298

   299 lemma finite_quotient: "finite A ==> r \<subseteq> A \<times> A ==> finite (A//r)"

   300   -- {* recall @{thm equiv_type} *}

   301   apply (rule finite_subset)

   302    apply (erule_tac  finite_Pow_iff [THEN iffD2])

   303   apply (unfold quotient_def)

   304   apply blast

   305   done

   306

   307 lemma finite_equiv_class:

   308   "finite A ==> r \<subseteq> A \<times> A ==> X \<in> A//r ==> finite X"

   309   apply (unfold quotient_def)

   310   apply (rule finite_subset)

   311    prefer 2 apply assumption

   312   apply blast

   313   done

   314

   315 lemma equiv_imp_dvd_card:

   316   "finite A ==> equiv A r ==> \<forall>X \<in> A//r. k dvd card X

   317     ==> k dvd card A"

   318   apply (rule Union_quotient [THEN subst [where P="\<lambda>A. k dvd card A"]])

   319    apply assumption

   320   apply (rule dvd_partition)

   321      prefer 3 apply (blast dest: quotient_disj)

   322     apply (simp_all add: Union_quotient equiv_type)

   323   done

   324

   325 lemma card_quotient_disjoint:

   326  "\<lbrakk> finite A; inj_on (\<lambda>x. {x} // r) A \<rbrakk> \<Longrightarrow> card(A//r) = card A"

   327 apply(simp add:quotient_def)

   328 apply(subst card_UN_disjoint)

   329    apply assumption

   330   apply simp

   331  apply(fastsimp simp add:inj_on_def)

   332 apply (simp add:setsum_constant)

   333 done

   334

   335 end