AI Search · Exam notes
Older papers ask goal-tree tracing: a Prolog knowledge base, a query with variables, and three sub-questions: which nodes expand in which order? what is the substitution? what is the solution path? One chain answers all: expand the leftmost goal first, bind variables to the leftmost matching fact, backtrack on the first failure, and read the solution off the final substitution.
| Prolog form | How to read it |
|---|---|
| dinnerPlan(X) :- restaurant(X), friendLikes(X). | Consequent FIRST, antecedents after. X is a dinner plan if X is a restaurant and the friend likes X. Uppercase = variable (substitutable); lowercase = constant (fixed). |
| Goal set {A, B, C} | Goals still to prove. Expanding A by a rule replaces it with the rule body; matching a fact deletes it. Empty set = solved. |
| Backward chaining | Match the goal against rule consequents and fact heads, moving from goal toward facts (the reverse of forward chaining). |
| DFS rule | What happens |
|---|---|
| Leftmost goal, leftmost clause, leftmost fact | Always expand the first goal in the set, try rules top to bottom, facts top to bottom. Clause and fact order decides everything. |
| Unification (substitution) | Binding a variable, e.g. \(X = beach\), rewrites every later goal containing X. One variable, one value at a time. |
| Backtrack on failure | A goal with no match fails: undo the latest binding and try the next fact, then the next rule. Untried branches right of a success stay untried. |
| Solution = subtree + substitution | The proof is the successful root-to-facts path (an AND-OR solution subtree); the answer is the final variable binding. |
3a. Knowledge base as tables (solve from this, then check the walkthrough)
| Rule | Prolog clause |
|---|---|
| R0 | outingPlan(X,Y,Z) :- eveningPlan(X), moviePlan(Y), dinnerPlan(Z). |
| R1 | eveningPlan(X) :- outing(X), friendLikes(X). |
| R2 | moviePlan(Y) :- movie(Y), friendLikes(Y). |
| R3 | dinnerPlan(Z) :- restaurant(Z), friendLikes(Z). |
| Facts, top to bottom | Entries in order |
|---|---|
| outing / movie / restaurant | outing: mall, beach. movie: matrix. restaurant: pizzaHut, saravanaBhavan. |
| friendLikes | beach, matrix, bhuvanShome, saravanaBhavan. (No mall, no pizzaHut: those branches must fail.) |
Query: ?- outingPlan(X,Y,Z). Cover the walkthrough. Expand leftmost, bind leftmost, backtrack on failure.
3b. Walkthrough: goal set plus substitution at every step
Why this step
Tracker (goals + substitution)
3c. The three questions
Knowledge base and query as above. Depth-first, leftmost-first search.
3d. Answers first, then the working
Expanded: outingPlan, eveningPlan, outing(mall) FAIL, outing(beach), moviePlan, movie(matrix), dinnerPlan, restaurant(pizzaHut) FAIL, restaurant(saravanaBhavan) (friendLikes checks ride along each binding). Substitution: X=beach, Y=matrix, Z=saravanaBhavan. Path: outingPlan - eveningPlan - beach; moviePlan - matrix; dinnerPlan - saravanaBhavan.
Rules: R1: niceToy(X) :- green(X), circle(X). R2: niceToy(X) :- red(X), square(X). Facts: green: A, B. circle: C, E. red: C, D. square: D. Query: ?- niceToy(X). Use the Nice toy tab above, then read the answers.
Expanded: niceToy, green(A), circle(A) FAIL, green(B), circle(B) FAIL, red(C), square(C) FAIL, red(D), square(D). Rule 1 exhausts both greens before rule 2 is ever tried. Substitution: X=D. Path: niceToy - rule 2 - red(D) - square(D). The C failure is the classic trap: C matches red but is a circle, not a square.
Solve each with a pen before opening the answer. Each drill is self contained.
Facts: p(a), p(b), q(b), q(c). Rule: r(X) :- p(X), q(X). Query: ?- r(X). Q1: expansion order? Q2: substitution?
Why this step
Tracker
Expanded: r(X), p(a), q(a) FAIL, p(b), q(b). X=a dies because q(a) is no fact; the same X must satisfy both subgoals. Substitution: X=b. Path: r - p(b) - q(b).
Facts: a, c. Rules: g :- a, b. and g :- c. Query: ?- g. Q1: expansion order? Q2: solution path?
Why this step
Tracker
Expanded: g, a, b FAIL (no fact b), c. First rule dies on b; backtrack to the second rule, c holds, goal set empties. Path: g - rule 2 - c. No variables, so the solution is the proof itself.