Class 22: Stateful Evaluation Rules

32
Class 22: Stateful Evaluation Rules cs1120 Fall 2011 David Evans 14 October 2011

description

Name, Places, Frames, and EnvironmentsStateful Evaluation RulesExam 1Tandem Repeats

Transcript of Class 22: Stateful Evaluation Rules

Page 1: Class 22: Stateful Evaluation Rules

Class 22: Stateful Evaluation Rules

cs1120 Fall 2011David Evans14 October 2011

Page 2: Class 22: Stateful Evaluation Rules

2

Plan

Names and PlacesEnvironmentsRevised Evaluation RulesExam 1

Page 3: Class 22: Stateful Evaluation Rules

3

Dennis Ritchie, 1941-2011

1972 (with Ken Thompson and PDP-11)

Page 4: Class 22: Stateful Evaluation Rules

4

Brie

f His

tory

of

Prog

ram

min

g La

ngua

ges

Fortran (1955, Backus)LISP

(1959, McCarthy)

Algol(1960)

C(1969, Ritchie)

Scheme(1975, S&S)

Python(1991, von Rossum)

Java(1995, Sun)

C++(1983, Stroustrup)

Simula(1967)

JavaScript(1995, Eich)

PS1-5

PS6-7

PS8

Page 5: Class 22: Stateful Evaluation Rules

5

Why design a new programming language?

C: wanted both portability andlow-level machine access

Page 6: Class 22: Stateful Evaluation Rules

6Image: cc http://en.wikipedia.org/wiki/File:Unix_history-simple.svg

Multics

Ken Thompson and Dennis Ritchie

Android iOS

Windows and Symbian:not direct descendants of Unix, but they are mostly programmed in C

Page 7: Class 22: Stateful Evaluation Rules

7

“Here’s how to succeed: by being lucky. Grab on to something that’s moving pretty fast. Let yourself be carried on when you’re in the right place at the right time.”

Dennis Ritchie

Page 8: Class 22: Stateful Evaluation Rules

8

Recap: Names and Places

A name refers to a place for storing a value.define creates a new place(set! name expr) changes the value in the place name to the value of expr

mcons creates a mutable pair of two new places(set-mcar! pair expr) changes the value in the mcar

place of pair to the value of expr(set-mcdr! pair expr) changes the value in the mcdr

place of pair to the value of expr

Page 9: Class 22: Stateful Evaluation Rules

9

Application and Places

(lambda (x) …) also creates a new place named xThe passed argument is put in that place

> (define x 3)> ((lambda (x) x) 4)4> x3 How are these

places different?

x : 3

x : 4

Page 10: Class 22: Stateful Evaluation Rules

10

Location, Location, Location

Places live in framesAn environment is a frame and a pointer to a

parent environmentAll environments except the global environment

have exactly one parent environment, global environment has no parent

Application creates a new environment

Page 11: Class 22: Stateful Evaluation Rules

11

Environments

globalenvironment

> (define x 3)

+ : #<primitive:+>

null? : #<primitive:null?>

The global environment points to the outermost frame. It starts with all Scheme built-ins defined.

x : 3

Page 12: Class 22: Stateful Evaluation Rules

12

Stateful Definition Evaluation Rule

A definition creates a new place with the definition’s name in the frame associated with the evaluation environment. The value in the place is value of the definition’s expression.

If there is already a place with the name in the current frame, the definition replaces the old place with a new place and value.

Page 13: Class 22: Stateful Evaluation Rules

13

Stateful Name Evaluation Rule To evaluate a name expression, search the evaluation environment’s frame for a place with a name that matches the name in the expression. If such a place exists, the value of the name expression is the value in that place. Otherwise, the value of the name expression is the result of evaluating the name expression in the parent environment. If the evaluation environment has no parent, the name is not defined and the name expression evaluates to an error.

Page 14: Class 22: Stateful Evaluation Rules

14

Evaluating NamesTo evaluate a name expression, search the evaluation environment’s frame for a place with a name that matches the name in the expression. If such a place exists, the value of the name expression is the value in that place. Otherwise, the value of the name expression is the result of evaluating the name expression in the parent environment. If the evaluation environment has no parent, the name is not defined and the name expression evaluates to an error.

globalenv x : 3

x : 17

y : 3

How are environments like this created?

Page 15: Class 22: Stateful Evaluation Rules

15

Procedures

globalenvironment

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

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

double: ??

x : 3

Page 16: Class 22: Stateful Evaluation Rules

16

How to Draw a Procedure

• A procedure needs both code and an environment– We’ll see why soon

• We draw procedures like this:Environmentpointer

environment: parameters: xbody: (+ x x)

Page 17: Class 22: Stateful Evaluation Rules

17

How to Draw a Procedure (for artists only)

Environmentpointer

x (+ x x)Input parameters(in mouth) Procedure Body

Page 18: Class 22: Stateful Evaluation Rules

18

Procedures

globalenvironment

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

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

double:

x : 3

environment:parameters: xbody: (+ x x)

Page 19: Class 22: Stateful Evaluation Rules

19

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.

Page 20: Class 22: Stateful Evaluation Rules

20

Stateful Application Rule(Constructed Procedures)

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 or 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.

Page 21: Class 22: Stateful Evaluation Rules

21

1. Construct a new environment, parent is procedure’s environment pointer

2. Make places in that frame with the names of each parameter, and operand values

3. Evaluate the body in the new environment

globalenvironment

> (double 4)8

+ : #<primitive:+>

x : 3

x : 4

(+ x x)

double:

environment:parameters: xbody: (+ x x)

Page 22: Class 22: Stateful Evaluation Rules

22

x : 3

x : 17

y : 3

What would create this environment?

Think about this, we’lldiscuss it next week…

Page 23: Class 22: Stateful Evaluation Rules

23

Exam 1

Page 24: Class 22: Stateful Evaluation Rules

24

Overall Results

5 below 70 5 above 100

Come to my office hours or arrange a meeting to go over things you didn’t understand

Solutions/comments will be posted on course site later by tomorrow.

Page 25: Class 22: Stateful Evaluation Rules

25

Honor15. Do you trust your classmates to follow the honor expectations in this class? — Yes, I trust them completely.— I worry that there may be a few transgressions, but I believe the vast majority

of the class is honorable and it is fair and beneficial to rely on this.— I think this class places too high a burden on students’ honor, and there are

enough dishonorable students that it is unfair on the honorable students.— I have reason to suspect that other students violated the honor policy on

problem sets.— I have direct knowledge of other students violating the honor policy on

problem sets.— I have reason to suspect that other students violated the honor policy on this

exam.— I have direct knowledge of other students violating the honor policy on this

exam.0000

Page 26: Class 22: Stateful Evaluation Rules

26

Honor Responses

Too High

Vast Majority

Between 1 and 2

Trust Completely

0 5 10 15 20 25 30 35

Page 27: Class 22: Stateful Evaluation Rules

27

Problem 11

Define a procedure, count-tandem-repeats, that takes as input a list, p, and a number, n. The output should be the total number of times the first n elements of p are consecutively repeated at the beginning of p. Repetitions may not overlap.

Page 28: Class 22: Stateful Evaluation Rules

28

Page 29: Class 22: Stateful Evaluation Rules

29

(define (count-tandem-repeats p n) (count-prefix-repeats p (list-prefix p n)))

Page 30: Class 22: Stateful Evaluation Rules

30

(define (list-prefix p n) (if (= n 0) null (cons (car p) (list-prefix (cdr p) (- n 1)))))

What is the asymptotic running time of list-prefix?

Page 31: Class 22: Stateful Evaluation Rules

31

(define (contains-matching-prefix p q) ; question 10 (or (null? q) (and (eq? (car p) (car q)) (contains-matching-prefix (cdr p) (cdr q)))))

(define (list-prefix p n) (if (= n 0) null (cons (car p) (list-prefix (cdr p) (- n 1)))))

(define (count-prefix-repeats p q) (if (contains-matching-prefix p q) (+ 1 (count-prefix-repeats ((n-times cdr (length q)) p) q)) 0)) (define (count-tandem-repeats p n) (count-prefix-repeats p (list-prefix p n))) Not defensive: errors if

(< (length p) n)

Page 32: Class 22: Stateful Evaluation Rules

32

Returning Exam 1

(define (list-quicksort cf p) (if (null? p) null (list-append (list-quicksort cf (list-filter (lambda (el) (cf el (car p))) (cdr p))) (cons (car p) (list-quicksort cf (list-filter (lambda (el) (not (cf el (car p)))) (cdr p)))))))

(list-quicksort (lambda (s1 s2) (< (get-email-id s1) (get-email-id s2))) cs1120-students)

Code from Chapter 8