0% found this document useful (0 votes)
1K views1 page

Forward Chaining

The document provides a set of inference rules and facts to implement forward chaining in Python. The rules state that a seed will produce a plant, a plant will produce fruit, and a plant being eaten will produce a human. The facts provided are that mango is a plant, mango is being eaten, and sprouts is a seed. The Python code uses global lists to store the facts and rules, and iterates through the facts to assert new facts based on the rules until no new facts can be inferred, printing the final list of facts.

Uploaded by

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

Forward Chaining

The document provides a set of inference rules and facts to implement forward chaining in Python. The rules state that a seed will produce a plant, a plant will produce fruit, and a plant being eaten will produce a human. The facts provided are that mango is a plant, mango is being eaten, and sprouts is a seed. The Python code uses global lists to store the facts and rules, and iterates through the facts to assert new facts based on the rules until no new facts can be inferred, printing the final list of facts.

Uploaded by

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

1)Write a python code for the following inference rules and facts such that the

inference engine generates a list. Implement the code by using Forward Chaining.

Seed(A) ==> Plant(A).


Plant(A) ==> Fruit(A).
Plant(A),Eating(A) ==> Human(A).
Plant("Mango").
Eating("Mango").
Seed("Sprouts").

Ans)
PYTHON CODE:

global facts
global rules

rules = True
facts = [["plant","mango"],["eating","mango"],["seed","sprouts"]]

def assert_fact(fact):
global facts
global rules
if not fact in facts:
facts += [fact]
rules = True

while rules:
rules = False
for A1 in facts:
if A1[0] == "seed":
assert_fact(["plant",A1[1]])
if A1[0] == "plant":
assert_fact(["fruit",A1[1]])
if A1[0] == "plant" and ["eating",A1[1]] in facts:
assert_fact(["human",A1[1]])

print(facts)
output:
[['plant', 'mango'], ['eating', 'mango'], ['seed', 'sprouts'], ['fruit', 'mango'],
['human', 'mango'], ['plant', 'sprouts'], ['fruit', 'sprout
s']

You might also like