CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San...

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

Transcript of CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San...

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

CS 152: Programming Language Paradigms

February 26 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 February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

2

Assignment #3

You are provided with Scheme code that differentiates polynomial expressions using infix notation.

Write two additional procedures evaluate and evaluate-deriv that each takes two parameters, f and x. f is a list that represents the function’s polynomial expression

in infix notation. x is the value of x to evaluate f or f ', respectively.

_

312645)7362( 2342345 xxbxaxxxxbxaxdx

d

(deriv '(a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7) 'x) (5 a x ^ 4 + 4 b x ^ 3 + 6 x ^ 2 + 12 x + 3)

No * operators.

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

3

Assignment #3, cont’d

The coefficient values can be defined beforehand. Example: (define a 1)

Your procedures must work at least with the following values for f, a, and b, and x bound to 0 and then to 1.

Of course, you should test with other values.

(define f '(a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7))(deriv f 'x) (5 a x ^ 4 + 4 b x ^ 3 + 6 x ^ 2 + 12 x + 3)

(define a 1)(define b 1)

(evaluate f 0) 7(evaluate f 1) 20

(evaluate-deriv f 0) 3(evaluate-deriv f 1) 30

No * operators.

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

4

First-Class Objects

A first-class object is an object that Can have variables bound to it.

(define v obj) Can be passed as an argument to a procedure.

(proc obj) Can be returned as a value by a procedure (i.e., function).

(define obj (proc x y))

A Scheme procedure is a first-class object. A procedure can be bound to a variable.

(define p proc) A procedure can have a procedure as an argument.

(proc1 x y proc2) A procedure can return a procedure as its return value.

(define proc2 (proc1 x y))

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

5

Example: Passing a Procedure as an Argument

(define first-or-second (lambda (pred x y) (if (pred x y) x y)))

(first-or-second < 5 6) 5(first-or-second > 5 6) 6(first-or-second equal? '(1 2 3) (cons 1 '(2 3))) (1 2 3)(first-or-second equal? '(1 2 3) (cons 1 '(2 4))) (1 2 4)

Apply predicate argument pred to arguments x and y.

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

6

Passing a Procedure: map

Apply procedure argument proc to each top-level element of argument lst.

