0% found this document useful (0 votes)
25 views63 pages

AI Fundamentals of Prolog by Prof. Deepali Jain

The document discusses various data types in Prolog including integers, characters, strings, symbols, variables, and reserved terms. It also discusses basic arithmetic and relational operators in Prolog. The structure of Prolog programs is given as having three sections - domains, predicates, and clauses.

Uploaded by

Deep Rakholiya
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)
25 views63 pages

AI Fundamentals of Prolog by Prof. Deepali Jain

The document discusses various data types in Prolog including integers, characters, strings, symbols, variables, and reserved terms. It also discusses basic arithmetic and relational operators in Prolog. The structure of Prolog programs is given as having three sections - domains, predicates, and clauses.

Uploaded by

Deep Rakholiya
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/ 63

Artificial Intelligence Prepared By: Prof.

Deepali Jain CT601-N


Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PROLOG Data Types


1. Char: Between a pair of single quotes, a character is encapsulated.

2. Integer: An integer between -32768 and 32767 that is a full number.


3. Real: A peculiar character that is either positive or negative, followed by numbers.
4. String: A collection of characters encased in a pair of double-quotes. Strings can have
up to 255 characters in them.
5. Symbol: A combination of letters (A to Z or a to z), numerals (0 to 9) and the
underscore(_) character.
6. Variables: A variable is a symbol that can have multiple values assigned to it at
different stages of the program’s execution.
7. Reserved terms: PROLOG features a few reserved words that should not be
substituted for user-defined names.
8. Arithmetic Operators: The basic arithmetic operators in PROLOG are +, -, *, and /.
9. Relational Operators: PROLOG utilises the relational operators,=, =>, >=, >=. A
relational operator in PROLOG can be either goal or subgoal. The relational operator
(=) resembles an assignment operator in appearance.

Structure of PROLOG: Syntax of Prolog


Que: Write the structure OR syntax of Prolog
Ans: It contains 3 Sections
a) Domains Section
b) Predicates Section
c) Clauses Section
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

File extension: .pl .pro, .p


Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 1
AIM: - Study of facts, objects, predicates and variables, Rules,
Unification in Prolog
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 2
(A) Write an introduction of cut and fail predicates in detail
Program (1): Implement program to find Minimum and Maximum
number without using cut and fail predicates in prolog.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Program (2): Program to find minimum and maximum using cut and
fail predicate.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL : 3
(A) Write detail of input and output with its types and compound goals.
Program (1): Program to implement arithmetic operator.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Program (2) : Program to print the values between given range.


Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Program (3) : Program to implement Tower of hanoi.


Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Program (4): Program to find grade.


Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 4
(A) Write detail of concept recursion in file page.
Program (1): Program to implement factorial.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Program (2): Program to implement factorial using cut and fail


predicates.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 5

(A)Write an introduction of list in detail.


Program (1): Prolog program to add an element to the beginning of a list.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Program (2): Prolog program Define the relation last (item, list) so that
item is the last element of the list using concatenate.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 6
Program (1): Code for Prolog program to check whether a given word is a
palindrome or not in Artificial Intelligence.

CODE:
domains
x = char
l = char*

predicates
palindrome(l)
reverse(l,l)
concatenate(l,l, l)

clauses
concatenate([],List,List).
concatenate([X|List1],List2,[X|List 3]) :- concatenate(List1,List2,List3).
reverse([],[]).
reverse([X|Tail],List) :- reverse(Tail,Tail1), concatenate(Tail1,[X],Li st).
palindrome(List):- reverse(List,Lis t).

Output: -
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Program (2): Code for Prolog program to check whether a given list is
palindrome or not in Artificial Intelligence

CODE:
domains
list=symbol*

predicates
palin(list)
findrev(list,list,li st)
compare(list,list)

clauses
palin(List1):-findrev(List1,[],Lis t2),compare(List1,List 2).
findrev([],List1,List1).
findrev([X|Tail],List1,Lis t2):-findrev(Tail,[X|List1],List2).
compare([],[]):-write("\nList is Palindrome").
compare([X|List1],[X|List2]):- compare(List1,List2).
compare([X|List1],[Y|List2]):- write("\nList is not Palindrome").

OUTPUT
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Program (3): Code for Prolog program to compare characters, strings and
also reverse string in Artificial Intelligence
CODE:
domains
strlist =string*

predicates
start createlist(integer,strlist,strlist,strli st)
strcmp(string,string)
charcmp(char,string)
reverse(strlist,strlist,strlist)

