Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SICP: Inside & Beyond

SICP: Inside & Beyond

A brief talk about SICP made on SICPxShanghai 2015.

James Deng

August 04, 2015
Tweet

More Decks by James Deng

Other Decks in Programming

Transcript

  1. Alan J Perlis If art interprets our dreams, the computer

    executes them in the guise of programs!
  2. SICP as The Book • Structure and Interpretation of Computer

    Programs • Purple Book • Wizard Book • THE BOOK • 《计算机程序的构造和解释》
  3. SICP as The Book • The very first SICP edition.

    • a.k.a AITR 735 • Draft on 1983.07.31
  4. 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
  5. 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))))
  6. 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))))
  7. 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))))
  8. 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))))
  9. 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)))) )
  10. 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>)
  11. 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))))
  12. 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)))
  13. 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.
  14. 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.
  15. Chap 1. Extract Common Pattern Don’t Repeat Yourself! Extract the

    common pattern. Using higher order procedure to implement the detail for each case.
  16. 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))))
  17. 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))))
  18. 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!
  19. 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)))
  20. 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)))
  21. 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)))
  22. 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+) ......
  23. Chap 1. Extract Common Pattern (Beyond) The Functional or Applicative

    Aspects Describing computation in terms of the definition and application of functions or procedures.
  24. 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.
  25. 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)
  26. 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
  27. 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
  28. 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))]))
  29. 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
  30. 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.
  31. 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)))
  32. 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.
  33. 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.
  34. Chap 3. Environment • A: Evaluate the lambda-form we got:

    (λ (init) (λ (inc) (set! init (+ init inc)) init)) code env top_env
  35. Chap 3. Environment • A: Assign the closure to make-counter.

    make-counter value pointer * the-code * code env nil
  36. 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.
  37. 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
  38. 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
  39. 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)
  40. 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)
  41. Chap 3. Environment Closure A data structure combine both the

    code it will call, and the environment when it be called.
  42. 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.
  43. 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.