Hi, I often create structures like: data MyData = MyData { foo :: ..., bar :: ..., .... } and most of the time i do one of two things: 1) read values from the structure, as in: let x = (foo myData) in ... 2) update values in the structure, as in: let myData' = myData { foo = (foo myData)+1 } Only very rarely (usually only during intializization) do I actually put values into the structure that *don't* depend on their previous value. I end up with expresions like: ... myData { foo = (foo myData) + 1 ; bar = (bar myData) ++ "bar" ; ick = (ick myData) ! n ; ... } I was wondering if there existed any sort of "update" syntax. Obviously not real update, but enough to get rid of the "(foo myData)" parts of my epxression which really serve to just clutter up with expression. Perhaps something like: ... myData { foo <- (+1) ; bar <- (++"bar") ; ick <- (!n) ; ... } or the like, where "x { ... y <- e ... } is translated to "x { ... y = e (y x) ... }" (i only use "<-" because that seems to be the default extension symbol, i guess because we don't want to trample symbols people might actually use.) Anyway, does such a thing exist, and, if not, is there any chance it could exist, or is it just syntactic salt to too many people? :) - Hal -- Hal Daume III "Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume
I often create structures like: data MyData = MyData { foo :: ..., bar :: ..., .... } That makes 2 of us :-)
and most of the time i do one of two things: 1) read values from the structure, as in: let x = (foo myData) in ... 2) update values in the structure, as in: let myData' = myData { foo = (foo myData)+1 }
1) I've used datatypes with labeled fields mostly to pass around implicit values. If that is your case then there is a way around it. Declare the datatype as
data MyData = MyData { foo_ :: fooType, bar_ :: ..., .... }
and then declare
foo :: (?implicitdata :: MyData)=> fooType foo = foo_ ?yourdata
So when you work in a contex that depends on some implicit data you can just use foo. I've used this *a lot* lately. 2) Yes. My method now is declaring set and apply functions to every field of my data structure. fooAp f ni=ni{foo=f(foo ni)} fooSet x = fooAp (const x)
Only very rarely (usually only during intializization) do I actually put values into the structure that *don't* depend on their previous value. I end up with expresions like:
... myData { foo = (foo myData) + 1 ; bar = (bar myData) ++ "bar" ; ick = (ick myData) ! n ; ... }
Yeap quite ugly isn't it? :-)
I was wondering if there existed any sort of "update" syntax. Obviously Nope, not that I know of.
not real update, but enough to get rid of the "(foo myData)" parts of my epxression which really serve to just clutter up with expression. Perhaps something like:
... myData { foo <- (+1) ; bar <- (++"bar") ; ick <- (!n) ; ... }
Yes looks nice, thought about something like that before too.
or the like, where "x { ... y <- e ... } is translated to "x { ... y = e (y x) ... }" (i only use "<-" because that seems to be the default extension symbol, i guess because we don't want to trample symbols people might actually use.)
Anyway I'd prefer to have some way to 'derive' apply and set functions. Something like
data MyData = MyData { foo :: fooType, bar :: ..., .... } deriving (Set, Apply)
Using the keyword "deriving" would probably be a bad idea though :) The set and apply functions could be derived with a standard postfix or maybe prefix... fooAp or apFoo. Maybe we could introduce sintax to specify it...
deriving (Set with "set", Apply with "ap")
I don't know... I'm just brainstorming right now. Having actual functions is important. I don't think I have to explain why to people in this mailing list :-)
Anyway, does such a thing exist, and, if not, is there any chance it could exist, or is it just syntactic salt to too many people? :) I whish you better luck than I've had so far whenever making posts about this same issue ;)
J.A.
DrIFT which i am now maintaining can derive such utility functions out of the box. just add a {-!deriving: update -} to get update functions for every labeled field in a datatype. quite useful, I have not updated the web page yet, but the new DrIFT homepage will be at http://homer.netmar.com/~john/computer/haskell/DrIFT/ On Mon, May 06, 2002 at 07:36:30PM +0100, Jorge Adriano wrote:
Anyway I'd prefer to have some way to 'derive' apply and set functions. Something like
data MyData = MyData { foo :: fooType, bar :: ..., .... } deriving (Set, Apply)
Using the keyword "deriving" would probably be a bad idea though :) The set and apply functions could be derived with a standard postfix or maybe prefix... fooAp or apFoo. Maybe we could introduce sintax to specify it...
deriving (Set with "set", Apply with "ap")
I don't know... I'm just brainstorming right now. Having actual functions is important. I don't think I have to explain why to people in this mailing list :-)
Anyway, does such a thing exist, and, if not, is there any chance it could exist, or is it just syntactic salt to too many people? :) I whish you better luck than I've had so far whenever making posts about this same issue ;)
-- --------------------------------------------------------------------------- John Meacham - California Institute of Technology, Alum. - john@foo.net ---------------------------------------------------------------------------
On Tuesday 07 May 2002 02:07, John Meacham wrote:
DrIFT which i am now maintaining can derive such utility functions out of the box. just add a {-!deriving: update -} to get update functions for every labeled field in a datatype. quite useful, I have not updated the web page yet, but the new DrIFT homepage will be at
Very nice :) I'll check it out. J.A.
participants (3)
-
Hal Daume III -
John Meacham -
Jorge Adriano