本次代写是一个Symbolic Programming相关的限时测试

## Instructions:

Answer two of the three questions, labelled Q1, Q2 and Q3. If you answer all three

questions, your answer to question Q3 will be ignored (and left unmarked). Each

question is worth 50 marks; two questions together are 100 marks.

Please type your answers and upload a PDF or plaintext file in answer to Blackboard

## Question 3. (PDF converters are available online.)

Tip: As partial credit will be awarded where possible, try not to leave any part of a

question you are answering blank. Write down some thoughts that show effort and

understanding (even if the understanding is that the effort falls short). But to avoid

penalties for overly long answers, please be concise.

### Question Q1

(a) The English sentence \Pat is happy every day it is sunny” can be translated

many different ways in Prolog, including (among others)

(t1) ’Pat is happy every day it is sunny’.

(t2) happyPat(sunnyDay).

(t3) sunnyDay(pat,happy).

(t4) happy(pat,X) :- sunnyDay(X).

(t5) pat(happy) :- day(sunny).

The choice of a translation depends on the queries we wish to answer (and the

additional information that may go into a program). Give five queries (q1), (q2),

(q3), (q4) and (q5) that suggest choosing (t1), (t2), (t3), (t4) and (t5),

respectively, so that the Prolog interpreter replies

(i) true (or yes) to (q1) if told (t1)

(ii) When = sunnyDay to (q2) if told (t2)

(iii) Who = pat, What = happy to (q3) if told (t3)

(iv) Who = pat, When = today to (q4) if told (t4) and

sunnyDay(today)

(v) What = happy to (q5) if told (t5) and day(sunny).

(b) Learn Prolog Now defines sublist(Sub,List) to be true when Sub is a prefix

of a suffix of List.

sublist(Sub,List) :- append(_,Suffix,List),

append(Sub,_,Suffix).

This makes [2,3] a sublist of [1,2,3,4].

?- sublist([2,3],[1,2,3,4]).

true

On the other hand, [2,4] is not a sublist of [1,2,3,4].

?- sublist([2,4],[1,2,3,4]).

false

Define a modification subl/2 of sublist so that subl(Sub,List) is true not

only when sublist(Sub,List) is true but more generally, whenever Sub can be

obtained from List by deleting any number of List’s members. For example,

?- subl([2,4],[3,2,0,4]).

true.

?- setof(X,subl([2,X],[3,2,0,4]),L).

L = [0,4]

(c) A list L is non-repeating if no member of L occurs more than once in L. For

example, [2,1] is non-repeating, whereas [2,1,2] is not. Define a predicate

nonRep/1 such that nonRep(L) is true exactly if L is a non-repeating list.

?- nonRep([2,1]).

true.

?- nonRep([2,1,2]).

false.

(d) SWI-Prolog and SWISH have a built-in predicate subset/2 such that

subset(+SubSet,+Set) is described to be

true if all elements of Subset belong to Set as well.

For example,

?- subset([],[1]).

true.

?- subset([1],[1]).

true.

?- subset([2],[1]).

false.

Moreover, in SWI-Prolog and SWISH, we have

?- findall(X,subset(X,[1]),P).

P = [[]].

Why is the list [1] not included in the answer P = [[]]? Explain.

(e) A convenient way to represent non-negative integers is via the predicate

numeral/1 defined in Chapter 3 of Learn Prolog Now

numeral(0).

numeral(succ(X)) :- numeral(X).

(i) Explain how Prolog uses these two clauses to reply to the query

?- X = succ(X), numeral(X).

(ii) Modify numeral to a predicate nu/1 so that

?- X = succ(X), nu(X).

false.

?- nu(X).

X = 0 ;

X = succ(0) ;

X = succ(succ(0)) ;

X = succ(succ(succ(0))) ;

…