HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulelens-5.3.5Haskell2010

Control.Lens.Zoom

  • 2 classes
  • Packagelens-5.3.5
  • Exports4
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceZoom.hs
familytype family Magnified (m :: Type -> Type) :: Type -> Type -> Type
#

This type family is used by Magnify to describe the common effect type.

Instances6Magnified
classclass (Magnified m ~ Magnified n, MonadReader b m, MonadReader a n) => Magnify (m :: Type -> Type) (n :: Type -> Type) b a | m -> b, n -> a, m a -> n, n b -> m where
#

This class allows us to use magnify part of the environment, changing the environment supplied by many different Monad transformers. Unlike zoom this can change the environment of a deeply nested Monad transformer.

Also, unlike zoom, this can be used with any valid Getter, but cannot be used with a Traversal or Fold.

Methods

  • magnify :: ((Functor (Magnified m c), Contravariant (Magnified m c)) => LensLike' (Magnified m c) a b) -> m c -> n cinfixr 2

    Run a monadic action in a larger environment than it was defined in, using a Getter.

    This acts like local, but can in many cases change the type of the environment as well.

    This is commonly used to lift actions in a simpler Reader Monad into a Monad with a larger environment type.

    This can be used to edit pretty much any Monad transformer stack with an environment in it:

    Example1 expression
    (1,2) & magnify _2 (+1)3
    Example1 expression
    flip Reader.runReader (1,2) $ magnify _1 Reader.ask1
    Example1 expression
    flip Reader.runReader (1,2,[10..20]) $ magnify (_3._tail) Reader.ask[11,12,13,14,15,16,17,18,19,20]

    The type can be read as

      magnify :: LensLike' (Magnified m c) a b -> m c -> n c
    

    but the higher-rank constraints make it easier to apply magnify to a Getter in highly-polymorphic code.

    magnify :: Getter s a -> (a -> r) -> s -> r
    magnify :: Monoid r => Fold s a   -> (a -> r) -> s -> r
    
    magnify :: Monoid w                 => Getter s t -> RWS t w st c -> RWS s w st c
    magnify :: (Monoid w, Monoid c) => Fold s a   -> RWS a w st c -> RWS s w st c
    ...
    
Instances6Magnify
classclass (MonadState s m, MonadState t n) => Zoom (m :: Type -> Type) (n :: Type -> Type) s t | m -> s, n -> t, m t -> n, n s -> m where
#

This class allows us to use zoom in, changing the State supplied by many different Monad transformers, potentially quite deep in a Monad transformer stack.

Methods

  • zoom :: LensLike' (Zoomed m c) t s -> m c -> n cinfixr 2

    Run a monadic action in a larger State than it was defined in, using a Lens' or Control.Lens.Traversal.Traversal'.

    This is commonly used to lift actions in a simpler State Monad into a State Monad with a larger State type.

    When applied to a Control.Lens.Traversal.Traversal' over multiple values, the actions for each target are executed sequentially and the results are aggregated.

    This can be used to edit pretty much any Monad transformer stack with a State in it!

    Example1 expression
    flip State.evalState (a,b) $ zoom _1 $ use ida
    Example1 expression
    flip State.execState (a,b) $ zoom _1 $ id .= c(c,b)
    Example1 expression
    flip State.execState [(a,b),(c,d)] $ zoom traverse $ _2 %= f[(a,f b),(c,f d)]
    Example1 expression
    flip State.runState [(a,b),(c,d)] $ zoom traverse $ _2 <%= f(f b <> f d <> mempty,[(a,f b),(c,f d)])
    Example1 expression
    flip State.evalState (a,b) $ zoom both (use id)a <> b
    zoom :: Monad m             => Lens' s t      -> StateT t m a -> StateT s m a
    zoom :: (Monad m, Monoid c) => Control.Lens.Traversal.Traversal' s t -> StateT t m c -> StateT s m c
    zoom :: (Monad m, Monoid w)             => Lens' s t      -> RWST r w t m c -> RWST r w s m c
    zoom :: (Monad m, Monoid w, Monoid c) => Control.Lens.Traversal.Traversal' s t -> RWST r w t m c -> RWST r w s m c
    zoom :: (Monad m, Monoid w, Error e)  => Lens' s t      -> ErrorT e (RWST r w t m) c -> ErrorT e (RWST r w s m) c
    zoom :: (Monad m, Monoid w, Monoid c, Error e) => Control.Lens.Traversal.Traversal' s t -> ErrorT e (RWST r w t m) c -> ErrorT e (RWST r w s m) c
    ...
    
Instances11Zoom, …
familytype family Zoomed (m :: Type -> Type) :: Type -> Type -> Type
#

This type family is used by Zoom to describe the common effect type.

Instances11Zoomed, …