[go: up one dir, main page]

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

AI Unit 1

Prolog is a declarative programming language that focuses on logic and relationships through facts and rules, allowing users to query for solutions rather than defining procedures. It operates on a knowledge base and employs elements such as facts, rules, and queries to infer answers. Prolog also features constructs like the cut and fail predicates to control backtracking and manage the flow of logic in programs.

Uploaded by

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

AI Unit 1

Prolog is a declarative programming language that focuses on logic and relationships through facts and rules, allowing users to query for solutions rather than defining procedures. It operates on a knowledge base and employs elements such as facts, rules, and queries to infer answers. Prolog also features constructs like the cut and fail predicates to control backtracking and manage the flow of logic in programs.

Uploaded by

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

Unit - 12

Introduction to Prolog

By Prof. Hetvi Kotak


What is Prolog ?
• Prolog as the name itself suggests, is the short
form of LOGical PROgramming. It is a logical and
declarative programming language.
• Prolog is a declarative language, which means
that a program consists of data based on the facts
and rules (Logical relationship) rather than
computing how to find a solution.
• In prolog, logic is expressed as relations (called as
Facts and Rules). Core heart of prolog lies at
the logic being applied. Formulation or
Computation is carried out by running a query
over these relations.
What is Prolog ?
• PROLOG is a declarative language, which implies
that rather than computing how to find a
solution, a program is made up of data based on
facts and rules (logical connection).
• A logical relationship is a description of the
relationships that exist for a certain application.
• To obtain the solution, the user asks a question
rather than running a program.
• When a user asks a question, then to determine
the answer, the run time system searches through
the database of facts and rules.
What is Prolog ?
Functional Programming vs Logical Programming

• In Functional Programming, we have to define


the procedures, and the rule how the
procedures work.
• These procedures work step by step to solve one
specific problem based on the algorithm.
• On the other hand, for the Logic Programming,
we will provide knowledge base. Using this
knowledge base, the machine can find answers
to the given questions, which is totally different
from functional programming.
How Prolog Works ?
• In Prolog, we need not mention the way how
one problem can be solved, we just need to
mention what the problem is, so that Prolog
automatically solves it.
• However, in Prolog we are supposed to give clues
as the solution method.
• Prolog language basically has three different
elements −
How Prolog Works ?
• Prolog language basically has three different
elements −
• Facts − The fact is predicate that is true, for
example, if we say, “Tom is the son of Jack”, then
this is a fact.
• Rules − Rules are extinctions of facts that contain
conditional clauses.
• To satisfy a rule these conditions should be met.
How Prolog Works ?
• For example, if we define a rule as −
• grandfather(X, Y) :- father(X, Z), parent(Z, Y)
• This implies that for X to be the grandfather of Y,
Z should be a parent of Y and X should be father
of Z.
• Questions − And to run a prolog program, we
need some questions, and those questions can
be answered by the given facts and rules.
Prolog Basics

English PROLOG

and ,

or ;

if :-

