6
27/12/2011 CSE 428: Solutions to exercises on Logic Programming and Prolog 1/6 www.cse.psu.edu/acatuscia/teaching/cg428/exercises/Prolog_solutions.html CSE 428: Solutions to e[ercises on Logic Programming and Prolog The VXSeUVcUiSW " (d)" VWaQdV fRU "difficXOW". E[eUciVeV ViPiOaU WR WhRVe PaUNed ZiWh " (d)" PighW aSSeaU iQ caQdidac\ e[aPV, bXW QRW iQ Whe VWaQdaUd e[aPV Rf CSE 428. 1. AVVXPe giYeQ a VeW Rf facWV Rf Whe fRUP father(name1,name2) (name1 iV Whe faWheU Rf name2 ). 1. DefiQe a SUedicaWe brother(X,Y) Zhich hROdV iff X aQd Y aUe bURWheUV. 2. DefiQe a SUedicaWe cousin(X,Y) Zhich hROdV iff X aQd Y aUe cRXViQV. 3. DefiQe a SUedicaWe grandson(X,Y) Zhich hROdV iff X iV a gUaQdVRQ Rf Y . 4. DefiQe a SUedicaWe descendent(X,Y) Zhich hROdV iff X iV a deVceQdeQW Rf Y . 5. CRQVideU Whe fROORZiQg geQeaORgicaO WUee: father(a,b). % 1 father(a,c). % 2 father(b,d). % 3 father(b,e). % 4 father(c,f). % 5 ZhRVe gUaShicaO UeSUeVeQWaWiRQ iV: a / \ b c / \ _ d e f Sa\ Zhich aQVZeUV, aQd iQ Zhich RUdeU, aUe geQeUaWed b\ \RXU defiQiWiRQV fRU Whe TXeUieV ?- brother(X,Y). ?- cousin(X,Y). ?- grandson(X,Y). ?- descendent(X,Y). JXVWif\ \RXU aQVZeUV b\ dUaZiQg Whe SLD-WUee (aNaV PURORg VeaUch WUee) fRU each TXeU\, aW OeaVW VchePaWicaOO\. SolXWion 1. brother(X,Y) :- father(Z,X), father(Z,Y), not(X=Y). % 6 2. cousin(X,Y) :- father(Z,X), father(W,Y), brother(Z,W). % 7 3. grandson(X,Y) :- father(Z,X), father(Y,Z). % 8 4. descendent(X,Y) :- father(Y,X). % 9 descendent(X,Y) :- father(Z,X), descendent(Z,Y). % 10 5. ?- brother(X,Y). X = b Y = c ; X = c Y = b ; X = d Y = e ; X = e Y = d ; No ?- cousin(X,Y). X = d Y = f ; X = e Y = f ; X = f Y = d ; X = f Y = e ; No ?- grandson(X,Y). X = d Y = a ; X = e Y = a ; X = f Y = a ; No

CSE 428_ Solutions to Exercises on Logic Programming and Prolog

Embed Size (px)

Citation preview

Page 1: CSE 428_ Solutions to Exercises on Logic Programming and Prolog

27/12/2011 CSE 428: Solutions to exercises on Logic Programming and Prolog

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

CSE 428: Solutions to exercises on Logic Programming and Prolog

The superscript "(d)" stands for "difficult". Exercises similar to those marked with "(d)" might appear in candidacy exams, but not in the standardexams of CSE 428.

1. Assume given a set of facts of the form f a t h e r ( n a m e 1 , n a m e 2 ) (n a m e 1 is the father of n a m e 2 ).1. Define a predicate b r o t h e r ( X , Y ) which holds iff X and Y are brothers.2. Define a predicate c o u s i n ( X , Y ) which holds iff X and Y are cousins.3. Define a predicate g r a n d s o n ( X , Y ) which holds iff X is a grandson of Y .4. Define a predicate d e s c e n d e n t ( X , Y ) which holds iff X is a descendent of Y .5. Consider the following genealogical tree:

f a t h e r ( a , b ) . % 1 f a t h e r ( a , c ) . % 2 f a t h e r ( b , d ) . % 3 f a t h e r ( b , e ) . % 4 f a t h e r ( c , f ) . % 5

whose graphical representation is:

a / \ b c / \ | d e f

Say which answers, and in which order, are generated by your definitions for the queries

? - b r o t h e r ( X , Y ) . ? - c o u s i n ( X , Y ) . ? - g r a n d s o n ( X , Y ) . ? - d e s c e n d e n t ( X , Y ) .

Justify your answers by drawing the SLD-tree (akas Prolog search tree) for each query, at least schematically.

Solution

1. b r o t h e r ( X , Y ) : - f a t h e r ( Z , X ) , f a t h e r ( Z , Y ) , n o t ( X = Y ) . % 6

2. c o u s i n ( X , Y ) : - f a t h e r ( Z , X ) , f a t h e r ( W , Y ) , b r o t h e r ( Z , W ) . % 7

3. g r a n d s o n ( X , Y ) : - f a t h e r ( Z , X ) , f a t h e r ( Y , Z ) . % 8

4. d e s c e n d e n t ( X , Y ) : - f a t h e r ( Y , X ) . % 9

d e s c e n d e n t ( X , Y ) : - f a t h e r ( Z , X ) , d e s c e n d e n t ( Z , Y ) . % 1 0

5. ? - b r o t h e r ( X , Y ) .

X = b Y = c ; X = c Y = b ; X = d Y = e ; X = e Y = d ; N o

? - c o u s i n ( X , Y ) . X = d Y = f ; X = e Y = f ; X = f Y = d ; X = f Y = e ; N o ? - g r a n d s o n ( X , Y ) . X = d Y = a ; X = e Y = a ; X = f Y = a ; N o

Page 2: CSE 428_ Solutions to Exercises on Logic Programming and Prolog

27/12/2011 CSE 428: Solutions to exercises on Logic Programming and Prolog

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

? - d e s c e n d e n t ( X , Y ) . 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 draw the SLD-tree for the first query. Abbreviation: fa stands for father.

? - b r o t h e r ( X , Y ) . - - - - - - - - - - - - | ( 6 ) | ? - f a ( Z , X ) , f a ( Z , Y ) , n o t ( X = Y ) . - - - - - - -Z = a X = b / Z = a X = c / Z = b X = d | Z = b X = e \ Z = c X = f \ ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) / / | \ \? - f a ( a , Y ) , ? - f a ( a , Y ) , ? - f a ( b , Y ) , ? - f a ( b , Y ) , ? - f a ( c , Y ) , - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - n o t ( b = Y ) . n o t ( c = Y ) . n o t ( d = Y ) . n o t ( e = Y ) . n o t ( f = Y ) . / \ / \ / \ / \ |( 1 ) ( 2 ) ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 3 ) ( 4 ) ( 5 ) | \ / \ Y = d / \ / \ |. . . . . . . . . . . . ? - n o t ( d = d ) ? - n o t ( d = e ) . . . . . . . . .f a i l X = b X = c f a i l f a i l s u c c e s s X = e f a i l f a i l Y = c Y = b X = d Y = e Y = d

2. Define a predicate r e v e r s e ( L , K ) which holds if and only if the list K is the reverse of the list L .

Solution

Naive, inefficent (quadratic) solution:

n a i v e _ r e v e r s e ( [ ] , [ ] ) . n a i v e _ r e v e r s e ( [ X | L ] , K ) : - n a i v e _ r e v e r s e ( L , M ) , a p p e n d ( M , [ X ] , K ) .

Fast (linear), tail-recursive solution:

f a s t _ r e v e r s e ( L , K ) : - r e v _ a u x ( L , K , [ ] ) . r e v _ a u x ( [ ] , K , K ) . r e v _ a u x ( [ X | L ] , K , M ) : - r e v _ a u x ( L , K , [ X | M ] ) . r e v e r s e

3. Consider a representation of sets as lists. Define the following predicates:1. m e m b e r ( X , L ) , which holds iff the element X occurs in L .2. s u b s e t ( L , K ) , which holds iff L is a subset of K .3. d i s j o i n t ( L , K ) , which holds iff L and K are disjoint (i.e. they have no elements in common).4. u n i o n ( L , K , M ) , which holds iff M is the union of L and K .5. i n t e r s e c t i o n ( L , K , M ) , which holds iff M is the intersection of L and K .6. d i f f e r e n c e ( L , K , M ) , which holds iff M is the difference of L and K .

Note that the solution to these exercises depends on the way you decide to represent sets. There are various possibilities:lists with possible repetitions of the same elementlists without repetitions(in case of lists of integers) ordered lists

For each of these representation, give your solution to the above problems.

Solution

Lists with possible repetitions of the same element

1. : - r e d e f i n e _ s y s t e m _ p r e d i c a t e ( m e m b e r ( _ , _ ) ) .

Page 3: CSE 428_ Solutions to Exercises on Logic Programming and Prolog

27/12/2011 CSE 428: Solutions to exercises on Logic Programming and Prolog

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

m e m b e r ( X , [ X | _ ] ) . m e m b e r ( X , [ _ | L ] ) : - m e m b e r ( X , L ) .

2. : - r e d e f i n e _ s y s t e m _ p r e d i c a t e ( s u b s e t ( _ , _ ) ) .

s u b s e t ( [ ] , _ ) . s u b s e t ( [ X | L ] , K ) : - m e m b e r ( X , K ) , s u b s e t ( L , K ) .

3. d i s j o i n t ( [ ] , _ ) .

d i s j o i n t ( [ X | L ] , K ) : - n o t ( m e m b e r ( X , K ) ) , d i s j o i n t ( L , K ) .

4. : - r e d e f i n e _ s y s t e m _ p r e d i c a t e ( u n i o n ( _ , _ , _ ) ) .

u n i o n ( L , K , M ) : - a p p e n d ( L , K , M ) .

5. : - r e d e f i n e _ s y s t e m _ p r e d i c a t e ( i n t e r s e c t i o n ( _ , _ , _ ) ) .

i n t e r s e c t i o n ( [ ] , _ , [ ] ) . i n t e r s e c t i o n ( [ X | L ] , K , M ) : - n o t ( m e m b e r ( X , K ) ) , i n t e r s e c t i o n ( L , K , M ) . i n t e r s e c t i o n ( [ X | L ] , K , [ X | M ] ) : - m e m b e r ( X , K ) , i n t e r s e c t i o n ( L , K , M ) .

6. d i f f e r e n c e ( [ ] , _ , [ ] ) .

d i f f e r e n c e ( [ X | L ] , K , M ) : - m e m b e r ( X , K ) , d i f f e r e n c e ( L , K , M ) . d i f f e r e n c e ( [ X | L ] , K , [ X | M ] ) : - n o t ( m e m b e r ( X , K ) ) , d i f f e r e n c e ( L , K , M ) .

Lists without repetitions. The main difference is in the implementation of the predicate u n i o n . Since we do not want repetitions, wehave to check, for every element of the first list, that it does not appear already in the second list.

: - r e d e f i n e _ s y s t e m _ p r e d i c a t e ( u n i o n ( _ , _ , _ ) ) . u n i o n ( [ ] , K , K ) . u n i o n ( [ X | L ] , K , [ X | M ] ) : - n o t ( m e m b e r ( X , K ) ) , u n i o n ( L , K , M ) . u n i o n ( [ X | L ] , K , M ) : - m e m b e r ( X , K ) , u n i o n ( L , K , M ) .

The other predicates can be defined as before (although for some of them we could give a more efficient definition by using the"cut").

Ordered lists of integers. The main difference is that, in the implementation of the predicate u n i o n , we have to ensure that theoutput list is ordered. This can be done with a predicate o r d e r e d _ m e r g e :

: - r e d e f i n e _ s y s t e m _ p r e d i c a t e ( u n i o n ( _ , _ , _ ) ) . u n i o n ( L , K , M ) : - o r d e r e d _ m e r g e ( L , K , M ) . o r d e r e d _ m e r g e ( [ ] , K , K ) . o r d e r e d _ m e r g e ( L , [ ] , L ) . o r d e r e d _ m e r g e ( [ X | L ] , [ Y | K ] , [ X | M ] ) : - X < Y , o r d e r e d _ m e r g e ( L , [ Y | K ] , M ) . o r d e r e d _ m e r g e ( [ X | L ] , [ Y | K ] , [ Y | M ] ) : - Y < X , o r d e r e d _ m e r g e ( [ X | L ] , K , M ) . o r d e r e d _ m e r g e ( [ X | L ] , [ Y | K ] , [ X | M ] ) : - X = : = Y , o r d e r e d _ m e r g e ( L , K , M ) .

Some of the other predicates can be defined more efficiently, by using the fact that lists are ordered:

4. Define a predicate l e n g t h ( L , N ) which holds iff N is the length of the list L .

Solution

: - r e d e f i n e _ s y s t e m _ p r e d i c a t e ( l e n g t h ( _ , _ ) ) . l e n g t h ( [ ] , 0 ) . l e n g t h ( [ _ | L ] , N ) : - l e n g t h ( L , M ) , N i s M + 1 .

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

Solution

o c c u r r e n c e s ( _ , [ ] , 0 ) . o c c u r r e n c e s ( X , [ X | L ] , N ) : - o c c u r r e n c e s ( X , L , M ) , N i s M + 1 . o c c u r r e n c e s ( X , [ Y | L ] , N ) : - n o t ( X = Y ) , o c c u r r e n c e s ( X , L , N ) .

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

Solution

Page 4: CSE 428_ Solutions to Exercises on Logic Programming and Prolog

27/12/2011 CSE 428: Solutions to exercises on Logic Programming and Prolog

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

o c c u r s ( 1 , [ X | _ ] , X ) . o c c u r s ( N , [ _ | L ] , X ) : - N > 1 , M i s N - 1 , o c c u r s ( M , L , X ) .

7. Define a predicate s u m l i s t ( L , N ) which, given a list of integers L , returns the sum N of all the elements of L .

Solution

s u m l i s t ( [ ] , 0 ) . s u m l i s t ( [ X | L ] , N ) : - s u m l i s t ( L , M ) , N i s M + X .

8. Define a predicate a d d _ u p _ l i s t ( L , K ) which, given a list of integers L , returns a list of integers in which each element is the sum of allthe elements in L up to the same position. Example:

? - a d d _ u p _ l i s t ( [ 1 , 2 , 3 , 4 ] , K ) . K = [ 1 , 3 , 6 , 1 0 ] ; n o

Solution

a d d _ u p _ l i s t ( L , K ) : - a u x ( L , K , 0 ) . a u x ( [ ] , [ ] , _ ) . a u x ( [ X | L ] , [ Y | K ] , Z ) : - Y i s Z + X , a u x ( L , K , Y ) .

9. Define a predicate q u i c k s o r t ( L , K ) which, given a list of integers L , returns an ordered list K obtained from L with the method ofquicksort.

Solution

q u i c k s o r t ( [ ] , [ ] ) . q u i c k s o r t ( [ X | L ] , K ) : - s p l i t ( X , L , L 1 , L 2 ) , q u i c k s o r t ( L 1 , K 1 ) , q u i c k s o r t ( L 2 , K 2 ) , a p p e n d ( K 1 , [ X | K 2 ] , K ) .

s p l i t ( _ , [ ] , [ ] , [ ] ) . s p l i t ( X , [ Y | L ] , K , [ Y | M ] ) : - X < Y , s p l i t ( X , L , K , M ) . s p l i t ( X , [ Y | L ] , [ Y | K ] , M ) : - X > = Y , s p l i t ( X , L , K , M ) .

10. Define a predicate m e r g e ( L , K , M ) which, given two ordered lists of integers L and K , returns an ordered list M containing all theelements of L and K .

Solution

If we do not allow multiple elements in the resulting list, then the solution is the same as the o r d e r e d _ m e r g e used above for the definitionof u n i o n . If allow multiple elements in the resulting list, then we can write the program as follows:

: - r e d e f i n e _ s y s t e m _ p r e d i c a t e ( m e r g e ( _ , _ , _ ) ) . m e r g e ( L , [ ] , L ) . m e r g e ( [ ] , K , K ) . m e r g e ( [ X | L ] , [ Y | K ] , [ X | M ] ) : - X < Y , m e r g e ( L , [ Y | K ] , M ) . m e r g e ( [ X | L ] , [ Y | K ] , [ Y | M ] ) : - X > = Y , m e r g e ( [ X | L ] , K , M ) .

11. Consider a representation of binary trees as terms, as follows

e m p t y b t t h e e m p t y b i n a r y t r e e c o n s b t ( N , T 1 , T 2 ) t h e b i n a r y t r e e w i t h r o o t N a n d l e f t a n d r i g h t s u b t r e e s T 1 a n d T 2

1. Define a predicate p r e o r d e r ( T , L ) which holds iff L is the list of nodes produced by the preorder traversal of the binary tree T .2. Define a predicate s e a r c h _ t r e e ( L , T ) which, given a list of integers L , returns a balanced search-tree T containing the

elements of L .

Solution

Page 5: CSE 428_ Solutions to Exercises on Logic Programming and Prolog

27/12/2011 CSE 428: Solutions to exercises on Logic Programming and Prolog

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

1. p r e o r d e r ( e m p t y b t , [ ] ) .

p r e o r d e r ( c o n s b t ( N , T 1 , T 2 ) , L ) : - p r e o r d e r ( T 1 , L 1 ) , p r e o r d e r ( T 2 , L 2 ) , a p p e n d ( [ N | L 1 ] , L 2 , L ) .

2. s e a r c h _ t r e e ( L , T ) : - q u i c k s o r t ( L , K ) , l e n g t h ( K , N ) , c o n s t r u c t _ t r e e ( K , N , T ) .

c o n s t r u c t _ t r e e ( [ ] , 0 , e m p t y b t ) . c o n s t r u c t _ t r e e ( L , N , c o n s b t ( X , T 1 , T 2 ) ) : - N 1 i s N / / 2 , % i n t e g e r d i v i s i o n N 2 i s N - N 1 - 1 , d i v i d e ( L , N 1 , L 1 , [ X | L 2 ] ) , c o n s t r u c t _ t r e e ( L 1 , N 1 , T 1 ) , c o n s t r u c t _ t r e e ( L 2 , N 2 , T 2 ) . d i v i d e ( L , 0 , [ ] , L ) . d i v i d e ( [ X | L ] , N , [ X | L 1 ] , L 2 ) : - N > 0 , N 1 i s N - 1 , d i v i d e ( L , N 1 , L 1 , L 2 ) .

12. A directed graph can be represented in Prolog by listing the arcs among the nodes, as a set of facts (clauses with empty body). Forinstance, the clause

a r c ( a , b ) .

would represent the existence of an arc going from the node a to the node b .1. Define in Prolog a predicate p a t h ( X , Y ) which holds iff there is an (acyclic) path from the node X to the node Y . Beware of

loops: the graph may contain cycles, but your program should avoid looping on them.2. Enrich the previous program so to return not only the answer yes/no, but also, in case of success, the actual path represented as a

list of nodes. Namely, define a predicate p a t h ( X , Y , P ) which holds iff P is an (acyclic) path from the node X to the node Y . Incase there is more than one acyclic path, the program should generate all of them.

3. Consider the following graph:

a r c ( a , b ) . % 1 a r c ( a , c ) . % 2 a r c ( b , c ) . % 3 a r c ( b , d ) . % 4 a r c ( c , d ) . % 5

What answers are given for the goal ? - p a t h ( a , d , P ) by your solution to the last point? Justify your answer by drawing theSLD-tree (akas Prolog search tree), at least schematically.

Solution

1. In order to avoid cycles, we will use an auxiliary predicate with an additional argument, which represents the list of visisted nodes.

p a t h ( X , Y ) : - p a t h _ a u x ( X , Y , [ X ] ) . % T h e t h i r d a r g u m e n t i s t h e l i s t o f v i s i t e d n o d e s p a t h _ a u x ( X , Y , _ ) : - a r c ( X , Y ) . p a t h _ a u x ( X , Y , L ) : - a r c ( X , Z ) , n o t ( m e m b e r ( Z , L ) ) , p a t h _ a u x ( Z , Y , [ Z | L ] ) .

2. The list of visited nodes, at the end, contains the nodes of the path. The problem is that this list gets built "in reverse", so we shouldreverse it to obtain the path. Alternatively, we can search for the path starting from the end node, and walking back towards thestart node. Here we follow this approach.

p a t h ( X , Y , P ) : - p a t h _ a u x ( X , Y , [ Y ] , P ) . % P i s t h e p a t h ; 6 p a t h _ a u x ( X , Y , L , [ X | L ] ) : - a r c ( X , Y ) . % T h e t h i r d a r g i s t h e l i s t o f v i s i t e d n o d e s . 7 p a t h _ a u x ( X , Y , L , P ) : - a r c ( Z , Y ) , n o t ( m e m b e r ( Z , L ) ) , p a t h _ a u x ( X , Z , [ Z | L ] , P ) . % 8

3. The answers are the following

? - p a t h ( a , d , P ) . P = [ a , b , d ] ; P = [ a , c , d ] ; P = [ a , b , c , d ] ; N o

The SLD-tree is the following. Abbreviation: m stands for member; pa stands for path_aux:

? - p a t h ( a , d , P ) . - - - - - - - - - - - | ( 6 ) | ? - p a ( a , d , [ d ] , P ) . - - - - - - - - - - - - - |

Page 6: CSE 428_ Solutions to Exercises on Logic Programming and Prolog

27/12/2011 CSE 428: Solutions to exercises on Logic Programming and Prolog

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

( 8 ) | ? - a r c ( Z , d ) , n o t ( m e m b e r ( Z , [ d ] ) ) , p a ( a , Z , [ Z , d ] , P ) . - - - - - - - - / | Z = b ( 4 ) ( 5 ) Z = c / |? - n o t ( m ( b , [ d ] ) ) , p a ( a , b , [ b , d ] , P ) . ? - n o t ( m ( c , [ d ] ) ) , p a ( a , c , [ c , d ] , P ) . - - - - - - - - - - - - - - - - - - - - - - - - - - / | / | ? - p a ( a , b , [ b , d ] , P ) . ? - p a ( a , c , [ c , d ] , P ) . - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - P = / \ / \ [ a , b , d ] ( 7 ) ( 8 ) P = [ a , c , d ] ( 7 ) ( 8 ) / \ / \ ? - a r c ( a , b ) . . . . ? - a r c ( a , c ) . ? - a r c ( W , c ) , n o t ( m e m b e r ( W , [ c , d ] ) ) , p a ( a , W , [ W , c , d ] ) . - - - - - - - - f a i l - - - - - - - - - - - - - - - - / / | | ( 1 ) ( 2 ) W = a ( 2 ) ( 3 ) W = b / / | |s u c c e s s s u c c e s s . . . ? - n o t ( m e m b e r ( b , [ c , d ] ) ) , f a i l - - - - - - - - - - - - - - - - - - - - p a ( a , b , [ b , c , d ] ) . | | ? - p a ( a , b , [ b , c , d ] ) . - - - - - - - - - - - - - - - / \ P = [ a , b , c , d ] ( 7 ) ( 8 ) / \ ? - a r c ( a , b ) . . . . - - - - - - - - f a i l / ( 1 ) / s u c c e s s