32
Lecture 4: Metacircl es David Evans http://www.cs.virginia.edu/ ~evans CS655: Programming Languages University of Virginia Computer Science Eval Apply

Lecture 4: Metacircles

  • Upload
    lonna

  • View
    11

  • Download
    0

Embed Size (px)

DESCRIPTION

Lecture 4: Metacircles. Eval. Apply. CS655: Programming Languages University of Virginia Computer Science. David Evans http://www.cs.virginia.edu/~evans. Menu. Recap Higher Order Procedures Metalinguistic Abstraction. Doubling Elements. (define (double-els lis) (if (null? lis) - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 4: Metacircles

Lecture 4: Metacircles

David Evanshttp://www.cs.virginia.edu/~evans

CS655: Programming LanguagesUniversity of VirginiaComputer Science

Eval

Apply

Page 2: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 2

Menu

• Recap Higher Order Procedures

• Metalinguistic Abstraction

Page 3: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 3

Doubling Elements

(define (double-els lis)

(if (null? lis)

lis

(cons (+ (car lis) (car lis)) (cdr lis))))

(double-els ‘(1 2 3))

2 4 6

Page 4: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 4

Incrementing Elements

(define (increment-els lis)

(if (null? lis)

lis

(cons (+ (car lis) 1) (cdr lis))))

(increment-els ‘(1 2 3))

2 3 4

Page 5: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 5

Mapping Elements

(define (map-els f lis)

(if (null? lis)

lis

(cons (f (car lis))

(map-els f (cdr lis)))))

(map-els (lambda (x) x) ‘(1 2 3))1 2 3 Can we define double-els using map-els?

Page 6: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 6

Using Map

(define (double-els lis)

(map-els (lambda (x) (+ x x)) lis))

(define (increment-els lis)

(map-els (lambda (x) (+ x 1)) lis))

(define (???-els lis)

(map-els

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

lis))

Page 7: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 7

Compose

(define (compose f g x) (f (g x)))

(compose (lambda (x) (+ x 1))

(lambda (x) (* x 2))

2)

5

Page 8: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 8

Composef

(define (composef f g)

(lambda (x) (f (g x))))

((composef (lambda (x) (+ x 1))

(lambda (x) (* x 2)))

2)

5

Page 9: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 9

Composer

(define (composer lis)

(if (null? lis) (lambda (x) x)

(lambda (x)

((car lis) ((composer (cdr lis)) x)))))

((composer (???-els '(1 2 3))) 0)6

Page 10: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 10

Metalinguistic Abstraction

Page 11: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 11

Metacircular Evaluator

Eval

Apply

Page 12: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 12

Scheme Evaluation1. To evaluate a compound expression, evaluate the subexpressions, then apply the value of the first subexpression to the values of the other subexpressions.

2. To apply a procedure to a list of arguments, evaluate the procedure in a new environment that binds the formal parameters of the procedure to the arguments it is applied to.

Page 13: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 13

Environments• Bind variables to values

• Binding table is a list of pairs:

((‘x 3)

(‘+ +)

(‘id (lambda (x) x)))

• Add a binding to a binding table:(define (add-binding name value bindings)

(cons (cons name value) bindings))

Page 14: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 14

Environments

• An environment is a list of binding tables

• The first binding table corresponds to the innermost environment

(define proc

(lambda (x)

(lambda (y)

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

Page 15: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 15

Environments

• An environment is a list of binding tables

• The first binding table corresponds to the innermost environment

(define proc

(lambda (x)

(lambda (y)

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

((proc 3) 4)7

Page 16: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 16

Managing Environments

(define (bind-variable var value env)

(cons (cons (cons var value) (car env)) (cdr env)))

(define (extend-environment env)

(cons '() env))

Page 17: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 17

Lookup(define (lookup-variable var env)

(if (null? env)

(error "Unbound variable" var)

(if (null? (car env))

(lookup-variable var (cdr env))

(if (eq? var (car (car (car env))))

(cdr (car (car env)))

(lookup-variable var

(cons (cdr (car env)) (cdr env)))))))

Data abstraction would be a good thing here.

Page 18: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 18

Environments Quiz

(lookup-variable 'x

(bind-variable 'x 7

(extend-environment

(bind-variable 'y 4

(bind-variable 'x 3 env)))))7 The environment is:

(((x . 7)) ((y . 4) (x . 3)))

Page 19: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 19

Scheme Evaluation1. To evaluate a compound expression, evaluate the subexpressions, then apply the value of the first subexpression to the values of the other subexpressions.

2. To apply a procedure to a list of arguments, evaluate the procedure in a new environment that binds the formal parameters of the procedure to the arguments it is applied to.

Page 20: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 20

Apply;;; proc is ('procedure (param) body)

(define (apply proc operand env)

(eval

(car (cdr (cdr proc)))

(bind-variable

(car (car (cdr proc)))

operand

(extend-environment env))))

Page 21: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 21

Scheme Evaluation1. To evaluate a compound expression, evaluate the subexpressions, then apply the value of the first subexpression to the values of the other subexpressions.

2. To apply a procedure to a list of arguments, evaluate the procedure in a new environment that binds the formal parameters of the procedure to the arguments it is applied to.

Page 22: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 22

Eval (literal translation)

(define (eval expr)

(apply (eval (car expr))

(eval (car (cdr expr)))))

Page 23: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 23

Eval(define (eval expr env)

(if (number? expr) expr

(if (symbol? expr) (lookup-variable expr env)

(if (eq? (car expr) 'lambda)

(list 'procedure (car (cdr expr))

(car (cdr (cdr expr))))

(apply (eval (car expr) env)

(eval (car (cdr expr)) env)

env)))))

Page 24: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 24

Examples

(eval 3 '(()))

3

(eval '((lambda (x) x) 3) ‘(()))

3

(eval '(((lambda (x) (lambda (y) (+ x y))) 3) 4) ‘(()))

;No binding for x

Page 25: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 25

Handling Primitives

;; represented by (primitive func)

(define (apply proc operand env)

(if (eq? (car proc) 'primitive)

((car (cdr proc)) operand)

same as before]

Page 26: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 26

Handling Primitives: Eval(define (eval expr env)

(if (or (number? expr)

(and (list? expr)

(eq? (car expr) 'primitive))

expr

rest is same]

Page 27: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 27

Primitives: Environment

(define global-env

(bind-variable 'minus (list 'primitive -) (bind-variable 'inc

(list 'primitive (lambda (x) (+ x 1)))

'(()))))

Page 28: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 28

Mini-Scheme Examples

(eval ‘(minus 3) global-env)

-3

(eval '((lambda (x) (inc x)) 3) global-env)

4

Page 29: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 29

An Entire Mini-Scheme Interpreter!(define (apply proc operand env)

(if (eq? (car proc) 'primitive) ((car (cdr proc)) operand)

(eval (car (cdr (cdr proc)))

(bind-variable (car (car (cdr proc))) operand

(extend-environment env)))))

(define (eval expr env)

(if (or (number? expr)

(and (list? expr) (eq? (car expr) 'primitive))) expr

(if (symbol? expr)

(lookup-variable-value expr env)

(if (and (list? expr) (eq? (car expr) 'lambda))

(list 'procedure (car (cdr expr)) (car (cdr (cdr expr))))

(apply (eval (car expr) env) (eval (car (cdr expr)) env) env)))))

Page 30: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 30

Bookkeeping Code(define (bind-variable var value env)

(cons (cons (cons var value) (car env)) (cdr env)))

(define (extend-environment env) (cons '() env))

(define (lookup-variable-value var env)

(if (null? env) (error "No binding for " var)

(if (null? (car env)) (lookup-variable-value var (cdr env))

(if (eq? var (car (car (car env))))

(cdr (car (car env)))

(lookup-variable-value var

(cons (cdr (car env)) (cdr env)))))))

Page 31: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 31

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

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

Abelson & Sussman, Ch 4

Page 32: Lecture 4: Metacircles

30 Jan 2001 CS 655: Lecture 4 32

Charge

• Problem Set 1 due Thursday

• Source code from today is on web site

• Next time (and PS2): Changing Mini-Scheme

• Next Tuesday: An even simpler language

• Next Thursday: does it really make sense to define things in terms of themselves?