0% found this document useful (0 votes)
5 views26 pages

Introduction

Prolog is a logic programming language used in artificial intelligence, characterized by its declarative nature, use of facts and rules, and features such as unification and backtracking. The document outlines the basic elements of Prolog, including facts, rules, and queries, along with examples of Prolog code for various applications like family trees, medical diagnosis, and list operations. It also discusses the syntax for defining facts and rules, as well as basic operations on lists.

Uploaded by

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

Introduction

Prolog is a logic programming language used in artificial intelligence, characterized by its declarative nature, use of facts and rules, and features such as unification and backtracking. The document outlines the basic elements of Prolog, including facts, rules, and queries, along with examples of Prolog code for various applications like family trees, medical diagnosis, and list operations. It also discusses the syntax for defining facts and rules, as well as basic operations on lists.

Uploaded by

ind0boy634
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction

What is Prolog?
Prolog is a logic programming language that is used in artificial intelligence. It is a
declarative programming language expressing logic as relations, called facts and rules. A
Prolog program consists of a collection of facts and rules; a query is a theorem to be
proved. Here are some basic elements of Prolog:

Facts: These are statements that are true. They are written as a predicate followed by a
period. For example, `likes(john, mango).` is a fact that states that John likes mango.

Rules: These are statements that define relationships between facts. They are written as a
predicate followed by a body, which is a list of one or more predicates separated by
commas and enclosed in parentheses, followed by a period. For example,
Grandfather(X, Y) :-father(X,Z),parent(Z,Y)
This implies that for X to be the grandfather of Y, Z should be a parent of Y and X should
be the father of Z.

Queries: These are questions that we ask Prolog. They are written as a predicate followed
by a question mark. For example, `?- likes(john, mango).` is a query that asks if John likes
mango.

Key features of Prolog:


The key features of Prolog are as follows:
1. Declarative Language: Prolog is a declarative programming language, meaning
that programs are written as sets of logical statements. This makes Prolog
programs concise and easy to read.
2. Predicate Calculus: Prolog uses the language of predicate calculus, which allows for
the representation of relationships between facts and rules.
3. Handling Lists and Recursion: Prolog naturally handles lists and recursion, making it
well-suited for tasks that involve these concepts.
4. Unification: Prolog uses unification, which is the process of determining if given terms
can represent the same structure. Unification is a fundamental concept in Prolog.
5. Backtracking: When a task fails, Prolog traces backward and tries to satisfy the
previous task. This backtracking feature allows for flexible problem-solving.
6. Efficiency: Prolog is known for its efficiency in solving problems that would be
difficult in other programming languages.

Symbols in Prolog
Using the following truth-functional symbols, the Prolog expressions are
English Predicate Prolog
Calculus
If --> :-
Not ~ Not
Or V ;
and ^ ,
comprised. These symbols have the same interpretation as in the predicate calculus.
Two types of prolog programming are there based on the features of:
1. Facts, rules and queries
2. Knowledge based

Facts
We can define fact as an explicit relationship between objects, and properties these
objects might have. So facts are unconditionally true in nature. Suppose we have some
facts as given below −
• Tom is a cat
• Kunal loves to eat Pasta
So these are some facts, that are unconditionally true. These are actually statements, that
we have to consider as true.
Following are some guidelines to write facts −
• Names of properties/relationships begin with lower case letters.
• The relationship name appears as the first term.
• Objects appear as comma-separated arguments within parentheses.
• A period "." must end a fact.
• Objects also begin with lower case letters. They also can begin with digits (like 1234),
and can be strings of characters enclosed in quotes e.g. color(penink, ‘red’).
• phoneno(agnibha, 1122334455). is also called a predicate or clause.

Syntax
The syntax for facts is as follows −
relation(object1,object2...).
Example
Following is an example of the above concept −
cat(tom).
loves_to_eat(kunal,pasta).
of_color(hair,black).
loves_to_play_games(nawaz).
lazy(pratyusha).

