adjunctions-4.4: Adjunctions and representable functors

Copyright(c) Edward Kmett 2011-2014
LicenseBSD3
Maintainerekmett@gmail.com
Stabilityexperimental
Safe HaskellTrustworthy
LanguageHaskell98

Data.Functor.Rep

Contents

Description

Representable endofunctors over the category of Haskell types are isomorphic to the reader monad and so inherit a very large number of properties for free.

Synopsis

Representable Functors

class Distributive f => Representable f where Source #

A Functor f is Representable if tabulate and index witness an isomorphism to (->) x.

Every Distributive Functor is actually Representable.

Every Representable Functor from Hask to Hask is a right adjoint.

tabulate . index  ≡ id
index . tabulate  ≡ id
tabulate . returnreturn

Associated Types

type Rep f :: * Source #

If no definition is provided, this will default to GRep.

Methods

tabulate :: (Rep f -> a) -> f a Source #

fmap f . tabulatetabulate . fmap f

If no definition is provided, this will default to gtabulate.

tabulate :: (Generic1 f, GRep f ~ Rep f, GTabulate (Rep1 f)) => (Rep f -> a) -> f a Source #

fmap f . tabulatetabulate . fmap f

If no definition is provided, this will default to gtabulate.

index :: f a -> Rep f -> a Source #

If no definition is provided, this will default to gindex.

index :: (Generic1 f, GRep f ~ Rep f, GIndex (Rep1 f)) => f a -> Rep f -> a Source #

If no definition is provided, this will default to gindex.

Instances

Representable Par1 Source # 

Associated Types

type Rep (Par1 :: * -> *) :: * Source #

Methods

tabulate :: (Rep Par1 -> a) -> Par1 a Source #

index :: Par1 a -> Rep Par1 -> a Source #

Representable Complex Source # 

Associated Types

type Rep (Complex :: * -> *) :: * Source #

Methods

tabulate :: (Rep Complex -> a) -> Complex a Source #

index :: Complex a -> Rep Complex -> a Source #

Representable Identity Source # 

Associated Types

type Rep (Identity :: * -> *) :: * Source #

Methods

tabulate :: (Rep Identity -> a) -> Identity a Source #

index :: Identity a -> Rep Identity -> a Source #

Representable Dual Source # 

Associated Types

type Rep (Dual :: * -> *) :: * Source #

Methods

tabulate :: (Rep Dual -> a) -> Dual a Source #

index :: Dual a -> Rep Dual -> a Source #

Representable Sum Source # 

Associated Types

type Rep (Sum :: * -> *) :: * Source #

Methods

tabulate :: (Rep Sum -> a) -> Sum a Source #

index :: Sum a -> Rep Sum -> a Source #

Representable Product Source # 

Associated Types

type Rep (Product :: * -> *) :: * Source #

Methods

tabulate :: (Rep Product -> a) -> Product a Source #

index :: Product a -> Rep Product -> a Source #

Representable (U1 *) Source # 

Associated Types

type Rep (U1 * :: * -> *) :: * Source #

Methods

tabulate :: (Rep (U1 *) -> a) -> U1 * a Source #

index :: U1 * a -> Rep (U1 *) -> a Source #

Representable (Proxy *) Source # 

Associated Types

type Rep (Proxy * :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Proxy *) -> a) -> Proxy * a Source #

index :: Proxy * a -> Rep (Proxy *) -> a Source #

Representable f => Representable (Cofree f) Source # 

Associated Types

type Rep (Cofree f :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Cofree f) -> a) -> Cofree f a Source #

index :: Cofree f a -> Rep (Cofree f) -> a Source #

Representable f => Representable (Co f) Source # 

Associated Types

type Rep (Co f :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Co f) -> a) -> Co f a Source #

index :: Co f a -> Rep (Co f) -> a Source #

Representable f => Representable (Rec1 * f) Source # 

Associated Types

type Rep (Rec1 * f :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Rec1 * f) -> a) -> Rec1 * f a Source #

index :: Rec1 * f a -> Rep (Rec1 * f) -> a Source #

Representable w => Representable (TracedT s w) Source # 

Associated Types

type Rep (TracedT s w :: * -> *) :: * Source #

Methods

tabulate :: (Rep (TracedT s w) -> a) -> TracedT s w a Source #

index :: TracedT s w a -> Rep (TracedT s w) -> a Source #

Representable m => Representable (IdentityT * m) Source # 

Associated Types

type Rep (IdentityT * m :: * -> *) :: * Source #

Methods

tabulate :: (Rep (IdentityT * m) -> a) -> IdentityT * m a Source #

index :: IdentityT * m a -> Rep (IdentityT * m) -> a Source #

Representable (Tagged * t) Source # 

Associated Types

type Rep (Tagged * t :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Tagged * t) -> a) -> Tagged * t a Source #

index :: Tagged * t a -> Rep (Tagged * t) -> a Source #

Representable f => Representable (Reverse * f) Source # 

Associated Types

type Rep (Reverse * f :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Reverse * f) -> a) -> Reverse * f a Source #

