[mercury-users] Deterministic on not?

Fergus Henderson fjh at cs.mu.OZ.AU
Sat Apr 17 13:46:11 AEST 2004

On 16-Apr-2004, Sergio Rafael Trindade Marques <l16447 at alunos.uevora.pt> wrote:
> Can i call a function declared semidet in other that is det?

Yes.  Just enclose the call in an if-then-else.
For example:

	:- func foo(int) = int is semidet.

	:- func bar(int) = int is det.
	bar(X) = (if some [Y] foo(X) = Y then Y else 42).

This can be read as "bar(X) is equal to the following:
if there is some Y for which foo(X) = Y, then that Y, otherwise 42."
The "some [Y]" part is optional and can be omitted if you prefer.

Note that you need to supply a default value to use in case the
function that you called fails -- in this case 42.

If you are calling the semidet function in a way such that you
are sure it will never fail, then you can call error/1 or func_error/1:

	bar(X) = (if foo(X) = Y then Y else func_error("foo failed")).

This way avoids the need to provide a default value, but can of course
lead to run-time exceptions if the semidet function does in fact fail.

Fergus Henderson                    |  "I have always known that the pursuit
                                    |  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