The Untyped Lambda Calculus

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

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

Lecture 5: The Untyped λ-calculus

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

Lambda Calculus. Lecture 4 CS /26/10

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

Activity. CSCI 334: Principles of Programming Languages. Lecture 4: Fundamentals II. What is computable? What is computable?

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

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

Lambda Calculus. Variables and Functions. cs3723 1

VU Semantik von Programmiersprachen

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

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

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

Chapter 5: The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

Lambda Calculi With Polymorphism

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

Fundamentals and lambda calculus

Pure Lambda Calculus. Lecture 17

Lambda Calculi With Polymorphism

Lambda Calculus and Type Inference

The Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus

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

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

CMSC 330: Organization of Programming Languages

Untyped Lambda Calculus

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

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CITS3211 FUNCTIONAL PROGRAMMING

dynamically typed dynamically scoped

Type Systems Winter Semester 2006

Introduction to the Lambda Calculus

Untyped Lambda Calculus

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Formal Systems and their Applications

3.1 λ-calculus: Syntax

Lambda Calculus.

Introduction to the λ-calculus

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

Lambda Calculus and Extensions as Foundation of Functional Programming

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

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

Concepts of programming languages

Lambda Calculus. Concepts in Programming Languages Recitation 6:

1 Scope, Bound and Free Occurrences, Closed Terms

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages

CIS 500 Software Foundations Fall September 25

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

Lambda Calculus and Type Inference

Recursive Definitions, Fixed Points and the Combinator

CMSC330. Objects, Functional Programming, and lambda calculus

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

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

10.6 Theoretical Foundations

Programming Languages

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

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

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

Lexicografie computationala Feb., 2012

Lambda Calculus. Lambda Calculus

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

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

An Introduction to the Lambda Calculus

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

CSE 341: Programming Languages

Part I. Historical Origins

Week 4: Functional Programming

λ calculus is inconsistent

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Graphical Untyped Lambda Calculus Interactive Interpreter

(Refer Slide Time: 4:00)

Programming Languages. Dr. Philip Cannata 1

Costly software bugs that could have been averted with type checking

lci Manual Kostas Chatzikokolakis 12 March 2006

Introductory Example

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

From the λ-calculus to Functional Programming Drew McDermott Posted

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

Programming Language Concepts: Lecture 19

Lambda Calculus. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Functional Languages and Higher-Order Functions

CSCI.4430/6969 Programming Languages Lecture Notes

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.

Transcription:

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 based on [Pierce] as the underlying textbook. x(1).!x(1) x.set(1) Programming Paradigms and Formal Semantics The Untyped Lambda Calculus Ralf Lämmel [Järvi] Slides by J. Järvi: Programming Languages, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002 1h

What s the lambda calculus? It is the core of functional languages. It is a mathematical system for studying programming languages. design, specification, implementation, type systems, et al. It comes in variations of typing: implicit/explicit/none. Formal systems built on top of simply typed lambda calculus: System F for studying polymorphism System F<: for studying subtyping... 140

Lambda calculus examples x λx.x λf.λg.λx.f (g x) The identity function Function composition 141

Lambda calculus overview Abstract syntax: M is a lambda term. M ::= x M M λx.m An infinite set of variables x, y, z,... is assumed. M N is an application. Function M is applied to the argument N. λx.m is an abstraction. The resulting function maps x to M. Lambda functions are anonymous. 142

What s the lambda calculus? Church s thesis: All intuitively computable functions are λ-definable. An established equivalence of notions of computability: Set of Lambda-definable functions = Set of Turing-computable functions 143

Formalization of the lambda calculus Syntax t ::=x λx.t tt Terms Evaluation v ::=λx.t Values (normal forms) t 1 t 1 t t t 1 t 2 t 1 t 2 vt vt (λx.t) v [v/x]t Reduce function position, then reduce argument position, then apply. 144

Syntactic sugar and conventions M N1...Nk means (...((M N1) N2)... Nk). Function application groups from left to right. λx.x y means (λx.(x y)). Function application has higher precedence. λx1x2... xk. M means λx1.(λx2.(...(λxk.(m))...))). 145

Variable binding λ is a binding operator: It binds a variable in the scope of the lambda abstraction. Examples: λx.m λx.x y x is bound (in the lambda abstraction) y is not bound (in the lambda abstraction). If a variable occurs in an expression without being bound, then it is called a free occurrence, or a free variable. Other occurrences of variables are called bound. A closed term is one without free variable occurrences. 146

Variable binding precise definition FV(M) defines the set of free variables in the term M FV(x) ={x} FV(MN) =FV(M) FV(N) FV(λx.M) =FV(M) \{x} 147

Exercise: what are the free and bound variable occurrences in these terms? (λx.y)(λy.y) λx.(λy.x y)y 148

Substitution and β-equivalence Computation for the λ-calculus is based on substitution. Substitution is defined by the equational axiom: (λx.m)n = [N/x]M Think of substitution as invoking a function: The terms on both sides are also called β-equivalent. (λx.m) is the function, N is the argument, Substitution takes care of parameter passing. 149

α-equivalence and conversion Names of bound variable are insignificant. λx.x defines the same function as λy.y Suppose two terms differ only on the names of bound variables. Then, they are said to be α-equivalent ( =α ). Equational axiom: λx.m = λy.[y/x]m where y does not appear in M Performing such renaming is also called α-conversion. and substitution applies to free occurrences only. 150

Reduction M N Redex (reducible expression) (λx.m )N Apply substitution; this is called β-reduction. (λx.m)n [N/x]M Computation with the lambda calculus is then a series of β-reductions, and α-conversions. Normal form: a term that cannot be reduced further. 151