(define map (lambda (proc lst) (if (null? lst) '() (cons (proc (car lst)) (map proc (cdr lst))))))

(map add1 '(1 2 3 4 5)) (2 3 4 5 6)(map (lambda (n) (* n n)) '(1 2 3 4 5)) (1 4 9 16 25)

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

7

Passing a Procedure: map cont’d

(map (lambda (lst) (car lst)) '((a) (b c d) (e f))) (a b e)

(define list-of-first-items (lambda (lst) (map car lst)))

(list-of-first-items '((a) (b c d) (e f))) (a b e)

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

8

Returning a Procedure: compose

compose is a procedure that takes two arguments f and g.

It returns a procedure that takes a single argument x.

(define compose (lambda (f g) (lambda (x) (f (g x)))))

compose #<procedure>

(compose sqrt add1) #<procedure>

((compose sqrt add1) 8) 3((compose add1 sqrt) 8) 3.8284271247461903

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

9

Only a Brief Taste of Lisp Lisp is a language that epitomizes

the functional programming paradigm. It is rich with features and concepts not often found

in object-oriented languages. Scheme is a major dialect of Lisp, along with Common Lisp.

You only got a brief taste of the power, flexibility, and usefulness of this language!

The classic reference for Scheme is the textbook Structure and Interpretation of Computer Programs, by Harold Abelson and Gerald Jay Sussman of M.I.T. For years, Scheme was the language they taught in their

Introduction to Programming classes. Free copy of the second edition:

http://mitpress.mit.edu/sicp/full-text/sicp/book/book.html

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

10

Programming Paradigms

Different programming paradigms (cultures).

Functional Lisp, Scheme, ML, Haskell, F#

Logic Prolog

Object-oriented C++, C#, Objective C, Java, etc.

_

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

11

Logic

Logic is the “science of reasoning and proof”.

Studied by several ancient civilizations. India, China, Persia Greece

Established as a formal discipline by Aristotle as a fundamental place in philosophy.

Deductive reasoning “Top down” logic. Reach a conclusion from one or more premises. Example:

1. All men are mortal. 2. Aristotle is a man. 3. Therefore, Aristotle is mortal.

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

12

Prolog

Prolog (Programming in Logic) language

A logical and a declarative programming language.

Logical Based on deductive reasoning

Declarative Your program specifies what result you want,

and the Prolog system figures out how to compute it._

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

13

Objects and Relations

Prolog programs solve problems that involve objects and the relationships between objects. Example: “John owns the book.”

Objects: John, the book Relationship: ownership

Relationship has a specific order. John owns the book,

but the book doesn’t own John.

Query a relationship. Does John own the book?

_

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

14

Objects and Relations

Objects and relationships are abstractions. How much detail we need to provide

about the objects and their relationships depends on the problem we want to solve.

Example: “Two people are sisters if they are both female and have the same parents.” Perhaps an oversimplified rule. But acceptable as a definition. Sufficient for the query: “Are Charlotte and Emily sisters?”

_

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

15

Programming in Prolog

Prolog programming consists of: Declaring some facts about objects and their relationships. Defining some rules about objects and their relationships. Asking questions about objects and their relationships.

A Prolog system enables a computer To be a database of facts and rules. To make inferences from one fact to another.

Inference: The process of deriving logical conclusions from premises known or assumed to be true. Prolog is an inference engine.

_

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

16

Programming in Prolog

Prolog is an interactive language, like Scheme.

Download and install SWI Prolog on Windows or Mac:http://www.swi-prolog.org/ Reference manual:

http://www.swi-prolog.org/download/stable/doc/SWI-Prolog-6.4.1.pdf

Windows documentation: http://www.swi-prolog.org/windows.html In the interaction window, load the file demo/likes.pl:

[swi('demo/likes')].

Prolog tutorial “Learn Prolog Now!” http://www.learnprolognow.org/

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

17

Facts

Examples of facts, with possible English interpretations:

Prolog syntax: The names of all objects and relationships

must begin with a lower-case letter. The name of a relationship is the predicate. The objects that the predicate uses are its arguments. Each fact must end with a period.

valuable(gold). Gold is valuable.

female(mary). Mary is female.

owns(john, gold). John owns gold.

father(sam, john). Sam is John’s father.

likes(john, mary). John likes Mary.

gives(john, flowers, mary). John gives flowers to Mary.

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

18

Questions

A question in Prolog:

Does Mary own a book? Is it a fact that Mary owns a book?

When you ask a question in Prolog:

Prolog searches through its database of facts.

It looks for facts that match the fact in question. Two facts match if they have the same predicate.

If it finds a match, Prolog answers yes or true.

If there is no matching fact in the database, Prolog answers no or false._

?- owns(mary, book).

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

19

Questions, cont’d

Consider this database:

Then we can have this interactive session with Prolog:

likes(joe, fish).likes(joe, mary).likes(mary, book).likes(john, book).

?- likes(joe, money).false.?- likes(mary, joe).false.?- likes(mary, book).true.?- king(john, france).ERROR

The answer falsemeans only that there was no match in the database.

Or that the questionis not provable.

Demo

likes1.pl

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

20

Variables

Consider this database:

We can ask a question containing a variable. A variable name starts with a capital letter.

What does John like? Prolog will respond:

By pressing the Enter key, you tell Prolog that you are satisfied with the answer, and it should not search further.

If you press the semicolon key, Prolog will continue its search:

likes(john, flowers).likes(john, mary).likes(paul, mary).

?- likes(john, X).

X = flowers

X = mary.

likes2.pl

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

21

Variables, cont’d

A variable is instantiated when it stands for an object. In this example, X is instantiated to flowers Otherwise, the variable is not instantiated.

_

likes(john, flowers).likes(john, mary).likes(paul, mary).

?- likes(john, X).X = flowers

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

22

Place Markers

Whenever Prolog finds a fact that matches a question, it places a marker in the database at that fact.

If you ask it to search further, Prolog starts from the place marker. Prolog attempts to re-satisfy the question.

Another question: Who likes Mary?

?- likes(X, mary).

X = john ;X = paul.

First answer. We type ; in reply.

No more answers.

likes(john, flowers).likes(john, mary).likes(paul, mary).

Page 23: CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

23

Conjunctions

Consider this database:

Does John like Mary and does Mary like John?

The comma , is pronounced “and”. Prolog must now satisfy the two separate subgoals.

Is there anything that Mary and John both like?

Prolog keeps a separate place marker for each goal.

likes(mary, food).likes(mary, wine).likes(john, wine).likes(john, mary).

?- likes(john, mary), likes(mary, john).

?- likes(mary, X), likes(john, X).

likes3.pl

Page 24: CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

24

Backtracking

Suppose Prolog satisfies the first goal of a conjunction.

If then it cannot satisfy the second goal, it backtracks to the first goal and attempts to re-satisfy that goal. If it succeeds, then it will attempt the second goal again.

_

?- likes(mary, X), likes(john, X).

Page 25: CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

25

Backtracking, cont’d

The first goal succeeds. X instantiates to food.

Next attempt the second goal. There is no rule likes(John, food). The second goal fails.

_

?- likes(mary, X), likes(john, X).

likes(mary, food).likes(mary, wine).likes(john, wine).likes(john, mary).

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

26

Backtracking, cont’d

Backtrack to the first goal. X instantiates to wine.

Next attempt the second goal. Now the second goal succeeds.

Mary and John both like wine._

?- likes(mary, X), likes(john, X).

likes(mary, food).likes(mary, wine).likes(john, wine).likes(john, mary).

Page 27: CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

27

Rules

An example rule in Prolog:

John likes anyone who likes wine. The symbol :- is pronounced “if”:

John likes X if X likes wine.

A rule consists of a head and a body. The head is to the left of :- The body is to the right of :-

Another rule:

John loves women who like wine.

likes(john, X) :- likes(X, wine).

loves(john, X) :- female(X), likes(X, wine).

Page 28: CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

28

Rules, cont’d

male(john).male(bob).female(mary).

likes(bob, wine).likes(mary, wine).

likes(john, X) :- likes(X, wine).loves(john, X) :- female(X), likes(X, wine).

?- likes(john, X).X = bob ;X = mary ;false.

?- loves(john, Y).Y = mary.

likes4.pl

Demo

Page 29: CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

29

Rules, cont’d

Is Alice the sister of Edward?

Match rule sister_of(X, Y). X instantiates to alice. Y instantiates to edward.

male(albert).male(edward).

female(alice).female(victoria).

parents(edward, victoria, albert).parents(alice, victoria, albert).

sister_of(X, Y) :- female(X), parents(X, Mom, Dad), parents(Y, Mom, Dad).

?- sister_of(alice, edward).

Satisfy the subgoals of the rule sister_of(X, Y).

Match fact female(alice).

Match fact parents(alice, victoria, albert). Mom instantiates to victoria. Dad instantiates to albert.

Match fact parents(edward, victoria, albert).

All the subgoals succeeded, therefore Prolog answers true_

family.pl

Page 30: CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

30

Rules, cont’d

Match rule sister_of(X, Y). The question’s variable X is

shared with the rule’s variable Y. When one is instantiated, so is

the other to the same object.

male(albert).male(edward).

female(alice).female(victoria).

parents(edward, victoria, albert).parents(alice, victoria, albert).

sister_of(X, Y) :- female(X), parents(X, Mom, Dad), parents(Y, Mom, Dad).

?- sister_of(alice, X).

Satisfy the subgoals of therule sister_of(X, Y). Match fact female(alice). Match fact

parents(alice, victoria, albert). Mom instantiates to victoria. Dad instantiates to albert.

Variable Y is still uninstantiated. Match fact

parents(edward, victoria, albert). Y instantiates to edward.

All the subgoals succeeded. Y is shared with the question’s X

X also instantiates to edward. Prolog answers X = edward

family.pl

Page 31: CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

31

Satisfy the subgoals of therule may_steal(X, Y). Match fact thief(john). Search for likes(john, Y).

Y is still uninstantiated. Match the head of the rule

likes(john, X). The subgoal’s variable Y is shared

with the head’s variable X. Both remain uninstantiated.

Satisfy the subgoal likes(X, wine). Match fact likes(mary, wine). Variable X instantiates to mary. Shared Y also instantiates to mary. Subgoal likes(X, wine) satisfied.

All subgoals satisfied. Variable X shared with Y

also instantiates to mary. So John may steal Mary: X = Mary

Rules, cont’dthief(john).

likes(mary, food).likes(mary, wine).

likes(john, X) :- likes(X, wine).

may_steal(X, Y) :- thief(X), likes(X, Y).

?- may_steal(john, X).

What may John steal?

Match rule may_steal(X, Y). X instantiates to john. The question’s variable X is

shared with rule’s variable Y.

thief.pl