src/HOL/Library/Quotient.thy
author wenzelm
Wed Nov 15 19:43:42 2000 +0100 (2000-11-15)
changeset 10473 4f15b844fea6
parent 10459 df3cd3e76046
child 10477 c21bee84cefe
permissions -rw-r--r--
separate rules for function/operation definitions;
wenzelm@10250
     1
(*  Title:      HOL/Library/Quotient.thy
wenzelm@10250
     2
    ID:         $Id$
wenzelm@10250
     3
    Author:     Gertrud Bauer and Markus Wenzel, TU Muenchen
wenzelm@10250
     4
*)
wenzelm@10250
     5
wenzelm@10250
     6
header {*
wenzelm@10473
     7
  \title{Quotient types}
wenzelm@10250
     8
  \author{Gertrud Bauer and 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@10250
    34
text {*
wenzelm@10285
    35
 \medskip The quotient type @{text "'a quot"} consists of all
wenzelm@10285
    36
 \emph{equivalence classes} over elements of the base type @{typ 'a}.
wenzelm@10250
    37
*}
wenzelm@10250
    38
wenzelm@10392
    39
typedef 'a quot = "{{x. a \<sim> x} | a::'a::eqv. True}"
wenzelm@10250
    40
  by blast
wenzelm@10250
    41
wenzelm@10250
    42
lemma quotI [intro]: "{x. a \<sim> x} \<in> quot"
wenzelm@10250
    43
  by (unfold quot_def) blast
wenzelm@10250
    44
wenzelm@10250
    45
lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"
wenzelm@10250
    46
  by (unfold quot_def) blast
wenzelm@10250
    47
wenzelm@10250
    48
text {*
wenzelm@10250
    49
 \medskip Abstracted equivalence classes are the canonical
wenzelm@10250
    50
 representation of elements of a quotient type.
wenzelm@10250
    51
*}
wenzelm@10250
    52
wenzelm@10250
    53
constdefs
wenzelm@10285
    54
  equivalence_class :: "'a::equiv => 'a quot"    ("\<lfloor>_\<rfloor>")
wenzelm@10250
    55
  "\<lfloor>a\<rfloor> == Abs_quot {x. a \<sim> x}"
wenzelm@10250
    56
wenzelm@10311
    57
theorem quot_exhaust: "\<exists>a. A = \<lfloor>a\<rfloor>"
wenzelm@10278
    58
proof (cases A)
wenzelm@10278
    59
  fix R assume R: "A = Abs_quot R"
wenzelm@10278
    60
  assume "R \<in> quot" hence "\<exists>a. R = {x. a \<sim> x}" by blast
wenzelm@10278
    61
  with R have "\<exists>a. A = Abs_quot {x. a \<sim> x}" by blast
wenzelm@10285
    62
  thus ?thesis by (unfold equivalence_class_def)
wenzelm@10250
    63
qed
wenzelm@10250
    64
wenzelm@10311
    65
lemma quot_cases [cases type: quot]: "(!!a. A = \<lfloor>a\<rfloor> ==> C) ==> C"
wenzelm@10311
    66
  by (insert quot_exhaust) blast
wenzelm@10250
    67
wenzelm@10250
    68
wenzelm@10285
    69
subsection {* Equality on quotients *}
wenzelm@10250
    70
wenzelm@10250
    71
text {*
wenzelm@10286
    72
 Equality of canonical quotient elements coincides with the original
wenzelm@10286
    73
 relation.
wenzelm@10250
    74
*}
wenzelm@10250
    75
wenzelm@10459
    76
theorem equivalence_class_iff [iff?]: "(\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> b)"
wenzelm@10285
    77
proof
wenzelm@10285
    78
  assume eq: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10285
    79
  show "a \<sim> b"
wenzelm@10285
    80
  proof -
wenzelm@10285
    81
    from eq have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10285
    82
      by (simp only: equivalence_class_def Abs_quot_inject quotI)
wenzelm@10285
    83
    moreover have "a \<sim> a" ..
wenzelm@10285
    84
    ultimately have "a \<in> {x. b \<sim> x}" by blast
wenzelm@10285
    85
    hence "b \<sim> a" by blast
wenzelm@10285
    86
    thus ?thesis ..
wenzelm@10285
    87
  qed
wenzelm@10285
    88
next
wenzelm@10250
    89
  assume ab: "a \<sim> b"
wenzelm@10285
    90
  show "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10285
    91
  proof -
wenzelm@10285
    92
    have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10285
    93
    proof (rule Collect_cong)
wenzelm@10285
    94
      fix x show "(a \<sim> x) = (b \<sim> x)"
wenzelm@10285
    95
      proof
wenzelm@10285
    96
        from ab have "b \<sim> a" ..
wenzelm@10285
    97
        also assume "a \<sim> x"
wenzelm@10285
    98
        finally show "b \<sim> x" .
wenzelm@10285
    99
      next
wenzelm@10285
   100
        note ab
wenzelm@10285
   101
        also assume "b \<sim> x"
wenzelm@10285
   102
        finally show "a \<sim> x" .
wenzelm@10285
   103
      qed
wenzelm@10250
   104
    qed
wenzelm@10285
   105
    thus ?thesis by (simp only: equivalence_class_def)
wenzelm@10250
   106
  qed
wenzelm@10250
   107
qed
wenzelm@10250
   108
wenzelm@10250
   109
wenzelm@10285
   110
subsection {* Picking representing elements *}
wenzelm@10250
   111
wenzelm@10250
   112
constdefs
wenzelm@10285
   113
  pick :: "'a::equiv quot => 'a"
wenzelm@10250
   114
  "pick A == SOME a. A = \<lfloor>a\<rfloor>"
wenzelm@10250
   115
wenzelm@10285
   116
theorem pick_equiv [intro]: "pick \<lfloor>a\<rfloor> \<sim> a"
wenzelm@10250
   117
proof (unfold pick_def)
wenzelm@10250
   118
  show "(SOME x. \<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>) \<sim> a"
wenzelm@10250
   119
  proof (rule someI2)
wenzelm@10250
   120
    show "\<lfloor>a\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10250
   121
    fix x assume "\<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>"
wenzelm@10285
   122
    hence "a \<sim> x" .. thus "x \<sim> a" ..
wenzelm@10250
   123
  qed
wenzelm@10250
   124
qed
wenzelm@10250
   125
wenzelm@10285
   126
theorem pick_inverse: "\<lfloor>pick A\<rfloor> = A"
wenzelm@10250
   127
proof (cases A)
wenzelm@10250
   128
  fix a assume a: "A = \<lfloor>a\<rfloor>"
wenzelm@10285
   129
  hence "pick A \<sim> a" by (simp only: pick_equiv)
wenzelm@10285
   130
  hence "\<lfloor>pick A\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10250
   131
  with a show ?thesis by simp
wenzelm@10250
   132
qed
wenzelm@10250
   133
wenzelm@10285
   134
text {*
wenzelm@10285
   135
 \medskip The following rules support canonical function definitions
wenzelm@10285
   136
 on quotient types.
wenzelm@10285
   137
*}
wenzelm@10285
   138
wenzelm@10473
   139
theorem quot_cond_function1:
wenzelm@10473
   140
  "(!!X. f X == g (pick X)) ==>
wenzelm@10473
   141
    (!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x = g x') ==>
wenzelm@10473
   142
    (!!x x'. x \<sim> x' ==> P x = P x') ==>
wenzelm@10473
   143
  P a ==> f \<lfloor>a\<rfloor> = g a"
wenzelm@10473
   144
proof -
wenzelm@10473
   145
  assume cong_g: "!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x = g x'"
wenzelm@10473
   146
  assume cong_P: "!!x x'. x \<sim> x' ==> P x = P x'"
wenzelm@10473
   147
  assume P: "P a"
wenzelm@10473
   148
  assume "!!X. f X == g (pick X)"
wenzelm@10473
   149
  hence "f \<lfloor>a\<rfloor> = g (pick \<lfloor>a\<rfloor>)" by (simp only:)
wenzelm@10473
   150
  also have "\<dots> = g a"
wenzelm@10473
   151
  proof (rule cong_g)
wenzelm@10473
   152
    show "pick \<lfloor>a\<rfloor> \<sim> a" ..
wenzelm@10473
   153
    hence "P (pick \<lfloor>a\<rfloor>) = P a" by (rule cong_P)
wenzelm@10473
   154
    also note P
wenzelm@10473
   155
    finally show "P (pick \<lfloor>a\<rfloor>)" .
wenzelm@10473
   156
  qed
wenzelm@10473
   157
  finally show ?thesis .
wenzelm@10473
   158
qed
wenzelm@10473
   159
wenzelm@10473
   160
theorem quot_function1:
wenzelm@10473
   161
  "(!!X. f X == g (pick X)) ==>
wenzelm@10473
   162
    (!!x x'. x \<sim> x' ==> g x = g x') ==>
wenzelm@10473
   163
    f \<lfloor>a\<rfloor> = g a"
wenzelm@10473
   164
proof -
wenzelm@10473
   165
  case antecedent from this refl TrueI
wenzelm@10473
   166
  show ?thesis by (rule quot_cond_function1)
wenzelm@10473
   167
qed
wenzelm@10473
   168
wenzelm@10473
   169
theorem quot_cond_operation1:
wenzelm@10459
   170
  "(!!X. f X == \<lfloor>g (pick X)\<rfloor>) ==>
wenzelm@10459
   171
    (!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x \<sim> g x') ==>
wenzelm@10459
   172
    (!!x x'. x \<sim> x' ==> P x = P x') ==>
wenzelm@10459
   173
  P a ==> f \<lfloor>a\<rfloor> = \<lfloor>g a\<rfloor>"
wenzelm@10459
   174
proof -
wenzelm@10473
   175
  assume defn: "!!X. f X == \<lfloor>g (pick X)\<rfloor>"
wenzelm@10473
   176
  assume "!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x \<sim> g x'"
wenzelm@10473
   177
  hence cong_g: "!!x x'. x \<sim> x' ==> P x ==> P x' ==> \<lfloor>g x\<rfloor> = \<lfloor>g x'\<rfloor>" ..
wenzelm@10473
   178
  assume "!!x x'. x \<sim> x' ==> P x = P x'" and "P a"
wenzelm@10473
   179
  with defn cong_g show ?thesis by (rule quot_cond_function1)
wenzelm@10459
   180
qed
wenzelm@10459
   181
wenzelm@10473
   182
theorem quot_operation1:
wenzelm@10437
   183
  "(!!X. f X == \<lfloor>g (pick X)\<rfloor>) ==>
wenzelm@10437
   184
    (!!x x'. x \<sim> x' ==> g x \<sim> g x') ==>
wenzelm@10437
   185
    f \<lfloor>a\<rfloor> = \<lfloor>g a\<rfloor>"
wenzelm@10285
   186
proof -
wenzelm@10459
   187
  case antecedent from this refl TrueI
wenzelm@10473
   188
  show ?thesis by (rule quot_cond_operation1)
wenzelm@10459
   189
qed
wenzelm@10459
   190
wenzelm@10473
   191
theorem quot_cond_function2:
wenzelm@10473
   192
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
wenzelm@10473
   193
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   194
      ==> g x y = g x' y') ==>
wenzelm@10459
   195
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y') ==>
wenzelm@10473
   196
    P a b ==> f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10459
   197
proof -
wenzelm@10473
   198
  assume cong_g: "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   199
    ==> g x y = g x' y'"
wenzelm@10459
   200
  assume cong_P: "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y'"
wenzelm@10459
   201
  assume P: "P a b"
wenzelm@10473
   202
  assume "!!X Y. f X Y == g (pick X) (pick Y)"
wenzelm@10473
   203
  hence "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)" by (simp only:)
wenzelm@10473
   204
  also have "\<dots> = g a b"
wenzelm@10473
   205
  proof (rule cong_g)
wenzelm@10473
   206
    show "pick \<lfloor>a\<rfloor> \<sim> a" ..
wenzelm@10473
   207
    moreover show "pick \<lfloor>b\<rfloor> \<sim> b" ..
wenzelm@10473
   208
    ultimately have "P (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>) = P a b" by (rule cong_P)
wenzelm@10473
   209
    also show "P a b" .
wenzelm@10473
   210
    finally show "P (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)" .
wenzelm@10285
   211
  qed
wenzelm@10285
   212
  finally show ?thesis .
wenzelm@10285
   213
qed
wenzelm@10285
   214
wenzelm@10473
   215
theorem quot_function2:
wenzelm@10473
   216
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
wenzelm@10473
   217
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y = g x' y') ==>
wenzelm@10473
   218
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10473
   219
proof -
wenzelm@10473
   220
  case antecedent from this refl TrueI
wenzelm@10473
   221
  show ?thesis by (rule quot_cond_function2)
wenzelm@10473
   222
qed
wenzelm@10473
   223
wenzelm@10473
   224
theorem quot_cond_operation2:
wenzelm@10473
   225
  "(!!X Y. f X Y == \<lfloor>g (pick X) (pick Y)\<rfloor>) ==>
wenzelm@10473
   226
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   227
      ==> g x y \<sim> g x' y') ==>
wenzelm@10473
   228
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y') ==>
wenzelm@10473
   229
    P a b ==> f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = \<lfloor>g a b\<rfloor>"
wenzelm@10473
   230
proof -
wenzelm@10473
   231
  assume defn: "!!X Y. f X Y == \<lfloor>g (pick X) (pick Y)\<rfloor>"
wenzelm@10473
   232
  assume "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   233
    ==> g x y \<sim> g x' y'"
wenzelm@10473
   234
  hence cong_g: "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   235
    ==> \<lfloor>g x y\<rfloor> = \<lfloor>g x' y'\<rfloor>" ..
wenzelm@10473
   236
  assume "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y'" and "P a b"
wenzelm@10473
   237
  with defn cong_g show ?thesis by (rule quot_cond_function2)
wenzelm@10473
   238
qed
wenzelm@10473
   239
wenzelm@10473
   240
theorem quot_operation2:
wenzelm@10437
   241
  "(!!X Y. f X Y == \<lfloor>g (pick X) (pick Y)\<rfloor>) ==>
wenzelm@10437
   242
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y \<sim> g x' y') ==>
wenzelm@10437
   243
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = \<lfloor>g a b\<rfloor>"
wenzelm@10285
   244
proof -
wenzelm@10459
   245
  case antecedent from this refl TrueI
wenzelm@10473
   246
  show ?thesis by (rule quot_cond_operation2)
wenzelm@10285
   247
qed
wenzelm@10285
   248
wenzelm@10437
   249
text {*
wenzelm@10437
   250
 \medskip HOL's collection of overloaded standard operations is lifted
wenzelm@10437
   251
 to quotient types in the canonical manner.
wenzelm@10437
   252
*}
wenzelm@10437
   253
wenzelm@10437
   254
instance quot :: (zero) zero ..
wenzelm@10437
   255
instance quot :: (plus) plus ..
wenzelm@10437
   256
instance quot :: (minus) minus ..
wenzelm@10437
   257
instance quot :: (times) times ..
wenzelm@10437
   258
instance quot :: (inverse) inverse ..
wenzelm@10437
   259
instance quot :: (power) power ..
wenzelm@10437
   260
instance quot :: (number) number ..
wenzelm@10459
   261
instance quot :: (ord) ord ..
wenzelm@10437
   262
wenzelm@10437
   263
defs (overloaded)
wenzelm@10437
   264
  zero_quot_def: "0 == \<lfloor>0\<rfloor>"
wenzelm@10437
   265
  add_quot_def: "X + Y == \<lfloor>pick X + pick Y\<rfloor>"
wenzelm@10437
   266
  diff_quot_def: "X - Y == \<lfloor>pick X - pick Y\<rfloor>"
wenzelm@10437
   267
  minus_quot_def: "- X == \<lfloor>- pick X\<rfloor>"
wenzelm@10437
   268
  abs_quot_def: "abs X == \<lfloor>abs (pick X)\<rfloor>"
wenzelm@10437
   269
  mult_quot_def: "X * Y == \<lfloor>pick X * pick Y\<rfloor>"
wenzelm@10437
   270
  inverse_quot_def: "inverse X == \<lfloor>inverse (pick X)\<rfloor>"
wenzelm@10437
   271
  divide_quot_def: "X / Y == \<lfloor>pick X / pick Y\<rfloor>"
wenzelm@10437
   272
  power_quot_def: "X^n == \<lfloor>(pick X)^n\<rfloor>"
wenzelm@10437
   273
  number_of_quot_def: "number_of b == \<lfloor>number_of b\<rfloor>"
wenzelm@10459
   274
  le_quot_def: "X \<le> Y == pick X \<le> pick Y"
wenzelm@10459
   275
  less_quot_def: "X < Y == pick X < pick Y"
wenzelm@10437
   276
wenzelm@10250
   277
end