A LEVEL COMPUTING
Declarative Language
Theory
4. Practice
Consider the Prolog database from the previous page:
- Fact: spouse (john, jane)
- Fact: spouse (david, mary)
- Fact: spouse(george, susan)
- Fact: female (jane)
- Fact: female (mary)
- Fact: female (susan)
- Fact: male (john)
- Fact: male (david)
- Fact: male (george)
- Rule: husband(A,B) IF spouse(A,B) AND male (A)
- Rule: wife (A,B) IF spouse(A,B) AND female(B)
Now the database will be expanded to include some additional details. The following questions and answers help show how this is done.
Question and Answers:
Click on each question to see the answer
How would you write the fact 'Sandra is female'
female(sandra)
How could you write the fact 'Sandra and Mary are related'
related(mary,sandra)
Write a rule to check if two people are female family members
female_family(A,B) IF related(A,B) AND female(A) AND female(B)
Write a predicate statement to see if Sandra and Jane are related
female_family(jane,sandra)
False is returned
False is returned
Write a predicate to find the Sandra's sister
female_family(A,sandra)
The answer is Mary i.e. the argument 'A' is instantiated to 'Mary' and the predicate is True
The answer is Mary i.e. the argument 'A' is instantiated to 'Mary' and the predicate is True
Click to view completed database
- Fact: spouse (john, jane)
- Fact: spouse (david, mary)
- Fact: spouse(george, susan)
- Fact: related(mary, sandra)
- Fact: female (jane)
- Fact: female (mary)
- Fact: female (susan)
- Fact: female(sandra)
- Fact: male (john)
- Fact: male (david)
- Fact: male (george)
- Rule: husband(A,B) IF spouse(A,B) AND male (A)
- Rule: wife (A,B) IF spouse(A,B) AND female(B)
- Rule: female_family(A,B) IF related(A,B) AND female(A) AND female(B)
Copyright © www.teach-ict.com