not not
Variables and Names
• Variables begin with an uppercase letter.
Predicate names, function names, and the
names for objects must begin with a lowercase
letter.
• Rules for forming names are the same as for the
predicate logic. E.g.
mother_of
male
female
greater_than
Prolog Facts Representation
• likes(john, marry). /* John likes Marry */
• friends(sita, gita). /* Sita is friend with Gita */
• spicy(pickles). /* Pickles are spicy*/
• likes(priya, Food):-delicious(Food). /* Priya like
food if they are delicious*/
• barking(dog). /* Dog is barking */
• not(likes(john,pizza)). /* John does not like pizza
*/
Prolog Rules Representation
• A rule is a predicate expression that uses logical
implication (:-) to describe a relationship among
facts. Thus a Prolog rule takes the form
left_hand_side :- right_hand_side .
• This sentence is interpreted
as: left_hand_side if right_hand_side.
• The left_hand_side is restricted to a single,
positive, literal, which means it must consist of a
positive atomic expression. It cannot be negated
and it cannot contain logical connectives.
Prolog Facts Representation
Examples of valid rules:
• friends(X,Y) :- likes(X,Y),likes(Y,X).
/* X and Y are friends if they like each other */
• hates(X,Y) :- not(likes(X,Y)).
/* X hates Y if X does not like Y. */
• enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)).
/* X and Y are enemies if they don't like each other
*/
Prolog Queries
• The Prolog interpreter responds
to queries about the facts and rules represented
in its database.
• The database is assumed to represent what is
true about a particular problem domain. In
making a query you are asking Prolog whether it
can prove that your query is true.
• If so, it answers "yes" and displays any variable
bindings that it made in coming up with the
answer. If it fails to prove the query true, it
answers "No".
Prolog Queries
• Whenever you run the Prolog interpreter, it
will prompt you with ?-.
• For example, suppose our database consists of
the following facts about a fictitious family.
• father_of(joe,paul).
• father_of(joe,mary).
• mother_of(jane,paul).
• mother_of(jane,mary).
• male(paul).
• male(joe).
• female(mary).
Prolog Queries
• We get the following results when we make
queries about this database:
?- father_of(joe,paul).
Yes
?- father_of(paul,mary).
no
Prolog Terminology
• ?-
• The above symbol shows the system prompt.
The prompt is used to show that the Prolog
system is ready to specify one or more goals of
sequence to the user.
• Using a full stop, we can terminate the sequence
of goals.
• E.g.
• ?- write('Welcome to
Javatpoint'),nl,write('Example of Prolog'),nl.
Prolog Terminology
?- write('Welcome to AI'),nl,write('Example of
Prolog'),nl.
• nl indicates 'start a new line'. When we press
'return' key, the above line will show the effect
like this:
• Welcome to AI
• Example of Prolog
• yes
Prolog Terminology
?- prompt shows the sequence of goal which is
entered by the user.
• The user will not type the prompt. Prolog system
will automatically generate this prompt. It means
that it is ready to receive a sequence of goals.
• To show that the goals have succeeded, we will
output yes.
Prolog Variables
• In Prolog, variables are used to represent values
that can change or be determined by the
interpreter. They are written using a name that
begins with an uppercase letter, such as X or Y.
• Variables can be used in both facts and rules to
represent values that are not known at the time
the program is written. For example, the
following fact uses a variable to represent the
capital of a country:
Prolog Variables
capital_of(Country, Capital).
• In this case, the fact states that the Capital of a
given Country is unknown, and the interpreter
will use other facts and rules to determine the
value of Capital when a query is made.
Prolog Program Example
• Here is a simple Prolog program that defines a
set of rules and facts about a problem domain,
and then uses those rules and facts to answer a
few queries:
Prolog Program Example
Prolog Program Example
• In this example, the program defines three
facts: man(john), woman(mary),
and capital_of(france, paris).
• These facts state that John is a man, Mary is a
woman and Paris is the capital of France.
• The program also defines a rule using
the not/2 predicate. This rule states that if X is a
man and Y is a woman, then X is not Y.
Prolog Program Example
• Finally, the program includes two queries. The
first query, not(john, mary)?, asks the interpreter
to determine whether John is not Mary, based
on the not/2 rule and
the man/1 and woman/1 facts.
• The interpreter will use these rules and facts to
deduce that John is not Mary, and it will
return true as the solution to the query.
Prolog Program Example
• The second query, capital_of(france, X)?, asks
the interpreter to determine the capital of
France.
• The interpreter will use the capital_of/2 fact to
determine that the capital of France is Paris, and
it will return the value paris for the variable X as
the solution.
• Overall, this Prolog program demonstrates how
to define rules and facts about a problem
domain, and how to use those rules and facts to
automatically infer solutions to problems.
Cut Predicate in Prolog
• In Prolog, the "cut" predicate, denoted by the
symbol !, is used to control the backtracking
behavior of the Prolog interpreter.
• It is used to commit to a choice and prevent
further backtracking on a particular branch of
the search tree.
• In Prolog, the "cut" predicate, denoted by the
symbol !, is used to control the backtracking
behavior of the Prolog interpreter. It is used to
commit to a choice and prevent further
backtracking on a particular branch of the search
tree.
Cut Predicate in Prolog
• likes(john, pizza).
• likes(john, sushi).
• likes(john, pasta).
• likes(mary, sushi).
• likes(mary, pasta).
• food_lover(X) :- likes(X, _), !.
?- food_lover(john).
Cut Predicate Example in Prolog
• In this example, we have defined
a likes/2 predicate that represents the food
preferences of individuals.
• The food_lover/1 predicate is defined to check if a
person is a food lover.
• It uses the likes/2 predicate to determine if the
person likes any type of food.
• The cut predicate ! is used after the likes(X, _) goal
to commit to the choice and prevent backtracking.
• As a result, the query food_lover(john) will
succeed, indicating that John is a food lover.
Fail Predicate in Prolog
• In Prolog, the "fail" predicate, denoted by the
symbol fail, is used to explicitly fail a goal.
• It is used to indicate that a particular branch of
the search tree should not be explored further.
• In Prolog, the "fail" predicate, denoted by the
symbol fail, is used to explicitly fail a goal.
• It is used to indicate that a particular branch of
the search tree should not be explored further.
Fail Predicate in Prolog
• likes(john, pizza).
• likes(john, sushi).
• likes(john, pasta).
• likes(mary, sushi).
• likes(mary, pasta).
• likes_pizza(X) :- likes(X, pizza), fail.
• likes_pizza(_).
?- likes_pizza(john).
Fail Predicate in Prolog
• In this example, we have defined
a likes/2 predicate that represents the food
preferences of individuals.
• The likes_pizza/1 predicate is defined to check if
a person likes pizza.
• It uses the likes/2 predicate to determine if the
person likes pizza.
• The fail predicate is used after the likes(X,
pizza) goal to commit to the choice and prevent
backtracking.
Fail Predicate in Prolog
• The query will fail and backtrack to explore
alternative solutions, resulting in the
output true for the second clause
of likes_pizza/1, indicating that John does not
like pizza.
Cut with Failure
• In the following example, we have names of
birds in the database as follows:
• bird(crow).
• bird(sparrow).
• bird(parrot).
• bird(penguins).
• bird(dove).
• bird(robin).
• bird(turkey).
Cut with Failure
• bird(hawk).
• bird(goose).
• bird(swallow).
• bird(pigeon).
• bird(woodpecker).
• The following shows the natural rule to add this:
• can_fly(A) :- bird(A).
The above rule means that 'all birds can fly'.
Cut with Failure
• can_fly(penguins) :- fail.
• can_fly(A) :- bird(A).
• However, the desired result is not provided by
the above:
• ?- can_fly(parrot).
yes
• ?- can_fly(penguins).
yes
Cut with Failure
• can_fly(penguins) :- !, fail.
• can_fly(A) :- bird(A).
• ?- can_fly(parrot).
yes
• ?- can_fly(penguins).
no
Cut with Failure
• As before, the head of the first can_fly clause
and the goal can_fly(penguins) are matched
with each other.
• In the body of that clause, we are trying to
satisfy the goal, the goal obviously fails.
• But here the cut prevents it from backtracking
the system, so the goal can_fly(penguins) fails.
• Cut with failure is the combination of fail and
goals !
Features of PROLOG
• PROLOG is a declarative programming language.
• It employs predicate calculus terminology.
• PROLOG is a natural handler of lists and recursions.
• This language has a built-in inference engine as
well as automated backtracking.
• PROLOG has built-in parallelism.
• Unification: The main concept is to see if the
phrases can be combined to reflect the same
structure.
• For problems requiring inference, PROLOG allows
for very efficient coding.
Advantages & Disadvantages of Prolog
• Advantages :
1. Easy to build database. Doesn’t need a lot of
programming effort.
2. Pattern matching is easy. Search is recursion
based.
3. It has built in list handling. Makes it easier to
play with any algorithm involving lists.
• Disadvantages :
1. LISP (another logic programming language)
dominates over prolog with respect to I/O
features.

You might also like