A Prolog programmer writes the rule: ancestor(X, Z) :- ancestor(X, Y), parent(Y, Z). When queried, the program immediately enters an infinite loop. What is the most likely cause?
AThe rule is logically incorrect — ancestor cannot be defined in terms of itself
BProlog searches depth-first and always tries to resolve the recursive subgoal first, looping before reaching base cases
CUnification fails because X and Z are the same variable
DProlog requires all facts to be listed before rules, and the parent facts must be missing
This is the classic Prolog infinite loop from left recursion. When Prolog tries to prove ancestor(X, Z), it first tries the only rule, generating subgoal ancestor(X, Y) — which immediately recurses again before any base-case fact is ever checked. Prolog's depth-first search dives into recursive calls without limit. The fix is to reorder the rule: ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z). — by placing the base-case fact (parent) first, Prolog grounds variables before recursing. This demonstrates why clause ordering is not semantically neutral in Prolog.
Question 2 Multiple Choice
What happens in Prolog when you query a predicate that has been misspelled (e.g., grandparent vs grandparnet)?
AA compile-time error is raised identifying the unknown predicate
BA runtime type error is thrown when the predicate is first evaluated
CThe query silently fails — Prolog returns 'false' with no error message
DProlog prompts the user to define the missing predicate interactively
Prolog has no type system and no compile-time predicate checking. A misspelled predicate name simply has no matching clauses in the database, so unification fails and the query returns false (or 'no'). There is no error message unless you have explicitly enabled the 'unknown' flag. This silent failure is a notorious source of debugging difficulty in Prolog: a typo masquerades as a genuine negative answer. This is one of the practical limitations that makes Prolog error-prone for large programs.
Question 3 True / False
In Prolog, when a sub-goal fails during query resolution, the interpreter backtracks by undoing the most recent variable binding and attempting alternative clause matches.
TTrue
FFalse
Answer: True
Backtracking is the core control mechanism in Prolog. When a sub-goal cannot be proved with the current variable bindings, Prolog backtracks to the most recent choice point — the last point where multiple clause matches were possible — undoes all bindings made since then, and tries the next alternative. This systematic search through the proof space is what allows Prolog to find all solutions to a query, not just the first one, and is fundamental to understanding Prolog's execution model.
Question 4 True / False
The order in which clauses appear in a Prolog program does not affect whether a query will succeed or fail — mainly the logical content of the clauses matters.
TTrue
FFalse
Answer: False
Clause order is semantically significant in Prolog because the search is depth-first and ordered. Prolog tries clauses in the order they are written. A logically correct but poorly ordered program can loop infinitely (as in left-recursive rules) or return results in a different order. Unlike pure logic, where any order of rules leads to the same conclusions, Prolog's procedural search means the programmer must reason about both the logical content and the execution order of clauses.
Question 5 Short Answer
Explain the difference between how an imperative program and a Prolog program approach computing the grandparent relationship, and what this reveals about the logic programming paradigm.
Think about your answer, then reveal below.
Model answer: An imperative program specifies the procedure: iterate over known parents, find each person's parent, check if they match the query. Prolog instead declares what is true: grandparent(X,Z) :- parent(X,Y), parent(Y,Z). The interpreter handles the search automatically via backward chaining and unification. This reveals that logic programming separates the knowledge (what relationships exist) from the control (how to search for them) — the programmer states facts and rules, not algorithms.
This distinction — declarative vs procedural — is the fundamental insight of logic programming. In Prolog, you are writing a knowledge base, not a procedure. The interpreter's resolution strategy (backward chaining, depth-first search, backtracking) is the generic 'how'; your clauses supply the domain-specific 'what.' The power is that the same set of facts and rules can answer many different queries without modification. The limitation is that you cannot fully control execution order, which is why performance and termination require understanding the interpreter's search strategy.