Re: updating labelled fields
I just fudgeted around in the ghc source code and this doesn't seem to be a change that would require a lot of work. Briefly, the changes that would need to be made would be: 1) In the parser, change bind so that in addition to qname '=' exp you can also have qname 'somefunkysymbol' exp And change the return type from (a,b,Bool) to (a,b, Either () Bool) and then for the normal case, returh Right False and for the update case return Left (). 2) in the mkRecConstrOrUpdate function, when you have a conid applied to an update, make sure there are no Left () guys in the list; otoh, if the guy sitting in from of the { is an exp, do a map (mkRecUpdate exp) on the list where this function is something like: mkRecUpdate exp (a,b,Right c) = (a,b,c) mkRecUpdate exp (a,b,Left ()) = (a,HsApp b (HsApp (HsVar a) exp),False) -- warning, this isn't 100% correct, but it's the basic idea 3) recompile ghc I wouldn't at all mind making this addition if I had a sense that it would actually be accepted and that people weren't going to go crazy over the syntax. Would something like "<-" be preferred or something like "$=>"? - Hal -- Hal Daume III "Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume On Tue, 7 May 2002, Bryn Humberstone wrote:
Hi Hal,
2) update values in the structure, as in: let myData' = myData { foo = (foo myData)+1 }
I do this a lot too, and think it would be lovely to have some sugar for it. My original idea for the syntax was something like myData = myData { foo $=> (+1), bar $=> (*2) } just because $ is a bit reminiscent of function application. But I'm not really fussed; as long as some syntax for it appeared in the language.
-- Bryn
-- Bryn Humberstone + Email bdh@alphalink.com.au + Web http://bryn.alphalink.com.au/
On Mon, May 06, 2002, Hal Daume III wrote:
I wouldn't at all mind making this addition if I had a sense that it would actually be accepted and that people weren't going to go crazy over the syntax. Would something like "<-" be preferred or something like "$=>"?
- Hal
Why not $= ?
On Monday 06 May 2002 23:28, Hal Daume III wrote:
I wouldn't at all mind making this addition if I had a sense that it would actually be accepted and that people weren't going to go crazy over the syntax. Would something like "<-" be preferred or something like "$=>"?
I'd still prefer having some way to automaticly derive 'apply' functions. There is already nice syntax for setting a field value and I always end up defining 'set' functions to each and every field because I want to pass them as arguments. Imagine you have an STRef to a labeled datatype, lets call it "stdata". You want to apply some function "g" to field "foo" of that structure.
modifySTRef (fooAp g) stdata
Changing its value to "x"
modifySTRef (fooSet x) stdata
With syntatic sugar only you'd have to read the reference, apply the function to the field and then update it. IMO, 'set field' and 'apply to field' functions are as usefull as the 'field projection' functions that are derived from the definition of the labeled datatype. Anyway I agree that it would be nice to have special syntax for updates. I'll use it if I have it available. On Monday 06 May 2002 23:42, David Feuer wrote:
Why not $= ? Yeap very nice in deed. I'd vote for this one.
J.A.
participants (3)
-
David Feuer -
Hal Daume III -
Jorge Adriano