0% found this document useful (0 votes)
225 views

CSE 428 - Solutions To Exercises On Logic Programming and Prolog

This document provides solutions to exercises on logic programming and Prolog. It defines several predicates with arguments like bohrXY, cui(X,Y), gado(X,Y), dsedn(X,Y) to represent relationships between variables. It then shows the SLD-tree for each predicate to demonstrate how they would be solved using Prolog. It also defines predicates for representing lists and their properties, like length, membership, position of elements. Finally it defines predicates for representing binary trees and operations on them like preorder traversal.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

CSE 428 - Solutions To Exercises On Logic Programming and Prolog

This document provides solutions to exercises on logic programming and Prolog. It defines several predicates with arguments like bohrXY, cui(X,Y), gado(X,Y), dsedn(X,Y) to represent relationships between variables. It then shows the SLD-tree for each predicate to demonstrate how they would be solved using Prolog. It also defines predicates for representing lists and their properties, like length, membership, position of elements. Finally it defines predicates for representing binary trees and operations on them like preorder traversal.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

27/12/2011

CSE 428: Solutions to exercises on Logic Programming and Prolog

CSE 428: Solutions to e ercises on Logic Programming and Prolog


The e a e c i "(d)" a d f "diffic ". E e ci e i ia f CSE 428. e gi e a Defi e a Defi e a Defi e a Defi e a C ide e f fac edica e edica e edica e edica e he f f he f h e a ed i h "(d)" igh a ea i ca didac e a ,b i he a da d

1. A 1. 2. 3. 4. 5.

f t e ( a e , a e ) (n m 1 i ahrnm1nm2 ae

hich h d iff X a d Y a e b c u i ( , ) hich h d iff X a d Y a e c osnXY g a d o ( , ) hich h d iff X i a g a d rnsnXY d s e d n ( , ) hich h d iff X i a de ce ecnetXY i g ge ea gica ee:
bohrXY rte(,) %1 %2 %3 %4 %5

he fa he f n m 2 a e ). he . i . f Y . de f Y .

fte(,) ahrab. fte(,) ahrac. fte(,) ahrbd. fte(,) ahrbe. fte(,) ahrcf.

h e g a hica e e e a i
a /\ b c /\ d ef

i:

Sa

hich a

e ,a di

hich

de , a e ge e a ed b

defi i i

he

e ie

? bohrXY. - rte(,) ? cui(,) - osnXY. ? gado(,) - rnsnXY. ? dsedn(,) - ecnetXY.

if

e b d a i g he SLD- ee (a a P

g ea ch ee) f each

e , a ea

che a ica .

Sol ion
1. 2. 3. 4. 5.
bohrXY : fte(,) fte(,) ntXY. rte(,) - ahrZX, ahrZY, o(=) %6

cui(,): fte(,) fte(,) bohrZW.%7 osnXY - ahrZX, ahrWY, rte(,) gado(,): fte(,) fte(,) rnsnXY - ahrZX, ahrYZ. dsedn(,): fte(,) ecnetXY - ahrYX. dsedn(,): fte(,) dsedn(,) ecnetXY - ahrZX, ecnetZY. ? bohrXY. - rte(,) X=b Y=c; X=c Y=b; X=d Y=e; X=e Y=d; N o ? cui(,) - osnXY. X=d Y=f; X=e Y=f; X=f Y=d; X=f Y=e; N o ? gado(,) - rnsnXY. X=d Y=a; X=e Y=a; X=f Y=a; N o %8 %9 %1 0

www.cse.psu.edu/ catuscia/teaching/cg428/exercises/Prolog_solutions.html

1/6

27/12/2011

CSE 428: Solutions to exercises on Logic Programming and Prolog

? dsedn(,) - ecnetXY. X=b Y=a; X=c Y=a; X=d Y=b; X=e Y=b; X=f Y=c; X=d Y=a; X=e Y=a; X=f Y=a; N o

We d a

he SLD- ee f

he fi

e . Abb e ia i : fa a d f fa he .
? bohrXY. - rte(,) ----------() 6

ZaXb/ = = () 1 / ? f(,) - aaY, -----ntbY. o(=) / \ () () 1 2 \ .. . .. . fi al Xb = Yc =

? f(,) f(,) ntXY. - aZX, aZY, o(=) -----ZaXc / = = ZbXd = = ZbXe\ = = ZcXf\ = = () 2 () 3 () 4 () 5 / \ \ ? f(,) - aaY, ? f(,) - abY, ? f(,) - abY, ? f(,) - acY, --------------------ntcY. o(=) ntdY. o(=) nteY. o(=) ntfY. o(=) / \ / \ / \ () () 1 2 () () 3 4 () () 3 4 () 5 / \ Yd / = \ / \ .. . .. ?ntdd ?ntde . -o(=) -o(=) .. . .. . .. . Xc = fi al fi al sces ucs Xe = fi al fi al Yb = XdYe = = Yd =

