1. EXP NO. 1
STUDY OF PROLOG, its insatllation procedure and working.
Prolog – Programming in Logic
PROLOG stands for Programming In Logic – an idea that emerged in the early 1970s to use logic
as programming language. PROLOG is a programming language centered around a small set of
basic mechanisms, including pattern matching, tree-based data structuring and automatic
backtracking.
FACTS , RULES AND QUERIES
Programming in PROLOG is accomplished by creating a database of facts and rules about objects,
their properties, and their relationships to other objects. Queries then can be posed about the objects
and valid conclusions will be determined and returned by the program.
For example:
1. Facts : Some facts about family relationships could be written as :
sister(sue, bill)
parent(ann, sam)
parent(joe,ann)
male(joe)
female(ann)
2. Rules : To represent the general rule for grandfather , we write :
Grandfather(X,Z):-
parent(X,Y),
parent(Y,Z),
male(X).
3. Queries : Given a data of facts and rules such as that above, we mat make queries by tying after
a query symbol ‘?_’ statements such as :
?_parent(X,sam)
X=ann
?_male(joe)
true
?_grandfather(X,Y)
X=joe;
Y=sam
?_female(joe)
false
Installation & Working with Prolog:
1. Download swi-prolog stable version
2. Run the installer
3. After SWI-Prolog has been installed on a Windows system, the following important new
things are available to the user:
➢ A folder (called directory in the remainder of this document) called swipl containing
the executables, libraries, etc., of the system. No files are installed outside this
directory.
➢ A program swipl-win.exe, providing a window for interaction with Prolog. The
program swipl.exe is a version of SWI-Prolog that runs in a console window.
➢ The file extension .pl is associated with the program swipl-win.exe. Opening a .pl file
will cause swipl-win.exe to start, change directory to the directory in which the file to
open resides, and load this file.
4. Create a notepad file with extension <filename>.pl, which consists of set of procedures
and each procedure consists of clauses. There are two types of clauses: facts and rules.
Order of these clauses are important.
2. ➢ facts: That does not depend on any other information. A fact must start with a
predicate (which is an atom) and end with a fullstop.
➢ predicates: That provide information about individuals.A predicate can take any fixed
number of ARGUMENTS (parameters).
➢ The arguments can be atoms (in this case, these atoms are treated as constants),
numbers, variables or lists. Arguments are separated by commas.
The number of arguments that a predicate takes is called its ARITY ( like unary,
binary, ternary, and the like). Two distinct predicates can have the same name if they
have different arities; thus you might have both:
-- mother(melody), meaning Melody is a mother, and
-- mother(melody,sharon), meaning Melody is the mother of Sharon.
In a Prolog program, a presence of a fact indicates a statement that is true. An absence
of a fact indicates a statement that is not true.
➢ rules: the real power of Prolog is in rules. While facts state the relation explicitely,
rules define the relation in a more general way. Each rule has its head - name of the
defined relation, and its body - a real definition of the relation. A rule can be viewed as
an extension of a fact with added conditions that also have to be satisfied for it to be
true. In other words, 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.
➢ Goals: A goal is a statement starting with a predicate and probably followed by its
arguments. In a valid goal, the predicate must have appeared in at least one fact or
rule in the consulted program, and the number of arguments in the goal must be the
same as that appears in the consulted program. Also, all the arguemnts (if any) are
constants.
The purpose of submitting a goal is to find out whether the statement represented by
the goal is true according to the knowledge database (i.e. the facts and rules in the
consulted program).
➢ Queries: A query is a statement starting with a predicate and followed by its
arguments, some of which are variables. Similar to goals, the predicate of a valid
query must have appeared in at least one fact or rule in the consulted program, and
the number of arguments in the query must be the same as that appears in the
consulted program.
The purpose of submitting a query is to find values to substitute into the variables in
the query such that the query is satisfied. This is similar to asking a question, asking
for "what values will make my statement true".
5. In prolog window run [<filename>]. or consult [<filename>]. Result should be 'true'. If file
doesnot exist there, it will result 'false'].
if you want to enter fully qualified file name, then enter ['path..to..file/filename.pl'].
6. (dot). mark the end of command.
7. listing. displays the content of loaded program.
8. use command 'halt' with a . (dot) to stop the prolog system.
3. EXP NO. 2
Write a prolog code having facts and rules about students and the courses they are studying.
Query about the connection among students of different courses.
PROLOG CODE:
/* Facts */
studies(kanika,cse).
studies(niharika,cse).
studies(shivani,cse).
studies(rakhi,cse).
studies(chanchal, cse).
studies(akram, cse).
studies(alka, cse).
studies(shabistan, cse).
studies(prince, ece).
/* Rules */
batchmate(X,Y): studies(X,Z),studies(Y,Z).
OUTPUT
4. EXP NO. 3
Find all paths between any two vertices in the given graph:
PROLOG CODE:
/* facts: */
edge(1,2).
edge(1,3).
edge(1,4).
edge(2,3).
edge(2,5).
edge(3,4).
edge(3,5).
edge(4,5).
/* rules: */
connected(X,Y) : edge(X,Y) ; edge(Y,X).
path(A,B,Path) :
travel(A,B,[A],Q),
reverse(Q,Path).
travel(A,B,P,[B|P]) :
connected(A,B).
travel(A,B,Visited,Path) :
connected(A,C),
C == B,
not(member(C,Visited)),
travel(C,B,[C|Visited],Path).
OUTPUT
5. EXP NO. 4
"MURDER MYSTERY" Problem
Alice, her husband, son, daughter, and brother are involved in a murder. One of the five killed
one of the other four. Who was the killer?
1. A man and a woman were together in the bar at the time of the murder.
2. The victim and the killer were together on the beach at the time of the murder.
3. One of the children was alone at the time of the murder.
4. Alice and her husband were not together at the time of the murder.
5. The victim's twin was innocent.
6. The killer was younger than the victim.
PROLOG CODE
/* Facts */
person(alice).
person(husband).
person(son).
person(daughter).
person(brother).
child(son).
child(daughter).
male(husband).
male(son).
male(brother).
female(alice).
female(daughter).
twin(alice, brother).
twin(brother, alice).
twin(son, daughter).
twin(daughter, son).
/* Rules */
istwin(X) : twin(X, _).
older(alice, son).
older(alice, daughter).
older(husband, son).
older(husband, daughter).
inbar(M, N) : person(M), person(N),
male(M), female(N).
together(S, T) : S=alice, T=husband.
together(S, T) : T=alice, S=husband.
alone(P) : person(P), child(P).
/* Rule Combining Clues */
solution(Killer, Victim, InBarA, InBarB, Alone) :
person(Killer), person(Victim),
7. EXP NO. 5
Missionaries and cannibals. Suppose 3 missionaries and 3 cannibals are walking together
through the forest. They arrive at a river they have to cross, but there is only one boat, and
that boat can carry at most 2 people. Of course, for the boat to cross the river, there should be
at least one person (missionary or cannibal) in the boat (to row the boat). The problem is that
if there are more cannibals than missionaries at any place, they will eat the missionaries.
Write a program that finds a strategy for the six people to cross the river without a missionary
being eaten.
PROLOG CODE
start([3,3,left,0,0]).
goal([0,0,right,3,3]).
legal(CL,ML,CR,MR) :
/* is this state a legal one? */
ML>=0, CL>=0, MR>=0, CR>=0,
(ML>=CL ; ML=0),
(MR>=CR ; MR=0).
/* Possible moves: */
move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):
/* Two missionaries cross left to right. */
MR2 is MR+2,
ML2 is ML2,
legal(CL,ML2,CR,MR2).
move([CL,ML,left,CR,MR],[CL2,ML,right,CR2,MR]):
/* Two cannibals cross left to right. */
CR2 is CR+2,
CL2 is CL2,
legal(CL2,ML,CR2,MR).
move([CL,ML,left,CR,MR],[CL2,ML2,right,CR2,MR2]):
/* One missionary and one cannibal cross left to right. */
CR2 is CR+1,
CL2 is CL1,
MR2 is MR+1,
ML2 is ML1,
legal(CL2,ML2,CR2,MR2).
move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):
/* One missionary crosses left to right. */
MR2 is MR+1,
ML2 is ML1,
legal(CL,ML2,CR,MR2).