One-Slide Summary Programming The substitution model for evaluating - - PDF document

one slide summary programming
SMART_READER_LITE
LIVE PREVIEW

One-Slide Summary Programming The substitution model for evaluating - - PDF document

One-Slide Summary Programming The substitution model for evaluating Scheme does with not allow us to reason about mutation. In the environment model : State A name is a place for storing a value. define , cons and function application


slide-1
SLIDE 1

#1

Programming with State & Golden Ages

#2

One-Slide Summary

  • The substitution model for evaluating Scheme does

not allow us to reason about mutation. In the environment model:

  • A name is a place for storing a value. define, cons

and function application create places. set! changes the value in a place.

  • Places live in frames. An environment is a frame and

a pointer to a parent frame. The global environment has no parent.

  • To evaluate a name, walk up the frames until you

find a definition.

  • A golden age is a period when knowledge or quality

increases rapidly.

#3

Outline

  • Names and Places
  • set! and friends
  • Environment Model
  • Golden Ages
  • Interested in random weekly emails about

available CS 150 tutoring? Send email to the course staff (or me) to get on that list.

  • There will not be normally scheduled lab hours or
  • ffice hours over spring break.
  • Your Exam 1 grade will be visible on the

Automatic Adjudication website.

#4

Evaluation Rule 2: Names

If the expression is a name, it evaluates to the value associated with that name.

> (define two 2) > two 2

From Lecture 3:

This is called the substitution model. You can reason about Scheme expressions by substituting the definition in whenever it is used.

#5

Names and Places

  • A name is not just a value, it is a

place for storing a value.

  • define creates a new place,

associates a name with that place, and stores a value in that place

x: 3 (define x 3)

#6

Bang!

set! (“set bang”) changes the value associated with a place

> (define x 3) > x 3 > (set! x 7) > x 7

x:

3

7

slide-2
SLIDE 2

#7

set! should make you nervous

> (define x 2) > (nextx) 3 > (nextx) 4

> x 4

Before set! all procedures were pure functions (except for some with side-effects). The value of (f) was the same every time you evaluated it. Now it might be different!

#8

Defining nextx

(define (nextx) (set! x (+ x 1)) x)

(define nextx (lambda () (begin (set! x (+ x 1)) x))))

syntactic sugar for

#9

Evaluation Rules

> (define x 3) > (+ (nextx) x) 7

  • r 8

> (+ x (nextx)) 9

  • r 10

#10

Evaluation Rules

> (define x 3) > (+ (nextx) x) 7

  • r 8

> (+ x (nextx)) 9

  • r 10

DrScheme evaluates application subexpressions left to right, but Scheme evaluation rules allow any

  • rder.

#11

set-car! and set-cdr!

(set-car! p v) Replaces the car of the cons p with v. (set-cdr! p v) Replaces the cdr of the cons p with

  • v. These should scare you even more then set! !

#12

> (define pair (cons 1 2)) > pair (1 . 2)

pair:

1

2

slide-3
SLIDE 3

#13

> (define pair (cons 1 2)) > pair (1 . 2) > (set-car! pair 0) > (car pair) > (cdr pair) 2

pair:

1

2 2

#14

> (define pair (cons 1 2)) > pair (1 . 2) > (set-car! pair 0) > (car pair) > (cdr pair) 2 > (set-cdr! pair 1) > pair (0 . 1)

pair:

1

2 1

#15

Functional vs. Imperative

Functional Solution: A procedure that takes a procedure of one argument and a list, and returns a list of the results produced by applying the procedure to each element in the list.

(define (map proc lst) (if (null? lst) null (cons (proc (car lst)) (map proc (cdr lst)))))

#16

Imperative Solution

A procedure that takes a procedure and list as arguments, and replaces each element in the list with the value of the procedure applied to that element.

(define (map! f lst) (if (null? lst) (void) (begin (set-car! lst (f (car lst))) (map! f (cdr lst)))))

(define (map proc lst) (if (null? lst) null (cons (proc (car lst)) (map proc (cdr lst)))))

#17

Programming with Mutation

> (map! square (intsto 4)) > (define i4 (intsto 4)) > (map! square i4) > i4 (1 4 9 16) > (define i4 (intsto 4)) > (map square i4) (1 4 9 16) > i4 (1 2 3 4) Functional Imperative

#18

Mutation Changes Everything!

  • We can no longer talk about the “value of

an expression”

– The value of a give expression can change! – We need to talk about “the value of an expression in an execution environment”

  • “execution environment” = “context so far”
  • The order in which expressions are

evaluated now matters

slide-4
SLIDE 4

#19

Why Substitution Fails

> (define (nextx) (set! x (+ x 1)) x) > (define x 0) > ((lambda (x) (+ x x)) (nextx)) 2

Substitution model for evaluation would predict:

(+ (nextx) (nextx)) (+ (begin (set! x (+ x 1)) x) (begin (set! x (+ x 1)) x)) (+ (begin (set! 0 (+ 0 1)) 0) (begin (set! 0 (+ 0 1)) 0)) (+ 0 0)

#20

Liberal Arts Trivia: Astrophysics

  • According to this 1915 theory (be specific), the
  • bserved gravitational attraction between

masses results from the warping of space and time by those masses. This theory helps to explain observed phenomena, such as anomalies in the orbit of Mercury, that are not predicted by Newton's Laws, and can deal with accelerated reference frames. It is part of the framework of the standard Big Bang model of Cosmology.

#21

Liberal Arts Trivia: Rhetoric

  • This type of “values” debate traditionally

places a heavy emphasis on logic, ethical values and philosophy. It is a one-on-one debate practiced in National Forensic League competitions. The format was named for the series of seven debates in 1858 for the Illinois seat in the United State Senate.

#22

Very Scary!

  • The old

substitution model does not explain Scheme programs that contain mutation.

  • We need a new

environment model.

#23

Names and Places

  • A name is a place for storing a value.
  • define creates a new place
  • cons creates two new places, the car and

the cdr

  • (set! name expr) changes the value in the

place name to the value of expr

  • (set-car! pair expr) changes the value in the

car place of pair to the value of expr

#24

Lambda and Places

  • (lambda (x) …) also creates a new place

named x

  • The passed argument is put in that place

> (define x 3) > ((lambda (x) x) 4) 4 > x 3

How are these places different?

x : 3 x : 4

slide-5
SLIDE 5

#25

Location, Location, Location

  • Places live in frames
  • An environment is a frame and a

pointer to a parent environment

  • All environments except the global

environment have exactly one parent environment, global environment has no parent

  • Application creates a new environment

#26

Environments

global environment + : #<primitive:+> null? : #<primitive:null?>

The global environment points to the outermost

  • frame. It starts with all Scheme primitives.

#27

Environments

global environment

> (define x 3) >

+ : #<primitive:+> null? : #<primitive:null?>

The global environment points to the outermost

  • frame. It starts with all Scheme primitives.

x : 3

#28

Evaluation Rule 2: Names

A name expression evaluates to the value associated with that name.

To find the value associated with a name, look for the name in the frame associated with the evaluation

  • environment. If it contains a place with that name, the

value of the name expression is the value in that place. If it doesn’t, the value of the name expression is the value of the name expression evaluated in the parent environment if the current environment has a parent. Otherwise, the name expression evaluates to an error (the name is not defined).

#29

Procedures

global environment

> (define x 3) > (define double (lambda (x) (+ x x))) >

+ : #<primitive:+> null? : #<primitive:null?> double: ??? x : 3

#30

How to Draw a Procedure

  • A procedure needs both code and an

environment

– We’ll see why soon

  • We draw procedures like this:

Environment pointer environment: parameters: x body: (+ x x)

slide-6
SLIDE 6

#31

How to Draw a Procedure (for artists only)

Environment pointer

x (+ x x)

Input parameters (in mouth) Procedure Body

#32

Procedures

global environment

> (define double

(lambda (x) (+ x x)))

+ : #<primitive:+> null? : #<primitive:null?> double: x : 3

environment: parameters: x body: (+ x x)

#33

Application

  • Old rule: (Substitution model)

Apply Rule 2: Constructed Procedures. To apply a constructed procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value.

#34

New Application Rule 2:

  • 1. Construct a new environment, whose

parent is the environment to which the environment pointer of the applied procedure points.

  • 2. Create places in that frame for each

parameter containing the value of the corresponding operand expression.

  • 3. Evaluate the body in the new
  • environment. Result is the value of the

application.

#35

1. Construct a new environment, parent is procedure’s environment pointer 2. Make places in that frame with the names of each parameter, and

  • perand values

3. Evaluate the body in the new environment

global environment

> (double 4)

8

+ : #<primitive:+> x : 3 x :

4 (+ x x)

double:

environment: parameters: x body: (+ x x)

(+ x x)

#36

1. Construct a new environment, parent is procedure’s environment pointer 2. Make places in that frame with the names of each parameter, and

  • perand values

3. Evaluate the body in the new environment

global environment

> (define x 999)

+ : #<primitive:+> x : 999

slide-7
SLIDE 7

#37

1. Construct a new environment, parent is procedure’s environment pointer 2. Make places in that frame with the names of each parameter, and

  • perand values

3. Evaluate the body in the new environment

global environment

> (define x 999) > (define (adder x) (lambda (y) (+ x y))))

+ : #<primitive:+> x : 999 adder:

environment: parameters: x body: (lambda (y) (+ x y))

#38

1. Construct a new environment, parent is procedure’s environment pointer 2. Make places in that frame with the names of each parameter, and

  • perand values

3. Evaluate the body in the new environment

global environment

> (define x 999) > (define (adder x) (lambda (y) (+ x y)))) > (define addtwo (adder 2))

+ : #<primitive:+> x : 999 adder:

environment: parameters: x body: (lambda (y) (+ x y))

x : 2

#39

1. Construct a new environment, parent is procedure’s environment pointer 2. Make places in that frame with the names of each parameter, and

  • perand values

3. Evaluate the body in the new environment

global environment

> (define x 999) > (define (adder x) (lambda (y) (+ x y)))) > (define addtwo (adder 2))

+ : #<primitive:+> x : 999 adder:

environment: parameters: x body: (lambda (y) (+ x y))

x : 2

environment: parameters: y body: (+ x y)

#40

1. Construct a new environment, parent is procedure’s environment pointer 2. Make places in that frame with the names of each parameter, and

  • perand values

3. Evaluate the body in the new environment

global environment

> (define x 999) > (define (adder x) (lambda (y) (+ x y)))) > (define addtwo (adder 2))

+ : #<primitive:+> x : 999 adder:

environment: parameters: x body: (lambda (y) (+ x y))

:addtwo x : 2

environment: parameters: y body: (+ x y)

#41

1. Construct a new environment, parent is procedure’s environment pointer 2. Make places in that frame with the names of each parameter, and

  • perand values

3. Evaluate the body in the new environment

global environment

> (define x 999) > (define (adder x) (lambda (y) (+ x y)))) > (define addtwo (adder 2)) > (addtwo 6)

+ : #<primitive:+> x : 999 adder:

environment: parameters: x body: (lambda (y) (+ x y))

:addtwo x : 2

environment: parameters: y body: (+ x y)

y : 6

#42

1. Construct a new environment, parent is procedure’s environment pointer 2. Make places in that frame with the names of each parameter, and

  • perand values

3. Evaluate the body in the new environment

global environment

> (define x 999) > (define (adder x) (lambda (y) (+ x y)))) > (define addtwo (adder 2)) > (addtwo 6) 8

+ : #<primitive:+> x : 999 adder:

environment: parameters: x body: (lambda (y) (+ x y))

:addtwo x : 2

environment: parameters: y body: (+ x y)

y : 6

slide-8
SLIDE 8

#43

Liberal Arts Trivia: Statistics

  • In probability theory and statistics, this

indicates the strength and direction of a linear relationship between two random

  • variables. A number of different

coefficients are in different situations, the best known of which is the Pearson product-moment coefficient. Notably, this concept does not imply causation.

#44

Liberal Arts Trivia: Music

  • This baroque keyboard

instrument is the spiritual predecessor of the

  • pianoforte. It produces a

sound by plucking a string when each key is pressed, but unlike the piano it lacks responsiveness to keyboard touch and thus fails to produce notes at different dynamic levels.

Jan Vermeer, 1670

45

http://www.pbs.org/wgbh/nova/sciencenow/3313/nn-video-toda-w-220.html

Science's Endless Golden Age

#46

Astrophysics

  • “If you’re going to use

your computer to simulate some phenomenon in the universe, then it only becomes interesting if you change the scale of that phenomenon by at least a factor of 10. … For a 3D simulation, an increase by a factor of 10 in each of the three dimensions increases your volume by a factor of 1000.”

  • How much work is

astrophysics simulation (in Θ notation)?

#47

Astrophysics

  • “If you’re going to use your computer to simulate

some phenomenon in the universe, then it only becomes interesting if you change the scale of that phenomenon by at least a factor of 10. … For a 3D simulation, an increase by a factor of 10 in each of the three dimensions increases your volume by a factor of 1000.”

  • How much work is astrophysics simulation (in Θ

notation)?

Θ(n3)

When we double the size of the simulation, the work octuples! (Just like oceanography octopi simulations)

#48

Orders of Growth

20 40 60 80 100 120 140 1 2 3 4 5

n 3 n 2 n

insert-sort simulating universe find-best

slide-9
SLIDE 9

#49

Orders of Growth

200000 400000 600000 800000 1000000 1200000 1400000 1 11 21 31 41 51 61 71 81 91 101

insert-sort simulating universe find-best

#50

Astrophysics and Moore’s Law

  • Simulating universe is Θ(n3)
  • Moore’s law: computing power

doubles every 18 months

  • Dr. Tyson: to understand

something new about the universe, need to scale by 10x

  • How long does it take to know

twice as much about the universe?

#51

;;; doubling every 18 months = ~1.587 * every 12 months (define (computing-power nyears) (if (= nyears 0) 1 (* 1.587 (computing-power (- nyears 1))))) ;;; Simulation is Θ(n3) work (define (simulation-work scale) (* scale scale scale)) (define (log10 x) (/ (log x) (log 10))) ;;; log is base e

