Challenge 3: Why Reusing X Across Clauses Doesn't Connect Them — Possible Solution ==================================================================== // Each CLAUSE (each individual fact or rule) in Prolog gets its own, // completely fresh, independent set of variables the moment it's // used -- variable scope in Prolog is per-clause, not per-predicate // and not per-file. Writing X in one clause and X again in a // different clause of the SAME predicate looks, visually, like it // might be "the same X" -- but the two X's are never actually the // same variable at all. They just happen to share a spelling. This // is genuinely different from ordinary lexical scoping in an // imperative or functional language, where reusing a name in a // nearby scope usually either shadows the outer one deliberately or // produces a real "already defined" error -- Prolog does neither; it // silently treats them as two unrelated variables with no warning. // // If a programmer genuinely needs two goals within the SAME clause // to be related -- e.g. requiring that a value produced by one goal // is used again by a second goal -- the correct approach is to use // the SAME variable name within that ONE clause's own body, where // Prolog's per-clause scoping actually does apply and does connect // them. For example, in: // // sibling(X, Y) :- parent(P, X), parent(P, Y), X \= Y. // // P genuinely IS shared correctly here, because all three goals -- // parent(P, X), parent(P, Y), and X \= Y -- belong to the SAME // clause's body. It's only reuse ACROSS separate clauses (separate // facts or separate rule definitions) that fails to connect anything, // not reuse within one clause's own body. WHY THIS WORKS AS AN ANSWER ------------------------------ This correctly explains that Prolog's variable scoping is per-clause, not per-predicate, and clarifies the real distinction the chapter's warn-box is pointing at: reuse WITHIN one clause's body genuinely connects variables, while reuse ACROSS separate clauses does not, giving a concrete working example of the correct within-clause pattern.