Download presentation
Presentation is loading. Please wait.
1
The interpreter
2
Derived Expressions (define derived? (lambda (exp) (or (if? exp) (function-definition? exp) (let? exp))))
3
Shallow-derive (define shallow-derive (lambda (exp) (cond ((if? exp) (if->cond exp)) ((function-definition? exp) (function-define->define exp)) ((let? exp) (let->combination exp)) (else (error ’shallow-derive "unhandled derivation: ~s" exp)))))
4
Recursive derivation (define (derive exp) (if (atomic? exp) exp (let ((mapped-derive-exp (map derive exp))) (if (not (derived? exp)) mapped-derive-exp (shallow-derive mapped-derive-exp)))
5
Concrete derivation
7
Cont.
8
let
9
Function-definition
10
Reminder
11
Core (applicative-substitution)
Language expressions ASP The global environment Data structures+core Values Data structures+ core
12
We will start with the data structures, that actually do most of the work
Then the core – evaluation rules will be rather easy
13
Values The Primitive-procedure ADT: 1. Constructor make-primitive-procedure: Attaches a tag to an implemented code argument. Type: [T -> Primitive-procedure]. 2. Identification predicate primitive-procedure?. Type: [T –> Boolean]. 3. Selector primitive-implementation: It retrieves the implemented code from a primitive procedure value. Type: [Primitive-procedure –> T].
14
Usage
15
(User) Procedure (define (make-procedure pars body)
(attach-tag (cons pars body) ‘procedure)) …..
16
Other values (define (make-value x) (attach-tag (list x) ‘value))
17
Environment
18
frame
19
Initialization with primitives
20
Before introducing the constructor: A word on mutation
So far we kept our code fully functional That is, no state required “define” is somewhat an exception, but mainly for convenience Recursive function definition is an exception to that, can be done without define, but requires fixpoint operation For implementing the global environment we want to be able to change bindings A functional solution is possible but messy We use an abstraction of mutation through the Box ADT of Dr. Racket It basically “wrappes” the object and allows (sort of ) a “pointer” to it
21
Global-env constructor
23
Defining the global-env
(define the-global-environment (make-the-global-environment)) This expression is in the global scope of the interperter…
24
Selector:lookup
25
MUTATOR: add-binding!
26
Binding ADT
27
Using the environment
28
Eval-apply loop (applicative-eval exp) (apply-procedure proc args) Mutually recursive (why?)
29
eval
30
Eval-atomic
31
Lambda+definitions
32
If
33
application
34
Apply-primitive
35
substitute
38
“main” code of substitute
39
rename(exp)
41
“main” code of rename
42
Environment Model Formal algorithm Interpreter
43
Environment Model (Diagram)
46
Dynamic env-model
47
Interpreter for env. model
What should be changed?
48
List of main changes ASP stays intact Data structures: Core:
Support an Environment ADT Procedure ADT should include an environment pointer Core: Every eval function gets an environment as an additional parameter Uses it e.g. for lookup, and passes it on to recursive eval and apply calls apply of user procedures (closures) change to manage envs.
50
env
51
lookup
52
Adding binding in global env
53
Core:eval
54
Every eval now has env argument
55
apply-procedure
56
apply-procedure (cont.)
58
Dynamic-env interpreter
What would you change?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.