[mercury-users] higher order syntax

Fergus Henderson fjh at cs.mu.OZ.AU
Sat Feb 12 20:02:18 AEDT 2000


On 11-Feb-2000, Ralph Becket <rbeck at microsoft.com> wrote:
> [Peter Schachte wrote:]
> > 	:- pred p(int).
> > 	:- func p = pred(int).
> > 
> > now what does the goal p(3) mean?  Do you apply the predicate returned
> > by the p/0 function to 3, or do you invoke the predicate p/1?  I guess
> > this is just the usual ambiguous overloading problem and can be
> > handled the same way, but somehow it seems more confusing.
> 
> Under my suggestion, p(3) is an unambiguous call to p/1, whereas (p)(3) 
> means `apply(p, 3)' or `call(p, 3)' as circumstances dictate; p can be
> any term that evaluates to a func or pred respectively.

Yes, that was how I interpreted your proposal too.

See below for the exact wording that I propose for the
language reference manual.  That wording says that
"A higher-order term", i.e. one which gets expanded
to ''(...), is "a term, other than a name or an operator term,
followed without any intervening whitespace by an open parenthesis".
If the thing before the open parenthesis is a name,
as is the case for `p(3)', then the term is just a "simple compound term",
not a "higher-order term", and so it gets parsed as just p(3),
not ''(p, 3).

So in this example, a goal `p(3)' would be unambiguously refer
to the predicate

	:- pred p(int).

But a goal `(p)(3)' would be ambiguous, since it could refer
to either of the two declarations for `p' in scope.

> So, p(X)(Y,Z) is shorthand for `apply(p(X), Y, Z)'.

Minor correction: it's actually shorthand for ''(p(X), Y, Z),
and if that occurs as a goal, rather than an expresion,
then it is the same as `call(p(X), Y, Z)', not `apply(p(X), Y, Z)'.


Anyway, I like Ralph Becket's proposal. I've documented it
and implemented it (it was quite easy to implement), and 
so unless I hear any objections, I plan to go ahead and commit
this change, which will mean that it will be included in our future
releases.

Thanks for the feedback.

Cheers,
	Fergus.

Enclosed below is a unified diff of my proposed changes
to the language reference manual, followed by the new
version (in case you find the unified diff hard to read).
Comments welcome.

Index: reference_manual.texi
===================================================================
RCS file: /home/mercury1/repository/mercury/doc/reference_manual.texi,v
retrieving revision 1.172
diff -u -d -u -r1.172 reference_manual.texi
--- reference_manual.texi	2000/02/11 05:04:05	1.172
+++ reference_manual.texi	2000/02/11 05:44:12
@@ -317,27 +317,34 @@
 A functor is an integer, a float, a string, a name, a compound term,
 or a higher-order term.
 
-A compound term is a name followed without any intervening
+A compound term is a simple compound term, an operator term,
+or a parenthesized term.
+
+A simple compound term is a name followed without any intervening
 whitespace by an open parenthesis (i.e. an open_ct token),
 a sequence of argument terms separated by commas, and a close
-parenthesis.  Compound terms may also be specified using
-operator notation, as in Prolog.
+parenthesis.  
 
-Operators can also be formed by enclosing an variable or name between grave
+An operator term is a term specified using operator notation, as in Prolog.
+Operators can also be formed by enclosing a variable or name between grave
 accents (backquotes).  Any variable or name may
 be used as an operator in this way.  If @var{fun} is a variable or name,
 then a term of the form @code{@var{X} `@var{fun}` @var{Y}} is equivalent to 
 @code{@var{fun}(@var{X}, @var{Y})}.  The operator is treated as having the
 highest precedence possible and is left associative.
 
+A parenthesized term is just an open parenthesis
+followed by a term and a close parenthesis.
+
-A higher-order term is a variable followed without any intervening
-whitespace by an open parenthesis (i.e. an open_ct token),
+A higher-order term is a term, other than a name or an operator term,
+followed without
+any intervening whitespace by an open parenthesis (i.e. an open_ct token),
 a sequence of argument terms separated by commas, and a close
-parenthesis.  A higher-order term is equivalent to a compound term
+parenthesis.  A higher-order term is equivalent to a simple compound term
 whose functor is the empty name, and whose arguments are the 
 the variable followed by the arguments of the higher-order term.
 That is, a term such as @code{Var(Arg1, @dots{}, ArgN)} is
 parsed as @code{''(Var, Arg1, @dots{}, ArgN)}.

-----------------------------------------------------------------------------
 
|A compound term is a simple compound term, an operator term,
|or a parenthesized term.
|
|A simple compound term is a name followed without any intervening
 whitespace by an open parenthesis (i.e. an open_ct token),
 a sequence of argument terms separated by commas, and a close
|parenthesis.  
 
|An operator term is a term specified using operator notation, as in Prolog.
|Operators can also be formed by enclosing a variable or name between grave
 accents (backquotes).  Any variable or name may
 be used as an operator in this way.  If @var{fun} is a variable or name,
 then a term of the form @code{@var{X} `@var{fun}` @var{Y}} is equivalent to 
 @code{@var{fun}(@var{X}, @var{Y})}.  The operator is treated as having the
 highest precedence possible and is left associative.
 
|A parenthesized term is just an open parenthesis
|followed by a term and a close parenthesis.
|
|A higher-order term is a term, other than a name or an operator term,
|followed without
|any intervening whitespace by an open parenthesis (i.e. an open_ct token),
 a sequence of argument terms separated by commas, and a close
|parenthesis.  A higher-order term is equivalent to a simple compound term
 whose functor is the empty name, and whose arguments are the 
 the variable followed by the arguments of the higher-order term.
 That is, a term such as @code{Var(Arg1, @dots{}, ArgN)} is
 parsed as @code{''(Var, Arg1, @dots{}, ArgN)}.
-- 
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 128.250.37.3        |     -- 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