AI Fundamentals of Prolog by Prof. Deepali Jain
AI Fundamentals of Prolog by Prof. Deepali Jain
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
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
PRACTICAL: 5
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).
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).
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.
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(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(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).
conc([], L, L).
member(X, [X|T]).
member(X, [H|T]):- member(X, T).
OUTPUT: -
goal: solve([a, e], S)
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).
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
f_function(State,D,F) :- h_function(State,H), F is D + H.
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#_#_#_|_]).
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).
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: