Title: Prolog: Lists
1Prolog 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
2Prolog 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
3Prolog 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
-
4Prolog Anonymous Variables
Solution Anonymous variables car(X_, X).
_ whatever cdr(_Tail, Tail).
5Prolog 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).
6Prolog 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
7Prolog 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).
8Prolog 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).
9Prolog Backtracking and Cut
- We can view this process as a tree, where each
branch represents a binding choice
10Prolog Backtracking and Cut
- We can view this process as a tree, where each
branch represents a binding choice
X
simon
11Prolog Backtracking and Cut
X
simon
P
linda
S
12Prolog Backtracking and Cut
X
simon
P
linda
S
simon
FAIL
13Prolog Backtracking and Cut
Backtrack to most recent success
X
simon
P
linda
S
14Prolog Backtracking and Cut
X
simon
P
linda
S
sharon
Succeed
15Prolog Backtracking and Cut
User asks for more answers
X
simon
P
linda
S
sharon
Succeed
16Prolog Backtracking and Cut
Backtrack no more choices for S
X
simon
P
linda
S
17Prolog Backtracking and Cut
Backtrack
X
simon
P
18Prolog Backtracking and Cut
X
simon
P
sam
S
19Prolog Backtracking and Cut
X
simon
P
sam
S
sharon
Succeed
20Prolog 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.