0% found this document useful (0 votes)
21 views54 pages

PROLOG

The document discusses Prolog, a logic programming language. It covers topics like facts, rules, queries, objects, relations, predicates, variables, structures, and provides family relationship examples to demonstrate Prolog concepts.

Uploaded by

Himanshu Rawat
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)
21 views54 pages

PROLOG

The document discusses Prolog, a logic programming language. It covers topics like facts, rules, queries, objects, relations, predicates, variables, structures, and provides family relationship examples to demonstrate Prolog concepts.

Uploaded by

Himanshu Rawat
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/ 54

PROLOG

Topics for Discussion

 Introduction
 Facts ,Rules, Queries
INTRODUCTION
PROLOG
 Prolog is a short for programming in logic.
 It has important role in artificial intelligence.
 Unlike many other programming languages, Prolog is intended primarily
as a declarative programming language.
 In declarative programming language , programmers specifies a goal to
be achieved , the PROLOG system works out how to achieve it.
 Traditional programming languages are said to be procedural as
programmer must specifies in detail how to solve a problem.
 PROLOG is specially well suited for solving problems that involve
objects and relations between objects.
 A prolog program is a collection of data or facts
and the relationship among these facts.
 i.e the program is a database.
 PROLOG is specially well suited for solving
problems that involve objects and relations
between objects.
Facts, objects and relations
Fact
 samyara likes john.
 likes(samyra , john). Fact

Relation objects period (.)


 Facts can be described as symbolic relationships.

 Fact is something that is always, unconditionally true.

Ex. X is a parent of Y.
 parent(X,Y). Clause
 This factual expression is called clause.
Object
 An object is the name of the element of certain type.
 Object type could be as follows:
 Char type, integer, real, string, symbol etc.
 Objects appear as comma-separated arguments
within parenthesis.
Relation
 A relation is a name that defines the way in which a
collection of objects or variables belong together.
Predicate
Ex. Car is blue.
is(car, blue) . Predicate
 Predicate express a relationship.

 The elements within the parenthesis are the

arguments of the predicate, which may be objects


or variables.
 The word before the parenthesis is the name of the

relation.
Note
 Names of the relationship begin with the lowercase letter.
 A period “.” must end a fact.
Prolog clauses are of three type:
 Facts , rules, question.
Rules
 Rules declare things that are true depending on a given
condition.
 A rule is an expression that shows that the truth of a particular
fact is depends upon one or more facts.
Ex. Jenny likes X if X likes Badminton.
likes(jenny, X) :- likes(X, badminton).
 :- operator is also called a BREAK or neck symbol.

means “if” or “ is implied by”.


Therefore we say that rules have:-
 a condition part (the right hand side of the rule) is also called

the body.
 a conclusion part (the left hand side of the rule) is also called the

head.
 The comma “,” stands for conjuction/and.

 The comma “;” stands for disjunction/or.


 If the condition part is true then a logical consequences of this
is also true.
 Prolog rule consist of the head and the body.
 The body is a list of goals separated by commas.
Questions
 By means of questions the user can ask the program what
things are true.
Data objects
 Fig. shows a classification of data objects in
Prolog.
 The Prolog system recognizes the type of an object
in the program by its syntactic form.
 This is possible because the syntax of Prolog
specifies different forms for each type of data
objects.
Atoms and numbers
Atoms and numbers can take more complicated
forms-that is, strings of the following characters:
 upper-case letters A, B, ..., Z
 lower-case letters a, b, ..., z
 digits 0, L,2, ...,9 .
 special characters such as + - * I <) : : . & _ ¬.
Atoms can be constructed in three ways:
 Strings of letters, digits and the underscore character, '-', starting
with a lower-case letter:
 Strings of special characters:
When using atoms of this form, some care is necessary because
some strings of special characters already have a predefined
meaning; an example is':-'.
 Strings of characters enclosed in single quotes.
This is useful if we want, for example, to have an atom that starts
with a capital letter. By enclosing it in quotes we make it
distinguishable from variables:
'Tom' 'South-America' 'Sarah Jones'
Numbers
 Numbers used in Prolog include integer numbers and real numbers.
 The syntax of integers is simple as illustrated by the following
examples: 1 , 1313 , 0 ,-97.
Variables
 Variables are strings of letters, digits and underscore characters. They
