Professional Documents
Culture Documents
UNIT-4
PROLOG
Artificial Intelligence
Prolog stands for programming logic it is a computer programming language. Since its
beginning in 1970’s its been used by the programmers for applications of symbolic
computation like
• Relational databases
• Mathematical logic
• Abstract problem solving
• Understanding natural language
• Design automation
• Symbolic equation solving
• Many areas of artificial intelligence
• Objects in prolog differs from objects and relationships of OOP. object is a data
structure that can inherit fields and executable methods from class hierarchy. Prolog
does not compared with OOP
• In prolog objects represents things that we can represent using terms from which all
data and prolog programs are constructed.
• Prolog programs consists of a set of clauses where each clause is either a fact about
the given information or a rule about how the solution may relate to the given facts.
Programming:-
Computer programing in prolog consists of:-
• Specifying some facts about objects and their relationships
• Defining some rules about objects and their relationships
• Asking questions about objects and their relationships
The names of the objects written inside round brackets are called arguments.
And the relationship is called the predicate.
likes(john, mary) ----→ likes is a predicate having two arguments.
Facts in prolog allows to represent arbitrary facts about arbitrary objects.
Collection of facts is called database.
Questions:-
Once we have facts we can ask some questions about them.
In prolog a question looks just like facts except that we put a special symbol before it.
The special symbol followed by a ? And a - hyphen
?-owns(mary,book)
Prolog tries to match the predicate with in the database. It looks for the facts that
unify the facts in the question.
If prolog finds a fact that unifies with the question then it returns “yes” if no facts
unifies with the question then prolog replies “no”.
First three questions answers is no, it does not mean the statement is false. It means
that no facts unifies with the question.
What happens suppose if we ask a question about relationships not there In the
database?
The question can simply say no or a warning can be given before saying no, or an error
message can be printed.
Example:-
Variables:-
• In prolog we can not only name particular objects but we can also use
terms like X to stand for objects that are unwilling to name. Terms of this
kind are called variables.
• When prolog uses variables the variables can be instantiated or non
instantiated. A variable is instantiated when there is a object that stands
for. A variable is not instantiated when the object is not known.
• In prolog programming variables begins with capital letter.
• When prolog is asked question containing variable it searches through all
the facts to find the objects that stand for the variable
After returning one value prolog program will wait for further
instructions:-
1. If you press the enter key it means you are satisfied with just one
answer then prolog will stop searching for more.
2. If you press semicolon ( ; ) with enter key prolog will resume and
starts searching for other possibilities where it left the place
marker. When prolog tries to find other possible answers from the
place marker we say that prolog is re-satisfying the question.
3. The variable X is now instantiated with mary. Prolog puts a place
marker at the fact likes(john, mary) and returns X=mary.
Conjunctions:-
( , ) represents and
Conjunctions and the use of variables:-
Rules:-
This can be listed instead of writing all the objects what john likes. This
fact can be written as a rule.
Rules are used when a fact depends on a group of other facts.
comments:-
Comments are enclosed in /*……*/ they are ignored by the prolog. But for
programmer convenience they can be added to the prolog program.
CHAPTER-2
Syntax:-
Syntax:- it describes how we can allow words to fit together.
Ex:- I see a zebra is a correct sentence.
Zebra I see a is not a correct sentence.
PROLOG programs are built from terms. A term is either a constant, a variable or a
structure. Each term is written as a sequence of characters.
A to Z--→Upper case letters
A to z--→lower case letters
0 to 9-→ digits
+,- / \ ( ) → special signs.
Constants:-
Structures:-
Characters:-
ex:-
(3 + 4) --→ +(3,4)
To evaluate an operator we need to know 3 things about each operator
* its position
* its precedence
* its associativity.
The syntax of the term with operator depends on the position of the
operator.
+ * - / operators are written between their arguments so we call them
infix operators.
It is also possible to write operator before the argument –x+y negates the
value of X.
Operators that comes before arguments are called prefix operators.
Some operators comes after their arguments called postfix operators.
Example:- “ X!” factorial of X. factorial is denoted by exclamation mark
comes after the argument.
The precedence of operator tells us about which operation is to be carried
out first.
+(x, * ( y,z)). * is performed first before addition.
Multiplication and division has higher precedence over addition and
subtraction.
A-b/c --→-(a, /(b,c)).
Operator associativity:-
Consider how operators are associated. When we have several operators
with same precedence then we use associativity.
Addition, subtraction, multiplication, division all are left associative.
8/2/2 = (8/2)/2 = 2, 8/(2/2)= 8
A left associate operator must have the same or lower precedence
operations on the left and lower precedence operations on the right .
8/4/4= (8/4)/4 5+8/2/2= 5+ ((8/2)/2).
Arithmetic:-
People use computers to perform operations on numbers.
Consider the following database about the population and area of various
countries in 1976.
pop(x,y) means the population of country X is about Y million people.
Now to find the density of a country, we must use the rule that the density
is the population divided by the area.
A prolog rule for this is:
CHAPTER-3
DATA STRUCTURES AND TREES:
Lists:-
• It is a common data structure in non-numeric programming.
• List Is a ordered sequence of elements that can have any length.
• “ ordered” means the order of elements in the sequence matters.
• Elements in the list may be any terms, constants, variables or
structures.
• Lists are used to represent parse trees, grammars, city map, computer
programs and mathematical entities such as graphs, formulas and
functions.
List can be represented as a special kind of tree.
• A list is either empty list having no elements, or it is a structure that
has two components head & tail.
• The end of a list is represented as a tail that is set to the empty list.
• Empty list is writes as [ ].
• The head and tail of a list are components of the functor named “ . “
.a(,.(b,.(c, [])))
Some people uses vine diagram to represent the lists. The list growing
from left to right, the branches are hanging down.
The dot notation is not suitable for writing complicated lists.
Another syntax for writing list in prolog program ie., list notation.
It consists of the elements of the list separated by commas, & the whole
list is enclosed in square brackets list notation as [ a ] and [ a, b, c ].
[ ] empty list
[the, men[like, to , fish]]
[ a, v1, b [x,y]]
The head of a list is the first component of the “ . “ functor that is used
to construct lists.
Head of a rule & head of a list both are not same. The tail of Variables in
a list can be instantiated at any time, we can provide a way to put “
holes “ in the lists that can be filled with data at a later time
a list is a second component of the “.” functor.
When a list appears in the square bracket notation the head of the list is
the first element of the list. The tail is a list that contains every element
except the first
X=1 Y = [2, 3]
X = the [Y= cat, sat, [on, the, mat]]
?- P([-, -, -, [-IX]]).
X = [the, mat]
Mapping:-
Given a prolog structure, we frequently wish to construct a new
structure that is similar to the old one but changed in some way. We
traverse the old structure component- by – component and construct
the components of the new structure we call this as mapping.
For example:-
Let us consider a prolog program in which we type an English sentence
and prolog replies with another sentence that is an altered version of
the one we typed.
Altering a list with head H and tail T gives a list with head X and
tail Y if changing word H gives word X and altering the list T gives
the list Y. what is meant by “changing “ one word into another.
change(X,Y) means word X changes to Y at the end of the
database we need a “ catchall” fact because if a word is not
changed into another word it needs to be changed into itself.
Change(X,X) means word x is changed to itself.
a) A unifying fact can be found. In this case we say the goal has been
matched. We mark the place in the database, and instantiate any
previously uninstantiated variables that have unified. If we matched
against a rule, we shall first have to attempt to satisfy the subgoals
introduced by the rule. If the goal succeeds, we attempt to satisfy the
next goal.
b) No unifying fact can be found. In this case, we say the goal has failed.
We attempt to re-satisfy the goal.
2. We can attempt to re-satisfy a goal. We try to attempt to re-satisfy each of
the subgoals in turn. If no subgoal can be re-satisfied in a suitable way, we
attempt to find an alternative clause for the goal itself. We must make
uninstantiated any variables that became instantiated when the previous
clause was chosen. We resume searching the database from where the goal’s
place-marker was previously put. The new “backtracked” goal may either
succeed or fail and either step (a) or (b) would occur.
A special mechanism that can be used in Prolog programs: the “cut”. The
cut allows you to tell Prolog which previous choices it need not consider
again.
The simplest way a set of facts can allow multiple solutions to a question
is when there are several facts that will match against the question.
If we have the following facts in which father(X,Y) means that the father
of X is Y:
A special mechanism that can be used in Prolog programs: the “cut”. The
cut allows you to tell Prolog which previous choices it need not consider
again when it backtracks through the chain of satisfied goals. There are
two reasons it may be important to do this.
The cut, in Prolog, is a goal, written as !, which always succeeds, but
cannot be backtracked. It is best used to prevent
unwanted backtracking, including the finding of extra solutions by Prolog
and to avoid unnecessary computations.
We shall define a predicate pp such that the goal pp(X,Y) displays the list
in a helpful way. The name pp means “pretty print”.
For example the list [1,2,3] is pretty printed as
?- start.
X= |: 1.
Y= |: 2.
Sum is 3
yes