0% found this document useful (0 votes)
6 views8 pages

Lab Report of AI2

Uploaded by

mdsourovahmed7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Lab Report of AI2

Uploaded by

mdsourovahmed7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment No.

1: Deduction Based on Food Preferences

Statement of the Program:


Write a PROLOG program to deduce what kinds of food Priya and Pranab like based on properties
like being delicious or spicy.

Solution of the Program:


We define facts describing the food (e.g., whether it is delicious or spicy) and define rules for what
Priya and Pranab like based on those food properties.

PROLOG Code:
% Facts
delicious(cakes).
delicious(pickles).
delicious(biryani).
spicy(pickles).
relishes(priya,coffee).

% Rules
likes(priya,Food) :- delicious(Food).
likes(pranab,Food) :- spicy(Food), delicious(Food).
%?- delicious(Food).

Observation.
Experiment No. 2: Student-Teacher-Subject Relationship

Statement of the Program:


Create a PROLOG program to relate students and teachers through the subjects they study and teach.

Solution of the Program:


We define who is a student and teacher, what subjects they are associated with, and create a rule that
connects a student and teacher via a common subject.

PROLOG Code:

% Facts
student(john).
student(sara).
teacher(mr_smith).
teacher(ms_clark).

studies(john,math).
studies(sara,science).

teaches(mr_smith,math).
teaches(ms_clark,science).

% Rules
teaches_subject_to(Teacher, Student, Subject) :-
teacher(Teacher),
student(Student),
teaches(Teacher, Subject),
studies(Student, Subject).

Observation.

Experiment No. 3: Family Relationships

Statement of the Program:


Write a PROLOG program to describe family relationships including parents, siblings, and
grandparents.

Solution of the Program:


Facts are created for parenthood and gender. Rules use logic to infer additional family relations.
PROLOG Code:

parent(john, mary).
parent(jane, mary).
parent(john, mike).
parent(jane, mike).
parent(mary, alice).
parent(tom, alice).

male(john).
male(mike).
male(tom).

female(jane).
female(mary).
female(alice).

% Rules
father(X, Y) :- parent(X, Y), male(X).
mother(X, Y) :- parent(X, Y), female(X).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).

Observation.

Experiment No. 4: Understanding PROLOG Terminology

Statement of the Program:


Learn the concepts of facts, rules, queries, and logical deduction in PROLOG.
Solution of the Program:
This experiment builds a foundational understanding. Examples are derived from previous
experiments.

Experiment No. 5: Using Variables and Querying Facts

Statement of the Program:


Write a PROLOG program to store car brand, color, and type. Query using variables to get details.

Solution of the Program:


We define facts for cars and add a rule to identify eco-friendly (electric) vehicles.

PROLOG Code:

car(maruti, white).
car(fiat, black).
car(tesla, red).

Observation.
Agin:
Code;
% ----------- Knowledge Base (Facts) -----------
% car(Brand, Color, Type).

car(maruti, white, sedan).


car(fiat, black, hatchback).
car(tesla, red, electric).
car(toyota, white, suv).
car(honda, black, sedan).

% ----------- Rule: Eco-friendly Cars -----------


% Electric cars are considered eco-friendly.

eco_friendly(Car) :-
car(Car, _, electric).

%Query:
%?- car(_, Color, _).
%?- car(Brand, black, _).
%?- car(Brand, white, sedan).
%?- car(Brand, _, Type).
%?- eco_friendly(Brand).
%?- car(_, _, Type).
%?- car(Brand, red, _).

Observation.
% ----- Facts -----
a.
b.
c.
d.
e.
f.

% ----- Rules -----


p :- a, b, c.
p :- d, e, f.
p :- (a, b -> c ; (d, e, f)).

%query
%?-p.

Observation.

● .

Previous class work question:(Just practice)

Lab 2:
Code:

% Facts
man(marcus).
pompeian(marcus).
roman(X) :- pompeian(X). % All Pompeians are Romans
ruler(caesar).
tried_to_assassinate(marcus, caesar).

% Rules
% Clauses for hates/2
hates(X, Y) :- tried_to_assassinate(X, Y), ruler(Y). % People hate rulers they try to assassinate
hates(X, caesar) :- roman(X), \+ loyal_to(X, caesar). % Romans hate Caesar if they are not loyal

% Clauses for loyal_to/2


loyal_to(X, caesar) :- roman(X), \+ hates(X, caesar). % Romans are loyal if they do not hate
loyal_to(X, someone) :- man(X), X \= caesar. % Everyone is loyal to someone (not necessarily
Caesar)

%query
%?-hates(marcus, caesar).
%?-loyal_to(marcus, caesar).

Result:
Lab-3:

Code:
% Facts
greek(aristotle).
greek(plato).
student(plato, aristotle).
born(aristotle, -348). % -348 represents 348 BC
lifespan(human, 120).

% Rules
is_greek(X) :- greek(X).
is_alive(X, Year) :-
born(X, BirthYear),
lifespan(human, Lifespan),
DeathYear is BirthYear + Lifespan,
Year =< DeathYear.
% Queries
% ?- is_greek(plato). % To check if Plato was a Greek
% ?- is_alive(plato, 2024). % To check if Plato is alive in 2024

You might also like