;;; knowledge of the universe is log10 the scale of universe ;;; we can simulate

(define (knowledge-of-universe scale) (log10 scale))

Knowledge of the Universe

#52

(define (computing-power nyears) (if (= nyears 0) 1 (* 1.587 (computing-power (- nyears 1))))) ;;; doubling every 18 months = ~1.587 * every 12 months (define (simulation-work scale) (* scale scale scale)) ;;; Simulation is O(n^3) work (define (log10 x) (/ (log x) (log 10))) ;;; primitive log is natural (base e) (define (knowledge-of-universe scale) (log10 scale)) ;;; knowledge of the universe is log 10 the scale of universe we can simulate

(define (find-knowledge-of-universe nyears) (define (find-biggest-scale scale)

;;; today, can simulate size 10 universe = 1000 work

(if (> (/ (simulation-work scale) 1000) (computing-power nyears)) (- scale 1) (find-biggest-scale (+ scale 1)))) (knowledge-of-universe (find-biggest-scale 1)))

Knowledge of the Universe

#53

> (find-knowledge-of-universe 0) 1.0 > (find-knowledge-of-universe 1) 1.041392685158225 > (find-knowledge-of-universe 2) 1.1139433523068367 > (find-knowledge-of-universe 5) 1.322219294733919 > (find-knowledge-of-universe 10) 1.6627578316815739 > (find-knowledge-of-universe 15) 2.0 > (find-knowledge-of-universe 30) 3.00560944536028 > (find-knowledge-of-universe 60) 5.0115366121349325 > (find-knowledge-of-universe 80) 6.348717927935257

Will there be any mystery left in the Universe when you die?

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein

#54

The Endless Golden Age

  • Golden Age: period in which

knowledge/quality of something doubles quickly

  • At any point in history, half of what is

known about astrophysics was discovered in the previous 15 years!

– Moore’s law today, but other advances previously: telescopes, photocopiers, clocks, agriculture, etc.

slide-10
SLIDE 10

#55

Endless/Short Golden Ages

  • Endless golden age: at any point in history,

the amount known is twice what was known 15 years ago

– Always exponential growth: Θ(kn)

k is some constant, n is number of years

  • Short golden age: knowledge doubles during

a short, “golden” period, but only improves linearly most of the time

– Usually linear growth: Θ(n)

n is number of years

#56

10,000,000 20,000,000 30,000,000 40,000,000 50,000,000 60,000,000 70,000,000 80,000,000 1969 1972 1975 1978 1981 1984 1987 1990 1993 1996 1999 2002 2005 2008

Computing Power 1969-2008 (in Apollo Control Computer Units)

Moore’s “Law”: computing power roughly doubles every 18 months!

#57

2,000 4,000 6,000 8,000 10,000 12,000 14,000 16,000 18,000 1 9 6 9 1 9 7 . 5 1 9 7 2 1 9 7 3 . 5 1 9 7 5 1 9 7 6 . 5 1 9 7 8 1 9 7 9 . 5 1 9 8 1 1 9 8 2 . 5 1 9 8 4 1 9 8 5 . 5 1 9 8 7 1 9 8 8 . 5 1 9 9

Computing Power 1969-1990 (in Apollo Control Computer Units)

#58

1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 1930 1934 1938 1950 1954 1958 1962 1966 1970 1974 1978 1982 1986 1990 1994 1998 2002 2006

Average Goals per Game, FIFA World Cups

Changed goalkeeper passback rule

Goal-den age

#59

Endless Golden Age and “Grade Inflation”

  • Average student gets twice as smart

and well-prepared every 15 years

– You had grade school teachers (maybe even parents) who went to college!

  • If average GPA in 1977 is 2.00 what

should it be today (if grading standards didn’t change)?

#60

Grade Inflation or Deflation?

2.00 average GPA in 1977 (“gentle C”?) * 2 better students 1977-1992 * 2 better students 1992-2007 * 1.49 population increase * 0.74 increase in enrollment

Average GPA today should be: 8.82 (but our expectations should also increase)

Students 1976: 10,330 Students 2006: 13,900 Virginia 1976: ~5.1M Virginia 2006: ~7.6M

slide-11
SLIDE 11

#61

The Real Golden Rule?

Why do fields like astrophysics, medicine, biology and computer science have “endless golden ages”, but fields like ...

– rock n’ roll (1962-1973, or whatever was popular when you were 16) – music (1775-1825) – philosophy (400BC-350BC?) – art (1875-1925?) – soccer (1950-1966) – baseball (1925-1950?) – movies (1920-1940?)

have short golden ages? Think about it over the break!

#62

Homework

  • Start PS 5 now!

– Due Wednesday after Break – If you wait until after Break, you will probably not have enough time.

  • Read Course Book 9 and 10 over Break