[mercury-users] Unique mode questions

Robert Jeschofnik rejj at cs.mu.OZ.AU
Fri Apr 20 13:57:06 AEST 2001


On 19-Apr-2001, Terrence Brannon <princepawn at earthlink.net> wrote:
> In looking at this section of the manual I developed the following
> questions:
> 
> 1- The manual states:
> 
>  Mercury also provides "unique" insts `unique' and `unique(...)' which
>  are like `ground' and `bound(...)' respectively, except that they
>  carry the additional constraint that there can only be one reference
>  to the corresponding value.
> 
> So my question is, what is meant by one reference to a value? Does
> that mean I cannot access the memory contents more than once? For
> example, here is a predicate with a unique-out variable:
> 
> :- pred string__length(string, int).
> :- mode string__length(in, uo) is det.
> 
> it is wrong for me to do the following:
> 
> string__length(S,L), use_it_once(L), use_it_again(L).

This would be fine if you had the following definitions:

:- mode use_it_once(ui) is <whatever>.
:- mode use_it_again(ui) is <whatever>.

and then ensured that the code for these predicates was mode-correct
:)

Doing so would involve that L is never unified with a term of inst
ground, though. (or bound(..)) ... which usually requires a deep-copy,
which is rather expensive


> 
> 2- Continuing, the manual states:
> 
>  There is also an inst `dead' which means that there are no references
>  to the corresponding value, so the compiler is free to generate code
>  that reuses that value. 
> 
> Now the obvious question is: why would a dead value exist in the first
> place?! And the neophyte's intuitive explanation would be:
> 
> :- pred list__length_2(list(T), int, int).
> :- mode list__length_2(in, in, out) is det.
> 
> list__length_2([], N, N).
> list__length_2([_ | L1], N0, N) :-
> 	N1 is N0 + 1,
> 	list__length_2(L1, N1, N).
> 
> 
> ... the _ above is a case where the mode could have been dead but
> unfortunately the tail of the list was needed. So then the question
> remains, why pass something that isn't going to be used? It doesn't
> make much sense to have a mode like this in my eyes.

"dead" is an inst, not a mode. The "dead" instantiation states that
this variable can never be referenced again.

Modes are defined in terms of insts. eg, the mode "out" is shorthand
for "free->ground", meaning that on calling this predicate, that
argument was free, and on exiting it is ground. "in" is
"ground->ground".

"dead" is used in the mode "di" (destructive input), which is
"unique->dead" - you pass in a unique reference, and upon exiting this
predicate, you may never use that reference again.


I hope this helps clear things up a little. I've been awake for over
24hours now, so I apologise if this was confusing and unhelpful :)


rob
--------------------------------------------------------------------------
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