1 / 32

Lecture 4: Metacircles

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)

lonna
Download Presentation

Lecture 4: Metacircles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  10. Metalinguistic Abstraction CS 655: Lecture 4

  11. Metacircular Evaluator Eval Apply 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. 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)) 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)))) 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 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)) CS 655: Lecture 4

  17. Data abstraction would be a good thing here. 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))))))) 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))) 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. 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)))) 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. CS 655: Lecture 4

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

  27. Primitives: Environment (define global-env (bind-variable 'minus (list 'primitive -) (bind-variable 'inc (list 'primitive (lambda (x) (+ x 1))) '(())))) CS 655: Lecture 4

  28. Mini-Scheme Examples (eval ‘(minus 3) global-env) -3 (eval '((lambda (x) (inc x)) 3) global-env) 4 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))))) 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))))))) 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 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? CS 655: Lecture 4

More Related