Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prolog rules Module 14.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.

Similar presentations


Presentation on theme: "Prolog rules Module 14.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez."— Presentation transcript:

1 Prolog rules Module 14.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

2 Topics Some Useful Predicates Examples: member, select, nth length
Backtracking, Depth-first search

3 Some Useful Predicates
The predicate append(X,Y,Z) is true iff appending Y onto the end of X yields Z. append can be used with any pattern of instantiation (i.e.with variables in any position). Examples: ?- append(X,Y,[1,2,3]). X = [] Y = [1, 2, 3] ; X = [1] Y = [2, 3] ; X = [1, 2] Y = [3] ; X = [1, 2, 3] Y = [] ; No. ?- append([1,2],[3,4],X). X = [1, 2, 3, 4] Yes. ?- append(X,[3,4],[1,2,3,4]). X = [1, 2] Yes.

4 Other Predefined List Predicates
Queries using these predicates can contain variables anywhere. Predicate Description member(X,Y) Provable if list Y contains element X. select(X,Y,Z) Provable if list Y contains element X, and removing X from Y yields Z. nth0(X,Y,Z) Provable if Z is the Xth element of list Y, counting from 0. length(X,Y) Provable if X is a list of length Y.

5 Sample Use of select select(X,Y,Z): provable if list Y contains element X, and removing X from Y yields Z. ?- select(2,[1,2,3],Z). Z = [1, 3] ; No. ?- select(2,Y,[1,3]). Y = [2, 1, 3] ; Y = [1, 2, 3] ; Y = [1, 3, 2] ; No.

6 example More complex rules:
FatherOf(john,ted). FatherOf(fred,carol). FatherOf(ted,ken). FatherOf(ted,lloyd). FatherOf(lloyd,jim). FatherOf(lloyd,joan). MotherOf(connie,ted). MotherOf(connie,carol). MotherOf(jane,ken). MotherOf(alice,lloyd) MotherOf(carol,jim). MotherOf(carol,joan). More complex rules: ParentOf(X,Y) :- FatherOf(X,Y). ParentOf(X,Y) :- MotherOf(X,Y). Alternatives are attempted in the order specified: ?- ParentOf(connie,ted). First, match FatherOf(connie,ted). Fails. Then, match MotherOf(connie,ted). Succeed. Note: There is no gender.

7 example Using variables, we can get answers: ?- ParentOf(ted,C)
FatherOf(john,ted). FatherOf(fred,carol). FatherOf(ted,ken). FatherOf(ted,lloyd). FatherOf(lloyd,jim). FatherOf(lloyd,joan). MotherOf(connie,ted). MotherOf(connie,carol). MotherOf(jane,ken). MotherOf(alice,lloyd) MotherOf(carol,jim). MotherOf(carol,joan). Using variables, we can get answers: ?- ParentOf(ted,C) C = ken; C = lloyd; No. ?- ParentOf(P,carol) P = fred; P = connie;

8 example Using more than one condition:
FatherOf(john,ted). FatherOf(fred,carol). FatherOf(ted,ken). FatherOf(ted,lloyd). FatherOf(lloyd,jim). FatherOf(lloyd,joan). MotherOf(connie,ted). MotherOf(connie,carol). MotherOf(jane,ken). MotherOf(alice,lloyd) MotherOf(carol,jim). MotherOf(carol,joan). Using more than one condition: GrandParent(X,Y) :ParentOf(X,Z),ParentOf(Z,Y). ?- GrandParent(G, jim) Prolog performs a backtracking, depth-first, left-to-right search for values to satisfy the predicate(s) sought.

9 example GP(G=X,Y=jim) PO(G=X,Z) PO(Z,Y=jim)
GrandParent(X,Y) :ParentOf(X,Z),ParentOf(Z,Y). ?- GrandParent(G, jim) FatherOf(john,ted). FatherOf(fred,carol). FatherOf(ted,ken). FatherOf(ted,lloyd). FatherOf(lloyd,jim). FatherOf(lloyd,joan). MotherOf(connie,ted). MotherOf(connie,carol). MotherOf(jane,ken). MotherOf(alice,lloyd) MotherOf(carol,jim). MotherOf(carol,joan). GP(G=X,Y=jim) PO(G=X,Z) PO(Z,Y=jim) fff FO (G=X= , Z= ) MO Z= ) (Z= , Y=jim) G = fred, ted, connie, alice AND OR

10 Another example To choose a reviewer, we’d like an author who:
- has written books on the same subject: Reviewer(Book,Person) :- SubjectOf(Book,Sub), SubjectOf(AnotherBook,Sub), AuthorOf(AnotherBook,Person), not AuthorOf(Book,Person). If not available (by subject), then find someone who has written for the same audience. Reviewer(Book,Person) :- AudienceOf(Book,Aud), AudienceOf(AnotherBook,Aud), AuthorOf(AnotherBook,Person), not AuthorOf(Book,Person). AuthorOf("Life on a Donkey","Swartz"). AuthorOf("Big and Little","Jones"). AuthorOf("Where are We","White"). AuthorOf("In a Pig’s Eye","Brown"). AuthorOf("High Flight","Smith"). SubjectOf("Life on a Donkey",travel). SubjectOf("Big and Little",life). SubjectOf("Where are We",life). SubjectOf("In a Pig’s Eye",travel). SubjectOf("High Flight",bio). AudienceOf("Life on a Donkey",adult). AudienceOf("Big and Little",child). AudienceOf("Where are We",teen). AudienceOf("In a Pig’s Eye",adult). AudienceOf("High Flight",adult).

11 summary Some Useful Predicates Examples: member, select, nth length
Backtracking, Depth-first search


Download ppt "Prolog rules Module 14.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez."

Similar presentations


Ads by Google