8/3/2019 IS file
1/23
Program No.1
STUDY OF PROLOG
Prolog Programming in LogicPROLOG stands forProgramming In Logic an idea that emerged in the early1970s to use logic as programming language. The early developers of this ideaincluded Robert Kowalski at Edinburgh ( on the theoretical side ), Marrten vanEmden at Edinburgh (experimental demonstration ) and Alain Colmerauer atMarseilles ( implementation). David D.H.Warrens efficient implementation atEdinburgh in the mid 1970s greatly contributed to the popularity of PROLOG.
PROLOG is a programming language centered around a small set of basicmechanisms, including pattern matching , tree-based data structuring andautomatic backtracking. This small set constitutes a surprisingly powerful and
flexible programming framework. PROLOG is especially well suited for problemsthat involve objects in particular, structured objects and relations betweenthem .
SYMBOLIC LANGUAGEPROLOG is a programming language for symbolic , non numeric computation.It is especially well suited for solving problems that involve objects and relationsbetween objects .
For example , it is an easy exercise in prolog to express spatial relationship
between objects , such as the blue sphere is behind the green one . It is alsoeasy to state a more general rule : if object X is closer to the observer than objectY , and Y is closer than Z, then X must be closer than Z. PROLOG can reasonabout the spatial relationships and their consistency with respect to the generalrule . Features like this make PROLOG a powerful language for ArtificialLanguage (A1) and non numerical programming.
There are well-known examples of symbolic computation whose implementationin other standard languages took tens of pages of indestible code . When thesame algorithms were implemented in PROLOG, the result was a cryetal-clearprogram easily fitting on one page.
FACTS , RULES AND QUERIESProgramming in PROLOG is accomplished by creating a database of facts andrules about objects, their properties , and their relationships to other objects .Queries then can be posed about the objects and valid conclusions will bedetermined and returned by theprogram. Responses to user queries are determined through a form ofinferencing control
8/3/2019 IS file
2/23
known as resolution.
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 matmake queries by tying after a query symbol ?_ statements such as :
?_parent(X,sam)X=ann?_male(joe)yes?_grandfather(X,Y)X=joe, Y=sam?_female(joe)no
PROLOG in Designing Expert SystemsAn Expert System is a set of programs that manipulates encoded knowledge tosolveproblems in a specialized domain that normally requires human expertise. AnExpertsystems knowledge is obtained from the expert sources such as texts, journalsarticles,.databases etc. and coded in a form suitable for the system to use in its inferenceorreasoning processes. Once a sufficient body of Expert knowledge has beenacquired, itmust be encoded in some form, loaded into knowledge base, then tested, andrefinedcontinually throughout the life of the system.
PROLOG serves as a powerful language in designing expert systems because ofitsfollowing features:
8/3/2019 IS file
3/23
Use knowledge rather than data.
Modification of the knowledge base without recompilation ofcontrol programs.
Capable of explaing conclusions.
Symbolic computations resembling manipulations of natural
language. Reason with meta-knowledge.
META-PROGRAMMINGA meta-program is a program that other programs as data. Interpreters andcompilers are examples of meta-programs. Meta-interpreter is a particular kind ofmeta-program: an interpreter for a language written in that language. So aPROLOG meta-interpreter is an interpreter for PROLOG, itself written inPROLOG.Due to its symbol-manipulation capabilities, prolog is a powerful language for
meta-programming. Therefore, it is often used as an implementation language forother languages. PROLOG is particularly suitable as a language for rapidprototyping where we are interested in implementing new ideas quickly. Newideas are rapidly implemented and experimented with. In prototyping theemphasis is on bringing new ideas to life quickly and cheaply, so that they can beimmediately tested.
On the other hand, there is not much emphasis on efficiency of implementation.Once the ideas are developed, a prototype may have to be re-implemented,possibly in another, more efficient programming language. Even if this isnecessary, the prototype is useful because it usually helps to speed up the
creative development stage.
8/3/2019 IS file
4/23
Program No.2
W.A.P. to show the relationship in prolog system.
domains
person a,b,c,x,k,l,m,n=symbol
predicates
father(person,person)
mother(person,person)
brother(person,person)sister(person,person)
grandfather(person,person)
cousin(person,person)
clauses
brother(m,n):-father(x,m),father(x,n).
brother(l,k):-mother(b,l),mother(b,k).sister(b,a):-father(c,a),father(c,b).
grandfather(c,n):-father(c,a),father(a,n).
cousin(n,l):-father(a,n),mother(b,l),brother(a,b).
cousin(n,l):-mother(a,n),father(b,l),brother(a,b).
cousin(n,l):-father(a,n),mother(b,l),sister(a,b).
cousin(n,l):-mother(a,n),father(b,l),sister(a,b).
father(ram,ravi).
father(ram,kiran).father(ravi,prakash).
father(ravi,rani).
mother(kiran,kelly).
mother(kiran,prince).
son(ravi,ram).
8/3/2019 IS file
5/23
son(prakash,ravi).
son(prince,kiran).
daughter(kiran,ram).
daughther(rani,ravi).
daughter(kelly,kiran).grandfather(ram,prakash).
grandfather(ram,rani).
grandfather(ram,kelly).
grandfather(ram,prince).
sister(kiran,ravi)
8/3/2019 IS file
6/23
Output 2:
Goal:Father(X,A)
X=ram A=ravi
X=ram A=kiranX=ravi A=prakash
X=ravi A=rani
8/3/2019 IS file
7/23
Program No.3
W.A.P. to show the hypothesis using the symptoms.
domainsIndication,Patient,Disease=symbol
predicates
sym(Patient,Disease)
hypothesis(Patient,Disease)
clauses
hypothesis(Patient,commoncold):-
sym(Patient,headache),
sym(Patient,sneezing),sym(Patient,chills),
sym(Patient,soarthroat),
sym(Patient,runnynose).
hypothesis(Patient,flu):-
sym(Patient,fever),
sym(Patient,cough),
sym(Patient,conjuctivitis),
sym(Patient,rash),sym(Patient,runnynose).
hypothesis(Patient,chickenpox):-
sym(Patient,fever),
sym(Patient,chills),
sym(Patient,rash),
sym(Patient,bodyache).
hypothesis(Patient,whoopingcough):-
sym(Patient,cough),
sym(Patient,sneezing),sym(Patient,runnynose).
hypothesis(Patient,mumps):-
sym(Patient,fever),
sym(Patient,swollenglands).
hypothesis(Patient,measles):-
8/3/2019 IS file
8/23
sym(Patient,fever),
sym(Patient,cough),
sym(Patient,runnynose),
sym(Patient,rash),
sym(Patient,conjuctivitis).sym(charlie,fever).
sym(charlie,rash).
sym(charlie,headache).
sym(charlie,runnynose).
sym(charlie,cough).
sym(charlie,conjuctivitis).
sym(charlie,sneezing).
sym(charlie,chills).sym(charlie,sorethroat).
sym(charlie,bodyache).
sym(charlie,swollenglands).
8/3/2019 IS file
9/23
OUTPUT 3:-
Goal:hypothesis(Patient,Disease)
Patient=Charlie,Disease=commomcold
1 solution
8/3/2019 IS file
10/23
Program No.4
W.A.P.in Prolog implementing the List functions:
(a) Appending(b) last element in a list.
domains
Namelist=symbol*
predicates
Clubname(Namelist)
Append(Namelist,Namelist,Namelist)
Last(Namelist,symbol)
clauses
Clubname([n1,n2,n3]).Append([],ListB,ListB).
Append([X|List1],List2,[X|List3]):-
Append(List1,List2,List3).
last([Head],X):-X=Head.
last([_|Tail],X):-last(Tail,X)
8/3/2019 IS file
11/23
Output 4:-
Goal:clubname(X)
X=[n1,n2,n3]1 Solution
Goal:append([n1],[n2],X)
X=[n1,n2]
1 Solution
Goal:last([n1,n2],X)
X=n21 Solution
8/3/2019 IS file
12/23
Program No.5
W.A.P. to show the implementation of Cut and Fail Predicates.
domains
City,State=symbol
predicates
location(string,string)
go
chkstate(string)
go1
clauses
go1:-clearwindow.
go:-
writef("**********************"),nl,
writef("%-10 %5","CITY","STATE"),nl,
writef("**********************"),nl,
fail.go:-
location(City,State),
chkstate(State),nl,
writef("%-10 %5",City,State),nl,
fail.
go.
location("NEWDELHI","DELHI\n").
location("KOLKOTA","WEST BENGAL\n").
location("MUMBAI","MAHARASHTRA\n").location("CHENNAI","TAMILNADU\n").
chkstate("WEST BENGAL\n"):-
!,fail.
chkstate(_).
8/3/2019 IS file
13/23
Output 5:-
Goal:go
*************************
CITY STATE
NEWDELHI DELHI
MUMBAI MAHARASHTRA
CHENNAI TAMILNADU
*************************
yes
8/3/2019 IS file
14/23
Program No.6
W.A.P. to find the factorial of a number.
domains
N,N1,F,F1,NUM=integer
predicates
Fact(integer,integer)
clauses
Fact(0,1).
Fact(N,F):-
N>0,
N1=N-1,
Fact(N1,F1),
F=N*F1.
8/3/2019 IS file
15/23
Output 6:-
Goal:fact(5)
1201 solution
8/3/2019 IS file
16/23
Program No.7
W.A.P. to implement Fibonacci series using the Recursion rule.
domains
N,N1,N2,F,F1,F2=integer
predicates
Fib(integer,integer)
clauses
Fib(0,1).
Fib(1,1).
Fib(N,F):-N>1,
N1=N-1,
N2=N-2,
Fib(N1,F1),Fib(N2,F2),
F=F1+F2.
8/3/2019 IS file
17/23
Output 7:-
Goal:fib(5)
1
23
5
8
13
8/3/2019 IS file
18/23
Program No.8
W.A.P. to find the distance between two cities.
domains
X,Y=symbol.
Z=integer.
predicates
dis(symbol,symbol,integer).distance(symbol,symbol,integer).
clauses
distance(X,Y,Z):-dis(X,Y,Z).
distance(X,Y,Z):-dis(X,A,Z1),dis(A,Y,Z2),Z=Z1+Z2.
dis(a,b,100).dis(b,c,100).
dis(c,d,100).
dis(d,f,200).
dis(f,e,100).
dis(e,a,100).
dis(b,d,210).
dis(b,f,310).
dis(a,f,150).
8/3/2019 IS file
19/23
Output 8:-
Goal:distance(a,d)
3001 solution
8/3/2019 IS file
20/23
Program No.9
W.A.P.in Prolog implementing the input and output
predicates.
domains
Name,Address=string
Rollno,Phoneno=integer.
predicates
go
clauses
go:-
write("Enter the name"),nl,
readln(Name),
write("Enter the rollno:"),nlreadln(Rollno),
write("Enter the address:"),nl
readln(Address),
write("Enter the phoneno:"),nl
readln(Phoneno),
write(Rollno),nl,
write(Name),nl,
write(Address),nl,
write(Phoneno).
8/3/2019 IS file
21/23
OUTPUT 9:-
Goal:go
Enter the name:XYZ GGR
Enter the rollno:
8035
Enter the address:
Sec-10a Gurgaon
Enter the phone no:
09891409106
yes
8/3/2019 IS file
22/23
Program No.10
PROGRAM FOR DEPTH FIRST SEARCH
domains
X,Y,S=symbol
predicates
dfs(symbol,symbol)
successor(symbol,symbol)
clausessuccessor(a,b).
successor(b,d).
successor(b,e).
successor(a,c).
successor(c,f).
successor(c,g).
successor(e,h).
successor(e,i).successor(g,j).
successor(g,k).
dfs(X,X).
dfs(X,S):-successor(X,Y),Write("\n",Y),dfs(Y,S).
8/3/2019 IS file
23/23
Output 10:-
Goal:dfs(a,f).
ab
d
e
c
f