2. Defi e a

edica e r v r e L K hich h d if a d ees(,)

if he i

Ki

he e e e f he i

L .

Sol ion
Nai e, i effice ( ad a ic) i :

niervre[,]. av_ees(][) niervre[ L,): niervreLM,apn(,X,) av_ees(X ]K - av_ees(,) pedM[]K.

Fa ( i ea ), ai- ec

ie

i :

fs_ees(,): rvaxLK[) atrvreLK - e_u(,,]. rvax[,,) e_u(]KK. rvax[ L,,): rvaxLK[ M)rvre e_u(X ]KM - e_u(,,X ].ees

3. C

1. 2. 3. 4. 5. 6. N e

ide a e e e a i f e a i . Defi e he f ig m m e ( , ) hich h d iff he e e e X cc ebrXL, i s b e ( , ) hich h d iff L i a b e f K ustLK, . d s o n ( , ) hich h d iff L a d K a e di j i ijitLK, u i n L K M , hich h d iff M i he i no(,,) f La i t r e t o ( , , ) hich h d iff M i he i e nescinLKM, d f e e c ( , , ) hich h d iff M i he diffe e ifrneLKM, ha he i he e e e ci e de e d he a i ih ib e e e i i f he a e e e e i ih e e ii (i ca e f i f i ege ) de ed i each f he e e e e a i , gi e i he ab

edica e :
L .

(i.e. he ha e e e e i c ). d K . ec i f La d K . ce f L a d K . decide e e e e . The e a e a i

ibii ie :

be

Sol ion
Li 1. ih ib e e e i i f he a e e e e

: rdfn_ytmpeiaemme(,). - eeiesse_rdct(ebr__)
2/6

www.cse.psu.edu/ catuscia/teaching/cg428/exercises/Prolog_solutions.html

27/12/2011

CSE 428: Solutions to exercises on Logic Programming and Prolog

mme(,X_) ebrX[ ]. mme(,_L): mme(,) ebrX[ ] - ebrXL.

2.

