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

AI Practical File

Uploaded by

kbeniwal289
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

AI Practical File

Uploaded by

kbeniwal289
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Practical File

COMPUTER SCIENCE AND ENGINEERING (ARTIFICIAL


INTELLIGENCE & MACHINE LEARNING)

By

Krishan Beniwal
(ROLLNO - 23UGBTC35132)

To

Ms. Ritu Rana


(Skill Assistant Professor ,CSE Department)

Department of Skill Faculty of Engineering & Technology


Shri Vishwakarma Skill University
Dudhola, Palwal
INDEX :-
S.NO Experiment Sign
.
1. Write a prolog program to calculate the sum of
two numbers.
2. Write a Prolog program to implement max(X, Y,
M) so that M is the maximum of two numbers X
and Y.
3. Write a program in PROLOG to implement
factorial (N, F) where F represents the factorial of
a number N.
4. Write a Prolog program to implement member(A,
S): to check whether A is a member of S or not.
5. 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.
6. Write a Prolog program to implement GCD of two
numbers.
7. Write a program in PROLOG to implement
palindrome (S) which checks whether a list S is a
palindrome or not.
8. Write a Prolog program to implement maxlist(S,
M); minlist(S, M) so that M is the
maximum/minimum number in the list.
9. Write a Prolog program to merge two lists.
10. 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
11. Write a prolog program to implement delete_nth
(N, S, R) that removes Nth position itemof list S
to generate a list R.
12. Write a Prolog program for BFS (Breadth First
Search).
13. Write a Prolog program for DFS (Depth First
Search) .
14. Write a Prolog program for Family Tree
15. Write a Prolog program for Calculator
16. Write a Prolog program for various List
operations .
Q1. Write a prolog program to calculate the sum of two numbers.

Source Code :-

% Predicate to calculate the sum


sum(X,Y):-
S is X+Y,
write(S).

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 :-

% Predicate to calculate the maximum


max(X, Y, M) :-
( X>Y
-> M = X
; M=Y
).

Output :-
Q3. Write a program in PROLOG to implement factorial (N, F) where F represents the factorial of a number N.

Source Code:

% Base case: factorial of 0 is 1


factorial(0, 1).

% Handling negative numbers


factorial(N, error(factorial_is_not_defined_for_negative_numbers))
:-
integer(N),
N < 0.

% Recursive case: N! = N * (N-1)!


factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.

Output:

Q4. Write a Prolog program to implement member(A, S): to check whether A is a member of S or not.

Source Code:

% Base case: A is a member of the list [A|_]


member(A, [A|_]).

/* Recursive case: A is a member of the list [_|Tail] if A is a member of


Tail */
member(A, [_|Tail]) :- member(A, Tail).

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:

% Predicate to calculate the multiply


multiply(X1, X2, M) :-
M is X1 * X2.

Output:

Q6. Write a Prolog program to implement GCD of two numbers.

Source Code:
% Base case: GCD of X and 0 is X
gcd(X, 0, X).

% Recursive case: GCD of X and Y is the GCD of Y and X mod Y


gcd(X, Y, GCD) :-
Y > 0,
R is X mod Y,
gcd(Y, R, GCD).

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

% Alternative implementation for maxList/2


maxList([X], X).
maxList([H|T], M) :-
maxList(T, M1),
M is max(H, M1).

% Alternative implementation for minList/2


minList([X], X).
minList([H|T], M) :-
minList(T, M1),
M is min(H, M1).

Output:
Q9. Write a Prolog program to merge two lists.

Source Code:
% Base case: Append empty list to another list
merge([], L, L).

% Recursive case: Merge heads and tails


merge([H|T1], L2, [H|T]) :-
merge(T1, L2, T).

% Alternative implementation using append/3


merge(L1, L2, L) :-
append(L1, L2, 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:

% Base case: Insert at 1st position


insert_nth(I, 1, S, [I|S]).

% Recursive case: Insert at Nth position


insert_nth(I, N, [H|T], [H|R]) :-
N > 1,
N1 is N - 1,
insert_nth(I, N1, T, R).

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:

% Base case: Delete 1st position


delete_nth(1, [_|T], T).

% Recursive case: Delete Nth position


delete_nth(N, [H|T], [H|R]) :-
N > 1,
N1 is N - 1,
delete_nth(N1, T, R).

Output:
Q12. Write a Prolog program for BFS (Breadth First Search).

Source Code:

% Define the graph as a list of edges


edge(a, b).
edge(a, c).
edge(b, d).
edge(c, e).
edge(d, f).
edge(e, f).

% 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:

% Define the graph as a list of edges


edge(a, b).
edge(a, c).
edge(b, d).
edge(c, e).
edge(d, f).
edge(e, f).

% 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:

Q14. Write a Prolog program for Family Tree.

Source Code:

%% Define family relationships


father(john, emily).
father(john, michael).
mother(mary, emily).
mother(mary, michael).
father(david, sarah).
mother(emily, sarah).
father(michael, oliver).
mother(sarah, oliver).

%% Define gender
male(john).
male(michael).
male(david).
male(oliver).
female(mary).
female(emily).
female(sarah).

%% Define parent and child relationships


parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).

