Lecture 4: Metacircles Eval Apply CS655: Programming Languages David Evans University of Virginia http://www.cs.virginia.edu/~evans Computer Science Menu • Recap Higher Order Procedures • Metalinguistic Abstraction 30 Jan 2001 CS 655: Lecture 4

Download Report

Transcript Lecture 4: Metacircles Eval Apply CS655: Programming Languages David Evans University of Virginia http://www.cs.virginia.edu/~evans Computer Science Menu • Recap Higher Order Procedures • Metalinguistic Abstraction 30 Jan 2001 CS 655: Lecture 4

Lecture 4:
Metacircles
Eval
Apply
CS655: Programming Languages
David Evans
University of Virginia
http://www.cs.virginia.edu/~evans
Computer Science
Menu
• Recap Higher Order Procedures
• Metalinguistic Abstraction
30 Jan 2001
CS 655: Lecture 4
2
Doubling Elements
(define (double-els lis)
(if (null? lis)
lis
(cons (+ (car lis) (car lis)) (cdr lis))))
(double-els ‘(1 2 3))
246
30 Jan 2001
CS 655: Lecture 4
3
Incrementing Elements
(define (increment-els lis)
(if (null? lis)
lis
(cons (+ (car lis) 1) (cdr lis))))
(increment-els ‘(1 2 3))
234
30 Jan 2001
CS 655: Lecture 4
4
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))
123
Can we define double-els using map-els?
30 Jan 2001
CS 655: Lecture 4
5
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
6
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
7
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
8
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
30 Jan 2001
CS 655: Lecture 4
9
Metalinguistic Abstraction
30 Jan 2001
CS 655: Lecture 4
10
Metacircular Evaluator
Eval
Apply
30 Jan 2001
CS 655: Lecture 4
11
Scheme Evaluation
1. 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.
30 Jan 2001
CS 655: Lecture 4
12
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
13
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
14
Environments
• An environment is a list of binding tables
• The first binding table corresponds to the
innermost environment
(define proc
((proc 3) 4)
(lambda (x)
7
(lambda (y)
(+ ((lambda (x) x) y) x))))
30 Jan 2001
CS 655: Lecture 4
15
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
16
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)))) Data abstraction
would be a good
(cdr (car (car env)))
thing here.
(lookup-variable var
(cons (cdr (car env)) (cdr env)))))))
30 Jan 2001
CS 655: Lecture 4
17
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
18
Scheme Evaluation
1. 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.
30 Jan 2001
CS 655: Lecture 4
19
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
20
Scheme Evaluation
1. 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.
30 Jan 2001
CS 655: Lecture 4
21
Eval (literal translation)
(define (eval expr)
(apply (eval (car expr))
(eval (car (cdr expr)))))
30 Jan 2001
CS 655: Lecture 4
22
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
23
Examples
(eval 3 '(()))
3
(eval '((lambda (x) x) 3) ‘(()))
3
(eval '(((lambda (x) (lambda (y) (+ x y))) 3)
4) ‘(()))
;No binding for x
30 Jan 2001
CS 655: Lecture 4
24
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
25
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
26
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
27
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
28
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
29
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)))))))
30 Jan 2001
CS 655: Lecture 4
30
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
31
Charge
• Problem Set 1 due Thursday
• Source code from today is on web site
• Next time (and PS2): Changing MiniScheme
• 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
32