: rdfn_ytmpeiaesbe(,). - eeiesse_rdct(ust__) sbe(]_. ust[,) sbe(XL,): mme(,) sbe(,) ust[ ]K - ebrXK, ustLK. dson(]_. ijit[,) dson(XL,): ntmme(,),dson(,) ijit[ ]K - o(ebrXK) ijitLK. : rdfn_ytmpeiaeuin___) - eeiesse_rdct(no(,,). uinLKM : apn(,,) no(,,) - pedLKM. : rdfn_ytmpeiaeitreto(,,). - eeiesse_rdct(nescin___) itreto(]_[) nescin[,,]. itreto(XL,,): ntmme(,),itreto(,,) nescin[ ]KM - o(ebrXK) nescinLKM. itreto(XL,,XM): mme(,) itreto(,,) nescin[ ]K[ ] - ebrXK, nescinLKM. dfeec(]_[) ifrne[,,]. dfeec(XL,,): mme(,) dfeec(,,) ifrne[ ]KM - ebrXK, ifrneLKM. dfeec(XL,,XM): ntmme(,),dfeec(,,) ifrne[ ]K[ ] - o(ebrXK) ifrneLKM.

3. 4. 5.

6.

Lists without repetitions. The main difference is in the implementation of the predicate u i n Since we do not want repetitions, we no. have to check, for ever element of the first list, that it does not appear alread in the second list.
: rdfn_ytmpeiaeuin___) - eeiesse_rdct(no(,,). uin[,,) no(]KK. uin[ L,,XM): ntmme(,),uinLKM. no(X ]K[ ] - o(ebrXK) no(,,) uin[ L,,): mme(,) uinLKM. no(X ]KM - ebrXK, no(,,)

The other predicates can be defined as before (although for some of them we could give a more efficient definition b using the "cut"). Ordered lists of integers. The main difference is that, in the implementation of the predicate u i n we have to ensure that the no, output list is ordered. This can be done with a predicate o d r d m r e ree_eg:
: rdfn_ytmpeiaeuin___) - eeiesse_rdct(no(,,). uinLKM : odrdmreLKM. no(,,) - ree_eg(,,) odrdmre[,,) ree_eg(]KK. odrdmreL[,) ree_eg(,]L. odrdmre[ L,YK,XM): X<Y odrdmreL[ K,) ree_eg(X ][ ][ ] , ree_eg(,Y ]M. odrdmre[ L,YK,YM): Y<X odrdmre[ L,,) ree_eg(X ][ ][ ] , ree_eg(X ]KM. odrdmre[ L,YK,XM): X:Y odrdmreLKM. ree_eg(X ][ ][ ] - ==, ree_eg(,,)

Some of the other predicates can be defined more efficientl , b using the fact that lists are ordered: 4. Define a predicate l n t ( , ) which holds iff N is the length of the list L eghLN .

Sol ion
: rdfn_ytmpeiaelnt(,). - eeiesse_rdct(egh__) lnt(]0. egh[,) lnt(_L,): lnt(,) Ni M1 egh[ ]N - eghLM, s +.

5. Define a predicate o c r e c s X L N which holds iff the element X occurs N times in the list L curne(,,) .

Sol ion
ocrecs_[,) curne(,]0. ocrecsX[ L,): ocrecsXLM,Ni M1 curne(,X ]N - curne(,,) s +. ocrecsX[ L,): ntXY,ocrecsXLN. curne(,Y ]N - o(=) curne(,,)

6. Define a predicate o c r ( , , ) which holds iff X is the element occurring in position N of the list L cusLNX .

Sol ion
www.cse.psu.edu/ catuscia/teaching/cg428/exercises/Prolog_solutions.html 3/6

27/12/2011

CSE 428: Solutions to exercises on Logic Programming and Prolog

ocr(,X_,) cus1[ ]X. ocr(,_L,): N>1 Mi N1 ocr(,,) cusN[ ]X , s -, cusMLX.

7. Defi e a

edica e s m i t L N hich, gi e a i uls(,)

f i ege

L ,

he

f a he e e e

f L .

Sol ion
smit[,) uls(]0. smit[ L,): smitLM,Ni MX uls(X ]N - uls(,) s +.

8. Defi e a edica e a d u _ i t L K hich, gi e a i d_pls(,) he e e e i L he a e i i . E a e:


? adu_it[,,,]K. - d_pls(1234,) K=[,,,0; 1361] n o

f i ege

L ,

a i

f i ege i

hich each e e e i he

fa

Sol ion
adu_itLK : axLK0. d_pls(,) - u(,,) ax[,]_. u(][,) ax[ L,YK,): Yi ZX axLKY. u(X ][ ]Z s +, u(,,)

9. Defi e a edica e q i k o t L K hich, gi e a i ucsr(,) ic .

f i ege

L ,

de ed i

b ai ed f

i h he e h d f

Sol ion
qikot[,]. ucsr(][) qikot[ L,): sltXLL,2, ucsr(X ]K - pi(,,1L) qikotL,1, ucsr(1K) qikotL,2, ucsr(2K) apn(1[ K]K. pedK,X 2,) slt_[,][) pi(,][,]. sltX[ L,,YM): X<Y sltXLKM. pi(,Y ]K[ ] , pi(,,,) sltX[ L,YK,): X> Y sltXLKM. pi(,Y ][ ]M = , pi(,,,)

10. Defi e a edica e m r e L K M hich, gi e eg(,,) ee e f La d K .

de ed i

f i ege

La

d K e ,

de ed i

Mc

ai i g a he

Sol ion
If e d a f u i n If a no. i e ee e i e ee e i he e i he e i g i , he he i i he a e a he o d r d m r e ree_eg i g i , he e ca i e he ga a f : ed ab e f he defi i i

: rdfn_ytmpeiaemre___) - eeiesse_rdct(eg(,,). mreL[,) eg(,]L. mre[,,) eg(]KK. mre[ L,YK,XM): X<Y mreL[ K,) eg(X ][ ][ ] , eg(,Y ]M. mre[ L,YK,YM): X> Y mre[ L,,) eg(X ][ ][ ] = , eg(X ]KM.

11. C

ide a e e e a i
epyt mtb cnb(,1T) ostNT,2

f bi a

ee a e

,a f

teepybnr te h mt iay re tebnr te wt ro Nadlf adrgtsbre T adT h iay re ih ot n et n ih utes 1 n 2

1. Defi e a edica e p e r e ( , ) hich h d iff L i he i f de rodrTL d ced b he e de a e a f he bi a 2. Defi e a edica e s a c _ r e L T hich, gi e a i f i ege L e erhte(,) , a ba a ced ea ch- ee T c ai i g he ee e f L .

ee T .

Sol ion
www.cse.psu.edu/ catuscia/teaching/cg428/exercises/Prolog_solutions.html 4/6

27/12/2011

CSE 428: Solutions to exercises on Logic Programming and Prolog

1. 2.

pere(mtb,]. rodrepyt[) pere(ostNT,2,): pere(1L) pere(2L) apn(NL]L,) rodrcnb(,1T)L - rodrT,1, rodrT,2, ped[ 1,2L. sac_reLT : qikotLK,lnt(,) cntutte(,,) erhte(,) - ucsr(,) eghKN, osrc_reKNT. cntutte(]0epyt. osrc_re[,,mtb) cntutte(,,ostXT,2): N i N/ 2 %itgrdvso osrc_reLNcnb(,1T) - 1 s / , nee iiin N i N-N -1 2 s 1 , dvd(,1L,XL], iieLN,1[ 2) cntutte(1N,1, osrc_reL,1T) cntutte(2N,2. osrc_reL,2T) dvd(,,]L. iieL0[,) dvd(XL,,XL]L): N>0 N i N1 dvd(,1L,2. iie[ ]N[ 1,2 , 1 s -, iieLN,1L)

12. A di ec ed g aph can be ep e en ed in P olog b li ing he a c among he node , a a e of fac (cla e in ance, he cla e
acab. r(,)

i h emp bod ). Fo

o ld ep e en he e i ence of an a c going f om he node a o he node b . 1. Define in P olog a p edica e p t ( , ) hich hold iff he e i an (ac clic) pa h f om he node X o he node Y Be a e of ahXY . loop : he g aph ma con ain c cle , b o p og am ho ld a oid looping on hem. 2. En ich he p e io p og am o o e n no onl he an e e /no, b al o, in ca e of cce , he ac al pa h ep e en ed a a li of node . Namel , define a p edica e p t ( , , ) hich hold iff P i an (ac clic) pa h f om he node X o he node Y In ahXYP . ca e he e i mo e han one ac clic pa h, he p og am ho ld gene a e all of hem. 3. Con ide he follo ing g aph:
acab. r(,) acac. r(,) acbc. r(,) acbd. r(,) accd. r(,) %1 %2 %3 %4 %5

Wha an e a e gi en fo he goal ? p t ( , , ) b - ahadP SLD- ee (aka P olog ea ch ee), a lea chema icall .

ol ion o he la poin ? J

if

an

e b d a ing he

Sol ion
1. In o de o a oid c cle , e ill e an a ilia p edica e i h an addi ional a g men , hich ep e en he li of i i ed node .

pt(,): pt_u(,,X) ahXY - ahaxXY[]. %Tetidagmn i tels o vstdnds h hr ruet s h it f iie oe pt_u(,,): acXY. ahaxXY_ - r(,) pt_u(,,): acXZ,ntmme(,),pt_u(,,ZL) ahaxXYL - r(,) o(ebrZL) ahaxZY[ ].

2. The li of i i ed node , a he end, con ain he node of he pa h. The p oblem i ha hi li ge b il "in e e e", o e ho ld e e e i o ob ain he pa h. Al e na i el , e can ea ch fo he pa h a ing f om he end node, and alking back o a d he a node. He e e follo hi app oach.
pt(,,): pt_u(,,Y,) %Pi tept; ahXYP - ahaxXY[]P. s h ah 6 pt_u(,,,XL): acXY. %Tetidagi tels o vstdnds 7 ahaxXYL[ ] - r(,) h hr r s h it f iie oe. pt_u(,,,): acZY,ntmme(,),pt_u(,,ZL,) ahaxXYLP - r(,) o(ebrZL) ahaxXZ[ ]P. %8

3. The an

e a e he follo ing

? pt(,,) - ahadP. P=[,b d ; a , ] P=[,c d ; a , ] P=[,b c d ; a , , ] N o

The SLD- ee i he follo ing. Abb e ia ion: m and fo membe ; pa and fo pa h_a :
? pt(,,) - ahadP. ---------() 6 ? p(,,d,) - aad[]P. -----------www.cse.psu.edu/ catuscia/teaching/cg428/exercises/Prolog_solutions.html 5/6

27/12/2011

CSE 428: Solutions to exercises on Logic Programming and Prolog

() 8 ? acZd,ntmme(,d),p(,,Zd,) - r(,) o(ebrZ[]) aaZ[,]P. ------/ Zb () = 4 () Zc 5 = / ? ntmb[]) p(,,bd,) - o((,d), aab[,]P. ? ntmc[]) p(,,cd,) - o((,d), aac[,]P. ----------------------/ / ? p(,,bd,) - aab[,]P. ? p(,,cd,) - aac[,]P. --------------------------P = / \ / \ [,,] () () abd 7 8 P[,,] () =acd 7 () 8 / \ / \ ? acab. .. - r(,) . ? acac. - r(,) ? acWc,ntmme(,cd),p(,,Wcd) - r(,) o(ebrW[,]) aaW[,,]. ------- fi al ------------/ / () 1 () 2 Wa() = 2 ()Wb 3 = / / sces ucs sces ucs .. ? ntmme(,cd), . - o(ebrb[,]) fi al ------------------p(,,bcd) aab[,,].

? p(,,bcd) - aab[,,]. -------------/ \ P[,,,] () () =abcd 7 8 / \ ? acab. - r(,) .. . ------fi al / () 1 / sces ucs

www.cse.psu.edu/ catuscia/teaching/cg428/exercises/Prolog_solutions.html

6/6

You might also like