topics interpreters meta-linguistic abstraction eval and apply

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
(Thunking about Thunks)
Scheme in Scheme 1.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Functional Programming Languages
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Original material by Eric Grimson
Nondeterministic Evaluation
The Metacircular Evaluator
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
6.001 SICP Environment model
topics three representations entity relationship diagram object model
The Metacircular Evaluator (Continued)
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Environment model
overview today’s ideas set-car! and set-cdr!
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

6001 structure & interpretation of computer programs recitation 15/ november 14, 1997

topics interpreters meta-linguistic abstraction eval and apply the environment model in code 5/16/2019 daniel jackson

puzzle for student presentation build a “shell” with history that works like this: (define m (mk-shell)) (m ‘execute p) ; executes the procedure p and sets p to be the current proc (m ‘back) ; sets the current proc back one (m ‘forward) ; sets the current proc forward one (m ‘run) ; executes the current proc hint use the circular buffer we designed last time! 5/16/2019 daniel jackson

meta-linguistic abstraction ways to build a big system break it into smaller pieces invent a new language why invent a new language? new way to think about the system tailored to the problem at hand code can be much more succinct examples the Unix shell Mathematica Excel spreadsheet macros 5/16/2019 daniel jackson

a very simple interpreter an arithmetic evaluator code (define (arith-eval exp) (cond ((pair? exp) (arith-apply (cadr exp) (arith-eval (car exp)) (arith-eval (caddr exp)))) (else exp))) (define (arith-apply op x y) (cond ((eq? op ‘*) (* x y)) ((eq? op ‘+) (+ x y))) sample run (define e ‘(2 * (3 + 4))) (arith-eval e) ==> 14 question what will arith-apply look like if there are lots of primitive ops? how could it be coded more elegantly? 5/16/2019 daniel jackson

essential idea: recursive evaluation even in this trivial example we have the fundamental structure of a LISP interpreter recursive evaluation recall our rule for evaluation to evaluate a combination expression evaluate the subexpressions, and apply the value of the operator subexpressions to the values of the operand subexpressions a primitive expression – a number – evaluates to itself and in the better version of arith-apply namely (define (arith-apply op x y) (apply (lookup op op-table) (list x y))) we have the notion of an environment (but a crude one: it’s fixed!) 5/16/2019 daniel jackson

environments what does Scheme have that our arithmetic expression language doesn’t? procedures variables and environments these are interwoven because environments are needed to execute procedures basic idea of parameter binding: evaluate the body in an environment that has the parameters bound to the argument values lexical scoping recall that in Scheme a procedure carries with it the environment in which it was created and the environment in which its body is evaluated is an extension of this environment this is not so essential (but it’s very nice) 5/16/2019 daniel jackson

an evaluator for scheme the code (define mc-eval (lambda (exp env) (cond ((number? exp) exp) ;base-case ((symbol? exp) (lookup exp env)) ;base case ((eq? (car exp) 'quote) (car (cdr exp))) ;special forms ((eq? (car exp) 'cond) (evcond (cdr exp) env)) ((eq? (car exp) 'begin) (evseq (cdr exp) env)) ((eq? (car exp) 'lambda) (list 'proc (cdr exp) env)) ((eq? (car exp) 'define) (evdefine (cdr exp) env)) (else (mc-apply (mc-eval (car exp) env) (evlist (cdr exp) env)))))) features to look out for the recursive structure: where are the base cases? the call to mc-apply how procedures are represented how the environment is passed through 5/16/2019 daniel jackson

an applier for scheme the code (define mc-apply (lambda (fun args) (cond ((not (pair? fun)) (apply fun args)) ;ground out ((eq? (car fun) 'proc) (mc-eval (car (cdr (car (cdr fun)))) ; procedure body (bind (car (car (cdr fun))) ;formal params args ;supplied args (car (cdr (cdr fun)))))) ;saved env (else (error '"Unknown function"))))) features to look out for the recursive structure: where are the base cases? the call to mc-eval how procedures are applied how the environment is passed through 5/16/2019 daniel jackson

playing with the interpreter evaluate these expressions 1 (+ 3 4) (+ (* 3 4) (* 5 6)) ((lambda (x) (* x x)) 3) ((lambda (x) (lambda (y) (+ x y))) 3) 4) 5/16/2019 daniel jackson

filling in the details: sequences how sequences are evaluated (define evseq (lambda (clauses env) (cond ((null? (cdr clauses)) (mc-eval (car clauses) env)) (else (mc-eval (car clauses) env) (evseq (cdr clauses) env))))) can you add the and special form? definition: to evaluate (and e1 e2 … en) evaluate e1 to v1 if v1 is false, the value is v1, else continue if all vi are true, value is vn code (define evand (lambda (clauses env) ((mc-eval (car clauses) env) (evand (cdr clauses) env)) (else #f))) 5/16/2019 daniel jackson

filling in the details: define code (define evdefine (lambda (body env) ;mutate the first frame (begin (set-cdr! (car env) (cons (cons (car body) (mc-eval (car (cdr body)) env)) (cdr (car env)))) (car body)))) 5/16/2019 daniel jackson

meta-circularity this interpreter is a scheme interpreter written in scheme! can be viewed as a semantics: we can read to understand how scheme evaluates expressions figure out where these features are evident in the code applicative order evaluation: all the arguments of a procedure are evaluated before the call lexical scoping shadowing of variables 5/16/2019 daniel jackson

puzzle for student presentation implement an evaluator for a simple assignment language takes a list of statements a statement is either an assignment (var := expr) or a print statement (print expr) hint use arith-eval modified to include a simple environment 5/16/2019 daniel jackson