Compare commits

...

10 commits

12 changed files with 389 additions and 214 deletions

View file

@ -18,50 +18,50 @@ record Cayley {0 k1,k2,k3 : Type} f (p : k1 -> k2 -> k3) a b where
runCayley : f (p a b)
export
public export
Functor f => Profunctor p => Profunctor (Cayley f p) where
dimap f g (MkCayley p) = MkCayley (dimap f g <$> p)
lmap f (MkCayley p) = MkCayley (lmap f <$> p)
rmap g (MkCayley p) = MkCayley (rmap g <$> p)
export
public export
Functor f => ProfunctorFunctor (Cayley f) where
promap f (MkCayley p) = MkCayley (map f p)
export
public export
Monad m => ProfunctorMonad (Cayley m) where
propure = MkCayley . pure
projoin (MkCayley p) = MkCayley $ p >>= runCayley
export
public export
Functor f => GenStrong ten p => GenStrong ten (Cayley f p) where
strongl (MkCayley p) = MkCayley (strongl {ten} <$> p)
strongr (MkCayley p) = MkCayley (strongr {ten} <$> p)
export
public export
Functor f => GenCostrong ten p => GenCostrong ten (Cayley f p) where
costrongl (MkCayley p) = MkCayley (costrongl {ten} <$> p)
costrongr (MkCayley p) = MkCayley (costrongr {ten} <$> p)
export
public export
Functor f => Closed p => Closed (Cayley f p) where
closed (MkCayley p) = MkCayley (closed <$> p)
export
public export
Functor f => Traversing p => Traversing (Cayley f p) where
traverse' (MkCayley p) = MkCayley (traverse' <$> p)
wander f (MkCayley p) = MkCayley (wander f <$> p)
export
public export
Functor f => Mapping p => Mapping (Cayley f p) where
map' (MkCayley p) = MkCayley (map' <$> p)
roam f (MkCayley p) = MkCayley (roam f <$> p)
export
public export
Functor g => Sieve p f => Sieve (Cayley g p) (g . f) using Functor.Compose where
sieve (MkCayley p) x = ($ x) . sieve <$> p
export
mapCayley : (forall x. f x -> g x) -> Cayley f p a b -> Cayley g p a b
public export
mapCayley : (forall x. f x -> g x) -> Cayley f p :-> Cayley g p
mapCayley f (MkCayley p) = MkCayley (f p)

View file

