Ex. No.
7 Logic Programming
(Mandatory – Graded)
1. Implement using pyDatalog:
Assume given a set of facts of the form father(name1,name2) (name1 is the father
of name2).
a. Define a predicate brother(X,Y) which holds iff X and Y are brothers.
b. Define a predicate cousin(X,Y) which holds iff X and Y are cousins.
c. Define a predicate grandson(X,Y) which holds iff X is a grandson of Y.
d. Define a predicate descendent(X,Y) which holds iff X is a descendent of Y.
e. Consider the following genealogical tree:
a
/ \
b c
/ \ |
d e f
What are the answers generated by your definitions for the queries:
brother(X,Y)
cousin(X,Y)
grandson(X,Y)
descendent(X,Y)
2. Encode the following facts and rules in pyDatalog:
• Bear is big
• Elephant is big
• Cat is small
• Bear is brown
• Cat is black
• Elephant is gray
• An animal is dark if it is black
• An animal is dark if it is brown
Write a query to find which animal is dark and big.
3. The following are the marks scored by 5 students.
Student Name Mark
Ram 90
Raju 45
Priya 85
Carol 70
Shyam 80
Enter the above data using pyDatalog.
Write queries for the following:
a. Print Student name and mark of all students.
b. Who has scored 80 marks?
c. What mark has been scored by Priya?
d. Write a rule ‘passm’ denoting that pass mark is greater than 50. Use the
rule to print all students who failed.
e. Write rules for finding grade letters for a marks and use the rule to find
the grade letter of a given mark.
4. Solve the set of queries in the previous question using imperative programming
paradigm in Python. Store the data in a dictionary.
5. Write a recursive program to find factorial of a number using pyDatalog.