AI EXP 2
AI EXP 2
: 220430116149
Experiment No: 2
Date:
Objectives: To create a functional and efficient Prolog program for solving logic puzzles.
Theory:
In Prolog, a program consists of a set of facts and rules that define relationships and properties in
a knowledge base. The primary theoretical foundation of Prolog is based on formal logic,
specifically first-order logic. Here's a brief overview of the key theoretical concepts that underlie
Prolog programs:
1. Predicates:
In Prolog, predicates are used to represent relationships or properties in the form of clauses.
Predicates consist of a name (an atom) and a fixed number of arguments. For example, `parent(X,
Y)` represents the "parent" relationship with two arguments, X and Y.
2. Facts:
Facts are the basic building blocks of Prolog programs. They represent simple, unconditionally
true statements. For example:
```
parent(john, mary).
```
3. Rules:
Rules define relationships and properties based on the satisfaction of certain conditions. Rules
have a head and a body. The head specifies the predicate to be derived, and the body specifies the
conditions that must be satisfied for the rule to apply. For example:
```
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
```
In this rule, `grandparent(X, Y)` is derived if `parent(X, Z)` and `parent(Z, Y)` are satisfied.
4. Logical Inference:
Prolog uses logical inference to make queries and derive conclusions from the facts and rules. It
employs a resolution-based mechanism to perform backward chaining, where it works backward
from the goal (query) to the premises (facts and rules) to find a solution. This is done using
Enrollment No. : 220430116149
unification, a process that matches variables in the query with terms in the knowledge base.
6. Recursion:
Recursion is a common and powerful technique in Prolog. It allows rules to refer to themselves,
enabling the definition of complex relationships and properties. For example, a rule for
calculating factorial using recursion:
```
factorial(0, 1).
factorial(N, F) :- N > 0, N1 is N - 1, factorial(N1, F1), F is N * F1.
```
7. Termination:
It is essential to ensure that Prolog programs terminate for all possible queries. Non-terminating
programs can lead to infinite loops and are generally undesirable.
Prolog's theoretical foundation in logic, along with its mechanism for logical inference, allows it
to express and solve complex problems in a declarative way. Prolog is particularly well-suited for
tasks involving symbolic reasoning, knowledge representation, and search problems. It is widely
used in artificial intelligence, expert systems, natural language processing, and more.
Observation/Program:
a) To find the greatest variable among the three variables.
greatest(X, Y, Z, Max) :-
Max is max(X, max(Y, Z)).
is_palindrome(Num) :-
reverse_number(Num, Num),
write('Palindrome').
is_palindrome(Num) :-
\+ reverse_number(Num, Num),
write('Not a Palindrome').
has_factor(N, Factor) :-
N mod Factor =:= 0.
has_factor(N, Factor) :-
Factor * Factor < N,
NextFactor is Factor + 2,
has_factor(N, NextFactor).
Output:
a) To find the greatest variable among the three variables.
Conclusion:
Overall, these programs showcase the power and versatility of Prolog in handling various
computational tasks using logical rules and recursive definitions. They demonstrate Prolog's
ability to express complex algorithms concisely and elegantly, making it a suitable language for
tasks involving symbolic computation and logical reasoning.
1. In Prolog, what is the purpose of rules, and how are they structured?
Rules in Prolog are used to define relationships and infer new knowledge based on
existing facts and rules.
They allow the programmer to express logical implications: if certain conditions (defined
in the body of the rule) are met, then a conclusion (defined in the head of the rule) can be
inferred.
Rules are essential for defining the behavior of predicates and enabling logical reasoning
within Prolog programs.
2. Explain the difference between the head and body of a Prolog rule.
Head:
The head of a Prolog rule specifies what is being inferred or queried. It consists of a single
predicate.
Enrollment No. : 220430116149
Body:
The body of a Prolog rule contains conditions that must be satisfied for the inference
stated in the head to be true. It consists of one or more predicates separated by commas (,),
and the conjunction operator (``, `).
Suggested Reference:
1. http://lpn.swi-prolog.org/lpnpage.php?pageid=online
2. “Artificial Intelligence” -By Elaine Rich and Kevin Knight (2nd Edition) Tata McGraw-
Hill
3. “PROLOG Programming for Artificial Intelligence” -By Ivan Bratko (Addison-Wesley)
Problem Completeness
Knowledge Logic
Recognition and accuracy Ethics (2)
Rubrics (2) Building (2) Total
(2) (2)
Good Avg. Good Avg. Good Avg. Good Avg. Good Avg.
(2) (1) (2) (1) (2) (1) (2) (1) (2) (1)
Marks