src/HOL/Library/Quotient.thy
author wenzelm
Sun Nov 12 14:50:26 2000 +0100 (2000-11-12)
changeset 10459 df3cd3e76046
parent 10437 7528f9e30ca4
child 10473 4f15b844fea6
permissions -rw-r--r--
quot_cond_definition;
     1 (*  Title:      HOL/Library/Quotient.thy
     2     ID:         $Id$
     3     Author:     Gertrud Bauer and Markus Wenzel, TU Muenchen
     4 *)
     5 
     6 header {*
     7   \title{Quotients}
     8   \author{Gertrud Bauer and Markus Wenzel}
     9 *}
    10 
    11 theory Quotient = Main:
    12 
    13 text {*
    14  We introduce the notion of quotient types over equivalence relations
    15  via axiomatic type classes.
    16 *}
    17 
    18 subsection {* Equivalence relations and quotient types *}
    19 
    20 text {*
    21  \medskip Type class @{text equiv} models equivalence relations @{text
    22  "\<sim> :: 'a => 'a => bool"}.
    23 *}
    24 
    25 axclass eqv < "term"
    26 consts
    27   eqv :: "('a::eqv) => 'a => bool"    (infixl "\<sim>" 50)
    28 
    29 axclass equiv < eqv
    30   equiv_refl [intro]: "x \<sim> x"
    31   equiv_trans [trans]: "x \<sim> y ==> y \<sim> z ==> x \<sim> z"
    32   equiv_sym [elim?]: "x \<sim> y ==> y \<sim> x"
    33 
    34 text {*
    35  \medskip The quotient type @{text "'a quot"} consists of all
    36  \emph{equivalence classes} over elements of the base type @{typ 'a}.
    37 *}
    38 
    39 typedef 'a quot = "{{x. a \<sim> x} | a::'a::eqv. True}"
    40   by blast
    41 
    42 lemma quotI [intro]: "{x. a \<sim> x} \<in> quot"
    43   by (unfold quot_def) blast
    44 
    45 lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"
    46   by (unfold quot_def) blast
    47 
    48 text {*
    49  \medskip Abstracted equivalence classes are the canonical
    50  representation of elements of a quotient type.
    51 *}
    52 
    53 constdefs
    54   equivalence_class :: "'a::equiv => 'a quot"    ("\<lfloor>_\<rfloor>")
    55   "\<lfloor>a\<rfloor> == Abs_quot {x. a \<sim> x}"
    56 
    57 theorem quot_exhaust: "\<exists>a. A = \<lfloor>a\<rfloor>"
    58 proof (cases A)
    59   fix R assume R: "A = Abs_quot R"
    60   assume "R \<in> quot" hence "\<exists>a. R = {x. a \<sim> x}" by blast
    61   with R have "\<exists>a. A = Abs_quot {x. a \<sim> x}" by blast
    62   thus ?thesis by (unfold equivalence_class_def)
    63 qed
    64 
    65 lemma quot_cases [cases type: quot]: "(!!a. A = \<lfloor>a\<rfloor> ==> C) ==> C"
    66   by (insert quot_exhaust) blast
    67 
    68 
    69 subsection {* Equality on quotients *}
    70 
    71 text {*
    72  Equality of canonical quotient elements coincides with the original
    73  relation.
    74 *}
    75 
    76 theorem equivalence_class_iff [iff?]: "(\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> b)"
    77 proof
    78   assume eq: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
    79   show "a \<sim> b"
    80   proof -
    81     from eq have "{x. a \<sim> x} = {x. b \<sim> x}"
    82       by (simp only: equivalence_class_def Abs_quot_inject quotI)
    83     moreover have "a \<sim> a" ..
    84     ultimately have "a \<in> {x. b \<sim> x}" by blast
    85     hence "b \<sim> a" by blast
    86     thus ?thesis ..
    87   qed
    88 next
    89   assume ab: "a \<sim> b"
    90   show "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
    91   proof -
    92     have "{x. a \<sim> x} = {x. b \<sim> x}"
    93     proof (rule Collect_cong)
    94       fix x show "(a \<sim> x) = (b \<sim> x)"
    95       proof
    96         from ab have "b \<sim> a" ..
    97         also assume "a \<sim> x"
    98         finally show "b \<sim> x" .
    99       next
   100         note ab
   101         also assume "b \<sim> x"
   102         finally show "a \<sim> x" .
   103       qed
   104     qed
   105     thus ?thesis by (simp only: equivalence_class_def)
   106   qed
   107 qed
   108 
   109 
   110 subsection {* Picking representing elements *}
   111 
   112 constdefs
   113   pick :: "'a::equiv quot => 'a"
   114   "pick A == SOME a. A = \<lfloor>a\<rfloor>"
   115 
   116 theorem pick_equiv [intro]: "pick \<lfloor>a\<rfloor> \<sim> a"
   117 proof (unfold pick_def)
   118   show "(SOME x. \<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>) \<sim> a"
   119   proof (rule someI2)
   120     show "\<lfloor>a\<rfloor> = \<lfloor>a\<rfloor>" ..
   121     fix x assume "\<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>"
   122     hence "a \<sim> x" .. thus "x \<sim> a" ..
   123   qed
   124 qed
   125 
   126 theorem pick_inverse: "\<lfloor>pick A\<rfloor> = A"
   127 proof (cases A)
   128   fix a assume a: "A = \<lfloor>a\<rfloor>"
   129   hence "pick A \<sim> a" by (simp only: pick_equiv)
   130   hence "\<lfloor>pick A\<rfloor> = \<lfloor>a\<rfloor>" ..
   131   with a show ?thesis by simp
   132 qed
   133 
   134 text {*
   135  \medskip The following rules support canonical function definitions
   136  on quotient types.
   137 *}
   138 
   139 theorem quot_cond_definition1:
   140   "(!!X. f X == \<lfloor>g (pick X)\<rfloor>) ==>
   141     (!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x \<sim> g x') ==>
   142     (!!x x'. x \<sim> x' ==> P x = P x') ==>
   143   P a ==> f \<lfloor>a\<rfloor> = \<lfloor>g a\<rfloor>"
   144 proof -
   145   assume cong_g: "!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x \<sim> g x'"
   146   assume cong_P: "!!x x'. x \<sim> x' ==> P x = P x'"
   147   assume P: "P a"
   148   assume "!!X. f X == \<lfloor>g (pick X)\<rfloor>"
   149   hence "f \<lfloor>a\<rfloor> = \<lfloor>g (pick \<lfloor>a\<rfloor>)\<rfloor>" by (simp only:)
   150   also have "\<dots> = \<lfloor>g a\<rfloor>"
   151   proof
   152     show "g (pick \<lfloor>a\<rfloor>) \<sim> g a"
   153     proof (rule cong_g)
   154       show "pick \<lfloor>a\<rfloor> \<sim> a" ..
   155       hence "P (pick \<lfloor>a\<rfloor>) = P a" by (rule cong_P)
   156       also show "P a" .
   157       finally show "P (pick \<lfloor>a\<rfloor>)" .
   158     qed
   159   qed
   160   finally show ?thesis .
   161 qed
   162 
   163 theorem quot_definition1:
   164   "(!!X. f X == \<lfloor>g (pick X)\<rfloor>) ==>
   165     (!!x x'. x \<sim> x' ==> g x \<sim> g x') ==>
   166     f \<lfloor>a\<rfloor> = \<lfloor>g a\<rfloor>"
   167 proof -
   168   case antecedent from this refl TrueI
   169   show ?thesis by (rule quot_cond_definition1)
   170 qed
   171 
   172 theorem quot_cond_definition2:
   173   "(!!X Y. f X Y == \<lfloor>g (pick X) (pick Y)\<rfloor>) ==>
   174     (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y' ==> g x y \<sim> g x' y') ==>
   175     (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y') ==>
   176     P a b ==> f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = \<lfloor>g a b\<rfloor>"
   177 proof -
   178   assume cong_g: "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y' ==> g x y \<sim> g x' y'"
   179   assume cong_P: "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y'"
   180   assume P: "P a b"
   181   assume "!!X Y. f X Y == \<lfloor>g (pick X) (pick Y)\<rfloor>"
   182   hence "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = \<lfloor>g (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)\<rfloor>" by (simp only:)
   183   also have "\<dots> = \<lfloor>g a b\<rfloor>"
   184   proof
   185     show "g (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>) \<sim> g a b"
   186     proof (rule cong_g)
   187       show "pick \<lfloor>a\<rfloor> \<sim> a" ..
   188       moreover show "pick \<lfloor>b\<rfloor> \<sim> b" ..
   189       ultimately have "P (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>) = P a b" by (rule cong_P)
   190       also show "P a b" .
   191       finally show "P (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)" .
   192     qed
   193   qed
   194   finally show ?thesis .
   195 qed
   196 
   197 theorem quot_definition2:
   198   "(!!X Y. f X Y == \<lfloor>g (pick X) (pick Y)\<rfloor>) ==>
   199     (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y \<sim> g x' y') ==>
   200     f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = \<lfloor>g a b\<rfloor>"
   201 proof -
   202   case antecedent from this refl TrueI
   203   show ?thesis by (rule quot_cond_definition2)
   204 qed
   205 
   206 text {*
   207  \medskip HOL's collection of overloaded standard operations is lifted
   208  to quotient types in the canonical manner.
   209 *}
   210 
   211 instance quot :: (zero) zero ..
   212 instance quot :: (plus) plus ..
   213 instance quot :: (minus) minus ..
   214 instance quot :: (times) times ..
   215 instance quot :: (inverse) inverse ..
   216 instance quot :: (power) power ..
   217 instance quot :: (number) number ..
   218 instance quot :: (ord) ord ..
   219 
   220 defs (overloaded)
   221   zero_quot_def: "0 == \<lfloor>0\<rfloor>"
   222   add_quot_def: "X + Y == \<lfloor>pick X + pick Y\<rfloor>"
   223   diff_quot_def: "X - Y == \<lfloor>pick X - pick Y\<rfloor>"
   224   minus_quot_def: "- X == \<lfloor>- pick X\<rfloor>"
   225   abs_quot_def: "abs X == \<lfloor>abs (pick X)\<rfloor>"
   226   mult_quot_def: "X * Y == \<lfloor>pick X * pick Y\<rfloor>"
   227   inverse_quot_def: "inverse X == \<lfloor>inverse (pick X)\<rfloor>"
   228   divide_quot_def: "X / Y == \<lfloor>pick X / pick Y\<rfloor>"
   229   power_quot_def: "X^n == \<lfloor>(pick X)^n\<rfloor>"
   230   number_of_quot_def: "number_of b == \<lfloor>number_of b\<rfloor>"
   231   le_quot_def: "X \<le> Y == pick X \<le> pick Y"
   232   less_quot_def: "X < Y == pick X < pick Y"
   233 
   234 end