start with an upper-case letter or an underscore character:
 X
 Result
 Object2
 Participant_list
 _x23 , _23
Anonymous Variable
 When a variable appears in a clause once only, we do not have to invent
a name for it. We can use the so-called 'anonymous' variable, which is
written as a single underscore character.
 For example: let us consider the following rule:
hasachild( X) :- parent( X, Y).
 This rule says: for all X, X has a child if X is a parent of some Y.
 We are defining the property hasachild which, as it is meant here, does
not depend on the name of the child.
 Thus, this is a proper place in which to use an anonymous variable. The
clause above can thus be rewritten as :
hasachild( X) :- parent( X, -).
 Each time a single underscore character occurs in a
clause it represents a new anonymous variable.
For example,
we can say that there is somebody who has a child
if there are two objects such that one is a parent of
the other:
 somebody-has-child :- parent( _, _).
 This is equivalent
 somebody-has-child :- parent( X, Y).
Structures
 Structured objects (or simply structures) are objects
that have several components.
 The components themselves can, in turn, be
structures.
e.g
 the date can be viewed as a structure with three components: day,
month, year.
 Although composed of several components, structures are treated
in the program as single objects. In order to combine the
components into a single object we have to choose a functor.
 A suitable functor for our example is date.
 Then the date 1st May 1,983 can be written as:
 date( t, may, 1983)
 All the components in this example are constants (two integers
and one atom). Components can also be variables or other
structures. Any day in May can be represented by the structure:
 date( Duy, may, 1983)
 Note that Day is a variable and can be instantiated
to any object at some later point in the execution.
This method for data structuring is simple and
powerful.
 It is one of the reasons why Prolog is so naturally
applied to problems that involve symbolic
manipulation.
 Syntactically, all data objects in Prolog are terms
meaning
Declarative
 Consider a clause
P:- Q,R.
where P, Q and R have the syntax of terms. Some alternative
declarative readings of this clause are:
P is true if Q and R are true.
From Q and R follows P.
 A comma denotes the conjuction .
 A semicolon denotes the disnjuction
P:- Q;R.
 is read: P is true if Q is true or R is true. The meaning of this clause is
thus the same as the meaning of the following two clauses together:
 P:-Q.
P:-R.
 The comma binds stronger than the semicolon. So the clause
P:- Q,R;S,T,U.
 is understood as
 P :- (Q,R);(S,T,U).
 and means the same as the clauses:
 P:- Q,R.
P:- S,T,U.
 Figure shows an example: a family relation.
 The fact that Tom is a parent of Bob can be written
in Prolog as:
parent( tom, bob).
Here we choose parent as the name of a relation;
tom and bob are its arguments.
 parent( pam, bob).
parent( tom, bob).
parent( tom, liz).
parent( bob, ann).
parent( bob, pat).
parent( pat, jim).
 This program consists of six clauses. Each of these
clauses declares one fact about the parent relation
 When this program has been communicated to the
Prolog system, Prolog can be posed some questions
about the parent relation.
For example: Is Bob a parent of Pat?
This question can be communicated to the Prolog
system by typing into the terminal:
 ?- parent( bob, pat)
Yes
 ?- parent( liz, pat).
no
 because the program does not mention anything about Liz
being a parent of Pat. It also answers 'no' to the question
 ?- parent( tom, ben).
because the program has not even heard of the name Ben.
For example: Who is Liz's parent?
 ?- parent( X, liz).
Prolog's answer will not be just 'yes' or'no' this time. Prolog
will tell us what is the (yet unknown) value of X such that the
above statement is true.
 answer is: X: tom
The question Who are Bob's children? can be
communicated to Prolog as:
 ?- parent( bob, X).
This time there is more than just one possible answer. Prolog
first answers with one solution:
X:ann
 We may now want to see other solutions. We can say that to
Prolog (in most Prolog implementations by typing a
semicolon), and Prolog will find other answers:
 X - pat
 Our program can be asked an even broader question:
Who is a parent of whom? Another formulation of
this question is:
 Find X and Y such that X is a parent of Y.
 This is expressed in Prolog by:
?- parent (X, Y).
The answers are output as:
 X-pam
Y : bob;
 X:tom
Y : bob;
 X:tom
Y : liz;
 Who is a parent of Jim? Assume that this is some Y.
 Who is a parent of Y? Assume that this is some X.
