110 likes | 124 Views
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.
E N D
Prolog rules Module 14.3COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez
Some Useful Predicates member, select, nth length Examples: Backtracking, Depth-first search Topics
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.
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
?- 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.
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.
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.
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.
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
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).
Some Useful Predicates member, select, nth length Examples: Backtracking, Depth-first search summary