[mercury-users] Destructive list operations

Mark Brown mark at csse.unimelb.edu.au
Wed Jan 7 12:27:40 AEDT 2009

Hi Michael,

On 06-Jan-2009, Michael Day <mikeday at yeslogic.com> wrote:
> Hi Mark,
>> It is indeed possible to do this in Mercury using the store module.  Below
>> are two functions that destructively update the head/tail of a non-empty
>> list, re-using the cons cell of the original.  These could be used to
>> implement destructively updating versions of list.append and list.map,
>> among other things.
> That's clever, but is it an improvement on the equivalent C procs?

In some respects.  It's portable, for one thing, so it (or predicates based
on it) would be suitable for the standard library.  It's also readily
applicable to other Mercury types, not just lists, which is why I wanted to
post the example here.

> The use of unsafe_promise_unique detracts somewhat from the elegance of 
> keeping all the code in Mercury.

Yes it does, a bit.  The difficulty is that the code makes use of ground
references and there is no way for the compiler to check that we didn't
duplicate any subterms via these references, which would mean that the term
is no longer unique all the way down.  So we need to promise that it is in
order to satisfy the final inst.  (It is easy to verify that this code does
not duplicate any subterms.)

Fortunately, it doesn't detract from the portability at all.


mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au

More information about the users mailing list