Inductive definition of substitution [N/x]x = N [N/x]y = y, y any variable different from x [N/x](M 1 M 2 )=([N/x]M 1 )([N/x]M 2 ) [N/x](λx.M) =λx.m [N/x](λy.M) =λy.([n/x]m), y not free in N Examples [z/x]x [z/x](λx.x x) [z/x](λy.y x) [z/x](λz.x z) z If this condition is not met, then alpha conversion is needed. λx.x x λy.y z λa.z a 152

Properties of reduction (i.e., semantics) How do we select redexes for reduction steps? Does the result depend on such a choice? Does reduction ultimately terminate with a normal form? 153

Illustration of different reductions (We assume natural numbers with +.) Option 1 (λf.λx.f (f x)) (λy.y + 1) 2 (λx.(λy.y +1) ((λy.y +1) x)) 2 (λx.(λy.y +1) (x +1)) 2 (λx.(x+1+1)) 2 (2+1+1) 4 Option 2 (λf.λx.f (f x)) (λy.y + 1) 2 (λx.(λy.y +1) ((λy.y +1) x)) 2 (λy.y +1) ((λy.y +1) 2)...... 4 154

Confluence Confluence: evaluation strategy is not significant for final value. That is: there is (at most) one normal form of a given expression. [http://en.wikipedia.org/wiki/church Rosser_theorem] 155

Strong normalization property of a calculus with reduction Definition: For every term M there is a normal form N such that M * N. Strong normalization properties for lambda calculi: Untyped lambda calculus: no (λx.x x)(λx.x x) Simply-typed lambda calculus: yes 156

Evaluation/reduction strategies Full beta reduction Choice of strategy may impact termination behavior Reduce anywhere. Applicative order ( reduce the leftmost innermost redex ) Reduce argument before applying function. Normal order ( reduce the leftmost outermost redex ) Apply function before reducing argument. eager (strict) lazy (non-strict) http://en.wikipedia.org/wiki/evaluation_strategy 157

Extension vs. encoding Typical extensions (giving rise to so-called applied lambda calculi) Primitive types (numbers, Booleans,...) Type constructors (tuples, records,...) Recursive functions Effects (cell, exceptions,...)... Many extensions can be encoded in theory in terms of pure lambda calculus, except that such encoding is somewhat tedious. 158

Church Booleans Encodings of literals true = λt.λf.t false = λt.λf.f Conditional expression (if) Expectations test b v w * v, if b = true test b v w * w, if b = false Encoding test = λl.λm.λn.l m n 159

Example reduction (λl.λm.λn.l m n) true v w (λm.λn.true m n) v w (λn.true v n) w true v w (λt.λf.t) v w Likewise for numerals or pairs. (λf.v)w v 160

Encodings of numbers c0 = λs.λz.z c1 = λs.λz.s z c2 = λs.λz.s (s z) c3 = λs.λz.s (s (s z)) Church numerals... Encodings of functions on numbers succ = λn.λs.λz.s (n s z) plus = λm.λn.λs.λz.m s (n s z) times = λm.λn.m (plus n) c0... A numeral n is a lambda abstraction that is parameterized by a case for zero, and a case for succ. In the body, the latter is applied n times to the former. This caters for primitive recursion (akin to the Boolean TEST). 161

Recursive functions Let us define the factorial function. Suppose we had recursive function definitions. f λn. if n == 0 then 1 else n*f(n - 1) Can we do such recursion with anonymous functions. We can use a fixed point combinator instead. 162

Fixed points Consider a function f : X X. A fixed point of f is a value z such that z = f(z). A function may have none, one, or multiple fixed points. Examples (functions and sets of fixed points): f(x) = 2x f(x) = x f(x) = x + 1 {0} {0,1,...} We call Y a fixed-point combinator if it satisfies the following definitional property: For all f : X X it holds that Y f = f (Y f) 100$ Question: does such a Y exist? 163

Defining factorial as a fixed point Start from a recursive definition. f λn. if n == 0 then 1 else n*f(n - 1) Eliminate self-reference; receive function as argument. g λh.λn.if n == 0 then 1 else n*h(n - 1) g takes a function (h) and returns a function. Define f as a fixed point. f Y g 164

Fixed points cont d For example, apply definitional property to factorial g: (Y g) 2 =[Y def. prop.] g (Y g) 2 =[unfold g] (λ h.λ n.if n == 0 then 1 else n*h(n - 1)) (Y g) 2 =[beta reduce] (λ n.if n == 0 then 1 else n*((y g)(n - 1))) 2 =[beta reduce] if 2 == 0 then 1 else 2*((Y g)(2-1)) =[ - reduce] if 2 == 0 then 1 else 2*((Y g)(1)) =[ if reduce] 2*((Y g)(1)) =... = 2 This is as if we had extended evaluation. Apply these steps one more time. 165

A lambda term for Y One option: Y = λf.(λx.f (x x))(λx.f (x x)) Verification of the definitional property: Proof: Y g = g (Y g) Y g Not suitable for applicative order. =[unfold Y] (λf.(λx.f (x x))(λx.f (x x))) g =[beta reduce] (λx.g(x x))(λx.g(x x))) =[beta reduce] g ((λx.g(x x)) (λx.g(x x))) =[fold Y] g (Y g) 166

Summary: The untyped lambda calculus A concise core of functional programming. A foundation of computability. Prepping: Types and Programming Languages Chapter 5 Lab: NB and the lambda calculus in Prolog Outlook: The simply-typed lambda calculus 167