module Data.Functor.Invariant.Night (
Night(..)
, Not(..), refuted
, night
, runNight
, nerve
, runNightAlt
, runNightDecide
, toCoNight
, toCoNight_
, toContraNight
, assoc, unassoc
, intro1, intro2
, elim1, elim2
, swapped
, trans1, trans2
) where
import Control.Natural
import Data.Bifunctor
import Data.Functor.Alt
import Data.Functor.Contravariant.Decide
import Data.Functor.Contravariant.Night (Not(..), refuted)
import Data.Functor.Invariant
import Data.Functor.Invariant.Internative
import Data.Kind
import Data.Void
import GHC.Generics
import qualified Data.Bifunctor.Assoc as B
import qualified Data.Bifunctor.Swap as B
import qualified Data.Functor.Contravariant.Night as CN
import qualified Data.Functor.Coyoneda as CY
data Night :: (Type -> Type) -> (Type -> Type) -> (Type -> Type) where
Night :: f b
-> g c
-> (b -> a)
-> (c -> a)
-> (a -> Either b c)
-> Night f g a
instance Invariant (Night f g) where
invmap :: forall a b. (a -> b) -> (b -> a) -> Night f g a -> Night f g b
invmap a -> b
f b -> a
g (Night f b
x g c
y b -> a
h c -> a
j a -> Either b c
k) = forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night f b
x g c
y (a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
h) (a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> a
j) (a -> Either b c
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
g)
night :: f a -> g b -> Night f g (Either a b)
night :: forall (f :: * -> *) a (g :: * -> *) b.
f a -> g b -> Night f g (Either a b)
night f a
x g b
y = forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night f a
x g b
y forall a b. a -> Either a b
Left forall a b. b -> Either a b
Right forall a. a -> a
id
runNightAlt
:: forall f g h. Alt h
=> f ~> h
-> g ~> h
-> Night f g ~> h
runNightAlt :: forall (f :: * -> *) (g :: * -> *) (h :: * -> *).
Alt h =>
(f ~> h) -> (g ~> h) -> Night f g ~> h
runNightAlt f ~> h
f g ~> h
g (Night f b
x g c
y b -> x
h c -> x
j x -> Either b c
_) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> x
h (f ~> h
f f b
x) forall (f :: * -> *) a. Alt f => f a -> f a -> f a
<!> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> x
j (g ~> h
g g c
y)
runNightDecide
:: forall f g h. Decide h
=> f ~> h
-> g ~> h
-> Night f g ~> h
runNightDecide :: forall (f :: * -> *) (g :: * -> *) (h :: * -> *).
Decide h =>
(f ~> h) -> (g ~> h) -> Night f g ~> h
runNightDecide f ~> h
f g ~> h
g (Night f b
x g c
y b -> x
_ c -> x
_ x -> Either b c
k) = forall (f :: * -> *) a b c.
Decide f =>
(a -> Either b c) -> f b -> f c -> f a
decide x -> Either b c
k (f ~> h
f f b
x) (g ~> h
g g c
y)
toCoNight :: (Functor f, Functor g) => Night f g ~> f :*: g
toCoNight :: forall (f :: * -> *) (g :: * -> *).
(Functor f, Functor g) =>
Night f g ~> (f :*: g)
toCoNight (Night f b
x g c
y b -> x
f c -> x
g x -> Either b c
_) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> x
f f b
x forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> x
g g c
y
toCoNight_ :: Night f g ~> CY.Coyoneda f :*: CY.Coyoneda g
toCoNight_ :: forall (f :: * -> *) (g :: * -> *).
Night f g ~> (Coyoneda f :*: Coyoneda g)
toCoNight_ (Night f b
x g c
y b -> x
f c -> x
g x -> Either b c
_) = forall b a (f :: * -> *). (b -> a) -> f b -> Coyoneda f a
CY.Coyoneda b -> x
f f b
x forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: forall b a (f :: * -> *). (b -> a) -> f b -> Coyoneda f a
CY.Coyoneda c -> x
g g c
y
toContraNight :: Night f g ~> CN.Night f g
toContraNight :: forall (f :: * -> *) (g :: * -> *). Night f g ~> Night f g
toContraNight (Night f b
x g c
y b -> x
_ c -> x
_ x -> Either b c
h) = forall (a :: * -> *) b1 (b :: * -> *) c1 c.
a b1 -> b c1 -> (c -> Either b1 c1) -> Night a b c
CN.Night f b
x g c
y x -> Either b c
h
runNight
:: Inalt h
=> (f ~> h)
-> (g ~> h)
-> Night f g ~> h
runNight :: forall (h :: * -> *) (f :: * -> *) (g :: * -> *).
Inalt h =>
(f ~> h) -> (g ~> h) -> Night f g ~> h
runNight f ~> h
f g ~> h
g (Night f b
x g c
y b -> x
a c -> x
b x -> Either b c
c) = forall (f :: * -> *) b a c.
Inalt f =>
(b -> a) -> (c -> a) -> (a -> Either b c) -> f b -> f c -> f a
swerve b -> x
a c -> x
b x -> Either b c
c (f ~> h
f f b
x) (g ~> h
g g c
y)
nerve
:: Inalt f
=> Night f f ~> f
nerve :: forall (f :: * -> *). Inalt f => Night f f ~> f
nerve (Night f b
x f c
y b -> x
a c -> x
b x -> Either b c
c) = forall (f :: * -> *) b a c.
Inalt f =>
(b -> a) -> (c -> a) -> (a -> Either b c) -> f b -> f c -> f a
swerve b -> x
a c -> x
b x -> Either b c
c f b
x f c
y
assoc :: Night f (Night g h) ~> Night (Night f g) h
assoc :: forall (f :: * -> *) (g :: * -> *) (h :: * -> *).
Night f (Night g h) ~> Night (Night f g) h
assoc (Night f b
x (Night g b
y h c
z b -> c
f c -> c
g c -> Either b c
h) b -> x
j c -> x
k x -> Either b c
l) =
forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night (forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night f b
x g b
y forall a b. a -> Either a b
Left forall a b. b -> Either a b
Right forall a. a -> a
id) h c
z
(forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either b -> x
j (c -> x
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> c
f))
(c -> x
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> c
g)
(forall (p :: * -> * -> *) a b c.
Assoc p =>
p a (p b c) -> p (p a b) c
B.unassoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second c -> Either b c
h forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> Either b c
l)
unassoc :: Night (Night f g) h ~> Night f (Night g h)
unassoc :: forall (f :: * -> *) (g :: * -> *) (h :: * -> *).
Night (Night f g) h ~> Night f (Night g h)
unassoc (Night (Night f b
x g c
y b -> b
f c -> b
g b -> Either b c
h) h c
z b -> x
j c -> x
k x -> Either b c
l) =
forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night f b
x (forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night g c
y h c
z forall a b. a -> Either a b
Left forall a b. b -> Either a b
Right forall a. a -> a
id)
(b -> x
j forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> b
f)
(forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (b -> x
j forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> b
g) c -> x
k)
(forall (p :: * -> * -> *) a b c.
Assoc p =>
p (p a b) c -> p a (p b c)
B.assoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first b -> Either b c
h forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> Either b c
l)
intro1 :: g ~> Night Not g
intro1 :: forall (g :: * -> *). g ~> Night Not g
intro1 g x
y = forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night Not Void
refuted g x
y forall a. Void -> a
absurd forall a. a -> a
id forall a b. b -> Either a b
Right
intro2 :: f ~> Night f Not
intro2 :: forall (f :: * -> *). f ~> Night f Not
intro2 f x
x = forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night f x
x Not Void
refuted forall a. a -> a
id forall a. Void -> a
absurd forall a b. a -> Either a b
Left
elim1 :: Invariant g => Night Not g ~> g
elim1 :: forall (g :: * -> *). Invariant g => Night Not g ~> g
elim1 (Night Not b
x g c
y b -> x
_ c -> x
g x -> Either b c
h) = forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap c -> x
g (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a. Void -> a
absurd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Not a -> a -> Void
refute Not b
x) forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> Either b c
h) g c
y
elim2 :: Invariant f => Night f Not ~> f
elim2 :: forall (f :: * -> *). Invariant f => Night f Not ~> f
elim2 (Night f b
x Not c
y b -> x
f c -> x
_ x -> Either b c
h) = forall (f :: * -> *) a b.
Invariant f =>
(a -> b) -> (b -> a) -> f a -> f b
invmap b -> x
f (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. a -> a
id (forall a. Void -> a
absurd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Not a -> a -> Void
refute Not c
y) forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> Either b c
h) f b
x
swapped :: Night f g ~> Night g f
swapped :: forall (f :: * -> *) (g :: * -> *). Night f g ~> Night g f
swapped (Night f b
x g c
y b -> x
f c -> x
g x -> Either b c
h) = forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night g c
y f b
x c -> x
g b -> x
f (forall (p :: * -> * -> *) a b. Swap p => p a b -> p b a
B.swap forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> Either b c
h)
trans1 :: f ~> h -> Night f g ~> Night h g
trans1 :: forall (f :: * -> *) (h :: * -> *) (g :: * -> *).
(f ~> h) -> Night f g ~> Night h g
trans1 f ~> h
f (Night f b
x g c
y b -> x
g c -> x
h x -> Either b c
j) = forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night (f ~> h
f f b
x) g c
y b -> x
g c -> x
h x -> Either b c
j
trans2 :: g ~> h -> Night f g ~> Night f h
trans2 :: forall (g :: * -> *) (h :: * -> *) (f :: * -> *).
(g ~> h) -> Night f g ~> Night f h
trans2 g ~> h
f (Night f b
x g c
y b -> x
g c -> x
h x -> Either b c
j) = forall (f :: * -> *) b (g :: * -> *) c a.
f b
-> g c -> (b -> a) -> (c -> a) -> (a -> Either b c) -> Night f g a
Night f b
x (g ~> h
f g c
y) b -> x
g c -> x
h x -> Either b c
j