module Data.Functor.Contravariant.Rep
(
Representable(..)
, tabulated
, contramapRep
) where
import Control.Monad.Reader
import Data.Functor.Contravariant
import Data.Functor.Product
import Data.Profunctor
import Data.Proxy
import Prelude hiding (lookup)
class Contravariant f => Representable f where
type Rep f :: *
tabulate :: (a -> Rep f) -> f a
index :: f a -> a -> Rep f
contramapWithRep :: (b -> Either a (Rep f)) -> f a -> f b
contramapWithRep f p = tabulate $ either (index p) id . f
tabulated :: (Representable f, Representable g, Profunctor p, Functor h)
=> p (f a) (h (g b)) -> p (a -> Rep f) (h (b -> Rep g))
tabulated = dimap tabulate (fmap index)
contramapRep :: Representable f => (a -> b) -> f b -> f a
contramapRep f = tabulate . (. f) . index
instance Representable Proxy where
type Rep Proxy = ()
tabulate _ = Proxy
index Proxy _ = ()
contramapWithRep _ Proxy = Proxy
instance Representable (Op r) where
type Rep (Op r) = r
tabulate = Op
index = getOp
instance Representable Predicate where
type Rep Predicate = Bool
tabulate = Predicate
index = getPredicate
instance (Representable f, Representable g) => Representable (Product f g) where
type Rep (Product f g) = (Rep f, Rep g)
tabulate f = Pair (tabulate (fst . f)) (tabulate (snd . f))
index (Pair f g) a = (index f a, index g a)
contramapWithRep h (Pair f g) = Pair
(contramapWithRep (fmap fst . h) f)
(contramapWithRep (fmap snd . h) g)