Class 22: Stateful Evaluation Rules

Post on 07-Nov-2014

374 views 1 download

Tags:

description

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

Transcript of Class 22: Stateful Evaluation Rules

Class 22: Stateful Evaluation Rules

cs1120 Fall 2011David Evans14 October 2011

2

Plan

Names and PlacesEnvironmentsRevised Evaluation RulesExam 1

3

Dennis Ritchie, 1941-2011

1972 (with Ken Thompson and PDP-11)

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

5

Why design a new programming language?

C: wanted both portability andlow-level machine access

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

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

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

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

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

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

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.

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.

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?

15

Procedures

globalenvironment

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

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

double: ??

x : 3

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)

17

How to Draw a Procedure (for artists only)

Environmentpointer

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

18

Procedures

globalenvironment

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

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

double:

x : 3

environment:parameters: xbody: (+ x x)

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.

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.

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)

22

x : 3

x : 17

y : 3

What would create this environment?

Think about this, we’lldiscuss it next week…

23

Exam 1

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.

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

26

Honor Responses

Too High

Vast Majority

Between 1 and 2

Trust Completely

0 5 10 15 20 25 30 35

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.

28

29

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

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?

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)

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