Functional Languages and Higher-Order Functions

Similar documents
CSE 3302 Programming Languages Lecture 8: Functional Programming

Chapter 5: The Untyped Lambda Calculus

CIS 500 Software Foundations Fall September 25

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Type Systems Winter Semester 2006

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Urmas Tamm Tartu 2013

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Exercise 1 ( = 24 points)

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Administration CS 412/413. Advanced Language Support. First-class vs. Second-class. First-class functions. Function Types

Exercise 1 ( = 18 points)

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

The Untyped Lambda Calculus

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Introduction to Lambda Calculus. Lecture 7 CS /08/09

CSE 505: Concepts of Programming Languages

Introduction to the Lambda Calculus

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Exercise 1 (2+2+2 points)

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

Lambda Calculus. Lecture 4 CS /26/10

Untyped Lambda Calculus

Functional Programming

Lambda Calculus-2. Church Rosser Property

Programming Languages Lecture 15: Recursive Types & Subtyping

Concepts of programming languages

Untyped Lambda Calculus

Graphical Untyped Lambda Calculus Interactive Interpreter

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Lecture 5: The Untyped λ-calculus

Exercise 1 ( = 22 points)

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

Organization of Programming Languages CS3200/5200N. Lecture 11

Last class. CS Principles of Programming Languages. Introduction. Outline

Recursive Definitions, Fixed Points and the Combinator

CITS3211 FUNCTIONAL PROGRAMMING

Recursion. Lecture 6: More Lambda Calculus Programming. Fixed Points. Recursion

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Lambda Calculus. Variables and Functions. cs3723 1

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Types and Type Inference

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

λ calculus is inconsistent

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Costly software bugs that could have been averted with type checking

Elixir, functional programming and the Lambda calculus.

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Lecture 5: Lazy Evaluation and Infinite Data Structures

Calculus of Inductive Constructions

More Lambda Calculus and Intro to Type Systems

CMSC 330: Organization of Programming Languages

Semantic Analysis and Type Checking

The Untyped Lambda Calculus

Fundamentals and lambda calculus

CMSC330 Fall 2013 Practice Problems 6 Solutions

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

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

Types and Type Inference

Polymorphism and Type Inference

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Introduction to System F. Lecture 18 CS 565 4/20/09

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

More Lambda Calculus and Intro to Type Systems

Concepts Introduced in Chapter 7

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Polymorphism and Type Inference

Weeks 6&7: Procedures and Parameter Passing

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

Lambda Calculus and Type Inference

301AA - Advanced Programming

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

Type Checking and Type Inference

Lambda Calculus and Type Inference

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

Functional Programming

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

G Programming Languages - Fall 2012

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

The Untyped Lambda Calculus

The Polymorphic Blame Calculus and Parametricity

Type Systems, Type Inference, and Polymorphism

Transcription:

Functional Languages and Higher-Order Functions Leonidas Fegaras CSE 5317/4305 L12: Higher-Order Functions 1

First-Class Functions Values of some type are first-class if They can be assigned to local variables They can be components of data structures Passed as arguments to functions Returned from functions Created at run-time How functions are treated by programming languages? language passed as arguments returned from functions nested scope Java No No No C Yes Yes No C++ Yes Yes No Pascal Yes No Yes Modula-3 Yes No Yes Scheme Yes Yes Yes ML Yes Yes Yes CSE 5317/4305 L12: Higher-Order Functions 2

Function Types A new type constructor (T1,T2,...,Tn) -> T0 Takes n arguments of type T1, T2,..., Tn and returns a value of type T0 Unary function: T1 -> T0 Nullary function: () -> T0 Example: sort ( A: int[], order: (int,int)->boolean ) { for (int i = 0; i<a.size; i++) for (int j=i+1; j<a.size; j++) if (order(a[i],a[j])) switch A[i] and A[j]; } boolean leq ( x: int, y: int ) { return x <= y; } boolean geq ( x: int, y: int ) { return x >= y; } sort(a,leq) sort(a,geq) CSE 5317/4305 L12: Higher-Order Functions 3

How can you do this in Java? interface Comparison { boolean compare ( int x, int y ); } void sort ( int[] A, Comparison cmp ) { for (int i = 0; i<a.length; i++) for (int j=i+1; j<a.length; j++) if (cmp.compare(a[i],a[j]))... } class Leq implements Comparison { boolean compare ( int x, int y ) { return x <=y; } } sort(a,new Leq); CSE 5317/4305 L12: Higher-Order Functions 4

Nested Functions Without nested scopes, a function may be represented as a pointer to its code Functional languages (Scheme, ML, Haskell) as well as Pascal and Modula-3, support nested functions They can access variables of the containing lexical scope plot ( f: (float)->float ) {... } plotq ( a, b, c: float ) p ( x: float ) { return a*x*x + b*x + c; } { plot(p); } Nested functions may access and update free variables from containing scopes Representing functions as pointers to code is not good any more CSE 5317/4305 L12: Higher-Order Functions 5

Closures Nested functions may need to access variables in previous frames in the stack Function values is a closure that consists of Pointer to code An environment for free variables Implementation of environment: It is simply a static link to the beginning of the frame that defined the function plot ( f: (float)->float ) {... } plotq ( a, b, c: float ) p ( x: float ) { return a*x*x + b*x + c; } { plot(p); } p f plotq plot code for p closure of p CSE 5317/4305 L12: Higher-Order Functions 6

What about Returned Functions? If the frame of the function that defined the passing function has been exited, the static link will be a dangling pointer ()->int make_counter () { } int count = 0; int inc () { return count++; } return inc; make_counter()() + make_counter()(); ()->int c = make_counter(); c()+c(); CSE 5317/4305 L12: Higher-Order Functions 7

Frames in Heap! Solution: heap-allocate function frames No need for run-time stack Frames of all lexically enclosing functions are reachable from a closure via static link chains The GC will collect unused frames Frames make a lot of garbage look reachable CSE 5317/4305 L12: Higher-Order Functions 8

Escape Analysis Local variables need to be stored on heap if they can escape and be accessed after the defining function returns It happens only if the variable is referenced from within some nested function the nested function is returned or passed to some function that might store it in a data structure Variables that do not escape are allocated on a stack frame rather than on heap No escaping variable => no heap allocation Escape analysis must be global Often approximate (conservative analysis) CSE 5317/4305 L12: Higher-Order Functions 9

Functional Programming Languages Programs consist of functions with no side-effects Functions are first class values Build programs by function composition No accidental coupling between components No assignments, statements, for-loops, while-loops, etc Supports higher-level, declarative programming style Automatic memory management (garbage collection) Emphasis on types and type inference Built-in support for lists and other recursive data types Type inference is like type checking but no type declarations are required Types of variables and expressions can be inferred from context Parametric data types and polymorphic type inference Strict vs lazy functional programming languages CSE 5317/4305 L12: Higher-Order Functions 10

Lambda Calculus The theoretical foundation of functional languages is lambda calculus Formalized by Church in 1941 Minimal in form Turing-complete Syntax: if e 1, e 2, and e are expressions in lambda calculus, so are Variable: v Application: e 1 e 2 Abstraction: λv. e Bound vs free variables Beta reduction: (λv. e 1 ) e 2 e 1 [e 2 /v] (e 1 but with all free occurrences of v in e 1 replaced by e 2 ) careful to avoid the variable capturing problem (name clashes) CSE 5317/4305 L12: Higher-Order Functions 11

Integers Integers: 0 = λs. λz. z 1 = λs. λz. s z 2 = λs. λz. s s z 6 = λs. λz. s s s s s s z like successor (s) and zero (z) Simple arithmetic: add = λn. λm. λs. λz. n s (m s z) add 2 3 = (λn. λm. λs. λz. n s (m s z)) 2 3 = λs. λz. 2 s (3 s z) = λs. λz. (λs. λz. s s z) s ((λs. λz. s s s z) s z) = λs. λz. (λs. λz. s s z) s (s s s z) = λs. λz. s s s s s z = 5 CSE 5317/4305 L12: Higher-Order Functions 12

Other Types Booleans true = λt. λf. t false = λt. λf. f if pred e 1 e 2 = pred e 1 e 2 eg, if pred is true, then (λt. λf. t) e 1 e 2 =e 1 Lists nil = λc. λn. n [2,5,8] = λc. λn. c 2 (c 5 (c 8 n)) cons = λx. λr. λc. λn. c x (r c n) cons 2 (cons 5 (cons 8 nil)) = = λc. λn. c 2 (c 5 (c 8 n)) append = λr. λs. λc. λn. r c (s c n) head = λs. s (λx. λr. x)? Pairs pair = λx. λy. λp. p x y first = λs. s (λx. λy. x) CSE 5317/4305 L12: Higher-Order Functions 13

Reductions REDucible EXpression (redex) an application is a redex abstractions and variables are not redexes Use beta reduction to reduce (λx. add x x) 5 is reduced to 10 Normal form = no reductions Reduction is confluent (has the Church-Rosser property) normal forms are unique regardless of the order of reduction Weak normal forms (WNF) no redexes outside of abstraction bodies Call by value (eager evaluation): WNF + leftmost innermost reductions Call by name: WNF + leftmost outermost reductions (normal order) Call by need (lazy evaluation): call by name, but redexes are evaluated at most once terms are represented by graphs and reductions make shared subgraphs CSE 5317/4305 L12: Higher-Order Functions 14

Recursion Infinite reduction: (λx. x x) (λx. x x) no normal form; no termination A fixpoint combinator Y satisfies: Y f is reduced to f (Y f) Y = (λg. (λx. g (x x)) (λx. g (x x))) Y is always built-in Implements recursion factorial = Y (λf. λn. if (= n 0) 1 (* n (f (- n 1)))) CSE 5317/4305 L12: Higher-Order Functions 15

Second-Order Polymorphic Lambda Calculus Types are: Type variable: v Universal quantification: v. t Function: t 1 t 2 Lambda terms are: Variable: v Application: e 1 e 2 Abstraction: λv:t. e Type abstraction: Λv. e Type instantiation: e[t] Integers int = a. (a a) a a succ= λx:int. Λa. λs:(a a). λz:a. s (x[a] s z) plus = λx:int. λy:int. x[int] succ y CSE 5317/4305 L12: Higher-Order Functions 16

Type Checking CSE 5317/4305 L12: Higher-Order Functions 17

Functional Languages Functional languages = typed lambda calculus + syntactic sugar Functional languages support parametric (generic) data types data List a = Nil Cons a (List a) data Tree a b = Leaf a Node b (Tree a b) (Tree a b) (Cons 1 (Cons 2 Nil)) (Cons a (Cons b Nil)) Polymorphic functions: append (Cons x r) s = Cons x (append r s) append Nil s = s The type of append is a. (List a) (List a) (List a) Parametric polymorphism vs ad-hoc polymorphism (overloading) CSE 5317/4305 L12: Higher-Order Functions 18

Type Inference Functional languages need type inference rather than type checking λv:t. e requires type checking λv. e requires type inference (need to infer the type of v) Type inference is undecidable in general Solution: type schemes (shallow types): a 1. a 2. a n. t no other universal quantification in t ( b. b int) ( b. b int) is not shallow When a type is missing, then a fresh type variable is used Type checking is based on type equality; type inference is based on type unification A type variable can be unified with any type Example in Haskell: let f = \x -> x in (f 5, f a ) \x -> x has type a. a a Cost of polymorphism: polymorphic values must be boxed (pointers to heap) CSE 5317/4305 L12: Higher-Order Functions 19