Re: Optics?

2021-10-04 Thread Sebastian Graf
Hi Alan, hi Vlad,

Yes, one thing that is nice about van Laarhoven lenses is that you don't
actually need to depend on anything if all you want is export lenses in
your API.

We have also discussed using a small lens library in the past, in
https://gitlab.haskell.org/ghc/ghc/-/issues/18693.
The MVP would be to just depend on the Lens module defined in newer
versions of Cabal.

Sebastian

Am Mo., 4. Okt. 2021 um 00:35 Uhr schrieb Alan & Kim Zimmerman <
alan.z...@gmail.com>:

> 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 
> 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 
>> 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@haskell.org
>> > http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>>
>> ___
> ghc-devs mailing list
> ghc-devs@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Re: Optics?

2021-10-03 Thread Alan & Kim Zimmerman
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 
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 
> 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@haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
>
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Re: Optics?

2021-10-03 Thread Vladislav Zavialov
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  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@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs