Скачать презентацию PROLOG PROgramming in LOGic Introductory Lecture Features Скачать презентацию PROLOG PROgramming in LOGic Introductory Lecture Features

1640db36733760cfe4257e6c00fb50bd.ppt

  • Количество слайдов: 19

PROLOG PROgramming in LOGic Introductory Lecture PROLOG PROgramming in LOGic Introductory Lecture

Features of PROLOG l Logical variables l Pattern matching facility l Backtracking strategy to Features of PROLOG l Logical variables l Pattern matching facility l Backtracking strategy to search for proofs

Structure of PROLOG Programs l Facts l Rules l Query l Variables: must begin Structure of PROLOG Programs l Facts l Rules l Query l Variables: must begin with an upper case letter l Constants: must begin with lowercase letter, or enclosed in single quotes

Facts l Fact is just what it appears to be l Often a proposition Facts l Fact is just what it appears to be l Often a proposition like “It is sunny” l represented as ‘It is sunny’.

Queries Action of asking the program about information contained within its data base l Queries Action of asking the program about information contained within its data base l After a program is loaded, you will receive the query prompt, ? l you can ask the program a question such as ? - 'It is sunny'. and it will respond with the answer Yes ? l

Rules give Prolog the ability to pursue its decisionmaking process l 'It is sunny'. Rules give Prolog the ability to pursue its decisionmaking process l 'It is sunny'. 'It is summer'. ‘It is hot' : - 'It is summer', 'It is sunny'. 'It is cold' : - 'It is winter', 'It is snowing'. l The query, ? - 'It is hot'. Yes l

Predicate Logic l l female(amy). female(johnette). l l l male(anthony). male(bruce). male(ogden). l l Predicate Logic l l female(amy). female(johnette). l l l male(anthony). male(bruce). male(ogden). l l l parentof(amy, johnette). parentof(amy, anthony). parentof(amy, bruce). parentof(ogden, johnette). parentof(ogden, anthony). parentof(ogden, bruce). l siblingof(X, Y) : - parentof(Z, X) , parentof(Z, Y) , X ≠ Y.

Lists & Records l List is designated by square brackets. Ex: [dog, cat, mouse] Lists & Records l List is designated by square brackets. Ex: [dog, cat, mouse] l Records or tuples are represented as patterns. l The elements of a tuple are accessed by pattern matching. book(Title, Author, Publisher, Date). author(Last. Name, First. Name, MI). publisher(Company, City). book(T, A, publisher(C, rome), Date)

Arithmetic Operators l l l l + * / // mod ** addition subtraction Arithmetic Operators l l l l + * / // mod ** addition subtraction multiplication real division integer division modulus power ? - X is 3*4. X = 12 yes

Logical Operators la : - b. % a if b l a : - Logical Operators la : - b. % a if b l a : - b, c. % a if b and c. l a : - b; c. % a if b or c. l a : - ++ b. % a if b is not provable l a : - not b. % a if b fails l a : - b -> c; d. % a if (if b then c else d)

Unification and Pattern Matching % deriv(Polynomial, variable, derivative) % dc/dx = 0 deriv(C, X, Unification and Pattern Matching % deriv(Polynomial, variable, derivative) % dc/dx = 0 deriv(C, X, 0) : - number(C). % dx/dx = 1 deriv(X, X, 1). % d(cv)/dx = c(dv/dx) deriv(C*U, X, C*DU) : - number(C), deriv(U, X, DU). % d(u v)/dx = u(dv/dx) + v(du/dx) deriv(U*V, X, U*DV + V*DU) : - deriv(U, X, DU), deriv(V, X, DV).

Functions l Prolog does not provide for a function type so, functions must be Functions l Prolog does not provide for a function type so, functions must be defined as relations. l fac(0, 1). fac(N, F) : - N > 0, M is N - 1, fac(M, Fm), F is N * Fm. l minimum(M, N, M) : - M =< N. minimum(M, N, N) : - N =< M.

Lists l List --> [ ] List --> [Element|List] l Example: length([ ], 0). Lists l List --> [ ] List --> [Element|List] l Example: length([ ], 0). length([H|T], N) : - length(T, M), N is M+1. sum([ ], 0). sum([X|L], Sum) : - sum(L, SL), Sum is X + SL.

Prolog Program l Write the program in simple text editor & save it as. Prolog Program l Write the program in simple text editor & save it as. pl l On the terminal, type gprolog. l To load a file, “[filename]. ”

Tracing l Allows to watch Prolog's reasoning as it happens l Helpful for debugging Tracing l Allows to watch Prolog's reasoning as it happens l Helpful for debugging l Type “trace. ” on the ? prompt & then type any query. l To turn off tracing type “notrace. ”

Cut It allows you to tell Prolog, which previous choices it need not consider Cut It allows you to tell Prolog, which previous choices it need not consider again when it backtracks through the chain of satisfied goals. l Example: facility(Pers, Fac) : book_overdue(Pers, Book), !, basic_facility(Fac). Facility(Pers, Fac) : - general_facility(Fac) l ? - client(X) , facility(X, Y). If a client is found to have an overdue book, then only allow the client the basic facilities of the library. Don’t bother going through all the client’s overdue books, and don’t consider any other rule about facilities.

FAIL – Built-in Predicate l Has no arguments l When a fail is encountered FAIL – Built-in Predicate l Has no arguments l When a fail is encountered after a cut, the normal backtracking behavior is altered by the effect of cut. l average_taxpayer : - foreigner(X) , ! , fail. average_taxpayer : - spouse(X, Y) , ……

l Can read up various online tutorials for detailed study of Prolog. l One l Can read up various online tutorials for detailed study of Prolog. l One good reference is “Programming in Prolog”, by Clocksin & Mellish, Narosa Publishing House.

Thank You Thank You