Simon Peyton-Jones | 2 Oct 23:51 2013
Picon

Using lenses

Friends

 

Some of you will know that I’ve promised to give a talk about Edward’s lens library at the Haskell Exchange in London next Wednesday (9th).  I did this to give everyone (including me) a break from GHC hackery, and also to force me to learn about this lens voodoo that everyone is twittering about.  Edward generously gave me quite a bit of one-to-one attention last week (my hair is still standing on end), but this message is to ask your help too.


Specifically, I’d like to give some compelling use-cases.   If you are using the lens library yourself, could you spare a few minutes to tell me how you are using it?  I expect to cover Lens and Traversal but not Prism.

 

The use-case everyone starts with is nested records, but I’d like to go beyond that.  The next levels seem to be:

·         Lenses as views of data that isn’t “really there” e.g. regarding a record with rectangular coordinates as having polar coordinates too.

·         Lenses and Traversals that focus on elements of finite maps (Control.Lens.At)

 

What else? I’m sure you are using them in all sorts of cool ways that I would never think of, and I’d love to know.

 

Please don’t tell me anything secret!

 

And don’t burn too many cycles on this...I don’t want to waste your time, and I can always get back to you if I can’t understand what you say.  Sooner is better than later...Weds is coming.

 

Simon “Edward’s prophet” PJ

 

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Simon Peyton-Jones | 3 Oct 10:07 2013
Picon

Using lenses

(I sent this to ‘libraries’ but Kim-Ee suggested adding Café, where so many smart people hang out.)

 

Friends

 

Some of you will know that I’ve promised to give a talk about Edward’s lens library at the Haskell Exchange in London next Wednesday (9th).  I did this to give everyone (including me) a break from GHC hackery, and also to force me to learn about this lens voodoo that everyone is twittering about.  Edward generously gave me quite a bit of one-to-one attention last week (my hair is still standing on end), but this message is to ask your help too.


Specifically, I’d like to give some compelling use-cases.   If you are using the lens library yourself, could you spare a few minutes to tell me how you are using it?  I expect to cover Lens and Traversal but not Prism.

 

The use-case everyone starts with is nested records, but I’d like to go beyond that.  The next levels seem to be:

·         Lenses as views of data that isn’t “really there” e.g. regarding a record with rectangular coordinates as having polar coordinates too.

·         Lenses and Traversals that focus on elements of finite maps (Control.Lens.At)

 

What else? I’m sure you are using them in all sorts of cool ways that I would never think of, and I’d love to know.

 

Please don’t tell me anything secret!  To give everyone the benefit I may just concatenate all the replies and send to you all, so please say if you don’t want me to do that with yours.

 

And don’t burn too many cycles on this...I don’t want to waste your time, and I can always get back to you if I can’t understand what you say.  Sooner is better than later...Weds is coming.

 

Simon “Edward’s prophet” PJ

 

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Roman Cheplyaka | 3 Oct 10:57 2013

Re: Using lenses

Hi Simon,

An interesting use case is my time-lens library.
http://hackage.haskell.org/package/time-lens-0.3/docs/Data-Time-Lens.html

You can do things like

  >>> modL minutes (+5) (TimeOfDay 16 57 13)
  17:02:13

But one has to be somewhat lenient about the lens laws here.

Roman

