prolog homework 1: search trees - universität bremen · 1. write out the full set of search trees...

19
Prolog Homework 1: search trees answer

Upload: others

Post on 03-Feb-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Prolog Homework 1: search trees

answer

Page 2: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Backtracking

• Query: k(Y)

©P

atr

ick B

lack

bu

rn,

Joh

an

Bos

& K

rist

ina S

trie

gn

itz

Homework:1. write out the full set of search trees

forthis query to find out what Prologshould produce as solutions for Y.

2. check that Prolog produces the results you thought

3. see if you can following through using trace(k) the steps that Prolog actually went through: are they the same as your proof tree?

Page 3: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

Page 4: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

Page 5: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

Y=X

Page 6: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

X=a

Y=X

Page 7: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

Y=X

Page 8: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

Y=X

Page 9: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

?- g(b), h(b).

X=b

Y=X

Page 10: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

?- g(b), h(b).

X=b

?- h(b).

Y=X

Page 11: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).Y=b

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

?- g(b), h(b).

X=b

?- h(b).

Y=X

Page 12: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).Y=b;no?-

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

?- g(b), h(b).

X=b

?- h(b).

Y=X

Page 13: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

Tracing…[debug] 13 ?- k(Y).T Call: (7) k(_G397)T Call: (8) f(_G397)T Exit: (8) f(a)T Call: (8) g(a)T Exit: (8) g(a)T Call: (8) h(a)T Fail: (8) h(a)T Redo: (8) f(_G397)T Exit: (8) f(b)T Call: (8) g(b)T Exit: (8) g(b)T Call: (8) h(b)T Exit: (8) h(b)T Exit: (7) k(b)

Y = b.

Page 14: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

[debug] 13 ?- k(Y).T Call: (7) k(_G397)T Call: (8) f(_G397)T Exit: (8) f(a)T Call: (8) g(a)T Exit: (8) g(a)T Call: (8) h(a)T Fail: (8) h(a)T Redo: (8) f(_G397)T Exit: (8) f(b)T Call: (8) g(b)T Exit: (8) g(b)T Call: (8) h(b)T Exit: (8) h(b)T Exit: (7) k(b)Y = b.

Page 15: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

[debug] 13 ?- k(Y).T Call: (7) k(_G397)T Call: (8) f(_G397)T Exit: (8) f(a)T Call: (8) g(a)T Exit: (8) g(a)T Call: (8) h(a)T Fail: (8) h(a)T Redo: (8) f(_G397)T Exit: (8) f(b)T Call: (8) g(b)T Exit: (8) g(b)T Call: (8) h(b)T Exit: (8) h(b)T Exit: (7) k(b)Y = b.

Page 16: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

[debug] 13 ?- k(Y).T Call: (7) k(_G397)T Call: (8) f(_G397)T Exit: (8) f(a)T Call: (8) g(a)T Exit: (8) g(a)T Call: (8) h(a)T Fail: (8) h(a)T Redo: (8) f(_G397)T Exit: (8) f(b)T Call: (8) g(b)T Exit: (8) g(b)T Call: (8) h(b)T Exit: (8) h(b)T Exit: (7) k(b)Y = b.

†we know that this goal willnever be satisfied…

Page 17: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

[debug] 13 ?- k(Y).T Call: (7) k(_G397)T Call: (8) f(_G397)T Exit: (8) f(a)T Call: (8) g(a)T Exit: (8) g(a)T Call: (8) h(a)T Fail: (8) h(a)T Redo: (8) f(_G397)T Exit: (8) f(b)T Call: (8) g(b)T Exit: (8) g(b)T Call: (8) h(b)T Exit: (8) h(b)T Exit: (7) k(b)Y = b.

Page 18: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

[debug] 13 ?- k(Y).T Call: (7) k(_G397)T Call: (8) f(_G397)T Exit: (8) f(a)T Call: (8) g(a)T Exit: (8) g(a)T Call: (8) h(a)T Fail: (8) h(a)T Redo: (8) f(_G397)T Exit: (8) f(b)T Call: (8) g(b)T Exit: (8) g(b)T Call: (8) h(b)T Exit: (8) h(b)T Exit: (7) k(b)Y = b.

no more goals to satisfy,all done and happy!

Page 19: Prolog Homework 1: search trees - Universität Bremen · 1. write out the full set of search trees forthis query to find out what Prolog should produce as solutions for Y. 2. check

What ‘problem’ has this funny example just solved?

k(X):-f(X),g(X),h(X).

X

fh

g

f(a).f(b).g(a).g(b).h(b).

a

b

human(X):-mortal(X).