index :: Reverse * f a -> Rep (Reverse * f) -> a Source #

Representable f => Representable (Backwards * f) Source # 

Associated Types

type Rep (Backwards * f :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Backwards * f) -> a) -> Backwards * f a Source #

index :: Backwards * f a -> Rep (Backwards * f) -> a Source #

(Representable f, Representable m) => Representable (ReaderT f m) Source # 

Associated Types

type Rep (ReaderT f m :: * -> *) :: * Source #

Methods

tabulate :: (Rep (ReaderT f m) -> a) -> ReaderT f m a Source #

index :: ReaderT f m a -> Rep (ReaderT f m) -> a Source #

Representable ((->) LiftedRep LiftedRep e) Source # 

Associated Types

type Rep ((LiftedRep -> LiftedRep) e :: * -> *) :: * Source #

Methods

tabulate :: (Rep ((LiftedRep -> LiftedRep) e) -> a) -> (LiftedRep -> LiftedRep) e a Source #

index :: (LiftedRep -> LiftedRep) e a -> Rep ((LiftedRep -> LiftedRep) e) -> a Source #

(Representable f, Representable g) => Representable ((:*:) * f g) Source # 

Associated Types

type Rep ((* :*: f) g :: * -> *) :: * Source #

Methods

tabulate :: (Rep ((* :*: f) g) -> a) -> (* :*: f) g a Source #

index :: (* :*: f) g a -> Rep ((* :*: f) g) -> a Source #

(Representable f, Representable g) => Representable (Product * f g) Source # 

Associated Types

type Rep (Product * f g :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Product * f g) -> a) -> Product * f g a Source #

index :: Product * f g a -> Rep (Product * f g) -> a Source #

Representable m => Representable (ReaderT * e m) Source # 

Associated Types

type Rep (ReaderT * e m :: * -> *) :: * Source #

Methods

tabulate :: (Rep (ReaderT * e m) -> a) -> ReaderT * e m a Source #

index :: ReaderT * e m a -> Rep (ReaderT * e m) -> a Source #

Representable f => Representable (M1 * i c f) Source # 

Associated Types

type Rep (M1 * i c f :: * -> *) :: * Source #

Methods

tabulate :: (Rep (M1 * i c f) -> a) -> M1 * i c f a Source #

index :: M1 * i c f a -> Rep (M1 * i c f) -> a Source #

(Representable f, Representable g) => Representable ((:.:) * * f g) Source # 

Associated Types

type Rep ((* :.: *) f g :: * -> *) :: * Source #

Methods

tabulate :: (Rep ((* :.: *) f g) -> a) -> (* :.: *) f g a Source #

index :: (* :.: *) f g a -> Rep ((* :.: *) f g) -> a Source #

(Representable f, Representable g) => Representable (Compose * * f g) Source # 

Associated Types

type Rep (Compose * * f g :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Compose * * f g) -> a) -> Compose * * f g a Source #

index :: Compose * * f g a -> Rep (Compose * * f g) -> a Source #

tabulated :: (Representable f, Representable g, Profunctor p, Functor h) => p (f a) (h (g b)) -> p (Rep f -> a) (h (Rep g -> b)) Source #

tabulate and index form two halves of an isomorphism.

This can be used with the combinators from the lens package.

tabulated :: Representable f => Iso' (Rep f -> a) (f a)

Wrapped representable functors

newtype Co f a Source #

Constructors

Co 

Fields

Instances

ComonadTrans Co Source # 

Methods

lower :: Comonad w => Co w a -> w a #

(Representable f, (~) * (Rep f) a) => MonadReader a (Co f) Source # 

Methods

ask :: Co f a #

local :: (a -> a) -> Co f a -> Co f a #

reader :: (a -> a) -> Co f a #

Representable f => Monad (Co f) Source # 

Methods

(>>=) :: Co f a -> (a -> Co f b) -> Co f b #

(>>) :: Co f a -> Co f b -> Co f b #

return :: a -> Co f a #

fail :: String -> Co f a #

Functor f => Functor (Co f) Source # 

Methods

fmap :: (a -> b) -> Co f a -> Co f b #

(<$) :: a -> Co f b -> Co f a #

Representable f => Applicative (Co f) Source # 

Methods

pure :: a -> Co f a #

(<*>) :: Co f (a -> b) -> Co f a -> Co f b #

liftA2 :: (a -> b -> c) -> Co f a -> Co f b -> Co f c #

(*>) :: Co f a -> Co f b -> Co f b #

(<*) :: Co f a -> Co f b -> Co f a #

(Representable f, Monoid (Rep f)) => Comonad (Co f) Source # 

Methods

extract :: Co f a -> a #

duplicate :: Co f a -> Co f (Co f a) #

extend :: (Co f a -> b) -> Co f a -> Co f b #

Representable f => Distributive (Co f) Source # 

Methods

distribute :: Functor f => f (Co f a) -> Co f (f a) #

collect :: Functor f => (a -> Co f b) -> f a -> Co f (f b) #

distributeM :: Monad m => m (Co f a) -> Co f (m a) #

