{-# OPTIONS_GHC -Wno-orphans #-}

module Proarrow.Profunctor.Yoneda where

import Data.Function (($))
import Data.Kind (Type)

import Proarrow.Category.Instance.Prof (Prof (Prof))
import Proarrow.Core (CategoryOf (..), PRO, Profunctor (..), Promonad (..), (//), (:~>))
import Proarrow.Functor (Functor (..))
import Proarrow.Profunctor.Cofree (HasCofree (..))
import Proarrow.Profunctor.Star (Star (..))

type Yoneda :: (j -> k -> Type) -> PRO j k
data Yoneda p a b where
  Yoneda :: (Ob a, Ob b) => {forall {k} {k1} (a :: k) (b :: k1) (p :: k -> k1 -> Type).
Yoneda p a b -> Yo a b :~> p
unYoneda :: Yo a b :~> p} -> Yoneda p a b

instance (CategoryOf j, CategoryOf k) => Profunctor (Yoneda (p :: PRO j k)) where
  dimap :: forall (c :: j) (a :: j) (b :: k) (d :: k).
(c ~> a) -> (b ~> d) -> Yoneda p a b -> Yoneda p c d
dimap c ~> a
l b ~> d
r (Yoneda Yo a b :~> p
k) = c ~> a
l (c ~> a) -> ((Ob c, Ob a) => Yoneda p c d) -> Yoneda p c d
forall {k1} {k2} (p :: PRO k1 k2) (a :: k1) (b :: k2) r.
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
// b ~> d
r (b ~> d) -> ((Ob b, Ob d) => Yoneda p c d) -> Yoneda p c d
forall {k1} {k2} (p :: PRO k1 k2) (a :: k1) (b :: k2) r.
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
// (Yo c d :~> p) -> Yoneda p c d
forall {k} {k1} (a :: k) (b :: k1) (p :: k -> k1 -> Type).
(Ob a, Ob b) =>
(Yo a b :~> p) -> Yoneda p a b
Yoneda \(Yo a ~> c
ca d ~> b
bd) -> Yo a b a b -> p a b
Yo a b :~> p
k (Yo a b a b -> p a b) -> Yo a b a b -> p a b
forall a b. (a -> b) -> a -> b
$ (a ~> a) -> (b ~> b) -> Yo a b a b
forall {k} {k} (a :: k) (b :: k) (c :: k) (d :: k).
(c ~> a) -> (b ~> d) -> Yo a b c d
Yo (c ~> a
l (c ~> a) -> (a ~> c) -> a ~> a
forall (b :: j) (c :: j) (a :: j). (b ~> c) -> (a ~> b) -> a ~> c
forall {k} (p :: PRO k k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. a ~> c
ca) (d ~> b
bd (d ~> b) -> (b ~> d) -> b ~> b
forall (b :: k) (c :: k) (a :: k). (b ~> c) -> (a ~> b) -> a ~> c
forall {k} (p :: PRO k k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. b ~> d
r)
  (Ob a, Ob b) => r
r \\ :: forall (a :: j) (b :: k) r.
((Ob a, Ob b) => r) -> Yoneda p a b -> r
\\ Yoneda{} = r
(Ob a, Ob b) => r
r

instance Functor Yoneda where
  map :: forall (a :: j -> k -> Type) (b :: j -> k -> Type).
(a ~> b) -> Yoneda a ~> Yoneda b
map (Prof a :~> b
n) = (Yoneda a :~> Yoneda b) -> Prof (Yoneda a) (Yoneda b)
forall {k} {j} (p :: j +-> k) (q :: j +-> k).
(Profunctor p, Profunctor q) =>
(p :~> q) -> Prof p q
Prof \(Yoneda Yo a b :~> a
k) -> (Yo a b :~> b) -> Yoneda b a b
forall {k} {k1} (a :: k) (b :: k1) (p :: k -> k1 -> Type).
(Ob a, Ob b) =>
(Yo a b :~> p) -> Yoneda p a b
Yoneda (a a b -> b a b
a :~> b
n (a a b -> b a b) -> (Yo a b a b -> a a b) -> Yo a b a b -> b a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: PRO k k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Yo a b a b -> a a b
Yo a b :~> a
k)

instance HasCofree Profunctor where
  type Cofree Profunctor = Star Yoneda
  lower' :: forall (a :: PRO j k) (b :: PRO j k).
Cofree Profunctor a b -> a ~> b
lower' (Star (Prof a :~> Yoneda b
n)) = (a :~> b) -> Prof a b
forall {k} {j} (p :: j +-> k) (q :: j +-> k).
(Profunctor p, Profunctor q) =>
(p :~> q) -> Prof p q
Prof (Yoneda b a b -> b a b
Yoneda b :~> b
forall j k (p :: PRO j k).
(CategoryOf j, CategoryOf k) =>
Yoneda p :~> p
yoneda (Yoneda b a b -> b a b)
-> (a a b -> Yoneda b a b) -> a a b -> b a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: PRO k k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. a a b -> Yoneda b a b
a :~> Yoneda b
n)
  section' :: forall (b :: PRO j k) (a :: PRO j k).
Profunctor b =>
(a ~> b) -> Cofree Profunctor a b
section' (Prof a :~> b
n) = (a ~> Yoneda b) -> Star Yoneda a b
forall {k1} {k2} (b :: k1) (a :: k2) (f :: k1 -> k2).
Ob b =>
(a ~> f b) -> Star f a b
Star ((a :~> Yoneda b) -> Prof a (Yoneda b)
forall {k} {j} (p :: j +-> k) (q :: j +-> k).
(Profunctor p, Profunctor q) =>
(p :~> q) -> Prof p q
Prof (b a b -> Yoneda b a b
b :~> Yoneda b
forall {j} {k} (p :: PRO j k). Profunctor p => p :~> Yoneda p
mkYoneda (b a b -> Yoneda b a b)
-> (a a b -> b a b) -> a a b -> Yoneda b a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: PRO k k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. a a b -> b a b
a :~> b
n))

yoneda :: (CategoryOf j, CategoryOf k) => Yoneda (p :: PRO j k) :~> p
yoneda :: forall j k (p :: PRO j k).
(CategoryOf j, CategoryOf k) =>
Yoneda p :~> p
yoneda (Yoneda Yo a b :~> p
k) = Yo a b a b -> p a b
Yo a b :~> p
k (Yo a b a b -> p a b) -> Yo a b a b -> p a b
forall a b. (a -> b) -> a -> b
$ (a ~> a) -> (b ~> b) -> Yo a b a b
forall {k} {k} (a :: k) (b :: k) (c :: k) (d :: k).
(c ~> a) -> (b ~> d) -> Yo a b c d
Yo a ~> a
forall (a :: j). Ob a => a ~> a
forall {k} (p :: PRO k k) (a :: k). (Promonad p, Ob a) => p a a
id b ~> b
forall (a :: k). Ob a => a ~> a
forall {k} (p :: PRO k k) (a :: k). (Promonad p, Ob a) => p a a
id

mkYoneda :: (Profunctor p) => p :~> Yoneda p
mkYoneda :: forall {j} {k} (p :: PRO j k). Profunctor p => p :~> Yoneda p
mkYoneda p a b
p = p a b
p p a b -> ((Ob a, Ob b) => Yoneda p a b) -> Yoneda p a b
forall {k1} {k2} (p :: PRO k1 k2) (a :: k1) (b :: k2) r.
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
// (Yo a b :~> p) -> Yoneda p a b
forall {k} {k1} (a :: k) (b :: k1) (p :: k -> k1 -> Type).
(Ob a, Ob b) =>
(Yo a b :~> p) -> Yoneda p a b
Yoneda \(Yo a ~> a
ca b ~> b
bd) -> (a ~> a) -> (b ~> b) -> p a b -> p a b
forall (c :: j) (a :: j) (b :: k) (d :: k).
(c ~> a) -> (b ~> d) -> p a b -> p c d
forall {j} {k} (p :: PRO j k) (c :: j) (a :: j) (b :: k) (d :: k).
Profunctor p =>
(c ~> a) -> (b ~> d) -> p a b -> p c d
dimap a ~> a
ca b ~> b
bd p a b
p

-- | Yoneda embedding
data Yo a b c d = Yo (c ~> a) (b ~> d)

instance (CategoryOf j, CategoryOf k) => Profunctor (Yo (a :: j) (b :: k) :: PRO j k) where
  dimap :: forall (c :: j) (a :: j) (b :: k) (d :: k).
(c ~> a) -> (b ~> d) -> Yo a b a b -> Yo a b c d
dimap c ~> a
l b ~> d
r (Yo a ~> a
f b ~> b
g) = (c ~> a) -> (b ~> d) -> Yo a b c d
forall {k} {k} (a :: k) (b :: k) (c :: k) (d :: k).
(c ~> a) -> (b ~> d) -> Yo a b c d
Yo (a ~> a
f (a ~> a) -> (c ~> a) -> c ~> a
forall (b :: j) (c :: j) (a :: j). (b ~> c) -> (a ~> b) -> a ~> c
forall {k} (p :: PRO k k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. c ~> a
l) (b ~> d
r (b ~> d) -> (b ~> b) -> b ~> d
forall (b :: k) (c :: k) (a :: k). (b ~> c) -> (a ~> b) -> a ~> c
forall {k} (p :: PRO k k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. b ~> b
g)
  (Ob a, Ob b) => r
r \\ :: forall (a :: j) (b :: k) r. ((Ob a, Ob b) => r) -> Yo a b a b -> r
\\ Yo a ~> a
f b ~> b
g = r
(Ob a, Ob a) => r
(Ob a, Ob b) => r
r ((Ob a, Ob a) => r) -> (a ~> a) -> r
forall (a :: j) (b :: j) r. ((Ob a, Ob b) => r) -> (a ~> b) -> r
forall {j} {k} (p :: PRO j k) (a :: j) (b :: k) r.
Profunctor p =>
((Ob a, Ob b) => r) -> p a b -> r
\\ a ~> a
f ((Ob b, Ob b) => r) -> (b ~> b) -> r
forall (a :: k) (b :: k) r. ((Ob a, Ob b) => r) -> (a ~> b) -> r
forall {j} {k} (p :: PRO j k) (a :: j) (b :: k) r.
Profunctor p =>
((Ob a, Ob b) => r) -> p a b -> r
\\ b ~> b
g