src/HOL/Library/Quotient.thy
author haftmann
Tue Mar 20 08:27:15 2007 +0100 (2007-03-20)
changeset 22473 753123c89d72
parent 22390 378f34b1e380
child 23373 ead82c82da9e
permissions -rw-r--r--
explizit "type" superclass
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
haftmann@22390
    14
 via 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
haftmann@22473
    24
class eqv = type +
haftmann@22390
    25
  fixes eqv :: "'a \<Rightarrow> 'a \<Rightarrow> bool"    (infixl "\<^loc>\<sim>" 50)
wenzelm@10250
    26
haftmann@22390
    27
class equiv = eqv +
haftmann@22390
    28
  assumes equiv_refl [intro]: "x \<^loc>\<sim> x"
haftmann@22390
    29
  assumes equiv_trans [trans]: "x \<^loc>\<sim> y \<Longrightarrow> y \<^loc>\<sim> z \<Longrightarrow> x \<^loc>\<sim> z"
haftmann@22390
    30
  assumes equiv_sym [sym]: "x \<^loc>\<sim> y \<Longrightarrow> y \<^loc>\<sim> x"
wenzelm@10250
    31
wenzelm@12371
    32
lemma equiv_not_sym [sym]: "\<not> (x \<sim> y) ==> \<not> (y \<sim> (x::'a::equiv))"
wenzelm@10477
    33
proof -
wenzelm@10477
    34
  assume "\<not> (x \<sim> y)" thus "\<not> (y \<sim> x)"
wenzelm@10477
    35
    by (rule contrapos_nn) (rule equiv_sym)
wenzelm@10477
    36
qed
wenzelm@10477
    37
wenzelm@10477
    38
lemma not_equiv_trans1 [trans]: "\<not> (x \<sim> y) ==> y \<sim> z ==> \<not> (x \<sim> (z::'a::equiv))"
wenzelm@10477
    39
proof -
wenzelm@10477
    40
  assume "\<not> (x \<sim> y)" and yz: "y \<sim> z"
wenzelm@10477
    41
  show "\<not> (x \<sim> z)"
wenzelm@10477
    42
  proof
wenzelm@10477
    43
    assume "x \<sim> z"
wenzelm@10477
    44
    also from yz have "z \<sim> y" ..
wenzelm@10477
    45
    finally have "x \<sim> y" .
wenzelm@10477
    46
    thus False by contradiction
wenzelm@10477
    47
  qed
wenzelm@10477
    48
qed
wenzelm@10477
    49
wenzelm@10477
    50
lemma not_equiv_trans2 [trans]: "x \<sim> y ==> \<not> (y \<sim> z) ==> \<not> (x \<sim> (z::'a::equiv))"
wenzelm@10477
    51
proof -
wenzelm@10477
    52
  assume "\<not> (y \<sim> z)" hence "\<not> (z \<sim> y)" ..
wenzelm@10477
    53
  also assume "x \<sim> y" hence "y \<sim> x" ..
wenzelm@10477
    54
  finally have "\<not> (z \<sim> x)" . thus "(\<not> x \<sim> z)" ..
wenzelm@10477
    55
qed
wenzelm@10477
    56
wenzelm@10250
    57
text {*
wenzelm@10285
    58
 \medskip The quotient type @{text "'a quot"} consists of all
wenzelm@10285
    59
 \emph{equivalence classes} over elements of the base type @{typ 'a}.
wenzelm@10250
    60
*}
wenzelm@10250
    61
wenzelm@10392
    62
typedef 'a quot = "{{x. a \<sim> x} | a::'a::eqv. True}"
wenzelm@10250
    63
  by blast
wenzelm@10250
    64
wenzelm@10250
    65
lemma quotI [intro]: "{x. a \<sim> x} \<in> quot"
wenzelm@18730
    66
  unfolding quot_def by blast
wenzelm@10250
    67
wenzelm@10250
    68
lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"
wenzelm@18730
    69
  unfolding quot_def by blast
