src/HOL/Library/Quotient_Type.thy
author bulwahn
Fri Apr 08 16:31:14 2011 +0200 (2011-04-08)
changeset 42316 12635bb655fd
parent 35100 53754ec7360b
child 45694 4a8743618257
permissions -rw-r--r--
deactivating other compilations in quickcheck_exhaustive momentarily that only interesting for my benchmarks and experiments
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@10392
    61
typedef 'a quot = "{{x. a \<sim> x} | a::'a::eqv. True}"
wenzelm@10250
    62
  by blast
wenzelm@10250
    63
wenzelm@10250
    64
lemma quotI [intro]: "{x. a \<sim> x} \<in> quot"
wenzelm@18730
    65
  unfolding quot_def by blast
wenzelm@10250
    66
wenzelm@10250
    67
lemma quotE [elim]: "R \<in> quot ==> (!!a. R = {x. a \<sim> x} ==> C) ==> C"
wenzelm@18730
    68
  unfolding quot_def by blast
wenzelm@10250
    69
wenzelm@10250
    70
text {*
wenzelm@10250
    71
 \medskip Abstracted equivalence classes are the canonical
wenzelm@10250
    72
 representation of elements of a quotient type.
wenzelm@10250
    73
*}
wenzelm@10250
    74
wenzelm@19086
    75
definition
wenzelm@21404
    76
  "class" :: "'a::equiv => 'a quot"  ("\<lfloor>_\<rfloor>") where
wenzelm@19086
    77
  "\<lfloor>a\<rfloor> = Abs_quot {x. a \<sim> x}"
wenzelm@10250
    78
wenzelm@10311
    79
theorem quot_exhaust: "\<exists>a. A = \<lfloor>a\<rfloor>"
wenzelm@10278
    80
proof (cases A)
wenzelm@10278
    81
  fix R assume R: "A = Abs_quot R"
wenzelm@23373
    82
  assume "R \<in> quot" then have "\<exists>a. R = {x. a \<sim> x}" by blast
wenzelm@10278
    83
  with R have "\<exists>a. A = Abs_quot {x. a \<sim> x}" by blast
wenzelm@23373
    84
  then show ?thesis unfolding class_def .
wenzelm@10250
    85
qed
wenzelm@10250
    86
wenzelm@10311
    87
lemma quot_cases [cases type: quot]: "(!!a. A = \<lfloor>a\<rfloor> ==> C) ==> C"
wenzelm@18730
    88
  using quot_exhaust by blast
wenzelm@10250
    89
wenzelm@10250
    90
wenzelm@10285
    91
subsection {* Equality on quotients *}
wenzelm@10250
    92
wenzelm@10250
    93
text {*
wenzelm@10286
    94
 Equality of canonical quotient elements coincides with the original
wenzelm@10286
    95
 relation.
wenzelm@10250
    96
*}
wenzelm@10250
    97
wenzelm@12371
    98
theorem quot_equality [iff?]: "(\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>) = (a \<sim> b)"
wenzelm@10285
    99
proof
wenzelm@10285
   100
  assume eq: "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10285
   101
  show "a \<sim> b"
wenzelm@10285
   102
  proof -
wenzelm@10285
   103
    from eq have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10551
   104
      by (simp only: class_def Abs_quot_inject quotI)
wenzelm@10285
   105
    moreover have "a \<sim> a" ..
wenzelm@10285
   106
    ultimately have "a \<in> {x. b \<sim> x}" by blast
wenzelm@23373
   107
    then have "b \<sim> a" by blast
wenzelm@23373
   108
    then show ?thesis ..
wenzelm@10285
   109
  qed
wenzelm@10285
   110
next
wenzelm@10250
   111
  assume ab: "a \<sim> b"
wenzelm@10285
   112
  show "\<lfloor>a\<rfloor> = \<lfloor>b\<rfloor>"
wenzelm@10285
   113
  proof -
wenzelm@10285
   114
    have "{x. a \<sim> x} = {x. b \<sim> x}"
wenzelm@10285
   115
    proof (rule Collect_cong)
wenzelm@10285
   116
      fix x show "(a \<sim> x) = (b \<sim> x)"
wenzelm@10285
   117
      proof
wenzelm@10285
   118
        from ab have "b \<sim> a" ..
wenzelm@10285
   119
        also assume "a \<sim> x"
wenzelm@10285
   120
        finally show "b \<sim> x" .
wenzelm@10285
   121
      next
wenzelm@10285
   122
        note ab
wenzelm@10285
   123
        also assume "b \<sim> x"
wenzelm@10285
   124
        finally show "a \<sim> x" .
wenzelm@10285
   125
      qed
wenzelm@10250
   126
    qed
wenzelm@23373
   127
    then show ?thesis by (simp only: class_def)
wenzelm@10250
   128
  qed
wenzelm@10250
   129
