src/HOL/Library/Quotient_Type.thy
author huffman
Fri Mar 30 12:32:35 2012 +0200 (2012-03-30)
changeset 47220 52426c62b5d0
parent 45694 4a8743618257
child 49834 b27bbb021df1
permissions -rw-r--r--
replace lemmas eval_nat_numeral with a simpler reformulation
wenzelm@35100
     1
(*  Title:      HOL/Library/Quotient_Type.thy
wenzelm@10483
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@10250
     3
*)
wenzelm@10250
     4
wenzelm@14706
     5
header {* Quotient types *}
wenzelm@10250
     6
wenzelm@35100
     7
theory Quotient_Type
haftmann@30738
     8
imports Main
nipkow@15131
     9
begin
wenzelm@10250
    10
wenzelm@10250
    11
text {*
wenzelm@10285
    12
 We introduce the notion of quotient types over equivalence relations
haftmann@22390
    13
 via 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
haftmann@29608
    23
class eqv =
haftmann@25062
    24
  fixes eqv :: "'a \<Rightarrow> 'a \<Rightarrow> bool"    (infixl "\<sim>" 50)
wenzelm@10250
    25
haftmann@22390
    26
class equiv = eqv +
haftmann@25062
    27
  assumes equiv_refl [intro]: "x \<sim> x"
haftmann@25062
    28
  assumes equiv_trans [trans]: "x \<sim> y \<Longrightarrow> y \<sim> z \<Longrightarrow> x \<sim> z"
haftmann@25062
    29
  assumes equiv_sym [sym]: "x \<sim> y \<Longrightarrow> y \<sim> x"
wenzelm@10250
    30
wenzelm@12371
    31
lemma equiv_not_sym [sym]: "\<not> (x \<sim> y) ==> \<not> (y \<sim> (x::'a::equiv))"
wenzelm@10477
    32
proof -
wenzelm@23373
    33
  assume "\<not> (x \<sim> y)" then show "\<not> (y \<sim> x)"
wenzelm@10477
    34
    by (rule contrapos_nn) (rule equiv_sym)
wenzelm@10477
    35
qed
wenzelm@10477
    36
wenzelm@10477
    37
lemma not_equiv_trans1 [trans]: "\<not> (x \<sim> y) ==> y \<sim> z ==> \<not> (x \<sim> (z::'a::equiv))"
wenzelm@10477
    38
proof -
wenzelm@23373
    39
  assume "\<not> (x \<sim> y)" and "y \<sim> z"
wenzelm@10477
    40
  show "\<not> (x \<sim> z)"
wenzelm@10477
    41
  proof
wenzelm@10477
    42
    assume "x \<sim> z"
wenzelm@23373
    43
    also from `y \<sim> z` have "z \<sim> y" ..
wenzelm@10477
    44
    finally have "x \<sim> y" .
wenzelm@23373
    45
    with `\<not> (x \<sim> y)` show False by contradiction
wenzelm@10477
    46
  qed
wenzelm@10477
    47
qed
wenzelm@10477
    48
wenzelm@10477
    49
lemma not_equiv_trans2 [trans]: "x \<sim> y ==> \<not> (y \<sim> z) ==> \<not> (x \<sim> (z::'a::equiv))"
wenzelm@10477
    50
proof -
wenzelm@23373
    51
  assume "\<not> (y \<sim> z)" then have "\<not> (z \<sim> y)" ..
wenzelm@23373
    52
  also assume "x \<sim> y" then have "y \<sim> x" ..
wenzelm@23373
    53
  finally have "\<not> (z \<sim> x)" . then show "(\<not> x \<sim> z)" ..
wenzelm@10477
    54
qed
wenzelm@10477
    55
wenzelm@10250
    56
text {*
wenzelm@10285
    57
 \medskip The quotient type @{text "'a quot"} consists of all
wenzelm@10285
    58
 \emph{equivalence classes} over elements of the base type @{typ 'a}.
wenzelm@10250
    59
*}
wenzelm@10250
    60
wenzelm@45694
    61
definition "quot = {{x. a \<sim> x} | a::'a::eqv. True}"
wenzelm@45694
    62
wenzelm@45694
    63
typedef (open) 'a quot = "quot :: 'a::eqv set set"
wenzelm@45694
    64
  unfolding quot_def by blast
wenzelm@10250
    65
wenzelm@10250
    66
lemma quotI [intro]: "{x. a \<sim> x} \<in> quot"
wenzelm@18730
    67
  unfolding quot_def by blast
wenzelm@10250
    68
wenzelm@10250
    69
lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"
wenzelm@18730
    70
  unfolding quot_def by 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@19086
    77
definition
wenzelm@21404
    78
  "class" :: "'a::equiv => 'a quot"  ("\<lfloor>_\<rfloor>") where
wenzelm@19086
    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@23373
    84
  assume "R \<in> quot" then have "\<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@23373
    86
  then show ?thesis unfolding 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@18730
    90
  using quot_exhaust by 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@23373
   109
    then have "b \<sim> a" by blast
wenzelm@23373
   110
    then show ?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@23373
   129
    then show ?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@19086
   136
definition
wenzelm@21404
   137
  pick :: "'a::equiv quot => 'a" where
wenzelm@19086
   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@23373
   146
    then have "a \<sim> x" .. then show "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@23373
   153
  then have "pick A \<sim> a" by (simp only: pick_equiv)
wenzelm@23373
   154
  then have "\<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@18372
   166
  assumes eq: "!!X Y. P X Y ==> f X Y == g (pick X) (pick Y)"
wenzelm@18372
   167
    and cong: "!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor>
wenzelm@18372
   168
      ==> P \<lfloor>x\<rfloor> \<lfloor>y\<rfloor> ==> P \<lfloor>x'\<rfloor> \<lfloor>y'\<rfloor> ==> g x y = g x' y'"
wenzelm@18372
   169
    and P: "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>"
wenzelm@18372
   170
  shows "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10473
   171
proof -
wenzelm@18372
   172
  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
   173
  also have "... = g a b"
wenzelm@10491
   174
  proof (rule cong)
wenzelm@10483
   175
    show "\<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10483
   176
    moreover
wenzelm@10483
   177
    show "\<lfloor>pick \<lfloor>b\<rfloor>\<rfloor> = \<lfloor>b\<rfloor>" ..
wenzelm@10491
   178
    moreover
wenzelm@23373
   179
    show "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>" by (rule P)
wenzelm@10491
   180
    ultimately show "P \<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> \<lfloor>pick \<lfloor>b\<rfloor>\<rfloor>" by (simp only:)
wenzelm@10285
   181
  qed
wenzelm@10285
   182
  finally show ?thesis .
wenzelm@10285
   183
qed
wenzelm@10285
   184
wenzelm@10483
   185
theorem quot_function:
wenzelm@18372
   186
  assumes "!!X Y. f X Y == g (pick X) (pick Y)"
wenzelm@18372
   187
    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
   188
  shows "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@23394
   189
  using assms and TrueI
wenzelm@18372
   190
  by (rule quot_cond_function)
wenzelm@10285
   191
bauerg@10499
   192
theorem quot_function':
bauerg@10499
   193
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
bauerg@10499
   194
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y = g x' y') ==>
bauerg@10499
   195
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@18372
   196
  by (rule quot_function) (simp_all only: quot_equality)
bauerg@10499
   197
wenzelm@10250
   198
end