* Simon Peyton-Jones <simonpj <at> microsoft.com> [2013-10-03 08:07:12+0000]
> (I sent this to 'libraries' but Kim-Ee suggested adding Café, where so many smart people hang out.)
> 
> Friends
> 
> Some of you will know that I've promised to give a talk about Edward's lens
library<http://hackage.haskell.org/package/lens> at the Haskell
Exchange<http://skillsmatter.com/event/scala/haskell-exchange> in London next Wednesday (9th). 
I did this to give everyone (including me) a break from GHC hackery, and also to force me to learn about this
lens voodoo that everyone is twittering about.  Edward generously gave me quite a bit of one-to-one
attention last week (my hair is still standing on end), but this message is to ask your help too.
> 
> Specifically, I'd like to give some compelling use-cases.   If you are using the lens library yourself,
could you spare a few minutes to tell me how you are using it?  I expect to cover Lens and Traversal but not Prism.
> 
> The use-case everyone starts with is nested records, but I'd like to go beyond that.  The next levels seem to be:
> 
> ·         Lenses as views of data that isn't "really there" e.g. regarding a record with rectangular coordinates
as having polar coordinates too.
> 
> ·         Lenses and Traversals that focus on elements of finite maps (Control.Lens.At)
> 
> What else? I'm sure you are using them in all sorts of cool ways that I would never think of, and I'd love to know.
> 
> Please don't tell me anything secret!  To give everyone the benefit I may just concatenate all the replies
and send to you all, so please say if you don't want me to do that with yours.
> 
> And don't burn too many cycles on this...I don't want to waste your time, and I can always get back to you if I
can't understand what you say.  Sooner is better than later...Weds is coming.
> 
> Simon "Edward's prophet" PJ
> 

> _______________________________________________
> Libraries mailing list
> Libraries <at> haskell.org
> http://www.haskell.org/mailman/listinfo/libraries

> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Christopher Done | 3 Oct 17:28 2013
Picon

Re: Using lenses

On 3 October 2013 10:57, Roman Cheplyaka <roma <at> ro-che.info> wrote:
> An interesting use case is my time-lens library.
> http://hackage.haskell.org/package/time-lens-0.3/docs/Data-Time-Lens.html
>
> You can do things like
>
>   >>> modL minutes (+5) (TimeOfDay 16 57 13)
>   17:02:13
>
> But one has to be somewhat lenient about the lens laws here.

Relatedly the thyme package (really fast time implementation) provides
lenses for all its data types:
http://hackage.haskell.org/package/thyme
Mike Ledger | 3 Oct 11:01 2013
Picon

Re: Using lenses

Hi,

In a game I made recently, I had to load OBJ formatted models into an OpenGL-friendly format. To do that, I'd parse the .obj, into a simple ADT, and build the model into a vector. Here's where lens comes in: we want to build separate vectors for the vertices, normals, UVs and faces indices. lens made that very easy, in conjunction with the state monad. Unlike normal record fields, you can actually pass around lenses freely.

data ModelBuilder = ModelBuilder { _mbVertices, _mbNormals, _mbUvs :: [Vector Float], ... }
makeFields ''ModelBuilder

addTo label a = label %= (++ Vector.fromList a)


which would be used like:

case objCommand of
    V  x y z -> addTo vertices [x,y,z]
    VN x y z -> addTo normals [x,y,z]
    VT x y z -> addTo uvs [x,y,z]

    ...

I hope to actually release the game that uses this soon -- it's one of the very few 'complete' 3D games written in Haskell -- when I have the time, which will probably be around november.

- Mike



On Thu, Oct 3, 2013 at 6:07 PM, Simon Peyton-Jones <simonpj <at> microsoft.com> wrote:

(I sent this to ‘libraries’ but Kim-Ee suggested adding Café, where so many smart people hang out.)

 

Friends

 

Some of you will know that I’ve promised to give a talk about Edward’s lens library at the Haskell Exchange in London next Wednesday (9th).  I did this to give everyone (including me) a break from GHC hackery, and also to force me to learn about this lens voodoo that everyone is twittering about.  Edward generously gave me quite a bit of one-to-one attention last week (my hair is still standing on end), but this message is to ask your help too.


Specifically, I’d like to give some compelling use-cases.   If you are using the lens library yourself, could you spare a few minutes to tell me how you are using it?  I expect to cover Lens and Traversal but not Prism.

 

The use-case everyone starts with is nested records, but I’d like to go beyond that.  The next levels seem to be:

·         Lenses as views of data that isn’t “really there” e.g. regarding a record with rectangular coordinates as having polar coordinates too.

·         Lenses and Traversals that focus on elements of finite maps (Control.Lens.At)

 

