CS 152 Programming Language Paradigms February 26 Class
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
Assignment #3 o You are provided with Scheme code that differentiates polynomial expressions using infix notation. (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. o Write two additional procedures evaluate and evaluate-deriv that each takes two parameters, f and x. n n 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. _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 2
Assignment #3, cont’d o The coefficient values can be defined beforehand. n o 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. (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) No * operators. (define a 1) (define b 1) (evaluate f 0) 7 (evaluate f 1) 20 (evaluate-deriv f 0) 3 (evaluate-deriv f 1) 30 n Of course, you should test with other values. SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 3
First-Class Objects o A first-class object is an object that n Can have variables bound to it. o n Can be passed as an argument to a procedure. o n (proc obj) Can be returned as a value by a procedure (i. e. , function). o o (define v obj) (define obj (proc x y)) A Scheme procedure is a first-class object. n A procedure can be bound to a variable. o n A procedure can have a procedure as an argument. o n (define p proc) (proc 1 x y proc 2) A procedure can return a procedure as its return value. o (define proc 2 (proc 1 x y)) SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 4
Example: Passing a Procedure as an Argument (define first-or-second (lambda (pred x y) (if (pred x y) )) o Apply predicate argument pred to arguments x and y. (first-or-second 5 (first-or-second 6 (first-or-second (1 2 3) (first-or-second (1 2 4) SJSU Dept. of Computer Science Spring 2014: February 24 < 5 6) > 5 6) equal? '(1 2 3) (cons 1 '(2 3))) equal? '(1 2 3) (cons 1 '(2 4))) CS 152: Programming Language Paradigms © R. Mak 5
Passing a Procedure: map (define map (lambda (proc lst) (if (null? lst) '() (cons (proc (car lst)) (map proc (cdr lst)))) )) o Apply procedure argument proc to each top-level element of argument lst. (map add 1 '(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) SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 6
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) SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 7
Returning a Procedure: compose (define compose (lambda (f g) (lambda (x) (f (g x))) )) o compose is a procedure that takes two arguments f and g. compose #<procedure> n It returns a procedure that takes a single argument x. (compose sqrt add 1) #<procedure> ((compose sqrt add 1) 8) 3 ((compose add 1 sqrt) 8) 3. 8284271247461903 SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 8
Only a Brief Taste of Lisp o Lisp is a language that epitomizes the functional programming paradigm. n n 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. o You only got a brief taste of the power, flexibility, and usefulness of this language! o 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. n n 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. html SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 9
Programming Paradigms o Different programming paradigms (cultures). n Functional o n Logic o n Lisp, Scheme, ML, Haskell, F# Prolog Object-oriented o C++, C#, Objective C, Java, etc. _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 10
Logic o Logic is the “science of reasoning and proof”. o Studied by several ancient civilizations. n n India, China, Persia Greece o o Established as a formal discipline by Aristotle as a fundamental place in philosophy. Deductive reasoning n n n “Top down” logic. Reach a conclusion from one or more premises. Example: 1. 2. 3. All men are mortal. Aristotle is a man. Therefore, Aristotle is mortal. SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 11
Prolog o Prolog (Programming in Logic) language n A logical and a declarative programming language. n Logical o n Based on deductive reasoning Declarative o Your program specifies what result you want, and the Prolog system figures out how to compute it. _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 12
Objects and Relations o Prolog programs solve problems that involve objects and the relationships between objects. n Example: “John owns the book. ” o o o Relationship has a specific order. n o Objects: John, the book Relationship: ownership John owns the book, but the book doesn’t own John. Query a relationship. n Does John own the book? _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 13
Objects and Relations o Objects and relationships are abstractions. n o 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. ” n n n Perhaps an oversimplified rule. But acceptable as a definition. Sufficient for the query: “Are Charlotte and Emily sisters? ” _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 14
Programming in Prolog o Prolog programming consists of: n n n o A Prolog system enables a computer n n o Declaring some facts about objects and their relationships. Defining some rules about objects and their relationships. Asking questions about objects and their relationships. 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. n Prolog is an inference engine. _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 15
Programming in Prolog o Prolog is an interactive language, like Scheme. o Download and install SWI Prolog on Windows or Mac: http: //www. swi-prolog. org/ n n 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 o o In the interaction window, load the file demo/likes. pl: [swi('demo/likes')]. Prolog tutorial “Learn Prolog Now!” http: //www. learnprolognow. org/ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 16
Facts o Examples of facts, with possible English interpretations: 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. o Prolog syntax: n n 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. SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 17
Questions o A question in Prolog: ? - owns(mary, book). n n o Does Mary own a book? Is it a fact that Mary owns a book? When you ask a question in Prolog: n Prolog searches through its database of facts. n It looks for facts that match the fact in question. o Two facts match if they have the same predicate. n If it finds a match, Prolog answers yes or true. n If there is no matching fact in the database, Prolog answers no or false. _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 18
Questions, cont’d o Consider this database: likes(joe, fish). likes 1. pl likes(joe, mary). likes(mary, book). likes(john, book). o Demo Then we can have this interactive session with Prolog: ? - likes(joe, money). false. ? - likes(mary, joe). false. ? - likes(mary, book). true. ? - king(john, france). ERROR SJSU Dept. of Computer Science Spring 2014: February 24 n n The answer false means only that there was no match in the database. Or that the question is not provable. CS 152: Programming Language Paradigms © R. Mak 19
Variables o Consider this database: o We can ask a question containing a variable. n likes(john, flowers). likes(john, mary). likes 2. pl likes(paul, mary). A variable name starts with a capital letter. ? - likes(john, X). n n What does John like? Prolog will respond: X = flowers n n 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: X = mary. SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 20
Variables, cont’d likes(john, flowers). likes(john, mary). likes(paul, mary). ? - likes(john, X). X = flowers o A variable is instantiated when it stands for an object. n In this example, X is instantiated to flowers n Otherwise, the variable is not instantiated. _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 21
Place Markers o o 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. n Prolog attempts to re-satisfy the question. likes(john, flowers). likes(john, mary). likes(paul, mary). o Another question: Who likes Mary? ? - likes(X, mary). X = john ; X = paul. SJSU Dept. of Computer Science Spring 2014: February 24 First answer. We type ; in reply. No more answers. CS 152: Programming Language Paradigms © R. Mak 22
Conjunctions o Consider this database: likes(mary, likes(john, o likes 3. pl food). wine). mary). Does John like Mary and does Mary like John? ? - likes(john, mary), likes(mary, john). o n The comma , is pronounced “and”. n Prolog must now satisfy the two separate subgoals. Is there anything that Mary and John both like? ? - likes(mary, X), likes(john, X). o Prolog keeps a separate place marker for each goal. SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 23
Backtracking ? - likes(mary, X), likes(john, X). o Suppose Prolog satisfies the first goal of a conjunction. o If then it cannot satisfy the second goal, it backtracks to the first goal and attempts to re-satisfy that goal. n If it succeeds, then it will attempt the second goal again. _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 24
Backtracking, cont’d ? - likes(mary, X), likes(john, X). likes(mary, likes(john, o The first goal succeeds. n o food). wine). mary). X instantiates to food. Next attempt the second goal. n There is no rule likes(John, food). n The second goal fails. _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 25
Backtracking, cont’d ? - likes(mary, X), likes(john, X). o Backtrack to the first goal. n o food). wine). mary). X instantiates to wine. Next attempt the second goal. n o likes(mary, likes(john, Now the second goal succeeds. Mary and John both like wine. _ SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 26
Rules o An example rule in Prolog: likes(john, X) : - likes(X, wine). n n o A rule consists of a head and a body. n n o John likes anyone who likes wine. The symbol : - is pronounced “if”: John likes X if X likes wine. The head is to the left of : The body is to the right of : - Another rule: loves(john, X) : - female(X), likes(X, wine). n John loves women who like wine. SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 27
Rules, cont’d male(john). male(bob). female(mary). likes 4. pl 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. Demo ? - loves(john, Y). Y = mary. SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 28
Rules, cont’d male(albert). male(edward). family. pl o female(alice). female(victoria). parents(edward, victoria, albert). parents(alice, victoria, albert). Match rule sister_of(X, Y). n X instantiates to alice. Y instantiates to edward. SJSU Dept. of Computer Science Spring 2014: February 24 n Match fact parents(edward, victoria, albert). Is Alice the sister of Edward? n Match fact female(alice). n ? - sister_of(alice, edward). o n parents(alice, victoria, albert). o Mom instantiates to victoria. o Dad instantiates to albert. sister_of(X, Y) : female(X), parents(X, Mom, Dad), parents(Y, Mom, Dad). o Satisfy the subgoals of the rule sister_of(X, Y). o All the subgoals succeeded, therefore Prolog answers true _ CS 152: Programming Language Paradigms © R. Mak 29
Rules, cont’d male(albert). male(edward). family. pl o female(alice). female(victoria). Satisfy the subgoals of the rule sister_of(X, Y). n n parents(alice, victoria, albert). o Mom instantiates to victoria. o Dad instantiates to albert. parents(edward, victoria, albert). parents(alice, victoria, albert). sister_of(X, Y) : female(X), parents(X, Mom, Dad), parents(Y, Mom, Dad). n n Match rule sister_of(X, Y). n n 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. SJSU Dept. of Computer Science Spring 2014: February 24 Variable Y is still uninstantiated. Match fact parents(edward, victoria, albert). o Y instantiates to edward. ? - sister_of(alice, X). o Match fact female(alice). Match fact o All the subgoals succeeded. n Y is shared with the question’s X o n X also instantiates to edward. Prolog answers X = edward CS 152: Programming Language Paradigms © R. Mak 30
Rules, cont’d thief(john). thief. pl likes(mary, food). likes(mary, wine). o n n likes(john, X) : likes(X, wine). Match fact thief(john). Search for likes(john, Y). o n may_steal(X, Y) : thief(X), likes(X, Y). o Satisfy the subgoals of the rule may_steal(X, Y). Match the head of the rule likes(john, X). o What may John steal? o n ? - may_steal(john, X). o Match rule may_steal(X, Y). n n X instantiates to john. The question’s variable X is o shared with rule’s variable Y. SJSU Dept. of Computer Science Spring 2014: February 24 The subgoal’s variable Y is shared with the head’s variable X. Both remain uninstantiated. Satisfy the subgoal likes(X, wine). o o Y is still uninstantiated. o o 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. CS 152: Programming Language Paradigms 31 n© R. Mak So John may steal Mary: X = Mary n
- Slides: 31