[Haskell] Proposal: Allow "\=" for field update in record update syntax

S. Alexander Jacobson alex at alexjacobson.com
Thu Feb 17 10:52:13 EST 2005

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}


    rec {field = fn (field rec)}


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)}


S. Alexander Jacobson tel:917-770-6565 http://alexjacobson.com

More information about the Haskell mailing list