[mercury-users] Pred defns

Ralph Becket rwab1 at cam.sri.com
Thu Mar 26 20:52:10 AEDT 1998


Peter Schachte wrote on 26 Mar:
> On Wed, 25 Mar 1998, Ralph Becket wrote:
> > 
> > With the best will in the world, RTFM.  Have a look at
> 
> I think this is a fair question.  It *is* confusing and...

Ah well, it's been a long week!

> Better still, I think there should be a specialized syntax for
> declaring DCG predicates, ideally some variation on the EDCG
> notation.  There should also be a special syntax for calling DCG
> predicates from non-DCG code; maybe something like phrase/2,3 in Prolog.
> Then the current practice of declaring and calling DCG predicates with
> 2 extra arguments should be phased out.

I've got the Prolog for a syntactic expansion that I find *very*
helpful indeed, and much easier to read.

The idea is that X` denotes the `next state' of X and X`` expands to
the extra pair of arguments X, X`.  The syntactic transformation is
obvious and there's no need to re-work all those library predicates.
It allows you to reuse the same variable *name* when you know that the
previous value is dead and doesn't limit you to where you put your
threaded arguments.  Because it's a syntactic transformation it
shouldn't cause any logical grief (I think).

For example:

	io__write(..., IO``),		io__write(..., IO0, IO1),
	io__write(..., IO``),	 |->	io__write(..., IO1, IO2),
	io__write(..., IO``),		io__write(..., IO2, IO3),

and

	X` = X + 1		 |->	X1 = X0 + 1

and

	map__init(M),			map__init(M0),
	map__insert(M, K0, V0, M`),	map__insert(M0, K0, V0, M1),
	map__insert(M, K1, V1, M`), |->	map__insert(M1, K1, V1, M2),
	map__insert(M, K2, V2, M`),	map__insert(M2, K2, V2, M3),

I'm so taken with this I'd like to see it adopted somewhere (hint
hint!)  The only mildly tricky bit is that you sometimes have to add
unifications to balance things up:

	( foo(X) ->			( foo(X0) ->	
		bar(X``)			bar(X0, X1), Y1 = Y0
	;			 |->	;		
		baz(Y``)			baz(Y0, Y1), X1 = X0
	),				),		
	quux(X, Y)			quux(X1, Y1)	

But that really isn't hard to arrange at all.

Ralph

-- 
Ralph Becket  |  rwab1 at cam.sri.com  |  http://www.cam.sri.com/people/becket.html



More information about the users mailing list