Practical 1 Aim: - Study of PROLOG Software.: Using Turbo Prolog Topics
Practical 1 Aim: - Study of PROLOG Software.: Using Turbo Prolog Topics
Function Command
Character left/right left arrow/right arrow
Word left/right Ctrl-left arrow/Ctrl-right arrow
Line up/down up arrow/down arrow
Page up/down PgUp/PgDn
Beginning/End of line Home/End
Delete character Backspace/Delete
Delete line Ctrl-Y
Search Ctrl-QF
Replace Ctrl-QA
Dialog
• When a Prolog program is executing, output will be shown in the Dialog Panel
Message
• The Message Panel keeps the programmer up to date on processing activity.
Trace
• The Trace Panel is useful for finding problems in the programs you create.
Prolog Clauses
Any factual expression in Prolog is called a clause.
There are two types of factual expressions: facts and rules.
Facts: Those are true statements that form the basis for the knowledge base.
Rules: Similar to functions in procedural programming (C++, Java…) and has the
form of if/then.
Queries: Questions that are passed to the interpreter to access the knowledge base
and start the program.
Prolog is used for solving problems that involve objects and the relationships between
objects.
A program consists of a database containing one or more facts and zero or more
rules(next week).
A fact is a relationship among a collection of objects. A fact is a one-line statement that
ends with a full-stop.
2. The relationship is written first, and the objects are written separated by commas, and
the objects are enclosed by a pair of round brackets.
3. The character ‘.’ must come at the end of each fact.
Terminology:
1. The names of the objects that are enclosed within the round brackets in each fact are
called arguments.
2. The name of the relationship, which comes just before the round brackets, is called the
predicate.
4. When defining relationships between objects using facts, attention should be paid to
the order in which the objects are listed. While programming the order is arbitrary,
however the programmer must decide on some order and be consistent.
5. Ex. likes(tom, anna). >> The relationship defined has a different meaning if the order of
the objects is changed. Here the first object is understood to be the “liker”. If we
wanted to state that Anna also likes Tom then we would have to add to our database –
likes(anna, tom).
6. Remember that we must determine how to interpret the names of objects and
relationships.
Variables
• Variables take the place of constants in facts.
• Variables begin with upper case letters.
Predicates Section
• Predicates (relations) used in the clauses section are defined.
• Each relation used in the clauses of the clauses section must have a corresponding
predicate definition in the predicates section. Except for the built in predicates of Turbo
Prolog.
Turbo Prolog requires that each predicate in the predicate section must head at least
one clause in the clauses section.
A predicate definition in the predicates section does not end with a period.
Predicate definitions contain different names than those that appear in the clauses
section. Make sure that the predicate definition contains the same number of names as
the predicate does when it appears in the clauses section.
A Turbo Prolog may also have a domains section. In this section the programmer can define
the type of each object.
Examples:
Clauses Section – likes(tom, anna).
Predicates Section – likes(boy, girl)
Domains Section – boy, girl =
symbol
It is possible to omit the domains section by entering the data types of objects in the
predicates section. likes(symbol,symbol)
However, this might make the program harder to read especially if the predicate
associates many objects.
Simple Program:
domains
disease,indication = symbol
predicates
symptom(disease, indication)
clauses
symptom(chicken_pox, high_fever).
symptom(chicken_pox, chills).
symptom(flu, chills).
symptom(cold, mild_body_ache).
symptom(flu, severe_body_ache).
symptom(cold, runny_nose).
symptom(flu, runny_nose).
symptom(flu, moderate_cough).
Once you have followed these steps you will see the following prompt in the Dialog Panel:
Goal:
Using a Prolog program is essentially about asking questions. To ask the executing Prolog
program a question you specify the Goal.
Ex -
Goal: symptom(cold, runny_nose)
True
Goal:
Turbo Prolog will respond with True and prompt for
another goal. Possible outcomes of specifying a goal:
1. The goal will succeed; that is, it will be proven true.
2. The goal will fail; Turbo Prolog will not be able to match the goal with any facts in the
program.
3. The execution will fail because of an error in the program.
Execution is a matching process. The program attempts to match the goal with one of the
clauses in the clauses section beginning with the first clause. If it does find a complete match,
the goal succeeds and True is
displayed. In Prolog, False indicates a failure to find a match using the current database – not
that the goal is untrue.
Variables Revisited
Variables are used in a clause or goal to specify an unknown quantity. Variables enable the
user to ask more informative questions. For example, if we wanted to know for which
diseases, runny_nose was a symptom – type in
Goal: symptom(Disease, runny_nose).
Turbo Prolog will respond
Disease = cold
Disease = flu
2 Solutions Goal:
To find the two solutions Turbo Prolog began at the start of the clauses section and tried to
match the goal clause to one of the clauses. When a match succeeded, the values of the
variables for the successful match was displayed. Turbo prolog continued this process until it
had tested all predicates for a match with the specified goal.
If you wish Prolog to ignore the value of one or more arguments when determining a goal’s
failure or success then you can use the anonymous variable “_” (underscore character).
Ex -
Goal: symptom(_,chills).
True
Goal:
Matching
Two facts match if their predicates are the same, and if their corresponding arguments are the
same.
When trying to match a goal that contains an uninstantiated variable as an argument,
Prolog will allow that argument to match any other argument in the same position in the
fact.
If a variable has a value associated with a value at a particular time it is
instantiated otherwise it is uninstantiated.
Heuristics
A method to help solve a problem, commonly informal.
It is particularly used for a method that often rapidly leads to a solution that is usually
reasonably close to the best possible answer.
Heuristics are "rules of thumb", educated guesses, intuitive judgments or simply
common sense.
predicates
like(symbol,symbol)
hate(symbol,symbol)
clauses
like(sita,ram)
. like(x,y).
like(a,b).
hate(c,d).
hate(m,n)
.
hate(f,g).
Output:-
Practical 2
Aim: - Write a program to solve 8 queens problem
Output:-
goal: -solution(Q).
Q=[“3”,”8”,”4”,”7”,”1”,”6”,2”,”5”]
Practical 3
Aim: -Write a program to solve traveling salesman problem.
Production Rules:-
route(Town1,Town2,Distance)
road(Town1,Town2,Distance).
route(Town1,Town2,Distance)
road(Town1,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2,
domains
town = symbol
distance = integer
predicates
nondeterm road(town,town,distance)
nondeterm
route(town,town,distance)
clauses
road("tampa","houston",200).
road("gordon","tampa",300).
road("houston","gordon",100).
road("houston","kansas_city",120).
road("gordon","kansas_city",130).
route(Town1,Town2,Distance):-
road(Town1,Town2,Distance)
. route(Town1,Town2,Distance):-
road(Town1,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2,
!.
goal
route("tampa", "kansas_city", X),
write("Distance from Tampa to Kansas City is ",X),nl.
Distance from Tampa to Kansas City is 320 X=320
Practical 4
Aim: - Write a Program to add two numbers.
predicates
add
clauses
add:-write("input first number"),
readint(X),
write("input second number"),
readint(Y),
Z=X+Y,write("output=",Z).
Output:-
Practical 5
Aim: - Write a Program to categorize animal characteristics.
predicates
small(symbol
)
large(symbol)
color(symbol,symbol)
clauses
small(rat).
small(cat).
large(lion).
color(dog,black).
color(rabbit,white)
. color(X,dark):-
color(X,black);color(X,brown).
Output:-
Practical 6
Aim: - Write a Program to read address of a person using compound variable.
domains
person=address(name,street,city,state,zip
) name,street,city,state,zip=String
predicates
readaddress(person)
go
clauses
go:
-
readaddress(Address),nl,write(Address),nl,nl,write("Accept(y/n)?"),readchar(Reply),Reply='y',!
. go:-
nl,write("please re-enter"),nl,go.
readaddress(address(N,street,city,state,zip)):
-write("Name:"),readln(N),
write("Street:"),readln(street),
write("City:"),readln(city),
write("State:"),readln(state),
write("Zip:"),readln(zip).
Output:-
Practical 7
Aim: - Write a Program of fun to show concept of cut operator.
predicates
fun(integer,integer)
clauses
fun(Y,1):-Y<3,!.
fun(Y,2):-Y>3,Y<=10,!.
fun(Y,3):-Y>10,!.
Output:-
Practical 8
Aim: - Write a Program to count number of elements in a list .
domains
x=integer
list=integer*
predicates
count(list,x
)
clauses
count([],0).
count([_|T],N):-count(T,N1),N=N1+1.
Output:-
Practical 9
Aim: - Write a Program to reverse the list .
domains
x=integer
list=integer*
predicates
append(x,list,list)
rev(list,list)
clauses
append(X,[],[X]).
append(X,[H|T],[H|T1]):-
append(X,T,T1). rev([],[]). rev([H|T,rev):-
rev(T,L),append(H,L,rev).
Output:-
Practical 10
Aim: - Write a Program to replace an integer from the list.
domains
list=integer*
predicates
replace(integer,integer,list,list)
clauses
replace(X,Y,[X|T],[Y|T]).
replace(X,Y,[H|T],[H|T1]):-replace(X,Y,T,T1).
Output:-
Practical 11
Aim: - Write a Program to delete an integer from the list.
domains
list=integer*
predicates
del(integer,list,lis)
clauses
del(X,[X|T],T)
.
del(X,[H|T],[H|T1]):-
del(X,T,T1).
Output:-
Practical 12
Aim: - Write a Program to show concept of list.
domains
name=symbol
*
predicates
itnames(name
)
clauses
itnames([ram,kapil,shweta])
.
itnames([ram,shweta,kapil])
.
Output:-
Practical 13
Aim: - Write a Program to demonstrate family relationship.
predicates
parent(symbol,symbol)
child(symbol,symbol)
mother(symbol,symbol)
brother(symbol,symbol)
sister(symbol,symbol)
grandparent(symbol,symbol)
male(symbol)
female(symbol)
clauses
parent(a,b)
.
sister(a,c).
male(a).
female(b).
child(X,Y):-parent(Y,X). mother(X,Y):-
female(X),parent(X,Y). grandparent(X,Y):-
parent(X,Z),parent(Z,Y). brother(X,Y):-
male(X),parent(V,X),parent(V,Y).
Output:-
Practical 14
Aim: - Write a Program to show how integer variable is used in prolog program
predicates
go
clauses
go:-X=10,
write(X),
nl,X=20,
write(X),nl.
Output:-