3.6 Interpreter: Recursion

Slides:



Advertisements
Similar presentations
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Advertisements

Cs784(Prasad)L10Rec1 Implementing Recursion. cs784(Prasad)L10Rec2 let insufficient for recursion ( (let((fact(lambda (n) (if (zero? n) 1 (* n (fact (-
Closures & Environments CS153: Compilers Greg Morrisett.
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Letrec fact(n) = if zero?(n) then 1 else *(n, (fact sub1(n))) 4.4 Type Inference Type declarations aren't always necessary. In our toy typed language,
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.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
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.
3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let ): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question:
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.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
EOPL3: Section 3.3 PROC and App B: SLLGEN
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
Environment: Data type associating symbols with values { a: 4s:'foof: x.x } Interface: (empty-env) = [ Ø ] (apply-env [f] s ) = f(s) (extend-env '( s 1...
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
4.2 Type Checking (type-of-expression > tenv ) = bool (type-of-expression > tenv ) = x ( type-of-expression if > tenv ) = x Recall type of if statement:
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Example 1: counter (set! vs set-box!) 1 Imperative Programming: Mutable data and local state (define counter (let ((count 0)) (lambda () (set! count (+
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
3.3 Conditional Evaluation --> if 1 then 2 else 3 2 Want to support if/then/else We'll represent false as 0, true as 1: Syntax (BNF): ::= if then else.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Scheme in Scheme 1.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Abstract Syntax cs7100 (Prasad) L7AST.
6.001 SICP Variations on a Scheme
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
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.
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
5.4.1: Implementation Method Invocation
2.3 Representation Strategies for Data Types
Semantics of PLs via Interpreters: Getting Started
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
And more store-passing interpreters
Procedures App B: SLLGEN 1.
3.7 Variable Assignment Recall instance variables in Python:
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
Continuation Marks A john clements talk.
6.001 SICP Further Variations on a Scheme
Extending our interpreted language with Scheme’s set!
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Chapter 3: Environment-Passing Interpreters
Assignments cs7100(Prasad) L13Assg.
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
Principles of Programming Languages
Presentation transcript:

3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question: Why letrec and not let ?

3.6 Recursion Implement letrec for procedures in our toy language: --> letrec fact(x)= if zero?(x) then 1 else *(x, (fact sub1(x))) in (fact 6) 720 --> We'll use letrec only for procedures....

3.6 Recursion Syntax: <expression> ::= letrec {<identifier>({<identifier>}*(,)) = <expression>}* in <expression> letrec-exp (proc-names idss bodies letrec-body) Question: Why idss and not ids ?

3.6 Recursion Add some code to eval-expression: (define eval-expression (lambda (exp env) (cases expression exp ... (letrec-exp (proc-names idss bodies letrec-body) (eval-expression letrec-body (extend-env-recursively proc-names idss bodies env))) ...))) So how to write extend-env-recursively ?

3.6 Recursion First augment environment type to include recursive procs: (define-datatype environment environment? (empty-env-record) (extended-env-record (syms (list-of symbol?)) (vals (list-of scheme-value?)) (env environment?)) (recursively-extended-env-record (proc-names (list-of symbol?)) (idss (list-of (list-of symbol?))) (bodies (list-of expression?)) (env environment?)))

3.6 Recursion Now write extend-env-recursively as a constructor: (define extend-env-recursively (lambda (proc-names idss bodies env) (recursively-extended-env-record proc-names idss bodies env))) Recall that we look up symbols using apply-env: (define eval-expression (lambda (exp env) (cases expression exp (var-exp (id) (apply-env env id)

3.6 Recursion Current version: (define apply-env (lambda (env sym) (cases environment env (empty-env-record () (eopl:error ...)) (extended-env-record (syms vals old-env) (let ((pos (list-find-pos sym syms))) (if (number? pos) ; found (list-ref vals pos) (apply-env old-env sym)))))))

3.6 Recursion (define apply-env (lambda (env sym) (cases environment env ... (recursively-extended-env-record (proc-names idss bodies old-env) (let ((pos (list-find-pos sym proc-names))) (if (number? pos); found (closure (list-ref idss pos) (list-ref bodies pos) env) ; circular! (apply-env old-env sym)))))));notfound

3.6 Recursion For our fact example: proc-names = '(fact) idss = '((x)) bodies = '( <<if zero?(x) then 1 else *(x, fact sub1(x))>> ) env = '( (fact) (<closure: fact, x, <<if...) )

3.6 Recursion fact closure(x) <<if zero? (x) then 1 else fact sub1(x)>>