Prolog, programming in logic is used in logic programming
and various other places like expert system design.The prolog was developed by
Robert Kowalski, Maarten van Emden and Alain Colmerauer.Warren contributed to
the development of prolog.
This book uses SWI prolog for demonstrating code, which is developed in
University of Amsterdam.
The fact are represented as
fact(param,...)
For example
brother(James,Tim).
indicates that James is the brother of Tim.
The first example is a set of facts related to brother as
brother(james,tim).
brother(barack,george).
brother(bob,mike).
brother(Diana,Earl).
be
Consult the file and the following message will be displayed.
1 ?-
% c:/brother.pl compiled 0.00 sec, 1,192 bytes
1 ?-
The following questions can be asked.
1 ?- brother(X,mike).
X = bob
Yes
whose answers are in constant. The query posted is "who is the brother of mike"
and the answer is bob which satisfies the facts above. The other types of
questions are like
1 ?-
| brother(bob,mike).
Yes
2
whose answers are in yes or no.
There are rules in prolog which are used to describe relations between
variables. Rules are define as
pred(X,Y):- parent(X,Z),pred(Z,Y).
These first rules says that X is pred of Y if X is parent of Y. The second
rules says that X is pred of Y if X is parent of some Z and that Z is pred of
Y.
The following file can be consulted.
parent(,).
parent(michelle,sasha).
parent(Charles,williams).
parent(iop.klm).
pred(X,Y):- parent(X,Y).
pred(X,Y):- parent(X,Z),pred(Z,Y).
The follwoing types of questions can be asked.
1 ?- pred(abc,xyz).
Yes
which answer in yes or no and other
7 ?-
pred(X,xyz).
X = abc
Yes
The first item of list is head of list.The rest of items are
called tail of list. Various operations on list are possible. For example
whether a element is a member of list.
member(X,[X |Tail]).
member(X,[Head| Tail]):- member(X,Tail).
The first rule says that if first member is X of list is the member we as
searching then membership goal is reached.The second rules says if Head is not
the member we are searching then apply again the rule for tail.
The rules are applied to following. % c:/member.pl compiled 0.00 sec, 828 bytes
1 ?-
| member(a,[a,b,c]).
Yes
a is the member of list [a,b,c] so is b and c but not d.
2 ?- member(b,[a,b,c]).
Yes
3 ?- member(c,[a,b,c]).
Yes
4 ?-
Yes
5 ?- member(d,[a,b,c]).
No
The answer to which are the members of list a, b,d is :
7 ?- member(X,[a,b,d]).
X = a ;
X = b ;
X = d ;
No
Similary be made for add and delete.
Add a member is add(X,L,[X|L]).
Cut
X(a,b):=A(a,z)!B(z,c) is a cut used to control bactracking.it It will not backing to other once it find one satisfied .
Various arithmetic operators are predefined for example
These include +,-,*,/ and examples include
1 ?- X is 2 + 5.
X = 7
Yes
2 ?- X is 4 -3 .
X = 1
Yes 3 ?- X is 7 * 3.
X = 21
Yes 4 ?- X is 4 /3 . X = 1.33333 Yes
Structures are other useful tools for storing information.
They are similar to record in table in databases.
For example people with their favorite tv station name.
ft(person(james,abc)).
ft(person(hillary,cnn)).
ft(person(johnson,fox)).
ft(person(rice,fox)).
The following queries can be asked
2 ?- ft(person(X,Y)).
X = james
Y = abc ;
X = hillary
Y = cnn ;
X = johnson
Y = fox ;
X = rice
Y = fox ;
No
Example of other queries include
3 ?- ft(person(james,X)).
X = abc
Yes
and
4 ?- ft(person(X,fox)).
X = johnson ;
X = rice ;
No
5 ?-
read(X)
reads from input and store it to variable X. write(X) write data to output. 1
?- read(X).
|: 34
|: .
X = 34
Yes
and 8 ?- write(34).
34
Y es
1 Assert(a) adds a to the knowledge base. retract(a) makes a false. For example
23 ?- assert(crisis).
Yes
24 ?- crisis
| .
Yes
25 ?- retract(crisis).
Yes
26 ?- crisis.
No
1.Ivan Bratko. Prolog programming language Pearson Education
![]()
![]()