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

Updated 19 Nov 20265 min read
Declarative Programming and Logic-Based Programming: Facts, Rules, Unification and Backtracking

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:

prolog
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.

Family graph of anu, bhavna, chirag, dev and esha beside the parent facts, ancestor rules and query, with anu to dev to esha in green.

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

ancestor(anu, esha)

Try the base rule

Reduces to parent(anu, esha), which fails

2

ancestor(anu, esha)

Try the recursive rule

Becomes parent(anu, Z), ancestor(Z, esha)

3

parent(anu, Z)

First matching fact gives Z = bhavna

Continue with ancestor(bhavna, esha)

4

ancestor(bhavna, esha)

Direct fact fails; recursive path takes bhavna -> chirag

No path from chirag to esha, so this branch fails

5

parent(anu, Z)

Backtrack to the saved choice point

Abandon Z = bhavna and try the next match

6

parent(anu, Z)

Next matching fact gives Z = dev

Continue with ancestor(dev, esha)

7

ancestor(dev, esha)

Base rule asks for parent(dev, esha)

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.

Proof tree for ancestor(anu, esha): the Z = bhavna branch fails, backtracking returns to the choice point, and Z = dev succeeds.

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 Z = bhavna, later Z = dev

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:

  1. Can edge(X, X) and edge(2, 3) unify? No, they cannot unify.

  2. Is ancestor(anu, esha) true? Yes, via anu -> dev -> esha.

  3. What does ancestor(Who, esha) return? Who = dev before Who = anu under 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.