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

Prolog Problem Solving and Code Examples

Uploaded by

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

Prolog Problem Solving and Code Examples

Uploaded by

Kaushal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

17.

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.

Solve using

i. First Order definite clauses


ii. Prolog Code%
iii. Facts

PROLOG CODE:-

indian(george).

hostile_country(abc).

sells(george, indian_software, abc).

% Rules

crime(Person) :-

indian(Person),

sells(Person, indian_software, Country),

hostile_country(Country).

% Query

% To check if George is a criminal:

% ?- crime(george).

OUTPUT :-

[Link] 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?

PROLOG CODE :-
% Facts

Loves_all_animals(gokul).

Cat(lucy).

Animal(lucy).

% Rules

Loved_by_someone(X) :-

Loves_all_animals(X).

Loved_by_no_one(X) :-

Kills(X, Y),

Animal(Y).

Kills(vibhuti, lucy) ; kills(gokul, lucy).

% Query

% To check if Vibhuti killed Lucy:

% ?- kills(vibhuti, lucy).

OUTPUT :

[Link] & explain Semantic networks. Write Prolog code for its implementation based on
a real life situation.

PROLOG CODE :-
% Facts
Isa(rover, dog).

Isa(dog, animal).

Has(animal, tail).

Can(dog, bark).

% Inheritance Rules

Has(X, Y) :- isa(X, Z), has(Z, Y).

Can(X, Y) :- isa(X, Z), can(Z, Y).

% Queries

% ?- has(rover, tail). % Output: true

% ?- can(rover, bark). % Output: true

OUTPUT:-

[Link] & explain frames. Write Prolog code for its implementation.

PROLOG CODE :-
% Facts representing frames (objects) and their slots (attributes)

Frame(dog).

Frame(cat).

% Slots for dog

Has(dog, fur).
Has(dog, tail).

Can(dog, bark).

% Slots for cat

Has(cat, fur).

Has(cat, tail).

Can(cat, meow).

% Inheritance – Animals have tails

Has(Animal, tail) :-

Frame(Animal),

Has(Animal, fur).

OUTPUT :-

[Link] a Bayesian network and its representation, construction and inference. Write
Prolog code for its implementation.

PROLOG CODE

% Define probabilities

Probability(rain, 0.2).

Probability(sprinkler, 0.3).

Probability(grass_wet, [rain, sprinkler], 0.9).

Probability(grass_wet, [rain], 0.8).


Probability(grass_wet, [sprinkler], 0.7).

Probability(grass_wet, [], 0.1).

% Query inference

Query(grass_wet, Prob) :-

Findall(Condition, probability(grass_wet, Condition, _), Conditions),

Calculate_probability(Conditions, Prob).

Calculate_probability([], 0).

Calculate_probability([Condition | Rest], Prob) :-

Probability(grass_wet, Condition, P),

Calculate_probability(Rest, RestProb),

Prob is P + RestProb.

OUTPUT :-

[Link] are 10 rooms and each room has 5 lights. Write a program to switch off all lights of
all rooms using Prolog code. Create a network for it.

PROLOG CODE :-
% Facts: rooms and their lights

Room(1). Room(2). Room(3). Room(4). Room(5).

Room(6). Room(7). Room(8). Room(9). Room(10).


Light(R, L) :- room(R), between(1, 5, L).

% Rule to switch off all lights in all rooms

Switch_off_all :-

Room(R), light(R, L),

Format(‘Switching off light ~w in room ~w~n’, [L, R]),

Fail.

Switch_off_all.

[Link] a Semantic Network/Frame and write Prolog code for:

a. Shopping Mall
b. Drawing room

PROLOG CODE :-
% Shopping Mall: Entities and Relationships

% Entities

Mall(‘SuperMall’).

Shop(‘Clothing Store’). Shop(‘Grocery Store’). Shop(‘Electronics Store’).

Product(‘Clothing’, ‘Clothing Store’). Product(‘Fruits’, ‘Grocery Store’). Product(‘TV’,


‘Electronics Store’).

Customer(‘John’). Customer(‘Alice’).

Employee(‘Mike’). Employee(‘Sara’).

% Relationships

Has(‘SuperMall’, Shop) :- shop(Shop).

Offers(Shop, Product) :- product(Product, Shop).

Buys(‘John’, ‘Clothing’). Buys(‘Alice’, ‘TV’).

Works_in(‘Mike’, ‘Clothing Store’). Works_in(‘Sara’, ‘Electronics Store’).


% Drawing Room: Entities and Relationships

% Entities

Room(‘Drawing Room’).

Furniture(‘Sofa’). Furniture(‘Table’).

Person(‘Alice’). Person(‘Bob’).

% Relationships

Contains(‘Drawing Room’, Item) :- furniture(Item).

Occupies(‘Alice’, ‘Sofa’). Occupies(‘Bob’, ‘Table’).

Sitting(‘Alice’, ‘Sofa’). Sitting(‘Bob’, ‘Table’).

% Queries for Shopping Mall

?- has(‘SuperMall’, Shop).

?- offers(‘Clothing Store’, Product).

?- buys(‘John’, Product).

?- works_in(‘Mike’, Shop).

Bc% Queries for Drawing Room

?- contains(‘Drawing Room’, Furniture).

?- occupies(‘Alice’, Furniture).

?- sitting(‘Bob’, Furniture).
OUTPUT :-

You might also like