idris2-profunctors/Data/Profunctor/Yoneda.idr

166 lines
4.7 KiB
Idris
Raw Normal View History

2023-03-06 21:42:52 -05:00
module Data.Profunctor.Yoneda
import Data.Profunctor
2023-03-06 22:47:03 -05:00
import Data.Profunctor.Costrong
2023-03-06 22:05:45 -05:00
import Data.Profunctor.Traversing
import Data.Profunctor.Mapping
import Data.Profunctor.Sieve
2023-03-06 21:42:52 -05:00
%default total
2023-03-07 22:15:08 -05:00
------------------------------------------------------------------------------
-- Yoneda
------------------------------------------------------------------------------
||| The cofree profunctor given a data constructor with two type parameters.
2023-03-06 21:42:52 -05:00
public export
record Yoneda p a b where
constructor MkYoneda
runYoneda : forall x, y. (x -> a) -> (b -> y) -> p x y
2023-03-06 22:05:33 -05:00
public export
2023-03-06 22:05:45 -05:00
Profunctor (Yoneda p) where
lmap f (MkYoneda p) = MkYoneda $ \l,r => p (f . l) r
rmap f (MkYoneda p) = MkYoneda $ \l,r => p l (r . f)
dimap f g (MkYoneda p) = MkYoneda $ \l,r => p (f . l) (r . g)
public export
2023-03-06 22:05:45 -05:00
ProfunctorFunctor Yoneda where
promap f (MkYoneda p) = MkYoneda $ f .: p
public export
2023-03-06 22:05:45 -05:00
ProfunctorMonad Yoneda where
propure p = MkYoneda $ \l,r => dimap l r p
projoin (MkYoneda p) = p id id
public export
2023-03-06 22:05:45 -05:00
ProfunctorComonad Yoneda where
proextract (MkYoneda p) = p id id
produplicate p = MkYoneda $ \l,r => dimap l r p
2023-03-07 22:15:08 -05:00
||| A witness that `Yoneda p` and `p` are equivalent when `p` is a profunctor.
public export
2023-03-06 22:05:45 -05:00
yonedaEqv : Profunctor p => p a b <=> Yoneda p a b
yonedaEqv = MkEquivalence propure proextract
public export
2023-03-07 22:32:10 -05:00
yonedaIso : (Profunctor q, Profunctor r) => forall p. Profunctor p =>
p (q a b) (r a' b') -> p (Yoneda q a b) (Yoneda r a' b')
yonedaIso = dimap proextract propure
public export
2023-03-06 22:05:45 -05:00
Functor (Yoneda p a) where
map = rmap
public export
2023-03-06 22:05:45 -05:00
GenStrong ten p => GenStrong ten (Yoneda p) where
strongl = propure . strongl {ten,p} . proextract
strongr = propure . strongr {ten,p} . proextract
public export
2023-03-06 22:47:03 -05:00
GenCostrong ten p => GenCostrong ten (Yoneda p) where
costrongl = propure . costrongl {ten,p} . proextract
costrongr = propure . costrongr {ten,p} . proextract
public export
2023-03-06 22:05:45 -05:00
Closed p => Closed (Yoneda p) where
closed = propure . closed . proextract
public export
2023-03-06 22:05:45 -05:00
Traversing p => Traversing (Yoneda p) where
traverse' = propure . traverse' . proextract
wander f = propure . wander f . proextract
public export
2023-03-06 22:05:45 -05:00
Mapping p => Mapping (Yoneda p) where
map' = propure . map' . proextract
roam f = propure . roam f . proextract
public export
2023-03-06 22:05:45 -05:00
Sieve p f => Sieve (Yoneda p) f where
sieve = sieve . proextract
public export
2023-03-06 22:05:45 -05:00
Cosieve p f => Cosieve (Yoneda p) f where
cosieve = cosieve . proextract
2023-03-06 22:05:33 -05:00
2023-03-07 22:15:08 -05:00
------------------------------------------------------------------------------
-- Coyoneda
------------------------------------------------------------------------------
||| The free profunctor given a data constructor with two type parameters.
2023-03-06 22:05:33 -05:00
public export
data Coyoneda : (p : Type -> Type -> Type) -> Type -> Type -> Type where
MkCoyoneda : (a -> x) -> (y -> b) -> p x y -> Coyoneda p a b
2023-03-06 22:05:45 -05:00
public export
2023-03-06 22:05:45 -05:00
Profunctor (Coyoneda p) where
lmap f (MkCoyoneda l r p) = MkCoyoneda (l . f) r p
rmap f (MkCoyoneda l r p) = MkCoyoneda l (f . r) p
dimap f g (MkCoyoneda l r p) = MkCoyoneda (l . f) (g . r) p
public export
2023-03-06 22:05:45 -05:00
ProfunctorFunctor Coyoneda where
promap f (MkCoyoneda l r p) = MkCoyoneda l r (f p)
public export
2023-03-06 22:05:45 -05:00
ProfunctorMonad Coyoneda where
propure = MkCoyoneda id id
projoin (MkCoyoneda l r p) = dimap l r p
public export
2023-03-06 22:05:45 -05:00
ProfunctorComonad Coyoneda where
proextract (MkCoyoneda l r p) = dimap l r p
produplicate = MkCoyoneda id id
2023-03-07 22:15:08 -05:00
||| A witness that `Coyoneda p` and `p` are equivalent when `p` is a profunctor.
public export
2023-03-06 22:05:45 -05:00
coyonedaEqv : Profunctor p => p a b <=> Coyoneda p a b
coyonedaEqv = MkEquivalence propure proextract
public export
2023-03-07 22:32:10 -05:00
coyonedaIso : (Profunctor q, Profunctor r) => forall p. Profunctor p =>
p (q a b) (r a' b') -> p (Coyoneda q a b) (Coyoneda r a' b')
coyonedaIso = dimap proextract propure
public export
2023-03-06 22:05:45 -05:00
Functor (Coyoneda p a) where
map = rmap
public export
2023-03-06 22:05:45 -05:00
GenStrong ten p => GenStrong ten (Coyoneda p) where
strongl = propure . strongl {ten,p} . proextract
strongr = propure . strongr {ten,p} . proextract
public export
2023-03-06 22:47:03 -05:00
GenCostrong ten p => GenCostrong ten (Coyoneda p) where
costrongl = propure . costrongl {ten,p} . proextract
costrongr = propure . costrongr {ten,p} . proextract
public export
2023-03-06 22:05:45 -05:00
Closed p => Closed (Coyoneda p) where
closed = propure . closed . proextract
public export
2023-03-06 22:05:45 -05:00
Traversing p => Traversing (Coyoneda p) where
traverse' = propure . traverse' . proextract
wander f = propure . wander f . proextract
public export
2023-03-06 22:05:45 -05:00
Mapping p => Mapping (Coyoneda p) where
map' = propure . map' . proextract
roam f = propure . roam f . proextract
public export
2023-03-06 22:05:45 -05:00
Sieve p f => Sieve (Coyoneda p) f where
sieve = sieve . proextract
public export
2023-03-06 22:05:45 -05:00
Cosieve p f => Cosieve (Coyoneda p) f where
cosieve = cosieve . proextract