4/8/2021 COMP 3007
H C 3007
COMP 3007 - Assignment
Due: Sunday April 11th @ 11:55pm
[15 marks] For this problem you will build a family tree in a file called a5q1_family.pl. The members of
that family tree may be fiction or non-fiction, so long as they fulfill the requirements below.
a. Establish a database of facts with the members of a family tree for the following clauses:
male(X) % X is male
female(X) % X is female
father(X,Y) % X is the father of Y
mother(X,Y) % X is the mother of Y
married(X,Y) % X is married to Y
b. Write prolog rules to define the following relationships:
parent(X,Y) % True if X is a parent of Y
siblings(X,Y) % True if X and Y are siblings
has_sister(X) % True if X has a sister
has_brother(X) % True if X has a brother
aunt(X,Y) % True if X is an aunt of Y (by lineage or by marriage)
uncle(X,Y) % True if X is an uncle of Y (by lineage or by marriage)
cousin(X Y) % True if X and Y are cousins
4/8/2021 COMP 3007
cousin(X,Y) % True if X and Y are cousins
grandfather(X,Y) % True if X is a grandfather of Y
grandmother(X,Y) % True if X is a grandmother of Y
ancestor(X,Y) % True if X is an ancestor of Y
Please assume "traditional" definitions for the above relationships for consistency in marking. You are not
required to deal with complicated family structures involving things like divorces and re-marriage and
subsequent step-relationships. Just stick with basic definitions.
If you are in any doubt, please confirm your assumptions.
Make sure that you have enough facts to test each relation.
[12 marks total] Given the following database of facts: a5q2_hockey.pl.
Write prolog queries that answer the following questions:
a. On what team does Ryan Hartman play, and in what position?
b. What is the last name of any player who has at least 52 assists?
c. What players play left wing?
d. What teams have a player with the first name "Alex"?
e. What pairs of players play on the same team?
f. What players have between 90 and 100 points (inclusive)? (where points = goals + assists)
g. What is the shot accuracy of the player with the least goals? (where shot accuracy = goals / shots)
h. Who is the most accurate centre player?
The shortforms for positions are as follows: c = "centre", lw = "left wing", rw = "right wing", d =
You do not need to write rules for this problem, only queries.
For submission, include your queries as comments in the database, together with the output
generated when you tested them.
For questions that should result in more than one answer, your queries should resolve one at a time
using backtracking (';').
Your solutions must be queries that pull the relevant information from the provided knowledge base
(i.e., hard-coding results is worth 0 marks).
The following built-in predicates are forbidden for this problem: findall, setof, bagof, findnsols,
[14 marks total] In a file called a5q3_lotto.pl write and test solutions to the following problems. You may
assume the correct argument types are provided, but you may not assume good values.
Some permitted and useful built-in predicates: random/1, integer/1, length/2, sort/2
a. [2 marks] Write a predicate random_int/3 that takes two integers A and B as arguments and returns a
random integer in the range [A,B]. You may not use the built-in random_between/3 predicate.
E.g. ?- random_int(5,20,R). → R=7
b. [3 marks] Write a predicate range/3 that takes two integers A and B as arguments and returns a list
of all integers in the range [A,B].
E.g. ?- range(3,8,L). → L=[3,4,5,6,7,8]
c. [3 marks] Write a predicate remove_nth/4 that takes an integer N and a list L as arguments and
returns the Nth item, as well as the Remaining list without the removed item. (You may not use the
built in nth0 or nth1 predicates).
4/8/2021 COMP 3007
E.g. ?- remove_nth(0,[1,2,3,4,5],A,B). → A=1, B=[2,3,4,5]
E.g. ?- remove_nth(3,[a,c,d,c,l,m,n,o,p],Item,Rest). → Item=c, Rest=[a,c,d,l,m,n,o,p]
d. [4 marks] Write a predicate select_random/3 that takes an integer N and a list L as arguments and
returns N different random elements selected from L.
E.g. ?- select_random(4,[q,w,e,r,t,y,u,i,o,p],L). → [e,o,w,u]
E.g. ?- select_random(4,[a,b,c],L). → false.
e. [2 marks] Write a predicate lotto/1 that returns a list of 7 unique random integers from the range
[1,50] in sorted order.
E.g. ?- lotto(L). → [1,8,12,23,27,36,48]
[14 marks total] In a file called a5q4_trees.pl construct and test solutions to the following problems.
a. [1 mark] Write a predicate list/1 that succeeds if the argument is a list. (You may not use the built-
in is_list predicate for this).
E.g.: ?- list([1,2,3]). → True.
?- list(hello). → False.
b. [4 marks] Write a predicate treeFlat/2 which takes a tree T as argument and returns the flattened list
containing all of the elements in T. You may not use the built-in flatten predicate in your answer.
E.g.: ?- treeFlat([1,[2,3],[[4,],6]],[1,2,3,4,5,6]). → True.
E.g.: ?- treeFlat([1,[2,3],[[4,],6]], L). → L = [1,2,3,4,5,6]
c. [3 marks] Write a predicate treeSum/2 which has two parameters T and S, that succeeds if the sum of
all elements in the tree T equals the number S. You may not use the built-in sumlist predicate in
E.g.: ?- treeSum([[1,[2,3]],4,[5,6,]], S). → S = 28
d. [6 marks] Write a predicate treeSmush/2 which takes a tree T as argument and returns a tree in which
any two adjacent sublists have been merged into one sublist.
E.g.: ?- treeSmush([[a],[b],c,[d]], R). → R = [[a,b],c,[d]]
E.g.: ?- treeSmush([[1,2],,4,[5,,],[,9]], R). → R = [[1,2,3],4,[5,[6,7,8],9]].
Documentation & Testing
Ensure that your name and student number are in comments at the top of all files.
Document the purpose of each predicate including its expected input and output (parameters).
Ensure that your code is well-formatted and easily readable; a happy TA is a generous TA.
Please note, copying and pasting the assignment guidelines does not constitute sufficient
documentation, and will receive zero marks.
You may use any code or text found in lecture or the assignment but you must cite the source
Use of built-in predicates is allowed unless specifically prohibited.
Use of imported libraries is prohibited unless otherwise stated.
You are required to include testing runs for each problem in your submission.
Your testing in Prolog may be presented in one of the following formats:
Include in your submission a separate .txt file per question that includes a copy of the
interactions (queries and responses) for each manual testing run.
Include at the bottom of each file, the automatic test cases to be executed using swi-prolog's
unit testing syntax.
4/8/2021 COMP 3007
u t test g sy ta .
Include in a separate file for each question, the automatic test cases to be executed, as well as
the line ":-[filename]" (without quotes) to consult the target file.
Note: there are limitations to each format, so you may wish to mix formats. Just be clear as to
what and where your testing is.
The specific tests required depend on the question at hand, but should cover all valid inputs and all
possible branches of your code.
Unless otherwise specified, you may assume inputs supplied are of the correct type.
Fabricated test outputs will result in 0 marks for a question.
Your test cases are expected to be unique where appropriate
Please note, copying and pasting the provided example runs does not constitute sufficient testing,
and will receive zero marks.
For best practices: Comment your testing as to what you are testing and why, giving expected output
as well as observed output and explanations for any differences.
[5 marks Total]
An example submission including documentation and testing can be found here: primes.pl.
A similar example demonstrating unit testing (swi-prolog specific) can be found here: testing_primes.pl.
Another example demonstrating unit testing in a separate file, can be found here: testing_primes_alone.pl.
Any code files that are not loadable (using SWI-prolog under default conditions) will result in a
mark of 0 for that question.
You must use any and all provided file and function names for your submission. Failure to follow the
prescribed naming conventions will result in a grade of zero for the affected questions.
Do not use any external libraries unless otherwise stated. Unsolicited use of imports will result in a
mark of zero for any solutions in the given file.
Combine all files into a single .zip file for your submission.
Submit your assignment using cuLearn before the due date.
Marks will be deducted for late submissions.
You are responsible for submitting all files and ENSURING that the submission is completed
If you are having issues with submission, contact me before the due date, afterwards late deductions
Please see the course outline for all submission guidelines. 学霸联盟