4.63 Exercise 4.63
Suppose we are given the following genealogy from Genesis:
(son Adam Cain) (son Cain Enoch) (svmon Enoch Irad) (son Irad Mehujael) (son Mehujael Methushael) (son Methushael Lamech) (wife Lamech Ada) (son Ada Jabal) (son Ada Jubal)
We are asked to write rules to enable the query system to find the following:
The grandson of Cain
The sons oF Lamech
The grandsons of Methushael
To do this, the book suggests to implement the following rules:
If S is the son of F, and F is the son of G, then S is the grandson of G
If W is the wife of M, and S is the son of W, then S is the son of M
We’ll name the first rule grandson (naturally). The implementation is a straightforward translation of the book’s suggestion:
(rule (grandson ?G ?S) (and (son ?G ?F) (son ?F ?S)))
The same goes for the next rule, which we’ll title son, because we don’t want to introduce a separate rule for finding sons through mothers and fathers (this will work because the database allows assertions and rules to share names, and will always look for both of them):
(rule (son ?F ?S) (and (wife ?F ?M) (son ?M ?S)))