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:

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.
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.
Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
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:
Metacircular Evaluation SICP Chapter 4 Mark Boady.
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 First interactive, interpreted language Dynamic typing: values have types, variables.
EOPL3: Section 3.3 PROC and App B: SLLGEN
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.
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
5.4.1: Implementation Method Invocation (define eval-expression (lambda (exp env) (cases expression exp... (method-app-exp (obj-exp method-name rands)
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.
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:
Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types Recall inductive BNF definition of binary trees: ::= | ( ) To write programs using.
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
Inductively Defined Data Concrete and Abstract syntax Karl Lieberherr.
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.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Abstract Syntax cs7100 (Prasad) L7AST.
The interpreter.
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Corky Cartwright January 18, 2017
Implementing Recursion
Env. Model Implementation
5.4.1: Implementation Method Invocation
2.3 Representation Strategies for Data Types
Semantics of PLs via Interpreters: Getting Started
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
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)
6.001 SICP Further Variations on a Scheme
Extending our interpreted language with Scheme’s set!
Chapter 1 Review: BNF Grammar for lists:
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Chapter 3: Environment-Passing Interpreters
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
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Principles of Programming Languages
Inductively Defined Data Concrete and Abstract syntax
Presentation transcript:

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: let f = proc (y, z) +(y, -(z, 5)) in (f 2 28)

3.5 Procedures Syntax: ::= proc ( { }* (,) ) proc-exp (ids body) ::= ( { }* ) app-exp (rator rands)

3.5 Procedures: Step 1 Add to grammar: (define grammar-3-5 '((program (expression) a-program) (expression (number) lit-exp)... (expression ( "proc" "(" (separated-list identifier ",") ")" expression) ; body proc-exp) (expression ( "(" expression ; ‘rator (arbno expression) ; ‘rands ")") app-exp)

3.5 Procedures: Step 2 Extend datatype definition for expression (not in book): (define-datatype expression expression?... (proc-exp (ids (list-of symbol?)) (bodyexpression?)) (app-exp (rator expression?) (rands (list-of expression?))

3.5 Procedures: Step 3 Need to add some code to eval-expression But first we must understand procedure semantics: let x = 5 in let f = proc(y, z) +(y, -(z,x)) x = 28 in (f 2 x) Which value of x (5 or 28) is used in the let f... line? Which value of x (5 or 28) is used in the last line? Why?

3.5 Procedures let x = 5 in let f = proc(y, z) +(y, -(z,x)) x = 28 in (f 2 x) The value x = 28 is used in the last line. The value x = 5 is used in the second-to-top line, because that is the most recent definition “seen” by the definition of f. Again, we say that x is free in f.

3.5 Procedures Together, a procedure and its free variables form a closure: a “package” containing the proc and free vars: (define-datatype procval procval? (closure (ids (list-of symbol?)) (body expression?) (env environment?))); free vars We use this code as a constructor in eval-expression :

3.5 Procedures (define eval-expression (lambda (exp env) (cases expression exp... (proc-exp (ids body) (closure ids body env)) Now we can handle a procedure application:

3.5 Procedures (define eval-expression (lambda (exp env) (cases expression exp... (app-exp (rator rands) (let ((proc (eval-expression rator env)) (args (eval-rands rands env))) (if (procval? proc) (apply-procval proc args) (eopl:error 'eval-expression “Applied non-procedure ~s” proc))))

3.5 Procedures (define apply-procval (lambda (proc args) (cases procval proc ;only one case (closure (ids body env) (eval-expressionbody (extend-env ids args env))))))

apply-procval : Example let x = 3 in let f = proc(y) +(x, y) in (f 2) ids: ‘(y) body: env:x: 3 (eval-expression (extend-env ‘(y) ‘(2) ‘((x 3)))) (eval-expression ‘((y 2) (x 3)))