05.09.2014 Views

Computational Logic A “Hands-on” Introduction to Logic Programming

Computational Logic A “Hands-on” Introduction to Logic Programming

Computational Logic A “Hands-on” Introduction to Logic Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Exercises on Unification<br />

• Get the m.g.u. of:<br />

1. p(X, Y ) and p(f(Y ), f(Z))<br />

2. p(f(X), Y ) and p(Y, g(X))<br />

3. p(X, Y, f(X, Y )) and p(f(A, B), B, A)<br />

4. p(f(X, Y ), g(Z, X), h(V ), a, W ) and p(Z, W, Y, V, g(f(V, Y ), X))<br />

5. conv(us(Month, Day, Y ear), eur(Day, Month, Y ear)) and<br />

conv(us(jan, 25, Y ear), D)<br />

6. conv(us(Month, Day, Y ear), eur(Day, Month, Y ear)) and<br />

conv(D, eur(22, may, 2002))<br />

7. third(l(A, l(B, l(X, C))), X) and third(l(one, l(two, l(three, void))), Y )<br />

8. third(l(A, l(B, l(X, C))), X) and third(l(one, l(two, void)), Y )<br />

9. third(l(A, l(B, l(X, C))), X) and third(L, a)<br />

<strong>Computational</strong> <strong>Logic</strong><br />

A “Hands-on” <strong>Introduction</strong> <strong>to</strong> <strong>Logic</strong> <strong>Programming</strong><br />

Exercises<br />

2


Exercises on unification — Solutions (Contd.)<br />

• p(X, Y, f(X, Y )) and p(f(A, B), B, A)<br />

θ E T S<br />

{} { p(X,Y,f(X,Y))=p(f(A,B),B,A) } p(X,Y,f(X,Y)) p(f(A,B),B,A)<br />

{} { X=f(A,B), Y=B, f(X,Y)=A } X f(A,B)<br />

{ X=f(A,B) } { Y=B, f(f(A,B),Y)=A } Y B<br />

{ X=f(A,B), Y=B } { f(f(A,B),B)=A } f(f(A,B),Y) A<br />

fail<br />

Exercises on unification — Solutions<br />

• p(X, Y ) and p(f(Y ), f(Z))<br />

θ E T S<br />

{} { p(X,Y)=p(f(Y),f(Z)) } p(X,Y) p(f(Y),f(Z))<br />

{} { X=f(Y), Y=f(Z) } X f(Y)<br />

{ X=f(Y) } { Y=f(Z) } Y f(Z)<br />

{ X=f(f(Z)), Y=f(Z) } {}<br />

• p(f(X), Y ) and p(Y, g(X))<br />

θ E T S<br />

{} { p(f(X),Y)=p(Y,g(X)) } p(f(X),Y) p(Y,g(X))<br />

{} { f(X)=Y, Y=g(X) } f(X) Y<br />

{ Y=f(X) } { f(X)=g(X) } f(X) g(X)<br />

fail<br />

4


Exercises on unification — Solutions (Contd.)<br />

Exercises on unification — Solutions (Contd.)<br />

• conv(us(Month, Day, Y ear), eur(Day, Month, Y ear)) and conv(us(jan, 25, Y ear), D)<br />

• p(f(X, Y ), g(Z, X), h(V ), a, W ) and p(Z, W, Y, V, g(f(V, Y ), X))<br />

θ E T S<br />

{} { conv(us(Month,Day,Year), conv(...) conv(...)<br />

eur(Day,Month,Year))=<br />

conv(us(jan,25,Year),D) }<br />

{} { us(Month,Day,Year)= us(...) us(...),<br />

us(jan,25,Year),<br />

eur(Day,Month,Year)=D }<br />

{} { Month=jan, Day=25, Year=Year, Month jan<br />

eur(Day,Month,Year)=D }<br />

{Month=jan} { Day=25, Year=Year, Day 25<br />

eur(Day,jan,Year)=D }<br />

{ Month=jan, Day=25} { Year=Year, Year Year<br />

eur(25,jan,Year)=D }<br />

{Month=jan, Day=25} { eur(25,jan,Year)=D } eur(25,jan,Year) D<br />

θ E T S<br />

{} { p(f(X,Y),g(Z,X),h(V),a,W)= p(...) p(...)<br />

p(Z,W,Y,V,g(f(V,Y),X)) }<br />

{} { f(X,Y)=Z, g(Z,X)=W, h(V)=Y, f(X,Y) Z<br />

a=V, W=g(f(V,Y),X) }<br />

{Z=f(X, Y)} { g(f(X,Y),X)=W, h(V)=Y, g(f(X,Y),X) W<br />

a=V, W=g(f(V,Y),X) }<br />

{ Z=f(X,Y), { h(V)=Y, a=V, h(V) Y<br />

W=g(f(X,Y),X) } g(f(X,Y),X)=g(f(V,Y),X) }<br />

{ Z=f(X,Y), { a=V, g(f(X,h(V)),X)= a V<br />

W=g(f(X,Y),X), Y=h(V) } g(f(V,h(V)),X) }<br />

{ Z=f(X,Y), W=g(f(X,Y),X), { g(f(X,h(a)),X)= g(...) g(...)<br />

Y=h(V), V=a } g(f(a,h(a)),X) }<br />

{ Z=f(X,Y), W=g(f(X,Y),X), { f(X,h(a))=f(a,h(a)), X=X } f(X,h(a)) f(a,h(a))<br />

Y=h(V), V=a }<br />

{ Z=f(X,Y), W=g(f(X,Y),X), { X=a, X=X, h(a)=h(a) } X a<br />

Y=h(V), V=a }<br />

{ Z=f(X,Y), W=g(f(X,Y),X), { X=X, h(a)=h(a) }<br />

Y=h(V), V=a, X=a }<br />

{Month=jan, Day=25, {}<br />

D=eur(25,jan,Year)}<br />

6


Exercises on unification — Solutions (Contd.)<br />

Exercises on unification — Solutions (Contd.)<br />

• third(l(A, l(B, l(X, C))), X) and third(l(one, l(two, l(three, void))), Y )<br />

θ E T S<br />

{} { third(l(A,l(B,l(X,C))),X)= third(...) third(...)<br />

third(l(one,l(two,<br />

l(three,void))),Y) }<br />

{} { l(A,l(B,l(X,C)))= l(A,...) l(one,...)<br />

l(one,l(two,l(three,void))),<br />

X=Y }<br />

{} { A=one, l(B,l(X,C))= A one<br />

l(two,l(three,void)), X=Y }<br />

{A=one} { l(B,l(X,C))= l(B,l(X,C)) l(two,l(three,void))<br />

l(two,l(three,void)), X=Y }<br />

{A=one} { B=two, B two<br />

l(X,C)=l(three,void), X=Y }<br />

{A=one, B=two } {l(X,C)=l(three,void), X=Y } l(X,C) l(three,void)<br />

{A=one, B=two } {X=three, C=void, X=Y } X three<br />

{A=one, B=two, {C=void, three=Y } C void<br />

X=three }<br />

{A=one, B=two, { three=Y } three Y<br />

X=three, C=void }<br />

{A=one, B=two, {}<br />

X=three, C=void,<br />

Y=three }<br />

• conv(us(Month, Day, Y ear), eur(Day, Month, Y ear)) and conv(D, eur(22, may, 2002))<br />

θ E T S<br />

{} { conv(us(Month,Day,Year), conv(...) conv(..<br />

eur(Day,Month,Year))=<br />

conv(D,eur(22,may,2002)) }<br />

{} { us(Month,Day,Year)=D, us(Month,Day,Year) D<br />

eur(Day,Month,Year)=<br />

eur(22,may,2002) }<br />

{D=us(Month,Day,Year)} { eur(Day,Month,Year)= eur(...) eur(..<br />

eur(22,may,2002) }<br />

{D=us(Month,Day,Year)} {Day=22, Month=may, Day 22<br />

Year=2002 }<br />

{D=us(Month,22,Year), { Month=may, Year=2002} Month may<br />

Day=22}<br />

{D=us(may,22,Year), { Year=2002} Year 2002<br />

Day=22, Month=may}<br />

{D=us(may,22,2002), {}<br />

Day=22, Month=may,<br />

Year=2002}<br />

8


Exercises on execution, strategies, trees<br />

Exercises on unification — Solutions (Contd.)<br />

• Given the following logic program:<br />

eats(X,Y)


Exercises on execution, strategies, trees – Solutions (Contd.)<br />

Exercises on execution, strategies, trees – Solutions<br />

• Resolution tree:


Exercises on execution, strategies, trees – Solutions (Contd.)<br />

Exercises on execution, strategies, trees – Solutions (Contd.)<br />

• Search tree:


Exercises on execution, strategies, trees – Solutions (Contd.)<br />

Exercises on execution, strategies, trees – Solutions (Contd.)<br />

• Solution space tree:


Exercises on Modelling<br />

Exercises on execution, strategies, trees – Solutions (Contd.)<br />

falta<br />

• Solution space tree:


Exercises on lists and trees<br />

Exercises on ADTs, recursion, and types<br />

falta<br />

falta<br />

20


Exercises on structures<br />

falta

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!