Re: [Haskell-cafe] Is there any way to parametrize a value update using record syntax?

2011-09-06 Thread Brandon Allbery
2011/9/6 Poprádi Árpád 

> updateData1 :: X -> MonadicEnv()
> updateData1 d = do; env <- get; put env {data1 = d}
>

> updateData1 d = modify (\r -> r {data1 = d})

But there is, sadly, no eta-reduced version of record update to make the "\r
-> r ..." boilerplate go away; recognition of the syntax requires an
expression before the braces.  (Consider the ambiguity of the eta-reduced
expression "modify {data1 = d}".)  Also, and much more annoyingly, "data1"
must be constant.

If you can decipher the documentation, there are several alternative record
packages on Hackage based on functional lenses.  Also there are packages
which use Template Haskell to automate the above.

-- 
brandon s allbery  allber...@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Is there any way to parametrize a value update using record syntax?

2011-09-06 Thread Alexey Khudyakov

On 07.09.2011 00:56, Brandon Allbery wrote:

2011/9/6 Poprádi Árpád mailto:popradi_ar...@freemail.hu>>

updateData1 :: X -> MonadicEnv()
updateData1 d = do; env <- get; put env {data1 = d}


 > updateData1 d = modify (\r -> r {data1 = d})

But there is, sadly, no eta-reduced version of record update to make the
"\r -> r ..." boilerplate go away; recognition of the syntax requires an
expression before the braces.  (Consider the ambiguity of the
eta-reduced expression "modify {data1 = d}".)  Also, and much more
annoyingly, "data1" must be constant.


In principle ambiguity could be resolved with a slash:
  modyfy \{data = d}


But that's from land of hypothetical language extension and dog headed 
people.


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


Re: [Haskell-cafe] Is there any way to parametrize a value update using record syntax?

2011-09-06 Thread Erik Hesselink
2011/9/6 Poprádi Árpád :
> i have a record with a lot of items used in a state monad.
>
> data BigData = BigData {
>                   data1 :: X
>                 , data2 :: X
>                -- and so on
>                }
>
> updateData1 :: X -> MonadicEnv()
> updateData1 d = do; env <- get; put env {data1 = d}
>
> updateData2 :: X -> MonadicEnv()
> updateData2 d = do; env <- get; put env {data2 = d}
>
> But it's ugly. Always the same, only the record selector has another
> name.
> Is it possible to generalize it?

You can use the fclabels package [1] for this. It makes record labels
first class, and also provides functions to update parts of a record
in the state monad [2]. You would be able to write something like:

updateData1 = puts data1 d

It has a function for modifcation as well, which is even uglier with
regular record syntax.

Erik

[1] http://hackage.haskell.org/package/fclabels
[2] 
http://hackage.haskell.org/packages/archive/fclabels/1.0.4/doc/html/Data-Label-PureM.html#v:puts

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


Re: [Haskell-cafe] Is there any way to parametrize a value update using record syntax?

2011-09-06 Thread David Barbour
2011/9/6 Erik Hesselink 

> You can use the fclabels package [1] for this. It makes record labels
> first class, and also provides functions to update parts of a record
> in the state monad [2].


That's pretty nifty. Thanks for mentioning it.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Is there any way to parametrize a value update using record syntax?

2011-09-07 Thread Poprádi Árpád
Hi Erik,

thanks a lot!
fclabels is an amazing package!
My code become much clearer.

Greetings,
Árpád

On Wed, 2011-09-07 at 00:04 +0200, Erik Hesselink wrote:
> 2011/9/6 Poprádi Árpád :
> > i have a record with a lot of items used in a state monad.
> >
> > data BigData = BigData {
> >   data1 :: X
> > , data2 :: X
> >-- and so on
> >}
> >
> > updateData1 :: X -> MonadicEnv()
> > updateData1 d = do; env <- get; put env {data1 = d}
> >
> > updateData2 :: X -> MonadicEnv()
> > updateData2 d = do; env <- get; put env {data2 = d}
> >
> > But it's ugly. Always the same, only the record selector has another
> > name.
> > Is it possible to generalize it?
> 
> You can use the fclabels package [1] for this. It makes record labels
> first class, and also provides functions to update parts of a record
> in the state monad [2]. You would be able to write something like:
> 
> updateData1 = puts data1 d
> 
> It has a function for modifcation as well, which is even uglier with
> regular record syntax.
> 
> Erik
> 
> [1] http://hackage.haskell.org/package/fclabels
> [2] 
> http://hackage.haskell.org/packages/archive/fclabels/1.0.4/doc/html/Data-Label-PureM.html#v:puts
> 




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