Prolog -Cpt114 - Week3

756 views

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
756
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
63
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Prolog -Cpt114 - Week3

  1. 1. Week 3PROLOG
  2. 2. OBJECTIVES FOR THIS SESSIONThe Lists in PrologTracing
  3. 3. SIMPLE LISTS IN PROLOG
  4. 4. HOW TO MAKE A LIST IN PROLOG pam tom bob liz ann pat jim
  5. 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. 6. HOW TO MAKE A LIST IN PROLOG pam tom bob liz ann pat jim
  7. 7. HOW FINDALL WORKS findall(X,predecessor(pam,X),Preds). Output listThe query variable
  8. 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. 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. 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. 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. 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. 13. MEMBERSHIPParents = [pam, tom, tom, bob, pat]. Is pam a member of HEAD of Parents???? member(pam,[pam,Parents]).
  14. 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. 15. HEAD AND TAIL EXAMPLE 1 [mia, vincent, jules, yolanda] Head: Tail:
  16. 16. HEAD AND TAIL EXAMPLE 1 [mia, vincent, jules, yolanda] Head: mia Tail:
  17. 17. HEAD AND TAIL EXAMPLE 1 [mia, vincent, jules, yolanda] Head: mia Tail: [vincent, jules, yolanda]
  18. 18. HEAD AND TAIL EXAMPLE 2 [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: Tail:
  19. 19. HEAD AND TAIL EXAMPLE 2 [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ] Tail:
  20. 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. 21. HEAD AND TAIL EXAMPLE 3 [dead(z)] Head: Tail:
  22. 22. HEAD AND TAIL EXAMPLE 3 [dead(z)] Head: dead(z) Tail:
  23. 23. HEAD AND TAIL EXAMPLE 3 [dead(z)] Head: dead(z) Tail: [ ]
  24. 24. THE BUILT-IN OPERATOR | ?- [Head|Tail] = [mia, vincent, jules, yolanda]. Head = mia Tail = [vincent,jules,yolanda] yes ?-
  25. 25. THE BUILT-IN OPERATOR | ?- [X|Y] = [mia, vincent, jules, yolanda]. X = mia Y = [vincent,jules,yolanda] yes ?-
  26. 26. THE BUILT-IN OPERATOR | ?- [X|Y] = [ ]. no ?-
  27. 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. 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. 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. 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. 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. 32. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?-
  33. 33. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]).
  34. 34. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]). yes ?-
  35. 35. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]).
  36. 36. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]). yes ?-
  37. 37. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]).
  38. 38. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]). no ?-
  39. 39. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(X,[yolanda,trudy,vincent,jules]).
  40. 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. 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. 42. [X|L1]X L1 L2 L3 X L3 L3
  43. 43. SAMPLE OF CONCATENATION conc([a],[c,d],L). L = [a, c, d].
  44. 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. 45. SIMPLE TRACE
  46. 46. SIMPLE TRACE
  47. 47. SIMPLE TRACE
  48. 48. COME OUT OF TRACE
  49. 49. GUI TRACER
  50. 50. GUI TRACER

×