[m-dev.] Syntactic sugar for functor matching

Peter Schachte schachte at cs.mu.OZ.AU
Thu Nov 1 14:45:23 AEDT 2001


I mostly agree with Tom that Mercury syntax is a bit crufty, but that's not
going to stop me proposing another syntactic extension....


On Wed, Oct 31, 2001 at 05:06:06PM +1100, Fergus Henderson wrote:
> 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.

Agreed.  In fact, this problem is more general than Ralph states.  The code
might refer to some of the arguments of the constructors but leave most as
underscores.  It's still a nuissance writing (and reading) all those
underscores, and changing them when you add or remove arguments to the
constructors.

Solving the problem Fergus is referring to would mean Mercury would have to
forbid a type having two constructors with the same name (and different
arities).  At least as far as I can see allowing a syntax that doesn't
specify the arity but still uniquely determines a constructor seems to mean
that the functor name alone must determine the constructor.  But if you're
willing to accept that, here's a suggestion.

Modify the Mercury reader to accept terms like:

	con[name3=Foo, name1=whatever(Something)]

Here con would have to be a constructor with named arguments.  If, eg, con
has 4 arguments named, in order, name1, name2, name3, and name4, then the
compiler would interpret this as if you had written

	con(whatever(Something), _, Foo, _)

This fixes the problem of adding or removing arguments from a constructor,
as well as the problem of terms with heaps of underscore arguments (whether
or not all are underscores); Ralph's example could be written:

	p(constructor1[], ...) :- ...
	p(constructor2[], ...) :- ...
	p(constructor3[], ...) :- ...
	p(constructor4[], ...) :- ...

It also fixes the "is that 7 or 8 underscores before that X there?" and
"what was the 8th argument of that term supposed to be again?" visual
problems.  Finally, it's currently a syntax error, so adopting it is backward
compatible.  And it could be extended to named arguments for predicates and
functions, if people wanted that.

-- 
Peter Schachte              There is no statute of limitations on stupidity.
schachte at cs.mu.OZ.AU        -- Randomly produced by a computer program
www.cs.mu.oz.au/~schachte/  called Markov3. 
Phone: +61 3 8344 9166      
--------------------------------------------------------------------------
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