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

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

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

Concepts of programming languages

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-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.

Introduction to the Lambda Calculus

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

The Untyped Lambda Calculus

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

CSE 505: Concepts of Programming Languages

Pure Lambda Calculus. Lecture 17

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

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

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

Lecture 5: The Untyped λ-calculus

Introduction to the λ-calculus

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

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

Formal Systems and their Applications

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

1 Scope, Bound and Free Occurrences, Closed Terms

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

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

Fundamentals and lambda calculus

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

CMSC 330: Organization of Programming Languages

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

Costly software bugs that could have been averted with type checking

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

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

Functional Programming

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

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.

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

The Untyped Lambda Calculus

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)

Lambda Calculus. Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING

CMSC 330: Organization of Programming Languages. Lambda Calculus

CIS 500 Software Foundations Midterm I

Functional Languages and Higher-Order Functions

Introduction to the Lambda Calculus. Chris Lomont

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

Lambda Calculus. Variables and Functions. cs3723 1

Lecture 9: Typed Lambda Calculus

From the λ-calculus to Functional Programming Drew McDermott Posted

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

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

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

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

Elixir, functional programming and the Lambda calculus.

dynamically typed dynamically scoped

Lambda Calculus and Type Inference

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

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

Lambda Calculus-2. Church Rosser Property

Lambda Calculus and Extensions as Foundation of Functional Programming

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

Urmas Tamm Tartu 2013

Graphical Untyped Lambda Calculus Interactive Interpreter

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 alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

Programming Language Pragmatics

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

Functional Languages. Hwansoo Han

Lambda Calculus and Type Inference

An Introduction to the Lambda Calculus

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

λ calculus is inconsistent

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

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

Lexicografie computationala Feb., 2012

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

Recursive Definitions, Fixed Points and the Combinator

1.3. Conditional expressions To express case distinctions like

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

Programming Languages

Chapter 11 :: Functional Languages

CS 6110 S14 Lecture 1 Introduction 24 January 2014

VU Semantik von Programmiersprachen

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

CMSC 330: Organization of Programming Languages

Transcription:

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

Computation Models Turing Machines Wang Machines Counter Programs Lambda Calculus

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: (λ. λ. (λ.) ) ((λ. ) λ.) (λt. λf. t) (λ.) ((λ.) (λs. λz. s z))

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 ) Eamples: FV( (y z) ) = FV(. y. (y z) ) = FV( (. ) ) = FV( (. ) ) = 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)

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

[ 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

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

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 10

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

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 12

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 13

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 14

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

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

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

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

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

Summary Order of Evaluation Full-beta-reduction All possible orders Applicative order call by value (strict) 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

Different Evaluation Orders (. y. ( z. z) y) (( u. u) ( w. w)) y u w u w z y z

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

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

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

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 25

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 = 26

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 = 27

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 28

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

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

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

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

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 33

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

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 35