Prolog -Cpt114 - Week3
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Prolog -Cpt114 - Week3

on

  • 617 views

 

Statistics

Views

Total Views
617
Views on SlideShare
617
Embed Views
0

Actions

Likes
0
Downloads
42
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Prolog -Cpt114 - Week3 Presentation Transcript

  • 1. Week 3PROLOG
  • 2. OBJECTIVES FOR THIS SESSIONThe Lists in PrologTracing
  • 3. SIMPLE LISTS IN PROLOG
  • 4. HOW TO MAKE A LIST IN PROLOG pam tom bob liz ann pat jim
  • 5. HOW TO MAKE A LIST IN PROLOG parent(pam, bob). pam tom parent(tom, bob). parent(tom, liz). parent(bob, pat). liz parent(pat, jim). bob female(pam). female(liz). female(pat). ann pat male(tom). male(bob). male(jim). jim
  • 6. HOW TO MAKE A LIST IN PROLOG pam tom bob liz ann pat jim
  • 7. HOW FINDALL WORKS findall(X,predecessor(pam,X),Preds). Output listThe query variable
  • 8. EXAMPLE1The 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] ] 8
  • 9. EXAMPLE2Treat 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 | [ ] ]
  • 10. EXAMPLE2Treat 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 | [ ] ]
  • 11. MEMBERSHIP X is a member of L either: 1) X is the head of L, or 2) 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
  • 12. MEMBERSHIPSimple 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]).
  • 13. MEMBERSHIPParents = [pam, tom, tom, bob, pat]. Is pam a member of HEAD of Parents???? member(pam,[pam,Parents]).
  • 14. 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
  • 15. HEAD AND TAIL EXAMPLE 1 [mia, vincent, jules, yolanda] Head: Tail:
  • 16. HEAD AND TAIL EXAMPLE 1 [mia, vincent, jules, yolanda] Head: mia Tail:
  • 17. HEAD AND TAIL EXAMPLE 1 [mia, vincent, jules, yolanda] Head: mia Tail: [vincent, jules, yolanda]
  • 18. HEAD AND TAIL EXAMPLE 2 [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: Tail:
  • 19. HEAD AND TAIL EXAMPLE 2 [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ] Tail:
  • 20. 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]]]
  • 21. HEAD AND TAIL EXAMPLE 3 [dead(z)] Head: Tail:
  • 22. HEAD AND TAIL EXAMPLE 3 [dead(z)] Head: dead(z) Tail:
  • 23. HEAD AND TAIL EXAMPLE 3 [dead(z)] Head: dead(z) Tail: [ ]
  • 24. THE BUILT-IN OPERATOR | ?- [Head|Tail] = [mia, vincent, jules, yolanda]. Head = mia Tail = [vincent,jules,yolanda] yes ?-
  • 25. THE BUILT-IN OPERATOR | ?- [X|Y] = [mia, vincent, jules, yolanda]. X = mia Y = [vincent,jules,yolanda] yes ?-
  • 26. THE BUILT-IN OPERATOR | ?- [X|Y] = [ ]. no ?-
  • 27. 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 ?-
  • 28. 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 = mia X2 = vincent X3 = marsellus X4 = jody Tail = [yolanda] yes ?-
  • 29. ANONYMOUS VARIABLES ?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda]. X2 = vincent X4 = jody yes ?-  There is a simpler way of obtaining only the information we want: The underscore is the anonymous variable
  • 30. 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
  • 31. 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
  • 32. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?-
  • 33. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]).
  • 34. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]). yes ?-
  • 35. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]).
  • 36. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]). yes ?-
  • 37. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]).
  • 38. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]). no ?-
  • 39. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(X,[yolanda,trudy,vincent,jules]).
  • 40. 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
  • 41. CONCATENATION OF LISTSFor 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).
  • 42. [X|L1]X L1 L2 L3 X L3 L3
  • 43. SAMPLE OF CONCATENATION conc([a],[c,d],L). L = [a, c, d].
  • 44. LENGTH OF A LIST IN PROLOGlen([],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?-
  • 45. SIMPLE TRACE
  • 46. SIMPLE TRACE
  • 47. SIMPLE TRACE
  • 48. COME OUT OF TRACE
  • 49. GUI TRACER
  • 50. GUI TRACER