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