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