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.
% 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
1. What does typing ?- parent(tom, X). do at the Prolog top-level?
2. What happens when you type ; after Prolog reports a solution?
3. In what order does the Prolog top-level try clauses when resolving a query?
4. Why might ?- parent(tom, X). and ?- parent(X, ann). return different bindings even though they query the same facts?
5. What does it mean when Prolog answers false to a query?
Was this page helpful?
You May Also Like
Unification in Prolog
How Prolog's core matching mechanism works: when two terms unify, how atoms, numbers, variables, and compound terms are compared, and why the occurs check matters.
Backtracking Explained
How Prolog recovers from failed goals and finds multiple solutions by returning to choice points, and what that means for how programs actually execute.
The Cut Operator
How Prolog's cut operator (!) prunes backtracking, the difference between green and red cuts, and how cut underlies the if-then-else idiom -- along with its common pitfalls.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics