Prolog -Cpt114 - Week3
description
Transcript of Prolog -Cpt114 - Week3
PROLOGWeek 3
OBJECTIVES FOR THIS SESSION
The Lists in Prolog
Tracing
SIMPLE LISTS IN PROLOG
HOW TO MAKE A LIST IN PROLOG
pam tom
bob liz
pat
jim
ann
HOW TO MAKE A LIST IN PROLOG
pam tom
bob liz
pat
jim
ann
parent(pam, bob).parent(tom, bob).parent(tom, liz).parent(bob, pat).parent(pat, jim).female(pam).female(liz).female(pat).male(tom).male(bob).male(jim).
HOW TO MAKE A LIST IN PROLOGpam tom
bob liz
pat
jim
ann
HOW FINDALL WORKS
findall(X,predecessor(pam,X),Preds).
Output listThe query variable
8
EXAMPLE1
The following conversation with Prolog:
?- Hobbies1 = [tennis, music],
Hobbies2 = [skiing, food],
L = [ann, Hobbies1, tom, Hobbies2].
Hobbies1 = [tennis, music]
Hobbies2 = [skiing, food]
L = [ann, [tennis, music], tom, [skiing, food] ]
EXAMPLE2
Treat the whole tail as a single object:L = [a, b, c]
Tail = [b, c]
L = .(a, Tail)
L = [a | Tail]
[a, b, c] = [a | [b, c] ] = [a, b | [c] ] = [a, b, c | [ ] ]
EXAMPLE2
Treat the whole tail as a single object:L = [a, b, c]
Tail = [b, c]
L = .(a, Tail)
L = [a | Tail]
[a, b, c] = [a | [b, c] ] = [a, b | [c] ] = [a, b, c | [ ] ]
MEMBERSHIP
X is a member of L either:1) X is the head of L, or2) X is a member of the tail of L.member(X, L)
Can be written in two clauses:member(X, [X | Tail] ). %simple fact
member(X, [Head | Tail] ) :-
member(X, Tail). %rule Example:
member(b, [a,b,c] ). %true
member(b, [a,[b,c]] ). %false, the tail is [b,c]
member([b,c],[a,[b,c]] ). %true
MEMBERSHIP
Simple other tests:
findall(X,parent(X,_),Parents).
Parents = [pam, tom, tom, bob, pat].
Is pam a member of Parents????member(pam, [pam, tom,bob, pat]).
MEMBERSHIP
Parents = [pam, tom, tom, bob, pat].
Is pam a member of HEAD of Parents????
member(pam,[pam,Parents]).
HEAD AND TAIL
A non-empty list can be thought of as consisting of two parts The head The tail
The head is the first item in the list The tail is everything else
The tail is the list that remains when we take the first element away
The tail of a list is always a list
HEAD AND TAIL EXAMPLE 1
[mia, vincent, jules, yolanda]
Head: Tail:
HEAD AND TAIL EXAMPLE 1
[mia, vincent, jules, yolanda]
Head: miaTail:
HEAD AND TAIL EXAMPLE 1
[mia, vincent, jules, yolanda]
Head: miaTail: [vincent, jules, yolanda]
HEAD AND TAIL EXAMPLE 2
[[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
Head: Tail:
HEAD AND TAIL EXAMPLE 2
[[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
Head: [ ]Tail:
HEAD AND TAIL EXAMPLE 2
[[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
Head: [ ]Tail: [dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
HEAD AND TAIL EXAMPLE 3
[dead(z)]
Head: Tail:
HEAD AND TAIL EXAMPLE 3
[dead(z)]
Head: dead(z) Tail:
HEAD AND TAIL EXAMPLE 3
[dead(z)]
Head: dead(z) Tail: [ ]
THE BUILT-IN OPERATOR |
?- [Head|Tail] = [mia, vincent, jules, yolanda].
Head = miaTail = [vincent,jules,yolanda]yes
?-
THE BUILT-IN OPERATOR |
?- [X|Y] = [mia, vincent, jules, yolanda].
X = miaY = [vincent,jules,yolanda]yes
?-
THE BUILT-IN OPERATOR |
?- [X|Y] = [ ].
no
?-
THE BUILT-IN OPERATOR |?- [X,Y|Tail] = [[ ], dead(z), [2, [b,c]], [], Z, [2, [b,c]]] .
X = [ ]Y = dead(z)Z = _4543 Tail = [[2, [b,c]], [ ], Z, [2, [b,c]]] yes
?-
ANONYMOUS VARIABLE
Suppose we are interested in the second and fourth element of a list
?- [X1,X2,X3,X4|Tail] = [mia, vincent, marsellus, jody, yolanda].X1 = miaX2 = vincentX3 = marsellusX4 = jodyTail = [yolanda]yes
?-
ANONYMOUS VARIABLES
There is a simpler way of obtaining only the information we want:
?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda].X2 = vincentX4 = jodyyes
?-
The underscore is the anonymous variable
THE ANONYMOUS VARIABLE
Is used when you need to use a variable, but you are not interested in what Prolog instantiates it to
Each occurrence of the anonymous variable is independent, i.e. can be bound to something different
MEMBER
One of the most basic things we would like to know is whether something is an element of a list or not
So let`s write a predicate that when given a term X and a list L, tells us whether or not X belongs to L
This predicate is usually called member/2
MEMBER/2
member(X,[X|T]).member(X,[H|T]):- member(X,T).
?-
MEMBER/2
member(X,[X|T]).member(X,[H|T]):- member(X,T).
?- member(yolanda,[yolanda,trudy,vincent,jules]).
MEMBER/2
member(X,[X|T]).member(X,[H|T]):- member(X,T).
?- member(yolanda,[yolanda,trudy,vincent,jules]).yes?-
MEMBER/2
member(X,[X|T]).member(X,[H|T]):- member(X,T).
?- member(vincent,[yolanda,trudy,vincent,jules]).
MEMBER/2
member(X,[X|T]).member(X,[H|T]):- member(X,T).
?- member(vincent,[yolanda,trudy,vincent,jules]).yes?-
MEMBER/2
member(X,[X|T]).member(X,[H|T]):- member(X,T).
?- member(zed,[yolanda,trudy,vincent,jules]).
MEMBER/2
member(X,[X|T]).member(X,[H|T]):- member(X,T).
?- member(zed,[yolanda,trudy,vincent,jules]).no?-
MEMBER/2
member(X,[X|T]).member(X,[H|T]):- member(X,T).
?- member(X,[yolanda,trudy,vincent,jules]).
MEMBER/2
member(X,[X|T]).member(X,[H|T]):- member(X,T).
?- member(X,[yolanda,trudy,vincent,jules]).X = yolanda;X = trudy;X = vincent;X = jules;no
CONCATENATION OF LISTS
For concatenation we first define our rule.
Step 1: if the first list is emptyconc([],L,L).Step2: if first list is not emptyconc(L1,L2,L3)=> conc([X|L1],L2,[X|
L3]):-
conc(L1,L2,L3).
L1X L2
[X|L1]
L3
L3X
L3
SAMPLE OF CONCATENATION
conc([a],[c,d],L).L = [a, c, d].
LENGTH OF A LIST IN PROLOG
len([],0). %% not a Prolog native predicate!len([_|L],N):- len(L,X), N is X + 1.
?- len([a,b,c,d,e,[a,x],t],X).X=7yes?-
SIMPLE TRACE
SIMPLE TRACE
SIMPLE TRACE
COME OUT OF TRACE
GUI TRACER
GUI TRACER