CSE 3302 Programming Languages

Slides:



Advertisements
Similar presentations
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
Advertisements

Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Introduction to Prolog, cont’d Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
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).
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
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.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Programming Languages Third Edition
Discussion #20 1/11 Discussion #20 Resolution in Datalog.
(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
Formal Models of Computation Part II The Logic Model
CS 321 Programming Languages and Compilers Prolog part 2.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
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.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Lecture 9b: Prolog Heshaam Faili University of Tehran The language of logic Concepts Examples.
CS 363 Comparative Programming Languages Logic Programming Languages.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
© 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.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.
ISBN Chapter 16 Logic Programming Languages.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
Dr. Muhammed Al-Mulhem ICS An Introduction to Prolog.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
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”
1-1 An Introduction to Prolog Sept Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Lecture 2: Unification and proof search.
More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y),
CS 330 Programming Languages 12 / 06 / 2007 Instructor: Michael Eckmann.
Section 16.5, 16.6 plus other references
CSE 3302 Programming Languages
Prolog a declarative language
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
CSE 341, S. Tanimoto Logic Programming -
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
CSE 3302 Programming Languages
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Tests, Backtracking, and Recursion
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 12 :: Logic Languages
Logic Programming Language
CSE 3302 Programming Languages
Administrative Issues
Clauses and Predicates
Chapter 2: Prolog (Introduction and Basic Concepts)
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Presentation transcript:

