Computing with Sequences

All Prolog implementations deal implicitly with sequences of solutions to problems by means of backtracking: if one solution to a subproblem is rejected another is presented. A number of implementations also provide predicates which provide sets or bags {sets mith possibly repeated elements) of solutions. Sut in these case the system finds all the s.olutions before proceeding. It is suggested here that an implementation of bags, or sequences, which finds only one solution at a time can be integrated easily with exis~ing implementation techniques for Prolog. This technique has a number of advantages over similar proposals. If it is combined with subprograms which exhibit tail recursion, then one can write logically correct programs to process sets of solutions which do not use extra memory. These can include prog~am~ which reduce solutions (e.g. count, sum or averaga solutions) aithout using •impure• techniques. However thetechnique involves little overhead in programs mhich do not use it, unlike cenain other proposals for coroutining. This_ will have particular advantage in database applications ■here large amounts of infor,aation must be retrieved serially fro• secondary storage. It also has potential application for parallelism since the next solution of the subproblem may be pre-evaluated on a parallel processor, without changing the normal interpretation of Prolog clauses.