AI Practical File
AI Practical File
By
Krishan Beniwal
(ROLLNO - 23UGBTC35132)
To
Source Code :-
Output :-
Q2. Write a Prolog program to implement max(X, Y, M) so that M is the maximum of two numbers X and Y.
Source Code :-
Output :-
Q3. Write a program in PROLOG to implement factorial (N, F) where F represents the factorial of a number N.
Source Code:
Output:
Q4. Write a Prolog program to implement member(A, S): to check whether A is a member of S or not.
Source Code:
Output:
Q5. Write a Prolog program to implement multiply(X1, X2, M) : where X1 and X2 denotes the numbers to be
multiplied and M represents the result.
Source Code:
Output:
Source Code:
% Base case: GCD of X and 0 is X
gcd(X, 0, X).
Output:
Q7. Write a program in PROLOG to implement palindrome (S) which checks whether a list S is a palindrome or not.
Source Code:
% Base case: an empty list and a single element list are palindromes
palindrome([]).
palindrome([_]).
/* Recursive case: a list is a palindrome if the first and last elements are the same */
% and the sublist without the first and last elements is a palindrome
palindrome([Head|Tail]) :-
append(Mid, [Head], Tail),
palindrome(Mid).
Output:
Q8. Write a Prolog program to implement maxlist(S, M); minlist(S, M) so that M is the maximum/minimum
number in the list.
Source Code:
% maxList(S, M) :- M is max_list(S).
% minList(S, M) :- M is min_list(S).
Output:
Q9. Write a Prolog program to merge two lists.
Source Code:
% Base case: Append empty list to another list
merge([], L, L).
Output:
Q10. Write a prolog program to implement insert_nth (I, N, S, R) that inserts an item I into Nth
position of list S to generate a list R
Source Code:
Output:
Q11. Write a prolog program to implement delete_nth (N, S, R) that removes Nth position item of list S to generate
a list R.
Source Code:
Output:
Q12. Write a Prolog program for BFS (Breadth First Search).
Source Code:
% BFS predicate
bfs(Start, Goal, Path) :- bfs([Start], [Start], Goal, Path).
% Helper predicate
bfs([], _, _, _) :- fail.
bfs([Node|Nodes], Visited, Goal, Path) :-
( Node = Goal
-> reverse(Visited, Path);
findall(NewNode, (edge(Node, NewNode), \+ member(NewNode, Visited)), NewNodes),
append(Nodes, NewNodes, NextNodes),
bfs(NextNodes, [Node|Visited], Goal, Path)
).
Output :
Q13. Write a Prolog program for DFS (Depth First Search) .
Source Code:
% DFS predicate
dfs(Start, Goal, Path) :- dfs([Start], Start, Goal, Path).
% Helper predicate
dfs(Visited, Node, Node, [Node|Visited]).
dfs(Visited, Start, Goal, Path) :-
edge(Start, Next),
\+ member(Next, Visited),
dfs([Next|Visited], Next, Goal, Path).
Output:
Source Code:
%% Define gender
male(john).
male(michael).
male(david).
male(oliver).
female(mary).
female(emily).
female(sarah).
Output:
Q15. Write a Prolog program for Calculator .
Source Code:
%% Arithmetic Operations
%% Trigonometric Operations
%% trigo(P, B, H) calculates trigonometric functions given perpendicular (P), base (B), and hypotenuse (H)
trigo(P, B, H) :-
Sinx is P / H,
write('sin(x) = '), write(Sinx), nl,
Cosx is B / H,
write('cos(x) = '), write(Cosx), nl,
Tanx is P / B,
write('tan(x) = '), write(Tanx), nl,
Cosecx is H / P,
write('cosec(x) = '), write(Cosecx), nl,
Secx is H / B,
write('sec(x) = '), write(Secx), nl,
Cotx is B / P,
write('cot(x) = '), write(Cotx), nl.
Output :
Source Code:
%% List Operations
%% Append
append([], L, L).
append([H|T], L, [H|R]) :- append(T, L, R).
%% Length
length([], 0).
length([_|T], N) :- length(T, N1), N is N1 + 1.
%% Reverse
reverse(L, R) :- reverse(L, [], R).
reverse([], R, R).
reverse([H|T], Acc, R) :- reverse(T, [H|Acc], R).
%% Member
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
%% Delete
delete([], _, []).
delete([X|T], X, T).
delete([H|T], X, [H|R]) :- delete(T, X, R).
%% Insert
insert(X, [], [X]).
insert(X, [H|T], [X, H|T]).
insert(X, [H|T], [H|R]) :- insert(X, T, R).
%% Sort
sort([], []).
sort([X|T], S) :-
sort(T, S1),
insert(X, S1, S).
%% Maximum
max([X], X).
max([X|T], M) :-
max(T, M1),
M is max(X, M1).
%% Minimum
min([X], X).
min([X|T], M) :-
min(T, M1),
M is min(X, M1).
%% Sum
sum([], 0).
sum([H|T], S) :-
sum(T, S1),
S is H + S1.
Output:
Q17. Write a Prolog code for AI agents .
Source Code:
% Define the agent's knowledge base
kb(fact1).
kb(fact2).
kb(fact3).
Output:
Q18. Prolog code for designing semantic network for Shopping Mall .
Source Code:
% Define the entities in the shopping mall
entity(mall).
entity(store).
entity(product).
entity(customer).
entity(employee).
frame(store, [
name,
type,
has(product),
works_at(employee)
]).
frame(product, [
name,
price,
bought_by(customer)
]).
frame(customer, [
name,
age,
buys(product),
visited(mall)
]).
frame(employee, [
name,
role,
works_at(store)
]).
instance(store, [
name = 'Best Buy',
type = 'electronics',
has(product, [
name = 'iPhone',
price = 999
])
]).
instance(customer, [
name = 'John Doe',
age = 30,
buys(product, [
name = 'iPhone',
price = 999
])
]).
instance(employee, [
name = 'Jane Smith',
role = 'sales associate',
works_at(store, [
name = 'Best Buy',
type = 'electronics'
])
]).
Output:
Q19. Prolog code for Problem statement: The law says that it is a crime for an Indian to sell Indian software to
hostile nations. The country, named ABC, an enemy of India has some Indian software and all its software was
sold to it by George who is an Indian. Prove that George is a criminal.
Source Code:
Output:
Q20. Write a Prolog Code For Problem statement: Using Skolemization and definite / not definite clauses solve this
problem. To prove that Vibhuti killed Lucy i. Everyone who loves all animals is loved by someone. ii. Anyone
who kills an animal is loved by no one. iii. Gokul loves all animals. iv. Either Gokul or Vibhuti killed the cat who
is named Lucy. v. Did Vibhuti kill the cat?
Source Code:
Output:
Source Code:
Q22. Write a Prolog code for designing Semantic Network / Frames for Drawing Room.
Source Code:
query(is_a_chair) :-
relation(X, is_a, chair),
write(X), nl, fail.
query(is_a_chair).
query(is_a_furniture) :-
relation(X, is_a, furniture),
write(X), nl, fail.
query(is_a_furniture).
Output:
Source code:
Output:
Source code:
Output:
Q25. Write a Prolog program for A* search.
Source code:
Output:
Source code:
dfs_graph(Node, _, [Node]).
dfs_graph(Node, Nodes, [Node|Path]) :-
edge(Node, Next),
member(Next, Nodes),
dfs_graph(Next, Nodes, Path).
bfs_graph(Node, _, [Node]).
bfs_graph(Node, Nodes, [Node|Path]) :-
edge(Node, Next),
member(Next, Nodes),
bfs_graph(Next, Nodes, Path).
Output:
Q28. How Basic Input and Output can be performed Implementation using Prolog code.
Source code:
Source code:
% Family relations
parent(john, mary).
parent(john, peter).
sibling(mary, peter).
Output:
Source code:
frame(person, [name, age]).
create_instance(Frame, Instance) :-
frame(Frame, Slots),
instance(Slots, Instance).
instance([], []).
instance([Slot|Slots], [Slot=''|Instance]) :-
instance(Slots, Instance).
test :-
create_instance(person, Instance),
set_slot(Instance, name, 'John', Instance1),
set_slot(Instance1, age, 30, Instance2),
get_slot(Instance2, name, Name),
get_slot(Instance2, age, Age),
write('Name: '), write(Name), nl,
write('Age: '), write(Age), nl.
Output: