Programming in Logic
Prolog (short for Programming in Logic) is a high-level programming language that is
particularly well-suited for solving problems that involve relationships, rules, and logical
inference. It is commonly used in artificial intelligence (AI), natural language processing, and
computational linguistics.
Prolog (PROgramming in LOGic) is a declarative programming language rooted in
formal logic. Unlike procedural programming languages, where you specify the steps, the
computer must take to solve a problem, Prolog works by specifying facts, rules, and queries,
leaving it to the underlying inference engine to figure out how to derive answers. It’s widely
used in artificial intelligence, particularly in natural language processing, expert systems, and
knowledge representation.
Characteristics of Prolog
1. Declarative Nature: In Prolog, you declare what the program should do (facts and
rules), not how it should do it (procedural code). The Prolog engine figures out the
"how" by applying a built-in inference mechanism to resolve queries.
2. Predicate Logic: Prolog is based on first-order predicate logic, where relationships
between entities are described by predicates, and variables can range over these
entities.
3. Recursion: Recursion is heavily used in Prolog, as it is the primary way to define
iteration. For example, solving problems that involve traversing lists or trees often
involves recursive predicates.
4. Backtracking: Prolog uses backtracking as its search strategy. When a goal fails,
Prolog backtracks to the last decision point and tries alternative solutions.
Concepts of Prolog:
1. Facts: These are basic assertions about the world. For example:
parent(john, mary).
This states that John is the parent of Mary.
2. Rules: These define relationships between facts and allow us to infer new facts. For
example:
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
This rule states that X is a grandparent of Y if X is a parent of Z and Z is a parent of Y.
3. Queries: You can ask Prolog to find information based on the facts and rules. For
instance:
?- parent(john, mary).
1
This asks whether John is the parent of Mary.
4. Variables: Variables in Prolog are placeholders for any object and are denoted by
starting with an uppercase letter. They can unify with any term (fact, object, or rule).
?- parent(X, mary).
This query asks, “Who is Mary’s parent?” and Prolog will unify X with john based on
the facts.
Example of a Prolog Program:
% Facts
male(john).
male(mike).
female(mary).
parent(john, mary).
parent(mary, mike).
% Rules
father(X, Y) :- male(X), parent(X, Y).
mother(X, Y) :- female(X), parent(X, Y).
% Queries
?- father(john, mary). % true
?- mother(mary, mike). % true
Applications of Prolog
Artificial Intelligence: Knowledge representation, expert systems, natural language
processing.
Database Queries: Prolog’s logic-based structure makes it useful for querying
complex databases.
Theorem Proving: Prolog can be used to automate the proving of logical theorems.
2
1. Unification
Unification is the process of matching two terms in Prolog. It is a fundamental operation that
enables the inference engine to determine whether two terms (facts, variables, or rules) can be
made identical.
Variables can unify with constants, other variables, or complex structures.
Constants can only unify with themselves.
For example: prolog
?- X = 5.
X = 5. % Unifies X with 5
?- X = Y.
X = Y. % Unifies two variables
?- f(a, X) = f(a, b).
X = b. % Unifies X with b because the structure matches
In the above examples, Prolog tries to find assignments for variables that make the two terms
identical. If no such assignment exists, unification fails.
2. Forward Chaining
Forward chaining is a data-driven reasoning technique where you start with known facts and
apply inference rules to extract new facts until a goal is reached or no more inferences can be
made.
In Prolog, forward chaining is not the primary mechanism, but it can be simulated. It’s more
commonly used in production systems or rule-based expert systems.
Example (Pseudo-code in terms of rules and facts):
% Facts
bird(sparrow).
can_fly(X) :- bird(X). % Rule
% Forward Chaining:
% Start with the fact "bird(sparrow)" and infer "can_fly(sparrow)"
3
Steps:
1. Fact: bird(sparrow) is true.
2. Rule Application: can_fly(X) :- bird(X).
3. Conclusion: can_fly(sparrow) is inferred.
This approach is called forward because it moves forward from known facts to discover new
information.
3. Backward Chaining
Backward chaining is the default inference mechanism in Prolog and is a goal-driven
reasoning approach. You start with a goal and attempt to prove it by working backward, using
rules and facts. Prolog recursively tries to satisfy the goal by resolving it against facts and
rules.
In Prolog, when a query is made, the system uses backward chaining to try to prove it.
Example:
% Facts
parent(john, mary).
parent(mary, mike).
% Rule
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
% Query
?- grandparent(john, mike).
Steps:
1. Prolog starts with the goal grandparent(john, mike).
2. It looks for a rule that matches this goal, which is grandparent(X, Y) :- parent(X, Z),
parent(Z, Y).
3. It now has two subgoals to prove:
o parent(john, Z)
o parent(Z, mike)
4. It finds Z = mary by solving parent(john, mary) and then checks if parent(mary, mike)
holds, which it does.
5. So, grandparent(john, mike) is true.
4
This approach is backward because it works backward from the goal to facts or subgoals.
Differences Between Forward and Backward Chaining
Feature Forward Chaining Backward Chaining
Starts with a goal and works
Starting Point Starts with facts and applies rules.
backward.
Data or Goal
Data-driven (triggered by facts). Goal-driven (triggered by a query).
Driven
Can be inefficient if many rules
Efficiency More efficient for focused queries.
apply.
Usage Common in rule-based systems. Used in Prolog for solving queries.
Applications
Forward Chaining: Expert systems, like CLIPS, where new data continuously adds
to the knowledge base.
Backward Chaining: Logic programming languages like Prolog, AI reasoning, and
theorem proving.