src/HOL/Library/Quotient.thy
author wenzelm
Thu Nov 16 19:03:26 2000 +0100 (2000-11-16)
changeset 10477 c21bee84cefe
parent 10473 4f15b844fea6
child 10483 eb93ace45a6e
permissions -rw-r--r--
added not_equiv_sym, not_equiv_trans1/2;
tuned;
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@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@10285
    79
  equivalence_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@10285
    87
  thus ?thesis by (unfold equivalence_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@10285
   107
      by (simp only: equivalence_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@10285
   130
    thus ?thesis by (simp only: equivalence_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@10285
   163
theorem pick_inverse: "\<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@10285
   173
 on quotient types.
wenzelm@10285
   174
*}
wenzelm@10285
   175
wenzelm@10473
   176
theorem quot_cond_function1:
wenzelm@10473
   177
  "(!!X. f X == g (pick X)) ==>
wenzelm@10473
   178
    (!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x = g x') ==>
wenzelm@10473
   179
    (!!x x'. x \<sim> x' ==> P x = P x') ==>
wenzelm@10473
   180
  P a ==> f \<lfloor>a\<rfloor> = g a"
wenzelm@10473
   181
proof -
wenzelm@10473
   182
  assume cong_g: "!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x = g x'"
wenzelm@10473
   183
  assume cong_P: "!!x x'. x \<sim> x' ==> P x = P x'"
wenzelm@10473
   184
  assume P: "P a"
wenzelm@10473
   185
  assume "!!X. f X == g (pick X)"
wenzelm@10473
   186
  hence "f \<lfloor>a\<rfloor> = g (pick \<lfloor>a\<rfloor>)" by (simp only:)
wenzelm@10473
   187
  also have "\<dots> = g a"
wenzelm@10473
   188
  proof (rule cong_g)
wenzelm@10473
   189
    show "pick \<lfloor>a\<rfloor> \<sim> a" ..
wenzelm@10473
   190
    hence "P (pick \<lfloor>a\<rfloor>) = P a" by (rule cong_P)
wenzelm@10473
   191
    also note P
wenzelm@10473
   192
    finally show "P (pick \<lfloor>a\<rfloor>)" .
wenzelm@10473
   193
  qed
wenzelm@10473
   194
  finally show ?thesis .
wenzelm@10473
   195
qed
wenzelm@10473
   196
wenzelm@10473
   197
theorem quot_function1:
wenzelm@10473
   198
  "(!!X. f X == g (pick X)) ==>
wenzelm@10473
   199
    (!!x x'. x \<sim> x' ==> g x = g x') ==>
wenzelm@10473
   200
    f \<lfloor>a\<rfloor> = g a"
wenzelm@10473
   201
proof -
wenzelm@10473
   202
  case antecedent from this refl TrueI
wenzelm@10473
   203
  show ?thesis by (rule quot_cond_function1)
wenzelm@10473
   204
qed
wenzelm@10473
   205
wenzelm@10473
   206
theorem quot_cond_operation1:
wenzelm@10459
   207
  "(!!X. f X == \<lfloor>g (pick X)\<rfloor>) ==>
wenzelm@10459
   208
    (!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x \<sim> g x') ==>
wenzelm@10459
   209
    (!!x x'. x \<sim> x' ==> P x = P x') ==>
wenzelm@10459
   210
  P a ==> f \<lfloor>a\<rfloor> = \<lfloor>g a\<rfloor>"
wenzelm@10459
   211
proof -
wenzelm@10473
   212
  assume defn: "!!X. f X == \<lfloor>g (pick X)\<rfloor>"
wenzelm@10473
   213
  assume "!!x x'. x \<sim> x' ==> P x ==> P x' ==> g x \<sim> g x'"
wenzelm@10473
   214
  hence cong_g: "!!x x'. x \<sim> x' ==> P x ==> P x' ==> \<lfloor>g x\<rfloor> = \<lfloor>g x'\<rfloor>" ..
wenzelm@10473
   215
  assume "!!x x'. x \<sim> x' ==> P x = P x'" and "P a"
wenzelm@10473
   216
  with defn cong_g show ?thesis by (rule quot_cond_function1)
wenzelm@10459
   217
qed
wenzelm@10459
   218
wenzelm@10473
   219
theorem quot_operation1:
wenzelm@10437
   220
  "(!!X. f X == \<lfloor>g (pick X)\<rfloor>) ==>
wenzelm@10437
   221
    (!!x x'. x \<sim> x' ==> g x \<sim> g x') ==>
wenzelm@10437
   222
    f \<lfloor>a\<rfloor> = \<lfloor>g a\<rfloor>"
wenzelm@10285
   223
proof -
wenzelm@10459
   224
  case antecedent from this refl TrueI
wenzelm@10473
   225
  show ?thesis by (rule quot_cond_operation1)
wenzelm@10459
   226
qed
wenzelm@10459
   227
wenzelm@10473
   228
theorem quot_cond_function2:
wenzelm@10473
   229
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
wenzelm@10473
   230
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   231
      ==> g x y = g x' y') ==>
wenzelm@10459
   232
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y') ==>
wenzelm@10473
   233
    P a b ==> f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10459
   234
proof -
wenzelm@10473
   235
  assume cong_g: "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   236
    ==> g x y = g x' y'"
wenzelm@10459
   237
  assume cong_P: "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y'"
wenzelm@10459
   238
  assume P: "P a b"
wenzelm@10473
   239
  assume "!!X Y. f X Y == g (pick X) (pick Y)"
wenzelm@10473
   240
  hence "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)" by (simp only:)
wenzelm@10473
   241
  also have "\<dots> = g a b"
wenzelm@10473
   242
  proof (rule cong_g)
wenzelm@10473
   243
    show "pick \<lfloor>a\<rfloor> \<sim> a" ..
wenzelm@10473
   244
    moreover show "pick \<lfloor>b\<rfloor> \<sim> b" ..
wenzelm@10473
   245
    ultimately have "P (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>) = P a b" by (rule cong_P)
wenzelm@10473
   246
    also show "P a b" .
wenzelm@10473
   247
    finally show "P (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)" .
wenzelm@10285
   248
  qed
wenzelm@10285
   249
  finally show ?thesis .
wenzelm@10285
   250
qed
wenzelm@10285
   251
wenzelm@10473
   252
theorem quot_function2:
wenzelm@10473
   253
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
wenzelm@10473
   254
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y = g x' y') ==>
wenzelm@10473
   255
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10473
   256
proof -
wenzelm@10473
   257
  case antecedent from this refl TrueI
wenzelm@10473
   258
  show ?thesis by (rule quot_cond_function2)
wenzelm@10473
   259
qed
wenzelm@10473
   260
wenzelm@10473
   261
theorem quot_cond_operation2:
wenzelm@10473
   262
  "(!!X Y. f X Y == \<lfloor>g (pick X) (pick Y)\<rfloor>) ==>
wenzelm@10473
   263
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   264
      ==> g x y \<sim> g x' y') ==>
wenzelm@10473
   265
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y') ==>
wenzelm@10473
   266
    P a b ==> f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = \<lfloor>g a b\<rfloor>"
wenzelm@10473
   267
proof -
wenzelm@10473
   268
  assume defn: "!!X Y. f X Y == \<lfloor>g (pick X) (pick Y)\<rfloor>"
wenzelm@10473
   269
  assume "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   270
    ==> g x y \<sim> g x' y'"
wenzelm@10473
   271
  hence cong_g: "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y ==> P x' y'
wenzelm@10473
   272
    ==> \<lfloor>g x y\<rfloor> = \<lfloor>g x' y'\<rfloor>" ..
wenzelm@10473
   273
  assume "!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> P x y = P x' y'" and "P a b"
wenzelm@10473
   274
  with defn cong_g show ?thesis by (rule quot_cond_function2)
wenzelm@10473
   275
qed
wenzelm@10473
   276
wenzelm@10473
   277
theorem quot_operation2:
wenzelm@10437
   278
  "(!!X Y. f X Y == \<lfloor>g (pick X) (pick Y)\<rfloor>) ==>
wenzelm@10437
   279
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y \<sim> g x' y') ==>
wenzelm@10437
   280
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = \<lfloor>g a b\<rfloor>"
wenzelm@10285
   281
proof -
wenzelm@10459
   282
  case antecedent from this refl TrueI
wenzelm@10473
   283
  show ?thesis by (rule quot_cond_operation2)
wenzelm@10285
   284
qed
wenzelm@10285
   285
wenzelm@10437
   286
text {*
wenzelm@10437
   287
 \medskip HOL's collection of overloaded standard operations is lifted
wenzelm@10437
   288
 to quotient types in the canonical manner.
wenzelm@10437
   289
*}
wenzelm@10437
   290
wenzelm@10437
   291
instance quot :: (zero) zero ..
wenzelm@10437
   292
instance quot :: (plus) plus ..
wenzelm@10437
   293
instance quot :: (minus) minus ..
wenzelm@10437
   294
instance quot :: (times) times ..
wenzelm@10437
   295
instance quot :: (inverse) inverse ..
wenzelm@10437
   296
instance quot :: (power) power ..
wenzelm@10437
   297
instance quot :: (number) number ..
wenzelm@10459
   298
instance quot :: (ord) ord ..
wenzelm@10437
   299
wenzelm@10437
   300
defs (overloaded)
wenzelm@10437
   301
  zero_quot_def: "0 == \<lfloor>0\<rfloor>"
wenzelm@10437
   302
  add_quot_def: "X + Y == \<lfloor>pick X + pick Y\<rfloor>"
wenzelm@10437
   303
  diff_quot_def: "X - Y == \<lfloor>pick X - pick Y\<rfloor>"
wenzelm@10437
   304
  minus_quot_def: "- X == \<lfloor>- pick X\<rfloor>"
wenzelm@10437
   305
  abs_quot_def: "abs X == \<lfloor>abs (pick X)\<rfloor>"
wenzelm@10437
   306
  mult_quot_def: "X * Y == \<lfloor>pick X * pick Y\<rfloor>"
wenzelm@10437
   307
  inverse_quot_def: "inverse X == \<lfloor>inverse (pick X)\<rfloor>"
wenzelm@10437
   308
  divide_quot_def: "X / Y == \<lfloor>pick X / pick Y\<rfloor>"
wenzelm@10437
   309
  power_quot_def: "X^n == \<lfloor>(pick X)^n\<rfloor>"
wenzelm@10437
   310
  number_of_quot_def: "number_of b == \<lfloor>number_of b\<rfloor>"
wenzelm@10459
   311
  le_quot_def: "X \<le> Y == pick X \<le> pick Y"
wenzelm@10459
   312
  less_quot_def: "X < Y == pick X < pick Y"
wenzelm@10437
   313
wenzelm@10250
   314
end