src/HOL/Library/Quotient.thy
author wenzelm
Thu Nov 30 20:05:34 2000 +0100 (2000-11-30)
changeset 10551 ec9fab41b3a0
parent 10505 b89e6cc963e3
child 10681 ec76e17f73c5
permissions -rw-r--r--
renamed "equivalence_class" to "class";
wenzelm@10250
     1
(*  Title:      HOL/Library/Quotient.thy
wenzelm@10250
     2
    ID:         $Id$
wenzelm@10483
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@10250
     4
*)
wenzelm@10250
     5
wenzelm@10250
     6
header {*
wenzelm@10473
     7
  \title{Quotient types}
wenzelm@10483
     8
  \author{Markus Wenzel}
wenzelm@10250
     9
*}
wenzelm@10250
    10
wenzelm@10250
    11
theory Quotient = Main:
wenzelm@10250
    12
wenzelm@10250
    13
text {*
wenzelm@10285
    14
 We introduce the notion of quotient types over equivalence relations
wenzelm@10285
    15
 via axiomatic type classes.
wenzelm@10250
    16
*}
wenzelm@10250
    17
wenzelm@10285
    18
subsection {* Equivalence relations and quotient types *}
wenzelm@10250
    19
wenzelm@10250
    20
text {*
wenzelm@10390
    21
 \medskip Type class @{text equiv} models equivalence relations @{text
wenzelm@10390
    22
 "\<sim> :: 'a => 'a => bool"}.
wenzelm@10250
    23
*}
wenzelm@10250
    24
wenzelm@10285
    25
axclass eqv < "term"
wenzelm@10285
    26
consts
wenzelm@10285
    27
  eqv :: "('a::eqv) => 'a => bool"    (infixl "\<sim>" 50)
wenzelm@10250
    28
wenzelm@10285
    29
axclass equiv < eqv
wenzelm@10333
    30
  equiv_refl [intro]: "x \<sim> x"
wenzelm@10333
    31
  equiv_trans [trans]: "x \<sim> y ==> y \<sim> z ==> x \<sim> z"
wenzelm@10333
    32
  equiv_sym [elim?]: "x \<sim> y ==> y \<sim> x"
wenzelm@10250
    33
wenzelm@10477
    34
lemma not_equiv_sym [elim?]: "\<not> (x \<sim> y) ==> \<not> (y \<sim> (x::'a::equiv))"
wenzelm@10477
    35
proof -
wenzelm@10477
    36
  assume "\<not> (x \<sim> y)" thus "\<not> (y \<sim> x)"
wenzelm@10477
    37
    by (rule contrapos_nn) (rule equiv_sym)
wenzelm@10477
    38
qed
wenzelm@10477
    39
wenzelm@10477
    40
lemma not_equiv_trans1 [trans]: "\<not> (x \<sim> y) ==> y \<sim> z ==> \<not> (x \<sim> (z::'a::equiv))"
wenzelm@10477
    41
proof -
wenzelm@10477
    42
  assume "\<not> (x \<sim> y)" and yz: "y \<sim> z"
wenzelm@10477
    43
  show "\<not> (x \<sim> z)"
wenzelm@10477
    44
  proof
wenzelm@10477
    45
    assume "x \<sim> z"
wenzelm@10477
    46
    also from yz have "z \<sim> y" ..
wenzelm@10477
    47
    finally have "x \<sim> y" .
wenzelm@10477
    48
    thus False by contradiction
wenzelm@10477
    49
  qed
wenzelm@10477
    50
qed
wenzelm@10477
    51
wenzelm@10477
    52
lemma not_equiv_trans2 [trans]: "x \<sim> y ==> \<not> (y \<sim> z) ==> \<not> (x \<sim> (z::'a::equiv))"
wenzelm@10477
    53
proof -
wenzelm@10477
    54
  assume "\<not> (y \<sim> z)" hence "\<not> (z \<sim> y)" ..
wenzelm@10477
    55
  also assume "x \<sim> y" hence "y \<sim> x" ..
wenzelm@10477
    56
  finally have "\<not> (z \<sim> x)" . thus "(\<not> x \<sim> z)" ..
wenzelm@10477
    57
qed
wenzelm@10477
    58
wenzelm@10250
    59
text {*
wenzelm@10285
    60
 \medskip The quotient type @{text "'a quot"} consists of all
wenzelm@10285
    61
 \emph{equivalence classes} over elements of the base type @{typ 'a}.
wenzelm@10250
    62
*}
wenzelm@10250
    63
wenzelm@10392
    64
typedef 'a quot = "{{x. a \<sim> x} | a::'a::eqv. True}"
wenzelm@10250
    65
  by blast
wenzelm@10250
    66