CSE 3302 Programming Languages Logic Programming: Prolog (II) Chengkai Li Fall 2007 Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 SWI-Prolog Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Resources Download: http://www.swi-prolog.org/dl-stable.html Documentation: (You don’t necessarily need to read. But good for reference when you have questions.) http://www.swi-prolog.org/dl-doc.html Menu “Help -> Online Manual” (HTML files in directory “doc”) ?-help(help). Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Query Prompt query prompt ?- (Enter goals after “?-”) Example: ?- help(help). Load a file with facts ?-[swi(‘myprogram/test.pl’)]. or ?-[swi(‘myprogram/test’)]. (myprogram must be a subdirectory in the swi-prolog program directory) Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 User Interaction ?- parent(bob,sam). (a query must end with . ) Yes (can be proved) ?- parent(bob,jill). No (cannot prove) ?- parent(bill,X), | parent(X,sam) |. (user can use multiple lines to write a query, using | ) No ?- parent(X, sam). X = jill ; (user typed ; to ask for more answers.) X = bob Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Debugging ?- trace, parent(X, sam). Call: (8) parent(_G494, sam) ? creep Call: (9) mother(_G494, sam) ? creep Exit: (9) mother(jill, sam) ? creep Exit: (8) parent(jill, sam) ? creep X = jill ; Redo: (8) parent(_G494, sam) ? creep Call: (9) father(_G494, sam) ? creep Exit: (9) father(bob, sam) ? creep Exit: (8) parent(bob, sam) ? creep X = bob More details in section 2.9 and 4.2.8 of the manual Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Prolog Syntax Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Basic Syntax <clause> ::= <fact> | <rule> <fact> ::= <term> . <rule> ::= <term> :- <termlist> . <termlist> ::= <term> | <term> , <termlist> <term> ::= <variable> | <constant> | <compound-term> <constant> ::= <number> | <atom> <compound-term> ::= <atom> ( <termlist> ) Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Prolog syntax :- for  , for and Uppercase: variable Lowercase: other names (constants, atom (i.e., name of predicate)) Built-in predicates: read, write, nl (newline) =,is,<,>,=<,>=,/,*,+,-,mod,div (Note it is =<, not <=) Lecture 21 – Prolog, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Arithmetic Arithmetic operation can use prefix or infix notations. +(3,4) 3+4 Value is not immediately evaluated. ?- write(3+5). ?- X is 3+5. (is a predicate that evaluates 3+5) ?- 3+4 = 4+3. (these are two different terms) No. ?- X is 3+4, Y is 4+3, X = Y. (unification) X=7, Y=7 Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Unification The semantics of = is determined by unification, i.e., = forces unification. (See unification algorithm in Page 556) ?- me = me. Yes ?- me = you. No ?- me = X. X = me ?- f(a,X) = f(Y,b). X = b, Y = a ?- f(X) = g(X). Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 List ?- [H|T]=[1,2,3]. H = 1, T = [2,3] ?- [H1,H2|T]=[1,2,3]. H1 = 1, H2 = 2, T = [3] ?- [H1,H2,H3|T]=[1,2,3,4,5]. H3 = 3, T = [4,5] Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 List Operations Concatenation: ?- X = [0,1|[2,3,4]]. X = [0,1,2,3,4] Get elements, or tail : ?- [H1,H2|[3,4]] = [0,1|[2,3,4]] What do we get? Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Define List Operation Predicates cons(X,Y,L) :- L = [X|Y]. ?- cons (0,[1,2,3],A). ?- cons (X,Y,[1,2,3]). Rewrite cons: cons(X,Y, [X|Y]). Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Define List Operation Predicates append(X,Y,Z) :- X = [ ], Y=Z. append(X,Y,Z) :- X = [A|B], Z=[A|W], append(B,Y,W). Another definition append([ ],Y,Y). append([A|B], Y, [A|W]) :- append(B,Y,W). ?- append(X, Y, [1,2]). Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 reverse([],[]). reverse([H|T], L) :- reverse(T,L1), append(L1, [H], L). Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Prolog’s Search Strategy Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Resolution and Unification Order matters: The order to resolve subgoals. The order to use clauses to resolve subgoals. Thus programmers must know the orders used by the language implementations, in order to write efficient or even correct program. (Search Strategies) Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Prolog’s Strategy Depth-first search The order to resolve subgoals. (left to right) The order to use clauses to resolve subgoals. (top to bottom) Backtrack: try another clause when it fails. Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 1 Facts: ancestor(X,Y) :- ancestor(X,Z), parent(Z,Y). ancestor(X,Y) :- parent(X,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Queries: ?- ancestor(bill,sam). Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 1 Facts: ancestor(X,Y) :- ancestor(X,Z), parent(Z,Y). ancestor(X,Y) :- parent(X,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Queries: :- ancestor(bill,sam). ancestor(X1,Y1) :- ancestor(X1,Z1), parent(Z1,Y1). :- ancestor(bill,Z1), parent(Z1,sam). X1= bill, Y1=sam Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 1 Facts: ancestor(X,Y) :- ancestor(X,Z), parent(Z,Y). ancestor(X,Y) :- parent(X,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Queries: :- ancestor(bill,Z1), parent(Z1,sam). ancestor(X2,Y2) :- ancestor(X2,Z2), parent(Z2,Y2). :- ancestor(bill,Z2), parent(Z2,Z1), parent(Z1,sam). X2= bill, Y2=Z1 Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 1 Facts: ancestor(X,Y) :- ancestor(X,Z), parent(Z,Y). ancestor(X,Y) :- parent(X,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Queries: :- ancestor(bill,Z2), parent(Z2,Z1), parent(Z1,sam). ancestor(X3,Y3) :- ancestor(X3,Z3), parent(Z3,Y3). … Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 1 ancestor(bill,sam) 2 ancestor(bill,Z1), parent(Z1,sam) 2 ancestor(bill,Z2), parent(Z2,Z1) parent(Z1,sam) 2 … Resulting in an infinite loop. Original order was bad Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 2 Facts: ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- ancestor(X,Z), parent(Z,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Queries: ?- ancestor(bill,sam). What will happen? Note that we change the order of the first two clauses in facts. 1 2 3 4 5 6 7 Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 2 ancestor(bill,sam) 2 1 parent(bill,sam) ancestor(bill,Z), parent(Z,sam) 1 3 4 mother(bill,sam) father(bill,sam) parent(bill,Z), parent(Z,sam) failure failure 3 4 mother(bill,Z), parent(Z,sam) failure father(bill,Z), parent(Z,sam) 5 parent(jill,sam) 3 mother(jill,sam) 6 success Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 3 Facts: ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- ancestor(X,Z), parent(Z,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Queries: ?- ancestor(X,bob). 1 2 3 4 5 6 7 Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 3 ancestor(X,bob) 1 2 parent(X,bob) ancestor(X,Z1), parent(Z1,bob) 1 parent(X,Z1), parent(Z1,bob) failure 2 failure ancestor(X,Z2), parent(Z2,Z1), parent(Z1,bob) … Resulting in an infinite loop. Original order was bad Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example 4 Facts: ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Queries: ?- ancestor(X,bob). What will happen? Note that we change the order of the two subgoals in clause (2). 1 2 3 4 5 6 7 Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Loops and Control: fail and cut (!) Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 fail Loops: Enforce backtrack even when an answer is found (using built-in predicate fail) Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example Print all solutions of appending. printpieces(L) :- append(X,Y,L), write(X), write(Y), nl, fail. ?- printpieces([1,2]). [][1,2] [1][2] [1,2][] No Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example num(0). num(X) :- num(Y), X is Y+1. ?- num(X). writenum(I,J) :- num(X), I =< X, X =< J, write(X), nl, fail. ?- writenum(1,10). Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 cut Cut (using built-in predicate !) branches in the search tree (to avoid infinite loop). “prunes” the search tree of all other siblings to the right of the node containing !. …, !, … Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example writenum(I,J) :- num(X), I =< X, X =< J, write(X), nl, X = J, !, fail. ?- writenum(1,10). Lecture 22 – Prolog (II), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007