PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ08B - Logic programming and Prolog Programming Language.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Chapter 11 :: Logic Languages
PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03D - Program verification Programming Language Design.
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
First Order Logic Resolution
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
Programming Languages Third Edition
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
Inference and Resolution for Problem Solving
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.
Logic Programming Languages
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
1-1 Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
The AI War LISP and Prolog Basic Concepts of Logic Programming
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.
1 Logic programming and Prolog Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 8.4.
Prolog Harry R. Erwin, PhD COMM2M University of Sunderland.
Programming Languages Third Edition Chapter 4 Logic Programming.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX - Recursive descent parsing Programming Language.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
Knowledge Based Information System
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
ML Programming Language Design and Implementation (4th Edition)
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
1) C program development 2) Selection structure
Logic Programming Language
Logic programming and Prolog
PZ07B - Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CH 4 - Language semantics
PZ03BX - Recursive descent parsing
Presentation transcript:

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ08B - Logic programming and Prolog Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 8.4 Appendix A.12

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Logic Programming So far programming has been algorithmic. Procedural: statements (C, C++, FORTRAN, Pascal) Functional: expressions (Postscript, ML, LISP) Now we will look at declarative languages - They state what the solution should look like; not how to compute such a solution. You are already an expert in some of these languages! Example, BNF. Give a description of all even length palindromes over 0 and 1: Can list them: 00, 11, 0000, 0110, 1001, 1111,... Can give a specification rule: S  0S0 | 1S1 | 00 | 11 The BNF rule does not say how to generate such strings or how to recognize a given string, only a description of what all such strings look like. There is some other process that would build the parse tree for any given string.

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Basic for Prolog We can specify either all the strings that obey the relationship we want, or we can specify a pattern (the context free production in this case) that is matched with the input string. This model occurs frequently in database applications. Consider a relation X: X(a,b) is a fact that states that a and b have relation X. Such relations and facts are the basis for Prolog execution.

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Prolog overview Development by Alain Coulmerauer and Philippe Roussel, who were interested in developing a language for making deductions from text Developed in Marseilles, France, in 1972 Based on the the principle of resolution by Kowalski from the University of Edinburgh Although there is no standard for Prolog, the version developed at the University of Edinburgh has become a widely used variant A Prolog program consists of a series of facts (concrete relationships) among data objects and a set of rules. A process of unification on Horn clauses - as we shall soon see - is the basis for Prolog execution

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Sample Prolog program 1 %editor data.prolog 2 /* Read in data as a Prolog Relation */ 3 datavals(4,[1,2,3,4]). 4 %editor pgm.prolog 5 go :- reconsult('data.prolog'), 6 datavals(A,B), 7 INSUM is 0, 8 for(A,B,INSUM,OUTSUM),nl, 9 write('SUM ='),write(OUTSUM),nl. 10 /* for loop executes 'I' times */ 11 for(I,B,INSUM,OUTSUM) :- not(I=0), 12 B=[HEAD|TAIL], 13 write(HEAD), 14 NEWVAL is INSUM+HEAD, 15 for(I-1,TAIL,NEWVAL,OUTSUM). 16 /* If I is 0, return INSUM computed value */ 17 for(_,_,INSUM,OUTSUM) :- OUTSUM = INSUM. 18 not(X) :- X, !, fail. 19 not(_). Figure A.16 in text Relation go starts execution

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Simple Prolog facts A database of facts: inclass(john, cmsc330). inclass(mary, cmsc330). inclass(george, cmsc330). inclass(jennifer, cmsc330). inclass(john, cmsc311). inclass(george, cmsc420). inclass(susan, cmsc420).[.5ex] Queries: Prolog can confirm these facts: inclass(john, cmsc330).  Prolog responds “yes” inclass(susan, cmsc420).  Prolog responds “yes” inclass(susan, cmsc330).  Prolog responds “no” Prolog will search database; but these are not very interesting.

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Example of unification Can use a form of substitution called unification to derive other relationships. (Will discuss unification more formally later.) inclass(susan, X).  Prolog searches database and responds “X=cmsc420.” Hitting Enter key, Prolog says “No” since no other fact. inclass(john, Y).  Prolog has following responses: “Y=cmsc330.” “Y=cmsc311.” “no.” Can define more complex queries: takingboth(X):-inclass(X, cmsc330),inclass(X, cmsc311). takingboth(john) takingboth(Y) yes Y=john. no

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Prolog execution So Prolog is: 1. A database of facts. 2. A database of queries. 3. A sequential execution model: Search facts in order looking for matches. If failure, back up to last match and try next entry in database. Because of this last item, Prolog is not truly declarative. It does have an algorithmic execution model buried in structure of database. Also, unlike previous languages studied, Prolog is not complete. Not every program function can be specified as a Prolog program.

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Simple queries Other Examples. Multiple definitions: registered(X) :- takingboth(X). registered(X) :- inclass(X, cmsc330). registered(X) :- inclass(X, cmsc311). registered(X) :- inclass(X, cmsc420). printclass(X) :- registered(X), write(X), write(' is registered.'), nl. printclass(X) :- write(X), write(' is not registered.'),nl. printclass(susan) prints 'susan is registered' printclass(harold) prints 'harold is not registered' Adding to database: consult(user) or consult('filename') adds to data base reconsult(user) modifies database control-D (  D) returns to query mode.

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Prolog Data Data: Integers: 1, 2, 3, 4 Reals: 1.2, 3.4, 6.7 Strings: 'abc', '123' Facts: lower case names Variables: Upper case names Lists: [a, b, c, d] Like ML: [ head | tail ]

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Append function APPEND function: append([a,b,c], [d,e], X)  X = [a,b,c,d,e] Definition: append([ ], X, X). append( [ H | T], Y, [ H | Z]) :- append(T, Y, Z). (function design similar to applicative languages like ML.)

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Clauses REVERSE: reverse([ ],[ ]). reverse( [H | T ], L) :- reverse(T,Z), append(Z, [H], L). Prolog execution is via clauses: a,b,c,d. If any clause fails, backup and try alternative for that clause. Cuts(!). Always fail on backup. Forces sequential execution. Programs with many cuts are really an imperative program.

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Cuts Logical predicates: P :- a, b. P :- c,d. P is true if a and b are true or if c and d are true, or (a  b)  (c  d) (This is where the term logic programming comes from.) But: P :- a, !, b. P :- c,d. P is true if (a and b) are true or if (a is false and (c and d are true)) (a  b)  (not(a)  c  d) ( See difference?) Cuts can be used for sequential execution: P :- a, !, b, !, c, !, d.

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Not and Assignment Not definition: not(X) :- X, !, fail. not(_). If X succeeds, then fail (! prevents backtracking), and if X fails then the second definition always succeeds. Assignment: X is 3+1  means arithmetic value X = 3+1  means same expression X=3+1, X=4  fails (X unifies to expression 3+1. The expression 3+1 is not 4.) X is 3+1, X=4  succeeds (X unifies to 4.)

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Underlying theory of Prolog - briefly Any logical predicate can be written in disjunctive normal form (DNF): As a series of “or'ed” terms: A  (B  C) = (A  B)  (A  C) Consider any such predicate in DNF: A   B  C  D   E Group negated and non-negated terms together: (A  C  D)  (  B   E) Write as: A, C, D :- B, E. But: (A  C  D)  (  B   E)  (A  C  D)   (B  E) by de Morgan's laws  (B  E) implies (A  C  D). So if (B  E) is true, then (A  C  D) is true.

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Horn clauses What if we only have one non-negated term? A :- B, E. We call such expressions Horn clauses. A:- B, E. means (B  E)  A So if we show that B and E are true, then we have shown that A must be true. We call A :- B, E. a query. Note: If we write a fact A., then we are simply saying that we have the Horn clause: true  A.

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Unification Prolog unification is an extension to variable substitution. Macros are an example of substitution: Generate quadratic formula: a x 2 + b x + c = 0 root(A, B, C) = -B + sqrt(B*B-4*A*C)/(2*A) If A=1, B=2, C=3+4, substitution gives us: root(1, 2, 3+4) = -(2) + sqrt((2)*(2)- 4*(1)*(3+4))/(2*(1)) Consider following 2 sets of rules: P(x,y) = x * y Q(z) = z 2

PZ08B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Unification - 2 Now consider: P(c+d, Q(e+f)) = (c+d)*Q(e+f) = (c+d) * (e+f) 2 P(c+d, Q(e+f)) = P(c+d, (e+f) 2 ) = (c+d) * (e+f) 2 Same result achieved two ways, substitute into P first or into Q. (c+d) * (e+f) 2 represents both a substitution into P and a substitution into Q. The P substitution is c+d for x, Q(e+f) for y. The Q substitution is e+f for z. So we say c+d for x and Q(e+f) for y and e+f for z unifies P and Q. Example: append([2,3],[4,5],L)  L=[2,3,4,5] unifies clause. append([1,2],L,M), reverse(M,[3,2,1])  L=[3], M=[1,2,3] unify these clauses. Note in this last case, M came from reverse, L came from append. Substitution works both ways  and .