Share
Embed
SICP: Inside & Beyond
A brief talk about SICP made on SICPxShanghai 2015.
James Deng
August 04, 2015
Transcript
48 slides
-
Alan J Perlis If art interprets our dreams, the computer
executes them in the guise of programs! -
SICP as The Book • Structure and Interpretation of Computer
Programs • Purple Book • Wizard Book • THE BOOK • 《计算机程序的构造和解释》 -
SICP as The Book • The very first SICP edition.
• a.k.a AITR 735 • Draft on 1983.07.31 -
People in SICP • ༝$Z%'FDUၛభ൞۱$ӱჴđքྏӱ֥ଖུڬቔႨ ۴Чࣼ҂ିൌགྷđၹູاྟᆴఖѩ҂఼௧၂۱ਙ֥ଖུі ղൔᆴll • $Z%'FDU4JEF&GGFDUđࠧڬቔႨb •
llགྷᄝಞૌࡌקႵਆ۱ӱჴđ#FO#JUEJEEMFބ"MZTTB 1)BDLFS ૌᆞᄝٳљ৫ֹഡ࠹ᆃ၂گᄖ༢֥ऎุіൕྙൔb • "MZTTB1)BDLFSđ"-JTQ)BDLFS ref: http://sicp.iijlab.net/people.html -
Inside: • ӱсྶཿ֤ିܔ܂ದૌᄇđغֹಀ܂࠹ෘࠏᆳྛb • ቋࠎЧ֥ҋਘѩ҂൞หקӱഡ࠹ე֥ეمđ҂൞Ⴕི࠹ ෘଖᇕۿି֥ెෘمđ္҂൞ෘم֥ඔ࿐ٳ༅ࠇᆀ࠹ෘ֥ ЧᇉࠎԤđط൞၂ུିܔႨႿ॥ᇅնೈࡱ༢֥ᇆ৯گᄖ ྟඌb • ႋھޓႵϜֹಀྩڿ၂۱ӱđൈႻିЌӻჰটቔᆀ֥
ࣚപކڄ۬b • o࠹ෘࠏ॓࿐pѩ҂൞၂ᇕ॓࿐đطఃᇗေྟ္ა࠹ෘࠏЧദ ѩնܱ༢b -
Chap 1. Extract Common Pattern • Q: Write a program
to compute sum a to b. • A: That’s trivial. (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b)))) -
Chap 1. Extract Common Pattern • Q: Write a program
to compute the sum of n2 for all n ∈ [a, b]. • A: Trivial. (define (sum-squares a b) (if (> a b) 0 (+ (* a a) (sum-squares (+ a 1) b)))) -
Chap 1. Extract Common Pattern • Q: Write a program
to compute the sequence: • A: Not difficult. (define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b)))) -
Chap 1. Extract Common Pattern • Q: Can you figure
out the pattern? • A: Of course, it lies there. (define (<name> a b) (if (> a b) 0 (+ (<term> a) (<name> (<next> a) b)))) -
Chap 1. Extract Common Pattern • Q: <term> and <next>
are not bounded. • A: We can parameterize them. (define (<name> a b (if (> a b) 0 (+ (<term> a) (<name> (<next> a) b)))) ) -
Chap 1. Extract Common Pattern • Q: <term> and <next>
are not bounded. • A: We can parameterize them. (define (<name> a b (if (> a b) 0 (+ (<term> a) (<name> (<next> a) b)))) <term> <next>) -
Chap 1. Extract Common Pattern • Q: It’s a general
sum in some sense? • A: Yes. We could call it gen-sum. (define (gen-sum a b term next) (if (> a b) 0 (+ (term a) (gen-sum (next a) b)))) -
Chap 1. Extract Common Pattern • Q: How could you
express π-sum using new pattern. • A: Even easy. π-sum is: (gen-sum a b (λ (x) (/ 1.0 (* x (+ x 2)))) (λ (x) (+ x 4))) -
Chap 1. Extract Common Pattern • Q: Why it’s better?
• A: Because it abstract out the common pattern. Using higher-order procedures to implement the details. • Q: Dose it any good? • A: It gets more reusable. You can perform different action by call it with different procedures. -
Chap 1. Extract Common Pattern • Q: What’s a higher-order
procedure? • A: A higher-order procedure, or, higher-order function, or functor, or functional, is a function which: • either takes at least one function, • or returns a function. -
Chap 1. Extract Common Pattern Don’t Repeat Yourself! Extract the
common pattern. Using higher order procedure to implement the detail for each case. -
Chap 1. Extract Common Pattern (Beyond) • Q: Can you
be more abstract? • A: Of course. (define (gen-sum a b term next) (if (<predicate> a b) 0 (<proc> (term a) (gen-sum (next a) b)))) -
Chap 1. Extract Common Pattern (Beyond) • Q: Then <predicate>
and <proc> are free variables! How could we avoid that? (define (gen-sum a b term next) (if (<predicate> a b) 0 (<proc> (term a) (gen-sum (next a) b)))) -
Chap 1. Extract Common Pattern (Beyond) • Q: Then <predicate>
and <proc> are free variables! How could we avoid that? (define (gen-sum a b term next predicate proc) (if (predicate a b) 0 (proc (term a) (gen-sum (next a) b)))) • A: By parameterize them! -
Chap 1. Extract Common Pattern (Beyond) • Q: We got
a long parameter list! • A: Currying takes care of it! (define (sum-builder predicate proc) (λ (<para-list>) (gen-sum a b term next predicate proc))) -
Chap 1. Extract Common Pattern (Beyond) • Q: What is
the type of sum-builder? • A: Currying takes care of it! (define (sum-builder predicate proc) (λ (<para-list>) (gen-sum a b term next predicate proc))) -
Chap 1. Extract Common Pattern (Beyond) • Q: What do
we have to fill in the <para-list>? • A: The unbound variables! (define (sum-builder predicate proc) (λ (a b term next) (gen-sum a b term next predicate proc))) -
Chap 1. Extract Common Pattern (Beyond) • Q: sum-builder is
a functional builder in some sense? • A: It seems so. (define f1 (sum-builder (lambda (x y) #t) +) (define f2 (sum-builder < +)) (define f3 (sum-builder vector-cmpare vector+) ...... -
Chap 1. Extract Common Pattern (Beyond) The Functional or Applicative
Aspects Describing computation in terms of the definition and application of functions or procedures. -
Chap 3. Environment • Q: What’s the problem with assignment?
• A: Substitution is not suitable for this situation. Which means substitution the value of parameters first, then evaluate it. -
Chap 3. Environment • Q: What could we do? •
A: Introduced the environment. A environment is a map which maps a syntax element Ide to a locale L. And we have another map called save which maps a locale L to a pair (E, T). Identifier Locale Value Save Environment (Lvalue) (Rvalue) -
Chap 3. Environment • Q: Show me more on implementation.
• A: A binding associates a name with a value. and a frame is a table of bindings and a pointer to another frame. identifier value identifier value identifier value identifier value pointer identifier value identifier value pointer -
Chap 3. Environment • Q: So what is an environment.
• A: An environment is a sequence of frames. identifier value identifier value identifier value pointer identifier value identifier value pointer env0 env1 -
Chap 3. Environment • Q: How do we perform a
variable lookup? • A: Describe in Scheme: (define (lookup var env) (cond [(null? env) (error)] [(has-binding? var (get-current-frame env)) (get-value var (get-current-frame env))] [else (lookup var (father-env env))])) -
Chap 3. Environment • Q: How to evaluate the scheme
code? • Q: And how about this? (define (make-counter init) (λ (inc) (set! init (+ init inc)) init)) (define my-counter (make-counter 10)) (my-counter 2) ;=> 12 (my-counter 2) ;=> 14 -
Chap 3. Environment • Q: Dose the make-counter acts like
a function builder? • A: Yes. We know function builder already. It build a function, then you use that function. Of course, you can build more than one function though the procedure make-counter. -
Chap 3. Environment • A: Firstly, expand the syntax define.
• A: we got: (define (make-counter init) (λ (inc) (set! init (+ init inc)) init)) (define make-counter (λ (init) (λ (inc) (set! init (+ init inc)) init))) -
Chap 3. Environment • A:Which means: 1. first, evaluate the
lambda form 2. then, make counter refer to the evaluation result • A: Let’s try evaluate the lambda form firstly. -
Chap 3. Environment • LAMBDA: the ultimate closure. • lambda,
or, even better, make-closure. • What’s a closure? • A closure is a data structure. • Consists of a pointer to the code. • And a pointer to the environment where the lambda be called. -
Chap 3. Environment • A: Evaluate the lambda-form we got:
(λ (init) (λ (inc) (set! init (+ init inc)) init)) code env top_env -
Chap 3. Environment • A: Assign the closure to make-counter.
make-counter value pointer * the-code * code env nil -
Chap 3. Environment • Q: How do we perform a
function apply? • A: Evaluate the value of parameter first, save these bindings in a new environment, then evaluate the code under the new environment. -
Chap 3. Environment • A: Apply, (define my-counter (make-counter 10)).
make-counter value pointer * the-code * code env nil (λ (inc) (set! init (+ init inc)) init) code env init 10 pointer my-counter value -
Chap 3. Environment • A: Go further, (my-counter 2). (λ
(inc) (set! init (+ init inc)) init) code env init 10 pointer inc 2 pointer Prepare for Parameter -
Chap 3. Environment • A: Go further, (my-counter 2). (λ
(inc) (set! init (+ init inc)) init) code env init 12 pointer inc 2 pointer Evaluate the body ((set! init (+ init inc) init) -
Chap 3. Environment • A: You know it all. (λ
(inc) (set! init (+ init inc)) init) code env init 12 pointer inc 2 pointer Evaluate the body ((set! init (+ init inc) init) -
Chap 3. Environment Closure A data structure combine both the
code it will call, and the environment when it be called. -
Beyond: What to learn? • Theories of Programming Languages •
John C.Raynolds • A wisdom, clear, and gentle textbook on fundamental concept of programming languages with a wide range of topics include denotational semantics and the concurrency. -
Beyond: What to learn? • Lambda-Calculus, Combinators, and Functional Programming.
• G. Revesz, CTiTCS series. • About lambda calculus and the graph reduce theory. A gentle and beginner friendly textbook. Better finished the exercise.