{-# LANGUAGE Rank2Types, GADTs #-}
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 706
{-# LANGUAGE PolyKinds #-}
#endif
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
module Data.Functor.Kan.Ran
(
Ran(..)
, toRan, fromRan
, gran
, composeRan, decomposeRan
, adjointToRan, ranToAdjoint
, composedAdjointToRan, ranToComposedAdjoint
, repToRan, ranToRep
, composedRepToRan, ranToComposedRep
) where
import Data.Functor.Adjunction
import Data.Functor.Composition
import Data.Functor.Identity
import Data.Functor.Rep
newtype Ran g h a = Ran { forall {k} (g :: k -> *) (h :: k -> *) a.
Ran g h a -> forall (b :: k). (a -> g b) -> h b
runRan :: forall b. (a -> g b) -> h b }
instance Functor (Ran g h) where
fmap :: forall a b. (a -> b) -> Ran g h a -> Ran g h b
fmap a -> b
f Ran g h a
m = forall {k} (g :: k -> *) (h :: k -> *) a.
(forall (b :: k). (a -> g b) -> h b) -> Ran g h a
Ran (\b -> g b
k -> forall {k} (g :: k -> *) (h :: k -> *) a.
Ran g h a -> forall (b :: k). (a -> g b) -> h b
runRan Ran g h a
m (b -> g b
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f))
{-# INLINE fmap #-}
toRan :: Functor k => (forall a. k (g a) -> h a) -> k b -> Ran g h b
toRan :: forall {k} (k :: * -> *) (g :: k -> *) (h :: k -> *) b.
Functor k =>
(forall (a :: k). k (g a) -> h a) -> k b -> Ran g h b
toRan forall (a :: k). k (g a) -> h a
s k b
t = forall {k} (g :: k -> *) (h :: k -> *) a.
(forall (b :: k). (a -> g b) -> h b) -> Ran g h a
Ran (forall (a :: k). k (g a) -> h a
s forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap k b
t)
{-# INLINE toRan #-}
fromRan :: (forall a. k a -> Ran g h a) -> k (g b) -> h b
fromRan :: forall {k} (k :: * -> *) (g :: k -> *) (h :: k -> *) (b :: k).
(forall a. k a -> Ran g h a) -> k (g b) -> h b
fromRan forall a. k a -> Ran g h a
s k (g b)
kgb = forall {k} (g :: k -> *) (h :: k -> *) a.
Ran g h a -> forall (b :: k). (a -> g b) -> h b
runRan (forall a. k a -> Ran g h a
s k (g b)
kgb) forall a. a -> a
id
{-# INLINE fromRan #-}
composeRan :: Composition compose => Ran f (Ran g h) a -> Ran (compose f g) h a
composeRan :: forall (compose :: (* -> *) -> (* -> *) -> * -> *) (f :: * -> *)
(g :: * -> *) (h :: * -> *) a.
Composition compose =>
Ran f (Ran g h) a -> Ran (compose f g) h a
composeRan Ran f (Ran g h) a
r = forall {k} (g :: k -> *) (h :: k -> *) a.
(forall (b :: k). (a -> g b) -> h b) -> Ran g h a
Ran (\a -> compose f g b
f -> forall {k} (g :: k -> *) (h :: k -> *) a.
Ran g h a -> forall (b :: k). (a -> g b) -> h b
runRan (forall {k} (g :: k -> *) (h :: k -> *) a.
Ran g h a -> forall (b :: k). (a -> g b) -> h b
runRan Ran f (Ran g h) a
r (forall (o :: (* -> *) -> (* -> *) -> * -> *) (f :: * -> *)
(g :: * -> *) x.
Composition o =>
o f g x -> f (g x)
decompose forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> compose f g b
f)) forall a. a -> a
id)
{-# INLINE composeRan #-}
decomposeRan :: (Composition compose, Functor f) => Ran (compose f g) h a -> Ran f (Ran g h) a
decomposeRan :: forall (compose :: (* -> *) -> (* -> *) -> * -> *) (f :: * -> *)
(g :: * -> *) (h :: * -> *) a.
(Composition compose, Functor f) =>
Ran (compose f g) h a -> Ran f (Ran g h) a
decomposeRan Ran (compose f g) h a
r = forall {k} (g :: k -> *) (h :: k -> *) a.
(forall (b :: k). (a -> g b) -> h b) -> Ran g h a
Ran (\a -> f b
f -> forall {k} (g :: k -> *) (h :: k -> *) a.
(forall (b :: k). (a -> g b) -> h b) -> Ran g h a
Ran (\b -> g b
g -> forall {k} (g :: k -> *) (h :: k -> *) a.
Ran g h a -> forall (b :: k). (a -> g b) -> h b
runRan Ran (compose f g) h a
r (forall (o :: (* -> *) -> (* -> *) -> * -> *) (f :: * -> *)
(g :: * -> *) x.
Composition o =>
f (g x) -> o f g x
compose forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> g b
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f b
f)))
{-# INLINE decomposeRan #-}
adjointToRan :: Adjunction f g => f a -> Ran g Identity a
adjointToRan :: forall (f :: * -> *) (g :: * -> *) a.
Adjunction f g =>
f a -> Ran g Identity a
adjointToRan f a
f = forall {k} (g :: k -> *) (h :: k -> *) a.
(forall (b :: k). (a -> g b) -> h b) -> Ran g h a
Ran (\a -> g b
a -> forall a. a -> Identity a
Identity forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (u :: * -> *) a b.
Adjunction f u =>
(a -> u b) -> f a -> b
rightAdjunct a -> g b
a f a
f)
{-# INLINE adjointToRan #-}
ranToAdjoint :: Adjunction f g => Ran g Identity a -> f a
ranToAdjoint :: forall (f :: * -> *) (g :: * -> *) a.
Adjunction f g =>
Ran g Identity a -> f a
ranToAdjoint Ran g Identity a
r = forall a. Identity a -> a
runIdentity (forall {k} (g :: k -> *) (h :: k -> *) a.
Ran g h a -> forall (b :: k). (a -> g b) -> h b
runRan Ran g Identity a
r forall (f :: * -> *) (u :: * -> *) a.
Adjunction f u =>
a -> u (f a)
unit)
{-# INLINE ranToAdjoint #-}
ranToComposedAdjoint :: Adjunction f g => Ran g h a -> h (f a)
ranToComposedAdjoint :: forall (f :: * -> *) (g :: * -> *) (h :: * -> *) a.
Adjunction f g =>
Ran g h a -> h (f a)
ranToComposedAdjoint Ran g h a
r = forall {k} (g :: k -> *) (h :: k -> *) a.
Ran g h a -> forall (b :: k). (a -> g b) -> h b
runRan Ran g h a
r forall (f :: * -> *) (u :: * -> *) a.
Adjunction f u =>
a -> u (f a)
unit
{-# INLINE ranToComposedAdjoint #-}
composedAdjointToRan :: (Adjunction f g, Functor h) => h (f a) -> Ran g h a
composedAdjointToRan :: forall (f :: * -> *) (g :: * -> *) (h :: * -> *) a.
(Adjunction f g, Functor h) =>
h (f a) -> Ran g h a
composedAdjointToRan h (f a)
f = forall {k} (g :: k -> *) (h :: k -> *) a.
(forall (b :: k). (a -> g b) -> h b) -> Ran g h a
Ran (\a -> g b
a -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) (u :: * -> *) a b.
Adjunction f u =>
(a -> u b) -> f a -> b
rightAdjunct a -> g b
a) h (f a)
f)
{-# INLINE composedAdjointToRan #-}
gran :: Ran g h (g a) -> h a
gran :: forall {k} (g :: k -> *) (h :: k -> *) (a :: k).
Ran g h (g a) -> h a
gran (Ran forall (b :: k). (g a -> g b) -> h b
f) = forall (b :: k). (g a -> g b) -> h b
f forall a. a -> a
id
{-# INLINE gran #-}
repToRan :: Representable u => Rep u -> a -> Ran u Identity a
repToRan :: forall (u :: * -> *) a.
Representable u =>
Rep u -> a -> Ran u Identity a
repToRan Rep u
e a
a = forall {k} (g :: k -> *) (h :: k -> *) a.
(forall (b :: k). (a -> g b) -> h b) -> Ran g h a
Ran forall a b. (a -> b) -> a -> b
$ \a -> u b
k -> forall a. a -> Identity a
Identity forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index (a -> u b
k a
a) Rep u
e
{-# INLINE repToRan #-}
ranToRep :: Representable u => Ran u Identity a -> (Rep u, a)
ranToRep :: forall (u :: * -> *) a.
Representable u =>
Ran u Identity a -> (Rep u, a)
ranToRep (Ran forall b. (a -> u b) -> Identity b
f) = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall b. (a -> u b) -> Identity b
f (\a
a -> forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate forall a b. (a -> b) -> a -> b
$ \Rep u
e -> (Rep u
e, a
a))
{-# INLINE ranToRep #-}
ranToComposedRep :: Representable u => Ran u h a -> h (Rep u, a)
ranToComposedRep :: forall (u :: * -> *) (h :: * -> *) a.
Representable u =>
Ran u h a -> h (Rep u, a)
ranToComposedRep (Ran forall b. (a -> u b) -> h b
f) = forall b. (a -> u b) -> h b
f (\a
a -> forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate forall a b. (a -> b) -> a -> b
$ \Rep u
e -> (Rep u
e, a
a))
{-# INLINE ranToComposedRep #-}
composedRepToRan :: (Representable u, Functor h) => h (Rep u, a) -> Ran u h a
composedRepToRan :: forall (u :: * -> *) (h :: * -> *) a.
(Representable u, Functor h) =>
h (Rep u, a) -> Ran u h a
composedRepToRan h (Rep u, a)
hfa = forall {k} (g :: k -> *) (h :: k -> *) a.
(forall (b :: k). (a -> g b) -> h b) -> Ran g h a
Ran forall a b. (a -> b) -> a -> b
$ \a -> u b
k -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Rep u
e, a
a) -> forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index (a -> u b
k a
a) Rep u
e) h (Rep u, a)
hfa
{-# INLINE composedRepToRan #-}