lessons from 6.001 - 6.037 - structure and interpretation...

73
Lessons from 6.001 6.037 - Structure and Interpretation of Computer Programs Mike Phillips <mpp> Massachusetts Institute of Technology Lecture 8 Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 1 / 28

Upload: others

Post on 27-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Lessons from 6.0016.037 - Structure and Interpretation of Computer Programs

Mike Phillips <mpp>

Massachusetts Institute of Technology

Lecture 8

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 1 / 28

Page 2: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Contexualizing 6.001

We said at the start that this wasn’t a class in Scheme

You’re probably never again going to code in SchemeInstead, this is a class in ComputationHow do the concepts from 6.001 apply elsewhere?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 2 / 28

Page 3: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Contexualizing 6.001

We said at the start that this wasn’t a class in SchemeYou’re probably never again going to code in Scheme

Instead, this is a class in ComputationHow do the concepts from 6.001 apply elsewhere?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 2 / 28

Page 4: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Contexualizing 6.001

We said at the start that this wasn’t a class in SchemeYou’re probably never again going to code in SchemeInstead, this is a class in Computation

How do the concepts from 6.001 apply elsewhere?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 2 / 28

Page 5: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Contexualizing 6.001

We said at the start that this wasn’t a class in SchemeYou’re probably never again going to code in SchemeInstead, this is a class in ComputationHow do the concepts from 6.001 apply elsewhere?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 2 / 28

Page 6: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Syllabus and key ideas

Procedural and data abstractionConventional interfaces & programming paradigms

Type systemsStreamsObject-oriented programming

Metalinguistic abstractionCreating new languagesEvaluators

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 3 / 28

Page 7: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Static scoping is now standard

Scheme stole static scoping (aka lexical scoping) from ALGOL

Most languages now are statically scoped, if only by blockEnvironment model still describes how bindings work!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 4 / 28

Page 8: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Static scoping is now standard

Scheme stole static scoping (aka lexical scoping) from ALGOLMost languages now are statically scoped, if only by block

Environment model still describes how bindings work!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 4 / 28

Page 9: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Static scoping is now standard

Scheme stole static scoping (aka lexical scoping) from ALGOLMost languages now are statically scoped, if only by blockEnvironment model still describes how bindings work!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 4 / 28

Page 10: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Higher-order functions

Many modern languages support first-class functions:Javascript, Perl, Python, Ruby, MATLAB, Mathematica, C++11,C#, Clojure

Many even call anonymous functions lambdas

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 5 / 28

Page 11: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Higher-order functions

Many modern languages support first-class functions:Javascript, Perl, Python, Ruby, MATLAB, Mathematica, C++11,C#, ClojureMany even call anonymous functions lambdas

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 5 / 28

Page 12: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Closures

Static scoping + first-class functions = closures

Great for data hiding. . . access mediation. . . iterators. . . continuation passing style for flow control. . . laziness or other delayed evaluation

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 6 / 28

Page 13: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Closures

Static scoping + first-class functions = closuresGreat for data hiding

. . . access mediation

. . . iterators

. . . continuation passing style for flow control

. . . laziness or other delayed evaluation

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 6 / 28

Page 14: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Closures

Static scoping + first-class functions = closuresGreat for data hiding. . . access mediation

. . . iterators

. . . continuation passing style for flow control

. . . laziness or other delayed evaluation

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 6 / 28

Page 15: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Closures

Static scoping + first-class functions = closuresGreat for data hiding. . . access mediation. . . iterators

. . . continuation passing style for flow control

. . . laziness or other delayed evaluation

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 6 / 28

Page 16: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Closures

Static scoping + first-class functions = closuresGreat for data hiding. . . access mediation. . . iterators. . . continuation passing style for flow control

. . . laziness or other delayed evaluation

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 6 / 28

Page 17: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Closures

Static scoping + first-class functions = closuresGreat for data hiding. . . access mediation. . . iterators. . . continuation passing style for flow control. . . laziness or other delayed evaluation

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 6 / 28

Page 18: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Closures

Static scoping + first-class functions = closuresGreat for data hiding. . . access mediation. . . iterators. . . continuation passing style for flow control. . . laziness or other delayed evaluation

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 6 / 28

Page 19: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

List operations with anonymous functions

Other languages have filter, map, reduce. . .

Map. . . Reduce?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 7 / 28

Page 20: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

List operations with anonymous functions

Other languages have filter, map, reduce. . .Map. . . Reduce?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 7 / 28

Page 21: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

MapReduce

Massively parallel architecture for handling Big Data™

Purely functional code is easy to parallelize – no read/writecontentionIdea based on every call to func in (map func lst) being ableto be called in parallel. . . then also fed into fold-right in parallel

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 8 / 28

Page 22: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

MapReduce

Massively parallel architecture for handling Big Data™Purely functional code is easy to parallelize – no read/writecontention

Idea based on every call to func in (map func lst) being ableto be called in parallel. . . then also fed into fold-right in parallel

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 8 / 28

Page 23: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

MapReduce

Massively parallel architecture for handling Big Data™Purely functional code is easy to parallelize – no read/writecontentionIdea based on every call to func in (map func lst) being ableto be called in parallel

. . . then also fed into fold-right in parallel

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 8 / 28

Page 24: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

MapReduce

Massively parallel architecture for handling Big Data™Purely functional code is easy to parallelize – no read/writecontentionIdea based on every call to func in (map func lst) being ableto be called in parallel. . . then also fed into fold-right in parallel

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 8 / 28

Page 25: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

MapReduce

Congratulations, you already know how to write forHadoop/MapReduce clusters

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 9 / 28

Page 26: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

You know how to write evaluators

What good is writing an evaluator?

Allows you to move the level of abstractionWriting code in Python but need to generate HTML forms?Requires programmer have HTML knowledge..or write a Domain-Specific Language (DSL) to generate it for you

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 10 / 28

Page 27: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

You know how to write evaluators

What good is writing an evaluator?Allows you to move the level of abstraction

Writing code in Python but need to generate HTML forms?Requires programmer have HTML knowledge..or write a Domain-Specific Language (DSL) to generate it for you

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 10 / 28

Page 28: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

You know how to write evaluators

What good is writing an evaluator?Allows you to move the level of abstractionWriting code in Python but need to generate HTML forms?

Requires programmer have HTML knowledge..or write a Domain-Specific Language (DSL) to generate it for you

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 10 / 28

Page 29: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

You know how to write evaluators

What good is writing an evaluator?Allows you to move the level of abstractionWriting code in Python but need to generate HTML forms?Requires programmer have HTML knowledge

..or write a Domain-Specific Language (DSL) to generate it for you

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 10 / 28

Page 30: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

You know how to write evaluators

What good is writing an evaluator?Allows you to move the level of abstractionWriting code in Python but need to generate HTML forms?Requires programmer have HTML knowledge..or write a Domain-Specific Language (DSL) to generate it for you

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 10 / 28

Page 31: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

External DSLs

Read and parse a string (syntax)Apply arbitrary rules for meaning (semantics)

We know how to do the latter; there are tools for the former

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 11 / 28

Page 32: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

External DSLs

Read and parse a string (syntax)Apply arbitrary rules for meaning (semantics)We know how to do the latter; there are tools for the former

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 11 / 28

Page 33: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Internal DSLs

Can also just write clever function namesLet your language do the parsing

Constrains you to the syntax rules of your language“For when you want to write code in one language, and get yourerrors in another!”

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 12 / 28

Page 34: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Internal DSLs

Can also just write clever function namesLet your language do the parsingConstrains you to the syntax rules of your language

“For when you want to write code in one language, and get yourerrors in another!”

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 12 / 28

Page 35: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Internal DSLs

Can also just write clever function namesLet your language do the parsingConstrains you to the syntax rules of your language“For when you want to write code in one language, and get yourerrors in another!”

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 12 / 28

Page 36: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Data as code, and vice versa

Scheme is useful because code and data are just a quote awayGenetic Programming “evolves” programs by mutating syntax –doable because syntax is simpleLisp/Scheme key in early Artificial Intelligence in 1980sUseful in deduction languages – which led to PROLOG

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 13 / 28

Page 37: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Data as code now

Computers use a language where data is code all of the time

Assembly language is just bytesData it works on is just bytes

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 14 / 28

Page 38: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Data as code now

Computers use a language where data is code all of the timeAssembly language is just bytes

Data it works on is just bytes

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 14 / 28

Page 39: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Data as code now

Computers use a language where data is code all of the timeAssembly language is just bytesData it works on is just bytes

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 14 / 28

Page 40: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Some random bytes

BF FF FF FF FF 41 803C 08 00 75 F9 C3 90

