Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!killer!ames!pasteur!ucbvax!decwrl!sun!pitstop!sundc!seismo!uunet!kddlab!icot32!icot21!chik
From: chik@icot21.icot.JUNET (Chikayama Takashi)
Newsgroups: comp.lang.prolog
Subject: Re: Why no macro facility?
Message-ID: 
Date: 11 Jul 88 03:15:04 GMT
References: <9671@lll-winken.llnl.gov>
Sender: chik@icot32.icot.junet
Reply-To: chikayama@icot.junet
Organization: Institute for New Generation Computer Technology, Tokyo, Japan.
Lines: 51
In-reply-to: daven@lll-crg.llnl.gov's message of 7 Jul 88 18:00:51 GMT


The language ESP (a language consisting of Prolog + Object-oriented
features, running on the Prolog machine PSI) does have macro expansion
facility.  The language has been used for 4 years already for various 
software in the Japanese FGCS project.

Difficulty in macro processing in Prolog is that literal replacement
as seen in macro expansion for the language C, or expansion of
individual terms in the clauses, will never be enough.  More 
structural approach is needed.

With our macro:
	p(X) :- q(f(X+3)).			(1)
can be expanded to:
	p(X) :- Y is X+3, q(f(Y)).		(1e)
Note that "X+3" is NOT expanded in its original place.
Also, such a macro as to expand:
	p(X) :- q(X, Y!integer).		(2)
to:
	p(X) :- q(X, Y), integer(Y).		(2e)
can also be defined.  Note that, in (1e) and (2e), an extra goal is 
inserted in different places: before and after the goal in which the 
macro invocation appears.

The macro mechanism also helps to eliminate higher-level predicates. 
For example, consider a program with higher level predicate "mapcar":
	p(L0, L) :- mapcar(L0, q, L).
where:
	mapcar([], _, []).
	mapcar([X|L0], P, [Y|L]) :-
		Goal =.. [P,X,Y],
		call(Goal),
		mapcar(L0, P, L).
As "call/1" cannot but be interpretive, you may want to rewrite it (by
hand, maybe) for efficiency as:
	p(L0, L) :- mapcar_q(L0, L).
	mapcar_q([], []).
	mapcar_q([X|L0], [Y|L]) :-
		q(X, Y),
		mapcar_q(L0, P, L).
This transformation can be automatically done by our macro expansion
mechanism, if "mapcar" is defined as a macro properly.

DCG can be defined using the same mechanism.

More details will appear in the Seattle conference:

	Sei-ichi Kondo and Takashi Chikayama:
	"Macro processing in Prolog"

Takashi Chikayama (ICOT)