No Prev Next Up Home Keys Figs Search New

Term Expansion to Multiple Clauses

Appeared in Volume 6/3, August 1993

Keywords: terms.

spratt@hawk.cs.ukans.edu
Lindsey Spratt
2nd December 1992

Is there a Prolog system which supports a term-expansion mechanism, where the input term is expanded into a list of terms (clauses), instead of a one-for-one expansion? I have needed this technique several times, but I have never seen a Prolog which supports it.

For example, I have a defstruct facility which I use in Prolog. I would like:
defstruct(foo(bar, baz)).

to expand into several different clauses via term_expansion:

bar_in_foo(B, foo(B, _)).
baz_in_foo(B, foo(_, B)).
foo_parts(foo(Bar, Baz), Bar, Baz).
same_foo_except_bar(foo(B1, Baz), foo(B2, Baz), B1, B2).
same_foo_except_baz(foo(Bar, Baz1), foo(Bar, Baz2), Baz1, Baz2).
Instead, I use a translator directive:
:- defstruct(foo(bar, baz)).

which asserts the above clauses. This is much less satisfactory than compiling the clauses. I could have this directive write the requisite clauses out somewhere and then consult the result, but this is also unsatisfactory.

So, why is this multiple term-expansion facility not available? Am I the only person who has ever found a use for it? Is it fraught with subtle difficulties for language implementors?

matsc@sics.se
Mats Carlsson
3rd December 1992

Several Prologs, including Quintus and SICStus Prologs, have this feature. term_expansion/2 is allowed to return in its second argument a single term or a list of terms (clauses, declarations, or directives), to be treated one at a time by the compiler. More generally, the syntax of a Prolog program is defined to be a sequence of clauses, declarations, directives, or lists of such, although lists are hardly useful except as produced by term_expansion/2.

I've found it useful for a number of things. In the Prolog standardization group, I've been arguing (with little success) to include term_expansion/2 with this functionality.

It might cause some problems for vendors who have implemented their compilers in C.

tim@quintus.com
Tim Lindholm
3rd December 1992

Quintus Prolog 3.X lets you term-expand to multiple clauses, and we've found a number of uses for it. Implementing the basic functionality isn't really difficult, but there are certain things that are rather tricky to handle correctly (Quintus Prolog doesn't necessarily do them right).

A problem for us is that it is possible to term-expand to module or meta-predicate declarations. The question is whether the original source clauses must look like a valid module (a module declaration followed by zero or more meta-predicate declarations), or whether the term-expanded clauses must look like a module (tricky to enforce for tedious reasons), or what. Yes, we had someone here who wanted to do this.

ludemann@quintus.com
Peter Ludemann
3rd December 1992

I was that someone. I was building an object-oriented system using term expansion and I wanted the module import and export lists to be generated automatically.

There are additional complexities to term expansion. Some Prologs, such as Quintus Prolog, do DCG expansion after user term expansion; others, such as IBM Prolog, don't. If I define my own DCG-like expansion, I have to be very careful that it happens after my other term expansions.

Incidentally, IBM-Prolog also allows multiple clauses from term expansion.

Multiple clause term expansion has one other use: for conditionally excluding clauses.

No Prev Next Up Home Keys Figs Search New