Assignment :-5
Que 1: Answer the following Questions. (2 Marks)
1. What is Prolog?
Prolog (Programming in Logic) is a high-level programming language based on first-order
predicate logic. It is especially well-suited for tasks involving symbolic reasoning, natural
language processing, knowledge representation, and expert systems.
It was developed in the early 1970s.
Prolog programs consist of a series of facts and rules.
It uses a process called backtracking to search for answers.
🔹 Example:
father(john, mike). % fact
Que 2: Answer the following Questions. (4 Marks)
1. Application of Prolog
Prolog is widely used in:
Expert systems: Diagnosing problems (e.g., medical or technical).
Natural Language Processing (NLP): Parsing and understanding human languages.
Theorem proving: Automated reasoning tasks.
Artificial Intelligence: Representing knowledge, inference.
Semantic Web: Logic-based querying.
Robotics: Rule-based decision-making.
Games: Defining rules, strategy, and logic.
2. Define facts, object, and relation
Facts: Represent truths about the world in Prolog.
o Format: predicate(argument1, argument2, ...).
o Example: likes(john, pizza). means John likes pizza.
Object: Represent entities or instances.
o Example: john, pizza are objects.
Relation: Represent connections between objects.
o The predicate likes/2 defines a relation between the subject and the object.
3. Explain list in Prolog with an example
Lists in Prolog are ordered collections of items.
They are written using square brackets: [a, b, c].
🔹 Example:
member(X, [X|_]). % X is the head of the list
member(X, [_|Tail]) :- % X is in the tail
member(X, Tail).
This program checks if an element is a member of a list.
Que 3: Answer the following Questions. (6 Marks)
1. Structure of Prolog program
A Prolog program has three main components:
Facts: Define known truths.
parent(john, mary).
Rules: Define relationships using logic.
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
Queries: Ask questions about the data.
?- grandparent(john, Who).
The Prolog interpreter uses unification and backtracking to find solutions.
2. Explain Recursion in Prolog
Recursion in Prolog means a predicate calls itself to process complex or repeated logic.
🔹 Example:
factorial(0, 1).
factorial(N, Result) :-
N > 0,
N1 is N - 1,
factorial(N1, R1),
Result is N * R1.
This defines the factorial of a number using recursion.
3. Explain cut and fail with an example
Cut (!): Prevents backtracking past this point. It commits to the choices made so far.
Fail: Forces failure of the current rule or predicate.
🔹 Example:
max(X, Y, X) :- X >= Y, !.
max(_, Y, Y).
Here, if X >= Y is true, Prolog commits to the first rule and does not try the second one due to
the cut.
🔹 Example of fail:
always_fail :- fail.
Calling always_fail will always return false.