qed
wenzelm@10250
   130
wenzelm@10250
   131
wenzelm@10285
   132
subsection {* Picking representing elements *}
wenzelm@10250
   133
wenzelm@19086
   134
definition
wenzelm@21404
   135
  pick :: "'a::equiv quot => 'a" where
wenzelm@19086
   136
  "pick A = (SOME a. A = \<lfloor>a\<rfloor>)"
wenzelm@10250
   137
wenzelm@10285
   138
theorem pick_equiv [intro]: "pick \<lfloor>a\<rfloor> \<sim> a"
wenzelm@10250
   139
proof (unfold pick_def)
wenzelm@10250
   140
  show "(SOME x. \<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>) \<sim> a"
wenzelm@10250
   141
  proof (rule someI2)
wenzelm@10250
   142
    show "\<lfloor>a\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10250
   143
    fix x assume "\<lfloor>a\<rfloor> = \<lfloor>x\<rfloor>"
wenzelm@23373
   144
    then have "a \<sim> x" .. then show "x \<sim> a" ..
wenzelm@10250
   145
  qed
wenzelm@10250
   146
qed
wenzelm@10250
   147
wenzelm@10483
   148
theorem pick_inverse [intro]: "\<lfloor>pick A\<rfloor> = A"
wenzelm@10250
   149
proof (cases A)
wenzelm@10250
   150
  fix a assume a: "A = \<lfloor>a\<rfloor>"
wenzelm@23373
   151
  then have "pick A \<sim> a" by (simp only: pick_equiv)
wenzelm@23373
   152
  then have "\<lfloor>pick A\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10250
   153
  with a show ?thesis by simp
wenzelm@10250
   154
qed
wenzelm@10250
   155
wenzelm@10285
   156
text {*
wenzelm@10285
   157
 \medskip The following rules support canonical function definitions
wenzelm@10483
   158
 on quotient types (with up to two arguments).  Note that the
wenzelm@10483
   159
 stripped-down version without additional conditions is sufficient
wenzelm@10483
   160
 most of the time.
wenzelm@10285
   161
*}
wenzelm@10285
   162
wenzelm@10483
   163
theorem quot_cond_function:
wenzelm@18372
   164
  assumes eq: "!!X Y. P X Y ==> f X Y == g (pick X) (pick Y)"
wenzelm@18372
   165
    and cong: "!!x x' y y'. \<lfloor>x\<rfloor> = \<lfloor>x'\<rfloor> ==> \<lfloor>y\<rfloor> = \<lfloor>y'\<rfloor>
wenzelm@18372
   166
      ==> P \<lfloor>x\<rfloor> \<lfloor>y\<rfloor> ==> P \<lfloor>x'\<rfloor> \<lfloor>y'\<rfloor> ==> g x y = g x' y'"
wenzelm@18372
   167
    and P: "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>"
wenzelm@18372
   168
  shows "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@10473
   169
proof -
wenzelm@18372
   170
  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
   171
  also have "... = g a b"
wenzelm@10491
   172
  proof (rule cong)
wenzelm@10483
   173
    show "\<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> = \<lfloor>a\<rfloor>" ..
wenzelm@10483
   174
    moreover
wenzelm@10483
   175
    show "\<lfloor>pick \<lfloor>b\<rfloor>\<rfloor> = \<lfloor>b\<rfloor>" ..
wenzelm@10491
   176
    moreover
wenzelm@23373
   177
    show "P \<lfloor>a\<rfloor> \<lfloor>b\<rfloor>" by (rule P)
wenzelm@10491
   178
    ultimately show "P \<lfloor>pick \<lfloor>a\<rfloor>\<rfloor> \<lfloor>pick \<lfloor>b\<rfloor>\<rfloor>" by (simp only:)
wenzelm@10285
   179
  qed
wenzelm@10285
   180
  finally show ?thesis .
wenzelm@10285
   181
qed
wenzelm@10285
   182
wenzelm@10483
   183
theorem quot_function:
wenzelm@18372
   184
  assumes "!!X Y. f X Y == g (pick X) (pick Y)"
wenzelm@18372
   185
    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
   186
  shows "f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@23394
   187
  using assms and TrueI
wenzelm@18372
   188
  by (rule quot_cond_function)
wenzelm@10285
   189
bauerg@10499
   190
theorem quot_function':
bauerg@10499
   191
  "(!!X Y. f X Y == g (pick X) (pick Y)) ==>
bauerg@10499
   192
    (!!x x' y y'. x \<sim> x' ==> y \<sim> y' ==> g x y = g x' y') ==>
bauerg@10499
   193
    f \<lfloor>a\<rfloor> \<lfloor>b\<rfloor> = g a b"
wenzelm@18372
   194
  by (rule quot_function) (simp_all only: quot_equality)
bauerg@10499
   195
wenzelm@10250
   196
end