11{-# LANGUAGE AllowAmbiguousTypes #-}
22{-# LANGUAGE IncoherentInstances #-}
3- {-# OPTIONS_GHC -Wno-orphans #-}
3+ {-# OPTIONS_GHC -Wno-orphans -fprint-potential-instances #-}
44
55module Proarrow.Category.Monoidal.Optic where
66
77import Data.Bifunctor (bimap )
88import Data.Kind (Type )
9- import Prelude (Either , Maybe (.. ), Monad (.. ), Traversable , const , either , fmap , fst , snd , uncurry , ($) , type (~ ))
9+ import Prelude (Either ( .. ) , Maybe (.. ), Monad (.. ), Traversable , const , either , fmap , uncurry , ($) , type (~ ))
1010
1111import Proarrow.Category.Instance.Kleisli (KLEISLI (.. ), Kleisli (.. ))
1212import Proarrow.Category.Instance.Nat ((!) )
13- import Proarrow.Category.Instance.Product ((:**:) (.. ))
1413import Proarrow.Category.Instance.Sub (SUBCAT (.. ), Sub (.. ))
1514import Proarrow.Category.Monoidal (MonoidalProfunctor (.. ), SymMonoidal , swap )
16- import Proarrow.Category.Monoidal.Action (MonoidalAction (.. ), Strong (.. ), composeActs , decomposeActs )
15+ import Proarrow.Category.Monoidal.Action (MonoidalAction (.. ), Strong (.. ), composeActs , decomposeActs , actHom )
1716import Proarrow.Category.Opposite (OPPOSITE (.. ))
1817import Proarrow.Core (CAT , CategoryOf (.. ), Kind , Profunctor (.. ), Promonad (.. ), dimapDefault , obj , type (+-> ))
18+ import Proarrow.Core qualified as Core
1919import Proarrow.Functor (Prelude (.. ))
2020import Proarrow.Object (src , tgt )
2121import Proarrow.Object.BinaryCoproduct (COPROD (.. ), Coprod (.. ))
22- import Proarrow.Object.BinaryProduct ()
22+ import Proarrow.Object.BinaryProduct (Cartesian , HasBinaryProducts (.. ))
23+ import Proarrow.Profunctor.Identity (Id (.. ))
2324import Proarrow.Profunctor.Star (Star , pattern Star )
24- import Proarrow.Profunctor.Identity (Id (.. ))
2525
2626type Optic :: Kind -> c -> d -> c -> d -> Type
2727data Optic m a b s t where
@@ -42,13 +42,10 @@ instance (CategoryOf c, CategoryOf d) => Profunctor (Optic m a b :: c +-> d) whe
4242instance (IsOptic m c d ) => Strong m (Optic m a b :: c +-> d ) where
4343 act :: forall (a1 :: m ) (b1 :: m ) (s :: d ) (t :: c ). a1 ~> b1 -> Optic m a b s t -> Optic m a b (Act a1 s ) (Act b1 t )
4444 act w (Optic @ x @ x' f w' g) =
45- Optic (composeActs @ a1 @ x @ a (src w `act ` src f) f) (w `par` w') (decomposeActs @ b1 @ x' @ b g (tgt w `act ` tgt g))
45+ Optic (composeActs @ a1 @ x @ a (src w `actHom ` src f) f) (w `par` w') (decomposeActs @ b1 @ x' @ b g (tgt w `actHom ` tgt g))
4646 \\ w
4747 \\ w'
4848
49- parallel :: Optic m a b s t -> Optic m' c d u v -> Optic (m , m' ) '(a , c ) '(b , d ) '(s , u ) '(t , v )
50- parallel (Optic f w g) (Optic h w' i) = Optic (f :**: h) (w :**: w') (g :**: i)
51-
5249type data OPTIC m (c :: Kind ) (d :: Kind ) = OPT c d
5350type family OptL (p :: OPTIC w c d ) where
5451 OptL (OPT c d ) = c
@@ -64,30 +61,36 @@ instance (IsOptic m c d) => Profunctor (OpticCat :: CAT (OPTIC m c d)) where
6461instance (IsOptic m c d ) => Promonad (OpticCat :: CAT (OPTIC m c d )) where
6562 id = OpticCat (prof2ex id )
6663 OpticCat l@ Optic {} . OpticCat r@ Optic {} = OpticCat $ prof2ex (ex2prof l . ex2prof r)
64+
6765-- | The category of optics.
6866instance (IsOptic m c d ) => CategoryOf (OPTIC m c d ) where
6967 type (~> ) = OpticCat
7068 type Ob a = (a ~ OPT (OptL a ) (OptR a ), Ob (OptL a ), Ob (OptR a ))
7169
72- type MixedOptic m a b s t = forall p . (Strong m p ) = > p a b -> p s t
70+ type MixedOptic m a b s t = Core. Optic (Strong m ) s t a b
71+
72+ toIso :: MixedOptic () a b s t -> Core. Iso s t a b
73+ toIso l p = l p
7374
7475ex2prof :: forall m a b s t . Optic m a b s t -> MixedOptic m a b s t
7576ex2prof (Optic l w r) p = dimap l r (act w p)
7677
7778prof2ex
78- :: forall {c } {d } m a b s t
79+ :: forall {c } {d } m ( a :: c ) ( b :: d ) ( s :: c ) ( t :: d )
7980 . (MonoidalAction m c , MonoidalAction m d , Ob a , Ob b )
80- => MixedOptic m ( a :: c ) ( b :: d ) ( s :: c ) ( t :: d )
81+ => MixedOptic m a b s t
8182 -> Optic m a b s t
8283prof2ex p2p = p2p (Optic (unitorInv @ m ) par0 (unitor @ m ))
8384
84- type Lens s t a b = MixedOptic Type a b s t
85- mkLens :: (s -> a ) -> (s -> b -> t ) -> Lens s t a b
86- mkLens sa sbt = ex2prof (Optic (\ s -> (s, sa s)) (src sa) (uncurry sbt))
85+ type Lens (s :: k ) t a b = MixedOptic k a b s t
86+ mkLens
87+ :: (Cartesian k , Act s a ~ (s && a ), Act s b ~ (s && b ), Ob (b :: k ))
88+ => (s ~> a ) -> ((s && b ) ~> t ) -> Lens s t a b
89+ mkLens sa sbt = ex2prof (Optic (id &&& sa) (src sa) sbt) \\ sa
8790
88- type Prism s t a b = MixedOptic (COPROD Type ) ( COPR a ) ( COPR b ) ( COPR s ) ( COPR t )
91+ type Prism ( s :: k ) t a b = MixedOptic (COPROD k ) a b s t
8992mkPrism :: (s -> Either t a ) -> (b -> t ) -> Prism s t a b
90- mkPrism sat bt = ex2prof @ (COPROD Type ) (Optic ( Coprod ( Id sat)) id (Coprod ( Id ( either id bt)) ))
93+ mkPrism sta bt = ex2prof @ (COPROD Type ) (Optic sta id (either id bt))
9194
9295type Traversal s t a b = MixedOptic (SUBCAT Traversable ) a b s t
9396traversing :: (Traversable f ) => Traversal (f a ) (f b ) a b
@@ -113,10 +116,10 @@ infixl 8 ^.
113116(^.) :: s -> (Viewing a b a b -> Viewing a b s t ) -> a
114117(^.) s l = unView (l $ Viewing id ) s
115118
116- data Previewing a (b :: COPROD Type ) s (t :: COPROD Type ) where
117- Previewing :: {unPreview :: s -> Maybe a } -> Previewing ( COPR a ) ( COPR b ) ( COPR s ) ( COPR t )
119+ data Previewing a (b :: Type ) s (t :: Type ) where
120+ Previewing :: {unPreview :: s -> Maybe a } -> Previewing a b s t
118121instance Profunctor (Previewing a b ) where
119- dimap ( Coprod ( Id l)) Coprod {} (Previewing f) = Previewing (f . l)
122+ dimap l _ (Previewing f) = Previewing (f . l)
120123 r \\ Previewing f = r \\ f
121124instance Strong (COPROD Type ) (Previewing a b ) where
122125 act _ (Previewing f) = Previewing (either (const Nothing ) f)
@@ -125,7 +128,7 @@ instance Strong Type (Previewing a b) where
125128
126129infixl 8 ?.
127130(?.)
128- :: s -> (Previewing ( COPR a ) ( COPR b ) ( COPR a ) ( COPR b ) -> Previewing ( COPR a ) ( COPR b ) ( COPR s ) ( COPR t ) ) -> Maybe a
131+ :: s -> (Previewing a b a b -> Previewing a b s t ) -> Maybe a
129132(?.) s l = unPreview (l $ Previewing Just ) s
130133
131134type KlCat m = KLEISLI (Star (Prelude m ))
@@ -193,9 +196,10 @@ instance (IsChart m c d) => Promonad (ChartCat :: CAT (CHART m c d)) where
193196 id = ChartCat (prof2ex id )
194197 ChartCat (Optic @ x @ x' @ _ @ t ll lw lr) . ChartCat (Optic @ y @ y' @ a rl rw rr) =
195198 ChartCat $
196- Optic (composeActs @ x @ y @ a ll rl) (lw `par` rw) (decomposeActs @ y' @ x' @ t lr rr . (swap @ _ @ x' @ y' `act ` obj @ t ))
199+ Optic (composeActs @ x @ y @ a ll rl) (lw `par` rw) (decomposeActs @ y' @ x' @ t lr rr . (swap @ _ @ x' @ y' `actHom ` obj @ t ))
197200 \\ lw
198201 \\ rw
202+
199203-- | The category of charts.
200204instance (IsChart m c d ) => CategoryOf (CHART m c d ) where
201205 type (~> ) = ChartCat
0 commit comments