collectM :: Monad m => (a -> Co f b) -> m a -> Co f (m b) #

Representable f => Apply (Co f) Source # 

Methods

(<.>) :: Co f (a -> b) -> Co f a -> Co f b #

(.>) :: Co f a -> Co f b -> Co f b #

(<.) :: Co f a -> Co f b -> Co f a #

liftF2 :: (a -> b -> c) -> Co f a -> Co f b -> Co f c #

Representable f => Bind (Co f) Source # 

Methods

(>>-) :: Co f a -> (a -> Co f b) -> Co f b #

join :: Co f (Co f a) -> Co f a #

(Representable f, Semigroup (Rep f)) => Extend (Co f) Source # 

Methods

duplicated :: Co f a -> Co f (Co f a) #

extended :: (Co f a -> b) -> Co f a -> Co f b #

Representable f => Representable (Co f) Source # 

Associated Types

type Rep (Co f :: * -> *) :: * Source #

Methods

tabulate :: (Rep (Co f) -> a) -> Co f a Source #

index :: Co f a -> Rep (Co f) -> a Source #

type Rep (Co f) Source # 
type Rep (Co f) = Rep f

Default definitions

Functor

fmapRep :: Representable f => (a -> b) -> f a -> f b Source #

Distributive

distributeRep :: (Representable f, Functor w) => w (f a) -> f (w a) Source #

collectRep :: (Representable f, Functor w) => (a -> f b) -> w a -> f (w b) Source #

Apply/Applicative

apRep :: Representable f => f (a -> b) -> f a -> f b Source #

pureRep :: Representable f => a -> f a Source #

liftR2 :: Representable f => (a -> b -> c) -> f a -> f b -> f c Source #

liftR3 :: Representable f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d Source #

Bind/Monad

bindRep :: Representable f => f a -> (a -> f b) -> f b Source #

MonadFix

mfixRep :: Representable f => (a -> f a) -> f a Source #

MonadZip

mzipRep :: Representable f => f a -> f b -> f (a, b) Source #

mzipWithRep :: Representable f => (a -> b -> c) -> f a -> f b -> f c Source #

MonadReader

localRep :: Representable f => (Rep f -> Rep f) -> f a -> f a Source #

Extend

duplicatedRep :: (Representable f, Semigroup (Rep f)) => f a -> f (f a) Source #

extendedRep :: (Representable f, Semigroup (Rep f)) => (f a -> b) -> f a -> f b Source #

Comonad

duplicateRep :: (Representable f, Monoid (Rep f)) => f a -> f (f a) Source #

extendRep :: (Representable f, Monoid (Rep f)) => (f a -> b) -> f a -> f b Source #

extractRep :: (Representable f, Monoid (Rep f)) => f a -> a Source #

Comonad, with user-specified monoid

duplicateRepBy :: Representable f => (Rep f -> Rep f -> Rep f) -> f a -> f (f a) Source #

extendRepBy :: Representable f => (Rep f -> Rep f -> Rep f) -> (f a -> b) -> f a -> f b Source #

extractRepBy :: Representable f => Rep f -> f a -> a Source #

WithIndex

imapRep :: Representable r => (Rep r -> a -> a') -> r a -> r a' Source #

ifoldMapRep :: forall r m a. (Representable r, Foldable r, Monoid m) => (Rep r -> a -> m) -> r a -> m Source #

itraverseRep :: forall r f a a'. (Representable r, Traversable r, Applicative f) => (Rep r -> a -> f a') -> r a -> f (r a') Source #

Generics

type GRep f = GRep' (Rep1 f) Source #

A default implementation of Rep for a datatype that is an instance of Generic1. This is usually composed of Either, tuples, unit tuples, and underlying Rep values. For instance, if you have:

data Foo a = MkFoo a (Bar a) (Baz (Quux a)) deriving (Functor, Generic1)
instance Representable Foo

Then you'll get:

GRep Foo = Either () (Either (WrappedRep Bar) (WrappedRep Baz, WrappedRep Quux))

(See the Haddocks for WrappedRep for an explanation of its purpose.)

gindex :: (Generic1 f, GRep f ~ Rep f, GIndex (Rep1 f)) => f a -> Rep f -> a Source #

A default implementation of index in terms of GRep.

gtabulate :: (Generic1 f, GRep f ~ Rep f, GTabulate (Rep1 f)) => (Rep f -> a) -> f a Source #

A default implementation of tabulate in terms of GRep.

newtype WrappedRep f Source #

On the surface, WrappedRec is a simple wrapper around Rep. But it plays a very important role: it prevents generic Representable instances for recursive types from sending the typechecker into an infinite loop. Consider the following datatype:

data Stream a = a :< Stream a deriving (Functor, Generic1)
instance Representable Stream

With WrappedRep, we have its Rep being:

Rep Stream = Either () (WrappedRep Stream)

If WrappedRep didn't exist, it would be:

Rep Stream = Either () (Either () (Either () ...))

An infinite type! WrappedRep breaks the potentially infinite loop.

Constructors

WrapRep 

Fields