0% found this document useful (0 votes)
11 views5 pages

AI EXP 2

The document outlines a Prolog programming experiment focused on implementing programs to find the greatest of three variables, calculate factorials, check for palindromes, and determine prime numbers. It provides theoretical concepts of Prolog, including predicates, facts, rules, logical inference, and recursion. The conclusion emphasizes Prolog's effectiveness in solving computational tasks through logical rules and recursive definitions.

Uploaded by

7thmail77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

AI EXP 2

The document outlines a Prolog programming experiment focused on implementing programs to find the greatest of three variables, calculate factorials, check for palindromes, and determine prime numbers. It provides theoretical concepts of Prolog, including predicates, facts, rules, logical inference, and recursion. The conclusion emphasizes Prolog's effectiveness in solving computational tasks through logical rules and recursive definitions.

Uploaded by

7thmail77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Enrollment No.

: 220430116149
Experiment No: 2

Implement following prolog programs.


a. To find the greatest variable among the three variables.
b. To find a factorial of a given number.
c. To check whether a given number is palindrome or not.
d. To check whether a given number is prime or not.

Date:

Competency and Practical Skills: Knowledge of logic programming, Basics of search


Techniques

Relevant CO: CO1

Objectives: To create a functional and efficient Prolog program for solving logic puzzles.

Equipment/Instruments: Personal Computer, SWI-Prolog Interpreter

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.

5. Soundness and Completeness:


Prolog is sound, which means that if it derives a conclusion, that conclusion is guaranteed to be
true according to the rules and facts. It is also complete, meaning that it will find all valid
solutions to a query if they exist within the given 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.

8. Cut Operator (!):


The cut operator is used in Prolog to control the search process and prune certain branches of
the search tree. It can be used to prevent backtracking in some cases.

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)).

b) To find a factorial of a given number.


factorial(0, 1).
factorial(N, Result) :-
N > 0,
N1 is N - 1,
factorial(N1, SubResult),
Result is N * SubResult.

c) To check whether a given number is palindrome or not.


reverse_number(Num, RevNum) :-
reverse_number_helper(Num, 0, RevNum).

reverse_number_helper(0, Acc, Acc).


reverse_number_helper(Num, Acc, RevNum) :-
Num > 0,
Enrollment No. : 220430116149
Digit is Num mod 10,
NewAcc is Acc * 10 + Digit,
NextNum is Num // 10,
reverse_number_helper(NextNum, NewAcc, RevNum).

is_palindrome(Num) :-
reverse_number(Num, Num),
write('Palindrome').
is_palindrome(Num) :-
\+ reverse_number(Num, Num),
write('Not a Palindrome').

d) To check whether a given number is prime or not.


is_prime(2).
is_prime(3).
is_prime(P) :-
integer(P),
P > 3,
P mod 2 =\= 0,
\+ has_factor(P, 3).

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.

b) To find a factorial of a given number.


Enrollment No. : 220430116149

c) To check whether a given number is palindrome or not.

d) To check whether a given number is prime or not.

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.

Quiz: (Sufficient space to be provided for the answers)

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 (``, `).

3. What is logical inference in Prolog, and how does it work?


In Prolog, logical inference refers to the process of deriving conclusions or solutions based on
the rules and facts defined in the knowledge base.
When a query is made to the Prolog interpreter, it attempts to satisfy the conditions specified
in the body of the rules to prove the query true.
Prolog uses a mechanism called resolution to perform logical inference. It attempts to unify
the query with the head of available rules, then recursively tries to satisfy the conditions in
the body of the rule.
If all conditions in the body of a rule are satisfied (i.e., the rule is proven true), the inference
succeeds, and the conclusion specified in the head of the rule is considered true.
Prolog employs backtracking to explore alternative solutions. If a path fails, Prolog will
backtrack and try alternative paths to find all possible solutions.

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)

References used by the students:


1. https://www.geeksforgeeks.org/prolog-an-introduction/
2. https://www.tutorialspoint.com/prolog/prolog_introduction.htm
3. https://www.metalevel.at/prolog/concepts

Rubric wise marks obtained:

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy