[mercury-users] Mercury needs a Tutorial
Thomas Charles Conway
conway at hydra.cs.mu.oz.au
Tue Feb 16 08:39:19 AEDT 1999
On Mon, Feb 15, 1999 at 06:44:51PM EST, Bas de Bakker wrote:
> >>>>> "David" == David Powers <powers at ist.flinders.edu.au> writes:
>
> A few selected replies...
>
> David> Given that doing the factorial examples like this in Prolog
> David> would return the sum of the appropriate number of 1's, do I
> David> assume that = acts like Prolog's is and evaluates
> David> expressions.
>
> Not exactly, it's rather the other way around. You can use 'is' in
> Mercury, but it's just another spelling for '='. Expressions are
> always evaluated, e.g., you can write 'factorial(N1 - N2 + N3, F)'.
Well, not `always'. The operator `+' is declared as a function:
:- func (int + int) = int.
There is no reason at all why you couldn't build up an expression
(as you do in Prolog) and then have a function/predicate to evaluate
it:
:- import_module int.
:- type expr
---> num(int)
; (expr + expr)
; (expr - expr)
...
.
:- pred (int is expr).
:- mode (out is in) is det.
X is num(X).
(X + Y) is Ex + Ey :-
X is Ex,
Y is Ey.
(X - Y) is Ex - Ey :-
X is Ex,
Y is Ey.
...
Unfortunately, since Mercury doesn't have undiscriminated union
types, you need the num/1 wrapper around constants (besides,
with out the wrapper, code like the following would be ambiguous:
F = 1 + 2 + 3
is F an expr or an int?).
Thomas
--
Thomas Conway <conway at cs.mu.oz.au> )O+
To a killer whale, otters are like hairy popcorn -- Paul Dayton
More information about the users
mailing list