1-
What is Prolog?
Prolog (Programming in Logic) is a high-level programming language primarily used for artificial intelligence (AI) and computational linguistics. Unlike procedural programming languages such as C or Python, Prolog is based on logic programming, which is a form of declarative programming. In Prolog, you define facts, rules, and queries to represent knowledge, and the system derives solutions based on logical reasoning.
Prolog is unique because it focuses on what is to be done, not how it is to be done. You express knowledge using logical statements, and the Prolog interpreter uses these to infer answers through its built-in reasoning engine (using backtracking and unification).
What is Artificial Intelligence (AI)?
Artificial Intelligence (AI) refers to the simulation of human intelligence processes by machines, especially computer systems. It involves creating algorithms and models that enable computers to perform tasks typically requiring human-like intelligence, such as learning, reasoning, problem-solving, perception, and language understanding.
AI systems can range from narrow or weak AI (designed to perform a specific task) to general or strong AI (which would possess the ability to understand, learn, and apply intelligence across a wide range of tasks, similar to humans). Most of the AI systems in use today are narrow AI.
Applications of AI
-
Healthcare:
AI can be used for disease diagnosis (e.g., detecting cancer through image analysis), drug discovery, personalized treatment plans, and robotic surgeries.
-
Autonomous Vehicles:
Self-driving cars use AI to process data from cameras, sensors, and radars to make decisions about navigation, obstacle avoidance, and path planning.
-
Finance:
AI is used for fraud detection, algorithmic trading, customer service (chatbots), and risk assessment in finance and banking.
-
Retail:
AI enhances customer experiences through personalized recommendations (e.g., Amazon, Netflix), inventory management, and targeted marketing.
-
Smart Assistants:
AI powers virtual assistants like Siri, Alexa, and Google Assistant, enabling them to understand speech, answer questions, and control smart devices.
-
Manufacturing:
AI is used in predictive maintenance, supply chain optimization, quality control, and industrial robotics.
-
Entertainment:
AI powers recommendation engines on platforms like YouTube, Spotify, and Netflix, suggesting content based on user preferences.
-
Cybersecurity:
AI helps detect anomalies in network traffic and user behavior to prevent cyber-attacks and fraud.
2-
% ----- Likes -----
likes(john, pizza).
likes(john, ice_cream).
likes(sarah, salad).
likes(sarah, ice_cream).
likes(mike, burger).
likes(mike, pizza).
likes(anna, burger).
likes(anna, salad).
% ----- Dislikes -----
dislikes(john, salad).
dislikes(sarah, burger).
dislikes(mike, salad).
dislikes(anna, ice_cream).
% ----- Rules -----
% Rule to check mutual like
mutual_like(X, Y, Food) :-
likes(X, Food),
likes(Y, Food),
X \= Y.
% Rule to check if two people dislike the same food
mutual_dislike(X, Y, Food) :-
dislikes(X, Food),
dislikes(Y, Food),
X \= Y.
% Rule to be friends: share a like and no shared dislikes
friends(X, Y) :-
mutual_like(X, Y, _),
\+ (mutual_dislike(X, Y, _)). % not sharing any mutual dislike
% Rule to find what food people like together
common_likes(X, Y, Food) :-
likes(X, Food),
likes(Y, Food),
X \= Y.
mutual_dislike(mike,john,salad).
likes(mike,what).
3-
bfs
% --- Graph Representation (Undirected for simplicity) ---
edge(a, b).
edge(a, c).
edge(b, d).
edge(c, d).
edge(d, e).
edge(c, f).
% --- Make the graph undirected ---
connected(X, Y) :- edge(X, Y).
connected(X, Y) :- edge(Y, X).
% --- BFS Algorithm ---
bfs(Start, Goal, Path) :-
bfs_queue([[Start]], Goal, RevPath),
reverse(RevPath, Path).
% --- Queue-based traversal ---
bfs_queue([[Goal|Rest]|_], Goal, [Goal|Rest]).
bfs_queue([CurrentPath|OtherPaths], Goal, Path) :-
extend(CurrentPath, NewPaths),
append(OtherPaths, NewPaths, UpdatedQueue),
bfs_queue(UpdatedQueue, Goal, Path).
% --- Extend current path by adding connected nodes not already visited ---
extend([Node|Path], NewPaths) :-
findall([NewNode, Node|Path],
(connected(Node, NewNode), \+ member(NewNode, [Node|Path])),
NewPaths).
bfs(a, e, Path).
4-dfs
% --- Graph representation (undirected edges) ---
edge(a, b).
edge(a, c).
edge(b, d).
edge(c, d).
edge(d, e).
edge(c, f).
% --- Undirected connection ---
connected(X, Y) :- edge(X, Y).
connected(X, Y) :- edge(Y, X).
% --- DFS Algorithm ---
dfs(Start, Goal, Path) :-
dfs_util(Start, Goal, [Start], RevPath),
reverse(RevPath, Path).
% --- Utility: recursive DFS ---
dfs_util(Goal, Goal, Path, Path).
dfs_util(Current, Goal, Visited, Path) :-
connected(Current, Next),
\+ member(Next, Visited),
dfs_util(Next, Goal, [Next|Visited], Path).
dfs(a, e, Path).
5-
% Overflow conditions
water_jug(X, Y) :- X > 4, write('4L water jug overflowed.'), nl.
water_jug(X, Y) :- Y > 3, write('3L water jug overflowed.'), nl.
water_jug(X, Y) :- X > 4, Y > 3, write('Both water jugs overflowed.'), nl.
% Goal state
water_jug(2, 0) :- write('4L:2 & 3L:0 --> Goal State Reached!'), nl.
% Actions
water_jug(0, 0) :- write('4L:0 & 3L:3 (Action: Fill 3L jug)'), nl, water_jug(0, 3).
water_jug(0, 3) :- write('4L:3 & 3L:0 (Action: Pour 3L into 4L jug)'), nl, water_jug(3, 0).
water_jug(3, 0) :- write('4L:3 & 3L:3 (Action: Fill 3L jug)'), nl, water_jug(3, 3).
water_jug(3, 3) :- write('4L:4 & 3L:2 (Action: Pour from 3L to fill 4L jug)'), nl, water_jug(4, 2).
water_jug(4, 2) :- write('4L:0 & 3L:2 (Action: Empty 4L jug)'), nl, water_jug(0, 2).
water_jug(0, 2) :- write('4L:2 & 3L:0 (Action: Pour from 3L to 4L jug)'), nl, water_jug(2, 0).
% Catch-all to stop recursion when no match
water_jug(_, _) :- write('No valid action from this state or already at goal.'), nl.
water_jug(0, 0).
6-
% Base case: Move one disk directly
hanoi(1, Source, Target, _) :-
format('Move disk 1 from ~w to ~w~n', [Source, Target]).
% Recursive case
hanoi(N, Source, Target, Auxiliary) :-
N > 1,
M is N - 1,
hanoi(M, Source, Auxiliary, Target),
format('Move disk ~w from ~w to ~w~n', [N, Source, Target]),
hanoi(M, Auxiliary, Target, Source).
hanoi(3, left, right, middle).
7-
% Facts
parent(john, mary). % John is a parent of Mary
parent(john, mike).
parent(susan, mary).
parent(susan, mike).
parent(mike, emma).
parent(mike, ethan).
parent(lisa, emma).
parent(lisa, ethan).
% Rules
father(X, Y) :- parent(X, Y), male(X).
mother(X, Y) :- parent(X, Y), female(X).
child(X, Y) :- parent(Y, X).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
cousin(X, Y) :- parent(A, X), parent(B, Y), sibling(A, B), X \= Y.
% Gender facts
male(john).
male(mike).
male(ethan).
female(susan).
female(mary).
female(emma).
female(lisa).
8-
% Addition
add(X, Y, Result) :-
Result is X + Y.
% Subtraction
subtract(X, Y, Result) :-
Result is X - Y.
% Multiplication
multiply(X, Y, Result) :-
Result is X * Y.
% Division (with check to avoid division by zero)
divide(X, Y, Result) :-
Y \= 0, % Check that denominator is not zero
Result is X / Y.
divide(_, 0, 'Error: Division by zero') :- % Handle division by zero error
write('Error: Division by zero'), nl.
% Predicate to evaluate an expression (addition or subtraction)
evaluate_expression(X, Y, '+', Result) :-
add(X, Y, Result).
evaluate_expression(X, Y, '-', Result) :-
subtract(X, Y, Result).
evaluate_expression(X, Y, '*', Result) :-
multiply(X, Y, Result).
evaluate_expression(X, Y, '/', Result) :-
divide(X, Y, Result).
divide(10, 2, Result).
Comments
Post a Comment