(* Title: HOL/Library/Quotient.thy
ID: $Id$
Author: Gertrud Bauer and Markus Wenzel, TU Muenchen
*)
header {*
\title{Quotients}
\author{Gertrud Bauer and Markus Wenzel}
*}
theory Quotient = Main:
text {*
We introduce the notion of quotient types over equivalence relations
via axiomatic type classes.
*}
subsection {* Equivalence relations and quotient types *}
text {*
\medskip Type class @{text equiv} models equivalence relations @{text
"\ :: 'a => 'a => bool"}.
*}
axclass eqv < "term"
consts
eqv :: "('a::eqv) => 'a => bool" (infixl "\" 50)
axclass equiv < eqv
equiv_refl [intro]: "x \ x"
equiv_trans [trans]: "x \ y ==> y \ z ==> x \ z"
equiv_sym [elim?]: "x \ y ==> y \ x"
text {*
\medskip The quotient type @{text "'a quot"} consists of all
\emph{equivalence classes} over elements of the base type @{typ 'a}.
*}
typedef 'a quot = "{{x. a \ x}| a::'a::eqv. True}"
by blast
lemma quotI [intro]: "{x. a \ x} \ quot"
by (unfold quot_def) blast
lemma quotE [elim]: "R \ quot ==> (!!a. R = {x. a \ x} ==> C) ==> C"
by (unfold quot_def) blast
text {*
\medskip Abstracted equivalence classes are the canonical
representation of elements of a quotient type.
*}
constdefs
equivalence_class :: "'a::equiv => 'a quot" ("\_\")
"\a\ == Abs_quot {x. a \ x}"
theorem quot_exhaust: "\a. A = \a\"
proof (cases A)
fix R assume R: "A = Abs_quot R"
assume "R \ quot" hence "\a. R = {x. a \ x}" by blast
with R have "\a. A = Abs_quot {x. a \ x}" by blast
thus ?thesis by (unfold equivalence_class_def)
qed
lemma quot_cases [cases type: quot]: "(!!a. A = \a\ ==> C) ==> C"
by (insert quot_exhaust) blast
subsection {* Equality on quotients *}
text {*
Equality of canonical quotient elements coincides with the original
relation.
*}
theorem equivalence_class_eq [iff?]: "(\a\ = \b\) = (a \ b)"
proof
assume eq: "\a\ = \b\"
show "a \ b"
proof -
from eq have "{x. a \ x} = {x. b \ x}"
by (simp only: equivalence_class_def Abs_quot_inject quotI)
moreover have "a \ a" ..
ultimately have "a \ {x. b \ x}" by blast
hence "b \ a" by blast
thus ?thesis ..
qed
next
assume ab: "a \ b"
show "\a\ = \b\"
proof -
have "{x. a \ x} = {x. b \ x}"
proof (rule Collect_cong)
fix x show "(a \ x) = (b \ x)"
proof
from ab have "b \ a" ..
also assume "a \ x"
finally show "b \ x" .
next
note ab
also assume "b \ x"
finally show "a \ x" .
qed
qed
thus ?thesis by (simp only: equivalence_class_def)
qed
qed
subsection {* Picking representing elements *}
constdefs
pick :: "'a::equiv quot => 'a"
"pick A == SOME a. A = \a\"
theorem pick_equiv [intro]: "pick \a\ \ a"
proof (unfold pick_def)
show "(SOME x. \a\ = \x\) \ a"
proof (rule someI2)
show "\a\ = \a\" ..
fix x assume "\a\ = \x\"
hence "a \ x" .. thus "x \ a" ..
qed
qed
theorem pick_inverse: "\pick A\ = A"
proof (cases A)
fix a assume a: "A = \a\"
hence "pick A \ a" by (simp only: pick_equiv)
hence "\pick A\ = \a\" ..
with a show ?thesis by simp
qed
text {*
\medskip The following rules support canonical function definitions
on quotient types.
*}
theorem cong_definition1:
"(!!X. f X == g (pick X)) ==>
(!!x x'. x \ x' ==> g x = g x') ==>
f \a\ = g a"
proof -
assume cong: "!!x x'. x \ x' ==> g x = g x'"
assume "!!X. f X == g (pick X)"
hence "f \a\ = g (pick \a\)" by (simp only:)
also have "\ = g a"
proof (rule cong)
show "pick \a\ \ a" ..
qed
finally show ?thesis .
qed
theorem cong_definition2:
"(!!X Y. f X Y == g (pick X) (pick Y)) ==>
(!!x x' y y'. x \ x' ==> y \ y' ==> g x y = g x' y') ==>
f \a\ \b\ = g a b"
proof -
assume cong: "!!x x' y y'. x \ x' ==> y \ y' ==> g x y = g x' y'"
assume "!!X Y. f X Y == g (pick X) (pick Y)"
hence "f \a\ \b\ = g (pick \a\) (pick \b\)" by (simp only:)
also have "\ = g a b"
proof (rule cong)
show "pick \a\ \ a" ..
show "pick \b\ \ b" ..
qed
finally show ?thesis .
qed
end