The interpreter.

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.
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.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Evaluators for Functional Programming Chapter 4 1 Chapter 4 - Evaluators for Functional Programming.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
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).
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.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 Subt. model interpreter: Structure of the interpreter ASP Derived expressions Core Test Data structures Utils A racket lib Proc / Primitive-Proc. Global.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
The environment-based operational semantics Chapter
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Lecture 14: Assignment and the Environment Model (EM)
1 Subt. model interpreter: Introduction Abstract Syntax Parser Abstract Syntax Parser Derived Expressions Core Test Data Structures Utils (a racket lib)
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
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?
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Evaluators for Functional Programming 1. How to describe (specify) a programming language? 1.Syntax: atoms, primitives, combination and abstraction means.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Operational Semantics of Scheme
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
The role of abstractions
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
6.001 SICP Data abstractions
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
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Lecture 2 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
topics interpreters meta-linguistic abstraction eval and apply
Lecture 2 מבוא מורחב.
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

The interpreter

Derived Expressions (define derived? (lambda (exp) (or (if? exp) (function-definition? exp) (let? exp))))

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)))))

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)))

Concrete derivation

Cont.

let

Function-definition

Reminder

Core (applicative-substitution) Language expressions ASP The global environment Data structures+core Values Data structures+ core

We will start with the data structures, that actually do most of the work Then the core – evaluation rules will be rather easy

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].

Usage

(User) Procedure (define (make-procedure pars body) (attach-tag (cons pars body) ‘procedure)) …..

Other values (define (make-value x) (attach-tag (list x) ‘value))

Environment

frame

Initialization with primitives

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

Global-env constructor

Defining the global-env (define the-global-environment (make-the-global-environment)) This expression is in the global scope of the interperter…

Selector:lookup

MUTATOR: add-binding!

Binding ADT

Using the environment

Eval-apply loop (applicative-eval exp) (apply-procedure proc args) Mutually recursive (why?)

eval

Eval-atomic

Lambda+definitions

If

application

Apply-primitive

substitute

“main” code of substitute

rename(exp)

“main” code of rename

Environment Model Formal algorithm Interpreter

Environment Model (Diagram)

Dynamic env-model

Interpreter for env. model What should be changed?

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.

env

lookup

Adding binding in global env

Core:eval

Every eval now has env argument

apply-procedure

apply-procedure (cont.)

Dynamic-env interpreter What would you change?