wenzelm@10250
    70
wenzelm@10250
    71
text {*
wenzelm@10250
    72
 \medskip Abstracted equivalence classes are the canonical
wenzelm@10250
    73
 representation of elements of a quotient type.
wenzelm@10250
    74
*}
wenzelm@10250
    75
wenzelm@19086
    76
definition
wenzelm@21404
    77
  "class" :: "'a::equiv => 'a quot"  ("\<lfloor>_\<rfloor>") where
wenzelm@19086
    78
  "\<lfloor>a\<rfloor> = Abs_quot {x. a \<sim> x}"
wenzelm@10250
    79
wenzelm@10311
    80
theorem quot_exhaust: "\<exists>a. A = \<lfloor>a\<rfloor>"
wenzelm@10278
    81
proof (cases A)
wenzelm@10278
    82
  fix R assume R: "A = Abs_quot R"
wenzelm@10278
    83
  assume "R \<in> quot" hence "\<exists>a. R = {x. a \<sim> x}" by blast
wenzelm@10278
    84
  with R have "\<exists>a. A = Abs_quot {x. a \<sim> x}" by blast
wenzelm@18730
    85
  thus ?thesis unfolding class_def .
wenzelm@10250
    86
qed
wenzelm@10250
    87
wenzelm@10311
    88
lemma quot_cases [cases type: quot]: "(!!a. A = \<lfloor>a\<rfloor> ==> C) ==> C"
wenzelm@18730
    89
  using quot_exhaust by blast
wenzelm@10250
    90
wenzelm@10250
    91
wenzelm@10285
    92
subsection {* Equality on quotients *}
wenzelm@10250
    93
wenzelm@10250
    94
text {*
wenzelm@10286
    95
 Equality of canonical quotient elements coincides with the original
wenzelm@10286
    96
 relation.
wenzelm@10250
    97
*}
wenzelm@10250
    98
wenzelm@12371
    99
theorem quot_equality [iff?]: "(\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> b)"
wenzelm@10285
   100
proof
wenzelm@10285
   101
  assume eq: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10285
   102
  show "a \<sim> b"
wenzelm@10285
   103
  proof -
wenzelm@10285
   104
    from eq have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10551
   105
      by (simp only: class_def Abs_quot_inject quotI)
wenzelm@10285
   106
    moreover have "a \<sim> a" ..
wenzelm@10285
   107
    ultimately have "a \<in> {x. b \<sim> x}" by blast
wenzelm@10285
   108
    hence "b \<sim> a" by blast
wenzelm@10285
   109
    thus ?thesis ..
wenzelm@10285
   110
  qed
wenzelm@10285
   111
next
wenzelm@10250
   112
  assume ab: "a \<sim> b"
wenzelm@10285
   113
  show "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10285
   114
  proof -
wenzelm@10285
   115
    have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10285
   116
    proof (rule Collect_cong)
wenzelm@10285
   117
      fix x show "(a \<sim> x) = (b \<sim> x)"
wenzelm@10285
   118
      proof
wenzelm@10285
   119
        from ab have "b \<sim> a" ..
wenzelm@10285
   120
        also assume "a \<sim> x"
wenzelm@10285
   121
        finally show "b \<sim> x" .
wenzelm@10285
   122
      next
wenzelm@10285
   123
        note ab
wenzelm@10285
   124
        also assume "b \<sim> x"
wenzelm@10285
   125
        finally show "a \<sim> x" .
wenzelm@10285
   126
      qed
wenzelm@10250
   127
    qed
wenzelm@10551
   128
    thus ?thesis by (simp only: class_def)
wenzelm@10250
   129
  qed
wenzelm@10250
   130
qed
wenzelm@10250
   131
wenzelm@10250
   132
wenzelm@10285
   133
subsection {* Picking representing elements *}
wenzelm@10250
   134
wenzelm@19086
   135
definition
wenzelm@21404
   136
  pick :: "'a::equiv quot => 'a" where