@ -26,26 +26,26 @@ interface Profunctor p => Closed p where
------------------------------------------------------------------------------
-- Implementations for existing types
-- Implementations
------------------------------------------------------------------------------
export
public export
Closed Morphism where
closed (Mor f) = Mor (f .)
||| A named implementation of `Closed` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`.
export
public export
[Function] Closed (\a,b => a -> b) using Profunctor.Function where
closed = (.)
export
public export
Functor f => Closed (Costar f) where
closed (MkCostar p) = MkCostar $ \f,x => p (map ($ x) f)
export
public export
curry' : Closed p => p (a, b) c -> p a (b -> c)
curry' = lmap (,) . closed
@ -71,40 +71,40 @@ record Closure p a b where
runClosure : forall x. p (x -> a) (x -> b)
export
public export
Profunctor p => Profunctor (Closure p) where
dimap f g (MkClosure p) = MkClosure $ dimap (f .) (g .) p
lmap f (MkClosure p) = MkClosure $ lmap (f .) p
rmap f (MkClosure p) = MkClosure $ rmap (f .) p
export
public export
ProfunctorFunctor Closure where
promap f (MkClosure p) = MkClosure (f p)
export
public export
ProfunctorComonad Closure where
proextract (MkClosure p) = dimap const ($ ()) p
produplicate (MkClosure p) = MkClosure $ MkClosure $ dimap uncurry curry p
export
public export
Strong p => GenStrong Pair (Closure p) where
strongl (MkClosure p) = MkClosure $ dimap hither yon $ first p
strongr (MkClosure p) = MkClosure $ dimap hither yon $ second p
export
public export
Profunctor p => Closed (Closure p) where
closed p = runClosure $ produplicate p
export
public export
Profunctor p => Functor (Closure p a) where
map = rmap
export
public export
close : Closed p => p :-> q -> p :-> Closure q
close f p = MkClosure $ f $ closed p
export
public export
unclose : Profunctor q => p :-> Closure q -> p :-> q
unclose f p = dimap const ($ ()) $ runClosure $ f p
@ -121,17 +121,17 @@ data Environment : (p : Type -> Type -> Type) -> Type -> Type -> Type where
MkEnv : ((z -> y) -> b) -> p x y -> (a -> z -> x) -> Environment p a b
export
public export
Profunctor (Environment p) where
dimap f g (MkEnv l m r) = MkEnv (g . l) m (r . f)
lmap f (MkEnv l m r) = MkEnv l m (r . f)
rmap g (MkEnv l m r) = MkEnv (g . l) m r
export
public export
ProfunctorFunctor Environment where
promap f (MkEnv l m r) = MkEnv l (f m) r
export
public export
ProfunctorMonad Environment where
propure p = MkEnv ($ ()) p const
projoin (MkEnv {x=x',y=y',z=z'} l' (MkEnv {x,y,z} l m r) r') = MkEnv (ll . curry) m rr
@ -141,12 +141,12 @@ ProfunctorMonad Environment where
rr : a -> (z', z) -> x
rr a (b, c) = r (r' a b) c
export
public export
ProfunctorAdjunction Environment Closure where
prounit p = MkClosure (MkEnv id p id)
procounit (MkEnv l (MkClosure x) r) = dimap r l x
export
public export
Closed (Environment p) where
closed {x=x'} (MkEnv {x,y,z} l m r) = MkEnv l' m r'
where
@ -155,15 +155,15 @@ Closed (Environment p) where
r' : (x' -> a) -> (z, x') -> x
r' f (z,x) = r (f x) z
export
public export
Profunctor p => Functor (Environment p a) where
map = rmap
export
public export
environment : Closed q => p :-> q -> Environment p :-> q
environment f (MkEnv l m r) = dimap r l $ closed (f m)
export
public export
unenvironment : Environment p :-> q -> p :-> q
unenvironment f p = f (MkEnv ($ ()) p const)

View file

@ -80,15 +80,25 @@ unright = costrongr {ten=Either}
------------------------------------------------------------------------------
-- Implementations for existing types
-- Implementations
------------------------------------------------------------------------------
export
public export
GenCostrong Pair Tagged where
costrongl (Tag (x,_)) = Tag x
costrongr (Tag (_,x)) = Tag x
public export
GenCostrong Either (Forget r) where
costrongl (MkForget k) = MkForget (k . Left)
costrongr (MkForget k) = MkForget (k . Right)
public export
GenCostrong Pair (Coforget r) where
costrongl (MkCoforget k) = MkCoforget (fst . k)
costrongr (MkCoforget k) = MkCoforget (snd . k)
------------------------------------------------------------------------------
-- Cotambara
@ -101,27 +111,27 @@ public export
data GenCotambara : (ten, p : Type -> Type -> Type) -> Type -> Type -> Type where
MkCotambara : GenCostrong ten q => q :-> p -> q a b -> GenCotambara ten p a b
export
public export
Profunctor (GenCotambara ten p) where
lmap f (MkCotambara n p) = MkCotambara n (lmap f p)
rmap f (MkCotambara n p) = MkCotambara n (rmap f p)
dimap f g (MkCotambara n p) = MkCotambara n (dimap f g p)
export
public export
ProfunctorFunctor (GenCotambara ten) where
promap f (MkCotambara n p) = MkCotambara (f . n) p
export
public export
GenCostrong ten (GenCotambara ten p) where
costrongl (MkCotambara @{costr} n p) = MkCotambara n (costrongl @{costr} p)
costrongr (MkCotambara @{costr} n p) = MkCotambara n (costrongr @{costr} p)
export
public export
ProfunctorComonad (GenCotambara ten) where
proextract (MkCotambara n p) = n p
produplicate (MkCotambara n p) = MkCotambara id (MkCotambara n p)
export
public export
Functor (GenCotambara ten p a) where
map = rmap
@ -143,11 +153,11 @@ CotambaraSum : (p : Type -> Type -> Type) -> Type -> Type -> Type
CotambaraSum = GenCotambara Either
export
public export
cotambara : GenCostrong ten p => p :-> q -> p :-> GenCotambara ten q
cotambara f = MkCotambara f
export
public export
uncotambara : Tensor ten i => Profunctor q => p :-> GenCotambara ten q -> p :-> q
uncotambara f p = proextract (f p)
@ -164,27 +174,27 @@ record GenCopastro (ten, p : Type -> Type -> Type) a b where
constructor MkCopastro
runCopastro : forall q. GenCostrong ten q => p :-> q -> q a b
export
public export
Profunctor (GenCopastro ten p) where
dimap f g (MkCopastro h) = MkCopastro $ \n => dimap f g (h n)
lmap f (MkCopastro h) = MkCopastro $ \n => lmap f (h n)
rmap f (MkCopastro h) = MkCopastro $ \n => rmap f (h n)
export
public export
ProfunctorFunctor (GenCopastro ten) where
promap f (MkCopastro h) = MkCopastro $ \n => h (n . f)
export
public export
ProfunctorMonad (GenCopastro ten) where
propure p = MkCopastro ($ p)
projoin p = MkCopastro $ \x => runCopastro p (\y => runCopastro y x)
export
public export
GenCostrong ten (GenCopastro ten p) where
costrongl (MkCopastro h) = MkCopastro $ \n => costrongl {ten} (h n)
costrongr (MkCopastro h) = MkCopastro $ \n => costrongr {ten} (h n)
export
public export
ProfunctorAdjunction (GenCopastro ten) (GenCotambara ten) where
prounit p = MkCotambara id (propure {t=GenCopastro ten} p)
procounit (MkCopastro h) = proextract (h id)
@ -207,10 +217,10 @@ CopastroSum : (p : Type -> Type -> Type) -> Type -> Type -> Type
CopastroSum = GenCopastro Either
export
public export
copastro : GenCostrong ten q => p :-> q -> GenCopastro ten p :-> q
copastro f (MkCopastro h) = h f
export
public export
uncopastro : Tensor ten i => GenCopastro ten p :-> q -> p :-> q
uncopastro f x = f (MkCopastro ($ x))

View file

@ -1,6 +1,5 @@
module Data.Profunctor.Mapping
import Control.Monad.Identity
import Data.Morphisms
import Data.Tensor
import Data.Profunctor
@ -9,6 +8,9 @@ import Data.Profunctor.Traversing
%default total
functor : Functor (\a => (a -> b) -> t)
functor = MkFunctor (\f => (. (. f)))
------------------------------------------------------------------------------
-- Mapping interface
------------------------------------------------------------------------------
@ -20,7 +22,7 @@ import Data.Profunctor.Traversing
||| * `map' . lmap f = lmap (map f) . map'`
||| * `map' . rmap f = rmap (map f) . map'`
||| * `map' . map' = map' @{Compose}`
||| * `dimap Identity runIdentity . map' = id`
||| * `dimap Id runIdentity . map' = id`
public export
interface (Traversing p, Closed p) => Mapping p where
map' : Functor f => p a b -> p (f a) (f b)
@ -28,24 +30,21 @@ interface (Traversing p, Closed p) => Mapping p where
roam : ((a -> b) -> s -> t) -> p a b -> p s t
roam f = dimap (flip f) ($ id) . map' @{%search} @{functor}
where
functor : Functor (\a => (a -> b) -> t)
functor = MkFunctor (\f => (. (. f)))
------------------------------------------------------------------------------
-- Implementations for existing types
-- Implementations
------------------------------------------------------------------------------
export
public export
Mapping Morphism where
map' (Mor f) = Mor (map f)
roam f (Mor x) = Mor (f x)
||| A named implementation of `Mapping` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`.
export
public export
[Function] Mapping (\a,b => a -> b)
using Traversing.Function Closed.Function where
map' = map
@ -53,7 +52,7 @@ export
------------------------------------------------------------------------------
-- Implementations for existing types
-- CofreeMapping
------------------------------------------------------------------------------
@ -64,47 +63,44 @@ record CofreeMapping p a b where
constructor MkCFM
runCFM : forall f. Functor f => p (f a) (f b)
export
public export
Profunctor p => Profunctor (CofreeMapping p) where
lmap f (MkCFM p) = MkCFM (lmap (map f) p)
rmap f (MkCFM p) = MkCFM (rmap (map f) p)
dimap f g (MkCFM p) = MkCFM (dimap (map f) (map g) p)
export
public export
Profunctor p => Functor (CofreeMapping p a) where
map = rmap
export
public export
ProfunctorFunctor CofreeMapping where
promap f (MkCFM p) = MkCFM (f p)
export
public export
ProfunctorComonad CofreeMapping where
proextract (MkCFM p) = dimap Id runIdentity p
proextract (MkCFM p) = p @{FunctorId}
produplicate (MkCFM p) = MkCFM $ MkCFM $ p @{Compose}
export
public export
Symmetric ten => Profunctor p => GenStrong ten (CofreeMapping p) where
strongr (MkCFM p) = MkCFM (p @{Compose {g=ten c} @{%search} @{MkFunctor mapSnd}})
strongl (MkCFM p) = MkCFM (p @{Compose {g=(`ten` c)} @{%search} @{MkFunctor mapFst}})
export
public export
Profunctor p => Closed (CofreeMapping p) where
closed (MkCFM p) = MkCFM (p @{Compose {g = \b => x -> b} @{%search} @{MkFunctor (.)}})
roamCofree : Profunctor p => ((a -> b) -> s -> t) -> CofreeMapping p a b -> CofreeMapping p s t
roamCofree f (MkCFM p) = MkCFM $ dimap (map (flip f)) (map ($ id)) $
p @{Compose @{%search} @{functor}}
where
functor : Functor (\a => (a -> b) -> t)
functor = MkFunctor (\f => (. (. f)))
export
public export
Profunctor p => Traversing (CofreeMapping p) where
traverse' (MkCFM p) = MkCFM (p @{Compose})
wander f = roamCofree $ (runIdentity .) . f . (Id .)
wander f = roamCofree $ f @{ApplicativeId}
export
public export
Profunctor p => Mapping (CofreeMapping p) where
map' (MkCFM p) = MkCFM (p @{Compose})
roam = roamCofree
@ -119,51 +115,48 @@ public export
data FreeMapping : (p : Type -> Type -> Type) -> Type -> Type -> Type where
MkFM : Functor f => (f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
export
public export
Profunctor (FreeMapping p) where
lmap f (MkFM l m r) = MkFM l m (r . f)
rmap f (MkFM l m r) = MkFM (f . l) m r
dimap f g (MkFM l m r) = MkFM (g . l) m (r . f)
export
public export
ProfunctorFunctor FreeMapping where
promap f (MkFM l m r) = MkFM l (f m) r
export
public export
ProfunctorMonad FreeMapping where
propure p = MkFM runIdentity p Id
propure p = MkFM @{FunctorId} id p id
projoin (MkFM l' (MkFM l m r) r') = MkFM @{Compose} (l' . map l) m (map r . r')
export
public export
Functor (FreeMapping p a) where
map = rmap
export
public export
GenStrong Pair (FreeMapping p) where
strongr (MkFM l m r) = MkFM @{Compose} (map l) m (map r)
strongl = dimap swap' swap' . strongr {p=FreeMapping p}
export
public export
GenStrong Either (FreeMapping p) where
strongr (MkFM l m r) = MkFM @{Compose} (map l) m (map r)
strongl = dimap swap' swap' . strongr {p=FreeMapping p}
export
public export
Closed (FreeMapping p) where
closed (MkFM l m r) = dimap Mor applyMor $ MkFM @{Compose} (map l) m (map r)
roamFree : ((a -> b) -> s -> t) -> FreeMapping p a b -> FreeMapping p s t
roamFree f (MkFM l m r) = MkFM @{Compose @{functor}} (($ id) . map @{functor} l) m (map @{functor} r . flip f)
where
functor : Functor (\a => (a -> b) -> t)
functor = MkFunctor (\f => (. (. f)))
export
public export
Traversing (FreeMapping p) where
traverse' (MkFM l m r) = MkFM @{Compose} (map l) m (map r)
wander f = roamFree $ (runIdentity .) . f . (Id .)
wander f = roamFree $ f @{ApplicativeId}
export
public export
Mapping (FreeMapping p) where
map' (MkFM l m r) = MkFM @{Compose} (map l) m (map r)
roam = roamFree

View file

@ -1,5 +1,6 @@
module Data.Profunctor.Representable
import Control.Applicative.Const
import Control.Monad.Identity
import Data.Morphisms
import Data.Profunctor
@ -20,7 +21,7 @@ interface (Sieve p f, Strong p) => Representable p f | p where
tabulate : (a -> f b) -> p a b
||| A profunctor `p` is representable if it is isomorphic to `Costar f` for some `f`.
||| A profunctor `p` is corepresentable if it is isomorphic to `Costar f` for some `f`.
public export
interface Cosieve p f => Corepresentable p f | p where
cotabulate : (f a -> b) -> p a b
@ -49,9 +50,9 @@ Representable Morphism Identity where
||| A named implementation of `Representable` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`.
export
[Function] Representable (\a,b => a -> b) Identity
[Function] Representable (\a,b => a -> b) Prelude.id
using Sieve.Function Strong.Function where
tabulate = (runIdentity .)
tabulate = id
export
Functor f => Representable (Kleislimorphism f) f where
@ -61,9 +62,25 @@ export
Functor f => Representable (Star f) f where
tabulate = MkStar
export
Representable (Forget r) (Const r) where
tabulate = MkForget . (runConst .)
export
Corepresentable Morphism Identity where
cotabulate f = Mor (f . Id)
namespace Corepresentable
||| A named implementation of `Corepresentable` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`.
export
[Function] Corepresentable (\a,b => a -> b) Prelude.id using Cosieve.Function where
cotabulate = id
export
Functor f => Corepresentable (Costar f) f where
cotabulate = MkCostar
export
Corepresentable (Coforget r) (Const r) where
cotabulate = MkCoforget . (. MkConst)

View file

@ -1,5 +1,6 @@
module Data.Profunctor.Sieve
import Control.Applicative.Const
import Control.Monad.Identity
import Data.Morphisms
import Data.Profunctor
@ -29,37 +30,44 @@ interface (Profunctor p, Functor f) => Cosieve p f | p where
------------------------------------------------------------------------------
export
public export
Sieve Morphism Identity where
sieve (Mor f) = Id . f
||| A named implementation of `Sieve` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`.
export
[Function] Sieve (\a,b => a -> b) Identity using Profunctor.Function where
sieve = (Id .)
public export
[Function] Sieve (\a,b => a -> b) Prelude.id using Profunctor.Function FunctorId where
sieve = id
export
public export
Functor f => Sieve (Kleislimorphism f) f where
sieve = applyKleisli
export
public export
Functor f => Sieve (Star f) f where
sieve = applyStar
public export
Sieve (Forget r) (Const r) where
sieve (MkForget k) = MkConst . k
export
public export
Cosieve Morphism Identity where
cosieve (Mor f) = f . runIdentity
namespace Cosieve
||| A named implementation of `Cosieve` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`.
export
[Function] Cosieve (\a,b => a -> b) Identity using Profunctor.Function where
cosieve = (. runIdentity)
public export
[Function] Cosieve (\a,b => a -> b) Prelude.id using Profunctor.Function FunctorId where
cosieve = id
export
public export
Functor f => Cosieve (Costar f) f where
cosieve = applyCostar
public export
Cosieve (Coforget r) (Const r) where
cosieve (MkCoforget k) = k . runConst

View file

@ -80,58 +80,73 @@ right : Choice p => p a b -> p (Either c a) (Either c b)
right = strongr {ten=Either}
export
public export
uncurry' : Strong p => p a (b -> c) -> p (a, b) c
uncurry' = rmap (uncurry id) . first
export
public export
strong : Strong p => (a -> b -> c) -> p a b -> p a c
strong f = dimap dup (uncurry $ flip f) . first
------------------------------------------------------------------------------
-- Implementations for existing types
-- Implementations
------------------------------------------------------------------------------
export
public export
Bifunctor ten => GenStrong ten Morphism where
strongl (Mor f) = Mor (mapFst f)
strongr (Mor f) = Mor (mapSnd f)
||| A named implementation of `GenStrong` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`.
export
public export
[Function] Bifunctor ten => GenStrong ten (\a,b => a -> b)
using Profunctor.Function where
strongl = mapFst
strongr = mapSnd
export
public export
Functor f => GenStrong Pair (Kleislimorphism f) where
strongl (Kleisli f) = Kleisli $ \(a,c) => (,c) <$> f a
strongr (Kleisli f) = Kleisli $ \(c,a) => (c,) <$> f a
export
public export
Applicative f => GenStrong Either (Kleislimorphism f) where
strongl (Kleisli f) = Kleisli $ either (map Left . f) (pure . Right)
strongr (Kleisli f) = Kleisli $ either (pure . Left) (map Right . f)
export
public export
Functor f => GenStrong Pair (Star f) where
strongl (MkStar f) = MkStar $ \(a,c) => (,c) <$> f a
strongr (MkStar f) = MkStar $ \(c,a) => (c,) <$> f a
export
public export
Applicative f => GenStrong Either (Star f) where
strongl (MkStar f) = MkStar $ either (map Left . f) (pure . Right)
strongr (MkStar f) = MkStar $ either (pure . Left) (map Right . f)
export
public export
GenStrong Either Tagged where
strongl (Tag x) = Tag (Left x)
strongr (Tag x) = Tag (Right x)
public export
GenStrong Pair (Forget r) where
strongl (MkForget k) = MkForget (k . fst)
strongr (MkForget k) = MkForget (k . snd)
public export
Monoid r => GenStrong Either (Forget r) where
strongl (MkForget k) = MkForget (either k (const neutral))
strongr (MkForget k) = MkForget (either (const neutral) k)
public export
GenStrong Either (Coforget r) where
strongl (MkCoforget k) = MkCoforget (Left . k)
strongr (MkCoforget k) = MkCoforget (Right . k)
------------------------------------------------------------------------------
-- Tambara
@ -145,26 +160,26 @@ record GenTambara (ten, p : Type -> Type -> Type) a b where
constructor MkTambara
runTambara : forall c. p (a `ten` c) (b `ten` c)
export
public export
Bifunctor ten => Profunctor p => Profunctor (GenTambara ten p) where
dimap f g (MkTambara p) = MkTambara $ dimap (mapFst f) (mapFst g) p
export
public export
ProfunctorFunctor (GenTambara ten) where
promap f (MkTambara p) = MkTambara (f p)
export
public export
Tensor ten i => ProfunctorComonad (GenTambara ten) where
proextract (MkTambara p) = dimap unitr.rightToLeft unitr.leftToRight p
produplicate (MkTambara p) = MkTambara $ MkTambara $ dimap assocr assocl p
export
public export
Associative ten => Symmetric ten => Profunctor p => GenStrong ten (GenTambara ten p) where
strongl (MkTambara p) = MkTambara $ dimap assocr assocl p
strongr (MkTambara p) = MkTambara $ dimap (assocr . mapFst swap')
(mapFst swap' . assocl) p
export
public export
Bifunctor ten => Profunctor p => Functor (GenTambara ten p a) where
map = rmap
@ -186,11 +201,11 @@ TambaraSum : (p : Type -> Type -> Type) -> Type -> Type -> Type
TambaraSum = GenTambara Either
export
public export
tambara : GenStrong ten p => p :-> q -> p :-> GenTambara ten q
tambara @{gs} f x = MkTambara $ f $ strongl @{gs} x
export
public export
untambara : Tensor ten i => Profunctor q => p :-> GenTambara ten q -> p :-> q
untambara f x = dimap unitr.rightToLeft unitr.leftToRight $ runTambara $ f x
@ -207,17 +222,17 @@ data GenPastro : (ten, p : Type -> Type -> Type) -> Type -> Type -> Type where
MkPastro : (y `ten` z -> b) -> p x y -> (a -> x `ten` z) -> GenPastro ten p a b
export
public export
Profunctor (GenPastro ten p) where
dimap f g (MkPastro l m r) = MkPastro (g . l) m (r . f)
lmap f (MkPastro l m r) = MkPastro l m (r . f)
rmap g (MkPastro l m r) = MkPastro (g . l) m r
export
public export
ProfunctorFunctor (GenPastro ten) where
promap f (MkPastro l m r) = MkPastro l (f m) r
export
public export
(Tensor ten i, Symmetric ten) => ProfunctorMonad (GenPastro ten) where
propure x = MkPastro unitr.leftToRight x unitr.rightToLeft
projoin (MkPastro {x=x',y=y',z=z'} l' (MkPastro {x,y,z} l m r) r') = MkPastro ll m rr
@ -228,12 +243,12 @@ export
rr : a -> x `ten` (z' `ten` z)
rr = mapSnd swap' . assocr . mapFst r . r'
export
public export
ProfunctorAdjunction (GenPastro ten) (GenTambara ten) where
prounit x = MkTambara (MkPastro id x id)
procounit (MkPastro l (MkTambara x) r) = dimap r l x
export
public export
(Associative ten, Symmetric ten) => GenStrong ten (GenPastro ten p) where
strongl (MkPastro {x,y,z} l m r) = MkPastro l' m r'
where
@ -266,11 +281,11 @@ PastroSum : (p : Type -> Type -> Type) -> Type -> Type -> Type
PastroSum = GenPastro Either
export
public export
pastro : GenStrong ten q => p :-> q -> GenPastro ten p :-> q
pastro @{gs} f (MkPastro l m r) = dimap r l (strongl @{gs} (f m))
export
public export
unpastro : Tensor ten i => GenPastro ten p :-> q -> p :-> q
unpastro f x = f (MkPastro unitr.leftToRight x unitr.rightToLeft)

View file

@ -1,6 +1,6 @@
module Data.Profunctor.Traversing
import Control.Monad.Identity
import Control.Applicative.Const
import Data.Morphisms
import Data.Tensor
import Data.Profunctor
@ -8,6 +8,11 @@ import Data.Profunctor
%default total
------------------------------------------------------------------------------
-- Default implementation machinery
------------------------------------------------------------------------------
[FoldablePair] Foldable (Pair c) where
foldr op init (_, x) = x `op` init
foldl op init (_, x) = init `op` x
@ -16,14 +21,6 @@ import Data.Profunctor
[TraversablePair] Traversable (Pair c) using FoldablePair where
traverse f (l, r) = (l,) <$> f r
[FoldableIdentity] Foldable Identity where
foldr f i (Id x) = f x i
foldl f i (Id x) = f i x
null _ = False
[TraversableIdentity] Traversable Identity using FoldableIdentity where
traverse f (Id x) = map Id (f x)
record Bazaar a b t where
constructor MkBazaar
@ -48,7 +45,7 @@ Functor (Baz t b) where
sold : Baz t a a -> t
sold m = runIdentity (runBaz m Id)
sold m = runBaz @{ApplicativeId} m id
Foldable (Baz t b) where
foldr f i bz = runBaz bz @{appEndo} f i
@ -60,6 +57,11 @@ Foldable (Baz t b) where
Traversable (Baz t b) where
traverse f bz = map (\m => MkBaz (runBazaar m)) $ runBaz bz @{Compose} $ \x => sell <$> f x
public export
wanderDef : Profunctor p => (forall f. Traversable f => p a b -> p (f a) (f b))
-> (forall f. Applicative f => (a -> f b) -> s -> f t) -> p a b -> p s t
wanderDef tr f = dimap (\s => MkBaz $ \afb => f afb s) sold . tr
------------------------------------------------------------------------------
-- Traversing interface
@ -81,31 +83,41 @@ interface (Strong p, Choice p) => Traversing p where
traverse' = wander traverse
wander : (forall f. Applicative f => (a -> f b) -> s -> f t) -> p a b -> p s t
wander f = dimap (\s => MkBaz $ \afb => f afb s) sold . traverse'
wander = wanderDef traverse'
export
------------------------------------------------------------------------------
-- Implementations
------------------------------------------------------------------------------
public export
Traversing Morphism where
traverse' (Mor f) = Mor (map f)
wander f (Mor p) = Mor (runIdentity . (f $ Id . p))
wander f (Mor p) = Mor (f @{ApplicativeId} p)
||| A named implementation of `Traversing` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`.
export
public export
[Function] Traversing (\a,b => a -> b) using Strong.Function where
traverse' = map
wander f g = runIdentity . (f $ Id . g)
wander f = f @{ApplicativeId}
export
public export
Applicative f => Traversing (Kleislimorphism f) where
traverse' (Kleisli p) = Kleisli (traverse p)
wander f (Kleisli p) = Kleisli (f p)
export
public export
Applicative f => Traversing (Star f) where
traverse' (MkStar p) = MkStar (traverse p)
wander f (MkStar p) = MkStar (f p)
public export
Monoid r => Traversing (Forget r) where
traverse' (MkForget k) = MkForget (foldMap k)
wander f (MkForget k) = MkForget (runConst . f (MkConst . k))
------------------------------------------------------------------------------
-- CofreeTraversing
@ -119,45 +131,45 @@ record CofreeTraversing p a b where
constructor MkCFT
runCFT : forall f. Traversable f => p (f a) (f b)
export
public export
Profunctor p => Profunctor (CofreeTraversing p) where
lmap f (MkCFT p) = MkCFT (lmap (map f) p)
rmap g (MkCFT p) = MkCFT (rmap (map g) p)
dimap f g (MkCFT p) = MkCFT (dimap (map f) (map g) p)
export
public export
Profunctor p => GenStrong Pair (CofreeTraversing p) where
strongr (MkCFT p) = MkCFT (p @{Compose @{%search} @{TraversablePair}})
strongl = dimap swap' swap' . strongr {p=CofreeTraversing p}
export
public export
Profunctor p => GenStrong Either (CofreeTraversing p) where
strongr (MkCFT p) = MkCFT (p @{Compose {f=Either c}})
strongl = dimap swap' swap' . strongr {p=CofreeTraversing p}
export
public export
Profunctor p => Traversing (CofreeTraversing p) where
traverse' (MkCFT p) = MkCFT (p @{Compose})
export
public export
ProfunctorFunctor CofreeTraversing where
promap f (MkCFT p) = MkCFT (f p)
export
public export
ProfunctorComonad CofreeTraversing where
proextract (MkCFT p) = dimap Id runIdentity $ p @{TraversableIdentity}
proextract (MkCFT p) = p @{TraversableId}
produplicate (MkCFT p) = MkCFT $ MkCFT $ p @{Compose}
export
public export
Profunctor p => Functor (CofreeTraversing p a) where
map = rmap
export
public export
cofreeTraversing : Traversing p => p :-> q -> p :-> CofreeTraversing q
cofreeTraversing f p = MkCFT $ f $ traverse' p
export
public export
uncofreeTraversing : Profunctor q => p :-> CofreeTraversing q -> p :-> q
uncofreeTraversing f p = proextract $ f p
@ -173,40 +185,40 @@ public export
data FreeTraversing : (p : Type -> Type -> Type) -> Type -> Type -> Type where
MkFT : Traversable f => (f y -> b) -> p x y -> (a -> f x) -> FreeTraversing p a b
export
public export
Profunctor (FreeTraversing p) where
lmap f (MkFT l m r) = MkFT l m (r . f)
rmap f (MkFT l m r) = MkFT (f . l) m r
dimap f g (MkFT l m r) = MkFT (g . l) m (r . f)
export
public export
GenStrong Pair (FreeTraversing p) where
strongr (MkFT l m r) = MkFT @{Compose @{TraversablePair}} (map l) m (map r)
strongl = dimap swap' swap' . strongr {p=FreeTraversing p}
export
public export
GenStrong Either (FreeTraversing p) where
strongr (MkFT l m r) = MkFT @{Compose {t=Either c}} (map l) m (map r)
strongl = dimap swap' swap' . strongr {p=FreeTraversing p}
export
public export
Traversing (FreeTraversing p) where
traverse' (MkFT l m r) = MkFT @{Compose} (map l) m (map r)
export
public export
ProfunctorFunctor FreeTraversing where
promap f (MkFT l m r) = MkFT l (f m) r
export
public export
ProfunctorMonad FreeTraversing where
propure p = MkFT @{TraversableIdentity} runIdentity p Id
propure p = MkFT @{TraversableId} id p id
projoin (MkFT l' (MkFT l m r) r') = MkFT @{Compose} (l' . map l) m (map r . r')
export
public export
freeTraversing : Traversing q => p :-> q -> FreeTraversing p :-> q
freeTraversing fn (MkFT {f} l m r) = dimap r l (traverse' {f} (fn m))
export
public export
unfreeTraversing : FreeTraversing p :-> q -> p :-> q
unfreeTraversing f p = f (MkFT @{TraversableIdentity} runIdentity p Id)
unfreeTraversing f p = f (MkFT @{TraversableId} id p id)

View file

@ -59,7 +59,7 @@ p :-> q = forall a, b. p a b -> q a b
------------------------------------------------------------------------------
export
public export
Profunctor Morphism where
dimap f g (Mor h) = Mor (g . h . f)
lmap f (Mor g) = Mor (g . f)
@ -68,13 +68,13 @@ Profunctor Morphism where
namespace Profunctor
||| A named implementation of `Profunctor` for function types.
||| Use this to avoid having to use a type wrapper like `Morphism`.
export
public export
[Function] Profunctor (\a,b => a -> b) where
dimap f g h = g . h . f
lmap = flip (.)
rmap = (.)
export
public export
Functor f => Profunctor (Kleislimorphism f) where
dimap f g (Kleisli h) = Kleisli (map g . h . f)
lmap f (Kleisli g) = Kleisli (g . f)
@ -82,7 +82,7 @@ Functor f => Profunctor (Kleislimorphism f) where
------------------------------------------------------------------------------
-- Implementations for existing types
-- New profunctor types
------------------------------------------------------------------------------
@ -95,27 +95,27 @@ record Star {0 k : Type} (f : k -> Type) a (b : k) where
constructor MkStar
applyStar : a -> f b
export
public export
Functor f => Functor (Star f a) where
map f (MkStar g) = MkStar (map f . g)
export
public export
Applicative f => Applicative (Star f a) where
pure = MkStar . const . pure
MkStar f <*> MkStar g = MkStar (\x => f x <*> g x)
export
public export
Monad f => Monad (Star f a) where
MkStar f >>= g = MkStar $ \x => do
a <- f x
applyStar (g a) x
export
public export
Contravariant f => Contravariant (Star f a) where
contramap f (MkStar g) = MkStar (contramap f . g)
export
public export
Functor f => Profunctor (Star f) where
dimap f g (MkStar h) = MkStar (map g . h . f)
lmap f (MkStar g) = MkStar (g . f)
@ -128,20 +128,20 @@ record Costar {0 k : Type} (f : k -> Type) (a : k) b where
constructor MkCostar
applyCostar : f a -> b
export
public export
Functor (Costar f a) where
map f (MkCostar g) = MkCostar (f . g)
export
public export
Applicative (Costar f a) where
pure = MkCostar . const
MkCostar f <*> MkCostar g = MkCostar (\x => f x (g x))
export
public export
Monad (Costar f a) where
MkCostar f >>= g = MkCostar (\x => applyCostar (g (f x)) x)
export
public export
Functor f => Profunctor (Costar f) where
dimap f g (MkCostar h) = MkCostar (g . h . map f)
lmap f (MkCostar g) = MkCostar (g . map f)
@ -160,21 +160,21 @@ public export
retag : Tagged a c -> Tagged b c
retag (Tag x) = Tag x
export
public export
Functor (Tagged a) where
map f (Tag x) = Tag (f x)
export
public export
Applicative (Tagged a) where
pure = Tag
Tag f <*> Tag x = Tag (f x)
export
public export
Monad (Tagged a) where
join = runTagged
Tag x >>= f = f x
export
public export
Foldable (Tagged a) where
foldr f x (Tag y) = f y x
foldl f x (Tag y) = f x y
@ -183,12 +183,130 @@ Foldable (Tagged a) where
toList (Tag x) = [x]
foldMap f (Tag x) = f x
export
public export
Traversable (Tagged a) where
traverse f (Tag x) = map Tag (f x)
export
public export
Profunctor Tagged where
dimap _ f (Tag x) = Tag (f x)
lmap = const retag
rmap f (Tag x) = Tag (f x)
||| `Forget r` is equivalent to `Star (Const r)`.
public export
record Forget {0 k : Type} r a (b : k) where
constructor MkForget
runForget : a -> r
public export
reforget : Forget r a b -> Forget r a c
reforget (MkForget k) = MkForget k
public export
Functor (Forget r a) where
map _ = reforget
public export
Contravariant (Forget {k=Type} r a) where
contramap _ = reforget
public export
Monoid r => Applicative (Forget r a) where
pure _ = MkForget (const neutral)
MkForget f <*> MkForget g = MkForget (f <+> g)
public export
Monoid r => Monad (Forget {k=Type} r a) where
join = reforget
(>>=) = reforget .: const
public export
Foldable (Forget r a) where
foldr _ x _ = x
foldl _ x _ = x
null = const True
foldlM _ x _ = pure x
toList _ = []
foldMap _ _ = neutral
public export
Traversable (Forget r a) where
traverse _ = pure . reforget
public export
Profunctor (Forget r) where
dimap f _ (MkForget k) = MkForget (k . f)
lmap f (MkForget k) = MkForget (k . f)
rmap = map
||| `Coforget r` is equivalent to `Costar (Const r)`.
public export
record Coforget {0 k : Type} r (a : k) b where
constructor MkCoforget
runCoforget : r -> b
public export
recoforget : Coforget r a c -> Coforget r b c
recoforget (MkCoforget k) = MkCoforget k
public export
Functor (Coforget r a) where
map f (MkCoforget k) = MkCoforget (f . k)
public export
Bifunctor (Coforget r) where
bimap _ f (MkCoforget k) = MkCoforget (f . k)
mapFst _ = recoforget
mapSnd = map
public export
Applicative (Coforget r a) where
pure = MkCoforget . const
MkCoforget f <*> MkCoforget g = MkCoforget (\r => f r (g r))
public export
Monad (Coforget r a) where
MkCoforget k >>= f = MkCoforget (\r => runCoforget (f $ k r) r)
public export
Profunctor (Coforget r) where
dimap _ f (MkCoforget k) = MkCoforget (f . k)
lmap _ = recoforget
rmap = map
------------------------------------------------------------------------------
-- Identity functor
------------------------------------------------------------------------------
-- A few convenience definitions for use in other modules.
public export
[FunctorId] Functor Prelude.id where
map = id
public export
[ApplicativeId] Applicative Prelude.id using FunctorId where
pure = id
(<*>) = id
public export
[MonadId] Monad Prelude.id using ApplicativeId where
join = id
(>>=) = flip id
public export
[FoldableId] Foldable Prelude.id where
foldr = flip
foldl = id
null _ = False
foldlM = id
toList = pure
foldMap = id
public export
[TraversableId] Traversable Prelude.id using FunctorId FoldableId where
traverse = id

View file

@ -20,69 +20,69 @@ record Yoneda p a b where
constructor MkYoneda
runYoneda : forall x, y. (x -> a) -> (b -> y) -> p x y
export
public export
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)
export
public export
ProfunctorFunctor Yoneda where
promap f (MkYoneda p) = MkYoneda $ f .: p
export
public export
ProfunctorMonad Yoneda where
propure p = MkYoneda $ \l,r => dimap l r p
projoin (MkYoneda p) = p id id
export
public export
ProfunctorComonad Yoneda where
proextract (MkYoneda p) = p id id
produplicate p = MkYoneda $ \l,r => dimap l r p
||| A witness that `Yoneda p` and `p` are equivalent when `p` is a profunctor.
export
public export
yonedaEqv : Profunctor p => p a b <=> Yoneda p a b
yonedaEqv = MkEquivalence propure proextract
export
public export
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
export
public export
Functor (Yoneda p a) where
map = rmap
export
public export
GenStrong ten p => GenStrong ten (Yoneda p) where
strongl = propure . strongl {ten,p} . proextract
strongr = propure . strongr {ten,p} . proextract
export
public export
GenCostrong ten p => GenCostrong ten (Yoneda p) where
costrongl = propure . costrongl {ten,p} . proextract
costrongr = propure . costrongr {ten,p} . proextract
export
public export
Closed p => Closed (Yoneda p) where
closed = propure . closed . proextract
export
public export
Traversing p => Traversing (Yoneda p) where
traverse' = propure . traverse' . proextract
wander f = propure . wander f . proextract
export
public export
Mapping p => Mapping (Yoneda p) where
map' = propure . map' . proextract
roam f = propure . roam f . proextract
export
public export
Sieve p f => Sieve (Yoneda p) f where
sieve = sieve . proextract
export
public export
Cosieve p f => Cosieve (Yoneda p) f where
cosieve = cosieve . proextract
@ -98,68 +98,68 @@ data Coyoneda : (p : Type -> Type -> Type) -> Type -> Type -> Type where
MkCoyoneda : (a -> x) -> (y -> b) -> p x y -> Coyoneda p a b
export
public export
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
export
public export
ProfunctorFunctor Coyoneda where
promap f (MkCoyoneda l r p) = MkCoyoneda l r (f p)
export
public export
ProfunctorMonad Coyoneda where
propure = MkCoyoneda id id
projoin (MkCoyoneda l r p) = dimap l r p
export
public export
ProfunctorComonad Coyoneda where
proextract (MkCoyoneda l r p) = dimap l r p
produplicate = MkCoyoneda id id
||| A witness that `Coyoneda p` and `p` are equivalent when `p` is a profunctor.
export
public export
coyonedaEqv : Profunctor p => p a b <=> Coyoneda p a b
coyonedaEqv = MkEquivalence propure proextract
export
public export
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
export
public export
Functor (Coyoneda p a) where
map = rmap
export
public export
GenStrong ten p => GenStrong ten (Coyoneda p) where
strongl = propure . strongl {ten,p} . proextract
strongr = propure . strongr {ten,p} . proextract
export
public export
GenCostrong ten p => GenCostrong ten (Coyoneda p) where
costrongl = propure . costrongl {ten,p} . proextract
costrongr = propure . costrongr {ten,p} . proextract
export
public export
Closed p => Closed (Coyoneda p) where
closed = propure . closed . proextract
export
public export
Traversing p => Traversing (Coyoneda p) where
traverse' = propure . traverse' . proextract
wander f = propure . wander f . proextract
export
public export
Mapping p => Mapping (Coyoneda p) where
map' = propure . map' . proextract
roam f = propure . roam f . proextract
export
public export
Sieve p f => Sieve (Coyoneda p) f where
sieve = sieve . proextract
export
public export
Cosieve p f => Cosieve (Coyoneda p) f where
cosieve = cosieve . proextract

View file

@ -58,16 +58,16 @@ interface Associative ten => Tensor ten i | ten where
------------------------------------------------------------------------------
export
public export
Associative Pair where
assocl (x,(y,z)) = ((x,y),z)
assocr ((x,y),z) = (x,(y,z))
export
public export
Symmetric Pair where
swap' = swap
export
public export
Tensor Pair () where
unitl = MkEquivalence snd ((),)
unitr = MkEquivalence fst (,())
@ -78,7 +78,7 @@ Tensor Pair () where
------------------------------------------------------------------------------
export
public export
Associative Either where
assocl (Left x) = Left (Left x)
assocl (Right (Left x)) = Left (Right x)
@ -88,11 +88,11 @@ Associative Either where
assocr (Left (Right x)) = Right (Left x)
assocr (Right x) = Right (Right x)
export
public export
Symmetric Either where
swap' = either Right Left
export
public export
Tensor Either Void where
unitl = MkEquivalence (either absurd id) Right
unitr = MkEquivalence (either id absurd) Left

View file

@ -1,5 +1,7 @@
package profunctors
version = 1.0.0
version = 1.1.2
brief = "Profunctors for Idris2"
authors = "Kiana Sheibani"
license = "MIT"