What else? I’m sure you are using them in all sorts of cool ways that I would never think of, and I’d love to know.

 

Please don’t tell me anything secret!  To give everyone the benefit I may just concatenate all the replies and send to you all, so please say if you don’t want me to do that with yours.

 

And don’t burn too many cycles on this...I don’t want to waste your time, and I can always get back to you if I can’t understand what you say.  Sooner is better than later...Weds is coming.

 

Simon “Edward’s prophet” PJ

 


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Sebastiaan Visser | 3 Oct 12:25 2013
Picon

Re: Using lenses

Simon,

I've used lenses to manipulate URIs represented as strings in a structured way, like:

  modify (port . iso parsePrintUri) (+10) "http://localhost:8070/index.html"

Of course using fclabels and not lens ;-)

Sebastiaan

On Oct 3, 2013, at 10:07 AM, Simon Peyton-Jones <simonpj <at> microsoft.com> wrote:

> (I sent this to ‘libraries’ but Kim-Ee suggested adding Café, where so many smart people hang out.)
>  
> Friends
>  
> Some of you will know that I’ve promised to give a talk about Edward’s lens library at the Haskell
Exchange in London next Wednesday (9th).  I did this to give everyone (including me) a break from GHC
hackery, and also to force me to learn about this lens voodoo that everyone is twittering about.  Edward
generously gave me quite a bit of one-to-one attention last week (my hair is still standing on end), but
this message is to ask your help too.
> 
> Specifically, I’d like to give some compelling use-cases.   If you are using the lens library yourself,
could you spare a few minutes to tell me how you are using it?  I expect to cover Lens and Traversal but not Prism.
>  
> The use-case everyone starts with is nested records, but I’d like to go beyond that.  The next levels seem
to be:
> ·         Lenses as views of data that isn’t “really there” e.g. regarding a record with rectangular
coordinates as having polar coordinates too.
> ·         Lenses and Traversals that focus on elements of finite maps (Control.Lens.At)
>  
> What else? I’m sure you are using them in all sorts of cool ways that I would never think of, and I’d love
to know.
>  
> Please don’t tell me anything secret!  To give everyone the benefit I may just concatenate all the
replies and send to you all, so please say if you don’t want me to do that with yours.
>  
> And don’t burn too many cycles on this...I don’t want to waste your time, and I can always get back to you
if I can’t understand what you say.  Sooner is better than later...Weds is coming.
>  
> Simon “Edward’s prophet” PJ
>  
> _______________________________________________
> Libraries mailing list
> Libraries <at> haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
Gergely Risko | 3 Oct 12:51 2013
Picon
Picon

Re: Using lenses

Hi Simon,

http://hackage.haskell.org/package/lens-datetime-0.2/docs/Data-Time-Lens.html

Read the top of the page.

aDay = fromGregorian 2013 08 22
aLocal = LocalTime aDay (TimeOfDay 13 45 28)
aUTC = UTCTime aDay 7458.9

>>> aLocal ^. years
2013
>>> aUTC ^. months
8
>>> aDay ^. days
22
>>> aLocal & time .~ midnight
2013-08-22 00:00:00
>>> aUTC & days .~ 1 & months .~ 1
2013-01-01 02:04:18.9 UTC
>>> aLocal & hours +~ 1            -- But see the note below!
2013-08-22 14:45:28
>>> aLocal & flexDT.months +~ 12
2014-08-22 13:45:28
>>> aUTC & flexDT.days +~ 100
2013-11-30 02:04:18.9 UTC
>>> aLocal & flexDT.minutes +~ 120
2013-08-22 15:45:28

Gergely
Tony Morris | 3 Oct 14:06 2013
Picon

Re: Using lenses

Lenses for nested sum types e.g. Either.

On 03/10/2013 6:08 PM, "Simon Peyton-Jones" <simonpj <at> microsoft.com> wrote:

(I sent this to ‘libraries’ but Kim-Ee suggested adding Café, where so many smart people hang out.)

 