Rules
We can define rule as an implicit relationship between objects. So facts are conditionally
true. So when one associated condition is true, then the predicate is also true. Suppose we
have some rules as given below −
• Lili is happy if she dances.
• Tom is hungry if he is searching for food.

So these are some rules that are conditionally true, so when the right hand side is true,
then the left hand side is also true.
Here the symbol ( :- ) will be pronounced as “If”, or “is implied by”. This is also known
as neck symbol, the LHS of this symbol is called the Head, and right hand side is called
Body. Here we can use comma (,) which is known as conjunction, and we can also use
semicolon, that is known as disjunction.
Syntax
rule_name(object1, object2, ...) :- fact/rule(object1,
object2, ...)
Suppose a clause is like :
P :- Q;R.
This can also be written as
P :- Q.
P :- R.
If one clause is like :
P :- Q,R;S,T,U.
Is understood as
P :- (Q,R);(S,T,U).
Or can also be written as:
P :- Q,R.
P :- S,T,U.
Example
happy(lili) :- dances(lili).
hungry(tom) :- search_for_food(tom).
friends(jack, bili) :- lovesCricket(jack), lovesCricket(bili).
goToPlay(ryan) :- isClosed(school), free(ryan).
1. WAP to implement family tree in turbo prolog.
Code:
domains
name = symbol

predicates
male(name).
female(name).
daughter(name,name).
husband(name,name).
father(name,name).
mother(name,name).
son(name,name).
daughter(name,name).
wife(name,name).
grandmother(name,name).
grandfather(name,name).
sister(name,name).
brother(name,name).
uncle(name,name).
aunt(name,name).
niece(name,name).
nephew(name,name).
cousin(name,name).
sibling(name,name).

clauses
male(ritik).
male(manohar).
male(sham).
male(shiva).
male(krishna).
female(Kaushalya).
female(Asha).
female(sunita).
female(ritika).
female(supriya).
female(anandita).
father(manohar, ritik).
father(manohar, ritika).
father( sham, Manohar).
mother(kaushalya, manohar).
mother(asha, ritka).
mother(sunita, supriya).
mother(asha, ritik).
brother (Deepanshu, ritika).
sister(ritika, supriya).
sister(ritika, anandita).
sister(supriya, ritika).
nephew(ritik,shiva).

father(X,Y) :- male(X),son(Y,X).
father(X,Y) :- male(X),daughter(Y,X).
wife(X,Y) :- husband(Y,X).
mother(X,Y) :- father(Z,Y),husband(Z,X),female(X).
brother(X,Y):- father(Z,X),father(Z,Y),male(X),X<>Y.
sister(X,Y):- father(Z,X),father(Z,Y),female(X),X<>Y.
grandmother(X,Y):- father(Z,Y),mother(X,Z),female(X).
grandfather(X,Y) :- father(Z,Y),father(X,Z),male(X).
uncle(X,Y) :- father(Z,Y),brother(X,Z),male(X).
uncle(X,Y) :- father(Z,Y),sister(Z,X),male(X).
aunt(X,Y) :- uncle(Z,Y) , husband(Z,X).
aunt(X,Y) :- father(Z,Y) , sister(X,Z).
cousin(X,Y) :- uncle(Z,X) , father(Z,Y).
sibling(X,Y) :- father(Z,X) , father(Z,Y), X<>Y.
nephew(X,Y):- uncle(Y,X),male(X).
niece(X,Y) :- uncle(Y,X) , female(X).

Output:
2. WAP to implement disease, symptoms in turbo prolog.
Code:
domains
symptom, disease = symbol

predicates
indicates(symptom, disease).

clauses
indicates(headache, migrane).
indicates(headache, dehydration).
indicates(fever, thyroid).
indicates(swelling, thyroid).
indicates(nosebleeding, high_bp).
indicates(headache, high_bp).

Output:
3. WAP to implement lists in turbo prolog.
Code:
domains
list = symbol*

predicates
member(symbol,list)

clauses
member(X,[X | _]).
Member(X,[_ | Tail]):- member(X,Tail).

Output:
4. WAP to implement go predicates.
Code:
domains
patient = string

predicates
go

clauses
go:-
clearwindow,
write(“Enter the name of patient: ”),
readln(Patient),
write(“Patient’s name is ”,Patient),nl.

Output:
5. WAP to find GCD of two numbers.
Code:
domains
num = integer

predicates
gcd(num,num,num)

clauses
gcd(A,0,A):- !.
gcd(A,B,G):-
R = A mod B,
Gcd(B,R,G).

Output:
6. WAP to find largest of three numbers.
Code:
domains
num = integer

predicates
largest(num,num,num,num)

clauses
largest(A,B,C,A):- A >= B , A>=C.
largest(A,B,C,B):- B >= A , B>=C.
largest(A,B,C,C):- C >= A , C>=B.

Output:
7. WAP to implement read real and write predicates.
Code:
domains
item = string
price = real

predicates
askprice(Item,Price)

clauses
askprice(Item,Price):-
write(“Enter the price of ”,Item),
readreal(Price),
write(Item,“S”).

Output:
8. WAP to tell whether a student is pass or fail.
Code:
domains
marks = integer
status = symbol

predicates
result(marks, status)

clauses
result(M,pass):-
M>=40.
Result(M,“Fail”):-
M<40.

Output:
9. WAP to print the grades of students according to marks.
Code:
domains
score = integer

predicates
result(score)

clauses
result(S):-
S>=80,S<=100,
write(“Grade A”),nl.

result(S):-
S>=60,S<80,
write(“Grade B”),nl.

result(S):-
S>=40,S<60,
write(“Grade C”),nl.

result(S):-
S>=30,S<40,
write(“Grade D”),nl.

result(S):-
S<30,
write(“Fail”),nl.

Output:
10. WAP to write predicates one converts centigrade temperatures to
Fahrenheit, the other checks if a temperature is below freezing.
Code:
domains
temp = real
predicates
freeze(temp)
convert(temp)
clauses
freeze(Temp):-
Temp<=0,
write(“The temperature is below freezing point.”),nl.

convert(Temp):-
R=Temp*(9/5)+32,
write(“Temperature in Fahrenheit is ”R),nl.

Output:
11. WAP in turbo prolog for medical diagnosis and show the advantage and
disadvantage of green and red cut.
Code:
domains
type = green; red

predicates
cut(type)

clauses
cut(red):-
write(“The advantages are quick healing.”),nl,
write(“The disadvantages are less detailed.”),nl.
cut(green):-
write(“The advantages are clear signal of bacterial infection.”),nl,
write(“The disadvantages are delayed healing process.”),nl.

Output:
12. WAP to implement <> operator.
Code:
domains
object = symbol
color = symbol

predicates
color(object, color)
different_colors(object, object)

clauses
color(apple, red).
color(banana, yellow).
color(sky, blue).
different_colors(X,Y):-
color(X, Color1),
color(Y,Color2),
Color1 <> Color2

Output:
13. WAP to implement tower of honai.
Code:
domains
n = integer
rod = symbol

predicates
move(n, rod, rod, rod)

clauses
move(1,X,Y,Z):-
Z=Z,
Y=Y,
X=X,
write(“Move top disk from ”),
write(X),
write(“ to ”),
write(Y),nl.

move(N,X,Y,Z):-
N>1,
M=N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).

Output:
14. WAP to implement Monkey Banana problem.
Code:
domains
loc = atd; atw; mid; onf; onb
status = hasnot ; has
action1 = grasp; climb
action2 = drag(loc,loc); walk(loc,loc)
st=state(loc,loc,loc,status)

predicates
move1(st,action1,st)
move2(st,action2,st)
canget(st)

clauses
move1(state(mid, onb, mid, hasnot),
grasp,
state(mid, onb, mid, has)).
move1(state(P, onf, P, H),
climb,
state(P, onf, P, H)).
move2(state(P1, onf, P1, H),
drag(P1, P2),
state(P2, onf, P2, H)).
move1(state(P1, onf, B, H),
walk(P1, P2),
state(P2, onf, B, H)).
canget(state(_,_,_,has)).
canget(State1):- move1 (State1,_,State2),write(State1,“ to ”,State2),nl,canget(State2).
canget(State1):- move2 (State1,_,State2),write(State1,“ to ”,State2),nl,canget(State2).

Output:
List

 List is a simple data structure used to store homogenous values.


 It enclosed within square brackets.
 It can be empty and non-empty.
 The first item is called the head of the list and the remaining part of the list are called
Tail.
 ex: L= [a, b, c, d].
 [a]= head and [b, c,d] = tail
 L= [a | Tail]. (vertical bar | pipe symbol is used to separate head and tail part).
 L =[]. empty list
 Certain valid representations :
 [a,b,c] = [a | [b, c]]
 [a,b,c] = [a, b | [c]]
 [a,b,c] = [a,b,c| [] ]

Basic operations on list:

1. to check whether an element is a member of list or not


 prolog provides built-in predicates for common list operations.

member (Element, list)


rule: member (x, [_| Tail]):- member (x, Tail).
Eg. ?-member (b, [a,b,c]).
O/P-true

2. append (List1, list 2, Result)


Concatenates list1 and list 2 binding the result to Result
append [Head| Tail], L2 [Head| Result]:- append (Tail; L2, Resut).
Eg. ?-append ([a,b], [c ,d], x).
O/P: x = [a,b,c,d].

3. to reverse the order of the list- reverse (list, Reversed List)


ex: P-reverse ([1,2, 3], x).
O/P : x = [3, 2, 1].

4. To calculate the length of the list- length (list, length).


List_length ([_|Tail], N):- List-length (Tail, N1), N is N+1.
Eg. ?-list. length ([a, b, c, d], N).
O/P: 4
15. WAP to print and show the length of list .
Code :
domains
list = integer*

predicates
writelist(list)
length(list,integer)

clauses
writelist([]).
writelist([H|T]) :- write(H),nl,writelist(T).

length([],0).
length([_|T],N) :- length(T,N1) , N = N1 + 1.

Output :
16. WAP to delete a sublist from a list .
Code :
domains
list = symbols*

predicates
delete(symbol,list,list)

clauses delete(X,[X|
Tail],Tail).
delete(X,[Y|Tail],[Y|Tail1]):- delete(X,Tail,Tail1).

Output:
17. WAP for find sum of integer list .
Code:
domains
list = integer*

predicates
findsum(list)
sum(list,integer)

clauses
findsum(L):
-
sum(L,Sum),
write(“\n Sum of given list :”,Sum),nl.
sum([],0).
sum([X|Tail],Sum):- sum(Tail , Temp),Sum = Temp + X .

Output :
18. Concatenation of Two lists using user inputs .
Code :
domains
list = integer *

predicates
concatenate(list,list,list)

clauses
concatenate([],L,L).
concatenate([H|T],L2,[H|R]):-
concatenate(T,L2,R).

Output :
19. WAP to print the reverse of a list .
Code :
domains
list = integer*

predicates
reverse(list , list)
append(list,list,list)

clauses
reverse([H|T],R) :-
reverse(T,RT),append(RT,[H],R).
append([],L,L).
append([H|T],L,[H|R]):-
append(T,L,R).

Output:
20. WAP which will demonstrate the length of given string.
Code :
domains
str =
string
len = integer

predicates
go
str_len(str, len)

clauses

go :-
X = “Hello world! Prolog_”,
str_len(X,Length),
write(Length),nl.

Output :
21. WAP to convert lowercase to uppercase.
Code :
domains
str = string

predicates
go
upper_lower(str, str)

clauses
go:-
X= “turbo PROLOG2”,
upper_lower(Upper,X),
write(Upper),nl,
upper_lower(X,Lower),
write(Lower),nl.

Output :

You might also like