+ All Categories
Home > Documents > Logik für Informatiker: PROLOG Part 3: SLD Trees

Logik für Informatiker: PROLOG Part 3: SLD Trees

Date post: 18-Nov-2021
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
21
Simply Logical – Chapter 3 © Peter Flach, 2000 1 Andreas Karwath & Wolfram Burgard (original slides by Peter Flach) Logik für Informatiker: PROLOG Part 3: SLD Trees
Transcript
Page 1: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

1

Andreas Karwath

&

Wolfram Burgard

(original slides by Peter Flach)

Logik für Informatiker: PROLOG Part 3: SLD Trees

Page 2: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

2

SLD Resolution and SLD Trees

Page 3: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

3

SLD resolution

Resolution in Prolog based on definite clause logic

definite clauses: one atom in the head

Resolution strategy: which literal and which input clause is chosen for resolution?

Strategy in Prolog is called SLD resolution:

­ S selection rule (in Prolog from left to right)­ L linear resolution­ D definite clauses

Leftmost literal in query is selected; clauses are selected in textual order

SLD trees: different from proof trees: only show resolvents, but all possible resolution steps

Prolog searches SLD tree depth­first !

Page 4: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

4

:-teaches(peter,ai_techniques)

:-teaches(peter,expert_systems)

:-teaches(peter,computer_science)

?-student_of(S,peter)

SLD­tree

:-follows(S,C),teaches(peter,C):-follows(S,C),teaches(peter,C)

:-teaches(peter,expert_systems)

:-teaches(peter,computer_science) :-teaches(peter,ai_techniques)

?-student_of(S,peter)

student_of(X,T):-follows(X,C),teaches(T,C).follows(paul,computer_science).follows(paul,expert_systems).follows(maria,ai_techniques).teaches(adrian,expert_systems).teaches(peter,ai_techniques).teaches(peter,computer_science).

p.44­5

[][]

Page 5: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

5

Infinite SLD­trees

brother_of(X,Y):-brother_of(Y,X).brother_of(paul,peter).

brother_of(paul,peter).brother_of(peter,adrian).brother_of(X,Y):- brother_of(X,Z),

brother_of(Z,Y).

?-brother_of(paul,B)

[] :-brother_of(paul,Z),brother_of(Z,B)

:-brother_of(paul,Z1),brother_of(Z1,Z),brother_of(Z,B):-brother_of(peter,B)

[] :-brother_of(peter,Z),brother_of(Z,B)

•••

•••

•••

:-brother_of(B,peter)

[]

?-brother_of(peter,B)

:-brother_of(peter,B)

:-brother_of(B,peter)

[]

p.45­6

Page 6: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

6

Depth­first search

?-plist(L)

plist([]).plist([H|T]):-

p(H),plist(T).

p(1). p(2).[]

L = []

p.47

:-p(H1),plist(T1)

:-plist(T1)

[]

L = [1]

:-p(H1),plist(T1)

:-plist(T1)

[]

L = [1,1]

•••

:-plist(T1)

:-plist(T1)

[]

L = [1,2]

•••

[]

L = [2]

:-p(H1),plist(T1)

:-plist(T1):-plist(T1)

[]

L = [2,1]

[]

L = [2,2]

•••

•••

?-plist(L).L=[];L=[1];L=[1,1];…

Page 7: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

7

Summary

First problem:  trapped in infinite subtrees

Prolog is incomplete by the way the SLD tree is searched (depth-first; a deliberate design decision for memory efficiency)

if breadth-first search were used, then it would be refutation complete!

Second problem:  looping if no (further) solution for any infinite

SLD tree

due to semi-decidability of full clausal logic(infinity of Herbrand base)

Page 8: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

8

Programming Example:Monkey and banana

Page 9: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

9

Problem: Monkey and Banana

The Problem

There is a monkey at the door into a room. In the middle of the room a banana is hanging from the ceiling. The monkey is hungry and wants to get the banana, but he cannot stretch high enough from the floor. At the window of the room there is a box that the monkey can use. The monkey can perform the following actions: walk on the floor, climb the box, push the box around (if he is already at it), and grasp the banana if he is standing on the box and directly underneath the banana. 

Can the monkey grasp the banana? 

Page 10: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

10

banana

Problem: Monkey and Banana

door

window

box

mokey

middle

Page 11: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

11

Monkey and Banana: States

state( MR, MB , P , B )

Monkey‘s Room Position

•at_door•middle•at_window

Monkey‘s Box Position

•on_floor•on_box

Box Position

•at_door•middle•at_window

Banana

•has•has_not

Page 12: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

12

Monkey and Banana: Actions I

Possible actions:

walk, push, climb, grasp

Defined using:

move(OldState, Action, NewState)

OldState NewState

Action

Page 13: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

13

Monkey and Banana: Actions II

Grasp Banana:

move( state(middle, on_box, middle, has_not),grasp,state(middle, on_box, middle, has)

).

move( state(P, on_floor, P, H),climb,state(P, on_box, P, H) ).

Climb Box:

