[m-dev.] Syntactic sugar for functor matching

Ralph Becket rafe at cs.mu.OZ.AU
Fri Nov 2 17:26:41 AEDT 2001

In the Mercury group meeting we've just had we spent well over an
hour on this subject.  We believe we've arrived at a solution that
should be acceptable to most, if not all (none of the ideas we
considered were met with universal support...)

The suggestion is that we have three ways to describe a functor in
a unification:

(1) f(X, Y, Z)
	The current standard notation.

(2) f(^field1 = X, ^field2 = Y, ^field3 = Z)
	Fields are accessed by name, each field must be listed
	exactly once.

	This also provides a mechanism for constructing terms
	by field name rather than place value.

(3) f(^field3 = Z, ^field1 = X, ^...)
	The new symbol `^...' may appear at the end of a field
	access list and accounts for any unnamed fields.

	This also solves the problem of listing all the underscore
	arguments when only the functor name is important:


	and this works regardless of whether the functor f has any
	named fields.

A related proposal is to allow `X @ f(...)' to denote pattern matching
with f(...) and having X unify with that argument on success.

For example, rather than writing

	p(f(A, B, C)) :- X = f(A, B, C), ...

one could write

	p(X @ f(A, B, C)) :- ...

It's not clear to me that putting the pattern on the RHS of @ is
the right thing to do, but that's what ML, Haskell, etc. do.

- Ralph
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