100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Queries and the Prolog Interpreter

An introduction to how Prolog queries work: typing goals at the ?- prompt, reading bound variables and backtracking with ;, and understanding how the interpreter searches its clause database.

Queries & UnificationBeginner8 min readJul 10, 2026
Analogies

Queries and the Prolog Interpreter

A Prolog query is a goal you ask the interpreter to prove against its database of facts and rules. You type it after the ?- prompt in the top-level (the interactive loop provided by systems like SWI-Prolog's swipl), and Prolog searches its stored clauses trying to unify them with your goal, reporting any variable bindings that make the goal true.

🏏

Cricket analogy: When a captain reviews a run-out via DRS, the third umpire is essentially given a query -- 'was the batsman out?' -- and checks the available evidence (camera facts) exactly the way Prolog checks a goal like ?- parent(tom, bob) against its stored facts before answering yes or no.

The Read-Query-Print Loop

The top-level reads a query, attempts to unify it against clauses in the database from top to bottom, and prints the resulting variable bindings if it succeeds, or false if no clause can satisfy the goal. Typing a semicolon (;) after a solution asks Prolog to backtrack and search for the next alternative answer, rather than accepting only the first one found.

🏏

Cricket analogy: Pressing 'review' repeatedly for successive close lbw shouts across an innings is like typing ';' at the Prolog prompt -- each press asks the system to search for the next valid alternative answer instead of stopping at the first.

prolog
% facts
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).

?- parent(tom, X).
X = bob ;
X = liz.

?- parent(X, ann).
X = bob.

?- parent(tom, bob).
true.

How the Interpreter Searches the Database

Prolog resolution is depth-first and ordered: clauses are tried in the order they appear in the source file (top to bottom), and subgoals within a clause body are tried left to right. This is a form of SLD resolution combined with unification, and it builds an implicit proof tree as the interpreter commits to one path before backing off to try alternatives.

🏏

Cricket analogy: A bowler running through a fixed field-setting checklist top to bottom before every delivery is like Prolog's depth-first, top-to-bottom clause search -- if the first field placement rule doesn't match the situation, it tries the next one in listed order.

Query Order and Variable Bindings

Where you place a variable in a query changes which stored facts can satisfy it. ?- parent(tom, X). fixes the first argument and lets X range over matching second arguments, while ?- parent(X, ann). fixes the second argument instead. Both queries search the same fact base but return different bindings because unification checks each argument position independently.

🏏

Cricket analogy: Asking 'who partnered Kohli in that century stand?' versus 'which innings did Kohli register a century in?' are different queries against the same partnership records, just as ?-parent(tom,X) and ?-parent(X,ann) bind different variables against the same fact base.

Clause order matters for more than style: because the top-level tries clauses top-to-bottom and explores depth-first, a recursive predicate whose recursive clause is listed before its base case can search forever without ever reaching a solution. Always place the terminating (base) clause where the search will reach it before recursing indefinitely, and remember that reordering facts can change which solution is returned first, even though the set of solutions found via backtracking stays the same.

  • A Prolog query is a goal typed after ?- that the interpreter tries to prove against the facts and rules in its database.
  • The top-level read-query-print loop unifies the query against clauses top-to-bottom and prints variable bindings on success.
  • Typing ; after a solution asks Prolog to backtrack and search for the next matching answer.
  • Query resolution is depth-first and left-to-right: Prolog commits to the first matching clause and the first subgoal before considering alternatives.
  • Which argument of a query is left as a variable determines which facts match and what gets bound.
  • Clause order can affect both performance and termination, especially in recursive predicates.
  • false (or 'No') means the interpreter exhausted every clause and choice point without proving the goal.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PrologStudyNotes#QueriesAndThePrologInterpreter#Queries#Prolog#Interpreter#Read#SQL#StudyNotes#SkillVeris