#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 702
#endif
module Data.Profunctor.Ran
( Ran(..)
, decomposeRan
, precomposeRan
, curryRan
, uncurryRan
) where
import Control.Category
import Data.Profunctor
import Data.Profunctor.Composition
import Data.Profunctor.Monad
import Data.Profunctor.Unsafe
import Prelude hiding (id,(.))
newtype Ran p q a b = Ran { runRan :: forall x. p x a -> q x b }
instance ProfunctorFunctor (Ran p) where
promap f (Ran g) = Ran (f . g)
instance Category p => ProfunctorComonad (Ran p) where
proextract (Ran f) = f id
produplicate (Ran f) = Ran $ \ p -> Ran $ \q -> f (p . q)
instance (Profunctor p, Profunctor q) => Profunctor (Ran p q) where
dimap ca bd f = Ran (rmap bd . runRan f . rmap ca)
lmap ca f = Ran (runRan f . rmap ca)
rmap bd f = Ran (rmap bd . runRan f)
bd #. f = Ran (\p -> bd #. runRan f p)
f .# ca = Ran (\p -> runRan f (ca #. p))
instance Profunctor q => Functor (Ran p q a) where
fmap bd f = Ran (rmap bd . runRan f)
instance p ~ q => Category (Ran p q) where
id = Ran id
Ran f . Ran g = Ran (f . g)
decomposeRan :: Procompose (Ran q p) q :-> p
decomposeRan (Procompose (Ran qp) q) = qp q
precomposeRan :: Profunctor q => Procompose q (Ran p (->)) :-> Ran p q
precomposeRan (Procompose p pf) = Ran (\pxa -> runRan pf pxa `lmap` p)
curryRan :: (Procompose p q :-> r) -> p :-> Ran q r
curryRan f p = Ran $ \q -> f (Procompose p q)
uncurryRan :: (p :-> Ran q r) -> Procompose p q :-> r
uncurryRan f (Procompose p q) = runRan (f p) q