PROLOG
PROLOG
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
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.
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.
the body.
a conclusion part (the left hand side of the rule) is also called the
head.
The comma “,” stands for conjuction/and.
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