{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE Trustworthy #-}
#if !MIN_VERSION_base(4, 9, 0)
{-# LANGUAGE DataKinds #-}
#endif
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
module Lens.Micro.Mtl.Internal
(
Zoomed,
Zoom(..),
Magnified,
Magnify(..),
Focusing(..),
FocusingWith(..),
FocusingPlus(..),
FocusingOn(..),
FocusingMay(..),
FocusingErr(..),
Effect(..),
EffectRWS(..),
May(..),
Err(..),
)
where
import Control.Applicative
#if MIN_VERSION_mtl(2, 3, 0)
import Control.Monad (liftM, liftM2)
#else
#endif
import Control.Monad.Reader as Reader
import Control.Monad.State as State
import Control.Monad.Trans.State.Lazy as Lazy
import Control.Monad.Trans.State.Strict as Strict
import Control.Monad.Trans.Writer.Lazy as Lazy
import Control.Monad.Trans.Writer.Strict as Strict
import Control.Monad.Trans.RWS.Lazy as Lazy
import Control.Monad.Trans.RWS.Strict as Strict
#if !MIN_VERSION_transformers(0, 6, 0)
import Control.Monad.Trans.Error
import Control.Monad.Trans.List
#endif
import Control.Monad.Trans.Except
import Control.Monad.Trans.Identity
import Control.Monad.Trans.Maybe
import Lens.Micro
import Lens.Micro.Internal
#if __GLASGOW_HASKELL__ < 710
import Data.Monoid
#endif
#if MIN_VERSION_base(4,9,0)
import Data.Kind (Type)
#endif
#if MIN_VERSION_base(4,9,0)
type family Zoomed (m :: Type -> Type) :: Type -> Type -> Type
#else
type family Zoomed (m :: * -> *) :: * -> * -> *
#endif
type instance Zoomed (Strict.StateT s z) = Focusing z
type instance Zoomed (Lazy.StateT s z) = Focusing z
type instance Zoomed (ReaderT e m) = Zoomed m
type instance Zoomed (IdentityT m) = Zoomed m
type instance Zoomed (Strict.RWST r w s z) = FocusingWith w z
type instance Zoomed (Lazy.RWST r w s z) = FocusingWith w z
type instance Zoomed (Strict.WriterT w m) = FocusingPlus w (Zoomed m)
type instance Zoomed (Lazy.WriterT w m) = FocusingPlus w (Zoomed m)
#if !MIN_VERSION_transformers(0, 6, 0)
type instance Zoomed (ListT m) = FocusingOn [] (Zoomed m)
type instance Zoomed (ErrorT e m) = FocusingErr e (Zoomed m)
#endif
type instance Zoomed (MaybeT m) = FocusingMay (Zoomed m)
type instance Zoomed (ExceptT e m) = FocusingErr e (Zoomed m)
newtype Focusing m s a = Focusing { forall (m :: * -> *) s a. Focusing m s a -> m (s, a)
unfocusing :: m (s, a) }
instance Monad m => Functor (Focusing m s) where
fmap :: forall a b. (a -> b) -> Focusing m s a -> Focusing m s b
fmap a -> b
f (Focusing m (s, a)
m) = forall (m :: * -> *) s a. m (s, a) -> Focusing m s a
Focusing forall a b. (a -> b) -> a -> b
$ do
(s
s, a
a) <- m (s, a)
m
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, a -> b
f a
a)
{-# INLINE fmap #-}
instance (Monad m, Monoid s) => Applicative (Focusing m s) where
pure :: forall a. a -> Focusing m s a
pure a
a = forall (m :: * -> *) s a. m (s, a) -> Focusing m s a
Focusing (forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Monoid a => a
mempty, a
a))
{-# INLINE pure #-}
Focusing m (s, a -> b)
mf <*> :: forall a b.
Focusing m s (a -> b) -> Focusing m s a -> Focusing m s b
<*> Focusing m (s, a)
ma = forall (m :: * -> *) s a. m (s, a) -> Focusing m s a
Focusing forall a b. (a -> b) -> a -> b
$ do
(s
s, a -> b
f) <- m (s, a -> b)
mf
(s
s', a
a) <- m (s, a)
ma
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Monoid a => a -> a -> a
mappend s
s s
s', a -> b
f a
a)
{-# INLINE (<*>) #-}
newtype FocusingWith w m s a = FocusingWith { forall w (m :: * -> *) s a. FocusingWith w m s a -> m (s, a, w)
unfocusingWith :: m (s, a, w) }
instance Monad m => Functor (FocusingWith w m s) where
fmap :: forall a b.
(a -> b) -> FocusingWith w m s a -> FocusingWith w m s b
fmap a -> b
f (FocusingWith m (s, a, w)
m) = forall w (m :: * -> *) s a. m (s, a, w) -> FocusingWith w m s a
FocusingWith forall a b. (a -> b) -> a -> b
$ do
(s
s, a
a, w
w) <- m (s, a, w)
m
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, a -> b
f a
a, w
w)
{-# INLINE fmap #-}
instance (Monad m, Monoid s, Monoid w) => Applicative (FocusingWith w m s) where
pure :: forall a. a -> FocusingWith w m s a
pure a
a = forall w (m :: * -> *) s a. m (s, a, w) -> FocusingWith w m s a
FocusingWith (forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Monoid a => a
mempty, a
a, forall a. Monoid a => a
mempty))
{-# INLINE pure #-}
FocusingWith m (s, a -> b, w)
mf <*> :: forall a b.
FocusingWith w m s (a -> b)
-> FocusingWith w m s a -> FocusingWith w m s b
<*> FocusingWith m (s, a, w)
ma = forall w (m :: * -> *) s a. m (s, a, w) -> FocusingWith w m s a
FocusingWith forall a b. (a -> b) -> a -> b
$ do
(s
s, a -> b
f, w
w) <- m (s, a -> b, w)
mf
(s
s', a
a, w
w') <- m (s, a, w)
ma
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Monoid a => a -> a -> a
mappend s
s s
s', a -> b
f a
a, forall a. Monoid a => a -> a -> a
mappend w
w w
w')
{-# INLINE (<*>) #-}
newtype FocusingPlus w k s a = FocusingPlus { forall w (k :: * -> * -> *) s a. FocusingPlus w k s a -> k (s, w) a
unfocusingPlus :: k (s, w) a }
instance Functor (k (s, w)) => Functor (FocusingPlus w k s) where
fmap :: forall a b.
(a -> b) -> FocusingPlus w k s a -> FocusingPlus w k s b
fmap a -> b
f (FocusingPlus k (s, w) a
as) = forall w (k :: * -> * -> *) s a. k (s, w) a -> FocusingPlus w k s a
FocusingPlus (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f k (s, w) a
as)
{-# INLINE fmap #-}
instance Applicative (k (s, w)) => Applicative (FocusingPlus w k s) where
pure :: forall a. a -> FocusingPlus w k s a
pure = forall w (k :: * -> * -> *) s a. k (s, w) a -> FocusingPlus w k s a
FocusingPlus forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE pure #-}
FocusingPlus k (s, w) (a -> b)
kf <*> :: forall a b.
FocusingPlus w k s (a -> b)
-> FocusingPlus w k s a -> FocusingPlus w k s b
<*> FocusingPlus k (s, w) a
ka = forall w (k :: * -> * -> *) s a. k (s, w) a -> FocusingPlus w k s a
FocusingPlus (k (s, w) (a -> b)
kf forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> k (s, w) a
ka)
{-# INLINE (<*>) #-}
newtype FocusingOn f k s a = FocusingOn { forall (f :: * -> *) (k :: * -> * -> *) s a.
FocusingOn f k s a -> k (f s) a
unfocusingOn :: k (f s) a }
instance Functor (k (f s)) => Functor (FocusingOn f k s) where
fmap :: forall a b. (a -> b) -> FocusingOn f k s a -> FocusingOn f k s b
fmap a -> b
f (FocusingOn k (f s) a
as) = forall (f :: * -> *) (k :: * -> * -> *) s a.
k (f s) a -> FocusingOn f k s a
FocusingOn (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f k (f s) a
as)
{-# INLINE fmap #-}
instance Applicative (k (f s)) => Applicative (FocusingOn f k s) where
pure :: forall a. a -> FocusingOn f k s a
pure = forall (f :: * -> *) (k :: * -> * -> *) s a.
k (f s) a -> FocusingOn f k s a
FocusingOn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE pure #-}
FocusingOn k (f s) (a -> b)
kf <*> :: forall a b.
FocusingOn f k s (a -> b)
-> FocusingOn f k s a -> FocusingOn f k s b
<*> FocusingOn k (f s) a
ka = forall (f :: * -> *) (k :: * -> * -> *) s a.
k (f s) a -> FocusingOn f k s a
FocusingOn (k (f s) (a -> b)
kf forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> k (f s) a
ka)
{-# INLINE (<*>) #-}
newtype May a = May { forall a. May a -> Maybe a
getMay :: Maybe a }
instance Monoid a => Monoid (May a) where
mempty :: May a
mempty = forall a. Maybe a -> May a
May (forall a. a -> Maybe a
Just forall a. Monoid a => a
mempty)
{-# INLINE mempty #-}
#if !MIN_VERSION_base(4,11,0)
May Nothing `mappend` _ = May Nothing
_ `mappend` May Nothing = May Nothing
May (Just a) `mappend` May (Just b) = May (Just (mappend a b))
{-# INLINE mappend #-}
#else
instance Semigroup a => Semigroup (May a) where
May Maybe a
Nothing <> :: May a -> May a -> May a
<> May a
_ = forall a. Maybe a -> May a
May forall a. Maybe a
Nothing
May a
_ <> May Maybe a
Nothing = forall a. Maybe a -> May a
May forall a. Maybe a
Nothing
May (Just a
a) <> May (Just a
b) = forall a. Maybe a -> May a
May (forall a. a -> Maybe a
Just (a
a forall a. Semigroup a => a -> a -> a
<> a
b))
{-# INLINE (<>) #-}
#endif
newtype FocusingMay k s a = FocusingMay { forall (k :: * -> * -> *) s a. FocusingMay k s a -> k (May s) a
unfocusingMay :: k (May s) a }
instance Functor (k (May s)) => Functor (FocusingMay k s) where
fmap :: forall a b. (a -> b) -> FocusingMay k s a -> FocusingMay k s b
fmap a -> b
f (FocusingMay k (May s) a
as) = forall (k :: * -> * -> *) s a. k (May s) a -> FocusingMay k s a
FocusingMay (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f k (May s) a
as)
{-# INLINE fmap #-}
instance Applicative (k (May s)) => Applicative (FocusingMay k s) where
pure :: forall a. a -> FocusingMay k s a
pure = forall (k :: * -> * -> *) s a. k (May s) a -> FocusingMay k s a
FocusingMay forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE pure #-}
FocusingMay k (May s) (a -> b)
kf <*> :: forall a b.
FocusingMay k s (a -> b) -> FocusingMay k s a -> FocusingMay k s b
<*> FocusingMay k (May s) a
ka = forall (k :: * -> * -> *) s a. k (May s) a -> FocusingMay k s a
FocusingMay (k (May s) (a -> b)
kf forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> k (May s) a
ka)
{-# INLINE (<*>) #-}
newtype Err e a = Err { forall e a. Err e a -> Either e a
getErr :: Either e a }
instance Monoid a => Monoid (Err e a) where
mempty :: Err e a
mempty = forall e a. Either e a -> Err e a
Err (forall a b. b -> Either a b
Right forall a. Monoid a => a
mempty)
{-# INLINE mempty #-}
#if !MIN_VERSION_base(4,11,0)
Err (Left e) `mappend` _ = Err (Left e)
_ `mappend` Err (Left e) = Err (Left e)
Err (Right a) `mappend` Err (Right b) = Err (Right (mappend a b))
{-# INLINE mappend #-}
#else
instance Semigroup a => Semigroup (Err e a) where
Err (Left e
e) <> :: Err e a -> Err e a -> Err e a
<> Err e a
_ = forall e a. Either e a -> Err e a
Err (forall a b. a -> Either a b
Left e
e)
Err e a
_ <> Err (Left e
e) = forall e a. Either e a -> Err e a
Err (forall a b. a -> Either a b
Left e
e)
Err (Right a
a) <> Err (Right a
b) = forall e a. Either e a -> Err e a
Err (forall a b. b -> Either a b
Right (a
a forall a. Semigroup a => a -> a -> a
<> a
b))
{-# INLINE (<>) #-}
#endif
newtype FocusingErr e k s a = FocusingErr { forall e (k :: * -> * -> *) s a.
FocusingErr e k s a -> k (Err e s) a
unfocusingErr :: k (Err e s) a }
instance Functor (k (Err e s)) => Functor (FocusingErr e k s) where
fmap :: forall a b. (a -> b) -> FocusingErr e k s a -> FocusingErr e k s b
fmap a -> b
f (FocusingErr k (Err e s) a
as) = forall e (k :: * -> * -> *) s a.
k (Err e s) a -> FocusingErr e k s a
FocusingErr (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f k (Err e s) a
as)
{-# INLINE fmap #-}
instance Applicative (k (Err e s)) => Applicative (FocusingErr e k s) where
pure :: forall a. a -> FocusingErr e k s a
pure = forall e (k :: * -> * -> *) s a.
k (Err e s) a -> FocusingErr e k s a
FocusingErr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE pure #-}
FocusingErr k (Err e s) (a -> b)
kf <*> :: forall a b.
FocusingErr e k s (a -> b)
-> FocusingErr e k s a -> FocusingErr e k s b
<*> FocusingErr k (Err e s) a
ka = forall e (k :: * -> * -> *) s a.
k (Err e s) a -> FocusingErr e k s a
FocusingErr (k (Err e s) (a -> b)
kf forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> k (Err e s) a
ka)
{-# INLINE (<*>) #-}
infixr 2 `zoom`
class (MonadState s m, MonadState t n) => Zoom m n s t | m -> s, n -> t, m t -> n, n s -> m where
zoom :: LensLike' (Zoomed m c) t s -> m c -> n c
instance Monad z => Zoom (Strict.StateT s z) (Strict.StateT t z) s t where
zoom :: forall c.
LensLike' (Zoomed (StateT s z) c) t s
-> StateT s z c -> StateT t z c
zoom LensLike' (Zoomed (StateT s z) c) t s
l (Strict.StateT s -> z (c, s)
m) = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Strict.StateT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) s a. Focusing m s a -> m (s, a)
unfocusing forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Zoomed (StateT s z) c) t s
l (forall (m :: * -> *) s a. m (s, a) -> Focusing m s a
Focusing forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. s -> z (c, s)
m)
{-# INLINE zoom #-}
instance Monad z => Zoom (Lazy.StateT s z) (Lazy.StateT t z) s t where
zoom :: forall c.
LensLike' (Zoomed (StateT s z) c) t s
-> StateT s z c -> StateT t z c
zoom LensLike' (Zoomed (StateT s z) c) t s
l (Lazy.StateT s -> z (c, s)
m) = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Lazy.StateT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) s a. Focusing m s a -> m (s, a)
unfocusing forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Zoomed (StateT s z) c) t s
l (forall (m :: * -> *) s a. m (s, a) -> Focusing m s a
Focusing forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. s -> z (c, s)
m)
{-# INLINE zoom #-}
instance Zoom m n s t => Zoom (ReaderT e m) (ReaderT e n) s t where
zoom :: forall c.
LensLike' (Zoomed (ReaderT e m) c) t s
-> ReaderT e m c -> ReaderT e n c
zoom LensLike' (Zoomed (ReaderT e m) c) t s
l (ReaderT e -> m c
m) = forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT (forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike' (Zoomed (ReaderT e m) c) t s
l forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> m c
m)
{-# INLINE zoom #-}
instance Zoom m n s t => Zoom (IdentityT m) (IdentityT n) s t where
zoom :: forall c.
LensLike' (Zoomed (IdentityT m) c) t s
-> IdentityT m c -> IdentityT n c
zoom LensLike' (Zoomed (IdentityT m) c) t s
l (IdentityT m c
m) = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike' (Zoomed (IdentityT m) c) t s
l m c
m)
{-# INLINE zoom #-}
instance (Monoid w, Monad z) => Zoom (Strict.RWST r w s z) (Strict.RWST r w t z) s t where
zoom :: forall c.
LensLike' (Zoomed (RWST r w s z) c) t s
-> RWST r w s z c -> RWST r w t z c
zoom LensLike' (Zoomed (RWST r w s z) c) t s
l (Strict.RWST r -> s -> z (c, s, w)
m) = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Strict.RWST forall a b. (a -> b) -> a -> b
$ \r
r -> forall w (m :: * -> *) s a. FocusingWith w m s a -> m (s, a, w)
unfocusingWith forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Zoomed (RWST r w s z) c) t s
l (forall w (m :: * -> *) s a. m (s, a, w) -> FocusingWith w m s a
FocusingWith forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. r -> s -> z (c, s, w)
m r
r)
{-# INLINE zoom #-}
instance (Monoid w, Monad z) => Zoom (Lazy.RWST r w s z) (Lazy.RWST r w t z) s t where
zoom :: forall c.
LensLike' (Zoomed (RWST r w s z) c) t s
-> RWST r w s z c -> RWST r w t z c
zoom LensLike' (Zoomed (RWST r w s z) c) t s
l (Lazy.RWST r -> s -> z (c, s, w)
m) = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Lazy.RWST forall a b. (a -> b) -> a -> b
$ \r
r -> forall w (m :: * -> *) s a. FocusingWith w m s a -> m (s, a, w)
unfocusingWith forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Zoomed (RWST r w s z) c) t s
l (forall w (m :: * -> *) s a. m (s, a, w) -> FocusingWith w m s a
FocusingWith forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. r -> s -> z (c, s, w)
m r
r)
{-# INLINE zoom #-}
instance (Monoid w, Zoom m n s t) => Zoom (Strict.WriterT w m) (Strict.WriterT w n) s t where
zoom :: forall c.
LensLike' (Zoomed (WriterT w m) c) t s
-> WriterT w m c -> WriterT w n c
zoom LensLike' (Zoomed (WriterT w m) c) t s
l = forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Strict.WriterT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom (\s -> Zoomed m (c, w) s
afb -> forall w (k :: * -> * -> *) s a. FocusingPlus w k s a -> k (s, w) a
unfocusingPlus forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Zoomed (WriterT w m) c) t s
l (forall w (k :: * -> * -> *) s a. k (s, w) a -> FocusingPlus w k s a
FocusingPlus forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. s -> Zoomed m (c, w) s
afb)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
Strict.runWriterT
{-# INLINE zoom #-}
instance (Monoid w, Zoom m n s t) => Zoom (Lazy.WriterT w m) (Lazy.WriterT w n) s t where
zoom :: forall c.
LensLike' (Zoomed (WriterT w m) c) t s
-> WriterT w m c -> WriterT w n c
zoom LensLike' (Zoomed (WriterT w m) c) t s
l = forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Lazy.WriterT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom (\s -> Zoomed m (c, w) s
afb -> forall w (k :: * -> * -> *) s a. FocusingPlus w k s a -> k (s, w) a
unfocusingPlus forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Zoomed (WriterT w m) c) t s
l (forall w (k :: * -> * -> *) s a. k (s, w) a -> FocusingPlus w k s a
FocusingPlus forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. s -> Zoomed m (c, w) s
afb)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
Lazy.runWriterT
{-# INLINE zoom #-}
#if !MIN_VERSION_mtl(2, 3, 0) && !MIN_VERSION_transformers(0, 6, 0)
instance Zoom m n s t => Zoom (ListT m) (ListT n) s t where
zoom :: forall c.
LensLike' (Zoomed (ListT m) c) t s -> ListT m c -> ListT n c
zoom LensLike' (Zoomed (ListT m) c) t s
l = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom (\s -> Zoomed m [c] s
afb -> forall (f :: * -> *) (k :: * -> * -> *) s a.
FocusingOn f k s a -> k (f s) a
unfocusingOn forall b c a. (b -> c) -> (a -> b) -> a -> c
. LensLike' (Zoomed (ListT m) c) t s
l (forall (f :: * -> *) (k :: * -> * -> *) s a.
k (f s) a -> FocusingOn f k s a
FocusingOn forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Zoomed m [c] s
afb)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. ListT m a -> m [a]
runListT
{-# INLINE zoom #-}
instance (Error e, Zoom m n s t) => Zoom (ErrorT e m) (ErrorT e n) s t where
zoom :: forall c.
LensLike' (Zoomed (ErrorT e m) c) t s
-> ErrorT e m c -> ErrorT e n c
zoom LensLike' (Zoomed (ErrorT e m) c) t s
l = forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall e a. Err e a -> Either e a
getErr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom (\s -> Zoomed m (Err e c) s
afb -> forall e (k :: * -> * -> *) s a.
FocusingErr e k s a -> k (Err e s) a
unfocusingErr forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Zoomed (ErrorT e m) c) t s
l (forall e (k :: * -> * -> *) s a.
k (Err e s) a -> FocusingErr e k s a
FocusingErr forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. s -> Zoomed m (Err e c) s
afb)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall e a. Either e a -> Err e a
Err forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT
{-# INLINE zoom #-}
#endif
instance Zoom m n s t => Zoom (MaybeT m) (MaybeT n) s t where
zoom :: forall c.
LensLike' (Zoomed (MaybeT m) c) t s -> MaybeT m c -> MaybeT n c
zoom LensLike' (Zoomed (MaybeT m) c) t s
l = forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a. May a -> Maybe a
getMay forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom (\s -> Zoomed m (May c) s
afb -> forall (k :: * -> * -> *) s a. FocusingMay k s a -> k (May s) a
unfocusingMay forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Zoomed (MaybeT m) c) t s
l (forall (k :: * -> * -> *) s a. k (May s) a -> FocusingMay k s a
FocusingMay forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. s -> Zoomed m (May c) s
afb)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a. Maybe a -> May a
May forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT
{-# INLINE zoom #-}
instance Zoom m n s t => Zoom (ExceptT e m) (ExceptT e n) s t where
zoom :: forall c.
LensLike' (Zoomed (ExceptT e m) c) t s
-> ExceptT e m c -> ExceptT e n c
zoom LensLike' (Zoomed (ExceptT e m) c) t s
l = forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall e a. Err e a -> Either e a
getErr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom (\s -> Zoomed m (Err e c) s
afb -> forall e (k :: * -> * -> *) s a.
FocusingErr e k s a -> k (Err e s) a
unfocusingErr forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Zoomed (ExceptT e m) c) t s
l (forall e (k :: * -> * -> *) s a.
k (Err e s) a -> FocusingErr e k s a
FocusingErr forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. s -> Zoomed m (Err e c) s
afb)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall e a. Either e a -> Err e a
Err forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT
{-# INLINE zoom #-}
#if MIN_VERSION_base(4,9,0)
type family Magnified (m :: Type -> Type) :: Type -> Type -> Type
#else
type family Magnified (m :: * -> *) :: * -> * -> *
#endif
type instance Magnified (ReaderT b m) = Effect m
type instance Magnified ((->)b) = Const
type instance Magnified (Strict.RWST a w s m) = EffectRWS w s m
type instance Magnified (Lazy.RWST a w s m) = EffectRWS w s m
type instance Magnified (IdentityT m) = Magnified m
infixr 2 `magnify`
class (MonadReader b m, MonadReader a n) => Magnify m n b a | m -> b, n -> a, m a -> n, n b -> m where
magnify :: LensLike' (Magnified m c) a b -> m c -> n c
instance Monad m => Magnify (ReaderT b m) (ReaderT a m) b a where
magnify :: forall c.
LensLike' (Magnified (ReaderT b m) c) a b
-> ReaderT b m c -> ReaderT a m c
magnify LensLike' (Magnified (ReaderT b m) c) a b
l (ReaderT b -> m c
m) = forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r a. Effect m r a -> m r
getEffect forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Magnified (ReaderT b m) c) a b
l (forall (m :: * -> *) r a. m r -> Effect m r a
Effect forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. b -> m c
m)
{-# INLINE magnify #-}
instance Magnify ((->) b) ((->) a) b a where
magnify :: forall c.
LensLike' (Magnified ((->) b) c) a b -> (b -> c) -> a -> c
magnify LensLike' (Magnified ((->) b) c) a b
l b -> c
f = forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
Reader.asks (forall {k} a (b :: k). Const a b -> a
getConst forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Magnified ((->) b) c) a b
l (forall {k} a (b :: k). a -> Const a b
Const forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. b -> c
f))
{-# INLINE magnify #-}
instance (Monad m, Monoid w) => Magnify (Strict.RWST b w s m) (Strict.RWST a w s m) b a where
magnify :: forall c.
LensLike' (Magnified (RWST b w s m) c) a b
-> RWST b w s m c -> RWST a w s m c
magnify LensLike' (Magnified (RWST b w s m) c) a b
l (Strict.RWST b -> s -> m (c, s, w)
m) = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Strict.RWST forall a b. (a -> b) -> a -> b
$ forall w st (m :: * -> *) s a.
EffectRWS w st m s a -> st -> m (s, st, w)
getEffectRWS forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Magnified (RWST b w s m) c) a b
l (forall w st (m :: * -> *) s a.
(st -> m (s, st, w)) -> EffectRWS w st m s a
EffectRWS forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. b -> s -> m (c, s, w)
m)
{-# INLINE magnify #-}
instance (Monad m, Monoid w) => Magnify (Lazy.RWST b w s m) (Lazy.RWST a w s m) b a where
magnify :: forall c.
LensLike' (Magnified (RWST b w s m) c) a b
-> RWST b w s m c -> RWST a w s m c
magnify LensLike' (Magnified (RWST b w s m) c) a b
l (Lazy.RWST b -> s -> m (c, s, w)
m) = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Lazy.RWST forall a b. (a -> b) -> a -> b
$ forall w st (m :: * -> *) s a.
EffectRWS w st m s a -> st -> m (s, st, w)
getEffectRWS forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. LensLike' (Magnified (RWST b w s m) c) a b
l (forall w st (m :: * -> *) s a.
(st -> m (s, st, w)) -> EffectRWS w st m s a
EffectRWS forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. b -> s -> m (c, s, w)
m)
{-# INLINE magnify #-}
instance Magnify m n b a => Magnify (IdentityT m) (IdentityT n) b a where
magnify :: forall c.
LensLike' (Magnified (IdentityT m) c) a b
-> IdentityT m c -> IdentityT n c
magnify LensLike' (Magnified (IdentityT m) c) a b
l (IdentityT m c
m) = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (forall (m :: * -> *) (n :: * -> *) b a c.
Magnify m n b a =>
LensLike' (Magnified m c) a b -> m c -> n c
magnify LensLike' (Magnified (IdentityT m) c) a b
l m c
m)
{-# INLINE magnify #-}
newtype Effect m r a = Effect { forall (m :: * -> *) r a. Effect m r a -> m r
getEffect :: m r }
instance Functor (Effect m r) where
fmap :: forall a b. (a -> b) -> Effect m r a -> Effect m r b
fmap a -> b
_ (Effect m r
m) = forall (m :: * -> *) r a. m r -> Effect m r a
Effect m r
m
{-# INLINE fmap #-}
instance (Monad m, Monoid r) => Monoid (Effect m r a) where
mempty :: Effect m r a
mempty = forall (m :: * -> *) r a. m r -> Effect m r a
Effect (forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty)
{-# INLINE mempty #-}
#if !MIN_VERSION_base(4,11,0)
Effect ma `mappend` Effect mb = Effect (liftM2 mappend ma mb)
{-# INLINE mappend #-}
#else
instance (Monad m, Semigroup r) => Semigroup (Effect m r a) where
Effect m r
ma <> :: Effect m r a -> Effect m r a -> Effect m r a
<> Effect m r
mb = forall (m :: * -> *) r a. m r -> Effect m r a
Effect (forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a. Semigroup a => a -> a -> a
(<>) m r
ma m r
mb)
{-# INLINE (<>) #-}
#endif
instance (Monad m, Monoid r) => Applicative (Effect m r) where
pure :: forall a. a -> Effect m r a
pure a
_ = forall (m :: * -> *) r a. m r -> Effect m r a
Effect (forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty)
{-# INLINE pure #-}
Effect m r
ma <*> :: forall a b. Effect m r (a -> b) -> Effect m r a -> Effect m r b
<*> Effect m r
mb = forall (m :: * -> *) r a. m r -> Effect m r a
Effect (forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a. Monoid a => a -> a -> a
mappend m r
ma m r
mb)
{-# INLINE (<*>) #-}
newtype EffectRWS w st m s a = EffectRWS { forall w st (m :: * -> *) s a.
EffectRWS w st m s a -> st -> m (s, st, w)
getEffectRWS :: st -> m (s,st,w) }
instance Functor (EffectRWS w st m s) where
fmap :: forall a b.
(a -> b) -> EffectRWS w st m s a -> EffectRWS w st m s b
fmap a -> b
_ (EffectRWS st -> m (s, st, w)
m) = forall w st (m :: * -> *) s a.
(st -> m (s, st, w)) -> EffectRWS w st m s a
EffectRWS st -> m (s, st, w)
m
{-# INLINE fmap #-}
instance (Monoid s, Monoid w, Monad m) => Applicative (EffectRWS w st m s) where
pure :: forall a. a -> EffectRWS w st m s a
pure a
_ = forall w st (m :: * -> *) s a.
(st -> m (s, st, w)) -> EffectRWS w st m s a
EffectRWS forall a b. (a -> b) -> a -> b
$ \st
st -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Monoid a => a
mempty, st
st, forall a. Monoid a => a
mempty)
{-# INLINE pure #-}
EffectRWS st -> m (s, st, w)
m <*> :: forall a b.
EffectRWS w st m s (a -> b)
-> EffectRWS w st m s a -> EffectRWS w st m s b
<*> EffectRWS st -> m (s, st, w)
n = forall w st (m :: * -> *) s a.
(st -> m (s, st, w)) -> EffectRWS w st m s a
EffectRWS forall a b. (a -> b) -> a -> b
$ \st
st -> st -> m (s, st, w)
m st
st forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ (s
s,st
t,w
w) -> st -> m (s, st, w)
n st
t forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ (s
s',st
u,w
w') -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Monoid a => a -> a -> a
mappend s
s s
s', st
u, forall a. Monoid a => a -> a -> a
mappend w
w w
w')
{-# INLINE (<*>) #-}