Declarative Programming and Logic-Based Programming: Facts, Rules, Unification and Backtracking
Learn how a logic program turns a small knowledge base into answers. Trace one ancestor query through unification, recursion, failure and backtracking.
KnowledgeGate Team
Exam prep & CS education

Most programming lessons teach a sequence of commands, so a program made of facts, rules and a question can look inactive. Declarative programming states relations, logic programming derives answers, and unification, recursion and backtracking do separate jobs. The five-person ancestor knowledge base can be traced from query to result.
Declarative programming says what relation must hold
Declarative programming describes the facts, relations or constraints a result must satisfy, leaving much of the execution mechanism to the language system. An imperative ancestor search might manage a stack, queue and visited nodes. A logic program defines ancestor using parent and asks the engine for a proof.
Logic programming is one declarative paradigm, not a synonym for every declarative style. Imperative, functional and object-oriented classifications answer different design questions and can overlap. The earlier Programming Paradigms MCQs: 11 Solved Questions on Concepts and Classifications tests rapid mechanism recognition across imperative, object-oriented, functional and logic styles. The branching ancestor program goes deeper: every unifier, recursive goal, failed branch and backtrack is traced in execution order. The wider Coding and Skill Development Courses catalogue covers the broader programming ground this paradigm sits in.
Facts and rules form the knowledge base; queries and variables drive proofs
Keep this clause order fixed because it affects the search trace:
parent(anu, bhavna).
parent(bhavna, chirag).
parent(anu, dev).
parent(dev, esha).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).parent(anu, bhavna) is a fact. parent/2 and ancestor/2 are two-argument predicates. The first ancestor rule covers one edge; the second covers paths of two or more edges. Read :- as "is true if". Lowercase names are atoms, while uppercase X, Y and Z are variables. ?- ancestor(anu, esha). asks whether the goal can be proved.
Predicates and variables connect to Propositional and Predicate Logic Explained. A logic engine also follows an operational search procedure. Mathematical meaning determines valid answers; clause order and strategy affect how it reaches them.

Worked example: prove ancestor(anu, esha) step by step
Assume conventional top-to-bottom clause order and left-to-right, depth-first search.
Step | Current goal | Substitution or choice | Result |
|---|---|---|---|
1 |
| Try the base rule | Reduces to |
2 |
| Try the recursive rule | Becomes |
3 |
| First matching fact gives | Continue with |
4 |
| Direct fact fails; recursive path takes | No path from |
5 |
| Backtrack to the saved choice point | Abandon |
6 |
| Next matching fact gives | Continue with |
7 |
| Base rule asks for | The fact exists, so the query succeeds |
The conclusion is ancestor(anu, esha) = true, with proof path anu -> dev -> esha. No variable changed from Bhavna to Dev. The engine abandoned one substitution and explored another.

Ask for answers, not only true or false
?- ancestor(anu, Who). asks for substitutions. In the same order it returns Who = bhavna, Who = dev, Who = chirag, then Who = esha. The base rule finds both direct children before the recursive rule finds descendants. Requesting another answer resumes at the latest choice point.
For ?- ancestor(Who, esha)., the answers are Who = dev, then Who = anu. Dev has a one-edge path to Esha; Anu has a two-edge path. Logic determines the valid set, while clause order, fact order and strategy can determine answer order and termination.
Unification, recursion and backtracking do different work
Unifying edge(X, 3) with edge(2, Y) gives the most general substitution {X = 2, Y = 3}. Their structures are compatible. edge(X, X) cannot unify with edge(2, 3) because one X cannot be both 2 and 3. Unification is structural matching, not assignment.
Recursion defines the transitive relation by replacing an ancestor goal with one parent edge and a smaller ancestor goal. Backtracking revisits a saved choice after failure or a request for another answer.
Mechanism | Job | Worked value |
|---|---|---|
Unification | Find consistent substitutions | Try |
Recursion | Express a relation in terms of itself | Reduce ancestry to a parent edge plus ancestry |
Backtracking | Explore alternative proof branches | Return after the Bhavna branch fails and try Dev |
Clause order, negation and syntax create common traps
Head :- Body means the head is true if the body can be proved, not the reverse. Uppercase Who is a variable; lowercase who is an atom. In a Prolog-like language, = requests unification, not reassignment.
Ordering can prevent termination. Put ancestor(X, Y) :- ancestor(X, Z), parent(Z, Y). before the base case and a depth-first engine may keep expanding the leftmost goal for ancestor(anu, esha) instead of reaching a fact. A logical-looking rule can behave poorly under left-to-right search.
Negation as failure is limited. \+ parent(anu, farah) succeeds because the fact cannot be proved here. It does not establish a universal real-world fact. Answer order is likewise not the definition of the ancestor relation.
How exams test logic-based declarative programming
Questions can ask you to identify the paradigm, distinguish facts, rules and queries, compute a unifier, trace a recursive proof, predict answer order under a stated strategy, or spot looping clause order.
The key checks are:
Can
edge(X, X)andedge(2, 3)unify? No, they cannot unify.Is
ancestor(anu, esha)true? Yes, viaanu -> dev -> esha.What does
ancestor(Who, esha)return?Who = devbeforeWho = anuunder the declared order.
The short version and a concrete next step
Remember the chain: facts store relations; rules state implications; a query supplies a goal; unification and search find substitutions that prove it. Here, ancestor(anu, esha) = true. The first recursive branch, Z = bhavna, fails; backtracking reaches the successful branch, Z = dev.
Now append parent(esha, farah). and predict the answers to ancestor(anu, Who). Under the same order they become bhavna, dev, chirag, esha, farah. Redraw the graph and locate the new recursive proof: Farah is reached along anu -> dev -> esha -> farah.
Next, work through the C Language Course: Concepts, MCQs and Coding or Python Course: Concepts, MCQs and Coding to practise explicit program flow. Compare that imperative experience with this fact-rule-query model.
Keep learning
Inference Mechanisms in First Order Logic: Unification, Chaining, Resolution and Worked Proofs
See how substitutions turn First Order Logic rules into proofs. One Horn-clause example connects generalized modus ponens, forward and backward chaining, and resolution.
Communication General Knowledge and General Studies for Teaching Exams: Facts and Worked Examples
Build a reliable Communication GK revision map for teaching exams, then practise chronology, matching and statement questions with fully checked answers.
Backtracking Technique: Concepts, Two Worked Examples and Exam Patterns
Trace 4-Queens and subset sum step by step, prove safe pruning rules, and avoid common backtracking errors.
Backtracking Technique Interview Questions and Answers: Trace, Prune and Explain
Learn to explain backtracking through exact traces, safe pruning, clean state restoration, N-Queens constraints, complexity derivation and interview follow-ups.





