מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Fall 2008Programming Development Techniques 1 Topic 10 Example: Symbolic Differentiation Section October 2008.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
6.001 SICP 1/ : Structure and Interpretation of Computer Programs Symbols Example of using symbols Differentiation.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1/38. 2/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Functional Programming: Lisp MacLennan Chapter 10.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Edited by Original material by Eric Grimson
Tagged Data Tag: a symbol in a data structure that identifies its type
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001: Structure and Interpretation of Computer Programs
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Original material by Eric Grimson
6.001 SICP Data abstractions
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Lecture #9 מבוא מורחב.
Lecture 13 - Assignment and the environments model Chapter 3
Data Mutation Primitive and compound data mutators set! for names
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 25: The Metacircular Evaluator Eval Apply
Dispatch on Type (one-less x) => x-1 if x is a <number>
Presentation transcript:

מבוא מורחב 1 Lecture #9

מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors none operations: symbol?; type: anytype -> boolean (symbol? (quote alpha)) ==> #t eq? ; discuss in a minute

מבוא מורחב 3 Symbol: printed representation (lambda (x) (* x x)) eval lambda-rule A compound proc that squares its argument #[compound-...] print (quote beta) eval quote-rule symbol print beta

מבוא מורחב 4 Symbols are ordinary values (list 1 2) ==> (1 2) (list (quote delta) (quote gamma)) ==> (delta gamma) symbol gamma symbol delta

מבוא מורחב 5 A useful property of the quote special form (list (quote delta) (quote delta)) symbol delta Two quote expressions with the same name return the same object

מבוא מורחב 6 The operation eq? tests for the same object a primitive procedure returns #t if its two arguments are the same object very fast (eq? (quote eps) (quote eps)) ==> #t (eq? (quote delta) (quote eps)) ==> #f For those who are interested: ; eq?: EQtype, EQtype ==> boolean ; EQtype = any type except number or string One should therefore use = for equality of numbers, not eq?

מבוא מורחב 7 Summary so far symbol is a primitive type the special form quote is its constructor quoting the same name always returns the same object the primitive procedure eq? tests for sameness

מבוא מורחב 8 Generalization: quoting other expressions Expression: Rewrites to: 1. (quote (a b)) (list (quote a) (quote b)) 2. (quote ()) nil 3. (quote ) (quote (1 (b c))) rewrites to (list (quote 1) (quote (b c))) rewrites to (list 1 (quote (b c))) rewrites to (list 1 (list (quote b) (quote c))) ==> (1 (b c)) In general, (quote משהו ) evaluates to משהו

מבוא מורחב 9 Shorthand: the single quote mark 'a is shorthand for (quote a) '(1 2)(quote (1 2))

