CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
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.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CS 536 Spring Run-time organization Lecture 19.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 312 Spring 2002 Lecture 16 The Environment Model.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Imperative Programming
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Compiler Construction
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.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
1 Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002 ML (Meta Language) a brief introduction Alex Proctor and Brian Lee For CSCI 431 at the University.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
1 Bindings. 2 Outline Preliminaries Scope  Block structure  Visibility Static vs. dynamic binding Declarations and definitions More about blocks The.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
CMSC 330: Organization of Programming Languages Operational Semantics.
The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are.
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Operational Semantics of Scheme
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
The Environment Model*
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
Mini Language Interpreter Programming Languages (CS 550)
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
FP Foundations, Scheme In Text: Chapter 14.
The Metacircular Evaluator
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
Scoping and Binding of Variables
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
6.001 SICP Variations on a Scheme
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application.
CSE 341 Lecture 11 b closures; scoping rules
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
CSE S. Tanimoto Lambda Calculus
Recursive Procedures and Scopes
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Chapter 3 Discussion Pages
Presentation transcript:

CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures

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 meaningful type for the expression 3.ML evaluates expression to compute value Of the same “type” found in step 2 Expressions (Syntax)Values (Semantics) Types Compile-time “Static” Exec-time “Dynamic”

Recap Variables are names for values –Environment: dictionary/phonebook –Most recent binding used –Entries never changed, new entries added Environment frozen at fun definition Re-binding variables cannot change a function Same I/O behavior at every call

Next: Functions Expressions Values Types Q: What’s the value of a function ? Q: What’s a function expression ? Q: What’s the type of a function ?

Functions Expressions Two ways of writing function expressions: 1. Named functions: 2. Anonymous functions: Body Expr let fname = fun x -> e let fname x = e fun x => e Parameter (formal) Body Expr Parameter (formal)

Function Application Expressions Application: fancy word for “call” Function value e1 Argument e2 “apply” argument e2 to function value e1 (e1 e2)

Functions Type The type of any function is: T1 : the type of the “input” T2 : the type of the “output” T1 -> T2 let fname = fun x -> e T1 -> T2 let fname x = e T1 -> T2

Functions Type The type of any function is: T1 : the type of the “input” T2 : the type of the “output” T1, T2 can be any types, including functions! T1 -> T2

Function Type Examples What is the type of… int -> int (int -> int) -> int fun x -> x + 1 fun f -> f 0 + 1

of function application Application: fancy word for “call” “apply” argument e2 to function value e1 Type (e1 e2)

of function application Application: fancy word for “call” “apply” argument e2 to function value e1 if has type and has type then has type (e1 e2) Type T1 -> T2 T1 T2 e1 e2 (e1 e2)

Functions Values Two questions about function values: What is the value: 1. … of a function ? 2. … of a function “application” (call) ? (e1 e2)

of functions: Closures Values “Body” expression not evaluated until application –but type-checking takes place at compile time –i.e. when function is defined Function value = – –“closure” # let x = 2+2;; val x : int = 4 # let f = fun y -> x + y;; val f : int -> int = fn x 4 : int Environment at f’s definition: Value of f (closure): f fn x + y, >

of functions: Closures Values “Body” expression not evaluated until application –but type-checking takes place at compile time –i.e. when function is defined Function value = – –“closure” # let x = 2+2;; val x : int = 4 # let f = fun y -> x + y;; val f : int -> int = fn # let x = x + x ;; val x : int = 8 # f 0;; val it : int = 4 x 4 : int f fn : int->int x 8 : int Binding used to eval (f …) Binding for subsequent x

Free (vs. Bound) Variables let a = 20;; let f x = let y = 1 in let g z = y + z in a + (g x) ;; f 0;; Inside a function: A “bound” occurrence: 1. Formal variable 2. Variable bound in let-in x, y, z are “bound” inside f A “free” occurrence: Not bound occurrence a is “free” inside f Environment at definition, frozen inside “closure”, is used for values of free variables Binding

Nested function bindings let a = 20;; let f x = let a = 1 in let g z = a + z in a + (g x) ;; f 0; Inside a function: A “bound” occurrence: 1. Formal variable 2. Variable bound in let-in-end x, a, z are “bound” inside f A “free” occurrence: Not bound occurrence nothing is “free” inside f Environment at definition, frozen inside “closure”, is used for values of free variables

Nested function bindings Bound variable values determined when fun evaluated (“executed”) From arguments Local variable bindings Obtained from evaluation let a = 20;; let f x = let a = 1+1 in let g z = a + z in a + (g x) ;; f 0; Q: Where do values of bound variables come from ?

of function application Values Application: fancy word for “call” “apply” the argument e2 to the (function) e1 Application Value: 1. Evaluate e1 in current env to get (function) v1 –v1 is code + env –code is (formal x + body e ), env is E 2. Evaluate e2 in current env to get (argument) v2 3. Evaluate body e in env E extended by binding x to v2 (e1 e2)

Example 1 let x = 1;; let f y = x + y;; let x = 2;; let y = 3;; f (x + y);;

Example 2 let x = 1;; let f y = let x = 2 in fun z -> x + y + z ;; let x = 100;; let g =(f 4);; let y = 100;; (g 1);;

Example 3 let f g = let x = 0 in g 2 ;; let x = 100;; let h y = x + y;; f h;;

Example 4 let a = 20;; let f x = let y = 10 in let g z = y + z in a + (g x) ;; f 0;

Function/Application Values and Types int -> int fun x -> x / 0 (fun x -> x / 0) 10 int This expression has no value!

Static/Lexical Scoping For each occurrence of a variable, –Unique place in program text where variable defined –Most recent binding in environment Static/Lexical: Determined from the program text –Without executing the program Very useful for readability, debugging: –Don’t have to figure out “where” a variable got assigned –Unique, statically known definition for each occurrence

Dynamic Scopying let x = 1;; let f y = let x = 2 in fun z -> x + y + z ;; let x = 100;; let g =(f 4);; let y = 100;; (g 1);;

Dynamic Scoping let x = 0;; let f () = x;; let g () = let x = 10 in f ()

Static vs. Dynamic Scoping Any opinions? Analogies to other language features? Know any languages that use dynamic? What does Python do?