wenzelm@19086
   137
  "pick A = (SOME a. A = \<lfloor>a\<rfloor>)"
wenzelm@10250
   138
wenzelm@10285
   139
theorem pick_equiv [intro]: "pick \<lfloor>a\<rfloor> \<sim> a"
wenzelm@10250
   140
proof (unfold pick_def)
wenzelm@10250
   141
  show "(SOME x. \<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>) \<sim> a"
wenzelm@10250
   142
  proof (rule someI2)
wenzelm@10250
   143
    show "\<lfloor>a\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10250
   144
    fix x assume "\<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>"
wenzelm@10285
   145
    hence "a \<sim> x" .. thus "x \<sim> a" ..
wenzelm@10250
   146
  qed
wenzelm@10250
   147
qed
wenzelm@10250
   148
wenzelm@10483
   149
theorem pick_inverse [intro]: "\<lfloor>pick A\<rfloor> = A"
wenzelm@10250
   150
proof (cases A)
wenzelm@10250
   151
  fix a assume a: "A = \<lfloor>a\<rfloor>"
wenzelm@10285
   152
  hence "pick A \<sim> a" by (simp only: pick_equiv)
wenzelm@10285
   153
  hence "\<lfloor>pick A\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10250
   154
  with a show ?thesis by simp
wenzelm@10250
   155
qed
wenzelm@10250
   156
wenzelm@10285
   157
text {*
wenzelm@10285
   158
 \medskip The following rules support canonical function definitions
wenzelm@10483
   159
 on quotient types (with up to two arguments).  Note that the
wenzelm@10483
   160
 stripped-down version without additional conditions is sufficient
wenzelm@10483
   161
 most of the time.
wenzelm@10285
   162
*}
wenzelm@10285
   163
wenzelm@10483
   164
theorem quot_cond_function:
wenzelm@18372
   165
  assumes eq: "!!X Y. P X Y ==> f X Y == g (pick X) (pick Y)"
wenzelm@18372
   166
    and cong: "!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor>
wenzelm@18372
   167
      ==> P \<lfloor>x\<rfloor> \<lfloor>y\<rfloor> ==> P \<lfloor>x'\<rfloor> \<lfloor>y'\<rfloor> ==> g x y = g x' y'"
wenzelm@18372
   168
    and P: "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>"
wenzelm@18372
   169
  shows "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10473
   170
proof -
wenzelm@18372
   171
  from eq and P have "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)" by (simp only:)
wenzelm@10505
   172
  also have "... = g a b"
wenzelm@10491
   173
  proof (rule cong)
wenzelm@10483
   174
    show "\<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10483
   175
    moreover
wenzelm@10483
   176
    show "\<lfloor>pick \<lfloor>b\<rfloor>\<rfloor> = \<lfloor>b\<rfloor>" ..
wenzelm@10491
   177
    moreover
wenzelm@10491
   178
    show "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>" .
wenzelm@10491
   179
    ultimately show "P \<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> \<lfloor>pick \<lfloor>b\<rfloor>\<rfloor>" by (simp only:)
wenzelm@10285
   180
  qed
wenzelm@10285
   181
  finally show ?thesis .
wenzelm@10285
   182
qed
wenzelm@10285
   183
wenzelm@10483
   184
theorem quot_function:
wenzelm@18372
   185
  assumes "!!X Y. f X Y == g (pick X) (pick Y)"
wenzelm@18372
   186
    and "!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor> ==> g x y = g x' y'"
wenzelm@18372
   187
  shows "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@18372
   188
  using prems and TrueI
wenzelm@18372
   189
  by (rule quot_cond_function)
wenzelm@10285
   190
bauerg@10499
   191
theorem quot_function':
bauerg@10499
   192
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
bauerg@10499
   193
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y = g x' y') ==>
bauerg@10499
   194
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@18372
   195
  by (rule quot_function) (simp_all only: quot_equality)
bauerg@10499
   196
wenzelm@10250
   197
end