Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: Metacircles Eval Apply David Evans

Similar presentations


Presentation on theme: "Lecture 4: Metacircles Eval Apply David Evans"— Presentation transcript:

1 Lecture 4: Metacircles Eval Apply David Evans
Background just got here last week finished degree at MIT week before Philosophy of advising students don’t come to grad school to implement someone else’s idea can get paid more to do that in industry learn to be a researcher important part of that is deciding what problems and ideas are worth spending time on grad students should have their own project looking for students who can come up with their own ideas for research will take good students interested in things I’m interested in – systems, programming languages & compilers, security rest of talk – give you a flavor of the kinds of things I am interested in meant to give you ideas (hopefully even inspiration!) but not meant to suggest what you should work on CS655: Programming Languages University of Virginia Computer Science David Evans

2 Menu Recap Higher Order Procedures Metalinguistic Abstraction
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 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 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? 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)) 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 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 30 Jan 2001 CS 655: Lecture 4

9 Composer (define (composer lis) (if (null? lis) (lambda (x) x)
((car lis) ((composer (cdr lis)) x))))) ((composer (???-els '(1 2 3))) 0) 6 30 Jan 2001 CS 655: Lecture 4

10 Metalinguistic Abstraction
30 Jan 2001 CS 655: Lecture 4

11 Metacircular Evaluator
Apply 30 Jan 2001 CS 655: Lecture 4

12 Scheme Evaluation To evaluate a compound expression, evaluate the subexpressions, then apply the value of the first subexpression to the values of the other subexpressions. 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. 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)) 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)))) 30 Jan 2001 CS 655: Lecture 4

15 Environments ((proc 3) 4) 7 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 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)) 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. 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))) 30 Jan 2001 CS 655: Lecture 4

19 Scheme Evaluation To evaluate a compound expression, evaluate the subexpressions, then apply the value of the first subexpression to the values of the other subexpressions. 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. 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)))) 30 Jan 2001 CS 655: Lecture 4

21 Scheme Evaluation To evaluate a compound expression, evaluate the subexpressions, then apply the value of the first subexpression to the values of the other subexpressions. 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. 30 Jan 2001 CS 655: Lecture 4

22 Eval (literal translation)
(define (eval expr) (apply (eval (car expr)) (eval (car (cdr expr))))) 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))))) 30 Jan 2001 CS 655: Lecture 4

24 Examples (eval 3 '(())) 3 (eval '((lambda (x) x) 3) ‘(()))
(eval '(((lambda (x) (lambda (y) (+ x y))) 3) 4) ‘(())) ;No binding for x 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] 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] 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))) '(())))) 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 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))))) 30 Jan 2001 CS 655: Lecture 4

30 Bookkeeping Code (cons (cons (cons var value) (car env)) (cdr env)))
(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))))))) 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 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? 30 Jan 2001 CS 655: Lecture 4


Download ppt "Lecture 4: Metacircles Eval Apply David Evans"

Similar presentations


Ads by Google