Untyped Lambda Calculus

Similar documents
Untyped Lambda Calculus

Lambda Calculus. Concepts in Programming Languages Recitation 6:

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Lambda Calculus.

Type Systems Winter Semester 2006

Chapter 5: The Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus

CIS 500 Software Foundations Fall September 25

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

Lambda Calculus. Lecture 4 CS /26/10

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

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

Concepts of programming languages

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

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.

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Introduction to the Lambda Calculus

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

The Untyped Lambda Calculus

Pure Lambda Calculus. Lecture 17

CSE 505: Concepts of Programming Languages

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

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

Introduction to the λ-calculus

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

Lecture 5: The Untyped λ-calculus

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

Formal Systems and their Applications

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

1 Scope, Bound and Free Occurrences, Closed Terms

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

CMSC 330: Organization of Programming Languages

The Untyped Lambda Calculus

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

Costly software bugs that could have been averted with type checking

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

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

Functional Programming

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

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

Fundamentals and lambda calculus

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

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

CITS3211 FUNCTIONAL PROGRAMMING

The Untyped Lambda Calculus

Lambda Calculus. Lambda Calculus

CMSC 330: Organization of Programming Languages

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

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.

CIS 500 Software Foundations Midterm I

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

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

Functional Languages and Higher-Order Functions

The Untyped Lambda Calculus

Lecture 9: Typed Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

Introduction to the Lambda Calculus. Chris Lomont

Elixir, functional programming and the Lambda calculus.

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

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

From the λ-calculus to Functional Programming Drew McDermott Posted

Lambda Calculus and Extensions as Foundation of Functional Programming

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

dynamically typed dynamically scoped

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

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

Lambda Calculus. Variables and Functions. cs3723 1

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

Lambda Calculus-2. Church Rosser Property

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

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

Urmas Tamm Tartu 2013

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

Graphical Untyped Lambda Calculus Interactive Interpreter

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

Lambda Calculus and Type Inference

Programming Language Pragmatics

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

An Introduction to the Lambda Calculus

Recursive Definitions, Fixed Points and the Combinator

1.3. Conditional expressions To express case distinctions like

Functional Languages. Hwansoo Han

λ calculus is inconsistent

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

CMSC 330: Organization of Programming Languages

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

VU Semantik von Programmiersprachen

Functional Programming

Lexicografie computationala Feb., 2012

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

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

Lambda Calculus and Type Inference

Programming Languages

Transcription:

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 Programming Languages by Benjamin C. Pierce, Chapter 5 1

Computation Models Turing Machines Wang Machines Counter Programs Lambda Calculus 2

Historical Contet Like Alan Turing, another mathematician, Alonzo Church, was very interested, during the 1930s, in the question What is a computable function? He developed a formal system known as the pure lambda calculus, in order to describe programs in a simple and precise way. Today the Lambda Calculus serves as a mathematical foundation for the study of functional programming languages, and especially for the study of denotational semantics. Reference: http://en.wikipedia.org/wiki/lambda_calculus 3

t ::=. t t t Untyped Lambda Calculus - Synta 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) ) Eamples (taken from 2015 eams): (λ. λ. (λ.) ) ((λ. ) λ.) (λt. λf. t) (λ.) ((λ.) (λs. λz. s z)) 4

Free vs. Bound Variables An occurrence of in t is bound in. t otherwise it is free is a binder Eamples. y. (y z) z.. y. (y z) (. ) 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 ) 5

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) 6

Beta-Reduction: Eamples (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede (. ) y y (. (. ) ) (u r) u r (. ) ( ( w. w)) (y z) w. y z w 7

[ s] = s Substitution Subtleties (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) [ s] y = y [ s] ( y. t 1 ) = y. [ s] t 1 [ s] (t 1 t 2 ) = ([ s] t 1 ) ([ s] t 2 ) if y if y and y FV(s) (. (. )) y [ y] (. ) =. y? (. ( y. )) y [ y] ( y. ) = y. y? (. (. )) y and (. ( y. )) y are stuck! They have no -reduction 8

Alpha Conversion Alpha conversion: Renaming of a bound variable and its bound occurrences (. t) y. [ y] t if y FV(t). (. (. )) y (. ( z. z)) y [ y] ( z. z) = z. z. y (. ( y. )) y (. ( z. )) y [ y] ( z. ) = z. y y. y 9

Currying Multiple arguments Say we want to define a function with two arguments: f = (, y). s We do this by Currying: f =. y. s f is now a function of that returns a function of y Currying and -reduction: f v w = (f v) w = ((. y. s) v) w ( y.[ v]s ) w [ v] [y w] s Conclusion: f = (, y). s f =. y. s f (v,w) f v w 10

Church Booleans Define: tru = t. f. t fls = t. f. f test = l. m. n. l m n test tru then else = ( l. m. n. l m n) ( t. f. t) then else ( m. n. ( t. f. t) m n) then else ( n. ( t. f. t) then n) else ( t. f. t) then else ( f. then) else then test fls then else = ( l. m. n. l m n) ( t. f. f) then else ( m. n. ( t. f. f) m n) then else ( n. ( t. f. f) then n) else ( t. f. f) then else ( f. f) else else and = b. c. b c fls or = not = 11

Church Numerals c 0 = s. z. z c 1 = s. z. s z c 2 = s. z. s (s z) c 3 = s. z. s (s (s z)) scc = n. s. z. s (n s z) plus = m. n. s. z. m s (n s z) times = m. n. m (plus n) c 0 iszero = 12

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? 13

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 14

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 15

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 16

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) 17

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

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 19

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

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

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

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

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

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

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()) 26

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 27

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

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 29

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 30

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) 31

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? 32

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) 33

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? 34

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 35