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

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Chapter 11 :: Logic Languages
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
Chapter 12 - Logic Programming
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Chapter 2 Syntax and meaning of prolog programs Part 1.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Section 16.5, 16.6 plus other references
Prolog rules Module 14.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Functional Programming Languages
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Functional Programming Languages
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Logic Programming Language
Chapter 12 :: Logic Languages
PROLOG.
Presentation transcript:

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

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

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). ::= | | ::= | | ::= ( ) ::= |,

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 match X. X = florida; Type a semi-colon, and X = california; the system tries again. No. This time it fails.

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]

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

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.

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.

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

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.

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