[mercury-users] Fact tables

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Jul 18 13:43:46 AEST 2003


On 18-Jul-2003, Michael Day <mikeday at yeslogic.com> wrote:
> 
> To represent a relation that maps some ints to some other ints, which
> would be the best Mercury implementation:

Best in what way?  Do you mean most efficient? 
Or most readable/maintainable?

> :- pred encoding(int, int).
> :- mode encoding(in, out) is semidet.

That is a good solution.

> :- pred encoding(int, maybe(int)).
> :- mode encoding(in, out) is det.

That is not a particularly good solution.
Firstly, from a style perspective, it would be
better to use a function.  Secondly, as far as
efficiency goes, this will require a heap allocation
for every yes(_) answer, which the other alternatives don't.

> :- pred encoding(int, int).		% returns -1 on failure (ugh)
> :- mode encoding(in, out) is det.

That is likely to be only very marginally more efficient than the first
solution, and stylistically it's better to use a semidet predicate rather
than a sentinel value such as -1.

If you're really going for maximum efficiency above style,
then it's better to use a function:

	:- func encoding(int) = int.	% returns -1 on failure (ugh)

The reason is that for the MLDS based (--high-level-code) back-ends,
a det function's return value will be returned in a register, whereas
an output argument will be passed by reference, which is slightly
less efficient.  (For the original LLDS based back-end, it will make
no difference.)

So, in ordinary circumstances, I'd suggest using the semidet pred.
If your application is performance-critical, and you know that this
procedure will be called in the innermost loop of your application,
then consider using the det function with sentinel value.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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