Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code for generating and working with quotients #43

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Quotient/Quotient.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Quotient

import Control.Isomorphism

%access public export
%default total

extEq : (a -> b) -> (a -> b) -> Type
extEq {a} f g = (x : a) -> f x = g x

Rel : Type -> Type
Rel x = x -> x -> Type

record EqRel (x : Type) where
constructor MkEqRel
rel : Rel x
refl : (a : x) -> rel a a
sym : (a, b : x) -> rel a b -> rel b a
trans : (a, b, c : x) -> rel a b -> rel b c -> rel a c

RespectingMap : (x, y : Type) -> EqRel x -> Type
RespectingMap x y eq = (f : (x -> y) ** ((a, b : x) -> (rel eq) a b -> f a = f b))

record Quotient (x : Type) (eq : EqRel x) where
constructor MkQuotient
carrier : Type
proj : RespectingMap x carrier eq
exists : (y : Type) -> (f : RespectingMap x y eq)
-> (g : (carrier -> y) ** (extEq (fst f) (g . (fst proj))))
unique : (y : Type) -> (f : RespectingMap x y eq)
-> (g : (carrier -> y)) -> extEq (fst f) (g . (fst proj))
-> extEq g (fst (exists y f))

existsUnique : (q : Quotient x eq) -> (f : RespectingMap x y eq)
-> (g : carrier q -> y) -> (extEq (fst f) (g . (fst $ proj q)))
-> (h : carrier q -> y) -> (extEq (fst f) (h . (fst $ proj q)))
-> extEq g h
existsUnique {y=y} (MkQuotient carrier proj exists unique) f g gh h hh x =
trans (unique y f g gh x) $ sym $ unique y f h hh x

projectionInducesIdentity : (q : Quotient x eq) -> (f : carrier q -> carrier q) -> extEq (fst $ proj q) (f . (fst $ proj q)) -> extEq f Basics.id
projectionInducesIdentity q f h x = sym $ existsUnique q (proj q) id (\a => Refl) f h x

QuotientUnique : (q, q' : Quotient x eq)
-> (iso : Iso (carrier q) (carrier q') ** (extEq ((to iso) . (fst $ proj q)) (fst $ proj q')))
QuotientUnique q q' = let
(isoTo ** commTo) = exists q (carrier q') (proj q')
(isoFrom ** commFrom) = exists q' (carrier q) (proj q)
iso = MkIso isoTo isoFrom
(projectionInducesIdentity q' (isoTo . isoFrom) (\a => trans (commTo a) (cong $ commFrom a)))
(projectionInducesIdentity q (isoFrom . isoTo) (\a => trans (commFrom a) (cong $ commTo a)))
in (iso ** (\a => sym $ commTo a))

21 changes: 21 additions & 0 deletions src/Quotient/Quotients.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Quotients

import Quotient.Quotient

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this module use %access public export?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, forgot that.

trivialEqRel : (x : Type) -> EqRel x
trivialEqRel x = MkEqRel (\x, y => x = y) (\x => Refl) (\x, y, r => sym r) (\x, y, z, l, r => trans l r)

trivialQuotient : (x : Type) -> Quotient x $ trivialEqRel x
trivialQuotient x = MkQuotient x
((id {a=x}) ** (\a, b, h => h))
(\y, f => ((fst f) ** (\a => Refl)))
(\y, f, g, h, a => sym $ h a)

fullEqRel : (x : Type) -> EqRel x
fullEqRel x = MkEqRel (\x, y => ()) (\x => ()) (\x, y, r => ()) (\x, y, z, l, r => ())

fullQuotient : (x : Type) -> (a : x) -> Quotient x $ fullEqRel x
fullQuotient x a = MkQuotient ()
((\b => ()) ** (\a, b, h => Refl))
(\y, f => ((\b => (fst f) a) ** (\b => snd f b a ())))
(\y, f, g, h, () => sym $ h a)
32 changes: 32 additions & 0 deletions src/Quotient/UnsafeQuotient.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module UnsafeQuotient

import Quotient.Quotient

%default total
%access export

private
data InternalQuotientType : (x : Type) -> (eq : EqRel x) -> Type where
InternalWrap : x -> InternalQuotientType x eq

QuotientType : (x : Type) -> (eq : EqRel x) -> Type
QuotientType = InternalQuotientType

Wrap : x -> QuotientType x eq
Wrap = InternalWrap

unwrap : QuotientType x eq -> x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably best not to export unwrap, as it corresponds to a form of global axiom of choice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, completely missed that.

unwrap (InternalWrap a) = a

wrapUnwrapId : (a : QuotientType x eq) -> a = Wrap (unwrap a)
wrapUnwrapId (InternalWrap a) = Refl

postulate
QuotientEquality : (x : Type) -> (eq : EqRel x) -> (rel eq a b) -> Wrap a = Wrap b

UnsafeQuotient : (x : Type) -> (eq : EqRel x) -> Quotient x eq
UnsafeQuotient x eq = MkQuotient
(QuotientType x eq)
(Wrap ** (\a, b, h => QuotientEquality x eq h))
(\y, f => ((\a => fst f $ unwrap a) ** (\a => Refl)))
(\y, f, g, h, a => trans (cong $ wrapUnwrapId a) (sym $ h $ unwrap a))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(\y, f, g, h, a => trans (cong $ wrapUnwrapId a) (sym $ h $ unwrap a))
(\y, f, g, h, (InternalWrap a) => sym $ h a)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find these things very weird. In the previous line where I use unwrap, I cannot pattern match in the lambda, while in this line I can. The only reason why unwrap exists is because it didn't work there for me. Do you or @clayrat have any insight as to what might be the issue there?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the dependent pair, do you mean that it would be nicer if we didn't have to write fst $ exists ...? In that case, I'd just define another function.

Concretely, I suggest to replace the exists field with the following two fields (possibly with better chosen names):

  extension : (y : Type) -> (f : RespectingMap x y eq)
           -> carrier -> y
  commutes : (y : Type) -> (f : RespectingMap x y eq)
          -> extEq (fst f) (extension y f . (fst proj))

Why?

  1. Most of the time, the function extension y f is probably what you want easy access to (what I tried to say above).
  2. Should make it easier to build things stepwise using holes, because Idris doesn't need to juggle sigma types and first projections etc.
  3. Might make it easier to talk about the code, e.g. 'the commuting triangle in exists' above is the commutes field.

I'm not sure what you mean by 'the commuting triangle in exists would commute pointwise'.

What I meant was that in the Quotients.Unsafe module, the proof of commutes y f a is Refl for every a, which is nice, but outside this module, nothing reduces anymore.