wenzelm@10250
    67
lemma quotI [intro]: "{x. a \<sim> x} \<in> quot"
wenzelm@10250
    68
  by (unfold quot_def) blast
wenzelm@10250
    69
wenzelm@10250
    70
lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"
wenzelm@10250
    71
  by (unfold quot_def) blast
wenzelm@10250
    72
wenzelm@10250
    73
text {*
wenzelm@10250
    74
 \medskip Abstracted equivalence classes are the canonical
wenzelm@10250
    75
 representation of elements of a quotient type.
wenzelm@10250
    76
*}
wenzelm@10250
    77
wenzelm@10250
    78
constdefs
wenzelm@10551
    79
  class :: "'a::equiv => 'a quot"    ("\<lfloor>_\<rfloor>")
wenzelm@10250
    80
  "\<lfloor>a\<rfloor> == Abs_quot {x. a \<sim> x}"
wenzelm@10250
    81
wenzelm@10311
    82
theorem quot_exhaust: "\<exists>a. A = \<lfloor>a\<rfloor>"
wenzelm@10278
    83
proof (cases A)
wenzelm@10278
    84
  fix R assume R: "A = Abs_quot R"
wenzelm@10278
    85
  assume "R \<in> quot" hence "\<exists>a. R = {x. a \<sim> x}" by blast
wenzelm@10278
    86
  with R have "\<exists>a. A = Abs_quot {x. a \<sim> x}" by blast
wenzelm@10551
    87
  thus ?thesis by (unfold class_def)
wenzelm@10250
    88
qed
wenzelm@10250
    89
wenzelm@10311
    90
lemma quot_cases [cases type: quot]: "(!!a. A = \<lfloor>a\<rfloor> ==> C) ==> C"
wenzelm@10311
    91
  by (insert quot_exhaust) blast
wenzelm@10250
    92
wenzelm@10250
    93
wenzelm@10285
    94
subsection {* Equality on quotients *}
wenzelm@10250
    95
wenzelm@10250
    96
text {*
wenzelm@10286
    97
 Equality of canonical quotient elements coincides with the original
wenzelm@10286
    98
 relation.
wenzelm@10250
    99
*}
wenzelm@10250
   100
wenzelm@10477
   101
theorem quot_equality: "(\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> b)"
wenzelm@10285
   102
proof
wenzelm@10285
   103
  assume eq: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10285
   104
  show "a \<sim> b"
wenzelm@10285
   105
  proof -
wenzelm@10285
   106
    from eq have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10551
   107
      by (simp only: class_def Abs_quot_inject quotI)
wenzelm@10285
   108
    moreover have "a \<sim> a" ..
wenzelm@10285
   109
    ultimately have "a \<in> {x. b \<sim> x}" by blast
wenzelm@10285
   110
    hence "b \<sim> a" by blast
wenzelm@10285
   111
    thus ?thesis ..
wenzelm@10285
   112
  qed
wenzelm@10285
   113
next
wenzelm@10250
   114
  assume ab: "a \<sim> b"
wenzelm@10285
   115
  show "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10285
   116
  proof -
wenzelm@10285
   117
    have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10285
   118
    proof (rule Collect_cong)
wenzelm@10285
   119
      fix x show "(a \<sim> x) = (b \<sim> x)"
wenzelm@10285
   120
      proof
wenzelm@10285
   121
        from ab have "b \<sim> a" ..
wenzelm@10285
   122
        also assume "a \<sim> x"
wenzelm@10285
   123
        finally show "b \<sim> x" .
wenzelm@10285
   124
      next
wenzelm@10285
   125
        note ab
wenzelm@10285
   126
        also assume "b \<sim> x"
wenzelm@10285
   127
        finally show "a \<sim> x" .
wenzelm@10285
   128
      qed
wenzelm@10250
   129
    qed
wenzelm@10551
   130
    thus ?thesis by (simp only: class_def)
wenzelm@10250
   131
  qed
wenzelm@10250
   132
qed
wenzelm@10250
   133
wenzelm@10477
   134
lemma quot_equalI [intro?]: "a \<sim> b ==> \<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10477
   135
  by (simp only: quot_equality)
wenzelm@10477
   136
wenzelm@10477
   137
lemma quot_equalD [dest?]: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor> ==> a \<sim> b"
wenzelm@10477
   138
  by (simp only: quot_equality)
wenzelm@10477
   139
wenzelm@10477
   140
lemma quot_not_equalI [intro?]: "\<not> (a \<sim> b) ==> \<lfloor>a\<rfloor> \<noteq> \<lfloor>b\<rfloor>"
wenzelm@10477
   141
  by (simp add: quot_equality)
wenzelm@10477
   142
wenzelm@10477
   143
