The interpreter
description
Transcript of The interpreter
![Page 1: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/1.jpg)
The interpreter
![Page 2: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/2.jpg)
Derived Expressions
(define derived? (lambda (exp) (or (if? exp) (function-definition? exp) (let?
exp))))
![Page 3: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/3.jpg)
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)))))
![Page 4: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/4.jpg)
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)))
![Page 5: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/5.jpg)
Concrete derivation
![Page 6: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/6.jpg)
![Page 7: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/7.jpg)
Cont .
![Page 8: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/8.jpg)
let
![Page 9: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/9.jpg)
Function-definition
![Page 10: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/10.jpg)
Reminder
![Page 11: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/11.jpg)
Core (applicative-substitution)
• Language expressions– ASP
• The global environment– Data structures+core
• Values– Data structures+ core
![Page 12: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/12.jpg)
• We will start with the data structures, that actually do most of the work
• Then the core – evaluation rules will be rather easy
![Page 13: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/13.jpg)
Values
The Primitive-procedure ADT:1. Constructor make-primitive-procedure: Attaches a tag to
an implemented codeargument.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].
![Page 14: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/14.jpg)
Usage
![Page 15: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/15.jpg)
(User )Procedure
• (define (make-procedure pars body)(attach-tag (cons pars body) ‘procedure))
..…
![Page 16: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/16.jpg)
Other values
(define (make-value x) (attach-tag (list x) ‘value))
![Page 17: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/17.jpg)
Environment
![Page 18: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/18.jpg)
frame
![Page 19: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/19.jpg)
Initialization with primitives
![Page 20: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/20.jpg)
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
![Page 21: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/21.jpg)
Global-env constructor
![Page 22: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/22.jpg)
![Page 23: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/23.jpg)
Defining the global-env
(define the-global-environment (make-the-global-environment))
This expression is in the global scope of the interperter…
![Page 24: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/24.jpg)
Selector:lookup
![Page 25: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/25.jpg)
MUTATOR:add-binding!
![Page 26: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/26.jpg)
Binding ADT
![Page 27: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/27.jpg)
Using the environment
![Page 28: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/28.jpg)
Eval-apply loop
(applicative-eval exp)(apply-procedure proc args)
Mutually recursive (why?)
![Page 29: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/29.jpg)
eval
![Page 30: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/30.jpg)
Eval-atomic
![Page 31: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/31.jpg)
Lambda+definitions
![Page 32: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/32.jpg)
If
![Page 33: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/33.jpg)
application
![Page 34: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/34.jpg)
Apply-primitive
![Page 35: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/35.jpg)
substitute
![Page 36: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/36.jpg)
![Page 37: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/37.jpg)
![Page 38: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/38.jpg)
“main” code of substitute
![Page 39: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/39.jpg)
rename(exp)
![Page 40: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/40.jpg)
![Page 41: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/41.jpg)
“main” code of rename
![Page 42: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/42.jpg)
Environment Model
• Formal algorithm
• Interpreter
![Page 43: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/43.jpg)
Environment Model (Diagram)
![Page 44: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/44.jpg)
![Page 45: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/45.jpg)
![Page 46: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/46.jpg)
Dynamic env-model
![Page 47: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/47.jpg)
Interpreter for env. model
• What should be changed?
![Page 48: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/48.jpg)
List of main changes
• ASP stays intact• Data structures: – 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.
![Page 49: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/49.jpg)
![Page 50: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/50.jpg)
env
![Page 51: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/51.jpg)
lookup
![Page 52: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/52.jpg)
Adding binding in global env
![Page 53: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/53.jpg)
Core:eval
![Page 54: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/54.jpg)
Every eval now has env argument
![Page 55: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/55.jpg)
apply-procedure
![Page 56: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/56.jpg)
apply-procedure (cont.)
![Page 57: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/57.jpg)
![Page 58: The interpreter](https://reader031.fdocuments.us/reader031/viewer/2022020717/568167a6550346895ddcf2da/html5/thumbnails/58.jpg)
Dynamic-env interpreter
• What would you change?