Prolog: Lists - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Prolog: Lists

Description:

Recall family tree example: parent(linda, simon). parent(sam, simon). parent(linda, sharon) ... using trace: Prolog: Backtracking and Cut | ?- trace. yes [trace] ... – PowerPoint PPT presentation

Number of Views:154
Avg rating:3.0/5.0
Slides: 21
Provided by: csW7
Category:
Tags: family | lists | prolog | trace | tree

less

Transcript and Presenter's Notes

Title: Prolog: Lists


1
Prolog Lists
  • Recall lists in Scheme
  • '(a b c) (cons 'a (cons 'b (cons c '())))
  • ls (cons (car ls) (cdr ls))
  • Want the same operations in Prolog
  • a, b, c .(a, .(b, .(c, )))
  • car(XTail, X).
  • cdr(XTail, Tail).
  • cons(X, Tail, XTail).

lists.P
2
Prolog Lists
Try it out ?- consult(lists). ?-
car(1,2,3, H). H 1 ?- cdr(1,2,3, T). T
2,3 ?- cons(1, 2,3, L). L 1,2,3
3
Prolog Anonymous Variables
  • What really happens
  • ?- consult(lists).
  • WarningXSB Compiler ./lists Singleton
  • variable Tail in a clause of car/2
  • WarningXSB Compiler ./lists Singleton
    variable X in a clause of cdr/2
  • Problem We're not using some variables
  • car(XTail, X). Tail unused
  • cdr(XTail, Tail). X unused

4
Prolog Anonymous Variables
Solution Anonymous variables car(X_, X).
_ whatever cdr(_Tail, Tail).
5
Prolog Lists
  • Recall member? function (predicate) in Scheme
  • (define member?
  • (lambda (a ls)
  • (cond ((null? ls) f)
  • ((equal? a (car ls)) t)
  • (else (member? a (cdr ls))))))
  • Can also write it in ML
  • fun member(a, nil) false
  • member(a, xt)
  • if a x then true else member(a, t)
  • How about Prolog?
  • member(X, X_).
  • member(X, _T) - member(X, T).

6
Prolog Backtracking and Cut
  • Recall family tree example
  • parent(linda, simon).
  • parent(sam, simon).
  • parent(linda, sharon).
  • parent(sam, sharon).
  • female(sharon).
  • sister(S,X) - parent(P,S), parent(P,X),
  • female(S).
  • ?- sister(Who, simon).
  • Who sharon
  • Who sharon
  • Look under the hood using trace

7
Prolog Backtracking and Cut
?- trace. yes trace ?- sister(Who,
simon). (0) Call sister(_h81,simon) ?
sister(S, Xsimon) (1) Call
parent(_h148,_h81) ? parent(P, S) (1) Exit
parent(linda,simon) ? parent(Plinda,
Ssimon) (2) Call parent(linda,simon) ?
parent(Plinda, Xsimon) (2) Exit
parent(linda,simon) ? (3) Call female(simon)
? female(Ssimon) (3) Fail female(simon)
? (2) Redo parent(linda,simon) ?
parent(Plinda, Xsimon) (2) Fail
parent(linda,simon) ? fail on redo (1)
Redo parent(linda,simon) ? parent(Plinda,
Ssimon) (1) Exit parent(linda,sharon) ?
parent(Plinda, Ssharon) (4) Call
parent(linda,simon) ? parent(Plinda,
Xsimon) (4) Exit parent(linda,simon) ?
(5) Call female(sharon) ? female(Ssharon)
(5) Exit female(sharon) ? (0) Exit
sister(sharon,simon) ? Plinda, Xsimon,
Ssharon Who sharon
sister(S, X) - parent(P,S), parent(P,X),
female(S).
8
Prolog Backtracking and Cut
(0) Redo sister(sharon,simon) ? (5)
Redo female(sharon) ? female(Ssharon)
(5) Fail female(sharon) ? fail on redo
(4) Redo parent(linda,simon) ?
parent(Plinda, Xsimon) (4) Fail
parent(linda,simon) ? fail on redo (1)
Redo parent(linda,sharon) ? parent(Plinda,
Ssharon) (1) Exit parent(sam,simon) ?
parent(Psam, Ssimon) (6) Call
parent(sam,simon) ? parent(Psam, Xsimon)
(6) Exit parent(sam,simon) ? (7) Call
female(simon) ? female(Ssimon) (7)
Fail female(simon) ? (6) Redo
parent(sam,simon) ? parent(Psam, Xsimon)
(6) Fail parent(sam,simon) ? fail on redo
(1) Redo parent(sam,simon) ? parent(Psam,
Ssimon) (1) Exit parent(sam,sharon) ?
parent(Psam, Ssharon) (8) Call
parent(sam,simon) ? parent(Psam, Xsimon)
(8) Exit parent(sam,simon) ? (9) Call
female(sharon) ? female(Ssharon) (9)
Exit female(sharon) ? (0) Exit
sister(sharon,simon) ? Psam, Xsimon,
Ssharon
sister(S, X) - parent(P,S), parent(P,X),
female(S).
9
Prolog Backtracking and Cut
  • We can view this process as a tree, where each
    branch represents a binding choice

10
Prolog Backtracking and Cut
  • We can view this process as a tree, where each
    branch represents a binding choice

X
simon
11
Prolog Backtracking and Cut
X
simon
P
linda
S
12
Prolog Backtracking and Cut
X
simon
P
linda
S
simon
FAIL
13
Prolog Backtracking and Cut
Backtrack to most recent success
X
simon
P
linda
S
14
Prolog Backtracking and Cut
X
simon
P
linda
S
sharon
Succeed
15
Prolog Backtracking and Cut
User asks for more answers
X
simon
P
linda
S
sharon
Succeed
16
Prolog Backtracking and Cut
Backtrack no more choices for S
X
simon
P
linda
S
17
Prolog Backtracking and Cut
Backtrack
X
simon
P
18
Prolog Backtracking and Cut
X
simon
P
sam
S
19
Prolog Backtracking and Cut
X
simon
P
sam
S
sharon
Succeed
20
Prolog Backtracking and Cut
  • Cut (!) prevents backtracking
  • sister(S,X) - parent(P,S), parent(P,X),
  • female(S), !. don't backtrack!
  • ?- sister(Who, simon).
  • Who sharon
  • no
  • No longer a pure logic paradigm we are
    exploiting sequential order.
Write a Comment
User Comments (0)
About PowerShow.com