child(X, Y) :- parent(Y, X).

%% Define sibling relationship


sibling(X, Y) :-
parent(Z, X),
parent(Z, Y),
X \= Y.

%% Define sister relationship


sister(X, Y) :-
sibling(X, Y),
female(X).

%% Define brother relationship


brother(X, Y) :-
sibling(X, Y),
male(X).

%% Define grandparent relationship


grandparent(X, Y) :-
parent(X, Z),
parent(Z, Y).

%% Define grandfather relationship


grandfather(X, Y) :-
grandparent(X, Y),
male(X).

%% Define grandmother relationship


grandmother(X, Y) :-
grandparent(X, Y),
female(X).

Output:
Q15. Write a Prolog program for Calculator .

Source Code:
%% Arithmetic Operations

%% sum(A, B) calculates the sum of A and B


sum(A, B) :-
C is A + B,
write('Result: '), write(C), nl.

%% diff(A, B) calculates the difference of A and B


diff(A, B) :-
C is A - B,
write('Result: '), write(C), nl.

%% multi(A, B) calculates the product of A and B


multi(A, B) :-
C is A * B,
write('Result: '), write(C), nl.

%% divide(A, B) calculates the quotient of A and B


divide(A, B) :-
B =\= 0,
C is A / B,
write('Result: '), write(C), nl.
divide(A, 0) :-
write('Error: Division by zero!'), nl.

%% sqr(N, M) calculates N raised to the power of M


sqr(N, M) :-
Z is N ** M,
write('Result: '), write(Z), nl.

%% 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 :

Q16. Write a Prolog program for various List operations .

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

% Define the agent's goals


goal(reach_goal1).
goal(reach_goal2).

% Define the agent's actions


action(move_forward).
action(move_backward).
action(turn_left).
action(turn_right).

% Define the agent's planning rules


planning_rule(reach_goal1, [move_forward, turn_left]).
planning_rule(reach_goal2, [move_backward, turn_right]).

% Define the agent's reasoning rules


reasoning_rule(fact1, [kb(fact1)]).
reasoning_rule(fact2, [kb(fact2)]).
reasoning_rule(fact3, [kb(fact3)]).

% Define the agent's decision-making rules


decision_rule(reach_goal1, [goal(reach_goal1), planning_rule(reach_goal1, Actions)]).
decision_rule(reach_goal2, [goal(reach_goal2), planning_rule(reach_goal2, Actions)]).

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

% Define the relationships between entities


relationship(has, mall, store).
relationship(has, store, product).
relationship(buys, customer, product).
relationship(works_at, employee, store).
relationship(visited_by, mall, customer).

% Define the attributes of entities


attribute(mall, name, string).
attribute(mall, location, string).
attribute(store, name, string).
attribute(store, type, string).
attribute(product, name, string).
attribute(product, price, number).
attribute(customer, name, string).
attribute(customer, age, number).
attribute(employee, name, string).
attribute(employee, role, string).

% Define the frames for entities


frame(mall, [
name,
location,
has(store)
]).

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

% Sample data for the shopping mall


instance(mall, [
name = 'Springfield Mall',
location = '123 Main St'
]).

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:

% Define the predicate for "hostile nation"


hostile_nation(abc).

% Define the predicate for "Indian software"


indian_software(S) :- software(S), indian(S).

% Define the predicate for "sold software to hostile nation"


sold_to_hostile_nation(X, S, N) :-
indian(X),
indian_software(S),
hostile_nation(N),
sold(X, S, N).

% Define the predicate for "criminal"


criminal(X) :-
indian(X),
sold_to_hostile_nation(X, _, _).

% Define the facts


software(some_software).
indian(some_software).
indian/george).
sold/george, some_software, abc).

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:

% Define the predicates


loves(X, Y) :- animal(Y), loves_all_animals(X).
kills(X, Y) :- animal(Y), kills_animal(X).
loved_by_someone(X) :- loves(_, X).
loved_by_no_one(X) :- \+ loves(_, X).

% Define the clauses


clause1(X) :- loves_all_animals(X) -> loved_by_someone(X).
clause2(X) :- kills_animal(X) -> loved_by_no_one(X).
clause3 :- loves_all_animals(gokul).
clause4 :- kills(lucy, X) -> (X = gokul ; X = vibhuti).

% Define the facts


animal(lucy).
loves_all_animals(gokul).
kills_animal(vibhuti).
kills(lucy, vibhuti).

% Define the query