goal
clearwindow, start.

clauses
start:-createlist(3,[],Newlist,List 1), reverse(List1,[],List2), List2 = [Str1 |
Tail], Tail = [Str2 Tail1],Tail1=[Str3|Str4],write("cmp string1 and
string2"),nl, strcmp(Str1,Str2), write("cmp string1 and string3"),nl,
strcmp(Str1,Str3), write("cmp string2 and string3"),nl, strcmp(Str2,Str3).

createlist(Num,Oldlist,Newlist,Lis t1):- Num > 0,write("Enter any string


="), readln(Str), Newlist = [Str | Oldlist], NN
=Num1,createlist(NN,Newlist,List2,List1).

createlist(_,Oldlist,_,List 1):- List1 = Oldlist.

strcmp(Str1,Str2):-frontchar(Str1,Ch1,Rest 1), charcmp(Ch1,Str2),


Rest1<>"",strcmp(Rest1,Str2).
strcmp(Str1,Str2). charcmp(Ch1,Str2):- frontchar(Str2,Ch2,Rest 2),
Ch1<>Ch2, Rest2<>"", charcmp(Ch1,Rest2).

charcmp(Ch1,Str2):- frontchar(Str2,Ch2,Rest 2),


Ch1=Ch2,write("char=",Ch2),nl.

charcmp(Ch1,Str2). reverse([],Inputlist,Inputlist). reverse([Head


|Tail],List1,List2):- reverse(Tail,[Head | List1],List2).
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Output:-

PRACTICAL: 7.
Program (1): Code for Prolog program for family hierarchy in Artificial
Intelligence.
CODE:
predicates
male(symbol).
female(symbol).
father(symbol,symbol).
husband(symbol,symbol).
brother(symbol,symbol).
sister(symbol,symbol).
listbrothers(symbol).
listsisters(symbol).
mother(symbol,symbol).
grandfather(symbol).
grandmother(symbol).
uncle(symbol).
aunt(symbol).
cousin(symbol).
listgrandsons(symbol).
listgranddaughters(symbo l).
printmenu.
action(integer).
repeat.

clauses
male(dashrath).
male(ram).
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

male(laxman).
male(bharat).
male(luv).
male(kush).
male(son_of_laxman).

female(kaushalya).
female(sita).
female(urmila).
female(daughter_of_dashrath).

father(dashrath,ram).
father(dashrath,laxman).
father(dashrath,bharat).
father(ram,luv).
father(ram,kush).
father(laxman,son_of_l axman).
father(dashrath,daught er_of_dashrath).

husband(dashrath,kaushaly a).
husband(ram,sita).
husband(laxman,urmila).

mother(X,Y):-husband(Z,X),father(Z,Y).

brother(X,Y):-father(Z,X),father(Z,Y), X<>Y,male(X).

sister(X,Y):-father(Z,X),father(Z,Y), X<>Y,female(X).

listbrothers(X) :-brother(Z,X),write(Z).

listsisters(X):-sister(Z,X),write(Z).

grandfather(X):- father(Y, Z), father(Z,X),write(Y, " is the grandfather of ",X,"\n").

grandmother(X):-husband(Z,X),father(Z,V),father(V,Y),write(Y, " is the grandmother of ",X,"\n").

listgrandsons(X):-father(X,Z),father(Z,Y),male(Y),write(Y,"\n"), fail.

listgrandsons(X):-husband(Y,X),father(Y,V),father(V,Z),male(Z),write(Z,"\n"), fail.

listgranddaughters(X):- father(X,Z), father(Z,Y), female(Y),write(Y,"\n"), fail.

listgranddaughters(X):- husband(Y,X), father(Y,V), father(V,Z),female(Z), write(Z,"\n"), fail.

uncle(X):-brother(Z,Y),father(Z,X),male(Y),write(Y,"\n"), fail.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

aunt(X):-husband(Z,Y),brother(Z,V),father(V,X),write(Y,"\n"), fail.

cousin(X):-father(Z,X),father(V,Y), Z<>V,brother(V,Z),write(Y,"\n").
repeat.
repeat:- repeat.

action(1):- write("\nEnter name of person whose father is to be found: "), readln(X),write("\n"),


write("Father of ",X," is:"), father(Z,X), write(Z,"\n"), fail.

action(2):- write("\nEnter name of person whose mother is to be found: "), readln(X),write("\n"),


write("Mother of ",X," is:"),mother(Z,X), write(Z,"\n"), fail.

action(3):- write("\nEnter name of person whose brothers are to be found: "), readln(X),
write("\n"), write("Brothers of ",X," are:\n"), listbrothers(X), write("\n"), fail.

action(4):- write("\nEnter name of person whose sisters are to be found: "), readln(X),
write("\n"), write("Sisters of ",X," are:\n"), listsisters(X), write("\n"), fail.

action(5):- write("\nEnter name of person whose grandsons are to be found: "), readln(X),
write("\n"), write("Grandsons of ",X," are:\n"), listgrandsons(X), write("\n"), fail.

action(6):- write("\nEnter name of person whose granddaughters are to be found


: "), readln(X),write("\n"), write("Granddaughters of ",X," are:\n"), listgranddaughters(X),
write("\n"), fail.
action(7):- write("\nEnter name of person whose uncles are to be found: "), readln(X),
write("\n"), write("Uncles of ",X," are:\n"), uncle(X), write("\n"), fail.

action(8):- write("\nEnter name of person whose aunties are to be found: "), readln(X),
write("\n"),write("Aunties of ",X," are:\n"),aunt(X), write("\n"), fail.

action(9):- write("\nEnter name of person whose cousins are to be found: "), readln(X),
write("\n"), write("Cousins of ",X," are:\n"), cousin(X), write("\n"), fail. action(0). printmenu
:-repeat,write("\n1. Display Father of?\n"), write("2. Display Mother of?\n"), write("3. List all
brothers of?\n"), write("4. List all sisters of?\n"), write("5. List all grandson of?\n"), write("6. List
all granddaughter of?\n"), write("7. List all uncles of?\n"), write("8. List all aunty of?\n"), write("9.
list all cousins of?\n"), write("0. exit\n"), write("Enter your choice :
"), readInt(Choice), action(Choice), write("\n"), repeat.

goal
makewindow(1,2,3,"Family Tree",0,0,25,80), printmenu.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Output

+ -Family Tree +
¦ ¦
¦1. Display Father of? ¦
¦2. Display Mother of? ¦
¦3. List all brothers of? ¦
¦4. List all sisters of? ¦
¦5. List all grandson of? ¦
¦6. List all granddaughter of? ¦
¦7. List all uncles of? ¦
¦8. List all aunty of? ¦
¦9. list all cousins of? ¦
¦0. exit ¦
¦Enter your choice : 1 ¦
¦ ¦
¦Enter name of person whose father is to be found : ram ¦
¦ ¦
¦Father of ram is:dashrath ¦
¦ ¦
¦1. Display Father of? ¦
¦2. Display Mother of? ¦
¦3. List all brothers of? ¦
¦4. List all sisters of? ¦
¦5. List all grandson of? ¦
¦6. List all granddaughter of? ¦
¦7. List all uncles of? ¦
¦8. List all aunty of? ¦
¦9. list all cousins of? ¦
¦0. exit ¦
¦Enter your choice : 3 ¦
¦ ¦
¦Enter name of person whose brothers are to be found : ram ¦
¦ ¦
¦Brothers of ram are: ¦
¦laxman ¦
¦bharat ¦
¦ ¦
¦1. Display Father of? ¦
¦2. Display Mother of? ¦
¦3. List all brothers of? ¦
¦4. List all sisters of? ¦
¦5. List all grandson of? ¦
¦6. List all granddaughter of? ¦
¦7. List all uncles of? ¦
¦8. List all aunty of? ¦
¦9. list all cousins of? ¦
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

¦0. exit ¦
¦Enter your choice : 5 ¦
¦ ¦
¦Enter name of person whose grandsons are to be found : dashrath ¦
¦ ¦
¦Grandsons of dashrath are: ¦
¦luv ¦
¦kush ¦
¦son_of_laxman ¦
¦ ¦
¦1. Display Father of? ¦
¦2. Display Mother of? ¦
¦3. List all brothers of? ¦
¦4. List all sisters of? ¦
¦5. List all grandson of? ¦
¦6. List all granddaughter of? ¦
¦7. List all uncles of? ¦
¦8. List all aunty of? ¦
¦9. list all cousins of? ¦
¦0. exit ¦
¦Enter your choice : 7 ¦
¦ ¦
¦Enter name of person whose uncles are to be found : kus ¦
¦ ¦
¦Uncles of kush are: ¦
¦laxman ¦
¦bharat ¦
¦ ¦
¦1. Display Father of? ¦
¦2. Display Mother of? ¦
¦3. List all brothers of? ¦
¦4. List all sisters of? ¦
¦5. List all grandson of? ¦
¦6. List all granddaughter of? ¦
¦7. List all uncles of? ¦
¦8. List all aunty of? ¦
¦9. list all cousins of? ¦
¦0. exit ¦
¦Enter your choice : ¦
¦ ¦
¦ ¦
¦Press the SPACE bar ¦
+
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 8
Program : Write a program to implement BFS (for AI search problem).
CODE:
domains
X, H, N, ND=symbol
P, L, T, Z, Z1, L1, L2, L3, PS, NP, ST, SOL=symbol*

predicates
solve(L, L)
member(X,L)
extend(L, L)
conc(X, L, L)
breadthfirst(L, L)
goal(X)

clauses
solve(start, solution):-/*solution is a state from start to a goal*/ breadthfirst ([[start]], solution).

breadthfirst([[node|path]| _ ] ,[node|path]):- /*solution is an extension to a goal*/


/*of one of path*/ goal(node).

breadthfirst([path|paths], solution):- extend(path,newpaths), conc(paths,newpaths,path1),


breadthfirst(path1,solution).

extend([node|path],newpaths):- bagof([newnode, node|path],(s(node,


newnode),notmember(newnode,[node|path])), newpaths),!. extend(path, []).

conc([], L, L).

conc([X|L1], L2, [X|L3]):- conc(L1, L2, L3).

member(X, [X|T]).
member(X, [H|T]):- member(X, T).

OUTPUT: -
goal: solve([a, e], S)

L= [“a”, “b”, ”c”, “d”, ”e”]

goal: solve([a, h],S)

L= [“a”, “b”, ”c”, “d”, ”e”, ”f”, “g”, “h”]


Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 59
Program: Write a program to implement DFS (for 8 puzzle 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”]
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 60
Program : Write a program to implement A* Algorithm.
%%%
%%%
%%% Nodes have form S#D#F#A
%%% where S describes the state or configuration
%%% D is the depth of the node
%%% F is the evaluation function value
%%% A is the ancestor list for the node

:- op(400,yfx,'#'). /* Node builder notation

*/ solve(State,Soln) :- f_function(State,0,F), search([State#0#F#[]],S),


reverse(S,Soln).

f_function(State,D,F) :- h_function(State,H), F is D + H.

search([State#_#_#Soln|_], Soln) :- goal(State). search([B|R],S) :-


expand(B,Children),
insert_all(Children,R,Ope n), search(Open,S).
insert_all([F|R],Open1,Open3) :- insert (F,Open1,Open2),
insert_all(R,Open2,Open3).
insert_all([],Open,Open).

insert(B,Open,Open) :- repeat_node(B,Open), !
. insert(B,[C|R],[B,C|R]) :- cheaper(B,C), ! .
insert(B,[B1|R],[B1|S]) :- insert(B,R,S),
!. insert(B,[],[B]). repeat_node(P#_#_#_, [P#_#_#_|_]).

cheaper( _#_#F1#_ , _#_#F2#_ ) :- F1 < F2.


expand(State#D#_#S,All_My_Childr en) :- bagof(Child#D1#F#[Move|S],
(D1 is D+1,move(State,Child,Mov e), f_function(Child,D1,F)),
All_My_Children).
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 11
Program : Write a program to solve travelling salesman problem using Prolog.
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, !.
OUTPUT: -
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

PRACTICAL: 12
Program: Study of dynamic database in PROLOG.

Predicates
reading
writing
delete
find(integer)
startup(integer)
Database
Unsorted
Database(string,integer)
Sorted
Database(string)

Clauses
startup(0).
startup(Num):- write("Enter String = "), readln(Name), str_len(Name,Len),
asserta(unsortedDatabase(Name,Len)), TempNum = Num - 1,
startup(TempNum).

writing:- sortedDatabase(Name), write(Name),nl,fail.


writing.

find(Index):- unsortedDatabase(Name,Index), assertz(sortedDatabase(Name)),


retract(unsortedDatabase(Name,Index)), find(Index).

find(Index):- Index = 255.

find(Index):- TempIndex = Index + 1, find(TempIndex).

reading:- NumRead = 10, startup(NumRead).

delete :- retract(sortedDatabase(_)), fail.


delete.

Goal
Clearwindow,
makewindow(1,2,3,"String Operations",0,0,25,80), reading,!,
find(1),
write("\nString In Increasing Order Of Their Length Are : \n"), writing,delete.
Artificial Intelligence Prepared By: Prof. Deepali Jain CT601-N

Output:

You might also like