Lambda Calculus. Concepts in Programming Languages Recitation 6:

Similar documents
More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Untyped Lambda Calculus

Untyped Lambda Calculus

Lambda Calculus.

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

Lambda Calculus. Lecture 4 CS /26/10

Type Systems Winter Semester 2006

Chapter 5: The Untyped Lambda Calculus

Formal Systems and their Applications

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

The Untyped Lambda Calculus

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

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

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

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

Chapter 5: The Untyped Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING

CIS 500 Software Foundations Fall September 25

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.

The Untyped Lambda Calculus

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

1 Scope, Bound and Free Occurrences, Closed Terms

The Untyped Lambda Calculus

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

CSE 505: Concepts of Programming Languages

Lecture 5: The Untyped λ-calculus

Concepts of programming languages

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

Lecture 9: Typed Lambda Calculus

Programming Languages Fall 2014

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

CMSC 330: Organization of Programming Languages

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

1.3. Conditional expressions To express case distinctions like

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference

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

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

Lambda Calculus and Extensions as Foundation of Functional Programming

Types and Type Inference

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

An Introduction to the Lambda Calculus

Introduction to the λ-calculus

Costly software bugs that could have been averted with type checking

Pure Lambda Calculus. Lecture 17

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 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Introduction to the Lambda Calculus

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

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

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

dynamically typed dynamically scoped

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

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

Lambda Calculus. Lambda Calculus

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

Programming Language Concepts: Lecture 19

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

Functional Programming

Types, Type Inference and Unification

CMSC 330: Organization of Programming Languages. Lambda Calculus

From the λ-calculus to Functional Programming Drew McDermott Posted

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

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

λ calculus is inconsistent

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

Fundamentals and lambda calculus

Urmas Tamm Tartu 2013

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

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

Functional Programming. Pure Functional Languages

Programming Languages

Functional Languages and Higher-Order Functions

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Chapter 2 The Language PCF

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

The Untyped Lambda Calculus

Exercise 1 ( = 24 points)

Lecture Notes on Data Representation

CSE-321 Programming Languages 2012 Midterm

Introduction to the Lambda Calculus. Chris Lomont

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

CIS 500 Software Foundations Midterm I

Recursive Definitions, Fixed Points and the Combinator

Programming Languages

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

VU Semantik von Programmiersprachen

Mutable References. Chapter 1

(Advanced) topics in Programming Languages

Principles of Programming Languages 2017W, Functional Programming

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

Transcription:

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

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

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

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

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

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

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 5 5 10 (. (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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages

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

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

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

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

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

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

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

Retrospect

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

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

-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

-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

-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

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

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