WeChat:lovexc60
CS 381 Programming Lab 5
In this lab you will create a number of Prolog rules that define a number of common familial
relationships.
In Prolog parlance, we define a rule as its name and its arity. For instance the rule mother/2
indicates a rule named mother with two arguments.
mother(M,C):- parent(M,C), female(M).
Add this rule to your Prolog file. Reload your file. Test your new rule with the query:
?- mother(X, ‘Queen Elizabeth II’).
which should return the single result:
X = ‘Lady Elizabeth Bowes-Lyon’.
Now write the rule father/2 and add it to your file.
2
More Rules
Now write a set of additional rules. In your rules, you are encouraged to make use of other
rules you have written. This may effect the order you choose to implement these rules.
Protip: Make sure you get the order of the arguments correct as switching then will
change the meaning of the function.
spouse/2
child/2
son/2
daughter/2
sibling/2
brother/2
sister/2
uncle/2 two rules: one by blood, one by marriage.
aunt/2 two rules: one by blood, one by marriage.
grandparent/2
grandfather/2
grandmother/2
grandchild/2
For the next two rules, make use of rules you have already written. It may take more than
one rule to define these functions.
Write the rules:
ancestor/2 ancestor(X, Y) means X is the ancestor of Y.
descendant/2 descendant(X, Y) means X is the descendant of Y.
Protip: You may find tracing useful. To debug, you can use the keyword trace to
enable tracing and notrace to disable.
3
Numbers in Prolog
Now we will write some Prolog rules that require numeric comparisons. Write the rules:
older/2
younger/2
The rule older(X, Y) indicates person X is older than person Y. The rule younger/2 should
be written in a similar manner.
Lastly, write the rule regentWhenBorn/2. This rule, regentWhenBorn(X, Y), should ask
who was King or Queen (X) when person Y was born.
Testing
Output Format
Add this line to your code to alter the output formatting to work with the automatic tester.
portray(Term) :- atom(Term), format(“~s”, Term).
Single Test
This will run your code using a single test at the command line and print to stdout.
$ swipl -q lab5.pl < ./tests/t01.in
Protip: The prolog argument −−quiet (or −q for short) runs prolog in quiet mode.
Testing Script
This will run a single test using the provided Python3 script. It runs the specific test given
as the -t argument. In this example, it will run Test t01.
$ python lab5-tester.py lab5.pl -t t01
Or you can run all tests at once.
$ python lab5-tester.py lab5.pl