{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE RankNTypes #-}
module Data.Functor.Day
( Day(..)
, day
, dap
, assoc, disassoc
, swapped
, intro1, intro2
, elim1, elim2
, trans1, trans2
, cayley, dayley
) where
import Control.Applicative
import Control.Category
import Control.Comonad
import Control.Comonad.Trans.Class
import Data.Distributive
import Data.Profunctor.Cayley (Cayley(..))
import Data.Profunctor.Composition (Procompose(..))
import Data.Functor.Identity
import Data.Functor.Rep
#ifdef __GLASGOW_HASKELL__
import Data.Typeable
#endif
import Prelude hiding (id,(.))
data Day f g a = forall b c. Day (f b) (g c) (b -> c -> a)
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 707
deriving Typeable
#endif
day :: f (a -> b) -> g a -> Day f g b
day :: forall (f :: * -> *) a b (g :: * -> *).
f (a -> b) -> g a -> Day f g b
day f (a -> b)
fa g a
gb = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day f (a -> b)
fa g a
gb forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ < 707
instance (Typeable1 f, Typeable1 g) => Typeable1 (Day f g) where
typeOf1 tfga = mkTyConApp dayTyCon [typeOf1 (fa tfga), typeOf1 (ga tfga)]
where fa :: t f (g :: * -> *) a -> f a
fa = undefined
ga :: t (f :: * -> *) g a -> g a
ga = undefined
dayTyCon :: TyCon
#if MIN_VERSION_base(4,4,0)
dayTyCon = mkTyCon3 "contravariant" "Data.Functor.Day" "Day"
#else
dayTyCon = mkTyCon "Data.Functor.Day.Day"
#endif
#endif
instance Functor (Day f g) where
fmap :: forall a b. (a -> b) -> Day f g a -> Day f g b
fmap a -> b
f (Day f b
fb g c
gc b -> c -> a
bca) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day f b
fb g c
gc forall a b. (a -> b) -> a -> b
$ \b
b c
c -> a -> b
f (b -> c -> a
bca b
b c
c)
instance (Applicative f, Applicative g) => Applicative (Day f g) where
pure :: forall a. a -> Day f g a
pure a
x = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day (forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (\()
_ ()
_ -> a
x)
(Day f b
fa g c
fb b -> c -> a -> b
u) <*> :: forall a b. Day f g (a -> b) -> Day f g a -> Day f g b
<*> (Day f b
gc g c
gd b -> c -> a
v) =
forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day ((,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f b
fa forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f b
gc) ((,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> g c
fb forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> g c
gd)
(\(b
a,b
c) (c
b,c
d) -> b -> c -> a -> b
u b
a c
b (b -> c -> a
v b
c c
d))
instance (Representable f, Representable g) => Distributive (Day f g) where
distribute :: forall (f :: * -> *) a. Functor f => f (Day f g a) -> Day f g (f a)
distribute f (Day f g a)
f = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day (forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id) (forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id) forall a b. (a -> b) -> a -> b
$ \Rep f
x Rep g
y ->
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Day f b
m g c
n b -> c -> a
o) -> b -> c -> a
o (forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index f b
m Rep f
x) (forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index g c
n Rep g
y)) f (Day f g a)
f
collect :: forall (f :: * -> *) a b.
Functor f =>
(a -> Day f g b) -> f a -> Day f g (f b)
collect a -> Day f g b
g f a
f = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day (forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id) (forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id) forall a b. (a -> b) -> a -> b
$ \Rep f
x Rep g
y ->
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
q -> case a -> Day f g b
g a
q of Day f b
m g c
n b -> c -> b
o -> b -> c -> b
o (forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index f b
m Rep f
x) (forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index g c
n Rep g
y)) f a
f
instance (Representable f, Representable g) => Representable (Day f g) where
type Rep (Day f g) = (Rep f, Rep g)
tabulate :: forall a. (Rep (Day f g) -> a) -> Day f g a
tabulate Rep (Day f g) -> a
f = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day (forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id) (forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id) (forall a b c. ((a, b) -> c) -> a -> b -> c
curry Rep (Day f g) -> a
f)
index :: forall a. Day f g a -> Rep (Day f g) -> a
index (Day f b
m g c
n b -> c -> a
o) (Rep f
x,Rep g
y) = b -> c -> a
o (forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index f b
m Rep f
x) (forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index g c
n Rep g
y)
instance (Comonad f, Comonad g) => Comonad (Day f g) where
extract :: forall a. Day f g a -> a
extract (Day f b
fb g c
gc b -> c -> a
bca) = b -> c -> a
bca (forall (w :: * -> *) a. Comonad w => w a -> a
extract f b
fb) (forall (w :: * -> *) a. Comonad w => w a -> a
extract g c
gc)
duplicate :: forall a. Day f g a -> Day f g (Day f g a)
duplicate (Day f b
fb g c
gc b -> c -> a
bca) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day (forall (w :: * -> *) a. Comonad w => w a -> w (w a)
duplicate f b
fb) (forall (w :: * -> *) a. Comonad w => w a -> w (w a)
duplicate g c
gc) (\f b
fb' g c
gc' -> forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day f b
fb' g c
gc' b -> c -> a
bca)
instance (ComonadApply f, ComonadApply g) => ComonadApply (Day f g) where
Day f b
fa g c
fb b -> c -> a -> b
u <@> :: forall a b. Day f g (a -> b) -> Day f g a -> Day f g b
<@> Day f b
gc g c
gd b -> c -> a
v =
forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day ((,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f b
fa forall (w :: * -> *) a b.
ComonadApply w =>
w (a -> b) -> w a -> w b
<@> f b
gc) ((,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> g c
fb forall (w :: * -> *) a b.
ComonadApply w =>
w (a -> b) -> w a -> w b
<@> g c
gd)
(\(b
a,b
c) (c
b,c
d) -> b -> c -> a -> b
u b
a c
b (b -> c -> a
v b
c c
d))
instance Comonad f => ComonadTrans (Day f) where
lower :: forall (w :: * -> *) a. Comonad w => Day f w a -> w a
lower (Day f b
fb w c
gc b -> c -> a
bca) = b -> c -> a
bca (forall (w :: * -> *) a. Comonad w => w a -> a
extract f b
fb) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> w c
gc
assoc :: Day f (Day g h) a -> Day (Day f g) h a
assoc :: forall (f :: * -> *) (g :: * -> *) (h :: * -> *) a.
Day f (Day g h) a -> Day (Day f g) h a
assoc (Day f b
fb (Day g b
gd h c
he b -> c -> c
dec) b -> c -> a
bca) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day (forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day f b
fb g b
gd (,)) h c
he forall a b. (a -> b) -> a -> b
$
\ (b
b,b
d) c
e -> b -> c -> a
bca b
b (b -> c -> c
dec b
d c
e)
disassoc :: Day (Day f g) h a -> Day f (Day g h) a
disassoc :: forall (f :: * -> *) (g :: * -> *) (h :: * -> *) a.
Day (Day f g) h a -> Day f (Day g h) a
disassoc (Day (Day f b
fb g c
gc b -> c -> b
bce) h c
hd b -> c -> a
eda) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day f b
fb (forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day g c
gc h c
hd (,)) forall a b. (a -> b) -> a -> b
$ \ b
b (c
c,c
d) ->
b -> c -> a
eda (b -> c -> b
bce b
b c
c) c
d
swapped :: Day f g a -> Day g f a
swapped :: forall (f :: * -> *) (g :: * -> *) a. Day f g a -> Day g f a
swapped (Day f b
fb g c
gc b -> c -> a
abc) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day g c
gc f b
fb (forall a b c. (a -> b -> c) -> b -> a -> c
flip b -> c -> a
abc)
intro1 :: f a -> Day Identity f a
intro1 :: forall (f :: * -> *) a. f a -> Day Identity f a
intro1 f a
fa = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day (forall a. a -> Identity a
Identity ()) f a
fa forall a b. (a -> b) -> a -> b
$ \()
_ a
a -> a
a
intro2 :: f a -> Day f Identity a
intro2 :: forall (f :: * -> *) a. f a -> Day f Identity a
intro2 f a
fa = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day f a
fa (forall a. a -> Identity a
Identity ()) forall a b. a -> b -> a
const
elim1 :: Functor f => Day Identity f a -> f a
elim1 :: forall (f :: * -> *) a. Functor f => Day Identity f a -> f a
elim1 (Day (Identity b
b) f c
fc b -> c -> a
bca) = b -> c -> a
bca b
b forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f c
fc
elim2 :: Functor f => Day f Identity a -> f a
elim2 :: forall (f :: * -> *) a. Functor f => Day f Identity a -> f a
elim2 (Day f b
fb (Identity c
c) b -> c -> a
bca) = forall a b c. (a -> b -> c) -> b -> a -> c
flip b -> c -> a
bca c
c forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f b
fb
dap :: Applicative f => Day f f a -> f a
dap :: forall (f :: * -> *) a. Applicative f => Day f f a -> f a
dap (Day f b
fb f c
fc b -> c -> a
abc) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 b -> c -> a
abc f b
fb f c
fc
trans1 :: (forall x. f x -> g x) -> Day f h a -> Day g h a
trans1 :: forall (f :: * -> *) (g :: * -> *) (h :: * -> *) a.
(forall x. f x -> g x) -> Day f h a -> Day g h a
trans1 forall x. f x -> g x
fg (Day f b
fb h c
hc b -> c -> a
bca) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day (forall x. f x -> g x
fg f b
fb) h c
hc b -> c -> a
bca
trans2 :: (forall x. g x -> h x) -> Day f g a -> Day f h a
trans2 :: forall (g :: * -> *) (h :: * -> *) (f :: * -> *) a.
(forall x. g x -> h x) -> Day f g a -> Day f h a
trans2 forall x. g x -> h x
gh (Day f b
fb g c
gc b -> c -> a
bca) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day f b
fb (forall x. g x -> h x
gh g c
gc) b -> c -> a
bca
cayley :: Procompose (Cayley f p) (Cayley g q) a b -> Cayley (Day f g) (Procompose p q) a b
cayley :: forall (f :: * -> *) (p :: * -> * -> *) (g :: * -> *)
(q :: * -> * -> *) a b.
Procompose (Cayley f p) (Cayley g q) a b
-> Cayley (Day f g) (Procompose p q) a b
cayley (Procompose (Cayley f (p x b)
p) (Cayley g (q a x)
q)) = forall {k} {k1} {k2} (f :: k -> *) (p :: k1 -> k2 -> k) (a :: k1)
(b :: k2).
f (p a b) -> Cayley f p a b
Cayley forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day f (p x b)
p g (q a x)
q forall {k} {k1} {k2} (p :: k -> k1 -> *) (x :: k) (c :: k1)
(q :: k2 -> k -> *) (d :: k2).
p x c -> q d x -> Procompose p q d c
Procompose
dayley :: Category p => Procompose (Cayley f p) (Cayley g p) a b -> Cayley (Day f g) p a b
dayley :: forall (p :: * -> * -> *) (f :: * -> *) (g :: * -> *) a b.
Category p =>
Procompose (Cayley f p) (Cayley g p) a b -> Cayley (Day f g) p a b
dayley (Procompose (Cayley f (p x b)
p) (Cayley g (p a x)
q)) = forall {k} {k1} {k2} (f :: k -> *) (p :: k1 -> k2 -> k) (a :: k1)
(b :: k2).
f (p a b) -> Cayley f p a b
Cayley forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Day f (p x b)
p g (p a x)
q forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
(.)