{-# 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

-- This is needed because ErrorT is deprecated.
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}


{- |
Module      :  Lens.Micro.Mtl.Internal
Copyright   :  (C) 2013-2016 Edward Kmett, 2015-2016 Artyom Kazak, 2018 Monadfix
License     :  BSD-style (see the file LICENSE)

This module lets you define your own instances of 'Zoom' and 'Magnify'.

The warning from "Lens.Micro.Internal" applies to this module as well. Don't export functions that have 'Zoom' or 'Magnify' in their type signatures. If you absolutely need to define an instance (e.g. for internal use), only do it for your own types, because otherwise I might add an instance to one of the microlens packages later and if our instances are different it might lead to subtle bugs.
-}
module Lens.Micro.Mtl.Internal
(
  -- * Classes
  Zoomed,
  Zoom(..),
  Magnified,
  Magnify(..),

  -- * Focusing (used for 'Zoom')
  Focusing(..),
  FocusingWith(..),
  FocusingPlus(..),
  FocusingOn(..),
  FocusingMay(..),
  FocusingErr(..),

  -- * Effect (used for 'Magnify')
  Effect(..),
  EffectRWS(..),

  -- * Utilities
  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
-- microlens
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


------------------------------------------------------------------------------
-- Zoomed
------------------------------------------------------------------------------

-- | This type family is used by 'Zoom' to describe the common effect type.
#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)

------------------------------------------------------------------------------
-- Focusing
------------------------------------------------------------------------------

-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.State.StateT'.
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 (<*>) #-}

------------------------------------------------------------------------------
-- FocusingWith
------------------------------------------------------------------------------

-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.RWS.RWST'.
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 (<*>) #-}

------------------------------------------------------------------------------
-- FocusingPlus
------------------------------------------------------------------------------

-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.Writer.WriterT'.
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 (<*>) #-}

------------------------------------------------------------------------------
-- FocusingOn
------------------------------------------------------------------------------

-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.Trans.Maybe.MaybeT' or 'Control.Monad.Trans.List.ListT'.
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 (<*>) #-}

------------------------------------------------------------------------------
-- May
------------------------------------------------------------------------------

-- | Make a 'Monoid' out of 'Maybe' for error handling.
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

------------------------------------------------------------------------------
-- FocusingMay
------------------------------------------------------------------------------

-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.Error.ErrorT'.
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 (<*>) #-}

------------------------------------------------------------------------------
-- Err
------------------------------------------------------------------------------

-- | Make a 'Monoid' out of 'Either' for error handling.
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

------------------------------------------------------------------------------
-- FocusingErr
------------------------------------------------------------------------------

-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.Error.ErrorT'.
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 (<*>) #-}

------------------------------------------------------------------------------
-- Zoom
------------------------------------------------------------------------------

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
  {- |
When you're in a state monad, this function lets you operate on a part of your state. For instance, if your state was a record containing a @position@ field, after zooming @position@ would become your whole state (and when you modify it, the bigger structure would be modified as well).

(Your 'Lazy.State' \/ 'Lazy.StateT' or 'Lazy.RWS' \/ 'Lazy.RWST' can be anywhere in the stack, but you can't use 'zoom' with arbitrary 'MonadState' because it doesn't provide any methods to change the type of the state. See <https://github.com/ekmett/lens/issues/316 this issue> for details.)

For the sake of the example, let's define some types first:

@
data Position = Position {
  _x, _y :: Int }

data Player = Player {
  _position :: Position,
  ... }

data Game = Game {
  _player :: Player,
  _obstacles :: [Position],
  ... }

concat \<$\> mapM makeLenses [''Position, ''Player, ''Game]
@

Now, here's an action that moves the player north-east:

@
moveNE :: 'Lazy.State' Game ()
moveNE = do
  player.position.x 'Lens.Micro.Mtl.+=' 1
  player.position.y 'Lens.Micro.Mtl.+=' 1
@

With 'zoom', you can use @player.position@ to focus just on a part of the state:

@
moveNE :: 'Lazy.State' Game ()
moveNE = do
  'zoom' (player.position) $ do
    x 'Lens.Micro.Mtl.+=' 1
    y 'Lens.Micro.Mtl.+=' 1
@

You can just as well use it for retrieving things out of the state:

@
getCoords :: 'Lazy.State' Game (Int, Int)
getCoords = 'zoom' (player.position) ((,) '<$>' 'Lens.Micro.Mtl.use' x '<*>' 'Lens.Micro.Mtl.use' y)
@

Or more explicitly:

@
getCoords = 'zoom' (player.position) $ do
  x' <- 'Lens.Micro.Mtl.use' x
  y' <- 'Lens.Micro.Mtl.use' y
  return (x', y')
@

When you pass a traversal to 'zoom', it'll work as a loop. For instance, here we move all obstacles:

@
moveObstaclesNE :: 'Lazy.State' Game ()
moveObstaclesNE = do
  'zoom' (obstacles.'each') $ do
    x 'Lens.Micro.Mtl.+=' 1
    y 'Lens.Micro.Mtl.+=' 1
@

If the action returns a result, all results would be combined with '<>' – the same way they're combined when '^.' is passed a traversal. In this example, @moveObstaclesNE@ returns a list of old coordinates of obstacles in addition to moving them:

@
moveObstaclesNE = do
  xys <- 'zoom' (obstacles.'each') $ do
    -- Get old coordinates.
    x' <- 'Lens.Micro.Mtl.use' x
    y' <- 'Lens.Micro.Mtl.use' y
    -- Update them.
    x 'Lens.Micro.Mtl..=' x' + 1
    y 'Lens.Micro.Mtl..=' y' + 1
    -- Return a single-element list with old coordinates.
    return [(x', y')]
  ...
@

Finally, you might need to write your own instances of 'Zoom' if you use @newtype@d transformers in your monad stack. This can be done as follows:

@
import "Lens.Micro.Mtl.Internal"

type instance 'Zoomed' (MyStateT s m) = 'Zoomed' (StateT s m)

instance Monad m =\> 'Zoom' (MyStateT s m) (MyStateT t m) s t where
    'zoom' l (MyStateT m) = MyStateT ('zoom' l m)
@
  -}
  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 #-}

-- TODO: instance Zoom m m a a => Zoom (ContT r m) (ContT r m) a a where

------------------------------------------------------------------------------
-- Magnified
------------------------------------------------------------------------------

-- | This type family is used by 'Magnify' to describe the common effect type.
#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

------------------------------------------------------------------------------
-- Magnify
------------------------------------------------------------------------------

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
  {- |
This is an equivalent of 'Reader.local' which lets you apply a getter to your environment instead of merely applying a function (and it also lets you change the type of the environment).

@
'Reader.local'   :: (r -> r)   -> 'Reader.Reader' r a -> 'Reader.Reader' r a
'magnify' :: Getter r x -> 'Reader.Reader' x a -> 'Reader.Reader' r a
@

'magnify' works with 'Reader.Reader' \/ 'Reader.ReaderT', 'Lazy.RWS' \/ 'Lazy.RWST', and @(->)@.

Here's an example of 'magnify' being used to work with a part of a bigger config. First, the types:

@
data URL = URL {
  _protocol :: Maybe String,
  _path :: String }

data Config = Config {
  _base :: URL,
  ... }

makeLenses ''URL
makeLenses ''Config
@

Now, let's define a function which returns the base url:

@
getBase :: 'Reader.Reader' Config String
getBase = do
  protocol \<- 'Data.Maybe.fromMaybe' \"https\" '<$>' 'Lens.Micro.Mtl.view' (base.protocol)
  path     \<- 'Lens.Micro.Mtl.view' (base.path)
  return (protocol ++ path)
@

With 'magnify', we can factor out @base@:

@
getBase = 'magnify' base $ do
  protocol \<- 'Data.Maybe.fromMaybe' \"https\" '<$>' 'Lens.Micro.Mtl.view' protocol
  path     \<- 'Lens.Micro.Mtl.view' path
  return (protocol ++ path)
@

This concludes the example.

Finally, you should know writing instances of 'Magnify' for your own types can be done as follows:

@
import "Lens.Micro.Mtl.Internal"

type instance 'Magnified' (MyReaderT r m) = 'Magnified' (ReaderT r m)

instance Monad m =\> 'Magnify' (MyReaderT r m) (MyReaderT t m) r t where
    'magnify' l (MyReaderT m) = MyReaderT ('magnify' l m)
@
  -}
  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 #-}

-----------------------------------------------------------------------------
--- Effect
-------------------------------------------------------------------------------

-- | Wrap a monadic effect with a phantom type argument.
newtype Effect m r a = Effect { forall (m :: * -> *) r a. Effect m r a -> m r
getEffect :: m r }
-- type role Effect representational nominal phantom

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 (<*>) #-}

------------------------------------------------------------------------------
-- EffectRWS
------------------------------------------------------------------------------

-- | Wrap a monadic effect with a phantom type argument. Used when magnifying 'Control.Monad.RWS.RWST'.
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 (<*>) #-}