BF FFFFFFFF Store -1 in variable C41 Add 1 to C80 3C 08 00 Compare memory at (A + C) to 075 F9 If that is not 0, go back 6 bytesC3 Return90 Do nothing

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 15 / 28

Page 41: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Some random bytes

BF FF FF FF FF 41 803C 08 00 75 F9 C3 90

BF FFFFFFFF Store -1 in variable C41 Add 1 to C80 3C 08 00 Compare memory at (A + C) to 075 F9 If that is not 0, go back 6 bytesC3 Return90 Do nothing

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 15 / 28

Page 42: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

When data should not be code

The most common security vulnerabilities are when computersare convinced that data is actually code

a.k.a “Buffer overflows”Equivalent to making Scheme run an arbitrary function from insidem-eval

“Jumping out of the system”

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 16 / 28

Page 43: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

When data should not be code

The most common security vulnerabilities are when computersare convinced that data is actually codea.k.a “Buffer overflows”Equivalent to making Scheme run an arbitrary function from insidem-eval

“Jumping out of the system”

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 16 / 28

Page 44: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

When data should not be code

The most common security vulnerabilities are when computersare convinced that data is actually codea.k.a “Buffer overflows”Equivalent to making Scheme run an arbitrary function from insidem-eval

“Jumping out of the system”

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 16 / 28

Page 45: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Aside: Gödel, Escher, Bach

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 17 / 28

Page 46: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Evaluators as translators

Change our evaluator to work in two phases; one parses theexpression and returns a Scheme lambdaThe second phase just applies that lambda with a startingenvironment

The first phase is a translator from one language to anotherNo reason the language we translate to has to be scheme. . .. . . how about assembly?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 18 / 28

Page 47: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Evaluators as translators

Change our evaluator to work in two phases; one parses theexpression and returns a Scheme lambdaThe second phase just applies that lambda with a startingenvironmentThe first phase is a translator from one language to another

No reason the language we translate to has to be scheme. . .. . . how about assembly?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 18 / 28

Page 48: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Evaluators as translators

Change our evaluator to work in two phases; one parses theexpression and returns a Scheme lambdaThe second phase just applies that lambda with a startingenvironmentThe first phase is a translator from one language to anotherNo reason the language we translate to has to be scheme. . .

. . . how about assembly?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 18 / 28

Page 49: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Evaluators as translators

Change our evaluator to work in two phases; one parses theexpression and returns a Scheme lambdaThe second phase just applies that lambda with a startingenvironmentThe first phase is a translator from one language to anotherNo reason the language we translate to has to be scheme. . .. . . how about assembly?

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 18 / 28

Page 50: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Lowering the abstraction barrier

Scheme

Interpreter(m-eval)

Scheme

Interpreter(Racket)

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 19 / 28

Page 51: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Lowering the abstraction barrier

Scheme

Interpreter(Racket)

Scheme

Assembly

Compiler

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 20 / 28

Page 52: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Lowering the abstraction barrier

Scheme

Interpreter(Racket)

Scheme

Assembly

Compiler

Scheme

Interpreter(m-eval)

Scheme

Interpreter(m-eval)

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 21 / 28

Page 53: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Lowering the abstraction barrier

Scheme

Assembly

Compiler

Scheme

Interpreter(m-eval)

Scheme

Assembly

Compiler

Scheme

Assembly

Compiler

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 22 / 28

Page 54: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Transforming from any language to an language

Now have interpreter in assembly, for Scheme

How simple a language can we build on?Are there functions which can be computed in Java but notScheme?Church-Turing thesis: Turing Machines!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 23 / 28

Page 55: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Transforming from any language to an language

Now have interpreter in assembly, for SchemeHow simple a language can we build on?

Are there functions which can be computed in Java but notScheme?Church-Turing thesis: Turing Machines!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 23 / 28

Page 56: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Transforming from any language to an language

Now have interpreter in assembly, for SchemeHow simple a language can we build on?Are there functions which can be computed in Java but notScheme?

Church-Turing thesis: Turing Machines!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 23 / 28

Page 57: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Transforming from any language to an language

Now have interpreter in assembly, for SchemeHow simple a language can we build on?Are there functions which can be computed in Java but notScheme?Church-Turing thesis: Turing Machines!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 23 / 28

Page 58: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Church-Turing thesis

If a function can be computed by an algorithm, then it must alsobe computable by a Turing Machine

