[m-dev.] Syntactic sugar for functor matching

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Oct 31 17:06:06 AEDT 2001


On 31-Oct-2001, Ralph Becket <rafe at cs.mu.OZ.AU> wrote:
> Occasionally one comes across code like this:
> 
> p(constructor1(_, _, _), ...)             :- ...
> p(constructor2(_, _, _, _, _), ...)       :- ...
> p(constructor3(_, _, _, _), ...)          :- ...
> p(constructor4(_, _, _, _, _, _, _), ...) :- ...
> 
> Which looks rather awkward.  Would there be any merit in extending
> the language so that one could write
> 
> p(constructor1/3, ...) :- ...
> p(constructor2/5, ...) :- ...
> p(constructor3/4, ...) :- ...
> p(constructor4/7, ...) :- ...

The big disadvantage of the `constructor(_, _, _)' syntax is that it
hard-codes the arity, and thus the code has to be changed whenever you
add a new argument.

The proposed extension here would be slightly more concise, but would
still hard-code the arity, so you'd have the same advantage.

If we were going to add a new syntax extension here,
I'd much prefer a syntax such as `constructor(...)'
or `constructor(___)' or something like that which
didn't hard-code the arity.

`constructor(...)' would be nice, but it's preferable to
avoid using `...' in the language syntax, since it makes
it really hard to include example programs in papers, email, etc.
since it becomes unclear whether `...' is program syntax
or the usual meta-symbol indicating elision.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  | "... it seems to me that 15 years of
The University of Melbourne         | email is plenty for one lifetime."
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- Prof. Donald E. Knuth
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list