query :- kills(lucy, vibhuti).

Output:

Q21. Write a Prolog code for Bayesian Network .

Source Code:

% Define the nodes of the Bayesian network


node(cavity).
node(toothache).
node(brushing).

% Define the edges of the Bayesian network


edge(cavity, toothache).
edge(cavity, brushing).

% Define the conditional probability tables (CPTs)


cpt(cavity, [0.1, 0.7]).
cpt(toothache, cavity, [0.6, 0.9]).
cpt(brushing, cavity, [0.4, 0.8]).

% Define the query


query(P) :-
cpt(cavity, [P1, P2]),
cpt(toothache, cavity, [T_CPT1, T_CPT2]),
cpt(brushing, cavity, [B_CPT1, B_CPT2]),
P3 is P1 * T_CPT1 * B_CPT1,
P4 is P2 * T_CPT2 * B_CPT2,
P is P3 / (P3 + P4).
Output:

Q22. Write a Prolog code for designing Semantic Network / Frames for Drawing Room.

Source Code:

% Define the classes


class(drawing_room).
class(furniture).
class(chair).
class(table).
class(sofa).
class(decoration).
class(painting).
class(vase).

% Define the instances


instance(drawing_room1, drawing_room).
instance(chair1, chair).
instance(table1, table).
instance(sofa1, sofa).
instance(painting1, painting).
instance(vase1, vase).

% Define the relationships


relation(drawing_room1, has, chair1).
relation(drawing_room1, has, table1).
relation(drawing_room1, has, sofa1).
relation(drawing_room1, has, painting1).
relation(drawing_room1, has, vase1).

relation(chair1, is_a, chair).


relation(table1, is_a, table).
relation(sofa1, is_a, sofa).
relation(painting1, is_a, painting).
relation(vase1, is_a, vase).

relation(chair, is_a, furniture).


relation(table, is_a, furniture).
relation(sofa, is_a, furniture).
relation(painting, is_a, decoration).
relation(vase, is_a, decoration).

% Define the queries


query(drawing_room_contents) :-
relation(drawing_room1, has, X),
write(X), nl, fail.
query(drawing_room_contents).

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:

Q23. Write a Prolog program for Best-First Search.

Source code:

% Define the nodes and their heuristic values


node(a, 10).
node(b, 8).
node(c, 5).
node(d, 3).
node(e, 1).
node(f, 0).

% Define the edges between nodes


edge(a, b).
edge(a, c).
edge(b, d).
edge(c, e).
edge(d, f).
edge(e, f).

% Define the best first search function


best_first_search(Start, Goal, Path) :-
heuristic(Start, H),
best_first_search(Start, Goal, [Start], H, Path).

best_first_search(Current, Goal, Visited, _, [Current|Visited]) :-


Current = Goal.

best_first_search(Current, Goal, Visited, H, Path) :-


edge(Current, Next),
\+ member(Next, Visited),
heuristic(Next, H1),
H1 < H,
best_first_search(Next, Goal, [Next|Visited], H1, Path).

% Define the heuristic function


heuristic(Node, H) :-
node(Node, H).

Output:

Q24. Write a Prolog program for Heuristic Search.

Source code:

% Define the nodes and their heuristic values


node(a, 10).
node(b, 8).
node(c, 5).
node(d, 3).
node(e, 1).
node(f, 0).

% Define the edges between nodes


edge(a, b, 2).
edge(a, c, 3).
edge(b, d, 1).
edge(c, e, 2).
edge(d, f, 1).
edge(e, f, 2).

% Define the heuristic search function


heuristic_search(Start, Goal, Path, Cost) :-
heuristic_search(Start, Goal, [Start], Path, 0, Cost).

heuristic_search(Current, Goal, Visited, Path, Cost, TotalCost) :-


Current = Goal,
reverse(Visited, Path),
TotalCost is Cost.

heuristic_search(Current, Goal, Visited, Path, Cost, TotalCost) :-


edge(Current, Next, EdgeCost),
\+ member(Next, Visited),
NewCost is Cost + EdgeCost,
heuristic_search(Next, Goal, [Next|Visited], Path, NewCost, TotalCost).

% Define the heuristic function


heuristic(Node, H) :-
node(Node, H).

Output:
Q25. Write a Prolog program for A* search.

Source code:

% Define the nodes and their heuristic values


node(a, 10).
node(b, 8).
node(c, 5).
node(d, 3).
node(e, 1).
node(f, 0).

% Define the edges between nodes


edge(a, b, 2).
edge(a, c, 3).
edge(b, d, 1).
edge(c, e, 2).
edge(d, f, 1).
edge(e, f, 2).

% Define the A* search function


a_star_search(Start, Goal, Path, Cost) :-
a_star_search(Start, Goal, [Start], Path, 0, Cost).

