320 likes | 519 Views
Logic Programming. Programming Language Concepts Lecture 17. Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida. Logic Programming Fundamentals. Horn clauses: General form: H B 1 , B 2 , ... B n Meaning: if B 1 , B 2 , ... B n are true,
E N D
Logic Programming Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida
Logic Programming Fundamentals • Horn clauses: • General form: H B1, B2, ... Bn • Meaning: ifB1, B2, ... Bn are true, then H is true. • Deductive reasoning: C A,B D C D A,B
Resolution • Process of deriving new statements, combining old ones, cancelling like terms, etc. • Variables may acquire values through unification. More later. • Example: fun(X) sunny(X) sunny(Florida) fun(Florida)
Prolog Fundamentals • All Prolog programs built from terms: • Programs • The data manipulated by programs • Three types of terms: • Constants: integers, real numbers, atoms. • Variables. • Compound terms.
Prolog Fundamentals (cont’d) • Constants: Integers, e.g. 123; Reals, e.g.: 1.23 • Atoms: • Lexically, a lowercase letter followed by any number of additional letters, digits or underscores, e.g. foobar, 'Hello'. • Atoms do look like variables in other languages, but foobar is not a variable; it has no binding, it is a unique value. • An atom can also be sequence of punctuation characters:*, ., =, @#$
Prolog Fundamentals (cont’d) • Variables: begin with an upper-case letter, e.g. X, My_var. • Can be instantiated (take on a value) at run time. • Variables can start with an underscore, and get special treatment.
Prolog Fundamentals (cont’d) • A compound term, or structure, consists of an atom called a functor, and a list of arguments, e.g. sunny(florida), weird(prolog), related(jim, john). No space allowed • A compound term may look like a function call, but it isn’t. It is structured data, or logical facts.
Syntax for Terms • Even an arithmetic expression, usually written as 1+2, is just an abbreviation for +(1,2). <term> ::= <constant>|<variable>|<compound-term><constant> ::= <integer> | <real number> | <atom><compound-term> ::= <atom> ( <termlist> )<termlist> ::= <term> | <term> , <termlist>
The Prolog Database • A Prolog system maintains a collection of facts and rules of inference, a database. • A Prolog program is just a “store” of facts for this database. • The simplest item in the database is a fact: a term followed by a period: sunny(florida). sunny(california). father(jim, ann).
Simple Queries ?- sunny(florida). Yes. ?- father(jim, ann). Yes. ?- father(jim, tom). No. ?- sunny(X). Attempt to matchX. X = florida; Type a semi-colon, and X = california; the system tries again. No. This time it fails.
List notation Term denoted [] [1|X] [] .(1,X) [1] [1,2|X] .(1,[]) .(1,.(2,X))) [1,2,3] [1,2|[3,4]] [1,2,3,4] .(1,.(2,.(3,[]))) [1,parent(X,Y)] .(1,.(parent(X,Y),[])) Lists • Similar to Lisp. • [a, b, c] is syntactic sugar. • Separate head from tail using ‘|’. • ‘.’ similar to ‘cons’ in Lisp.
Pattern Matching: Unification. Examples: [george, X] unifies with [george, tom] [X, fred, [1, X]] unifies with [jane, fred, [1, Y]] by Y = X = jane [X, fred, [1, X]] does not unify with [jane, fred, [1, ralph]], because X cannot match both jane and ralph.
Unification • A constant unifies only with itself. • Two structures unify iff they have • The same functor. • The same number of arguments. • The arguments unify recursively. • A variable X unifies with anything. The other thing could: • have a value. So, instantiate X. • be an uninstantiated variable. Link the two variables, so that: • If either is instantiated later, they both share the value.
Example of Unification Unify ([p, q, [c, X]], [Y, q, [X, c]]) = Unify(p,Y) and Unify([q, [c, X]], [q, [X, c]]) = (Y=p) and Unify(q,q) and Unify( [[c, X]], [[X, c]]) = (Y=p) and Unify( [c, X], [X, c]) and Unify(nil,nil) = (Y=p) and Unify(c,X) and Unify([X],[c]) = (Y=p) and (X=c) and Unify(X,c) and Unify(nil,nil) =
Example of Unification (cont’d) (Y=p) and (X=c) and Unify(valueof(X),c) = (Y=p) and (X=c) and Unify(c,c) = (Y=p) and (X=c).
Some Useful Predicates • The predicate append(X,Y,Z) is true iff appending Y onto the end of X yields Z. Examples: ?-append([1,2],[3,4],X).X = [1, 2, 3, 4] Yes. ?- append(X,[3,4],[1,2,3,4]).X = [1, 2] Yes.
Some Useful Predicates (cont’d) • append can be used with any pattern of instantiation (that is, with variables in any position). Example: ?- append(X,[3,4],[1,2,3,4]).X = [1, 2] Yes
Some Useful Predicates (cont’d) Example: ?- 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.
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 • Queries using these predicates can contain variables anywhere.
Sample Use of select ?- 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.
Prolog Rules (or maybe not :-) • Rules are of the form predicate :- conditions. • The predicate is true iff all conditions are true. • Conditions appear as a list of terms, separated by commas.
Prolog Rules (cont’d) • The simplest rules are facts: FatherOf(john,ted). MotherOf(connie,ted). FatherOf(fred,carol). MotherOf(connie,carol). FatherOf(ted,ken). MotherOf(jane,ken). FatherOf(ted,lloyd). MotherOf(alice,lloyd) FatherOf(lloyd,jim). MotherOf(carol,jim). FatherOf(lloyd,joan). MotherOf(carol,joan). • Note: male/female names are meaningless.
Prolog Rules (cont’d) • 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.
Prolog Rules (cont’d) • Using variables, we can get answers: ?- ParentOf(ted,C) C = ken; C = lloyd; No. ?- ParentOf(P,carol) P = fred; P = connie; No.
Prolog Rules (cont’d) • Using more than one condition: GrandParent(X,Y) :- ParentOf(X,Z), ParentOf(Z,Y). ?- GranParent(G, jim) Prolog performs a backtracking, depth-first, left-to-right search for values to satisfy the predicate(s) sought.
Search for GrandParent(G,jim) 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
Another example • Facts: 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).
Another example (cont’d) • More facts: 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).
Selecting Reviewers • Would 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).
Selecting Reviewers (cont’d) • If someone who has written in the same subject is not available, 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). Exercise: Find reviewers for “Life on a Donkey”. Hint: There are 3. A reviewer can qualify twice.
Logic Programming Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida