[m-users.] Question about multiple modes.
strldr2 at yahoo.com
Wed Aug 12 02:23:50 AEST 2015
Hi all,Being new to Mercury, I need help with the following problem:
I want to create a simple predicate mult(A, B, C), such that C = A * B, and by substituting any 2 of (A, B, C) with values getting the third as a result. Something like a primitive equation solver. The predicate must fail if result is impossible (division by zero, like A * 0 = 42) or if result can be any number (like A * 0 = 0). So my mult() will be:
:- pred mult(float, float, float).:- mode mult(in, in, out) is det.:- mode mult(in, out, in) is semidet.:- mode mult(out, in, in) is semidet.
The problem is writing the body. There is no way to tell within the body which mode we currently describe, to add the corresponding semantics. Ultimately, the compiler will create a separate C function for each mode, based on a single body that unambiguously describes ALL modes at once.Am I correct?
As a side note, the same problem for addition would solved easily (compiles and runs as expected, but only for + and ints -- floats would give error):
:- pred add(int, int, int).:- mode add(in, in, out) is det.:- mode add(in, out, in) is det.:- mode add(out, in, in) is det.add(A,B,C) :- C = A + B.
Is there any way to achieve the same but for multiplication?How would one approach this problem, without writing separate predicates like mult1, mult2, mult3? I have read the introduction book (pdf from site) and haven't got any hints regarding this.Any help is greatly appreciated.Thank you in advance!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the users