מבוא מורחב 10 Your turn: what does evaluating these print out? (define x 20) (+ x 3) ==> '(+ x 3) ==> (list (quote +) x '3) ==> (list '+ x 3) ==> (list + x 3) ==> 23 (+ x 3) (+ 20 3) ([procedure #…] 20 3)

מבוא מורחב 11 Short summary (quote DATUM) evaluates to DATUM for any DATUM 'DATUM is the same as (quote DATUM) quoting self-evaluating expressions is worthless

מבוא מורחב 12 Manipulating lists and trees of symbols

מבוא מורחב 13 Example 1: memq (define (memq item x) (cond ((null? x) #f) ((eq? item (car x)) x) (else (memq item (cdr x))))) (memq 'a '(a b c)) => (memq 'b '(a b c)) => (memq 'a '(b c d)) => (a b c) (b c) #f#f

מבוא מורחב 14 Example 2: rember (define (rember item x) (cond ((null? x) ‘()) ((eq? item (car x))(cdr x)) (else (cons (car x) (rember item (cdr x)))))) (rember 'a '(a b c a)) => (rember 'b '(a b c)) => (rember 'a '(b c d)) => (b c a) (b c) (b c d)

15 Example 3: rember* (define (rember* a x) (cond ((null? x) ‘()) ((atom? (car x)) (cond ((eq? (car x) a) (rember* a (cdr x))) (else (cons (car x) (rember* a (cdr x)))))) (else (cons (rember* a (car x)) (rember* a (cdr x)))))) (rember* 'a '(a b)) => (rember* 'a '(a (b a) c (a)) => (b) ((b) c ())

מבוא מורחב 16 A bigger example: symbolic diffrentiation

מבוא מורחב 17 Symbolic differentiation (deriv ) ==> (deriv '(+ x 3) 'x) ==> 1 (deriv '(+ (* x y) 4) 'x) ==> y (deriv '(* x x) 'x) ==> (+ x x) (+ x (+ y 3))x+ y + 3 (* 5 y)5y5y XX (+ x 3)X + 3 RepresentationAlgebraic expression

מבוא מורחב 18 Why? Example of: trees how to use the symbol type symbolic manipulation How? 1. how to get started 2. a direct implementation 3. a better implementation

מבוא מורחב How to get started Analyze the problem precisely deriv constant dx = 0 deriv variable dx = 1 if variable is the same as x = 0 otherwise deriv (e1+e2) dx = deriv e1 dx + deriv e2 dx deriv (e1*e2) dx = e1 * (deriv e2 dx) + e2 * (deriv e1 dx) Observe: e1 and e2 might be complex subexpressions derivative of (e1+e2) formed from deriv e1 and deriv e2 a tree problem Base Recursive

מבוא מורחב 20 Type of the data will guide implementation legal expressions x(+ x y) 2(* 2 x)(+ (* x y) 3) illegal expressions *(3 5 +)(+ x y z) ()(3)(* x) ; Expr = SimpleExpr | CompoundExpr ; SimpleExpr = number | symbol ; CompoundExpr = a list of three elements where the first element is either + or * ; = pair >>

מבוא מורחב A direct implementation Overall plan: one branch for each subpart of the type (define deriv (lambda (expr var) (if (simple-expr? expr) ))) To implement simple-expr? look at the type CompoundExpr is a pair nothing inside SimpleExpr is a pair therefore (define simple-expr? (lambda (e) (not (pair? e))))

מבוא מורחב 22 Simple expressions One branch for each subpart of the type (define deriv (lambda (expr var) (if (simple-expr? expr) (if (number? expr) ) ))) Implement each branch by looking at the math 0 (if (eq? expr var) 1 0)

מבוא מורחב 23 Compound expressions One branch for each subpart of the type (define deriv (lambda (expr var) (if (simple-expr? expr) (if (number? expr) 0 (if (eq? expr var) 1 0)) (if (eq? (car expr) '+) ) )))

מבוא מורחב 24 Sum expressions To implement the sum branch, look at the math (define deriv (lambda (expr var) (if (simple-expr? expr) (if (number? expr) 0 (if (eq? expr var) 1 0)) (if (eq? (car expr) '+) (list '+ (deriv (cadr expr) var) (deriv (caddr expr) var)) ) ))) (deriv '(+ x y) 'x) ==> (+ 1 0) (a list!)

מבוא מורחב 25 The direct implementation works, but... Programs always change after initial design Hard to read Hard to extend safely to new operators or simple exprs Can't change representation of expressions Source of the problems: nested if expressions explicit access to and construction of lists few useful names within the function to guide reader

מבוא מורחב A better implementation 1. Use cond instead of nested if expressions 2. Use data abstraction To use cond : write a predicate that collects all tests to get to a branch: (define sum-expr? (lambda (e) (and (pair? e) (eq? (car e) '+)))) ; type: Expr -> boolean do this for every branch: (define variable? (lambda (e) (and (not (pair? e)) (symbol? e))))

מבוא מורחב 27 Use data abstractions To eliminate dependence on the representation: (define make-sum (lambda (e1 e2) (list '+ e1 e2)) (define addend (lambda (sum) (cadr sum)))

מבוא מורחב 28 A better implementation (define deriv (lambda (expr var) (cond ((number? expr) 0) ((variable? expr) (if (eq? expr var) 1 0)) ((sum-expr? expr) (make-sum (deriv (addend expr) var) (deriv (augend expr) var))) ((product-expr? expr) ) (else (error "unknown expression type" expr)) ))

מבוא מורחב 29 Deriv: Reduction problem (deriv '(+ x 3) 'x) ==> (+ 1 0) (deriv '(* x y) 'x) ==> (+ (* x 0) (* 1 y)) (deriv '(* (* x y) (+ x 3)) 'x) ==> (+ (* (* x y) (+ 1 0)) (* (+ (* x 0) (* 1 y)) (+ x 3)))

מבוא מורחב 30 Changes are isolated (deriv '(+ x y) 'x) ==> (+ 1 0) (a list!) (define (make-sum a1 a2) (cond ((=number? a1 0) a2) ((=number? a2 0) a1) ((and (number? a1) (number? a2)) (+ a1 a2)) (else (list '+ a1 a2)))) (define (=number? exp num) (and (number? exp) (= exp num))) (deriv '(+ x y) 'x) ==> 1 (deriv '(* x y) 'x) ==> y

מבוא מורחב 31 Guidelines Carefully think about problem and type of data before starting Structure your function around the branches of the type Avoid nested if expressions Use data abstractions for better modifiability Use good names for better readability