Вы находитесь на странице: 1из 34

PRACTICAL NO:- 1

AIM:- STUDY OF PROLOG


Prolog, which stands for programming in logic, is the most widely
available language in the logic programming area. The difference
between Prolog and other languages is that a Prolog program tells the
computer what to do (a technique called declarative programming) while
programs in other languages tell the computer how to do it (procedural
programming). Prolog does this by making deductions and derivations
from facts and rules stored in a database
1 Developed in 1972 by Alain Colmerauer and Philippe Roussel

1 Name comes from “PROgramming in LOGic

Prolog Structure

1 Prolog facts – a database of predicates and associations.


A fact is just what it appears to be --- a fact. A fact in everyday language
is often a proposition like ``It is sunny.'' or ``It is summer.'' In Prolog such
facts could be represented as follows:
'It is sunny'.
'It is summer'.

1 Prolog rules – define new predicates by using Prolog facts.


Rules extend the capabilities of a logic program. They are what give
Prolog the ability to pursue its decision-making process. The following
program contains two rules for temperature. The first rule is read as
follows: ``It is hot if it is summer and it is sunny.'' The second rule is read
as follows: ``It is cold if it is winter and it is snowing.''
'It is sunny'.
'It is summer'.
'It is hot' :- 'It is summer', 'It is sunny'.
'It is cold' :- 'It is winter', 'It is snowing'.
2 A Prolog program consists of a database of facts and rules, and
queries
 Fact: ... .
Page 1
 Rule: ... :- ... .
 Query: ?- ... .
 Variables: must begin with an upper case letter.
 Constants: numbers, begin with lowercase letter, or enclosed
in single quotes.
Prolog program is executed by asking a question. The question is called a
query. Facts, rules, and queries are called clauses.

Prolog Structure – Queries


1 A query searches the database for the first fact that satisfies its
goal.

1 If a fact is found it either unifies the variable with a constant or


returns yes.

1 If a fact is not found that meets that condition it returns no.


2 Use a semi-colon to request subsequent answers.

1 In other words, a semi-colon signifies disjunction.

1 A comma signifies conjunction

Prolog Structure - Unification


1 powerful pattern-matching facility
2 A query resolves by unifying all of its elements.
3 A constant unifies with itself and any variable.
4 Scope is limited to the rule in which a variable occurs.
5 When a variable is unified with a constant in a rule, all instances of
that variable in that rule are unified to that constant

Prolog Structure - Backtracking


1 In more complex examples, Prolog uses backtracking to find
possible solutions.
2 Prolog will attempt to resolve the first fact of its rule, unifying any
variables with the first constant that satisfies that fact
3 It then attempts to resolve the rest of that rules facts.
4 If it is unable to do so under those conditions it “backs up” and
tries again with the next available unifying constant.
5 Prolog rules can also be used write programs that do more than

Page 2
find the answers to simple database queries.
6 append([], L, L).
append([H|T], L, [H|L1]):-append(T, L, L1).
This will append a list to another list recursively.

CLAUSES:
In Prolog, a clause represents a statement about objects and relations
between objects
There are two kinds of clauses: rules and facts.

Rule contains both clause head and clause body

In Prolog, a clause represents a statement about objects and relations


between objects. Clauses represent the knowledge which the Prolog
system can draw upon when processing a query.

Operations Notatio
n
conjunction (AND, ,
comma)
disjunction (OR, ;
semicolon)
IF-THEN (arrow) ->
A rule represents conditional knowledge, i.e. it is an implication in the
form "clause head <== clause body" (the clause head is true if the clause
body is true). At the same time the clause head can be seen as the
activation point of a procedure which activates further procedures in the
clause body.
The most important operations between subgoals (conditions) in the
clause body are the following:
Fact is a Prolog clause without a clause body.
A fact represents unconditional knowledge, i.e. it describes a relation
between objects which always holds true, regardless of any other
relations. Thus, provided their clause heads are identical, a fact is
equivalent to a rule whose clause body always has the truth value TRUE.
The common clause name is called the predicate name or functor of the
predicate.
HORN CLAUSE: Set of statements joined by logical AND’s
DATA STRUCTURES : List structure is an important data structure in
PROLOG . The elements of the list are written between sqaure brackets

Page 3
separated by commas. Example
[apple,orange,mango,grapes]
HEAD AND TAIL OF LIST
The symbol | divides the list into two parts , the head and tail

PRACTICAL NO:- 2

