Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!pasteur!agate!ig!arizona!debray From: debray@arizona.edu (Saumya Debray) Newsgroups: comp.lang.prolog Subject: common subterms (was Re: Why no macro facility?) Summary: macros, inline expansion, common subterms Message-ID: <6206@megaron.arizona.edu> Date: 13 Jul 88 02:42:21 GMT References: <9671@lll-winken.llnl.gov> <6899@burdvax.PRC.Unisys.COM> Organization: U of Arizona CS Dept, Tucson Lines: 48 In article <6899@burdvax.PRC.Unisys.COM>, dowding@macbeth.PRC.Unisys.COM (John Dowding) writes: > I did some experiements ... and found that it slowed the > program down. I think that this is because current Prolog compilers > dont handle very large clauses well. Consider this example: > > p(X):- q(X), r(X), s(Y). > % ^^^ author probably meant s(X) -- skd > q(tree(_,_,_,_)). > > If we turn q into a macro, then the resulting clause for p is: > > p(tree(A,B,C,D)):- r(tree(A,B,C,D)), s(tree(A,B,C,D)). The problem, as the author points out, is that the compiler isn't factoring common subterms. As a result, multiple copies of the same structure are being created on the heap. Factoring common subterms in this example would give p(X) :- X = tree(_,_,_,_), q(X), s(X). While common subterm factoring looks very attractive, one problem with doing it incautiously is that indexing capabilities can be weakened because a nonvariable term is being pulled from the head into the body (as in the above example). The resulting choice point creation may well wipe out any benefits obtained from creating fewer structures on the heap. Some years back, I looked at incorporating common subterm factoring into the SB-Prolog compiler. My experience was that people (well, OK, Prolog hackers who were at Stony Brook at the time) didn't usually write code with a lot of common subterms to be factored. This, together with the complications arising from having to make sure that indexing didn't suffer from such factoring, made the cost of common subterm factoring unattractive. The current SB-Prolog compiler therefore does only a limited form of common subterm factoring: the clause p(f(a,b)) :- q(f(a,b)), r(f(a,b)). is compiled with three copies of the term f(a,b), but 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)). -- Saumya Debray CS Department, University of Arizona, Tucson internet: debray@arizona.edu uucp: arizona!debray