class 32: interpreters

36
Class 32: Interpret ers cs1120 Fall 2011 David Evans 7 November 2011

Upload: david-evans

Post on 28-Nov-2014

417 views

Category:

Technology


0 download

DESCRIPTION

Meta-Linguistic ProgrammingInterpretersHistory of Object-Oriented ProgrammingImplementing Application

TRANSCRIPT

Page 1: Class 32: Interpreters

Class 32:Interpreters

cs1120 Fall 2011David Evans7 November 2011

Page 2: Class 32: Interpreters

2

Plan

Implementing InterpretersHistory of Object-Oriented Programming

Problem Set 7: posted today, due Monday 14 November.• Understand the Charme interpreter (described in Chapter 11)• Modify it to change the evaluation rules

Page 3: Class 32: Interpreters

Building a Language

Design the grammarWhat strings are in the language?Use BNF to describe all the strings in the language

Make up the evaluation rulesDescribe what every string in the language means

Build an evaluatorImplement a procedure that takes a string in the language as input and an environment and outputs its value:

meval: String Environment → Value

Page 4: Class 32: Interpreters

Is this an exaggeration?It is no exaggeration to regard this as the most fundamental idea in programming:

The evaluator, which determines the meaning of expressions in the programming language, is just another program.

To appreciate this point is to change our images of ourselves as programmers. We come to see ourselves as designers of languages, rather than only users of languages designed by others.

Abelson and Sussman, Structure and Interpretation of

Computer Programs (p. 360)

Page 5: Class 32: Interpreters

Building an Evaluator

To build an evaluator we need to:Figure out how to represent data in programs

What is a procedure, frame, environment, etc.Implement the evaluation rules

For each evaluation rule, define a procedure that follows the behavior of that rule.

Next: we’ll look at a high-level how the application rule is implementedWednesday and Chapter 11: detailed walk-through of the interpreter

Page 6: Class 32: Interpreters

6

Core of the Evaluator

Page 7: Class 32: Interpreters

def meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error ('Unknown expression type: ' + str(expr))

Page 8: Class 32: Interpreters

To apply a constructed procedure:1. Construct a new environment, whose parent is the

environment of the applied procedure.2. For each procedure parameter, create a place in the frame

of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

Stateful Application Rule

Page 9: Class 32: Interpreters

Eval

Apply

Eval and Apply are defined in terms of each other.

Page 10: Class 32: Interpreters

10

Page 11: Class 32: Interpreters

Object-Oriented Programming

Whirlwind (MIT, 1947-)

Sketchpad (Ivan Sutherland, 1963)

Page 12: Class 32: Interpreters

Simula• Considered the first

“object-oriented” programming language

• Language designed for simulation by Kristen Nygaard and Ole-Johan Dahl (Norway, 1962)

• Had special syntax for defining classes that packages state and procedures together

Page 13: Class 32: Interpreters

XEROX Palo Alto Research Center (PARC)

1970s:Bitmapped displayGraphical User Interface

Steve Jobs paid $1M to visit and PARC, and returned to make Apple Lisa/Mac

EthernetFirst personal computer (Alto)PostScript PrintersObject-Oriented Programming

Page 14: Class 32: Interpreters

Dynabook, 1972

(Just a mockup – couldn’t really build this in 1972)

“Don’t worry about what anybody else is going to do… The best way to predict the future is to invent it. Really smart people with reasonable funding can do just about anything that doesn't violate too many of Newton's Laws!”

— Alan Kay, 1971

Page 15: Class 32: Interpreters

Dynabook 1972

Tablet computer intended as tool for learningAlan Kay wanted children to program it alsoHallway argument, Kay claims you could define

“the most powerful language in the world in a page of code”

Proof: Smalltalk Scheme is as powerful, but takes 1½ pages (PS7)

Page 16: Class 32: Interpreters

BYTE Magazine,

August 1981

Page 17: Class 32: Interpreters

Smalltalk

• Everything is an object• Objects communicate by sending and

receiving messages• Objects have their own state (which may

contain other objects)• How do Smalltalkers do 3 + 4?

send the object 3 the message “+ 4”

Page 18: Class 32: Interpreters

Counter in Smalltalk

class name counter instance variable names count new count <- 0 next count <- count + 1 current ^ count

Page 19: Class 32: Interpreters

So, who really was the first

object-oriented programmer?

Object-oriented programming is a state of mind where you program thinking about objects that package state and procedures.

Page 20: Class 32: Interpreters

Eval

Apply

Implementing the Interpreter

Page 21: Class 32: Interpreters

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

evalApplication

Page 22: Class 32: Interpreters

22

Representing Expressions

Use Python Lists:['+', '2', '3'] represents (+ 2 3)

How should we represent (+ 2 (+ 3 4)) ?

['+', '2', ['+', '3', '4']]

Page 23: Class 32: Interpreters

23

Parsing

parse(<string>) list representing the Charme code

See Chapter 11 (and the PS7 code) for the details on parsing.

>>> parse("(+ 2 (+ 3 4))")[0]['+', '2', ['+', '3', '4']]>>> parse("(define square (lambda (x) (* x x)))")[0]['define', 'square', ['lambda', ['x'], ['*', 'x', 'x']]]

Page 24: Class 32: Interpreters

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

evalApplication

Page 25: Class 32: Interpreters

25

def evalApplication(expr, env): subexprvals = map (lambda sexpr: meval(sexpr, env), expr) return mapply(subexprvals[0], subexprvals[1:])

Page 26: Class 32: Interpreters

26

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

How should we represent an environment?

Page 27: Class 32: Interpreters

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

class Environment: def __init__(self, parent): self._parent = parent self._frame = {} def addVariable(self, name, value): ... def lookupVariable(self, name): ...

Details in Chapter 11 (and Wednesday’s class)

Page 28: Class 32: Interpreters

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

def mapply(proc, operands): if (isPrimitiveProcedure(proc)): ... elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) for i in range(0, len(params)): newenv.addVariable(params[i], operands[i]) ...

def evalApplication(expr, env): subexprvals = map ( lambda sexpr: meval(sexpr, env), expr) return mapply(subexprvals[0], subexprvals[1:])

Page 29: Class 32: Interpreters

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

def mapply(proc, operands): if (isPrimitiveProcedure(proc)): ... elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) for i in range(0, len(params)): newenv.addVariable(params[i], operands[i])

Page 30: Class 32: Interpreters

def mapply(proc, operands): if (isPrimitiveProcedure(proc)): return proc(operands) elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) for i in range(0, len(params)): newenv.addVariable(params[i], operands[i]) return meval(proc.getBody(), newenv)

def evalApplication(expr, env): subexprvals = map ( lambda sexpr: meval(sexpr, env), expr) return mapply(subexprvals[0], subexprvals[1:])

def meval(expr, env): … elif isApplication(expr): return evalApplication(expr, env) …

Page 31: Class 32: Interpreters

So, who really was the first

object-oriented programmer?

Page 32: Class 32: Interpreters

32

Object-oriented programming is an exceptionally bad idea which could only have originated in California. Edsger Dijkstra

Page 33: Class 32: Interpreters

I don’t know how many of you have

ever met Dijkstra, but you probably know

that arrogance in computer science is

measured in nano-Dijkstras.

Alan Kay

Page 34: Class 32: Interpreters

The people who are the worst at programming are the people who refuse to accept the fact that their brains aren't equal to the task. Their egos keep them from being great programmers. The more you learn to compensate for your small brain, the better a programmer you’ll be. The more humble you are, the faster you’ll improve.

Edsger Dijkstra, 1972 Turing Awardhttp://www.cs.utexas.edu/users/EWD/ewd03xx/EWD340.PDF

Page 35: Class 32: Interpreters

AdaCountess of Lovelace

around 1843

By the word operation, we mean any process which alters the mutual relation of two or more things, be this relation of what kind it may. This is the most general definition, and would include all subjects in the universe. Again, it might act upon other things besides number, were objects found whose mutual fundamental relations could be expressed by those of the abstract science of operations...

Page 36: Class 32: Interpreters

36

Charge

Meta-linguistic programming is also a state of mind!

Read Chapter 11 before Wednesday

PS7 Due next Wednesday (delayed from Monday)