0% found this document useful (0 votes)
29 views

Program No.1 Study of Prolog

Program No.1 provides an overview of Prolog, including its origins in the 1970s, its core mechanisms of pattern matching and tree-based data structuring, and how it is well-suited for problems involving objects and relations between objects. It also discusses how Prolog is used for expert systems and meta-programming.

Uploaded by

Nishant Garg
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Program No.1 Study of Prolog

Program No.1 provides an overview of Prolog, including its origins in the 1970s, its core mechanisms of pattern matching and tree-based data structuring, and how it is well-suited for problems involving objects and relations between objects. It also discusses how Prolog is used for expert systems and meta-programming.

Uploaded by

Nishant Garg
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 23

Program No.

1 STUDY OF PROLOG
Prolog Programming in Logic PROLOG stands for Programming In Logic an idea that emerged in the early 1970s to use logic as programming language. The early developers of this idea included Robert Kowalski at Edinburgh ( on the theoretical side ), Marrten van Emden at Edinburgh (experimental demonstration ) and Alain Colmerauer at Marseilles ( implementation). David D.H.Warrens efficient implementation at Edinburgh in the mid 1970s greatly contributed to the popularity of PROLOG. PROLOG is a programming language centered around a small set of basic mechanisms, including pattern matching , tree-based data structuring and automatic backtracking. This small set constitutes a surprisingly powerful and flexible programming framework. PROLOG is especially well suited for problems that involve objects in particular, structured objects and relations between them . SYMBOLIC LANGUAGE PROLOG is a programming language for symbolic , non numeric computation. It is especially well suited for solving problems that involve objects and relations between 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 also easy to state a more general rule : if object X is closer to the observer than object Y , and Y is closer than Z, then X must be closer than Z. PROLOG can reason about the spatial relationships and their consistency with respect to the general rule . Features like this make PROLOG a powerful language for Artificial Language (A1) and non numerical programming. There are well-known examples of symbolic computation whose implementation in other standard languages took tens of pages of indestible code . When the same algorithms were implemented in PROLOG, the result was a cryetal-clear program easily fitting on one page. 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. Responses to user queries are determined through a form of inferencing control

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 mat make 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 Systems An Expert System is a set of programs that manipulates encoded knowledge to solve problems in a specialized domain that normally requires human expertise. An Expert systems knowledge is obtained from the expert sources such as texts, journals articles,. databases etc. and coded in a form suitable for the system to use in its inference or reasoning processes. Once a sufficient body of Expert knowledge has been acquired, it must be encoded in some form, loaded into knowledge base, then tested, and refined continually throughout the life of the system. PROLOG serves as a powerful language in designing expert systems because of its following features:

Use knowledge rather than data. Modification of the knowledge base without recompilation of control programs. Capable of explaing conclusions. Symbolic computations resembling manipulations of natural language. Reason with meta-knowledge.

META-PROGRAMMING A meta-program is a program that other programs as data. Interpreters and compilers are examples of meta-programs. Meta-interpreter is a particular kind of meta-program: an interpreter for a language written in that language. So a PROLOG meta-interpreter is an interpreter for PROLOG, itself written in PROLOG. Due to its symbol-manipulation capabilities, prolog is a powerful language for meta-programming. Therefore, it is often used as an implementation language for other languages. PROLOG is particularly suitable as a language for rapid prototyping where we are interested in implementing new ideas quickly. New ideas are rapidly implemented and experimented with. In prototyping the emphasis is on bringing new ideas to life quickly and cheaply, so that they can be immediately 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 is necessary, the prototype is useful because it usually helps to speed up the creative development stage.

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

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)

Output 2: Goal:Father(X,A) X=ram A=ravi X=ram A=kiran X=ravi A=prakash X=ravi A=rani

Program No.3 W.A.P. to show the hypothesis using the symptoms. domains Indication,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):-

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

OUTPUT 3:Goal:hypothesis(Patient,Disease) Patient=Charlie,Disease=commomcold 1 solution

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)

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=n2 1 Solution

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

Output 5:Goal:go ************************* CITY STATE NEWDELHI DELHI MUMBAI MAHARASHTRA CHENNAI TAMILNADU ************************* yes

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.

Output 6:Goal:fact(5) 120 1 solution

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.

Output 7:Goal:fib(5) 1 2 3 5 8 13

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

Output 8:Goal:distance(a,d) 300 1 solution

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:"),nl readln(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).

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

Program No.10 PROGRAM FOR DEPTH FIRST SEARCH domains X,Y,S=symbol predicates dfs(symbol,symbol) successor(symbol,symbol) clauses successor(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).

Output 10:Goal:dfs(a,f). a b d e c f

You might also like