Its a pity template haskell cannot define infix operators, but you could use TH like:
$update rec field fn
which would expand to:
rec { field = fn (rec field) }
That doesn't help you so much if you want to update more than one field at a time. I think the best case syntax in this spirit is to define:
x // f = f x infixl 6 // $(a field val) => \rec -> rec {field = val} $(u field fn) => \rec -> rec {field = fn (field rec)}
Which we would then be able to use like this:
fun rec = rec // $(u field1 fn) . $(a field2 val)
But, I'm not sure that is superior to having the user explicitly derive u_ and a_ functions on the rectype which looks like this:
$(deriveUpdate RecType)
fun rec = rec // u_field1 fn . a_field2 val
Aside: Why doesn't TH allow infix? Haskell doesn't allow operators that start with ':' so it would seem natural for TH to use those for infix. Then we could have e.g. (:=) and (:\=) and end up with a syntax that looks like this:
rec // field :\= fn . field2 := val
And that is BETTER than the Haskell syntax that I originally proposed. Are TH infix operators on the agenda?
-Alex-
______________________________________________________________ S. Alexander Jacobson tel:917-770-6565 http://alexjacobson.com
Keean. S. Alexander Jacobson wrote:
I don't know what it takes to get this sort of change into circulation, but I assume it starts with a clear and specific description of the problem, exploration of alternatives, and a specific proposed solution. So here goes:
---------- Proposal: Allow "\=" for field update in record update syntax
Specifically, intepret
rec {field\=fn}
as
rec {field = fn (field rec)}
Jusitification:
It is extremely common when updating records to be updating a field value based on its prior value rather than simply assigning a new value from nothing. Unforunately the syntax supports the later clearly but makes the former awkward to read, understand, and debug:
myFun db myType1 = db { tMyType1 = insert myType1 $ tMyType1 db nextId = 1 + (nextId db) }
One solution to this problem that does not involve changing syntax is to generate field update functions for every field of a type e.g.:
update_tMyType1 f s = s {tMyType1= f $ myType1 s} update_nextId f s = s {nextId = f $ nextId s}
Once you have those functions, myFun looks much better:
myFun db myType = update_nextId (+1) $ update_tMyType1 f db where f= insert myType1
But, generating the update_ functions is not programming; its just manual syntax generation. And, even if you have template haskell to do it,
* it is annoying to have to find/write the TH code to implement * update_ is incredibly verbose * shortening it risks proliferation of psuedo-syntax: u_tMyType, u__tMyType, or utMyType? * it adds clutter to have to call the TH for each record type, and * it pollutes the name space/increases risk of name collision
Rather than force the user to jump through these hoops, why not make the syntax flow naturally from the existing record update syntax? It makes sense to use \= for this task as it is the functional generalization of C or Java's += or *= . Then myFun would have the elegant and clear syntax:
myFun db myType1 =db {tMyType1\=insert myType1, nextId\=(+1)}
-Alex-
______________________________________________________________ S. Alexander Jacobson tel:917-770-6565 http://alexjacobson.com _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell