{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
module Data.Functor.Invariant.Day
( Day(..)
, day
, assoc, disassoc
, swapped
, intro1, intro2
, elim1, elim2
, trans1, trans2
, toContravariant, toCovariant
) where
import qualified Data.Functor.Contravariant.Day as Contravariant
import qualified Data.Functor.Day as Covariant
import Data.Functor.Identity
import Data.Functor.Invariant
data Day f g a = forall b c. Day (f b) (g c) (b -> c -> a) (a -> (b, c))
instance Invariant (Day f g) where
invmap :: forall a b. (a -> b) -> (b -> a) -> Day f g a -> Day f g b
invmap a -> b
f b -> a
g (Day f b
fb g c
gc b -> c -> a
bca a -> (b, c)
abc) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
Day f b
fb g c
gc ((a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
.) forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> c -> a
bca) (a -> (b, c)
abc forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
g)
day :: f a -> g b -> Day f g (a, b)
day :: forall (f :: * -> *) a (g :: * -> *) b.
f a -> g b -> Day f g (a, b)
day f a
fa g b
gb = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
Day f a
fa g b
gb (,) forall a. a -> a
id
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 c -> (b, c)
cde) b -> c -> a
bca a -> (b, c)
abc) = forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
Day (forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
Day f b
fb g b
gd (,) forall a. a -> a
id) h c
he) a -> ((b, b), c)
f (b, b) -> c -> a
g
where
f :: a -> ((b, b), c)
f a
a =
let (b
b,c
c) = a -> (b, c)
abc a
a
(b
d,c
e) = c -> (b, c)
cde c
c
in ((b
b,b
d),c
e)
g :: (b, b) -> c -> a
g (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
deb b -> (b, c)
bde) h c
hd b -> c -> a
bca a -> (b, c)
abc) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
Day f b
fb (forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
Day g c
gc h c
hd (,) forall a. a -> a
id) b -> (c, c) -> a
f a -> (b, (c, c))
g
where
f :: b -> (c, c) -> a
f b
e (c
d,c
c) =
b -> c -> a
bca (b -> c -> b
deb b
e c
d) c
c
g :: a -> (b, (c, c))
g a
a =
let (b
b,c
c) = a -> (b, c)
abc a
a
(b
d,c
e) = b -> (b, c)
bde b
b
in (b
d,(c
e,c
c))
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
bca a -> (b, c)
abc) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> 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
bca) (\a
a -> let (b
b, c
c) = a -> (b, c)
abc a
a in (c
c, b
b))
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) -> (a -> (b, c)) -> Day f g a
Day (forall a. a -> Identity a
Identity ()) f a
fa (\()
_ 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) -> (a -> (b, c)) -> Day f g a
Day f a
fa (forall a. a -> Identity a
Identity ()) forall a b. a -> b -> a
const (forall a b c. (a -> b -> c) -> b -> a -> c
flip (,) ())
elim1 :: Invariant f => Day Identity f a -> f a
elim1 :: forall (f :: * -> *) a. Invariant f => Day Identity f a -> f a
elim1 (Day (Identity b
b) f c
fc b -> c -> a
bca a -> (b, c)
abc) = forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap (b -> c -> a
bca b
b) (forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> (b, c)
abc) f c
fc
elim2 :: Invariant f => Day f Identity a -> f a
elim2 :: forall (f :: * -> *) a. Invariant f => Day f Identity a -> f a
elim2 (Day f b
fb (Identity c
c) b -> c -> a
bca a -> (b, c)
abc) = forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap (forall a b c. (a -> b -> c) -> b -> a -> c
flip b -> c -> a
bca c
c) (forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> (b, c)
abc) f b
fb
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 a -> (b, c)
abc) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
Day (forall x. f x -> g x
fg f b
fb) h c
hc b -> c -> a
bca a -> (b, c)
abc
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 a -> (b, c)
abc) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> (a -> (b, c)) -> Day f g a
Day f b
fb (forall x. g x -> h x
gh g c
gc) b -> c -> a
bca a -> (b, c)
abc
toContravariant :: Day f g a -> Contravariant.Day f g a
toContravariant :: forall (f :: * -> *) (g :: * -> *) a. Day f g a -> Day f g a
toContravariant (Day f b
fb g c
gc b -> c -> a
_ a -> (b, c)
abc) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (a -> (b, c)) -> Day f g a
Contravariant.Day f b
fb g c
gc a -> (b, c)
abc
toCovariant :: Day f g a -> Covariant.Day f g a
toCovariant :: forall (f :: * -> *) (g :: * -> *) a. Day f g a -> Day f g a
toCovariant (Day f b
fb g c
gc b -> c -> a
bca a -> (b, c)
_) = forall (f :: * -> *) (g :: * -> *) a b c.
f b -> g c -> (b -> c -> a) -> Day f g a
Covariant.Day f b
fb g c
gc b -> c -> a
bca