EXPERIMENT 2: Introduction to Prolog Programming
Objective
To understand the fundamentals of Prolog programming, including its syntax, structure,
real-life applications, and practical examples.
1. Introduction to Prolog
Prolog (short for Programming in Logic) is a high-level declarative programming
language developed in the early 1970s by Alain Colmerauer and Robert Kowalski. It
emphasizes what the problem is rather than how to solve it, using facts, rules, and
queries.
Rooted in first-order predicate logic, Prolog is especially useful in artificial intelligence
(AI), natural language processing (NLP), and expert systems due to its powerful
reasoning and symbolic computation capabilities.
2. Prolog Syntax and Structure
Prolog (short for Programming in Logic) is a declarative programming language
commonly used in artificial intelligence and computational linguistics. Unlike imperative
languages, Prolog is based on formal logic and allows developers to define relationships
and query them.
Basic Structure
A Prolog program consists of facts, rules, and queries.
1. Facts
Facts are used to represent basic assertions about the world.
prolog
CopyEdit
father(john, jim). % John is the father of Jim
mother(mary, jim). % Mary is the mother of Jim
2. Rules
Rules define relationships using existing facts.
prolog
CopyEdit
parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
This means X is a parent of Y if X is either a father or mother of Y.
3. Queries
You can ask questions (queries) to the Prolog system to infer information.
prolog
CopyEdit
?- parent(john, jim).
true.
?- parent(mary, jim).
true.
4. Variables
Variables in Prolog begin with uppercase letters or an underscore.
prolog
CopyEdit
?- parent(X, jim).
X = john ;
X = mary.
5. Lists
Lists in Prolog are written using square brackets.
prolog
CopyEdit
?- member(3, [1, 2, 3, 4]).
true.
6. Recursion Example
You can define recursive rules in Prolog:
prolog
CopyEdit
factorial(0, 1).
factorial(N, Result) :-
N > 0,
N1 is N - 1,
factorial(N1, R1),
Result is N * R1.
Query:
prolog
CopyEdit
?- factorial(5, X).
X = 120.
3. Real-Life Examples with Code :
Example 1
:- initialization(main).
main :-
write('Hello, World!').
Example 2
factorial(0, 1).
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
:- initialization(main).
main :-
factorial(5, F),
write('Factorial is: '), write(F), nl.
4. Advantages of Prolog
1. Declarative Logic
Prolog focuses on what is true rather than how to compute it, simplifying development
and logic expression.
2. Pattern Matching and Unification
Automatic variable binding during execution allows for concise logical expressions.
3. Built-in Backtracking
Automatically explores all valid solutions, removing the need for manual looping.
4. Symbolic Processing
Efficiently handles symbolic and recursive data structures for complex logic tasks.
5. Fast Prototyping
Enables rapid development and testing of logic-driven applications.
5. Real-Life Applications of Prolog
1. Expert Systems
Used in diagnostics, legal reasoning, and other rule-based decision systems.
2. Natural Language Processing (NLP)
Models language rules and structures for parsers, chatbots, and translation tools.
3. Theorem Proving
Used in formal logic systems and AI research to automate proof construction.
4. Semantic Web
Applies rule-based reasoning over ontologies and structured data.
5. Robotics and Planning
Used to model action sequences, plan tasks, and reason about environment states.
6. Conclusion
Prolog is a logic-based language that excels in areas requiring symbolic reasoning,
knowledge representation, and logical inference. While not typically used for general-
purpose programming, it remains highly relevant in AI and expert systems. Prolog
provides a unique perspective on problem-solving that complements procedural thinking.