CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Advertisements

LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
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.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
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).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Prolog: Lists Recall lists in Scheme: '(a b c) = (cons 'a (cons 'b (cons c '()))) ls = (cons (car ls) (cdr ls)) Want the same operations in Prolog: [a,
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
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
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Introduction to C Programming
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
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.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
On Using Scheme to Introduce Prolog Daniel E. Stevenson and Michael R. Wick Department of Computer Science University of Wisconsin - Eau Claire Eau Claire,
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Programming Languages Third Edition Chapter 4 Logic Programming.
Logic Programming Chapter 15 Part 2. Breadth-first v Depth-first Search Suppose a query has compound goals (several propositions must be satisfied) Depth-first.
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 149: Operating Systems March 5 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Knowledge Based Information System
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.
CS 235: User Interface Design March 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 154 Formal Languages and Computability March 8 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 330 Programming Languages 12 / 06 / 2007 Instructor: Michael Eckmann.
Section 16.5, 16.6 plus other references
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
CS 326 Programming Languages, Concepts and Implementation
Programming Paradigms and Languages
Programming Techniques
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
CSE 3302 Programming Languages
Lists in Lisp and Scheme
Presentation transcript:

CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 2 Backtracking and the Cut in Prolog  Cut is a mechanism you can use in Prolog programs. Tell Prolog not to reconsider certain previous choices it made.  Your program will run faster. It won’t waste time attempting to satisfy goals that you know beforehand will never contribute to a solution.  Your program will use less memory. Fewer intermediate results.  Cut can be the difference between a program that runs and one that does not. _

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 3 Cut Example: Library Application  What library services can a client use? First version: service(Person, Service) :- book_overdue(Person, Book), basic_service(Service). service(Person, Service) :- general_service(Service). basic_service(reference). basic_service(inquiries). special_service(borrowing). special_service(interlibrary_loan). general_service(X) :- basic_service(X). general_service(X) :- special_service(X). book_overdue(jones, war_and_peace). book_overdue(jones, oliver_twist). ?- service(smith, S). ?- service(jones, S). library1.pl

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 4 Cut Example: Library Application, cont’d Second version: service(Person, Service) :- book_overdue(Person, Book), !, basic_service(Service). service(Person, Service) :- general_service(Service). basic_service(reference). basic_service(inquiries). special_service(borrowing). special_service(interlibrary_loan). general_service(X) :- basic_service(X). general_service(X) :- special_service(X). book_overdue(jones, war_and_peace). book_overdue(jones, oliver_twist). cut ?- service(smith, S). ?- service(jones, S). library2.pl

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 5 Cut Example: Library Application, cont’d  Match the first service rule. Subgoal: Any overdue books? Match book_overdue(jones, war_and_peace).  The next goal is the cut “ ! ”. Cut always succeeds. Cut commits Prolog to all its decisions since it matched the service rule. service(Person, Service) :- book_overdue(Person, Book), !, basic_service(Service). service(Person, Service) :- general_service(Service). basic_service(reference). basic_service(inquiries). special_service(borrowing). special_service(interlibrary_loan). general_service(X) :- basic_service(X). general_service(X) :- special_service(X). book_overdue(jones, war_and_peace). book_overdue(jones, oliver_twist). ?- service(jones, S).

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 6 Cut Example: Library Application, cont’d  The cut prevents any backtracking from going past (before) it.  Therefore, after satisfying the subgoal basic_service (Service) the query is done. Don’t look for more overdue books.  The choice of the rule that the cut appears in is also blocked from backtracking. Therefore, don’t consider any other service rules. service(Person, Service) :- book_overdue(Person, Book), !, basic_service(Service). service(Person, Service) :- general_service(Service). basic_service(reference). basic_service(inquiries). special_service(borrowing). special_service(interlibrary_loan). general_service(X) :- basic_service(X). general_service(X) :- special_service(X). book_overdue(jones, war_and_peace). book_overdue(jones, oliver_twist). ?- service(jones, S).

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 7 Cut-Failure Example: Tax Calculator  A foreigner is not an average tax payer.  If your spouse makes over $100,000, then you’re not an average taxpayer.  If you get less than $10,000 from a pension, then you’re considered not to have any gross income. average_taxpayer(X) :- foreigner(X), !, fail. average_taxpayer(X) :- spouse(X, Y), gross_income(Y, Inc), Inc > , !, fail. average_taxpayer(X) :- gross_income(Y, Inc), Inc > 20000, Inc < gross_income(X, Y) :- receives_pension(X, P), P < 10000, !, fail. gross_income(X, Y) :- gross_salary(X, Z), investment_income(X, W), Y is Z+W. gross_salary :-...

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 8 Prolog Lists  A list in Prolog is similar to a list in Lisp or Scheme, but with a different syntax.  Use square brackets and commas: [a, b, c, d, e]  Use the notation [H|T ] (where H and T are any variables) to specify the head and tail of a list. ?- [H|T] = [a, b, c, d, e]. H = a, T = [b, c, d, e]. ?- [W, X, Y|Z] = [a, b, c, d, e]. W = a, X = b, Y = c, Z = [d, e].

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 9 Prolog Lists: cons  What does this predicate do? cons(H, T, L) :- L = [H|T]. ?- cons(x, [1, 2, 3], L). L = [x, 1, 2, 3].

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 10 Prolog Lists: append  Rewrite the Scheme append procedure in Prolog: Appending the empty list to any list L2 returns the list L2. Appending a list whose head is H and tail is T to a list L2 returns a list whose head is also H and whose tail T2 is T appended to L2. (define append (lambda (lst1 lst2) (if (null? lst1) lst2 (cons (car lst1) (append (cdr lst1) lst2)) ))) append([], L2, L2). append([H|T], L2, [H|T2]) :- append(T, L2, T2). ?- append([a, b, c], [1, 2, 3], L). L = [a, b, c, 1, 2, 3].

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 11 Prolog Lists: append, cont’d  Run append backwards to find all the ways that two lists can be appended to get a specified list. ?- append(L1, L2, [a, b, c]). L1 = [], L2 = [a, b, c] ; L1 = [a], L2 = [b, c] ; L1 = [a, b], L2 = [c] ; L1 = [a, b, c], L2 = [] ; false.

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 12 Prolog Lists: reverse  Rewrite the Scheme reverse procedure in Prolog: Reversing the empty list returns the empty list. Reversing a list whose head is H and whose tail is T returns a list L which is the reverse of T (variable RT ) appended to H. (define reverse (lambda (lst) (if (null? lst) '() (append (reverse (cdr lst)) (list (car lst))) ))) reverse([], []). reverse([H|T], L) :- reverse(T, RT), append(RT, [H], L). ?- reverse([1, 2, 3, 4, 5], L). L = [5, 4, 3, 2, 1].

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 13 Prolog’s Search Strategy  Prolog does a depth-first search of the solution tree. Consider the database: Who is Bob’s ancestor? ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). ancestor(X, X). parent(amy, bob) ?- ancestor(X, bob).

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 14 Prolog’s Search Strategy, cont’d  Can we understand this trace? ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). ancestor(X, X). parent(amy, bob) ?- ancestor(X, bob). 1 ?- trace. true. [trace] 1 ?- ancestor(X, bob). Call: (6) ancestor(_G2037, bob) ? creep Call: (7) parent(_G2037, _G2111) ? creep Exit: (7) parent(amy, bob) ? creep Call: (7) ancestor(bob, bob) ? creep Call: (8) parent(bob, _G2111) ? creep Fail: (8) parent(bob, _G2111) ? creep Redo: (7) ancestor(bob, bob) ? creep Exit: (7) ancestor(bob, bob) ? creep Exit: (6) ancestor(amy, bob) ? creep X = amy ; Redo: (6) ancestor(_G2037, bob) ? creep Exit: (6) ancestor(bob, bob) ? creep X = bob.

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 15 Prolog’s Search Strategy, cont’d ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). ancestor(X, X). parent(amy, bob) ?- ancestor(X, bob). ancestor(X, bob) Y = bob 1 [trace] 1 ?- ancestor(X, bob). Call: (6) ancestor(_G2037, bob) ? creep Call: (7) parent(_G2037, _G2111) ? creep Exit: (7) parent(amy, bob) ? creep parent(amy, bob) X = amy Z = bob 3 Success

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 16 Prolog’s Search Strategy, cont’d ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). ancestor(X, X). parent(amy, bob) ?- ancestor(X, bob). ancestor(X, bob) Y = bob 1 parent(amy, bob) X = amy Z = bob [trace] 1 ?- ancestor(X, bob). Call: (6) ancestor(_G2037, bob) ? creep Call: (7) parent(_G2037, _G2111) ? creep Exit: (7) parent(amy, bob) ? creep Call: (7) ancestor(bob, bob) ? creep 3 ancestor(bob, bob) Success ancestor(bob, bob) X = bob Y = bob 1

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 17 Prolog’s Search Strategy, cont’d ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). ancestor(X, X). parent(amy, bob) ?- ancestor(X, bob). ancestor(X, bob) Y = bob 1 parent(amy, bob) X = amy Z = bob [trace] 1 ?- ancestor(X, bob). Call: (6) ancestor(_G2037, bob) ? creep Call: (7) parent(_G2037, _G2111) ? creep Exit: (7) parent(amy, bob) ? creep Call: (7) ancestor(bob, bob) ? creep Call: (8) parent(bob, _G2111) ? creep Fail: (8) parent(bob, _G2111) ? creep Redo: (7) ancestor(bob, bob) ? creep 3 ancestor(bob, bob) X = bob Y = bob 1 parent(bob, Z) 3 Failure Success REDO

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 18 Prolog’s Search Strategy, cont’d ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). ancestor(X, X). parent(amy, bob). 1 3 ?- ancestor(X, bob). ancestor(X, bob) Y = bob 1 parent(amy, bob) X = amy Z = bob 3 ancestor(bob, bob) [trace] 1 ?- ancestor(X, bob). Call: (6) ancestor(_G2037, bob) ? creep Call: (7) parent(_G2037, _G2111) ? creep Exit: (7) parent(amy, bob) ? creep Call: (7) ancestor(bob, bob) ? creep Call: (8) parent(bob, _G2111) ? creep Fail: (8) parent(bob, _G2111) ? creep Redo: (7) ancestor(bob, bob) ? creep Exit: (7) ancestor(bob, bob) ? creep Exit: (6) ancestor(amy, bob) ? creep X = amy ; 2 ancestor(bob, bob) X = bob Success X = amy 2 REDO

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 19 Prolog’s Search Strategy, cont’d ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). ancestor(X, X). parent(amy, bob). 1 3 ?- ancestor(X, bob). [trace] 1 ?- ancestor(X, bob). Call: (6) ancestor(_G2037, bob) ? creep Call: (7) parent(_G2037, _G2111) ? creep Exit: (7) parent(amy, bob) ? creep Call: (7) ancestor(bob, bob) ? creep Call: (8) parent(bob, _G2111) ? creep Fail: (8) parent(bob, _G2111) ? creep Redo: (7) ancestor(bob, bob) ? creep Exit: (7) ancestor(bob, bob) ? creep Exit: (6) ancestor(amy, bob) ? creep X = amy ; Redo: (6) ancestor(_G2037, bob) ? creep Exit: (6) ancestor(bob, bob) ? creep X = bob. 2 ancestor(bob, bob) X = bob 2 Success X = bob REDO

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 20 Assignment #4: Prolog Murder Mystery  Use Prolog to create and solve a murder mystery. Construct a Prolog database that presents a good murder mystery that can be solved deductively.  Your database must have at least 20 facts and 20 rules.  You must make at least 15 queries.  At least half of your rules and half of your queries must have subgoals separated by commas.  Example facts:  Example rule: person(jenkins, butler). killed(alice, blunt_object). owns(john, baseball_bat). having_affair(Person1, Person2) :- married(Person1, Person3), seen_together(Person1, Person2), Person2 \= Person3.

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 21 Assignment #4, cont’d  Example query: If you were Sherlock Holmes at the scene of the crime, what questions would you ask?  a zip file to that A text file of your database of facts and rules that can be loaded into Prolog, for example: SuperCoders.pl A cut-and-paste text file or screen shots that show your queries and the responses.  Name the file after your team: SuperCoders.zip  Subject line: CS 152 Assignment #4, team name Don’t forget to CC all your team members.  Due: Friday, March 14. ?- having_affair(bill, X), killed(X, blunt_object), owns(Y, hockey_stick).

SJSU Dept. of Computer Science Spring 2014: March 5 CS 152: Programming Language Paradigms © R. Mak 22 Uses of Prolog  Expert systems  Natural language processing NASA’s Clarissa voice interface for the space station  Modelling weather prediction air pollution control  See: application-of-prolog/ http:// application-of-prolog/