?- parent( Y, jim), parent( X, Y).
The answer will be:
 X:bob Y-pat

Who are Tom's grandchildren?


 ?- parent( tom X), parent( X, Y).
Prolog's answers are:
 X:bob Y : ann;

X:bob Y: pat
 Do Ann and Pat have a common parent?
This can be expressed again in two steps:
 Who is a parent, X, of Ann ?
 Is (this same) X a parent of Pat ?
 The corresponding question to Prolog is then:
 ?- parent( X, ann), parent( X, pat).
The answer is:
 X:bob
 Assuming the parent relation as defined in this
section what will be Prolog's answers to the
folowing questions?
a). parent( jim, X).
b). parent( X, jim).
c). parent( paffi, X), parent( X, pat).
d). parent( paffi, X), parent( X, Y), parent( Y, jim).
 Formulate in Prolog the following questions about
the parent relation:
(a) Who is Pat's parent?
(b) Does Liz have a child?
(c) Who is Pat's grandparent?
 Representation of list.
 The list is a simple data structure widely used in
non-numeric programming. A list is a sequence of
any number of items, such as ann, tennis, tom,
cricket.
 Such a list can be written in Prolog as:
 [ ann, tennis, tom, cricket]
 the first item, called the head of the list;
 the remaining part of the list, called the tail.
For our example :
 [ann, tennis, tom, cricket]
 the head is ann and the tail is the list
[ tennis, tom, cricket]
 How can a list be represented as a standard Prolog
object?
 We have to list is either empty or non-empty. In
the first case, the list is simply written as a Prolog
atom, [].
 In the second case, the list can be viewed as
consisting of two things.
For example:
 List= [a,b,c]
 Then we could write
 tail = [b,c] and L = .( a, Tail)
 To express this in the square bracket notation for
lists,
 Prolog provides another notational extension, the
vertical bar, which separates the head and the tail:
 List=[ a | Tail].
 The vertical bar notation is in fact more general: we
can list any number of elements followed by 'l' and
the list of remaining items.
 Thus alternative ways of writing the above list are:
 [a,b,c] = [a | [b,c] ] = [a,b | [c] ] =[a,b,c | [] ]
Some operations on list
Membership
 Let us implement the membership relation as
 member( X, L)
 where X is an object and L is a list. The goal member (X, L) is
true if X occurs in L.
For example,
 member( b, [a,b,c] )
 is true
 member( b, [a,[b , c]] )
 is not true, but
 member ( [b, c], [a,[b , c]] )
 The program for the membership relation can be
based on the following observation:
 X is a member of L if either
(1) X is the head of L, or
(2) X is a member of the tail of L.
 This can be written in two clauses, the first is a
simple fact and the second is a rule:
 member( X, [X I Tail] ).
member( X, [Head I Tail] ) :-member( X, Tail).
Concatenation
 For concatenating lists we will define the relation
 conc( Ll, L2,L3)
 Here Ll andL2 are two lists, and L3 is their
concatenation.
For example
 conc( [a,b], [c,d], [a,b,c,d] )
 is true, but
 conc( [a,b], [c,d], [a,b,a,c,d]
false
 the concatenation of [x I L1] and some list L2. The
result of the concatenation is the list [X I L3] where
L3 is the concatenation of L1 and L2.
ln prolog this is written as:
 conc( [X I L1], L2, [X I L3] ) :-conc( L1, L2,L3).
 This program can now be used for concatenating
given lists, for example:
 ?- conc( [a,b,c], [ 1,2,3], L).
 L= [a,b,c,1,2,3]
 ?- conc( [a,[b,c],d], [a,[] ,b], L).
L = [a, [b,c], d, a, [], b].
size([],0).
size([_|T], N):-size(T,N1) , N is N1+1.
evenlength:-
write('even elements in the list').
oddlength:-
write('odd elements in the list').
oddeven([H|T]):-
length(T,L),
L>=0 ->
(
L1 is L+1,
L2 is mod(L1,2),
L2=:=0->
evenlength;
oddlength ).
 sumlist([],0).
 sumlist([H|T],N):-sumlist(T,N1),N is N1+H.
 size([],0).
 size([_|T], N):-size(T,N1) , N is N1+1.
 sum(X,Y):-S is X+Y, write(S).
cube
 cube(N, C):- C is N*N*N.
THANK YOU

You might also like