message passing: alternative to generics data is active knows how to compute dispatch on operation...

17
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n <integer>) (d <integer>)) (method ((op <symbol>)) (cond ((= op ’numer) n) ((= op ’denom) d) (else: (error ”Bogus operation")))))) ((rat 3 4) ’numer) => 3

Upload: owen-parks

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

New Special Form — set! (set! identifier expression)

TRANSCRIPT

Page 1: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

Message Passing: Alternative to Generics

• data is “active” — knows how to compute• dispatch on operation

(define (rat (n <integer>) (d <integer>)) (method ((op <symbol>)) (cond ((= op ’numer) n) ((= op ’denom) d) (else: (error ”Bogus operation"))))))

((rat 3 4) ’numer) => 3

Page 2: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

In Java:

String m = new String("CS212");

...

if (m.equals("CS211")) { ...

if (m == "CS211") { ...

Page 3: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

New Special Form — set!

(set! identifier expression)

Page 4: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

set! trashes the substitution model:

(define put-in-bowl (lambda (thing) (set! thing (cons thing '(in a bowl))) thing))

(put-in-bowl 'goldfish)=> (goldfish in a bowl)

Page 5: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

Which variable does set! set?

(define x 'barney)=> x

(let ((x 'purple) (y (set! x 'monster))) x)=> ???

x=> ???

Page 6: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

Which variable does set! set?

(define x 'barney)=> x

(let* ((x 'purple) (y (set! x 'monster))) x)=> monster

x=> barney

Page 7: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

An example not involving set!

(define x 10)

(define foo (lambda () x))

(let ((x 15)) (foo)) => ???

Page 8: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

An example not involving set!

(define x 10)

(define foo (lambda () x))

(let ((x 15)) (foo)) => 10

Page 9: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

Static vs. Dynamic Scoping

Static scoping:• free variables resolved in the environment of the

function definition

Dynamic scoping:• free variables resolved in the environment of the

function call

Closure:• when a function object is created, the environment

is packaged with it

Page 10: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

Referential Transparency

• replace equal values with equal values

• set! destroys referential transparency

Page 11: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

The Environment Model

• an extension of the substitution model• more "operational"• fully explains static scoping and the process by

which variable names are resolved in Scheme (and in almost any other programming language.)

An expression only has a value with respectto an environment

Page 12: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

Bindingidentifier : value

Frameunordered set of bindings

Environmentordered list of frames

Page 13: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

x:10 +:{add}y:20z:30

x:() w:foobar

x:ohy:(list of stuff)

globalenvironment(top level frame)

(machine code foradding numbers)

Page 14: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

The Lookup Rule

• the value of an identifier x is the value associated with x in the first (lowest) frame containing abinding for x

• undefined if there aren't any

Page 15: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

Define rule

To evaluate (define ident expr):

• evaluate expr in the current environment• add a binding to the top-level frame

(the global environment) Set! rule

To evaluate (set! ident expr):

• evaluate expr in the current environment• look up ident using lookup rule• change that binding to value of expr

Page 16: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

x:10 +:{add}y:20z:30

x:() w:foobar

x:ohy:(list of stuff)

x:10 +:{add}y:20 d:12z:30

x:() w:foobar

x:ohy:(list of stuff)

(define d (+ 5 7))

before after

Page 17: Message Passing: Alternative to Generics data is active  knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

x:10 +:{add}y:20z:30

x:() w:foobar

x:ohy:(list of stuff)

x:10 +:{add}y:20z:30

x:() w:foobar

x:myy:(list of stuff)

(set! x 'my)

before after