Page 4
AIM:- WRITE A PROGRAM IN
PROLOG TO SOLVE THE
PROBLEM OF WATER JUG.

domains
A,B,C,D=integer
predicates
go
water(integer,integer)
clauses
go:-
clearwindow(),
write("Enter the quantity of 1st jug (max capacity=4):- "),
readint(A),
nl,
A<=4,
write("Enter the quantity of 2nd jug (max capacity=3):- "),
readint(B),
nl,
B<=3,
write(A," ",B),
nl,
water(A,B).

go:-
write("You have entered a wrong value").

water(2,0):-
write("Goal achieved").

water(2,D):-
write("2 - ",D),nl,
water(2,0).

water(0,2):-
write("0 - 2"),nl,
water(2,0).

Page 5
water(C,2):-
write(C," - 2"),nl,
water(0,2).

water(3,3):-
write("3 - 3"),nl,
water(4,2).

water(0,3):-
write("0 - 3"),nl,
water(3,3).

water(0,3):-
write("0 - 3"),nl,
water(3,0).

water(0,0):-
write("0 - 0"),nl,
water(0,3).

water(4,3):-
write("4 - 3"),nl,
water(0,3).

Output:-
Enter the quantity of 1st jug (max capacity=4):- 4
Page 6
Enter the quantity of 2nd jug (max capacity=3):- 2

42
0-2
2-0
Goal achievedYes
Goal:

PRACTICAL NO:- 3

Page 7
AIM:-Program to insert a number between
two lists to make one list.

domains
x=integer
list=integer*

predicates
append(x,list,list)
conlist(list,list,list)
join(x,list,list,list)

clauses
append(X,[],[X]).
append(X,[H|Tail],[H|L2]):-
append(X,Tail,L2).
conlist([],[],[]).
conlist([],L2,L2).
conlist(L1,[],L1).
conlist([H|Tail],L2,[H|L3]):-
conlist(Tail,L2,L3).
join(X,L1,L2,L3):- append(X,L1,L4), conlist(L4,L2,L3).

OUTPUT:-

Page 8
PRACTICAL NO:- 4

Page 9
AIM:- Program to determine type of
character entered.

domains

predicates
go
check(char)

clauses
go :- write("Enter a character : "),
readchar(X),nl,
write(X),
check(X).

check(X) :- X>='a',X<='z',nl,write("Lower Case Alphabet "),nl;


X>='A',X<='Z',nl,write("Upper Case Alphabet "),nl;
X>='0',X<='9',nl,write("Numeric Digit "),nl.

OUTPUT:-

Page 10
PRACTICAL NO :- 5

Page 11
AIM:- Program to count number of
elements in a list.

domains
x=integer
list=integer*

predicates
count(list,x)

clauses
count([],0).
count([_|Tail],N):-
count(Tail,N1),N=N1+1.

OUTPUT :-

Page 12
PRACTICAL NO:- 6

Page 13
AIM:- Program to delete an element
from a list.

domains
x = integer
list = integer*

predicates
del(x,list,list).

clauses
del(_,[],[]).
del(X,[H|Tail],[H|Nlist]):-
del(X,Tail,Nlist), Not(X=H).
del(X,[H|Tail],Nlist):-
del(X,Tail,Nlist), X=H.

OUTPUT:-

Page 14
PRACTICAL NO:- 7

Page 15
AIM:-Program to check whether a
element is member of a list or not.

domains
x = integer
list = integer*

predicates
member(x,list,x)

clauses
member(_,[],0).
member(X,[X|_],1).
member(X,[H|Tail],Y):-
member(X,Tail,Y), NOT(H = X).

OUTPUT :-

Page 16
PRACTICAL NO:- 8

Page 17
AIM:- Program to check username and
password.

domains
name,password = symbol

predicates
getentry(name,password)
logon(integer)
user(name,password)
go

clauses
go :- logon(3),
write("Logged Successfully"),nl.

logon(0) :- !,write("Tresspasses Not permitted.") ,


fail.

logon(_) :- getentry(Name,Password),
user(Name,Password).

logon(X) :- write("Illegal Entry "),nl,


XX = X-1,
logon(XX).

getentry(Name,Password) :- write("Enter Name : "),


readln(Name),nl,
write("Enter Password : "),
readln(Password),nl.
user("AI","PROLOG").

OUTPUT :-

Page 18
PRACTICAL NO :- 9
AIM:- Program to quick sort a list.
Page 19
domains

x = integer
list = integer*

predicates

qsort(list,list).
split(x,list,list,list).
conlist(list,list,list).

