[mercury-users] The great mercury tutorial

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Apr 22 02:29:54 AEST 1999

On 21-Apr-1999, Robert Bossy <bossy at ccr.jussieu.fr> wrote:
> Bt the way, I can't remember where I saw this declaration:
> :- type foo(T) ---> T(T).
> What does the compiler understands?

Really that ought to be a compile error.

In the long term, we might want to allow type parameters and/or typeclass
parameters to be type constructors rather than just types.  Then if `TC'
was a variable standing for a type constructor, then `TC(T)' would denote
the result of applying that type constructor to the type `T'.

Currently, however, we don't support that kind of thing.
The current implementation will accepted a declaration of the above form,
but this is only due to an accident of syntax, and it probably
won't do what you expect.  Please do not make use of that syntax.


Now, in case you were wondering what that accident of syntax was,
I will describe it in detail...

Mercury extends Prolog-style terms with the higher-order syntax "Var(Args)".
But we wanted to do this without requiring drastic changes to the
representation of terms.  So to accomplish this, the higher-order syntax
"Var(Args)" gets parsed as the term "''(Var, Args)".
That's a term with '' (the null atom) as its functor and with two arguments,
namely "Var" and "Args".

This syntax is used for higher-order predicate and function calls.
If "Var" is a higher-order predicate variable, then "''(Var, Args)"
means the same as "call(Var, Args)".  If Var is a higher-order function
variable, then it means the same as "apply(Var, Args)".  But if, as in
this case, Var is a type, the language (as currently defined) does not
treat a term with a null functor as anything special, so it just means
"''(Var, Args)".  So the above declaration is equivalent to

	:- type foo(T) ---> ''(T, T).

The semantics of that declaration are similar to the semantics of

	:- type foo(T) ---> pair(T, T).

except that the name of the constructor is ''/2 rather than pair/2.

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        |     -- the last words of T. S. Garp.

More information about the users mailing list