
The Power of Prolog: A Comprehensive Guide to Logic Programming for AI
Last Updated on April 15, 2025 by Editorial Team
Author(s): Abinaya Subramaniam
Originally published on Towards AI.

In procedural or object-oriented programming (OOP), such as languages like C, Java, or Python, the focus is on how to do things. In these languages, we write explicit instructions to tell the computer what steps to take to solve a problem. These steps are executed in a specific sequence, and we define how things should be computed, handled, and manipulated.
For example, if we wanted to find the largest number in a list in a procedural language, we would need to write a series of steps to traverse the list, compare numbers, and update a variable with the maximum value found so far.
In Prolog, however, we do not tell the computer how to solve a problem. Instead, we describe what we want to achieve, and Prolog takes care of figuring out the how.
Prolog (PROgramming in LOGic) is a declarative programming language, which means that we focus on defining facts, rules, and relationships rather than providing a sequence of instructions. This is in contrast to the procedural paradigm where we are responsible for defining the exact sequence of operations that the program should follow.
Why we should Learn Prolog?
- Declarative Paradigm: Instead of specifying how to solve a problem (as in procedural languages), Prolog allows you to define what the problem is, letting the interpreter derive solutions.
- Symbolic AI: Prolog excels in rule-based systems, expert systems, natural language processing (NLP), and theorem proving.
- Historical Significance: Early AI systems like ELIZA (chatbot), XCON (expert system), and IBMβs Deep Blue used Prolog or LISP.
- Modern Relevance: While Python dominates machine learning, Prolog remains essential for knowledge-based AI, constraint logic programming, and semantic reasoning.
Important Concepts of Prolog

Facts, Rules, and Queries
Prolog programs consist of Facts, Rules and Queries.
- Facts: Ground truths
These are the basic building blocks of knowledge in Prolog. A fact is something that is always true. For example,
vehicle(car).
vehicle(boat).
operates_in(car, road).
operates_in(bicycle, road).
Here, weβre saying:
- βcar is a vehicleβ
- βboat is a vehicleβ
- βcar operates on roadβ
2. Rules: Logical implications
Rules let you define logic: If some condition is true, then something else is also true.
land_vehicle(X) :- vehicle(X), operates_in(X, road).
air_vehicle(X) :- vehicle(X), operates_in(X, air).
water_vehicle(X) :- vehicle(X), operates_in(X, water).
These rules define:
- A land_vehicle as the vehicle that operates on roads.
- An air_vehicle as the vehicle that flies in the air.
- A water_vehicle as the vehicle that sails in water.
3. Queries
Queries are the questions posed to the knowledge base.
- Which vehicles operate on land?
?- land_vehicle(X).
This will return,
X = car ;
X = bicycle.
2. Is helicopter an air vehicle?
?- air_vehicle(helicopter).
Returns,
true.
Horn Clauses in Prolog
A Horn clause in Prolog is a rule or fact with:
- At most one positive literal (the conclusion).
- No negations in the conclusion.
The typical form is:
Conclusion :- Condition1, Condition2, ..., ConditionN.
Recursion in Prolog
In Prolog, recursion is the process where a rule refers to itself. Itβs essential for problems that require traversing or searching through data structures, like lists or trees.
We will now walk through the ancestor example step by step and understand recursion.
parent(john, mary). % John is a parent of Mary
parent(mary, lisa). % Mary is a parent of Lisa
parent(lisa, emma). % Lisa is a parent of Emma

