0% found this document useful (0 votes)
31 views4 pages

Lecture - 31-Logic Programming

Logic programming is a programming method that utilizes formal logic to make decisions and solve problems in AI. It involves components such as facts, rules, and queries, exemplified by a grandparent rule in first-order logic. The document also provides a Python implementation to simulate logic programming by defining parent relationships and checking grandparent status.

Uploaded by

ji0400623
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)
31 views4 pages

Lecture - 31-Logic Programming

Logic programming is a programming method that utilizes formal logic to make decisions and solve problems in AI. It involves components such as facts, rules, and queries, exemplified by a grandparent rule in first-order logic. The document also provides a Python implementation to simulate logic programming by defining parent relationships and checking grandparent status.

Uploaded by

ji0400623
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
You are on page 1/ 4

Logic Programming

Logic programming is a method of writing programs using formal logic. In Artificial


Intelligence (AI), logic programming is used to make decisions, solve problems, and draw
conclusions by applying logical rules to known facts.
Basic components:
• Facts – things that are always true
• Rules – logical relationships between things
• Queries – questions we ask the system
Example in First Order Logic (FOL):
1. Facts:
• 𝑝𝑎𝑟𝑒𝑛𝑡(𝑗𝑜ℎ𝑛, 𝑚𝑎𝑟𝑦)
• 𝑝𝑎𝑟𝑒𝑛𝑡(𝑚𝑎𝑟𝑦, 𝑡𝑜𝑚)
2. Rule (grandparent):
• ∀𝑋∀𝑌∀𝑍 (𝑝𝑎𝑟𝑒𝑛𝑡(𝑋, 𝑌) ∧ 𝑝𝑎𝑟𝑒𝑛𝑡(𝑌, 𝑍) → 𝑔𝑟𝑎𝑛𝑑𝑝𝑎𝑟𝑒𝑛𝑡(𝑋, 𝑍))
Description:
1. You are given two facts:
1. parent(john, mary)
→ This means John is a parent of Mary.
2. parent(mary, tom)
→ This means Mary is a parent of Tom.
2. Given Rule: Grandparent Definition
Rule:

∀𝑋∀𝑌∀𝑍 𝑝 𝑎𝑟𝑒𝑛𝑡(𝑋, 𝑌) ∧ 𝑝𝑎𝑟𝑒𝑛𝑡(𝑌, 𝑍)


→ 𝑔𝑟𝑎𝑛𝑑𝑝𝑎𝑟𝑒𝑛𝑡(𝑋, 𝑍))

This is a first-order logic rule that defines when someone is a grandparent. Let's break it
down:
• ∀X∀Y∀Z: For all X, Y, and Z (these are variables representing people).
• parent(X, Y) ∧ parent(Y, Z): If X is a parent of Y and Y is a parent of Z,
• → grandparent(X, Z): Then X is a grandparent of Z.
So the rule says:
If someone (X) is a parent of another person (Y), and that person (Y) is a parent of a third
person (Z), then the first person (X) is a grandparent of the third person (Z).
3. Applying the Rule to the Facts
Let’s now apply this rule to the facts provided.
Step-by-Step Matching:
We need to match the pattern parent(X,Y) ∧ parent(Y,Z) with the given facts.
1. parent(john, mary) → we can match this with parent(X, Y):
o So, X = john
o Y = mary
2. parent(mary, tom) → match this with parent(Y, Z):
o Here, Y = mary (same as before, good!)
o Z = tom
This satisfies both parts of the rule:
• parent(john, mary) (X = john, Y = mary)
• parent(mary, tom) (Y = mary, Z = tom)
Now, the rule tells us:
𝑔𝑟𝑎𝑛𝑑𝑝𝑎𝑟𝑒𝑛𝑡(𝑗𝑜ℎ𝑛, 𝑡𝑜𝑚)
New Fact Derived:
grandparent(john, tom)
So John is a grandparent of Tom.
Example in Prolog
parent(john, mary).
parent(mary, tom).

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

Example in Python programming with explanation:


We want to simulate a logic program using Python, without using any special logic libraries.
The problem is:
• Define some parent relationships (facts).
• Create a function (rule) to check if someone is a grandparent of someone else.
• Use this function to query (ask) if a person is a grandparent.
1. Defining Facts
parent = {
('john', 'mary'),
('mary', 'tom')
}
This is a set of tuples representing parent relationships.
Read it as:
• John is a parent of Mary
• Mary is a parent of Tom
So the family tree looks like:
John → Mary → Tom
2. Writing the Rule: Grandparent
We define a function to simulate a logic rule.
def is_grandparent(x, z):
for (a, b) in parent:
if a == x:
for (c, d) in parent:
if b == c and d == z:
return True
return False
What this function does:
• It checks if x is a grandparent of z.
• A person x is a grandparent of z if there is someone y such that:
o x is a parent of y, and
o y is a parent of z
How it works:
1. The first loop finds all children b of person x (i.e., x → b).
2. The second loop checks if that child b is also a parent of someone z (i.e., b → z).
3. If both conditions are met, x is a grandparent of z.
3. Querying the Rule
print(is_grandparent('john', 'tom')) # Output: True
Is John a grandparent of Tom?
Let’s see:
• John → Mary (from facts)
• Mary → Tom (from facts)
So yes, John is a grandparent of Tom → Output: True
print(is_grandparent('mary', 'tom')) # Output: False
Is Mary a grandparent of Tom?
Let’s check:
• Mary → Tom
• But there's no one Mary is a parent of who is also a parent of Tom.
So the function returns False

Final Python Code


# Define facts
parent = {
('john', 'mary'),
('mary', 'tom')
}

# Define rule (grandparent relationship)


def is_grandparent(x, z):
for (a, b) in parent:
if a == x:
for (c, d) in parent:
if b == c and d == z:
return True
return False
# Queries
print(is_grandparent('john', 'tom')) # True
print(is_grandparent('mary', 'tom')) # False

You might also like