[mercury-users] Field updates and state variables
Peter Schachte
schachte at csse.unimelb.edu.au
Mon Jul 3 12:26:08 AEST 2006
On Sun, Jul 02, 2006 at 12:28:06PM -0700, Peter Hawkins wrote:
> Hi...
>
> I find myself writing code like this a lot:
> ...
> !:S = !.S ^ my_map := map.insert(!.S ^ my_map, Key, Value)
> ...
>
> Wouldn't it be nice if I could write the field update as:
> map.insert(Key, Value, !S ^ my_map)
That seems pretty magical; the former is not what I would naturally
expect the latter to expand to.
I think it would be nicer to use the elem getter/setter for maps,
writing
!.S ^ my_map ^ elem(Key)
(as an expression) to lookup a key in a map, and
!:S = !.S ^ my_map ^ elem(Key) := Value
(as a goal) to update a map. In fact, it wouldn't be hard to write
your own elem/2 getter and 'elem :='/3 setter functions for your
my_map type that directly operate on the map, to get rid of the
annoying "^ my_map" part, so you would write
!.S ^ elem(Key)
(as an expression) to lookup a key in a map, and
!:S = !.S ^ elem(Key) := Value
(as a goal) to update a map. See section 3.4.3 in the Mercury
reference manual.
What would be *really* useful would be to integrate state variable and
setter syntax, by translating
!Var ^ Acessors := Value
to !:Var = !.Var ^ Accessors := Value. Maybe mercury already does
that, but I couldn't find it in the reference manual. Then you could
just write
!S ^ elem(Key) := Value
That reads better to me than map.insert(Key, Value, !S ^ my_map).
--
Peter Schachte Beware of bugs in the above code; I have only
schachte at cs.mu.OZ.AU proved it correct, not tried it.
www.cs.mu.oz.au/~schachte/ -- Donald Knuth
Phone: +61 3 8344 1338
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list