Now, we define two rules for ancestor/2
:
ancestor(X, Y) :- parent(X, Y). %Rule 1
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). %Rule 2
Rule 1: X is an ancestor of Y if X is a parent of Y (Direct relationship)
Rule 2: X is an ancestor of Y if X is a parent of Z, and Z is an ancestor of Y (Indirect relationship)
The second rule allows Prolog to βlook deeperβ into the family tree, checking generations above Y by recursively calling ancestor(Z, Y). Essentially, Prolog asks, βIs X a parent of someone who is an ancestor of Y?β
How Prolog Deduce the Answer?
?- ancestor(john, emma).
First: Check if John is a parent of Emma (based on Rule 1).
parent(john, emma)
is false, so it proceeds to the second rule.
Second: Prolog will try to see if John is a parent of someone who is an ancestor of Emma.
parent(john, mary)
is true. Now, check if Mary is an ancestor of Emma (ancestor(mary, emma)
).
Third: Check if Mary is an ancestor of Emma.
parent(mary, lisa)
is true, so check if Lisa is an ancestor of Emma (ancestor(lisa, emma)
).
Fourth: Check if Lisa is an ancestor of Emma.
parent(lisa, emma)
is true, so we found that Lisa is a parent of Emma, and thus, Mary is an ancestor of Emma.- Finally, this proves John is an ancestor of Emma.
In Prolog, data is represented through several fundamental types, each of which serves a specific role in the knowledge representation and logical reasoning. Letβs go through each type one by one to explain how they work:
1. Atoms
Atoms are the simplest form of data in Prolog. They are used to represent basic facts or constants in the knowledge base.
Examples:
- Atoms can be single words like
cat
, or multi-word expressions enclosed in quotes like'Sri Lanka'
. cat
(a symbol representing a cat)'Sri Lanka'
(a string representing the name of a country)- They represent indivisible pieces of data in Prolog.
Atoms are often used as identifiers or facts, such as when defining rules or facts.
2. Numbers
Numbers in Prolog are used to represent numeric values.
- Prolog handles both integer and floating-point numbers.
- These numbers are used for arithmetic operations and logical comparisons.
Numbers can be used in facts, rules, and queries.
3. Variables
Variables in Prolog are used to represent unknown values and are central to querying and rule deduction. They start with capital letters or an underscore (_
).
Examples:
X
(a variable)Name
(another variable)_
(a placeholder/anonymous variable used when the value is not important)- Variables can stand for any value, and Prolog will try to unify them with values in the knowledge base.
- Variables are case-sensitive. They must start with an uppercase letter (or underscore).
- If a variable is prefixed with an underscore (
_
), it is treated as an anonymous variable, meaning Prolog does not retain its value.
4. Structures
Structures are more complex data objects that combine atoms, numbers, and variables. They are used to represent more complex facts. A structure consists of a functor (which is an atom) and a number of arguments (which can be atoms, numbers, variables, or even other structures).
Example:
animal(cat, domestic(yes))
This structure represents an animal, specifically a cat
, and indicates that it is domestic(yes)
.
- A functor is an atom that represents the type of structure (e.g.,
animal
). - The arguments of a structure are placed inside parentheses, separated by commas.
- Structures can be nested, as seen in
domestic(yes)
inside theanimal
structure.
5. Lists
Lists in Prolog are an essential data structure used to represent ordered collections of items. Lists are written as elements enclosed in square brackets []
, with the head (first element) and the tail (the remaining list).
[head | tail]
This notation means βa list where head
is the first element and tail
is the rest of the list." The tail
itself is a list (it can be empty).
% Example: List of numbers
numbers([1, 2, 3, 4]).
% Query: Check if the list is [1, 2, 3, 4]
?- numbers([1, 2, 3, 4]).
Applications of Prolog in AI

- Expert Systems: Rule-Based Decision-Making
An expert system is a computer program designed to emulate the decision-making abilities of a human expert in a specific field. Prolog is used in expert systems to represent knowledge as a set of rules and facts, and it performs inference to make decisions or provide solutions based on those rules.
In a medical diagnosis system, Prolog can be used to represent medical knowledge (e.g., symptoms, diseases, treatments) and use logical rules to infer potential diagnoses based on the symptoms presented.
Example:
% Facts
symptom(fever).
symptom(cough).
disease(flu).
disease(cold).
flu(fever).
flu(cough).
cold(cough).
% Rule for diagnosis
diagnose(Disease) :-
disease(Disease),
call(Disease, Symptom),
symptom(Symptom).
Based on the query (For Eg: ?- diagnose(flu) ), it will diagnose the disease accordingly (e.g., flu if symptoms like fever and cough are present).
Natural Language Processing
Natural Language Processing (NLP) involves programming computers to understand, interpret, and generate human language. Prolog is used in NLP for tasks such as parsing, sentence structure analysis, and semantic understanding.
Parsing is the process of analyzing a sentence to determine its grammatical structure. In Prolog, you can represent the grammar rules of a language as a set of facts and rules, then use Prologβs pattern matching capabilities to break down and understand sentences.
% Facts defining grammar rules
sentence(S) :- noun_phrase(NP), verb_phrase(VP).
noun_phrase(NP) :- determiner(D), noun(N).
verb_phrase(VP) :- verb(V), noun_phrase(NP).
% Lexical rules (basic words)
determiner(the).
noun(cat).
noun(dog).
verb(chases).
% Query: Is "the cat chases the dog" a valid sentence?
?- sentence([the, cat, chases, the, dog]).
Automated Theorem Proving
Automated Theorem Proving involves using computers to prove mathematical theorems automatically. Prolog can be used for theorem proving by encoding logical statements (such as axioms and theorems) and then querying whether a particular statement is true based on logical inference.
Semantic Web & Knowledge Graphs
The Semantic Web is an extension of the World Wide Web that aims to make data on the web more machine-readable and interpretable. Knowledge Graphs are a representation of interconnected data points (e.g., entities and their relationships) that can be used for intelligent reasoning and querying.
In conclusion, Learning Prolog isnβt just about syntax. Itβs about rethinking how we solve problems with logic, clarity, and deduction. If youβre exploring symbolic AI and curious about declarative programming , then Prolog is an interesting path to explore.
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI