! Lucy Lawless Lesbian

Transcription

! Lucy Lawless Lesbian
Carleton University
School of Computer Science
COMP 3007 Assignment 4 (Fall 2013)
Due: Sun, Dec 8, (11:55 pm)
There are two basic styles of logic programs: defining a logical
database, and manipulating data structures. A logic database is
comprised of a set of facts and rules. The first part of this assignment
gives you experience developing facts that define relations, as in
relational databases, and developing rules that define complex relational
queries, as in relational algebra. Together, facts and rules can express
functionalities associated with relational databases. The power of logic
programs comes from their ability to handle recursive data types. The
second part of the assignment gives you experience defining relations
over such recursive types.
Question 1
Establish a database of facts and rules for the following predicates:
father(X,Y) /* X is the father of Y */
mother(X,Y) /* X is the mother of Y */
male(X) /* X is male */
female(X) /* X is female */
parent(X, Y) /* X is the parent of Y */
difference(X,Y) /* X and Y are different */
Write and TEST Prolog clauses to define the following relationships:
is_mother(X) /* X is a mother */
is_father(X) /* X is a father */
aunt(X, Y) /* X is an aunt of Y */
uncle(X, Y) /* X is an uncle of Y */
sister_of(X,Y) /* X is a sister of Y */
grandfather_of(X, Y) /* X is a grandfather of Y */
grandmother_of(X, Y) /* X is a grandmother of Y */
grandchild(X,Y) /* X is a grandchild of Y */
sibling(X,Y) /* X is a sibling of Y, i.e they have the same parents */
half_sibling(X,Y) /*they have same mother but different fathers or
same father, different mothers */
related(X,Y) /* X is related to Y */
ancestor(X, Y) /* X is an ancestor of Y */
descendent(X,Y) /* X is a descendent of Y */
Please assume "traditional" definitions for the above relationships.
Question 2
Write and test a Prolog program to solve the Towers of Hanoi problem.
The problem is to move N disks from the left peg to the right peg using
the center peg as an auxiliary holding peg. At no time can a larger disk
be placed upon a smaller disk. The following diagram depicts the setup
for N=3 disks after the first disk has been moved.
A sample query might look like this:
?- move(3, left, right, center). The output should look similar to this:
Move top disk from left to right
Move top disk from left to center
Question 3
… etc.
Do not use append in question 3!
a) Write a program to find the last element of a list. e.g.
?- last(X, [how, are, you]).
X = you
Yes
b) Write a predicate after(X, List, Result) that returns
everything in a list after any occurrence of the given element X.
?- after(a,[b,a,x,d,a,f,g],R).
R = [x,d,a,f,g];
R = [f,g];
no
c) Write a predicate nextto(X, Y, L) that succeeds when elements
X and Y are immediately consecutive elements of list L. e.g.
?- nextto(a, b, [c,a,b,d]). succeeds
?- nextto(a, b, [c,a,d,b]). fails
d) The predicate occurs_at_position(Element, List,
Position) allows access to the nth element in a list. See the example
queries below:
?- occurs_at_position(x, [a,b,c,x,x,d,e], 5).
yes
?- occurs_at_position(x, [a,b,c,x,x,d,e], Pos).
Pos = 4;
Pos = 5;
no
Question 4
a) Write a program my_append(L1,L2,L3) to append 2 lists. e.g.
?- my_append([a,b],[c,d],L).
L=[a,b,c,d]
b) Rewrite the program of 3a), i.e. last(X,L) using my_append
c) Rewrite the program of 3c), i.e. nextto(X,Y,L) using my_append
d) Write a program reverse(L1,L2) that succeeds when the result of
reversing the elements of list L1 is the list L2. Again use
my_append...
Question 5
a) Write a program prefix(A,B) that holds if list A is a prefix of list
B, e.g.
?- prefix([1,2],[1,2,3,4]).
succeeds
b) Write a program suffix(A,B) that holds if list A is a suffix of list
B, e.g.
?- suffix([3,4],[1,2,3,4]).
succeeds
c) Write a program segment(A,B) that holds if list A is a contiguous
segment of list B, e.g.
?- segment([2,3],[1,2,3,4]).
succeeds
Question 6
We are given the following knowledge base of travel information:
byCar(auckland,hamilton).
byCar(hamilton,raglan).
byCar(valmont,saarbruecken).
byCar(valmont,metz).
byTrain(metz,frankfurt).
byTrain(saarbruecken,frankfurt).
byTrain(metz,paris).
byTrain(saarbruecken,paris).
byPlane(frankfurt,bangkok).
byPlane(frankfurt,singapore).
byPlane(paris,losAngeles).
byPlane(bangkok,auckland).
byPlane(singapore,auckland).
byPlane(losAngeles,auckland).
a) Write a predicate travel/2 which determines whether it is possible to
travel from one place to another by chaining together car, train, and
plane journeys. For example, your program should answer yes to the
query travel(valmont,raglan).
So, by using travel/2 to query the above database, you can find out that
it is possible to go from Valmont to Raglan. If you are planning such a
voyage, that’s already something useful to know, but you would
probably prefer to have the precise route from Valmont to Raglan.
b) Write a predicate travel/3 which tells you which route to take when
travelling from one place to another. For example, the program should
respond
X = go(valmont,metz,
go(metz,paris,
go(paris,losAngeles)))
to the query travel(valmont,losAngeles,X).
c) Extend the predicate travel/3 so that it not only tells you the route to
take to get from one place to another, but also how you have to travel.
That is, the new program should let you know, for each stage of the
voyage, whether we need to travel by car, train, or plane.
Test your predicates thoroughly.
Testing
As always, test thoroughly and present your test results clearly.
REMEMBER: YOU MUST VERIFY AND TEST THAT YOUR
SOLUTIONS WORK FOR ALL POSSIBLE GOALS, NOT JUST FOR
THE ONES PROVIDED. It is up to you to make up a convincing test
plan (i.e. convince the TAs that your procedures work properly).
Submission
Your assignment must be submitted electronically using the
cuLearn system.
Your assignment should include a README.TXT file that
contains details of the testing you have performed and any
special requirements that have for setup. Assignments that are
not adequately commented will have marks deducted. Your
name and student number should be included in a comment
block at the top of each file that you submit. All Prolog files
should have the extension .pl. All text files should have the
extension .txt. Documentation may also be submitted as a
Word .doc file. It is up to you to make up a convincing,
documented test plan.

Similar documents