Re: order of solutions on backtracking

"Richard A. O'Keefe" <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 2/08/2013, at 11:15 AM, Fernando Guzmán wrote:

> I have a database consisting, among other things, of a large number of facts with different predicates.  I need to run a query that can be satisfied either by 
> pred1(arguments)
> or 
> pred2(arguments).
> 
> If I submit the query:
> ?- pred1(arguments); pred2(arguments).
> on backtracking, this will generate all solutions with pred1 first, and then all solutions with pred2.
> 
> What I need is all the solutions with either predicate in the PRECISE order in which they appear in the database.

Why do you want that?

In general, the solutions _don't_ appear in the database,
but are _computed from_ it.  Consider

	funny(Xs, X-Y) :-
	    reverse(Xs, Ys),
	    member(X, Xs),
	    member(Y, Ys).

	?- funny([1,2,42,137], X, Y).

What is the order in which the solutions of this query appear in
the database?  Answer: the solutions DON'T appear in the database.
If you pick up the clause, it isn't a solution to the query.

Prolog defines an order on the clauses of a single predicate,
but it does not define an order between predicates.

The order in the database might depend on the order in which
you load files, especially if some of the predicates are :-multifile.


> My question here is this: is there a "cleaner/more elegant" solution to this problem?

There is a *vastly* cleaner solution which is to build the priority you
want into the *logic* rather than into accidents of loading.
What you are doing is very much like trying to order the answers to
an SQL query by the binary value of their addresses on disc.

What is the problem you are actually trying to solve?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.