1 / 11

Prolog rules

Prolog rules. Module 14.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez. Some Useful Predicates member, select, nth length Examples: Backtracking, Depth-first search. Topics. The predicate append(X,Y,Z) is true iff appending Y onto the end of X yields Z.

sbyler
Download Presentation

Prolog rules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. The predicateappend(X,Y,Z) is true iff appending Y onto the end of XyieldsZ. append can be used with any pattern of instantiation (i.e.with variables in any position). Examples: Some Useful Predicates ?- 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. 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. Other Predefined List Predicates

  5. ?- 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. Sample Use of select select(X,Y,Z):provable if list Y contains element X, and removing X from Y yields Z.

  6. 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). example 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,matchMotherOf(connie,ted).Succeed. Note: There is no gender.

  7. example 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; • No.

  8. example 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 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) AND PO(G=X,Z) PO(Z,Y=jim) OR OR FO (G=X= , Z= ) MO (G=X= , Z= ) FO (Z= , Y=jim) MO (Z= , Y=jim) fff G = fred, ted, connie, alice

  10. 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). 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).

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

More Related