Re: [Haskell-cafe] Using lenses

2013-10-03 Thread Nicolas Trangez
Simon,

On Thu, 2013-10-03 at 08:07 +, 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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Using lenses

2013-10-03 Thread Roman Cheplyaka
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 simo...@microsoft.com [2013-10-03 08:07:12+]
 (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 
 libraryhttp://hackage.haskell.org/package/lens at the Haskell 
 Exchangehttp://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
 librar...@haskell.org
 http://www.haskell.org/mailman/listinfo/libraries

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe



signature.asc
Description: Digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Using lenses

2013-10-03 Thread Mike Ledger
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 simo...@microsoft.comwrote:

  (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

 ** **

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Using lenses

2013-10-03 Thread Sebastiaan Visser
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 simo...@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
 librar...@haskell.org
 http://www.haskell.org/mailman/listinfo/libraries
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Using lenses

2013-10-03 Thread Niklas Haas
Another great use of lenses is the lens-aeson library (not to be
confused with aeson-lens). It's technically based around prisms, though,
so it's outside the scope of your talk; but you may wish to at least
reference it - it makes working with JSON really elegant!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Using lenses

2013-10-03 Thread Gergely Risko
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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Using lenses

2013-10-03 Thread Tony Morris
Lenses for nested sum types e.g. Either.
 On 03/10/2013 6:08 PM, Simon Peyton-Jones simo...@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 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

 ** **

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Using lenses

2013-10-03 Thread Niklas Haas
On Thu, 3 Oct 2013 22:06:22 +1000, Tony Morris tmor...@tmorris.net wrote:
 Lenses for nested sum types e.g. Either.

I think those would be leaning more in the direction of prisms.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Using lenses

2013-10-03 Thread Christopher Done
On 3 October 2013 10:57, Roman Cheplyaka r...@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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Using lenses

2013-10-03 Thread AntC
  Lenses for nested ... types ...
 

Hi Simon/Edward/all,

The most compelling uses I've seen for lenses is back to Benjamin Pierce's 
[et al] papers on Updatable Views. I think this is where the 'theory' 
started(?), although similar ideas had kicked around the relational 
database world for some time.

So, a trivial example of that would be treating co-ordinates as 
simultaneously polar and cartesian. This connects to Wadlers paper on 
Views, which became Haskell's view patterns.

I guess, though, that in a short talk, Simon won't have time to dig into 
all that history.

I see that many of the suggestions in response to Simon have talked about 
deeply nested structures. (Including for JSON and XML so-called databases.)

Now certainly there are applications for nested data, where the topic 
involves hierarchies. I can see that for AST's and (E)DSL's, organic 
molecules, exploring a game tree, ... nesting is the natural structure.

Given that you have a nested structure, lenses are a really neat approach. 
I'm going to offer a contrary view: lenses are a solution to a problem 
that never should have happened. A problem I'd characterise 
as 'Unnecessary Complexity' [per Fred Brooks].

The data processing industry abandon hierarchical databases in the '80's, 
because the relational model is so superior. In ten years time, I expect 
that XML-socalled databases will be regarded as a similar aberration.

One of the reasons is redundancy: XML so-called databases repeat field 
content all over the place. And that's going to give update anomalies: 
change some of those places, but fail to change all of them.

Now formally, hierarchical and relational data models can be made 
isomorphic. So I'm not criticising the ability to capture data. I am 
criticising the ease of access (for fetch and update). You end up with 
your code for the 'business logic' getting muddled in with the code for 
navigating the hierarchy, making both very brittle to maintain. Given that 
nested data gives you (especially) an update headache, lenses help you.

But a better solution is to do the data analysis up front, apply standard 
normalisation techniques, and deal with 'flat' data models.

And for flat data models, I don't see lenses having any advantages over 
old-fashioned records. (But! We do need to solve the Field names problem. 
It's great that Adam's GSOC work has incorporated lenses.)

AntC

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe