News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Similar documents
Recap: Functions as first-class values

Variables and Bindings

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Programming Languages

Programming Languages

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Programming Languages

Begin at the beginning

Programming Languages

Programming Languages

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Today s Plan. Programming Languages. Example : Factorial. Recursion. CSE 130 : Spring Lecture 6: Higher-Order Functions

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions

Write: evens. evens [] ====> [] evens [1;2;3;4] ====> [2;4]

Programming Languages

Programming Languages

Programming Languages

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

max function Next concat function max function What s the pattern? concat function More on recursion Higher-order functions

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Programming Languages

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

Today. Recap from last Week. Factorial. Factorial. How does it execute? How does it execute? More on recursion. Higher-order functions

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Factorial. Next. Factorial. How does it execute? Tail recursion. How does it execute? More on recursion. Higher-order functions

CSE 341 Section 5. Winter 2018

Next. Tail Recursion: Factorial. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial.

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Programming Languages

Programming Languages Lecture 14: Sum, Product, Recursive Types

Lambda Calculus.

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

Programming Languages and Compilers (CS 421)

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Scope, Functions, and Storage Management

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Programming Languages

FOFL and FOBS: First-Order Functions

CS558 Programming Languages

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

So what does studying PL buy me?

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Programming Languages

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

Variables. Substitution

Programming Languages

Scope and Parameter Passing 1 / 19

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Functional Programming. Pure Functional Programming

CSE341, Spring 2013, Final Examination June 13, 2013

CSE 130 [Winter 2014] Programming Languages

Programming Project #3: Syntax Analysis

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

Parsing Scheme (+ (* 2 3) 1) * 1

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

Introduction to OCaml

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Denotational Semantics. Domain Theory

G Programming Languages - Fall 2012

CPSC W1 University of British Columbia

61A Lecture 2. Friday, August 28, 2015

CSE 341 : Programming Languages

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

Hofl, a Higher-order Functional Language. 1 An Overview of Hofl. 2 Abstractions and Function Applications

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

CSE 130 [Winter 2014] Programming Languages

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

Programming Languages

CSE341, Fall 2011, Midterm Examination October 31, 2011

61A Lecture 2. Wednesday, September 4, 2013

To figure this out we need a more precise understanding of how ML works

CSE341, Fall 2011, Midterm Examination October 31, 2011

Transcription:

CSE 130: Programming Languages Environments & Closures News PA 3 due THIS Friday (5/1) Midterm NEXT Friday (5/8) Ranjit Jhala UC San Diego Recap: Functions as first-class values Arguments, return values, bindings What are the benefits? Parameterized, similar functions (e.g. Testers) Creating, (Returning) Functions Iterator, Accumul, Reuse computation pattern w/o Using, eposing local info (Taking) Functions Functions are first-class values Arguments, return values, bindings What are the benefits? Parameterized, similar functions (e.g. Testers) Creating, (Returning) Functions Iterator, Accumul, Reuse computation pattern w/o Using, eposing local info (Taking) Functions Compose Functions: Fleible way to build Comple functions from primitives.

Funcs taking/returning funcs Higher-order funcs enable modular code Each part only needs local information Map-Reduce et al. Higher-order funcs enable modular code Each part only needs local information Data Structure Client Uses list Data Structure Library list Map-Reduce Client Map-Reduce Library Uses meta-functions: map,fold,filter With locally-dependent funs (lt h), square etc. Without requiring Implement. details of data structure Provides meta-functions: map,fold,filter to traverse, accumulate over lists, trees etc. Meta-functions don t need client info (tester? accumulator?) Web Analytics Queries Clustering, Page Rank, etc as map/reduce + ops Provides: map, reduce to traverse, accumulate over WWW ( Big Data ) Distributed across cloud Higher Order Functions Higher Order Functions Are Awesome.....but how do they work

Net: Environments & Functions Variables and Bindings Epressions Q: How to use variables in ML? Q: How to assign to a variable? Types Lets start with the humble variable... # let = 2+2 val : int = 4 let = e Bind value of epr e to variable Variables and Bindings # let = 2+2 val : int = 4 # let y = * * val y : int = 64 # let z = [;y;+y] val z : int list = [4;64;68] Later epressions can use Most recent bound value used for evaluation Sounds like C/Java? NO Environments ( Phone Book ) How ML deals with variables Variables = names = phone number y 64 : int z [4;64;68] : int list 8 : int

Environments and Evaluation ML begins in a top-level environment Some names bound (e.g. +,-, print_string...) let = e ML program = Sequence of variable bindings Program evaluated by evaluating bindings in order 1. Evaluate epr e in current env to get value v : t 2. Etend env to bind to v : t (Repeat with net binding) Environments Phone book Variables = names = phone number 1. Evaluate: Find and use most recent value of variable 2. Etend: Add new binding at end of phone book Q: What is the value of res? let = 0 let y = + 1 let z = (, y) let = 100 let res = z (a) (0, 1) (b) (100, 101) (c) (0, 100) (d) (1, 100) Eample # let = 2+2 val : int = 4 # let y = * * val y : int = 64 # let z = [;y;+y] val z : int list = [4;64;68] # let = + val : int = 8 New binding 4 : int y 64 : int y 64 : int z [4;64;68] : int list y 64 : int z [4;64;68] : int list 8 : int

Q: What is the value of res? let = 0 let y = + 1 let z a = (, y) let = 100 let res = z [] Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new binding at end (a) (0, 1) (b) (100,101) (c) (0, 100) (d) (100, 1) How is it different from C/Java s store? Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new binding at end How is it different from C/Java s store? # let = 2+2 val : int = 4 # let f = fun y -> + y; val f : int -> int = fn # let = + ; val : int = 8 # f 0; val it : int = 4 Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new binding at end 4 : int f fn <code, >: int->int New binding: No change or mutation Old binding frozen in f How is it different from C/Java s store? # let = 2+2; val : int = 4 # let f = fun y -> + y; val f : int -> int = fn # let = + ; val : int = 8; # f 0; val it : int = 4 4 : int f fn <code, >: int->int f fn <code, >: int->int 8 : int # let = 2+2; val : int = 4 # let f = fun y -> + y val f : int -> int = fn # let = + ; val : int = 8 # f 0; val it : int = 4 Binding used to eval (f ) f fn <code, >: int->int 8 : int Binding for subsequent

Cannot change the world Cannot assign to variables Can etend the env by adding a fresh binding Does not affect previous uses of variable Environment at fun declaration frozen inside fun value Frozen env used to evaluate application (f e) Q: Why is this a good thing? # let = 2+2 val : int = 4 # let f = fun y -> + y val f : int -> int = fn # let = + val : int = 8; # f 0 val it : int = 4 Binding used to eval (f ) f fn <code, >: int->int 8 : int Binding for subsequent Immutability: The Colbert Principle A function behaves the same way on Wednesday, as it behaved on Monday, no matter what happened on Tuesday Cannot change the world Q: Why is this a good thing? A: Function behavior frozen at declaration Cannot change the world Q: Why is this a good thing? A: Function behavior frozen at declaration Nothing entered afterwards affects function Same inputs always produce same outputs Localizes debugging Localizes reasoning about the program No sharing means no evil aliasing

Eamples of no sharing Remember: No addresses, no sharing. Each variable is bound to a fresh instance of a value Tuples, Lists Efficient implementation without sharing? There is sharing and pointers but hidden from you Compiler s job is to optimize code Efficiently implement these no-sharing semantics Your job is to use the simplified semantics Write correct, cleaner, readable, etendable systems Q: What is the value of res? let f = 1 let f = if <2 then 1 else ( * f(-1)) let res = f 5 (a) 120 (b) 60 (c) 20 (d) 5 (d) 1 Function bindings Functions are values, can bind using val let fname = fun -> e Problem: Can t define recursive functions fname is bound after computing rhs value no (or old ) binding for occurences of fname inside e let rec fname = e Occurences of fname inside e bound to this definition let rec fac = if <=1 then 1 else *fac (-1) Q: What is the value of res? let y = let = 10 in + let res = (, y) (a) Synta Error (b) (10,20) (c) (10,10) (d) Type Error

Local bindings So far: bindings that remain until a re-binding ( global ) Local, temporary variables are useful inside functions Avoid repeating computations Make functions more readable let = e1 in e2 Let-in is an epression Evaluating let-in in env E: 1. Evaluate epr e1 in env E to get value v : t 2. Use etended E [ -> v : t] (only) to evaluate e2 Local bindings Evaluating let-in in env E: 1. Evaluate epr e1 in env E to get value v : t 2. Use etended E [ -> v : t] to evaluate e2 let = 10 in * 10 : int E E E[:=10] Let-in is an epression Evaluating let-in in env E: 1. Evaluate epr e1 in env E to get value v : t 2. Use etended E [ -> v : t] to evaluate e2 let y = let = 10 in * 10 : int y 100 : int Nested bindings Evaluating let-in in env E: 1. Evaluate epr e1 in env E to get value v : t 2. Use etended E [ -> v : t] to evaluate e2 let = 10 in (let y = 20 in * y) + 10 : int 10 : int y 20 : int 10 : int

Nested bindings Eample let = 10 in let y = 20 in * y BAD Formatting let = 10 in let y = 20 in * y GOOD Formatting let rec filter f s = match s with [] -> [] ::s -> let ys = if f then [] else [] in let ys = filter f s in ys @ ys Recap 1: Variables are names for values Recap 2: Big Eprs With Local Bindings Environment: dictionary/phonebook Most recent binding used Entries never change New entries added let-in epression Variable in-scope in-epression Outside, variable not in-scope

Recap 3: Env Frozen at Func Definition Re-binding vars cannot change function Indentical I/O behavior at every call Predictable code, localized debugging Static/Leical Scoping For each occurrence of a variable, A unique place where variable was defined Most recent binding in environment Static/Leical: Determined from program tet Without eecuting 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 Net: Functions Epressions Types Q: What s the value of a function? Immutability: The Colbert Principle A function behaves the same way on Wednesday, as it behaved on Monday, no matter what happened on Tuesday

Functions Epressions Function Application Epressions Two ways of writing function epressions: 1. Anonymous functions: 2. Named functions: Parameter (formal) Body Epr let fname = fun -> e Parameter (formal) Body Epr let fname = e Application: fancy word for call (e1 e2) Function value e1 Argument e2 apply argument e2 to function value e1 Functions Type Functions Type The type of any function is: T1 : the type of the input T2 : the type of the output let fname = fun -> e let fname = e T1 -> T2 T1 -> T2 T1 -> T2 The type of any function is: T1 : the type of the input T2 : the type of the output T1->T2 T1, T2 can be any types, including functions Whats an eample of? int -> int int * int -> bool (int -> int) -> (int -> int)

Type of function application Functions Application: fancy word for call (e1 e2) apply argument e2 to function value e1 e1 : T1 -> T2 (e1 e2) : T2 e2 : T1 Argument must have same type as input T1 Result has the same type as output T2 Two questions about function values: What is the value: 1. of a function? fun -> e 2. of a function application (call)? (e1 e2) of function = Closure of function = Closure Two questions about function values: What is the value: 1. of a function? fun -> e Two questions about function values: What is the value: 1. of a function? fun -> e Closure = Code of Fun. (formal + body e) + Environment at Fun. Definition Closure = Code of Fun. (formal + body e) + Environment at Fun. Definition

Q: Which vars in closure of f? of functions: Closures let = 2 + 2 let f y = + y let z = + 1 (a) (b) y (c) y (d) y z (e) None Q: Which vars in closure of f? Function value = Closure <code + environment at definition> Body not evaluated until application But type-checking when function is defined # let = 2+2 val : int = 4 # let f = fun y -> + y val f : int -> int = fn # let = + val : int = 8 # f 0 val it : int = 4 Binding used to eval (f ) 4 : int f fn <code, >: int->int 8 : int Binding for subsequent Free vs. Bound Variables let a = 20 let f = let y = 1 in let g z = y + z in a + (g ) (a) a y (b) a (c) y (d) z (e) y z let a = 20 let f = let y = 1 in let g z = y + z in a + (g ) f 0 (e1 e2) Environment frozen with function Used to evaluate fun application Which vars needed in frozen env?

Free vs. Bound Variables Q: Which vars are free in f? let a = 20 let f = let y = 1 in let g z = y + z in a + (g ) f 0 Inside a function: A bound occurrence: 1. Formal variable 2. Variable bound in let-in, y, z are bound inside f A free occurrence: Non-bound occurrence a is free inside f Frozen Environment needed for values of free vars let a = 20 let f = let a = 1 in let g z = a + z in a + (g ) (a) a (b) (c) y (d) z (e) None Free vs. Bound Variables Where do bound-vars values come from? let a = 20 let f = let a = 1 in let g z = a + z in a + (g ) f 0; Inside a function: A bound occurrence: 1. Formal variable 2. Variable bound in let-in-end, a, z are bound inside f A free occurrence: Not bound occurrence nothing is free inside f let a = 20 let f = let a = 1 in let g z = a + z in a + (g ) f 0; Bound values determined when function is evaluated ( called ) Arguments Local variable bindings

of function application of function application Two questions about function values: What is the value: 1. of a function? fun -> e 2. of a function application (call)? (e1 e2) apply the argument e2 to the (function) e1 Value of a function application (call) (e1 e2) 1. Find closure of e1 2. Eecute body of closure with param e2 Free values found in closure-environment Bound values by eecuting closure-body of function application Q: What is the value of res? Value of a function application (call) (e1 e2) 1. Evaluate e1 in current-env to get (closure) = code (formal + body e) + env E 2. Evaluate e2 in current-env to get (argument) v2 3. Evaluate body e in env E etended with := v2 let = 1 let y = 10 let f y = + y let = 2 let y = 3 let res = f ( + y) (a) 4 (b) 5 (c) 6 (d) 11 (e) 12

Q: What is the value of res? let = 1 let y = 10 let f y = + y let = 2 let y = 3 let res = f ( + y) Application: f ( + y) f -> formal:= y body := + y env := [ ->1] -> 2 y -> 3 + y ====> 5 Eval body in env etended with formal -> 5 Eval +y in [ ->1, y ->5] ====> 6 Q: What is the value of res? Eample let = 1 let f y = let = 2 in fun z -> + y + z let = 100 let g = f 4 let y = 100 (g 1) Q: Closure value of g? formal z body + y + z env [ ->2, y ->4] Eval body in env etended with formal -> 1 Eval +y+z in [ ->2, y ->4,z ->1] ====> 7 Eample 3 let f g = let = 0 in g 2 let = 100 let h y = + y let res = f h (a) Synta Error (b) 102 (c) Type Error (d) 2 (e) 100 let f g = let = 0 in g 2 let = 100 let h y = + y f h

Static/Leical Scoping For each occurrence of a variable, Unique place in program tet where variable defined Most recent binding in environment Static/Leical: Determined from the program tet Without eecuting 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 Immutability: The Colbert Principle A function behaves the same way on Wednesday, as it behaved on Monday, no matter what happened on Tuesday