Prolog代写 | CS 381 Programming Lab 5

CS 381 Programming Lab 5

In this lab you will create a number of Prolog rules that define a number of common familial
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.
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.
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.
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 < ./tests/
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 -t t01
Or you can run all tests at once.
$ python