Optics?

Vladislav Zavialov vladislav at serokell.io
Sun Oct 3 17:52:04 UTC 2021


Hi Alan,

Your pair of functions can be packaged up as a single function, so that

	getEpa :: a -> EpaLocation
	setEpa :: a -> EpaLocation -> a

becomes

	lensEpa :: forall f. Functor f => (EpaLocation -> f EpaLocation) -> (a -> f a)  

And the get/set parts can be recovered by instantiating `f` to either Identity or Const.

The nice thing about lenses is that they compose, so that if you need nested access, you could define several lenses, compose them together, and then reach deep into a data structure. Then lenses might offer some simplification. Otherwise, an ordinary getter/setter pair is just as good.

- Vlad

> On 3 Oct 2021, at 20:40, Alan & Kim Zimmerman <alan.zimm at gmail.com> wrote:
> 
> Hi all
> 
> I am working on a variant of the exact printer which updates the annotation locations from the `EpaSpan` version to the `EpaDelta` version, as the printing happens
> 
> data EpaLocation = EpaSpan RealSrcSpan
>                  | EpaDelta DeltaPos
> 
> The function doing the work is this
> 
> markAnnKw :: (Monad m, Monoid w)
>   => EpAnn a -> (a -> EpaLocation) -> (a -> EpaLocation -> a) -> AnnKeywordId -> EP w m (EpAnn a)
> 
> which gets an annotation, a function to pull a specific location out, and one to update it.
> 
> I do not know much about lenses, but have a feeling that I could simplify things by using one.
> 
> Can anyone give me any pointers?
> 
> Alan
> 
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs



More information about the ghc-devs mailing list