Friends

 

Some of you will know that I’ve promised to give a talk about Edward’s lens library at the Haskell Exchange in London next Wednesday (9th).  I did this to give everyone (including me) a break from GHC hackery, and also to force me to learn about this lens voodoo that everyone is twittering about.  Edward generously gave me quite a bit of one-to-one attention last week (my hair is still standing on end), but this message is to ask your help too.


Specifically, I’d like to give some compelling use-cases.   If you are using the lens library yourself, could you spare a few minutes to tell me how you are using it?  I expect to cover Lens and Traversal but not Prism.

 

The use-case everyone starts with is nested records, but I’d like to go beyond that.  The next levels seem to be:

·         Lenses as views of data that isn’t “really there” e.g. regarding a record with rectangular coordinates as having polar coordinates too.

·         Lenses and Traversals that focus on elements of finite maps (Control.Lens.At)

 

What else? I’m sure you are using them in all sorts of cool ways that I would never think of, and I’d love to know.

 

Please don’t tell me anything secret!  To give everyone the benefit I may just concatenate all the replies and send to you all, so please say if you don’t want me to do that with yours.

 

And don’t burn too many cycles on this...I don’t want to waste your time, and I can always get back to you if I can’t understand what you say.  Sooner is better than later...Weds is coming.

 

Simon “Edward’s prophet” PJ

 


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Nicolas Trangez | 3 Oct 10:47 2013

Re: Using lenses

Simon,

On Thu, 2013-10-03 at 08:07 +0000, Simon Peyton-Jones wrote:
> If you are using the lens library yourself, could you spare a few
minutes to tell me how you are using it?

I'm not a heavy 'lens'-user (yet), and this might not be the most pretty
use-case from a theoretic point of view, but anyway:

I use 'lens' in Kontiki [1], my implementation of Raft [2], a fairly
recent consensus protocol for distributed systems (think Paxos &
multi-Paxos, Zookeeper,...). The Raft protocol description is fairly
imperative ("When in state S, if you receive a message M, broadcast
message M'(M) to all other nodes, append entry E(M) to your log, and
update the state to S'(S)"), so in order to keep the code close to this
description, I created a TransitionT monad transformer (where the
underlying monad should, in some cases, provide access to the persisted
log of the system, most likely requiring IO access, but this is
abstracted over), which is basically RWST.

The R environment gives access to system configuration (e.g. the set of
nodes (or at least their names/IDs) part of the cluster), W is used to
output/list Commands to be executed as part of a state transition, S
gives access to the current state, and a transition should yield a new
state (or the old one if e.g. a message is received which should be
ignored in the current state). See [3] for an overview of the types
involved.

Finally, after this long introduction, the 'lens' part: thanks to the
'lens' operators, and my types involved in the TransitionT monad
(specifically the R environment and S state) being lens'ified, accessing
R & S becomes fairly unobtrusive and more imperative-looking (which
makes sense when using RWS). As an example, in [4], you see some parts
of the current (Candidate) state being accessed:

    currentTerm <- use cCurrentTerm
    votes <- use cVotes

Not using 'lens', this would be something (OTOH) like

    currentTerm <- fmap _cCurrentTerm get

which is less familiar from an imperative POV.

Then also updating the current state 'in-place' (not for real,
obviously):

    cVotes %= Set.insert sender

And accessing the configuration (R) environment, e.g.:

    nodeId <- view configNodeId

See other handler functions in that module, or in the Follower and
Leader modules in the same directory, for more examples.

Regards,

Nicolas

[1] https://github.com/NicolasT/kontiki
[2]
https://ramcloud.stanford.edu/wiki/download/attachments/11370504/raft.pdf
[3]
https://github.com/NicolasT/kontiki/blob/master/src/Network/Kontiki/Types.hs
[4]
https://github.com/NicolasT/kontiki/blob/master/src/Network/Kontiki/Raft/Candidate.hs#L32

Gmane