Path: utzoo!attcan!uunet!mcvax!unido!ecrcvax!micha
From: micha@ecrcvax.UUCP (Micha Meier)
Newsgroups: comp.lang.prolog
Subject: Re: common subterms (was Re: Why no macro facility?)
Message-ID: <561@ecrcvax.UUCP>
Date: 14 Jul 88 07:29:14 GMT
References: <9671@lll-winken.llnl.gov> <6899@burdvax.PRC.Unisys.COM> <6206@megaron.arizona.edu>
Reply-To: micha@ecrcvax.UUCP (Micha Meier)
Organization: ECRC, Munich 81, West Germany
Lines: 34

In article <6206@megaron.arizona.edu> debray@arizona.edu (Saumya Debray) writes:
> ... the clause
>
>     p(f(g(a))) :- q(h(g(a))), r(f(g(a))).
>
>is compiled as
>
>     p(f(X)) :- X = g(a), q(h(X)), r(f(X)).
>-- 

	I have also thought about factoring common terms, however it seems
	to me that recognizing them can become *very* costly, especially
	when many lists occur in the clause, or do you have some
	clever algorithm? On the other hand, it is possible to factor
	out even the top-level compound term from the head, you only
	have to remember its reference in a variable when the indexed
	argument is unified; this, of course, cannot be done using
	a source-to-source transformation, it must be done inside
	the compiler:

		p(f(X)) :- q(f(X)), r(f(X)).

		...
		switch_on_functor A1, ...
		...
		get_variable	Y1, A1	% so Y = f(X) is part of the head
		get_structure	f/1, A1
		unify_variable	Y2
		call		q/1	% no instruction necessary, anyway
		put_value	Y1, A1
		deallocate
		execute		r/1

--Micha