0% found this document useful (0 votes)
136 views20 pages

Practical 1 Aim: - Study of PROLOG Software.: Using Turbo Prolog Topics

The document provides an overview of using the Turbo Prolog software. It discusses that Prolog is a logical programming language used for AI programming. It describes the basic GUI of Turbo Prolog including the editor, dialog, message, and trace panels. It then explains the basics of Prolog programming including facts, rules, queries, variables, constants, and predicates. It provides an example of a simple Turbo Prolog program and describes how to execute it by specifying goals.

Uploaded by

Ck
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views20 pages

Practical 1 Aim: - Study of PROLOG Software.: Using Turbo Prolog Topics

The document provides an overview of using the Turbo Prolog software. It discusses that Prolog is a logical programming language used for AI programming. It describes the basic GUI of Turbo Prolog including the editor, dialog, message, and trace panels. It then explains the basics of Prolog programming including facts, rules, queries, variables, constants, and predicates. It provides an example of a simple Turbo Prolog program and describes how to execute it by specifying goals.

Uploaded by

Ck
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Practical 1

Aim: - Study of PROLOG Software.

Using Turbo Prolog


Topics:
a) Basics of Turbo Prolog
b) Intro to Prolog programming
c) Running a simple program
 Prolog is a logical programming language and stands for
PROgramming in Logic.
 Created around 1972
 Preferred for AI programming and mainly used in such areas as:
Theorem proving, expert systems, NLP, etc.
 Logical programming is the use of mathematical logic for computer programming. To
start Turbo Prolog, open a MSDOS window and type: N> prolog followed by a carriage
return.
The GUI:
• GUI is composed of four panels and a main menu bar.
• The menu bar lists six options – Files, Edit, Run, Compile, Options, Setup.
• The four panels are Editor, Dialog, Message and Trace.
MENU
• Files – Enables the user to load programs from disk, create new programs, save
modified programs to disk, and to quit the program.
• Edit – Moves user control to the Editor panel.
• Run – Moves user control to the Dialog panel; compiles the user program (if not
already done so) in memory before running the program.
• Compile – Provides the user with choices on how to save the compiled version of the
program.
• Options – Provides the user with choices on the type of compilation to be used.
• Setup – Enables the user to change panel sizes, colors, and positions.
Editor--Simple to use editor with support for common editing tasks.

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.

• There are three categories of statements in Prolog:

 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.

What is a Prolog 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.

parent (john, bart). parent


(barbara, bart). male
(john).
dog(fido). >> Fido is a dog or It is true that fido is
a dog sister(mary, joe). >> Mary is Joe’s sister.
play(mary, joe, tennis). >> It is true that Mary and Joe play tennis.

 Relationships can have any number of objects.


 Choose names that are meaningful – because in Prolog names are arbitrary strings but
people will have to associate meaning to them.
Facts…
Syntax rules:
1. The names of all relationships and objects must begin with a lower case letter. For
example: likes, john, rachel.

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.

3. The arguments of a predicate can either be names of objects (constants) or variables.

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.

Constants & Variables

• Constants are names that begin with lower case letters.


• Names of relationships are constants

Variables
• Variables take the place of constants in facts.
• Variables begin with upper case letters.

Turbo Prolog Program


A Turbo Prolog program consists of two or more sections.
Clauses Section
• The main body of the prolog program.
• Contains the clauses that define the program – facts and rules.

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).

Executing Simple Program


• Start Turbo Prolog
• Select the Edit mode
• Type in the program
• Exit the Editor using Esc.
• Save the program

• Select Run (which compiles the program for you in memory)

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.

Program to demonstrate a simple prolog program.

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

Steps for 8-queen problem:


STEP 1 : Represent the board positions as 8*8 vector , i.e., [1,2,3,4,5,6,7,8]. Store
the set of queens in the list ‘Q’.
STEP 2 : Calculate the permutation of the above eight numbers stored in set
P.
STEP 3 : Let the position where the first queen to be placed be (1,Y), for
second be (2,Y1) and so on and store the positions in Q.
STEP 4 : Check for the safety of the queens through the predicate , ‘noattack ()’.
STEP 5 : Calculate Y1-Y and Y-Y1. If both are not equal to Xdist , which is the
X – distance between the first queen and others, then go to Step 6 else go to
Step 7.
STEP 6 : Increment Xdist by 1.
STEP 7 : Repeat above for the rest of the queens , until the end of the list is reached .
STEP 8 : Print Q as answer .
STEP 9 : Exit.

Write a program for 8-queen problem


domains
H=integer
T=integer*
predicates
safe(T)
solution(T)
permutation(T,T)
del(H,T,T)
noattack(H,T,H)
clauses
del(I,[I|L],L). /*to take a position from the permutation
of list*/ del(I,[F|L],[F|L1]):-
del(I,L,L1).
permutation([],[]). /*to find the possible positions*/
permutation([H|T],PL):-
permutation(T,PT),\
del(H,PL,PT).
solution(Q):- /*final solution is stored in Q*/
permutation([1,2,3,4,5,6,7,8],Q),
safe(Q).
safe([]). /*Q is safe such that no queens attack each
other*/ safe([Q|others]):-
safe(others),
noattack(Q,others,1).
noattack(_,[],_). /*to find if the queens are in same row,
column or diagonal*/
noattack(Y,[Y1|Ydist],Xdist):-
Y1-Y<>Xdist,
Y-Y1<>Xdist,
dist1=Xdist,
noattack(Y,Ydist,dist1).

Output:-
goal: -solution(Q).
Q=[“3”,”8”,”4”,”7”,”1”,”6”,2”,”5”]
Practical 3
Aim: -Write a program to solve traveling salesman problem.

The following is the simplified map used for the prototype:

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:-

You might also like