[m-dev.] Is this a bug?
Peter Schachte
schachte at csse.unimelb.edu.au
Mon Jun 18 14:28:23 AEST 2007
Ralph Becket wrote:
> Ralph Becket, Monday, 18 June 2007:
>> I'm trying to use field update syntax to update a map of maps, but it
>> isn't working:
>>
>> ::: map_map_update_test.m :::
>>
>> :- module map_map_update_test.
>> :- interface.
>> :- import_module io.
>>
>> :- pred main(io::di, io::uo) is det.
>>
>> :- implementation.
>> :- import_module map.
>>
>> main(!IO) :-
>> A0 = map.init,
>> A = A0 ^ elem(42) ^ elem(99) := 123,
>> io.write_int(A ^ det_elem(42) ^ det_elem(99), !IO),
>> io.nl(!IO).
>>
>> ::: end of map_map_update_test.m :::
>
> Ah, I've understood the problem:
>
> A = A0 ^ elem(42) ^ elem(99) := 123
>
> expands according to the following rule in the reference manual
>
> transform(Term ^ Field(Arg1, ...) := FieldValue) =
> 'Field :='(Arg1, ..., Term, FieldValue)).
>
> transform(Term0 ^ Field(Arg1, ...) ^ Rest := FieldValue) = Term :-
> OldFieldValue = Field(Arg1, ..., Term0),
> NewFieldValue = transform(OldFieldValue ^ Rest := FieldValue),
> Term = 'Field :='(Arg1, ..., Term0, NewFieldValue).
>
> The OldFieldValue uses the `elem' field access function which is
> semidet.
>
> This is somewhat counterintuitive.
Yes, and a bit annoying to work around. Just switching to det_elem() won't
help. (BTW: why is the semidet version the default (the one with the
convenient name)? Didn't we decide a while back that semidet functions were a
bad idea?)
The only solution I can think of would be to use a variation on maps that have
a default value that is returned when looking up an unmapped key. Elem() would
be det for that type, so you could use it for the outer map, with default value
map.init.
--
Peter Schachte One thing's for sure: if we keep doing what
schachte at cs.mu.OZ.AU we're doing, we're going to keep getting what
www.cs.mu.oz.au/~schachte/ we're getting.
Phone: +61 3 8344 1338 -- Stephen Covey, et al.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions: mercury-developers-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the developers
mailing list