clauses

conlist([],[],[]).
conlist([],L2,L2).
conlist(L1,[],L1).
conlist([H|Tail],L2,[H|L3]):-
conlist(Tail,L2,L3).

split(_,[],[],[]).
split(X,[H|Tail],[H|LTHAN],GTHAN):-
split(X,Tail,LTHAN,GTHAN), H<X.
split(X,[H|Tail],LTHAN,[H|GTHAN]):-
split(X,Tail,LTHAN,GTHAN), H>=X.

qsort([],[]).
qsort([H|Tail],Sorted):-
split(H,Tail,Lthan,Gthan),
qsort(Lthan,Lsorted),
qsort(Gthan,Gsorted),
conlist(Lsorted,[H|Gsorted],Sorted).

OUTPUT :-

Page 20
Practical no:- 10
AIM:- Program to read from a file.
Page 21
domains
file = inputdata

predicates
go
readthefile

clauses
go :-write("Enter File Name to Read :"),
readln(Filename),
openread(inputdata,Filename),
readdevice(inputdata),
readthefile,
closefile(inputdata).

readthefile:- readchar(Data),
write(Data),
readthefile.

OUTPUT:-

Page 22
Practical no:- 11

AIM:- Program to reverse a list of


integers.
Page 23
domains
x = integer
list = integer*

predicates
append(x,list,list)
reverse(list,list).

clauses
append(X,[],[X]).
append(X,[H|Tail],[H|L2]):-
append(X,Tail,L2).

reverse([],[]).
reverse([H|Tail],REV):-
reverse(Tail,L),append(H,L,REV).

OUTPUT :-

Page 24
PRACTICAL NO:- 12

Page 25
AIM:- Program to reverse a string.

domains
S = string

predicates
reverse(S,S)

clauses
reverse("","").
reverse(S,REV) :- frontstr(1,S,F,R),
reverse(R,REV1),
concat(REV1,F,REV).

OUTPUT:-

Page 26
PRACTICAL NO:- 13

AIM:- Program for union of two sets.


Page 27
domains
list = integer*
x = integer

predicates
del(x,list,list)
union(list,list,list).

clauses
del(_,[],[]).
del(X,[H|Tail],[H|Nlist]):-
del(X,Tail,Nlist), Not(X=H).
del(X,[H|Tail],Nlist):-
del(X,Tail,Nlist), X=H.

/* union starts here */

union([],[],[]).
union([],L2,L2).
union(L1,[],L1).
union([H|Tail],L2,[H|L3]):-
del(H,L2,L4),del(H,Tail,L5),union(L5,L4,L3).

OUTPUT :-

Page 28
Practical no:- 14

Page 29
AIM:- Program for writing to a file.

domains
file = foutput

predicates
go

clauses
go:- openwrite(foutput,"five.txt"),
writedevice(foutput),
write("Working in Prolog 2.0 "),nl,
closefile(foutput),
writedevice(screen),
write("Written Successfully").

OUTPUT:-

Page 30
Practical no:- 15

Page 31
AIM:-WRITE A PROGRAM IN
PROLOG FOR DIAGNOSIS SYSTEM.
domains
disease,indication,name = symbol

predicates
hypothesis(name,disease)
symptom(name,indication)

clauses
symptom(amit,fever).
symptom(amit,rash).
symptom(amit,headache).
symptom(amit,runn_nose).

symptom(kaushal,chills).
symptom(kaushal,fever).
symptom(kaushal,hedache).

symptom(dipen,runny_nose).
symptom(dipen,rash).
symptom(dipen,flu).

hypothesis(Patient,measels):-
symptom(Patient,fever),
symptom(Patient,cough),
symptom(Patient,conjunctivitis),
symptom(Patient,rash).

hypothesis(Patient,german_measles) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,runny_nose),
Page 32
symptom(Patient,rash).

hypothesis(Patient,flu) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,body_ache),
symptom(Patient,chills).

hypothesis(Patient,common_cold) :-
symptom(Patient,headache),
symptom(Patient,sneezing),
symptom(Patient,sore_throat),
symptom(Patient,chills),
symptom(Patient,runny_nose).

hypothesis(Patient,mumps) :-
symptom(Patient,fever),
symptom(Patient,swollen_glands).

hypothesis(Patient,chicken_pox) :-
symptom(Patient,fever),
symptom(Patient,rash),
symptom(Patient,body_ache),
symptom(Patient,chills).

OUTPUT:-

Page 33
Page 34

Вам также может понравиться