
On Tue, Apr 22, 2008 at 4:12 AM, Evan Laforge
Has there been any work on improving update syntax in haskell? Possibly some improvement could be made with a typeclass or two and a few custom operators, to unify some of the disparate syntax. Maybe more improvement could be made with some TH hackery. A better record update syntax I'm sure could improve things even more. Or maybe there's a way to structure existing code to improve the above?
Here are two little TH functions I find useful: -- \f x -> x { field = f (field x) } alter :: Name -> Q Exp alter field = do f <- newName "f" x <- newName "x" lamE [varP f, varP x] $ recUpdE (varE x) [return (field, AppE (VarE f) (AppE (VarE field) (VarE x)))] -- \a x -> x { field = a } set :: Name -> Q Exp set field = do a <- newName "a" x <- newName "x" lamE [varP a, varP x] $ recUpdE (varE x) [return (field, VarE a)] They're not as flexible as FRefs (though they could be helpful in manually defining FRefs), but they still solve some of my frustrations with record update syntax. Usage: $(set 'fieldname) value record -- sets the value of the "fieldname" field to "value" $(alter 'fieldname) f record -- produces a new record where "fieldname" has been transformed by f Stuart