{-# LANGUAGE AllowAmbiguousTypes #-}
{-# OPTIONS_GHC -Wno-orphans #-}

-- | The __iso__: the bottom of the subtyping lattice, usable as every other flavor. 'IsoFl' is
-- the conjunction of the five maximal flavors ('Proarrow.Optic.Lens.LensFl',
-- 'Proarrow.Optic.Prism.PrismFl', 'Proarrow.Optic.PowerGrate.PowerGrateFl',
-- 'Proarrow.Optic.MonoidalLens.MonLensFl' and 'Proarrow.Optic.Tracer.TracerFl'). Build with
-- 'iso', and eliminate to the two legs with 'withIso' via the 'Yo' carrier. That carrier also
-- eliminates 'Proarrow.Optic.re'-versed isos, a conversion the subtyping lattice itself cannot
-- express. 'fromPIso'\/'toPIso' mediate with the profunctor-class-flavored 'PIso'.
module Proarrow.Optic.Iso where

import Data.Kind (Constraint)
import Data.Type.Equality ((:~:) (..))
import Proarrow.Category.Instance.Opposite (OPPOSITE (..))
import Prelude (Maybe, fmap)

import Proarrow.Category.Monoidal (Monoidal (..), MonoidalProfunctor (..))
import Proarrow.Core (CategoryOf (..), Kind, Profunctor (..), Promonad (..), obj, type (+->))
import Proarrow.Object (pattern Objs)
import Proarrow.Optic
  ( FLAVOR
  , Flip
  , Optic
  , Optic_ (..)
  , PIso
  , Prostrong (..)
  , Sub (..)
  , convert
  , iso
  )
import Proarrow.Optic.Getter (getP)
import Proarrow.Optic.Lens (LensFl)
import Proarrow.Optic.MonoidalLens (MonLensFl)
import Proarrow.Optic.PowerGrate (PowerGrateFl)
import Proarrow.Optic.Prism (PrismFl)
import Proarrow.Optic.TensorOps (Backward (..), Continuation (..), TensorRight (..))
import Proarrow.Optic.Tracer (TracerFl)
import Proarrow.Profunctor.Instance.Composition ((:.:) (..))
import Proarrow.Profunctor.Instance.Yoneda (Yo (..))

-- | The iso flavor. Reversed isos still view\/preview\/fold (@'Proarrow.Optic.re' iso@ is a getter,
-- and more), through the @'Proarrow.Optic.Getter.GetterFl' q p@ superclass of 'PrismFl'.
class (LensFl p q, PrismFl p q, PowerGrateFl p q, MonLensFl p q, TracerFl p q) => IsoFl p q

instance (LensFl p q, PrismFl p q, PowerGrateFl p q, MonLensFl p q, TracerFl p q) => IsoFl p q

-- | The 'Prostrong'-flavored iso; for the profunctor-class-flavored encoding see 'Proarrow.Optic.PIso'.
type Iso (s :: k) (t :: k) a b = Optic (Prostrong IsoFl) s t a b

type Iso' s a = Iso s s a a

-- | Any flavor whose optics are isos has strength for the 'Yo' profunctor.
instance (CategoryOf k, forall p q. (w p q) => Sub IsoFl p q) => Prostrong (w :: FLAVOR k k) (Yo a (OP b) :: k +-> k) where
  proact :: forall (f :: k +-> k) (g :: k +-> k).
(w f g, Profunctor f, Profunctor g) =>
((f :.: Yo a (OP b)) :.: g) :~> Yo a (OP b)
proact @f @g (f a b
f :.: Yo b ~> a
sa b1 ~> b
bt :.: g b b
g) = forall {j} {k} (w :: FLAVOR j k) (p :: k +-> k) (q :: j +-> j) r.
Sub w p q =>
(w p q => r) -> r
forall (w :: FLAVOR k k) (p :: k +-> k) (q :: k +-> k) r.
Sub w p q =>
(w p q => r) -> r
sub @IsoFl @f @g ((a ~> a) -> (b ~> b) -> Yo a (OP b) a b
forall {k} {j} (c :: k) (a :: k) (b1 :: j) (d :: j).
(c ~> a) -> (b1 ~> d) -> Yo a (OP b1) c d
Yo (b ~> a
sa (b ~> a) -> (a ~> b) -> a ~> a
forall (b :: k) (c :: k) (a :: k). (b ~> c) -> (a ~> b) -> a ~> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @f @g f a b
f) (forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @g @f g b b
g (b ~> b) -> (b ~> b) -> b ~> b
forall (b :: k) (c :: k) (a :: k). (b ~> c) -> (a ~> b) -> a ~> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. b ~> b
b1 ~> b
bt))

-- | 'Proarrow.Optic.re'-versed isos are still isos: the same carrier eliminates them by reading
-- the witness pair backwards. This is a conversion the subtyping lattice cannot express (the
-- entailment @IsoFl q p => IsoFl p q@ doesn't hold), but the carrier can compute it.
instance {-# OVERLAPPING #-} (CategoryOf k) => Prostrong (Flip IsoFl) (Yo (a :: k) (OP b) :: k +-> k) where
  proact :: forall (f :: k +-> k) (g :: k +-> k).
(Flip IsoFl f g, Profunctor f, Profunctor g) =>
((f :.: Yo a (OP b)) :.: g) :~> Yo a (OP b)
proact @f @g (f a b
f :.: Yo b ~> a
sa b1 ~> b
bt :.: g b b
g) = (a ~> a) -> (b ~> b) -> Yo a (OP b) a b
forall {k} {j} (c :: k) (a :: k) (b1 :: j) (d :: j).
(c ~> a) -> (b1 ~> d) -> Yo a (OP b1) c d
Yo (b ~> a
sa (b ~> a) -> (a ~> b) -> a ~> a
forall (b :: k) (c :: k) (a :: k). (b ~> c) -> (a ~> b) -> a ~> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @f @g f a b
f) (forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @g @f g b b
g (b ~> b) -> (b ~> b) -> b ~> b
forall (b :: k) (c :: k) (a :: k). (b ~> c) -> (a ~> b) -> a ~> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. b ~> b
b1 ~> b
bt)

-- | Eliminate any iso-flavored optic to its two legs, in either encoding, including the
-- profunctor-class-flavored 'Proarrow.Optic.PIso' and reversed ('Proarrow.Optic.re') isos.
withIso
  :: forall {k} c (s :: k) (t :: k) a b r
   . (CategoryOf k, (Ob a, Ob b) => c (Yo a (OP b)))
  => Optic c s t a b -> ((s ~> a) -> (b ~> t) -> r) -> r
withIso :: forall {k} (c :: (k -> k -> Type) -> Constraint) (s :: k) (t :: k)
       (a :: k) (b :: k) r.
(CategoryOf k, (Ob a, Ob b) => c (Yo a (OP b))) =>
Optic c s t a b -> ((s ~> a) -> (b ~> t) -> r) -> r
withIso (Optic forall (p :: k +-> k). (c p, Profunctor p) => p a b -> p s t
l) (s ~> a) -> (b ~> t) -> r
k = case forall (p :: k +-> k). (c p, Profunctor p) => p a b -> p s t
l @(Yo a (OP b)) ((a ~> a) -> (b ~> b) -> Yo a (OP b) a b
forall {k} {j} (c :: k) (a :: k) (b1 :: j) (d :: j).
(c ~> a) -> (b1 ~> d) -> Yo a (OP b1) c d
Yo a ~> a
a ~> a
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id b ~> b
b ~> b
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id) of Yo s ~> a
sa b1 ~> t
bt -> (s ~> a) -> (b ~> t) -> r
k s ~> a
s ~> a
sa b ~> t
b1 ~> t
bt

-- | Categories where it can be decided whether two objects are isomorphic, with an isomorphism
-- when they are. The iso is an optic of any flavor @c@, so it can be used as a
-- 'Proarrow.Optic.PIso', an 'Iso' or anything those convert to, and taken apart with 'withIso'.
type DecidableIso :: Kind -> Constraint
class (CategoryOf k) => DecidableIso k where
  isoOf :: forall (c :: (k +-> k) -> Constraint) (a :: k) (b :: k). (Ob a, Ob b) => Maybe (Optic c a a b b)

-- | The identity iso when two objects are equal.
isoFromEquality :: forall {k} c (a :: k) b. (CategoryOf k, Ob a) => Maybe (a :~: b) -> Maybe (Optic c a a b b)
isoFromEquality :: forall {k} (c :: (k +-> k) -> Constraint) (a :: k) (b :: k).
(CategoryOf k, Ob a) =>
Maybe (a :~: b) -> Maybe (Optic c a a b b)
isoFromEquality = ((a :~: b) -> Optic c a a b b)
-> Maybe (a :~: b) -> Maybe (Optic c a a b b)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a :~: b
Refl -> (a ~> b) -> (b ~> a) -> Optic c a a b b
forall {j} {k} (c :: (j +-> k) -> Constraint) (s :: k) (t :: j)
       (a :: k) (b :: j).
(CategoryOf j, CategoryOf k) =>
(s ~> a) -> (b ~> t) -> Optic c s t a b
iso a ~> a
a ~> b
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id a ~> a
b ~> a
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id)

-- | The two iso encodings are equivalent: this direction instantiates the
-- profunctor-class-flavored iso at the free 'IsoFl'-strong profunctor @ExOptic 'IsoFl' a b@,
-- which needs nothing beyond its 'Proarrow.Core.Profunctor' instance.
fromPIso :: forall {k} (s :: k) (t :: k) a b. (CategoryOf k) => PIso s t a b -> Iso s t a b
fromPIso :: forall {k} (s :: k) (t :: k) (a :: k) (b :: k).
CategoryOf k =>
PIso s t a b -> Iso s t a b
fromPIso = Optic Profunctor s t a b -> Optic (Prostrong IsoFl) s t a b
forall {j} {k} (c :: (k -> j -> Type) -> Constraint)
       (w :: FLAVOR j k) (s :: k) (t :: j) (a :: k) (b :: j).
(CategoryOf j, CategoryOf k, Flavor w,
 (Ob a, Ob b) => c (ExOptic w a b)) =>
Optic c s t a b -> Optic (Prostrong w) s t a b
convert

-- | The other direction of the equivalence, by eliminating to legs and rebuilding.
toPIso :: forall {k} (s :: k) (t :: k) a b. (CategoryOf k) => Iso s t a b -> PIso s t a b
toPIso :: forall {k} (s :: k) (t :: k) (a :: k) (b :: k).
CategoryOf k =>
Iso s t a b -> PIso s t a b
toPIso Iso s t a b
o = Iso s t a b
-> ((s ~> a) -> (b ~> t) -> PIso s t a b) -> PIso s t a b
forall {k} (c :: (k -> k -> Type) -> Constraint) (s :: k) (t :: k)
       (a :: k) (b :: k) r.
(CategoryOf k, (Ob a, Ob b) => c (Yo a (OP b))) =>
Optic c s t a b -> ((s ~> a) -> (b ~> t) -> r) -> r
withIso Iso s t a b
o (s ~> a) -> (b ~> t) -> PIso s t a b
forall {j} {k} (c :: (j +-> k) -> Constraint) (s :: k) (t :: j)
       (a :: k) (b :: j).
(CategoryOf j, CategoryOf k) =>
(s ~> a) -> (b ~> t) -> Optic c s t a b
iso

instance (Monoidal k, Profunctor p, Ob u, Ob v) => Prostrong IsoFl (TensorRight (p :: k +-> k) u v) where
  proact :: forall (f :: k +-> k) (g :: k +-> k).
(IsoFl f g, Profunctor f, Profunctor g) =>
((f :.: TensorRight p u v) :.: g) :~> TensorRight p u v
proact ((f :: f a b
f@f a b
Objs :: f s a) :.: TensorRight p (b ** u) (b ** v)
p :.: (g :: g b b
g@g b b
Objs :: g b t)) =
    p (a ** u) (b ** v) -> TensorRight p u v a b
forall {k} (p :: k +-> k) (u :: k) (v :: k) (a :: k) (b :: k).
(Ob a, Ob b) =>
p (a ** u) (b ** v) -> TensorRight p u v a b
TensorRight (((a ** u) ~> (b ** u))
-> ((b ** v) ~> (b ** v))
-> p (b ** u) (b ** v)
-> p (a ** u) (b ** v)
forall (c :: k) (a :: k) (b :: k) (d :: k).
(c ~> a) -> (b ~> d) -> p a b -> p c d
forall {j} {k} (p :: j +-> k) (c :: k) (a :: k) (b :: j) (d :: j).
Profunctor p =>
(c ~> a) -> (b ~> d) -> p a b -> p c d
dimap (forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @f @g f a b
f (a ~> b) -> (u ~> u) -> (a ** u) ~> (b ** u)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
** forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @u) (forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @g @f g b b
g (b ~> b) -> (v ~> v) -> (b ** v) ~> (b ** v)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
** forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @v) p (b ** u) (b ** v)
p)

instance (Monoidal k, Profunctor p, Ob m) => Prostrong IsoFl (Backward (p :: k +-> k) m) where
  proact :: forall (f :: k +-> k) (g :: k +-> k).
(IsoFl f g, Profunctor f, Profunctor g) =>
((f :.: Backward p m) :.: g) :~> Backward p m
proact ((f :: f a b
f@f a b
Objs :: f s a) :.: Backward p b (m ** b)
p :.: (g :: g b b
g@g b b
Objs :: g b t)) =
    p a (m ** b) -> Backward p m a b
forall {k} (p :: k +-> k) (m :: k) (a :: k) (b :: k).
(Ob a, Ob b) =>
p a (m ** b) -> Backward p m a b
Backward ((a ~> b) -> ((m ** b) ~> (m ** b)) -> p b (m ** b) -> p a (m ** b)
forall (c :: k) (a :: k) (b :: k) (d :: k).
(c ~> a) -> (b ~> d) -> p a b -> p c d
forall {j} {k} (p :: j +-> k) (c :: k) (a :: k) (b :: j) (d :: j).
Profunctor p =>
(c ~> a) -> (b ~> d) -> p a b -> p c d
dimap (forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @f @g f a b
f) (forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m Obj m -> (b ~> b) -> (m ** b) ~> (m ** b)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
** forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @g @f g b b
g) p b (m ** b)
p)

instance (CategoryOf k, Ob s, Ob x) => Prostrong IsoFl (Continuation output (s :: k) x) where
  proact :: forall (f :: k +-> k) (g :: k +-> k).
(IsoFl f g, Profunctor f, Profunctor g) =>
((f :.: Continuation output s x) :.: g) :~> Continuation output s x
proact ((f :: f a b
f@f a b
Objs :: f y z) :.: Continuation Optic output s b x b
context :.: (g :: g b b
g@g b b
Objs :: g t r)) =
    Optic output s b x a -> Continuation output s x a b
forall {k} (a :: k) (b :: k) (output :: (k +-> k) -> Constraint)
       (s :: k) (x :: k).
(Ob a, Ob b) =>
Optic output s b x a -> Continuation output s x a b
Continuation ((s ~> s) -> (b ~> b) -> Optic output s b s b
forall {j} {k} (c :: (j +-> k) -> Constraint) (s :: k) (t :: j)
       (a :: k) (b :: j).
(CategoryOf j, CategoryOf k) =>
(s ~> a) -> (b ~> t) -> Optic c s t a b
iso s ~> s
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id (forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @g @f g b b
g) Optic output s b s b
-> Optic_ (OPT x a) (OPT s b) -> Optic output s b x a
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
forall (b :: OPTIC k k output) (c :: OPTIC k k output)
       (a :: OPTIC k k output).
Optic_ b c -> Optic_ a b -> Optic_ a c
. Optic output s b x b
context Optic output s b x b
-> Optic_ (OPT x a) (OPT x b) -> Optic_ (OPT x a) (OPT s b)
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
forall (b :: OPTIC k k output) (c :: OPTIC k k output)
       (a :: OPTIC k k output).
Optic_ b c -> Optic_ a b -> Optic_ a c
. (x ~> x) -> (a ~> b) -> Optic_ (OPT x a) (OPT x b)
forall {j} {k} (c :: (j +-> k) -> Constraint) (s :: k) (t :: j)
       (a :: k) (b :: j).
(CategoryOf j, CategoryOf k) =>
(s ~> a) -> (b ~> t) -> Optic c s t a b
iso x ~> x
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id (forall {j} {k} (p :: k +-> k) (q :: j +-> j) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
forall (p :: k +-> k) (q :: k +-> k) (s :: k) (a :: k).
GetterFl p q =>
p s a -> s ~> a
getP @f @g f a b
f))