[clean-list] unique record fields (mk II)

John van Groningen johnvg@cs.kun.nl
Thu, 7 Jun 2001 12:00:34 +0200


Ilya Shpitser wrote:
>  Thank you everyone for replying, the bit about partially applied functions
>  inheriting the uniqueness attribute from their arguments makes sense.
>  I have another question (really basic this time):
>  What is a Clean idiom for destructive updates of fields deep in some tree-like
>  data structures.
>  The example I am trying is updating an array element of an array that's a
>  field of some record with some function of that array element.
>  I first tried something like:
>
>  { record & array.[index] = function record.array.[index] }
>
>  This doesn't work, I am guessing because there are two references to the
>  array in this expression.

Yes, this will only work if there are no array accesses in the selection,
but only selections of record fields.

>  After reading the language report for a while I tried:
>
>  { record & array.[index] = function (fst (fst record!array)![index]) }
>
>  This didn't work either, I am guessing for the same reason (though
>  references are not named in this case).

This should work (if the value is not unique):
  # (value,record) = record!array.[index]
  = { record & array.[index] = function value }

Or you could use observation typing:
  #! value = record!array.[index]
  = { record & array.[index] = function value }

>..
> Also, how would
>  this work in general when the unique field is really deep in the data
>  structure, and has to be updated to some function of itself?

If the fields is inside a record (or a record of a record) you should
select the field using pattern matching, for example:

f record=:{field}
  = {record & field=function field }

or:

f record=:{field1={field2}}
  = {record & field1.field2 = function field2 }

Martin has already explained how to update a unique array element.

Regards,
John van Groningen