lemma quot_not_equalD [dest?]: "\<lfloor>a\<rfloor> \<noteq> \<lfloor>b\<rfloor> ==> \<not> (a \<sim> b)"
wenzelm@10477
   144
  by (simp add: quot_equality)
wenzelm@10477
   145
wenzelm@10250
   146
wenzelm@10285
   147
subsection {* Picking representing elements *}
wenzelm@10250
   148
wenzelm@10250
   149
constdefs
wenzelm@10285
   150
  pick :: "'a::equiv quot => 'a"
wenzelm@10250
   151
  "pick A == SOME a. A = \<lfloor>a\<rfloor>"
wenzelm@10250
   152
wenzelm@10285
   153
theorem pick_equiv [intro]: "pick \<lfloor>a\<rfloor> \<sim> a"
wenzelm@10250
   154
proof (unfold pick_def)
wenzelm@10250
   155
  show "(SOME x. \<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>) \<sim> a"
wenzelm@10250
   156
  proof (rule someI2)
wenzelm@10250
   157
    show "\<lfloor>a\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10250
   158
    fix x assume "\<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>"
wenzelm@10285
   159
    hence "a \<sim> x" .. thus "x \<sim> a" ..
wenzelm@10250
   160
  qed
wenzelm@10250
   161
qed
wenzelm@10250
   162
wenzelm@10483
   163
theorem pick_inverse [intro]: "\<lfloor>pick A\<rfloor> = A"
wenzelm@10250
   164
proof (cases A)
wenzelm@10250
   165
  fix a assume a: "A = \<lfloor>a\<rfloor>"
wenzelm@10285
   166
  hence "pick A \<sim> a" by (simp only: pick_equiv)
wenzelm@10285
   167
  hence "\<lfloor>pick A\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10250
   168
  with a show ?thesis by simp
wenzelm@10250
   169
qed
wenzelm@10250
   170
wenzelm@10285
   171
text {*
wenzelm@10285
   172
 \medskip The following rules support canonical function definitions
wenzelm@10483
   173
 on quotient types (with up to two arguments).  Note that the
wenzelm@10483
   174
 stripped-down version without additional conditions is sufficient
wenzelm@10483
   175
 most of the time.
wenzelm@10285
   176
*}
wenzelm@10285
   177
wenzelm@10483
   178
theorem quot_cond_function:
wenzelm@10491
   179
  "(!!X Y. P X Y ==> f X Y == g (pick X) (pick Y)) ==>
wenzelm@10491
   180
    (!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor>
wenzelm@10491
   181
      ==> P \<lfloor>x\<rfloor> \<lfloor>y\<rfloor> ==> P \<lfloor>x'\<rfloor> \<lfloor>y'\<rfloor> ==> g x y = g x' y') ==>
wenzelm@10491
   182
    P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> ==> f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10491
   183
  (is "PROP ?eq ==> PROP ?cong ==> _ ==> _")
wenzelm@10473
   184
proof -
wenzelm@10491
   185
  assume cong: "PROP ?cong"
wenzelm@10491
   186
  assume "PROP ?eq" and "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>"
wenzelm@10491
   187
  hence "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)" by (simp only:)
wenzelm@10505
   188
  also have "... = g a b"
wenzelm@10491
   189
  proof (rule cong)
wenzelm@10483
   190
    show "\<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10483
   191
    moreover
wenzelm@10483
   192
    show "\<lfloor>pick \<lfloor>b\<rfloor>\<rfloor> = \<lfloor>b\<rfloor>" ..
wenzelm@10491
   193
    moreover
wenzelm@10491
   194
    show "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>" .
wenzelm@10491
   195
    ultimately show "P \<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> \<lfloor>pick \<lfloor>b\<rfloor>\<rfloor>" by (simp only:)
wenzelm@10285
   196
  qed
wenzelm@10285
   197
  finally show ?thesis .
wenzelm@10285
   198
qed
wenzelm@10285
   199
wenzelm@10483
   200
theorem quot_function:
wenzelm@10473
   201
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
wenzelm@10483
   202
    (!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor> ==> g x y = g x' y') ==>
wenzelm@10473
   203
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10473
   204
proof -
wenzelm@10491
   205
  case antecedent from this TrueI
wenzelm@10483
   206
  show ?thesis by (rule quot_cond_function)
wenzelm@10285
   207
qed
wenzelm@10285
   208
bauerg@10499
   209
theorem quot_function':
bauerg@10499
   210
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
bauerg@10499
   211
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y = g x' y') ==>
bauerg@10499
   212
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
bauerg@10499
   213
  by  (rule quot_function) (simp only: quot_equality)+
bauerg@10499
   214
wenzelm@10250
   215
end