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

Moduleoptics-extra-0.4.2.1Haskell2010

Optics.Zoom

  • 3 classes

Zoom

1 declaration
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 zoom in, changing the State supplied by many different monad transformers, potentially quite deep in a monad transformer stack.

Its functions can be used to run a monadic action in a larger State than it was defined in, using a Lens', an AffineTraversal' or a Traversal'.

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

When used with a 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 L.evalState ('a','b') $ zoom _1 $ use equality'a'
Example1 expression
flip S.execState ('a','b') $ zoom _1 $ equality .= 'c'('c','b')
Example1 expression
flip L.execState [(1,2),(3,4)] $ zoomMany traversed $ _2 %= (*10)[(1,20),(3,40)]
Example1 expression
flip S.runState [('a',"b"),('c',"d")] $ zoomMany traversed $ _2 <%= (\x -> x <> x)("bbdd",[('a',"bb"),('c',"dd")])
Example1 expression
flip S.evalState ("a","b") $ zoomMany each (use equality)"ab"

Methods

Instances10Zoom, …

Magnify

2 declarations
classclass (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 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.

Its functions can be used to run a monadic action in a larger environment than it was defined in, using a Getter or an AffineFold.

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

They're commonly used to lift actions in a simpler Reader Monad into a Monad with a larger environment type.

They 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 runReader (1,2) $ magnify _1 ask1
Example1 expression
flip runReader (1,2,[10..20]) $ magnifyMaybe (_3 % _tail) askJust [11,12,13,14,15,16,17,18,19,20]

Methods

Instances11Magnify, …
classclass (MonadReader b m, MonadReader a n, Magnify m n b a) => MagnifyMany (m :: Type -> Type) (n :: Type -> Type) b a | m -> b, n -> a, m a -> n, n b -> m where
#

Extends Magnify with an ability to magnify using a Fold over multiple targets so that actions for each one are executed sequentially and the results are aggregated.

There is however no sensible instance of MagnifyMany for StateT.

Methods

Instances9MagnifyMany, …