[mercury-users] mercury and determinism
fjh at cs.mu.oz.au
Sun Feb 23 15:46:53 AEDT 1997
> Any particular reason for the following:
> q(X, Y):-
> ( X = a -> Y=b(0);
> X = b(N), N1 is N + 5, Y = b(N1)
> little.m:007: In `q(in, out)':
> little.m:007: error: determinism declaration not satisfied.
> little.m:007: Declared `det', inferred `semidet'.
> little.m:021: Unification of `X' and `b(N)' can fail.
> Because I'd like to reuse my code in Prolog, I prefer the definition of
> q above the one of p. There could be some serious efficiency-problems
> in some Prolog-implementations with the first one.
There are a number of alternatives you can use.
Tom already mentioned one of them,
q(b(N), b(N1)) :-
N1 is N + 5.
but there are a couple of others.
One of them is that you can just add a call to error/1:
( X = a -> Y = b(0)
; X = b(N) -> N1 is N + 5, Y = b(N1)
:- pred not_reached is erroneous.
not_reached :- error("reached not_reached!_).
This has the disadvantage that code will be a slightly less
efficient in Mercury.
Another alternative is to use a cut.
( X = a, !, Y=b(0)
; X = b(N), N1 is N + 5, Y = b(N1)
Mercury doesn't have cuts, but to make it easier to migrate code from
Prolog, there's a predicate '!'/0 which does nothing.
This technique also has a slight efficiency cost in Mercury.
However, that cost will be eliminated if you compile with intermodule
optimization, since the call to '!'/0 will then be inlined and
optimized away. This is the technique that I would recommend
for situations where Tom's suggestion is cumbersome.
Thomas Conway wrote:
> I'll put it onto the todo list, but
> I don't expect anything to happen in the near future.
Then again, I think it's not very difficult to implement, so you might
We just need to think carefully about such things before changing the
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh at 188.8.131.52 | -- the last words of T. S. Garp.
More information about the users