Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zoom in acid-state?

data Foo = Foo {
  _bar :: Map String Integer
} deriving (Eq, Ord, Read, Show, Data, Typeable)

$(deriveSafeCopy 0 'base 'Foo)

$(makeLenses ''Foo)

Given the above code I am under the impression that it should be possible to do this:

addEntry :: String -> Update Foo ()
addEntry s = zoom bar $ modify $ insert s 0

But GHC will complain along the lines of:

src/Backend.hs:39:20:
    No instance for (Functor
                       (Control.Lens.Internal.Zoom.Zoomed (Update Foo) ()))

Any ideas?

like image 725
fho Avatar asked Dec 19 '13 16:12

fho


1 Answers

Control.Lens.Internal.Zoom.Zoomed is a type family which describes what kind of context is required during a zoom. It performs some special magic as you can see in the Control.Lens.Internal.Zoom module. Typically a user of zoom would never need to see that stuff so long as they zoom in on a "typical" monad transformer stack.

Update, while being implemented as just State beneath the covers, doesn't have a zoom instance. Its implementation is not exported either so you're unable to write your own, though it'd be quite trivial since Update doesn't use monad transformers.

type instance Zoomed (Update x) = Focusing Identity
like image 164
J. Abrahamson Avatar answered Sep 28 '22 08:09

J. Abrahamson