src/HOL/Library/Quotient.thy
author wenzelm
Thu May 06 14:14:18 2004 +0200 (2004-05-06)
changeset 14706 71590b7733b7
parent 12371 80ca9058db95
child 14981 e73f8140af78
permissions -rw-r--r--
tuned document;
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@14706
     7
header {* Quotient types *}
wenzelm@10250
     8
wenzelm@10250
     9
theory Quotient = Main:
wenzelm@10250
    10
wenzelm@10250
    11
text {*
wenzelm@10285
    12
 We introduce the notion of quotient types over equivalence relations
wenzelm@10285
    13
 via axiomatic type classes.
wenzelm@10250
    14
*}
wenzelm@10250
    15
wenzelm@10285
    16
subsection {* Equivalence relations and quotient types *}
wenzelm@10250
    17
wenzelm@10250
    18
text {*
wenzelm@10390
    19
 \medskip Type class @{text equiv} models equivalence relations @{text
wenzelm@10390
    20
 "\<sim> :: 'a => 'a => bool"}.
wenzelm@10250
    21
*}
wenzelm@10250
    22
wenzelm@12338
    23
axclass eqv \<subseteq> type
wenzelm@10285
    24
consts
wenzelm@10285
    25
  eqv :: "('a::eqv) => 'a => bool"    (infixl "\<sim>" 50)
wenzelm@10250
    26
wenzelm@11099
    27
axclass equiv \<subseteq> eqv
wenzelm@10333
    28
  equiv_refl [intro]: "x \<sim> x"
wenzelm@10333
    29
  equiv_trans [trans]: "x \<sim> y ==> y \<sim> z ==> x \<sim> z"
wenzelm@12371
    30
  equiv_sym [sym]: "x \<sim> y ==> y \<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@10250
    66
  by (unfold quot_def) blast
wenzelm@10250
    67
wenzelm@10250
    68
lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"
wenzelm@10250
    69
  by (unfold quot_def) 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@10250
    76
constdefs
wenzelm@10551
    77
  class :: "'a::equiv => 'a quot"    ("\<lfloor>_\<rfloor>")
wenzelm@10250
    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@10551
    85
  thus ?thesis by (unfold 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@10311
    89
  by (insert quot_exhaust) 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@10250
   135
constdefs
wenzelm@10285
   136
  pick :: "'a::equiv quot => 'a"
wenzelm@10250
   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@10491
   165
  "(!!X Y. P X Y ==> f X Y == g (pick X) (pick Y)) ==>
wenzelm@10491
   166
    (!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor>
wenzelm@10491
   167
      ==> P \<lfloor>x\<rfloor> \<lfloor>y\<rfloor> ==> P \<lfloor>x'\<rfloor> \<lfloor>y'\<rfloor> ==> g x y = g x' y') ==>
wenzelm@10491
   168
    P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> ==> f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10491
   169
  (is "PROP ?eq ==> PROP ?cong ==> _ ==> _")
wenzelm@10473
   170
proof -
wenzelm@10491
   171
  assume cong: "PROP ?cong"
wenzelm@10491
   172
  assume "PROP ?eq" and "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>"
wenzelm@10491
   173
  hence "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g (pick \<lfloor>a\<rfloor>) (pick \<lfloor>b\<rfloor>)" by (simp only:)
wenzelm@10505
   174
  also have "... = g a b"
wenzelm@10491
   175
  proof (rule cong)
wenzelm@10483
   176
    show "\<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10483
   177
    moreover
wenzelm@10483
   178
    show "\<lfloor>pick \<lfloor>b\<rfloor>\<rfloor> = \<lfloor>b\<rfloor>" ..
wenzelm@10491
   179
    moreover
wenzelm@10491
   180
    show "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>" .
wenzelm@10491
   181
    ultimately show "P \<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> \<lfloor>pick \<lfloor>b\<rfloor>\<rfloor>" by (simp only:)
wenzelm@10285
   182
  qed
wenzelm@10285
   183
  finally show ?thesis .
wenzelm@10285
   184
qed
wenzelm@10285
   185
wenzelm@10483
   186
theorem quot_function:
wenzelm@10473
   187
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
wenzelm@10483
   188
    (!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor> ==> g x y = g x' y') ==>
wenzelm@10473
   189
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10473
   190
proof -
wenzelm@11549
   191
  case rule_context from this TrueI
wenzelm@10483
   192
  show ?thesis by (rule quot_cond_function)
wenzelm@10285
   193
qed
wenzelm@10285
   194
bauerg@10499
   195
theorem quot_function':
bauerg@10499
   196
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
bauerg@10499
   197
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y = g x' y') ==>
bauerg@10499
   198
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
bauerg@10499
   199
  by  (rule quot_function) (simp only: quot_equality)+
bauerg@10499
   200
wenzelm@10250
   201
end