src/HOL/Library/Quotient.thy
 author wenzelm Fri Dec 15 17:59:45 2000 +0100 (2000-12-15) changeset 10681 ec76e17f73c5 parent 10551 ec9fab41b3a0 child 11099 b301d1f72552 permissions -rw-r--r--
GPLed;
     1 (*  Title:      HOL/Library/Quotient.thy

     2     ID:         $Id$

     3     Author:     Markus Wenzel, TU Muenchen

     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)

     5 *)

     6

     7 header {*

     8   \title{Quotient types}

     9   \author{Markus Wenzel}

    10 *}

    11

    12 theory Quotient = Main:

    13

    14 text {*

    15  We introduce the notion of quotient types over equivalence relations

    16  via axiomatic type classes.

    17 *}

    18

    19 subsection {* Equivalence relations and quotient types *}

    20

    21 text {*

    22  \medskip Type class @{text equiv} models equivalence relations @{text

    23  "\<sim> :: 'a => 'a => bool"}.

    24 *}

    25

    26 axclass eqv < "term"

    27 consts

    28   eqv :: "('a::eqv) => 'a => bool"    (infixl "\<sim>" 50)

    29

    30 axclass equiv < eqv

    31   equiv_refl [intro]: "x \<sim> x"

    32   equiv_trans [trans]: "x \<sim> y ==> y \<sim> z ==> x \<sim> z"

    33   equiv_sym [elim?]: "x \<sim> y ==> y \<sim> x"

    34

    35 lemma not_equiv_sym [elim?]: "\<not> (x \<sim> y) ==> \<not> (y \<sim> (x::'a::equiv))"

    36 proof -

    37   assume "\<not> (x \<sim> y)" thus "\<not> (y \<sim> x)"

    38     by (rule contrapos_nn) (rule equiv_sym)

    39 qed

    40

    41 lemma not_equiv_trans1 [trans]: "\<not> (x \<sim> y) ==> y \<sim> z ==> \<not> (x \<sim> (z::'a::equiv))"

    42 proof -

    43   assume "\<not> (x \<sim> y)" and yz: "y \<sim> z"

    44   show "\<not> (x \<sim> z)"

    45   proof

    46     assume "x \<sim> z"

    47     also from yz have "z \<sim> y" ..

    48     finally have "x \<sim> y" .

    49     thus False by contradiction

    50   qed

    51 qed

    52

    53 lemma not_equiv_trans2 [trans]: "x \<sim> y ==> \<not> (y \<sim> z) ==> \<not> (x \<sim> (z::'a::equiv))"

    54 proof -

    55   assume "\<not> (y \<sim> z)" hence "\<not> (z \<sim> y)" ..

    56   also assume "x \<sim> y" hence "y \<sim> x" ..

    57   finally have "\<not> (z \<sim> x)" . thus "(\<not> x \<sim> z)" ..

    58 qed

    59

    60 text {*

    61  \medskip The quotient type @{text "'a quot"} consists of all

    62  \emph{equivalence classes} over elements of the base type @{typ 'a}.

    63 *}

    64

    65 typedef 'a quot = "{{x. a \<sim> x} | a::'a::eqv. True}"

    66   by blast

    67

    68 lemma quotI [intro]: "{x. a \<sim> x} \<in> quot"

    69   by (unfold quot_def) blast

    70

    71 lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"

    72   by (unfold quot_def) blast

    73

    74 text {*

    75  \medskip Abstracted equivalence classes are the canonical

    76  representation of elements of a quotient type.

    77 *}

    78

    79 constdefs

    80   class :: "'a::equiv => 'a quot"    ("\<lfloor>_\<rfloor>")

    81   "\<lfloor>a\<rfloor> == Abs_quot {x. a \<sim> x}"

    82

    83 theorem quot_exhaust: "\<exists>a. A = \<lfloor>a\<rfloor>"

    84 proof (cases A)

    85   fix R assume R: "A = Abs_quot R"

    86   assume "R \<in> quot" hence "\<exists>a. R = {x. a \<sim> x}" by blast

    87   with R have "\<exists>a. A = Abs_quot {x. a \<sim> x}" by blast

    88   thus ?thesis by (unfold class_def)

    89 qed

    90

    91 lemma quot_cases [cases type: quot]: "(!!a. A = \<lfloor>a\<rfloor> ==> C) ==> C"

    92   by (insert quot_exhaust) blast

    93

    94

    95 subsection {* Equality on quotients *}

    96

    97 text {*

    98  Equality of canonical quotient elements coincides with the original

    99  relation.

   100 *}

   101

   102 theorem quot_equality: "(\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> b)"

   103 proof

   104   assume eq: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"

   105   show "a \<sim> b"

   106   proof -

   107     from eq have "{x. a \<sim> x} = {x. b \<sim> x}"

   108       by (simp only: class_def Abs_quot_inject quotI)

   109     moreover have "a \<sim> a" ..

   110     ultimately have "a \<in> {x. b \<sim> x}" by blast

   111     hence "b \<sim> a" by blast

   112     thus ?thesis ..

   113   qed

   114 next

   115   assume ab: "a \<sim> b"

   116   show "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"

   117   proof -

   118     have "{x. a \<sim> x} = {x. b \<sim> x}"

   119     proof (rule Collect_cong)

   120       fix x show "(a \<sim> x) = (b \<sim> x)"

   121       proof

   122         from ab have "b \<sim> a" ..

   123         also assume "a \<sim> x"

   124         finally show "b \<sim> x" .

   125       next

   126         note ab

   127         also assume "b \<sim> x"

   128         finally show "a \<sim> x" .

   129       qed

   130     qed

   131     thus ?thesis by (simp only: class_def)

   132   qed

   133 qed

   134

   135 lemma quot_equalI [intro?]: "a \<sim> b ==> \<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"

   136   by (simp only: quot_equality)

   137

   138 lemma quot_equalD [dest?]: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor> ==> a \<sim> b"

   139   by (simp only: quot_equality)

   140

   141 lemma quot_not_equalI [intro?]: "\<not> (a \<sim> b) ==> \<lfloor>a\<rfloor> \<noteq> \<lfloor>b\<rfloor>"

   142   by (simp add: quot_equality)

   143

   144 lemma quot_not_equalD [dest?]: "\<lfloor>a\<rfloor> \<noteq> \<lfloor>b\<rfloor> ==> \<not> (a \<sim> b)"

   145   by (simp add: quot_equality)

   146

   147

   148 subsection {* Picking representing elements *}

   149

   150 constdefs

   151   pick :: "'a::equiv quot => 'a"

   152   "pick A == SOME a. A = \<lfloor>a\<rfloor>"

   153

   154 theorem pick_equiv [intro]: "pick \<lfloor>a\<rfloor> \<sim> a"

   155 proof (unfold pick_def)

   156   show "(SOME x. \<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>) \<sim> a"

   157   proof (rule someI2)

   158     show "\<lfloor>a\<rfloor> = \<lfloor>a\<rfloor>" ..

   159     fix x assume "\<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>"

   160     hence "a \<sim> x" .. thus "x \<sim> a" ..

   161   qed

   162 qed

   163

   164 theorem pick_inverse [intro]: "\<lfloor>pick A\<rfloor> = A"

   165 proof (cases A)

   166   fix a assume a: "A = \<lfloor>a\<rfloor>"

   167   hence "pick A \<sim> a" by (simp only: pick_equiv)

   168   hence "\<lfloor>pick A\<rfloor> = \<lfloor>a\<rfloor>" ..

   169   with a show ?thesis by simp

   170 qed

   171

   172 text {*

   173  \medskip The following rules support canonical function definitions

   174  on quotient types (with up to two arguments).  Note that the

   175  stripped-down version without additional conditions is sufficient

   176  most of the time.

   177 *}

   178

   179 theorem quot_cond_function:

   180   "(!!X Y. P X Y ==> f X Y == g (pick X) (pick Y)) ==>

   181     (!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor>

   182       ==> P \<lfloor>x\<rfloor> \<lfloor>y\<rfloor> ==> P \<lfloor>x'\<rfloor> \<lfloor>y'\<rfloor> ==> g x y = g x' y') ==>

   183     P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> ==> f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"

   184   (is "PROP ?eq ==> PROP ?cong ==> _ ==> _")

   185 proof -

   186   assume cong: "PROP ?cong"

   187   assume "PROP ?eq" and "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>"

   188   hence "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)" by (simp only:)

   189   also have "... = g a b"

   190   proof (rule cong)

   191     show "\<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> = \<lfloor>a\<rfloor>" ..

   192     moreover

   193     show "\<lfloor>pick \<lfloor>b\<rfloor>\<rfloor> = \<lfloor>b\<rfloor>" ..

   194     moreover

   195     show "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>" .

   196     ultimately show "P \<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> \<lfloor>pick \<lfloor>b\<rfloor>\<rfloor>" by (simp only:)

   197   qed

   198   finally show ?thesis .

   199 qed

   200

   201 theorem quot_function:

   202   "(!!X Y. f X Y == g (pick X) (pick Y)) ==>

   203     (!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor> ==> g x y = g x' y') ==>

   204     f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"

   205 proof -

   206   case antecedent from this TrueI

   207   show ?thesis by (rule quot_cond_function)

   208 qed

   209

   210 theorem quot_function':

   211   "(!!X Y. f X Y == g (pick X) (pick Y)) ==>

   212     (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y = g x' y') ==>

   213     f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"

   214   by  (rule quot_function) (simp only: quot_equality)+

   215

   216 end