Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.

Similar presentations


Presentation on theme: "Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming."— Presentation transcript:

1 Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming

2 Logic Programming Fundamentals Horn clauses: –General form: H  B 1, B 2,... B n –Meaning: if B 1, B 2,... B n are true, then H is true. Deductive reasoning: C  A,B D  C D  A,B

3 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)

4 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.

5 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: *,., =, @#$

6 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.

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

8 Syntax for Terms Even an arithmetic expression, usually written as 1+2, is just an abbreviation for +(1,2). ::= | | ::= | | ::= ( ) ::= |,

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

10 Simple Queries ?- sunny(florida). Yes. ?- father(jim, ann). Yes. ?- father(jim, tom). No. ?- sunny(X). Attempt to match X. X = florida; Type a semi-colon, and X = california; the system tries again. No. This time it fails.

11 Lists Similar to Lisp. [a, b, c] is syntactic sugar. Separate head from tail using ‘|’. ‘.’ similar to ‘cons’ in Lisp. List notationTerm denoted [] [1].(1,[]) [1,2,3].(1,.(2,.(3,[]))) [1,parent(X,Y)].(1,.(parent(X,Y),[])) [1|X].(1,X) [1,2|X].(1,.(2,X))) [1,2|[3,4]][1,2,3,4]

12 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.

13 Unification 1.A constant unifies only with itself. 2.Two structures unify iff they have The same functor. The same number of arguments. The arguments unify recursively. 3.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.

14 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) =

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

16 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.

17 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

18 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.

19 Other Predefined List Predicates Queries using these predicates can contain variables anywhere. PredicateDescription 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 X th element of list Y, counting from 0. length(X,Y) Provable if X is a list of length Y.

20 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.

21 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.

22 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.

23 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.

24 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.

25 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.

26 Search for GrandParent(G,jim) GP(G=X,Y=jim) PO(G=X,Z) PO(Z,Y=jim) fff FO (G=X=, Z= ) MO (G=X=, Z= ) FO (Z=, Y=jim) MO (Z=, Y=jim) G = fred, ted, connie, alice AND OR

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

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

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

30 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.

31 Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming


Download ppt "Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming."

Similar presentations


Ads by Google