Closure and Environment Compiler Baojian Hua

Slides:



Advertisements
Similar presentations
Transposing F to C Transposing F to C Andrew Kennedy & Don Syme Microsoft Research Cambridge, U.K.
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Closures & Environments CS153: Compilers Greg Morrisett.
Data Structure & Abstract Data Type
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
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.
Standard ML- Part I Compiler Baojian Hua
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 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 17 Structs, Implementing Languages, Implementing Higher-Order Functions Dan Grossman Fall 2011.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Function Compiler Baojian Hua Function, or Procedure, or method, or … High-level abstraction of code logically-grouped Good for many.
Functions C and Data Structures Baojian Hua
Data Abstraction COS 441 Princeton University Fall 2004.
Abstract Syntax Trees Compiler Baojian Hua
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
C and Data Structures Baojian Hua
Elaboration or: Semantic Analysis Compiler Baojian Hua
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
CS 312 Spring 2002 Lecture 16 The Environment Model.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Pointers and Arrays C and Data Structures Baojian Hua
Code Generation Compiler Baojian Hua
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Polymorphism C and Data Structures Baojian Hua
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
CSE341: Programming Languages Structs, Implementing Languages, Implementing Higher-Order Functions Alan Borning (slides “borrowed” from Dan Grossman) Fall.
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
Advanced Functional Programming Tim Sheard 1 Lecture 18 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
A Staged Semantics of Overloading (preliminary) Bill Harrison & Tim Sheard.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CSE 5317/4305 L12: Higher-Order Functions1 Functional Languages and Higher-Order Functions Leonidas Fegaras.
CMSC 330: Organization of Programming Languages Operational Semantics.
Environments, Stores, and Interpreters. Overview As we study languages we will build small languages that illustrate language features We will use two.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Closure Compiler Baojian Hua Closure An implementation technique traditionally for functional languages Lisp, Scheme, Haskell, Ocaml,
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Winter 2013.
CSE 374 Programming Concepts & Tools
ML: a quasi-functional language with strong typing
Introduction to Parsing (adapted from CS 164 at Berkeley)
CS4450: Principles of Programming Languages
Closure conversion Compiling λ.
Programming Languages and Compilers (CS 421)
Tim Sheard Oregon Graduate Institute
Programming Languages and Compilers (CS 421)
Closure Representations in Higher-Order Programming Languages
Continuations and Compilation
CSE 341 Section 5 Winter 2018.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application.
Environments, Stores, and Interpreters
CSE 341 Lecture 11 b closures; scoping rules
Scope, Function Calls and Storage Management
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Closure and Environment Compiler Baojian Hua

Higher-order functions (HOF) Higher-order functions are not just for call can be passed as arguments can be returned as results can be stored in data structures objects! we ’ d discuss later If functions don ’ t nest, then the implementation is simple a simple code address e.g., the “ function pointer ” in C What about nesting with HOF?

Nesting (* ML code. *) (* Staging. Harper, section 11.5: *) val add = fn x => (fn y => x + y) val inc = add 1 (* fn y => 1 + y *) (* Can be more abstract: *) val bop = fn f => (fn x => (fn y => f (x, y))) val add = fn (op +) val inc = add 1

Nesting // C code. // GNU C extension supports this. But its // implementation is rather limited and incorrect! int (*(add)(int x))(int) { int f (int y) { return x + y; } return f; } // Don’t expect GCC behaves normally, hummmm… int (*inc) (int) = add (1);

Moral Nested HOL is a key feature in modern functional programming languages And now has grown into other language e.g., C# and Java7 Key observation: function arguments and locals can NOT be reclaimed after the call fn x => (fn y => x + y) they may be used in the returned nested function!

Closure A closure is a data structure to represent a function at runtime A closure is essentially a heap-allocated struct/tuple containing a code pointer, as well as a (L-)values for the function ’ s free variables (environment) The process of converting a function to a closure is called closure conversion

Lambda again e -> n -> x -> \x.e -> e e // or in ML: datatype e = Int of int | Var of string | Lam of string * e | App of e * e

Rules C (n) = n C (x) = #x env C (\x.e) = let fun f (env_t, x) = let val x1 = #x1 env_t … val xn = #xn env_t val env’ = {x=x, x1=x1, …, xn=xn} in C (e) end in (f, env) end C (e1 e2) = C(e1) C(e2)

Example #1 // for code: \x.x // the initial call: C (\x.x) = let fun f (env_t, x) = let val env’ = {x = x} in C (x) end in (f, env) end

Recursive transformation // for code: \x.x // the initial call: C (\x.x) = let fun f (env_t, x) = let val env = {x = x} in #x env end in (f, env) end

Hoisting // for code: \x.x // hoist all code to top-level: fun f (env_t, x) = let val env = {x = x} in #x env end (f, {})

Function call // consider the code: (\x.x) 5 // \x.x as before: fun f (env_t, x) = let val env’ = {x = x} in #x env end (f, env) // Leave it to you to verify the whole becomes: (f, env) 5 // and the call becomes: f (env, 5)

Summary so far Three steps in closure conversion: apply the conversion rules to make every function closed a function become a closure: (code, env) Hoisting: all functions at top-level like those in C function call become closure application (code, env) x ==> code (env, x)

Example #2 // code: \x.\y.x+y // conversion: C (\x.\y.x+y) = let fun f1 (env_t, x) = let val env = {x = x} in C (\y.x+y) end in (f1, env) end // Leave to you to finish other steps!

Example #2 // code: \x.\y.x+y // final result: fun f2 (ent_t, y) = let val x = #x ent_t val env = {x=x, y=y} in #x env + #y env end fun f1 (env_t, x) = let val env = {x = x} in (f2, env) end (f1, env)

In picture // for \x.\y.\z.x+y+z f1 /\ f2 f3

Linked vs flatten closure The flatten model of closure is bad: it evolves too much “ copy ” operations it ’ s space inefficient Instead, we could make the closure linked by sharing environment just as the static link

Linked Environment // revised rules: C (\x.e) = let fun f (env_t, x) = let val env = Cons (env_t, x) in C (e) end in (f, env) end

In picture // for \x.\y.\z.x+y+z f1 /\ f2 f3