Counting the answers to Prolog queries

In a logic language such as Prolog you declare facts and rules, and the system searches for every variable binding that makes a query true. Uppercase names are variables, and \= means the two terms must differ. Consider this program:


prolog
parent(ana, ben).
parent(ana, cara).
parent(ben, dev).
parent(ben, eli).
parent(cara, fay).

grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
sibling(X, Y) :- parent(P, X), parent(P, Y), X \= Y.


For each query in the table, enter the total number of answers Prolog reports if you keep asking for more until it says no. Count each reported binding once; enter 0 if the query has no answers.

QueryNumber of answers
grandparent(ana, Z)
grandparent(ben, Z)
grandparent(X, fay)
sibling(dev, Y)
sibling(X, Y)

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Declarative Languages Questions: