Optics?

Alan & Kim Zimmerman alan.zimm at gmail.com
Sun Oct 3 22:34:21 UTC 2021


With a pointer from Vlad and some study of the lens tutorial, I made a
proof of concept at [1].
I am deliberately not using the existing lens library as I envisage this
code ending up in GHC.

Alan

[1]
https://github.com/alanz/ghc-exactprint/blob/f218e211c47943c216a2e25d7855f98a0355f6b8/src/Language/Haskell/GHC/ExactPrint/ExactPrint.hs#L689-L723



On Sun, 3 Oct 2021 at 18:52, Vladislav Zavialov <vladislav at serokell.io>
wrote:

> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20211003/8413eef4/attachment.html>


More information about the ghc-devs mailing list