And vice-versaThus Java, Scheme, Python, etc, are all equivalent in thefunctions they can compute

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 24 / 28

Page 59: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Church-Turing thesis

If a function can be computed by an algorithm, then it must alsobe computable by a Turing MachineAnd vice-versa

Thus Java, Scheme, Python, etc, are all equivalent in thefunctions they can compute

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 24 / 28

Page 60: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Church-Turing thesis

If a function can be computed by an algorithm, then it must alsobe computable by a Turing MachineAnd vice-versaThus Java, Scheme, Python, etc, are all equivalent in thefunctions they can compute

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 24 / 28

Page 61: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Language equivalence

So if all languages are fundamentally equivalent

. . . so what do we like about Scheme?Lexical scoping, procedures as first-class objects, garbagecollection, eval and apply, asynchronous event handling. . .We have just such a language: Javascript

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 25 / 28

Page 62: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Language equivalence

So if all languages are fundamentally equivalent. . . so what do we like about Scheme?

Lexical scoping, procedures as first-class objects, garbagecollection, eval and apply, asynchronous event handling. . .We have just such a language: Javascript

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 25 / 28

Page 63: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Language equivalence

So if all languages are fundamentally equivalent. . . so what do we like about Scheme?Lexical scoping, procedures as first-class objects, garbagecollection, eval and apply, asynchronous event handling. . .

We have just such a language: Javascript

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 25 / 28

Page 64: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Language equivalence

So if all languages are fundamentally equivalent. . . so what do we like about Scheme?Lexical scoping, procedures as first-class objects, garbagecollection, eval and apply, asynchronous event handling. . .We have just such a language:

Javascript

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 25 / 28

Page 65: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Language equivalence

So if all languages are fundamentally equivalent. . . so what do we like about Scheme?Lexical scoping, procedures as first-class objects, garbagecollection, eval and apply, asynchronous event handling. . .We have just such a language: Javascript

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 25 / 28

Page 66: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Javascript

Brendan Eich was hired by Netscape in 1995 with the promise of“doing Scheme for the browser”

But Java was also being implemented for the browserSo if there was a second language, it should “look like Java”So syntax closer to Java, but semantics stolen from Scheme. . . JavaScript!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 26 / 28

Page 67: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Javascript

Brendan Eich was hired by Netscape in 1995 with the promise of“doing Scheme for the browser”But Java was also being implemented for the browser

So if there was a second language, it should “look like Java”So syntax closer to Java, but semantics stolen from Scheme. . . JavaScript!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 26 / 28

Page 68: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Javascript

Brendan Eich was hired by Netscape in 1995 with the promise of“doing Scheme for the browser”But Java was also being implemented for the browserSo if there was a second language, it should “look like Java”

So syntax closer to Java, but semantics stolen from Scheme. . . JavaScript!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 26 / 28

Page 69: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Javascript

Brendan Eich was hired by Netscape in 1995 with the promise of“doing Scheme for the browser”But Java was also being implemented for the browserSo if there was a second language, it should “look like Java”So syntax closer to Java, but semantics stolen from Scheme

. . . JavaScript!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 26 / 28

Page 70: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Javascript

Brendan Eich was hired by Netscape in 1995 with the promise of“doing Scheme for the browser”But Java was also being implemented for the browserSo if there was a second language, it should “look like Java”So syntax closer to Java, but semantics stolen from Scheme. . . JavaScript!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 26 / 28

Page 71: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Code comparison

Scheme:

(define (make-counter incrementer)(let ((counter 0))(lambda ()(let ((current-val counter))(set! counter (incrementer counter))current-val))))

Javascript:

function make-counter(incrementer) {var counter = 0;return function () {var current_val = counter;counter = incrementer(counter);return current_val;

};}

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 27 / 28

Page 72: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

Code comparison

Scheme:

(define (make-counter incrementer)(let ((counter 0))(lambda ()(let ((current-val counter))(set! counter (incrementer counter))current-val))))

Javascript:

function make-counter(incrementer) {var counter = 0;return function () {var current_val = counter;counter = incrementer(counter);return current_val;

};}

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 27 / 28

Page 73: Lessons from 6.001 - 6.037 - Structure and Interpretation ...web.mit.edu/alexmv/6.S184/l8c-transitions.pdfYou know how to write evaluators What good is writing an evaluator? Allows

And now...

And now for some magic!

Mike Phillips <mpp> (MIT) Lessons from 6.001 Lecture 8 28 / 28