[mercury-users] functions returning in values?

Ralph Becket rbeck at microsoft.com
Tue May 8 21:44:03 AEST 2001

> From: Terrence Brannon [mailto:princepawn at earthlink.net]
> Sent: 08 May 2001 03:37
> However, I was meandering through the Mercury source files and
> somewhere came across something like this:
> :- func g(int) = int.
> :- mode g(out) = in.

If you look at the float library, say, you will see mode collections
for the basic arithmetic operations that allow you to compute the
third argument of X = Y @ Z (for @ in {+,-,*./}) given any two

This can be quite handy.  For example,

:- pred fahrenheit(float) = float.
:- mode fahrenheit(in) = out is det.
:- mode fahrenheit(out) = in is det.

fahrenheit(Centigrade) = Fahrenheit :-
	Fahrenheit = 32.0 + (9.0/5.0 * Centigrade).

Now, F = fahrenheit(100.0) has the solution F = 212.0 while
212.0 = fahrenheit(C) has the solution C = 100.0.

Interestingly Prolog's is/2 doesn't let you do this sort of thing.

An interesting application of this stuff is in cryptography: you only
have to describe the cipher function in Mercury and supply the
modes and Mercury will give you both enciphering and deciphering,
the likelihood of you introducing errors in your code.

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