Is there any way to parametrize a value update using record syntax?

Hi all, i have a record with a lot of items used in a state monad. data BigData = BigData { data1 :: X , data2 :: X -- and so on } type MonadicEnv a = State BigData a I updates the fields in the computation with such functions: 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? Thanks, Árpád

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 allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

On 07.09.2011 00:56, Brandon Allbery wrote:
2011/9/6 Poprádi Árpád
mailto:popradi_arpad@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.

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-Lab...

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.

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-Lab...
participants (5)
-
Alexey Khudyakov
-
Brandon Allbery
-
David Barbour
-
Erik Hesselink
-
Poprádi Árpád