Lambda Calculus. Concepts in Programming Languages Recitation 6:

Size: px
Start display at page:

Download "Lambda Calculus. Concepts in Programming Languages Recitation 6:"

Transcription

1 Concepts in Programming Languages Recitation 6: Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto ) Reference: Types and Programming Languages by Benjamin C. Pierce, Chapter 5 1

2 Untyped Lambda Calculus - Synta t ::=. t t t terms variable abstraction application Terms can be represented as abstract synta trees Syntactic Conventions: Applications associates to left : e 1 e 2 e 3 (e 1 e 2 ) e 3 The body of abstraction etends as far as possible:. y. y. ( y. (( y) )) 2

3 Free vs. Bound Variables An occurrence of in t is bound in. t otherwise it is free is a binder FV: t P(Var) is the set free variables of t FV() = {} FV(. t) = FV(t) {} FV (t 1 t 2 ) = FV(t 1 ) FV(t 2 ) 3

4 Semantics: Substitution, β-reduction, α-conversion Substitution [ s] = s [ s] y = y [ s] ( y. t 1 ) = y. [ s] t 1 if y if y and y FV(s) [ s] (t 1 t 2 ) = ([ s] t 1 ) ([ s] t 2 ) -reduction (. t 1 ) t 2 [ t 2 ] t 1 α-conversion (. t) y. [ y] t if y FV(t) 4

5 Eamples of β-reduction, α-conversion (. ) y y (. (. ) ) (u r) + u r (. ) ( ( w. w)) (y z) w. y z w (. (. )) y (. ( y. )) y (. ( z. z)) y (. ( z. )) y z. z z. y 5

6 Non-Deterministic Operational Semantics (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 t t. t. t (E-Abs) t 1 t 1 t 1 t 2 t 1 t 2 t 2 t 2 (E-App 1 ) (E-App 2 ) t 1 t 2 t 1 t 2 Why is this semantics non-deterministic? 6

7 Different Evaluation Orders (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 t t. t. t (E-Abs) t 1 t 1 t 2 t 2 (E-App 1 ) (E-App 2 ) t 1 t 2 t 1 t 2 t 1 t 2 t 1 t 2 (. (add )) (add 2 3) (. (add )) (5) add (. (add )) (add 2 3) (add (add 2 3) (add 2 3)) (add 5 (add 2 3)) (add 5 5) 10 This eample: same final result but lazy performs more computations 7

8 Different Evaluation Orders (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 t t. t. t (E-Abs) t 1 t 1 t 2 t 2 (E-App 1 ) (E-App 2 ) t 1 t 2 t 1 t 2 t 1 t 2 t 1 t 2 (. y. ) 3 (div 5 0) Eception: Division by zero (. y. ) 3 (div 5 0) ( y. 3) (div 5 0) 3 This eample: lazy suppresses erroneous division and reduces to final result Can also suppress non-terminating computation. Many times we want this, for eample: if i < len(a) and a[i]==0: print found zero 8

9 Strict Lazy Normal Order (E-App 1 ) t 1 t 1 t 1 t 2 t 1 t 2 precedence (E-App 2 ) t 2 t 2 t 1 t 2 t 1 t 2 precedence (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 (E-App 1 ) t 1 t 1 t 1 t 2 t 1 t 2 (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 precedence (E-App 1 ) t 1 t 1 t 1 t 2 t 1 t 2 precedence (E-App 2 ) t 2 t 2 t 1 t 2 t 1 t 2 (E-Abs) t t. t. t 9

10 Call-by-value Operations Semantics via Inductive Definition (no precedence) t ::= terms v ::=. t abstraction values variable. t abstraction t t application (. t 1 ) v 2 [ v 2 ] t 1 (E-AppAbs) t 1 t 1 t 1 t 2 t 1 t 2 (E-APPL1) t 2 t 2 v 1 t 2 v 1 t 2 (E-APPL2) 10

11 Different Evaluation Orders - Eample (. y. ( z. z) y) (( u. u) ( w. w)) y u w u w z y z 11

12 Call By Value (. y. ( z. z) y) (( u. u) ( w. w)) y y u w y w w z y u w z z y z y z z 12

13 Call By Name (Lazy) (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y u w z z y z 13

14 Normal Order (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y y y u w z z y z 14

15 Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.( )) (.( )) 15

16 Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.( )) (.( )) 16

17 Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y 17

18 Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y 18

19 Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) def f(): while True: pass y def g(): return 2 print g(f()) 19

20 Summary: Orders of Evaluation Full-beta-reduction, Non-deterministic semantics All possible orders Call by value, Eager, Strict, Applicative order Left to right Fully evaluate arguments before function Normal order The leftmost, outermost rede is always reduced first Call by name, Lazy Evaluate arguments as needed Call by need Evaluate arguments as needed and store for subsequent usages Implemented in Haskell 20

21 Church Rosser Theorem If: a * b, a * c then there eists d such that: b * d, and c * d b a c d 21

22 Normal Form & Halting Problem A term is in normal form if it is stuck in normal order semantics Under normal order every term either: Reduces to normal form, or Reduces infinitely For a given term, it is undecidable to decide which is the case 22

23 Combinators A combinator is a function in the Lambda Calculus having no free variables Eamples. is a combinator. y. ( y) is a combinator. y. ( z) is not a combinator Combinators can serve nicely as modular building blocks for more comple epressions The Church numerals and simulated Booleans are eamples of useful combinators 23

24 Iteration in Lambda Calculus omega = (. ) (. ) (. ) (. ) (. ) (. ) Y = f. (. f ( )) (. f ( )) Z = f. (. f ( y. y)) (. f ( y. y)) Recursion can be simulated Y only works with call-by-name semantics Z works with call-by-value semantics Defining factorial: g = f. n. if n==0 then 1 else (n * (f (n - 1))) fact = Y g (for call-by-name) fact = Z g (for call-by-value) 24

25 Y-Combinator in action (lazy) g = f. n. if n==0 then 1 else (n * (f (n - 1))) Y = f. (. f ( )) (. f ( )) Y g v = ( f. (. f ( )) (. f ( ))) g v ((. g ( )) (. g ( ))) v (g ((. g ( )) (. g ( ))) ) v ~ (g (Y g)) v What happens to Y in strict semantics? 25

26 Z-Combinator in action (strict) g = f. n. if n==0 then 1 else (n * (f (n - 1))) Z = f. (. f ( y. y)) (. f ( y. y)) Z g v = ( f. (. f ( y. y)) (. f ( y. y))) g v ((. g ( y. y)) (. g ( y. y))) v (g ( y. (. g ( y. y)) (. g ( y. y)) y)) v ~ (g ( y. (Z g) y) ) v ~ (g (Z g)) v def f1(y): return f2(y) 26

27 Simulating laziness like Z-Combinator def f(): if ask_user("wanna see it?"): print def g(, y, z): # very epensive computation without side effects def main(): # compute a, b, c with side effects f(g(a, b, c)) In strict semantics, the above code computes g anyway Lazy will avoid it How can achieve this in a strict programming language? 27

28 Simulating laziness like Z-Combinator def f(): if ask_user(?"): print def g(, y, z): # epensive def main(): # compute a, b, c f(g(a, b, c)) def f(): if ask_user("?"): print () def g(, y, z): # epensive def main(): # compute a, b, c f(lambda: g(a, b, c)) Z = f. (. f ( y. y)) (. f ( y. y)) (E-Abs) t t. t. t 28

29 Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages

30 Simple Types T ::= Bool T T types type of Booleans type of functions T 1 T 2 T 3 = T 1 ( T 2 T 3 ) 30

31 Simple Typed Lambda Calculus t ::= : T. t t t terms variable abstraction application T::= Bool T T types atomic type for Booleans types of functions Tying Relation Type fact: t:t means term t has type T (e.g. 1+3:Int, f:int Int) Typing relation : relates sets of type facts and type facts t:t means under (contet, environment), term t has type T t:t means term t has type T under the empty environment (no assumptions) Can t have free variables? 31

32 Typing Relation Eamples Type fact: t:t means term t has type T (e.g. 1+3:Int, f:int Int) t:t means under (contet, environment), term t has type T t:t means closed term t has type T under the empty environment Eamples :Int, y:int +y :? :Int, y:bool +y :? :Int +y :? :Int, y:int, b:bool if b then else y :? :Int, y:bool, b:bool if b then else y :? :Int, b:bool if b then else y :? :Int, f:int Bool f :? :Int, f: Int Int Bool f :? :Int, f: Int Int Bool f :? f: Int Int Bool f :? 1+2 :? true :? :Int, y:int, f: Int Int Bool 1+2 :? 32

33 Formally Defining t ::= terms variable : T : T (T-VAR) : T. t t t abstraction application, : T 1 t 2 : T 2 : T 1. t 2 : T 1 T 2 (T-ABS) T::= types t 1 : T 11 T 12 t 2 : T 11 t 1 t 2 : T 12 (T-APP) T T types of functions ::= contet empty contet, : T term variable binding 33

34 Adding Booleans t ::= terms variable : T. t abstraction t t application true constant true false constant false if t then t else t conditional T::= types Bool type of Booleans T T types of functions ::= contet empty contet, : T term variable binding : T : T, : T 1 t 2 : T 2 : T 1. t 2 : T 1 T 2 t 1 : T 11 T 12 t 2 : T 11 t 1 t 2 : T 12 true : Bool false : Bool t 1 :Bool t 2 :T t 3 :T if t 1 then t 2 else t 3 : T (T-VAR) (T-ABS) (T-APP) (T-TRUE) (T-FALSE) (T-IF) 34

35 Eample Typing Tree T-VAR f:bool Bool, :Bool :Bool T-VAR f:bool Bool, :Bool f:bool Bool T-FALSE T-VAR T-APP f:bool Bool, :Bool false:bool f:bool Bool, :Bool :Bool f:bool Bool, :Bool f : Bool T-IF T-ABS f:bool Bool, :Bool if false then else f : Bool f:bool Bool :Bool if false then else f : Bool Bool : T : T (T-VAR) t 1 : T 11 T 12 t 2 : T 11 t 1 t 2 : T 12 (T-APP), : T 1 t 2 : T 2 : T 1. t 2 : T 1 T 2 (T-ABS) t 1 :Bool t 2 :T t 3 :T if t 1 then t 2 else t 3 : T (T-IF) false : Bool (T-FALSE) 35

36 Curry-Howard Isomorphism Beautiful connection between type systems (PL) and proof systems (logic) 36

37 Retrospect

38 Natural Operational Semantics is defined inductively using inference rules, with both syntactic conditions on S and semantic conditions on s 38

39 Structural Operational Semantics is defined inductively using inference rules, with both syntactic conditions on S and semantic conditions on s 39

40 -Calculus: Non-Deterministic Operational Semantics (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 t t. t. t (E-Abs) t 1 t 1 t 1 t 2 t 1 t 2 t 2 t 2 (E-App 1 ) (E-App 2 ) t 1 t 2 t 1 t 2 40

41 -Calculus: Lazy Evaluation Operational Semantics (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 (E-App 1 ) t 1 t 1 t 1 t 2 t 1 t 2 41

42 -Calculus: Call-by-Value Operational Semantics (E-AppAbs) (. t 1 ) v 2 [ v 2 ] t 1 t 1 t 1 t 1 t 2 t 1 t 2 t 2 t 2 (E-App 1 ) (E-App 2 ) v 1 t 2 v 1 t 2 42

43 Simply Typed -Calculus t ::= terms variable : T. t abstraction t t application true constant true false constant false if t then t else t conditional T::= types Bool type of Booleans T T types of functions ::= contet empty contet, : T term variable binding : T : T, : T 1 t 2 : T 2 : T 1. t 2 : T 1 T 2 t 1 : T 11 T 12 t 2 : T 11 t 1 t 2 : T 12 true : Bool false : Bool t 1 :Bool t 2 :T t 3 :T if t 1 then t 2 else t 3 : T (T-VAR) (T-ABS) (T-APP) (T-TRUE) (T-FALSE) (T-IF) 43

44 Retrospect Conclusion: For the past 7 weeks we have been formalizing programming languages Synta, Semantics, and Type Rules using Inductive Definitions Q: What s the difference between a mathematician and a computer scientist? A: The mathematician sometimes proves theorems without induction

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

More Untyped Lambda Calculus & Simply Typed Lambda Calculus Concepts in Programming Languages Recitation 6: More Untyped Lambda Calculus & Simply Typed Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky,

More information

Untyped Lambda Calculus

Untyped Lambda Calculus Concepts in Programming Languages Recitation 5: Untyped Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto ) Reference: Types and

More information

Untyped Lambda Calculus

Untyped Lambda Calculus Advanced Topics in Programming Languages Untyped Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto ) Reference: Types and Programming

More information

Lambda Calculus.

Lambda Calculus. Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto, Stephen A. Edwards) Benjamin Pierce Types and Programming Languages http://www.cs.cornell.edu/courses/cs3110/2008fa/recitations/rec26.html

More information

Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages

Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages t ::= x x. t t t Call-by-value small step perational Semantics terms variable v ::= values abstraction x. t abstraction values

More information

Lambda Calculus. Lecture 4 CS /26/10

Lambda Calculus. Lecture 4 CS /26/10 Lambda Calculus Lecture 4 CS 565 10/26/10 Pure (Untyped) Lambda Calculus The only value is a function Variables denote functions Functions always take functions as arguments Functions always return functions

More information

Type Systems Winter Semester 2006

Type Systems Winter Semester 2006 Type Systems Winter Semester 2006 Week 4 November 8 November 15, 2006 - version 1.1 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial

More information

Chapter 5: The Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus Chapter 5: The Untyped Lambda Calculus What is lambda calculus for? Basics: syntax and operational semantics Programming in the Lambda Calculus Formalities (formal definitions) What is Lambda calculus

More information

Formal Systems and their Applications

Formal Systems and their Applications Formal Systems and their Applications Dave Clarke (Dave.Clarke@cs.kuleuven.be) Acknowledgment: these slides are based in part on slides from Benjamin Pierce and Frank Piessens 1 Course Overview Introduction

More information

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Advanced Topics in Programming Languages Lecture 3 - Models of Computation Advanced Topics in Programming Languages Lecture 3 - Models of Computation Andrey Leshenko Maxim Borin 16/11/2017 1 Lambda Calculus At the beginning of the last century, several attempts were made to theoretically

More information

The Untyped Lambda Calculus

The Untyped Lambda Calculus 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... paste or by selection, annotation, or rewording. [Järvi] is in turn

More information

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu, Lambda Calculus Type Systems, Lectures 3 Jevgeni Kabanov Tartu, 13.02.2006 PREVIOUSLY ON TYPE SYSTEMS Arithmetical expressions and Booleans Evaluation semantics Normal forms & Values Getting stuck Safety

More information

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson Lambda Calculus CS 550 Programming Languages Jeremy Johnson 1 Lambda Calculus The semantics of a pure functional programming language can be mathematically described by a substitution process that mimics

More information

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

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods Course 2D1453, 2006-07 Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Some material from B. Pierce: TAPL + some from G. Klein, NICTA Alonzo Church, 1903-1995 Church-Turing thesis First

More information

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

Last class. CS Principles of Programming Languages. Introduction. Outline Last class CS6848 - Principles of Programming Languages Principles of Programming Languages V. Krishna Nandivada IIT Madras Interpreters A Environment B Cells C Closures D Recursive environments E Interpreting

More information

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

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements CIS 500 Software Foundations Fall 2004 27 September IS 500, 27 September 1 The Lambda Calculus IS 500, 27 September 3 Announcements Homework 1 is graded. Pick it up from Cheryl Hickey (Levine 502). We

More information

Chapter 5: The Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus Chapter 5: The Untyped Lambda Calculus What is lambda calculus for? Basics: syntax and operational semantics Programming in the Lambda Calculus Formalities (formal definitions) What is Lambda calculus

More information

CITS3211 FUNCTIONAL PROGRAMMING

CITS3211 FUNCTIONAL PROGRAMMING CITS3211 FUNCTIONAL PROGRAMMING 9. The λ calculus Summary: This lecture introduces the λ calculus. The λ calculus is the theoretical model underlying the semantics and implementation of functional programming

More information

CIS 500 Software Foundations Fall September 25

CIS 500 Software Foundations Fall September 25 CIS 500 Software Foundations Fall 2006 September 25 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial programming language, then the

More information

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.

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. Programming Languages Fall 2013 Midterm Exam 10/22/13 Time Limit: 100 Minutes Name (Print): Graduate Center I.D. This is a closed-book, closed-notes exam. Various definitions are provided in the exam.

More information

The Untyped Lambda Calculus

The Untyped Lambda Calculus 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... paste or by selection, annotation, or rewording. [Järvi] is in turn

More information

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages Call-by-value Operational Semantics

More information

1 Scope, Bound and Free Occurrences, Closed Terms

1 Scope, Bound and Free Occurrences, Closed Terms CS 6110 S18 Lecture 2 The λ-calculus Last time we introduced the λ-calculus, a mathematical system for studying the interaction of functional abstraction and functional application. We discussed the syntax

More information

The Untyped Lambda Calculus

The Untyped Lambda Calculus Resources: The slides of this lecture were derived from x = 1 [Järvi], with permission of the original author, by copy & let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn

More information

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped) 1 LAMBDA CALCULUS (untyped) 2 The Untyped Lambda Calculus (λ) Designed by Alonzo Church (1930s) Turing Complete (Turing was his doctoral student!) Models functions, always as 1-input Definition: terms,

More information

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Lesson 4 Typed Arithmetic Typed Lambda Calculus Lesson 4 Typed Arithmetic Typed Lambda 1/28/03 Chapters 8, 9, 10 Outline Types for Arithmetic types the typing relation safety = progress + preservation The simply typed lambda calculus Function types

More information

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

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08 Introduction to Lambda Calculus Lecture 5 CS 565 1/24/08 Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops)

More information

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

5. Introduction to the Lambda Calculus. Oscar Nierstrasz 5. Introduction to the Lambda Calculus Oscar Nierstrasz Roadmap > What is Computability? Church s Thesis > Lambda Calculus operational semantics > The Church-Rosser Property > Modelling basic programming

More information

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

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility λ Calculus Church s λ Calculus: Brief History One of a number of approaches to a mathematical challenge at the time (1930): Constructibility (What does it mean for an object, e.g. a natural number, to

More information

CSE 505: Concepts of Programming Languages

CSE 505: Concepts of Programming Languages CSE 505: Concepts of Programming Languages Dan Grossman Fall 2003 Lecture 6 Lambda Calculus Dan Grossman CSE505 Fall 2003, Lecture 6 1 Where we are Done: Modeling mutation and local control-flow Proving

More information

Lecture 5: The Untyped λ-calculus

Lecture 5: The Untyped λ-calculus Lecture 5: The Untyped λ-calculus Syntax and basic examples Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Static Analysis Pratikakis (CSD) Untyped λ-calculus I CS49040,

More information

Concepts of programming languages

Concepts of programming languages Concepts of programming languages Lecture 5 Wouter Swierstra 1 Announcements Submit your project proposal to me by email on Friday; The presentation schedule in now online Exercise session after the lecture.

More information

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21 Functions as data Massimo Merro 9 November 2011 Massimo Merro The Lambda language 1 / 21 The core of sequential programming languages In the mid 1960s, Peter Landin observed that a complex programming

More information

Lecture 9: Typed Lambda Calculus

Lecture 9: Typed Lambda Calculus Advanced Topics in Programming Languages Spring Semester, 2012 Lecture 9: Typed Lambda Calculus May 8, 2012 Lecturer: Mooly Sagiv Scribe: Guy Golan Gueta and Shachar Itzhaky 1 Defining a Type System for

More information

Programming Languages Fall 2014

Programming Languages Fall 2014 Programming Languages Fall 2014 Lecture 7: Simple Types and Simply-Typed Lambda Calculus Prof. Liang Huang huang@qc.cs.cuny.edu 1 Types stuck terms? how to fix it? 2 Plan First I For today, we ll go back

More information

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine. CMSC 330: Organization of Programming Languages Lambda Calculus Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying or tuples

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying

More information

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...

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... 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... paste or by selection, annotation, or rewording. [Järvi] is in turn

More information

1.3. Conditional expressions To express case distinctions like

1.3. Conditional expressions To express case distinctions like Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

More information

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus. Variables and Functions. cs3723 1 Lambda Calculus Variables and Functions cs3723 1 Lambda Calculus Mathematical system for functions Computation with functions Captures essence of variable binding Function parameters and substitution Can

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ October 13, 2004 Lambda Calculus and Type

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ August 17, 2007 Lambda Calculus and Type

More information

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

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland COMP 1130 Lambda Calculus based on slides by Jeff Foster, U Maryland Motivation Commonly-used programming languages are large and complex ANSI C99 standard: 538 pages ANSI C++ standard: 714 pages Java

More information

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6 Lambda calculus Advanced functional programming - Lecture 6 Wouter Swierstra and Alejandro Serrano 1 Today Lambda calculus the foundation of functional programming What makes lambda calculus such a universal

More information

Lambda Calculus and Extensions as Foundation of Functional Programming

Lambda Calculus and Extensions as Foundation of Functional Programming Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauß 29. September 2015 Lehrerbildungsforum Informatik Last update: 30. September 2015 Overview

More information

Types and Type Inference

Types and Type Inference Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion

More information

Higher-Order Logic. Specification and Verification with Higher-Order Logic

Higher-Order Logic. Specification and Verification with Higher-Order Logic Higher-Order Logic Specification and Verification with Higher-Order Logic Arnd Poetzsch-Heffter (Slides by Jens Brandt) Software Technology Group Fachbereich Informatik Technische Universität Kaiserslautern

More information

An Introduction to the Lambda Calculus

An Introduction to the Lambda Calculus Department of Computer Science Australian National University COMP3610 Principles of Programming Languages An Introduction to the Lambda Calculus Clem Baker-Finch August 13, 2013 Contents 1 Motivation

More information

Introduction to the λ-calculus

Introduction to the λ-calculus Announcements Prelim #2 issues: o Problem 5 grading guide out shortly o Problem 3 (hashing) issues Will be on final! Friday RDZ office hours are 11-12 not 1:30-2:30 1 Introduction to the λ-calculus Today

More information

Costly software bugs that could have been averted with type checking

Costly software bugs that could have been averted with type checking Type Checking Class Notes from Lectures 6 and 7 Lahav Yeffet and Ori Folger The material in these lectures is based on the textbook Types and Programming Languages by Benjamin Pierce. Here is a list of

More information

Pure Lambda Calculus. Lecture 17

Pure Lambda Calculus. Lecture 17 Pure Lambda Calculus Lecture 17 Lambda Calculus Lambda Calculus (λ-calculus) is a functional notation introduced by Alonzo Church in the early 1930s to formalize the notion of computability. Pure λ-calculus

More information

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008 CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008 Contents 1 Solution to the Exercise 1 1.1 Semantics for lambda calculus.......................

More information

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

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008. CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008 Contents 1 Announcements 1 2 Solution to the Eercise 1 3 Introduction 1 4 Multiple

More information

Introduction to the Lambda Calculus

Introduction to the Lambda Calculus Introduction to the Lambda Calculus Overview: What is Computability? Church s Thesis The Lambda Calculus Scope and lexical address The Church-Rosser Property Recursion References: Daniel P. Friedman et

More information

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010 Pure (Untyped) λ-calculus Andrey Kruglyak, 2010 1 Pure (untyped) λ-calculus An example of a simple formal language Invented by Alonzo Church (1936) Used as a core language (a calculus capturing the essential

More information

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15 Lambda Calculus: Implementation Techniques and a Proof COS 441 Slides 15 Last Time: The Lambda Calculus A language of pure functions: values e ::= x \x.e e e v ::= \x.e With a call-by-value operational

More information

dynamically typed dynamically scoped

dynamically typed dynamically scoped Reference Dynamically Typed Programming Languages Part 1: The Untyped λ-calculus Jim Royer CIS 352 April 19, 2018 Practical Foundations for Programming Languages, 2/e, Part VI: Dynamic Types, by Robert

More information

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

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial Begin at the beginning Epressions (Synta) Compile-time Static Eec-time Dynamic Types Values (Semantics) 1. Programmer enters epression 2. ML checks if epression is well-typed Using a precise set of rules,

More information

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Toby Murray, June Andronick, Gerwin Klein λ 1 Last time... λ calculus syntax free variables, substitution β reduction α and η conversion

More information

Lambda Calculus. Lambda Calculus

Lambda Calculus. Lambda Calculus Lambda Calculus Formalism to describe semantics of operations in functional PLs Variables are free or bound Function definition vs function abstraction Substitution rules for evaluating functions Normal

More information

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

Introduction to Lambda Calculus. Lecture 7 CS /08/09 Introduction to Lambda Calculus Lecture 7 CS 565 02/08/09 Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops)

More information

Programming Language Concepts: Lecture 19

Programming Language Concepts: Lecture 19 Programming Language Concepts: Lecture 19 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 19, 01 April 2009 Adding types

More information

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

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point Lambda Calculus CSE 505 Lecture #9 October 1, 2012 Expr ::= Var λ Var. Expr (Expr Expr) Key Concepts: Bound and Free Occurrences Substitution, Reduction Rules: α, β, η Confluence and Unique Normal Form

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information

Types, Type Inference and Unification

Types, Type Inference and Unification Types, Type Inference and Unification Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Cornell CS 6110 Summary (Functional Programming) Lambda Calculus Basic ML Advanced ML: Modules, References,

More information

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus CMSC 330: Organization of Programming Languages Lambda Calculus 1 Turing Completeness Turing machines are the most powerful description of computation possible They define the Turing-computable functions

More information

From the λ-calculus to Functional Programming Drew McDermott Posted

From the λ-calculus to Functional Programming Drew McDermott Posted From the λ-calculus to Functional Programming Drew McDermott drew.mcdermott@yale.edu 2015-09-28 Posted 2015-10-24 The λ-calculus was intended from its inception as a model of computation. It was used by

More information

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

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur Functional Programming and λ Calculus Amey Karkare Dept of CSE, IIT Kanpur 0 Software Development Challenges Growing size and complexity of modern computer programs Complicated architectures Massively

More information

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples: COMP80 Programming Languages Slides Courtesy of Prof. Sam Guyer Lambda Calculus Formal system with three parts Notation for functions Proof system for equations Calculation rules called reduction Idea:

More information

λ calculus is inconsistent

λ calculus is inconsistent Content Rough timeline COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Gerwin Klein, June Andronick, Toby Murray λ Intro & motivation, getting started [1] Foundations & Principles

More information

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSE 3302 Programming Languages Lecture 8: Functional Programming CSE 3302 Programming Languages Lecture 8: Functional Programming (based on the slides by Tim Sheard) Leonidas Fegaras University of Texas at Arlington CSE 3302 L8 Spring 2011 1 Functional Programming Languages

More information

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

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int Begin at the beginning Epressions (Synta) Compile-time Static Eec-time Dynamic Types Values (Semantics) 1. Programmer enters epression 2. ML checks if epression is well-typed Using a precise set of rules,

More information

Fundamentals and lambda calculus

Fundamentals and lambda calculus Fundamentals and lambda calculus Again: JavaScript functions JavaScript functions are first-class Syntax is a bit ugly/terse when you want to use functions as values; recall block scoping: (function ()

More information

Urmas Tamm Tartu 2013

Urmas Tamm Tartu 2013 The implementation and optimization of functional languages Urmas Tamm Tartu 2013 Intro The implementation of functional programming languages, Simon L. Peyton Jones, 1987 Miranda fully lazy strict a predecessor

More information

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012 CS-XXX: Graduate Programming Languages Lecture 9 Simply Typed Lambda Calculus Dan Grossman 2012 Types Major new topic worthy of several lectures: Type systems Continue to use (CBV) Lambda Caluclus as our

More information

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus A Quick Overview CAS 701 Class Presentation 18 November 2008 Lambda Department of Computing & Software McMaster University 1.1 Outline 1 2 3 Lambda 4 5 6 7 Type Problem Lambda 1.2 Lambda calculus is a

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

Programming Languages

Programming Languages CSE 230: Winter 2008 Principles of Programming Languages Ocaml/HW #3 Q-A Session Push deadline = Mar 10 Session Mon 3pm? Lecture 15: Type Systems Ranjit Jhala UC San Diego Why Typed Languages? Development

More information

Functional Languages and Higher-Order Functions

Functional Languages and Higher-Order Functions 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

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 Summer 2017 1 100 years ago Albert Einstein proposed special theory of relativity in 1905 In the paper On the Electrodynamics of

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Use currying

More information

Chapter 2 The Language PCF

Chapter 2 The Language PCF Chapter 2 The Language PCF We will illustrate the various styles of semantics of programming languages with an example: the language PCF Programming language for computable functions, also called Mini-ML.

More information

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Tuesday, February 19, 2013 The lambda calculus (or λ-calculus) was introduced by Alonzo Church and Stephen Cole Kleene in

More information

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus CMSC 330: Organization of Programming Languages Lambda Calculus 1 100 years ago Albert Einstein proposed special theory of relativity in 1905 In the paper On the Electrodynamics of Moving Bodies 2 Prioritätsstreit,

More information

The Untyped Lambda Calculus

The Untyped Lambda Calculus CS738: Advanced Compiler Optimizations The Untyped Lambda Calculus Amey Karkare karkare@cse.iitk.ac.in http://www.cse.iitk.ac.in/~karkare/cs738 Department of CSE, IIT Kanpur Reference Book Types and Programming

More information

Exercise 1 ( = 24 points)

Exercise 1 ( = 24 points) 1 Exercise 1 (4 + 5 + 4 + 6 + 5 = 24 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a =

More information

Lecture Notes on Data Representation

Lecture Notes on Data Representation Lecture Notes on Data Representation 15-814: Types and Programming Languages Frank Pfenning Lecture 9 Tuesday, October 2, 2018 1 Introduction In this lecture we ll see our type system in action. In particular

More information

CSE-321 Programming Languages 2012 Midterm

CSE-321 Programming Languages 2012 Midterm Name: Hemos ID: CSE-321 Programming Languages 2012 Midterm Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 14 15 29 20 7 15 100 There are six problems on 24 pages in this exam. The maximum score

More information

Introduction to the Lambda Calculus. Chris Lomont

Introduction to the Lambda Calculus. Chris Lomont Introduction to the Lambda Calculus Chris Lomont 2010 2011 2012 www.lomont.org Leibniz (1646-1716) Create a universal language in which all possible problems can be stated Find a decision method to solve

More information

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides) Fundamentals and lambda calculus Deian Stefan (adopted from my & Edward Yang s CSE242 slides) Logistics Assignments: Programming assignment 1 is out Homework 1 will be released tomorrow night Podcasting:

More information

CIS 500 Software Foundations Midterm I

CIS 500 Software Foundations Midterm I CIS 500 Software Foundations Midterm I October 11, 2006 Name: Student ID: Email: Status: Section: registered for the course not registered: sitting in to improve a previous grade not registered: just taking

More information

Recursive Definitions, Fixed Points and the Combinator

Recursive Definitions, Fixed Points and the Combinator Recursive Definitions, Fixed Points and the Combinator Dr. Greg Lavender Department of Computer Sciences University of Texas at Austin Recursive Self-Reference Recursive self-reference occurs regularly

More information

Programming Languages

Programming Languages Programming Languages Lambda Calculus and Scheme CSCI-GA.2110-003 Fall 2011 λ-calculus invented by Alonzo Church in 1932 as a model of computation basis for functional languages (e.g., Lisp, Scheme, ML,

More information

A Canonical 1 Locally Named Representation of Binding. α -equivalence is identity. Randy Pollack. Masahiko Sato. LFCS, University of Edinburgh

A Canonical 1 Locally Named Representation of Binding. α -equivalence is identity. Randy Pollack. Masahiko Sato. LFCS, University of Edinburgh A Canonical 1 Locally Named Representation of Binding Randy Pollack LFCS, University of Edinburgh Masahiko Sato Graduate School of Informatics, Kyoto University Version of December 7, 2009 1 α -equivalence

More information

VU Semantik von Programmiersprachen

VU Semantik von Programmiersprachen VU Semantik von Programmiersprachen Agata Ciabattoni Institute für Computersprachen, Theory and Logic group (agata@logic.at) (A gentle) Introduction to λ calculus p. 1 Why shoud I studyλcalculus? p. 2

More information

Mutable References. Chapter 1

Mutable References. Chapter 1 Chapter 1 Mutable References In the (typed or untyped) λ-calculus, or in pure functional languages, a variable is immutable in that once bound to a value as the result of a substitution, its contents never

More information

(Advanced) topics in Programming Languages

(Advanced) topics in Programming Languages Spring 2012 (Advanced) topics in Programming Languages Instructor: Mooly Sagiv TA: Shachar Itzhaky http://www.cs.tau.ac.il/~msagiv/courses/apl12.html Inspired by John Mitchell CS 242 Compilation course

More information

Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

More information

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER 2012 2013 MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS Time allowed TWO hours Candidates may complete the front

More information