Next: Derived Information Up: Simple Prolog Clauses Previous: Using the PDB

Getting All Solutions

As stated, the example queries require interactive prompting by typing a semicolon, to return more than one solution. However, there are two methods by which Prolog can return all possible solutions to a particular query. The first is to put the built-in predicate (also called a procedure) fail. at the end of the query. This forces backtracking whenever it is encountered. In order that the results of each solution to the query are recorded, some write statements must be added. For example:



| ?- select_chains(2,3,CID),chain_length(CID,Len),Len >= 150, 
        write(CID),nl,
        write(Len),nl,fail.

will print the values of chain identifier and length that satisfy the query. The nl predicate simply places a carriage-return character after each write. Part of the output of this query looks like this:



8at1c
310
8atca
310
8atcc
310

A more useful type of output would be to write out the results in a form that can be directly re-used by Prolog as a set of clauses. For example:



| ?- select_chains(2,3,CID),chain_length(CID,Len),Len >= 150, 
        writeq(special_chains(CID,Len)),writeq(.),nl,fail.

the writeq predicate makes sure that all syntax is correctly observed. Part of the output resulting from this query is shown below and can be read back into Prolog for further processing:



special_chains(5acn,754).
special_chains(5adh,374).
special_chains(5at1a,310).
special_chains(5at1c,310).
special_chains(5tln,316).

In this example, there is no real advantage in writing the results in this form since the original query is very quickly evaluated. However, for a complex query where Prolog may take several minutes to find all solutions, it makes sense to save intermediate results in this form. This is particularly true in the analysis of protein structure, where one question frequently leads to another and the results of the first question are necessary to solve the following question.

An alternative way to return all solutions to a query is to make use of the Prolog built-in procedure bagof. This is an extremely flexible procedure that returns the results of a query in the form of a Prolog list. For example, the previous query could be expressed as:



| ?- bagof(special_chains(CID,Len),
           (select_chains(2,3,CID),chain_length(CID,Len)),L)

and will return the variable L as a list containing special_chains(CID,Len) clauses. Normally, this is the method of choice since it gathers all solutions into an easily manipulated list structure. This example required 0.08 seconds to scan a database containing 501 entries on a Sun SPARCstation 1.



Next: Derived Information Up: Simple Prolog Clauses Previous: Using the PDB


gjb@bioch.ox.ac.uk