a_star_search(Current, Goal, Visited, Path, Cost, TotalCost) :-


Current = Goal,
reverse(Visited, Path),
TotalCost is Cost.

a_star_search(Current, Goal, Visited, Path, Cost, TotalCost) :-


edge(Current, Next, EdgeCost),
\+ member(Next, Visited),
NewCost is Cost + EdgeCost,
heuristic(Next, H),
EstimatedCost is NewCost + H,
a_star_search(Next, Goal, [Next|Visited], Path, NewCost, TotalCost).

% Define the heuristic function


heuristic(Node, H) :-
node(Node, H).

Output:

Q26. Write a Prolog program for Game Search.


Source code:

% Define the game tree


game_tree(
root,
[node1, node2],
[
arc(root, node1, 1),
arc(root, node2, 2),
arc(node1, node3, 3),
arc(node1, node4, 4),
arc(node2, node5, 5),
arc(node3, node6, 6),
arc(node4, node6, 7),
arc(node5, node6, 8)
]
).

% Define the goal node


goal_node(node6).

% Define the game search function


game_search(GameTree, GoalNode, Path) :-
game_tree(GameTree, _, Arcs),
search(Arcs, GameTree, GoalNode, Path).

% Define the search function


search(Arcs, CurrentNode, GoalNode, [CurrentNode|Path]) :-
goal_node(GoalNode),
CurrentNode = GoalNode.

search(Arcs, CurrentNode, GoalNode, [CurrentNode|Path]) :-


member(arc(CurrentNode, NextNode, _), Arcs),
search(Arcs, NextNode, GoalNode, Path).
Output:

Q27. Write a Prolog program for Search Tree & Graph.

Source code:

% Define the search tree


tree(root, [node1, node2]).
tree(node1, [node3, node4]).
tree(node2, [node5, node6]).
tree(node3, []).
tree(node4, []).
tree(node5, []).
tree(node6, []).

% Define the search graph


graph([root, node1, node2, node3, node4, node5, node6]).
edge(root, node1).
edge(root, node2).
edge(node1, node3).
edge(node1, node4).
edge(node2, node5).
edge(node2, node6).

% Define the depth-first search function for the tree


dfs_tree(Node, Path) :-
tree(Node, Children),
dfs_tree(Node, Children, Path).

dfs_tree(Node, [], [Node]).


dfs_tree(Node, [Child|Children], [Node|Path]) :-
dfs_tree(Child, Children, Path).

% Define the depth-first search function for the graph


dfs_graph(Node, Path) :-
graph(Nodes),
member(Node, Nodes),
dfs_graph(Node, Nodes, Path).

dfs_graph(Node, _, [Node]).
dfs_graph(Node, Nodes, [Node|Path]) :-
edge(Node, Next),
member(Next, Nodes),
dfs_graph(Next, Nodes, Path).

% Define the breadth-first search function for the tree


bfs_tree(Node, Path) :-
tree(Node, Children),
bfs_tree(Node, Children, Path).

bfs_tree(Node, [], [Node]).


bfs_tree(Node, [Child|Children], [Node|Path]) :-
bfs_tree(Child, Children, Path).

% Define the breadth-first search function for the graph


bfs_graph(Node, Path) :-
graph(Nodes),
member(Node, Nodes),
bfs_graph(Node, Nodes, Path).

bfs_graph(Node, _, [Node]).
bfs_graph(Node, Nodes, [Node|Path]) :-
edge(Node, Next),
member(Next, Nodes),
bfs_graph(Next, Nodes, Path).

% Run the search queries


?- dfs_tree(root, Path).
?- dfs_graph(root, Path).
?- bfs_tree(root, Path).
?- bfs_graph(root, Path).

Output:
Q28. How Basic Input and Output can be performed Implementation using Prolog code.

Source code:

% Read a line of input from the console


read(Line) :-
read_line_to_codes(user_input, Codes),
atom_codes(Line, Codes).

% Read a number from the console


read(Number) :-
read_line_to_codes(user_input, Codes),
atom_codes(Line, Codes),
atom_number(Line, Number).

% Write a message to the console


write_output(Message) :-
write(Message),
nl.
Output:

Q29 . Prolog Code for Semantic Network Implementation.

Source code:

% Family relations
parent(john, mary).
parent(john, peter).
sibling(mary, peter).

Output:

Q30. Write a Prolog Code for Frames Implementation.

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

set_slot([Slot=OldValue|Rest], Slot, Value, [Slot=Value|Rest]).


set_slot([OtherSlot=OtherValue|Rest], Slot, Value, [OtherSlot=OtherValue|NewRest]) :-
set_slot(Rest, Slot, Value, NewRest).

get_slot([Slot=Value|_], Slot, Value).


get_slot([_|Rest], Slot, Value) :-
get_slot(Rest, Slot, Value).

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:

You might also like