Page 14: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

14

Monkey and Banana: Actions III

Push Box:

move( state(P1, on_floor, P1, H),push(P1, P2),state(P2, on_floor, P2, H)).

move( state(P1, on_floor, P, H),walk(P1, P2),state(P2, on_floor, P, H)).

Walk:

Page 15: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

15

Monkey and Banana: Actions Summary

move(state(middle, on_box, middle, has_not),grasp,state(middle, on_box, middle, has)).

move(state(P, on_floor, P, H),climb,state(P, on_box, P, H)).

move(state(P1, on_floor, P1, H),push(P1, P2),state(P2, on_floor, P2, H)).

move(state(P1, on_floor, P, H),walk(P1, P2),state(P2, on_floor, P, H)).

Page 16: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

16

Monkey and Banana: Goals

How can the monkey get the banana?

canget(state(_P1, _F, _P2, has)).canget(OldState) :-

move(OldState, Move, NewState),canget(NewState).

•Can the monkey get the banana from the initial state?

?- canget( state(at_door, on_floor,at_window, has_not)).

Page 17: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

17

Monkey and Banana: Summary

move(state(middle, on_box, middle, has_not),grasp,state(middle, on_box, middle, has)).

move(state(P, on_floor, P, H),climb,state(P, on_box, P, H)).

move(state(P1, on_floor, P1, H),push(P1, P2),state(P2, on_floor, P2, H)).

move(state(P1, on_floor, P, H),walk(P1, P2),state(P2, on_floor, P, H)).

canget(state(_, _, _, has)).canget(OldState) :-

move(OldState, Move, NewState),canget(NewState).

Page 18: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

18

Monkey and Banana: Trace

| ?- canget(state(at_door,on_floor,at_window,has_not)).Call: canget(state(at_door,on_floor,at_window,has_not)) ?Call: move(state(at_door,on_floor,at_window,has_not),_989,_990) ?Exit: move(state(at_door,on_floor,at_window,has_not),walk(at_door,_1476),state(_1476,on_floor,at_window,has_not)) ?Call: canget(state(_1476,on_floor,at_window,has_not)) ?Call: move(state(_1476,on_floor,at_window,has_not),_2415,_2416) ?Exit: move(state(at_window,on_floor,at_window,has_not),climb,state(at_window,on_box,at_window,has_not)) ?Call: canget(state(at_window,on_box,at_window,has_not)) ?Call: move(state(at_window,on_box,at_window,has_not),_3841,_3842) ?Fail: move(state(at_window,on_box,at_window,has_not),_3841,_3842) ?Fail: canget(state(at_window,on_box,at_window,has_not)) ?Redo: move(state(at_window,on_floor,at_window,has_not),climb,state(at_window,on_box,at_window,has_not)) ?Exit: move(state(at_window,on_floor,at_window,has_not),push(at_window,_2902),state(_2902,on_floor,_2902,has_not)) ?Call: canget(state(_2902,on_floor,_2902,has_not)) ?Call: move(state(_2902,on_floor,_2902,has_not),_3844,_3845) ?Exit: move(state(_2902,on_floor,_2902,has_not),climb,state(_2902,on_box,_2902,has_not)) ?Call: canget(state(_2902,on_box,_2902,has_not)) ?Call: move(state(_2902,on_box,_2902,has_not),_5270,_5271) ?Exit: move(state(middle,on_box,middle,has_not),grasp,state(middle,on_box,middle,has)) ?Call: canget(state(middle,on_box,middle,has)) ?Exit: canget(state(middle,on_box,middle,has)) ?Exit: canget(state(middle,on_box,middle,has_not)) ?Exit: canget(state(middle,on_floor,middle,has_not)) ?Exit: canget(state(at_window,on_floor,at_window,has_not)) ?Exit: canget(state(at_door,on_floor,at_window,has_not)) ?yes

Page 19: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

19

Monkey and Banana: Notes

Order of clauses in monkey­banana example:

1. grasp

2. climb

3. push

4. walk

Changing this order can cause Prolog to loop indefinitely !

Page 20: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

20

Getting the necessary action sequence as output:

canget(state(_, _, _, has),[]).canget(OldState, [Move| Actions]) :-

move(OldState, Move, NewState),canget(NewState, Actions).

?- canget(state(at_door, on_floor,at_window, has_not),Actions).

Actions = [walk(at_door, at_window), push(at_window, middle), climb, grasp]

Yes

Page 21: Logik für Informatiker: PROLOG Part 3: SLD Trees

Simply Logical – Chapter 3  © Peter Flach, 2000

21

Why is there is an infinite number of solutions:

?- canget(state(at_door, on_floor, at_window, has_not),Actions).

Actions = [walk(at_door, at_window), push(at_window, middle), climb, grasp] ;

Actions = [walk(at_door, at_window), push(at_window, _G276), push(_G276, middle), climb, grasp] ;

Actions = [walk(at_door, at_window), push(at_window, _G276), push(_G276, _G287), push(_G287, middle), climb, grasp] ;


Recommended