CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose...

22
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

description

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

Transcript of CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose...

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

CS 152: Programming Language Paradigms

March 5 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2014Instructor: Ron Mak

www.cs.sjsu.edu/~mak

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

SJSU Dept. of Computer ScienceSpring 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._

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

SJSU Dept. of Computer ScienceSpring 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

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

SJSU Dept. of Computer ScienceSpring 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

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

SJSU Dept. of Computer ScienceSpring 2014: March 5

CS 152: Programming Language Paradigms© R. Mak

5

Cut Example: Library Application, cont’d

Match the firstservice 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).

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

SJSU Dept. of Computer ScienceSpring 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).

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

SJSU Dept. of Computer ScienceSpring 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 > 100000, !, fail.average_taxpayer(X) :- gross_income(Y, Inc), Inc > 20000, Inc < 80000.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 :- ...

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

SJSU Dept. of Computer ScienceSpring 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].

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

SJSU Dept. of Computer ScienceSpring 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].

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

SJSU Dept. of Computer ScienceSpring 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 isT 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].

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

SJSU Dept. of Computer ScienceSpring 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.

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

SJSU Dept. of Computer ScienceSpring 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].

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

SJSU Dept. of Computer ScienceSpring 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).

1

2

3

?- ancestor(X, bob).

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

SJSU Dept. of Computer ScienceSpring 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).

1

2

3

?- 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) ? creepX = amy ; Redo: (6) ancestor(_G2037, bob) ? creep Exit: (6) ancestor(bob, bob) ? creepX = bob.

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

SJSU Dept. of Computer ScienceSpring 2014: March 5

CS 152: Programming Language Paradigms© R. Mak

15

Prolog’s Search Strategy, cont’dancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).ancestor(X, X).parent(amy, bob).

1

2

3

?- 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 = amyZ = bob

3

Success

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

SJSU Dept. of Computer ScienceSpring 2014: March 5

CS 152: Programming Language Paradigms© R. Mak

16

Prolog’s Search Strategy, cont’dancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).ancestor(X, X).parent(amy, bob).

1

2

3

?- ancestor(X, bob).

ancestor(X, bob)

Y = bob

1

parent(amy, bob)

X = amyZ = 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 = bobY = bob

1

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

SJSU Dept. of Computer ScienceSpring 2014: March 5

CS 152: Programming Language Paradigms© R. Mak

17

Prolog’s Search Strategy, cont’dancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).ancestor(X, X).parent(amy, bob).

1

2

3

?- ancestor(X, bob).

ancestor(X, bob)

Y = bob

1

parent(amy, bob)

X = amyZ = 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)

ancestor(bob, bob)

X = bobY = bob

1

parent(bob, Z)3Failure

SuccessREDO

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

SJSU Dept. of Computer ScienceSpring 2014: March 5

CS 152: Programming Language Paradigms© R. Mak

18

Prolog’s Search Strategy, cont’dancestor(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 = amyZ = 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) ? creepX = amy ;

2 ancestor(bob, bob)

X = bob

Success

Success

X = amy

2

REDO

REDO

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

SJSU Dept. of Computer ScienceSpring 2014: March 5

CS 152: Programming Language Paradigms© R. Mak

19

Prolog’s Search Strategy, cont’dancestor(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) ? creepX = amy ; Redo: (6) ancestor(_G2037, bob) ? creep Exit: (6) ancestor(bob, bob) ? creepX = bob.

2

ancestor(bob, bob)

X = bob

2

Success

X = bob REDO

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

SJSU Dept. of Computer ScienceSpring 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.

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

SJSU Dept. of Computer ScienceSpring 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?

Email a zip file to [email protected] that contains: 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).

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

SJSU Dept. of Computer ScienceSpring 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: http://www.drdobbs.com/parallel/the-practical-application-of-prolog/184405220