next up previous contents
Next: Arithmetic Up: Introduction to logic programming Previous: Complex structures in Prolog   Contents

Turning the question around

Since Prolog deals with relations and not functions, we can invoke inverse computations in a natural way, unlike functional programming. For instance, in our first example, we can ask

  ?- edge(3,X).

This asks for all values of X that satisfy the relation edge. If we respond with ; to each answer provided, Prolog will exhaustively list out all possible values that X can take, in the order in which they are defined. The response would look something like

    X=4;
    X=5;
    X=2;
    No.

The final No. indicates that there are no further ways of satisfying the goal. Likewise

  ?- path(3,Y).

will exhaustively list out all values for Y that make this relational assertion true.



Madhavan Mukund 2004-04-29