Lecture 5: The Untyped λ-calculus

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

CMSC 330: Organization of Programming Languages

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

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

Introduction to the λ-calculus

The Untyped Lambda Calculus

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

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

CMSC330. Objects, Functional Programming, and lambda calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lambda Calculus and Extensions as Foundation of Functional Programming

dynamically typed dynamically scoped

Fundamentals and lambda calculus

CMSC 330: Organization of Programming Languages

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

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

CITS3211 FUNCTIONAL PROGRAMMING

CSE 505: Concepts of Programming Languages

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

Type Systems Winter Semester 2006

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

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

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

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

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

VU Semantik von Programmiersprachen

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

Lambda Calculus. Lambda Calculus

Chapter 5: The Untyped Lambda Calculus

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

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

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

Introduction to the Lambda Calculus

Lambda Calculus. Lecture 4 CS /26/10

Recursive Definitions, Fixed Points and the Combinator

Lecture 7: The Untyped Lambda Calculus

The Untyped Lambda Calculus

Lambda Calculus and Type Inference

More Lambda Calculus and Intro to Type Systems

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

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

Untyped Lambda Calculus

Lambda Calculus. Variables and Functions. cs3723 1

Untyped Lambda Calculus

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

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

Part I. Historical Origins

Programming Languages

International School on Rewriting (ISR 2015) Rewriting Techniques for Correctness of Program Transformations

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

1 Scope, Bound and Free Occurrences, Closed Terms

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

CIS 500 Software Foundations Fall September 25

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

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

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

Implementing functional languages with abstract machines

More Lambda Calculus and Intro to Type Systems

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

Chapter 5: The Untyped Lambda Calculus

CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

Pure Lambda Calculus. Lecture 17

Programming Languages Lecture 14: Sum, Product, Recursive Types

Lambda Calculus as a Programming Language

More Lambda Calculus and Intro to Type Systems

Introductory Example

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.

Functional Programming

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

Elixir, functional programming and the Lambda calculus.

3.1 λ-calculus: Syntax

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

Functional Languages and Higher-Order Functions

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

lambda calculus History 11/28/13 Jianguo Lu A formal system designed to inves:gate func:on defini:on, func:on applica:on, and recursion.

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

Concepts of programming languages

From the λ-calculus to Functional Programming Drew McDermott Posted

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

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

CSCI.4430/6969 Programming Languages Lecture Notes

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Lambda Calculus.

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

CS 242. Fundamentals. Reading: See last slide

Programming Languages Lecture 15: Recursive Types & Subtyping

CIS 500 Software Foundations Fall October 2

Transcription:

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, 2015-2016 1 / 36

Motivation Common programming languages are complex ANSI C99: 538 pages ANSI C++: 714 pages Java 20: 505 pages Not ideal for teaching and understanding principles of languages and program analysis Ideal: a core language with Essential features enough to express all computation No redundancy: encode extra features as syntactic sugar Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 2 / 36

Lambda Calculus Core language for sequential programming Can express all computation Still extremely simple and minimal Can encode many extensions as syntactic sugar Easy to extend with additional features Simple to understand Whole definition in one slide and fits in a can! http://alumwpiedu/~tfraser/software/arduino/lambdacanhtml Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 3 / 36

History Invented in the 1930s by Alonzo Church (1903-1995) Princeton Mathematician Lectures on λ-calculus published in 1941 Also known for Church s Thesis: Every effectively calculable (decidable) function can be expressed by recursive functions ie can be computed by λ-calculus Church s Theorem: The first order logic is undecidable Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 4 / 36

Syntax Simple syntax: e ::= x Variables λxe Function definition e e Function application Functions are the only language construct The argument is a function The result is a function Functions of functions are higher-order Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 5 / 36

Semantics To evaluate the term (λxe 1 ) e 2 Replace every x in e1 with e 2 Written as e 1 [e 2 /x], pronounced e 1 with e 2 for x Also written e 1 [x e 2 ] Evaluate the resulting term Return the result Formally called β-reduction (λxe 1 ) e 2 β e 1 [e 2 /x] A term that can be β-reduced is a redex We omit β when obvious Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 6 / 36

Convenient assumptions Syntactic sugar for declarations let x = e1 in e 2 really means (λxe 2 ) e 1 Scope of λ extends as far to the right as possible λxλyx y is λx(λy(x y)) Function application is left-associative x y z means (x y) z Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 7 / 36

Scoping and parameter passing β-reduction is not yet well-defined: (λxe1 ) e 2 e 1 [e 2 /x] There might be many x defined in e 1 Example Consider the program let x = a in let y = λzx in let x = b in y x Which x is bound to a, and which to b? Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 8 / 36

Static (Lexical) Scope Variable refers to closest definition We can rename variables to avoid confusion: let x = a in let y = λzx in let w = b in y w Renaming variables without changing the program meaning is called α-conversion Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 9 / 36

Free/bound variables The set of free variables of a term is FV(x) = x FV(λxe) = FV(e) \ {x} FV(e 1 e 2 ) = FV(e 1 ) FV(e 2 ) A term e is closed if FV(e) = A variable that is not free is bound Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 10 / 36

α-conversion Terms are equivalent up to renaming of bound variables λxe = λye[y/x] if y / FV(e) Used to avoid having duplicate variables, capturing during substitution This is called α-conversion, used implicitly Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 11 / 36

Substitution Formal definition x[e/x] = e y[e/x] = y when x y (e 1 e 2 )[e/x] = (e 1 [e/x] e 2 [e/x]) (λye 1 )[e/x] = λy(e 1 [e/x]) when y x and y / FV(e) Example (λxy x) x = α (λwy w) x β y x We omit writing α-conversion Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 12 / 36

Functions with many arguments We can t yet write functions with many arguments For example, two arguments: λ(x, y)e Solution: take the arguments, one at a time (like we do in OCaml) λxλye A function that takes x and returns another function that takes y and returns e (λxλye) a b (λye[a/x]) b e[a/x][b/y] This is called Currying Can represent any number of arguments Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 13 / 36

Representing booleans true = λxλyx false = λxλyy if a then b else c = a b c For example: if true then b else c (λxλyx) b c (λyb) c b if false then b else c (λxλyy) b c (λyy) c c Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 14 / 36

Combinators Any closed term is also called a combinator true and false are combinators Other popular combinators: I = λxx K = λxλyx S = λxλyλzx z (y z) We can define calculi in terms of combinators The SKI-calculus SKI-calculus is also Turing-complete Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 15 / 36

Encoding pairs (a, b) = λxif x then a else b fst = λpp true snd = λpp false Then fst (a, b) a snd (a, b) b Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 16 / 36

Natural numbers (Church) 0 = λsλzz 1 = λsλzs z 2 = λsλzs (s z) ie n = λsλz apply s n times to z succ = λnλsλzs (n s z) iszero = λnn (λsfalse) true Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 17 / 36

Natural numbers (Scott) 0 = λxλyx 1 = λxλyy 0 2 = λxλyy 1 ie n = λxλyy (n 1) succ = λzλxλyy z pred = λzz 0 (λxx) iszero = λzz true (λxfalse) Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 18 / 36

Nondeterministic semantics (λxe 1 ) e 2 e 1 [e 2 /x] e e (λxe) (λxe ) e 1 e 1 e 1 e 2 e 1 e 2 e 2 e 2 e 1 e 2 e 1 e 2 Question: why are these rules non-deterministic? Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 19 / 36

Example We can apply reduction anywhere in the term (λx(λyy) x ((λzw) x) λx(x ((λzw) x) λxx w (λx(λyy) x ((λzw) x) λx(λyy) x w λxx w Does the order of evaluation matter? Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 20 / 36

The Church-Rosser Theorem Lemma (The Diamond Property): If a b and a c, then there exists d such that b d and c d Church-Rosser theorem: If a b and a c, then there exists d such that b d and c d Proof by diamond property Church-Rosser also called confluence Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 21 / 36

Normal form A term is in normal form if it cannot be reduced Examples: λxx, λxλyz By the Church-Rosser theorem, every term reduces to at most one normal form Only for pure lambda calculus with non-deterministic evaluation Notice that for function application, the argument need not be in normal form Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 22 / 36

β-equivalence Let = β be the reflexive, symmetric, transitive closure of Eg, (λxx) y y (λzλwz) y y so all three are β-equivalent If a = β b, then there exists c such that a c and b c Follows from Church-Rosser theorem In particular, if a = β b and both are normal forms, then they are equal Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 23 / 36

Not every term has a normal form Consider = λxx x Then In general, self application leads to loops which is good if we want recursion Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 24 / 36

Fixpoint combinator Also called a paradoxical combinator Y = λf(λxf (x x)) (λxf (x x)) There are many versions of this combinator Then, Y F = β F (Y F) Y F = (λf(λxf (x x)) (λxf (x x))) F (λxf (x x)) (λxf (x x)) F ((λxf (x x)) (λxf (x x))) F (Y F) Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 25 / 36

Example fact (n) = if (n = 0) then 1 else n fact (n 1) Let G = λfλnif (n = 0) then 1 else n f(n 1) Y G 1 = β G (Y G) 1 =β (λfλnif (n = 0) then 1 else n f(n 1)) (Y G) 1 = β if (1 = 0) then 1 else 1 ((Y G) 0) =β 1 ((Y G) 0) = β 1 (G (Y G) 0) =β 1 (λfλnif (n = 0) then 1 else n f(n 1) (Y G) 0) = β 1 (if (0 = 0) then 1 else 0 ((Y G) 0)) =β 1 1 = 1 Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 26 / 36

In other words The Y combinator unrolls or unfolds its argument an infinite number of times Y G = G (Y G) = G (G (Y G)) = G (G (G (Y G))) = G needs to have a base case to ensure termination But, only works because we follow call-by-name Different combinator(s) for call-by-value Z = λf(λxf (λyx x y)) (λxf (λyx x y)) Why is this a fixed-point combinator? How does its difference from Y work for call-by-value? Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 27 / 36

Why encodings It s fun! Shows that the language is expressive In practice, we add constructs as language primitives More efficient Much easier to analyze the program, avoid mistakes Our encodings of 0 and true are the same, we may want to avoid mixing them, for clarity Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 28 / 36

Lazy and eager evaluation Our non-deterministic reduction rule is fine for theory, but awkward to implement Two deterministic strategies: Lazy: Given (λxe 1 ) e 2, do not evaluate e 2 if e 1 does not need x anywhere Also called left-most, call-by-name, call-by-need, applicative, normal-order evaluation (with slightly different meanings) Eager: Given (λxe 1 ) e 2, always evaluate e 2 to a normal form, before applying the function Also called call-by-value Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 29 / 36

Lazy operational semantics (λxe 1 ) l (λxe 1 ) e 1 l λxe e[e 2 /x] l e e 1 e 2 l e The rules are deterministic, big-step The right-hand side is reduced all the way The rules do not reduce under λ The rules are normalizing: If a is closed and there is a normal form b such that a b, then a l d for some d Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 30 / 36

Eager (big-step) semantics (λxe 1 ) e (λxe 1 ) e 1 e λxe e 2 e e e[e /x] e e e 1 e 2 e e This big-step semantics is also deterministic and does not reduce under λ But is not normalizing! Example: let x = in (λyy) Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 31 / 36

Eager Fixpoint The Y combinator works for lazy semantics Y = λf(λxf (x x))(λxf (x x)) The Z combinator does the same for eager (call-by-value) semantics Z = λf(λxf (λyx x y))(λxf (λyx x y)) Why doesn t the Y combinator work for call-by-value? Why does Z do the same thing for call-by-value? Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 32 / 36

Lazy vs eager in practice Lazy evaluation (call by name, call by need) Has some nice theoretical properties Terminates more often Lets you play some tricks with infinite objects Main example: Haskell Eager evaluation (call by value) Is generally easier to implement efficiently Blends more easily with side-effects Main examples: Most languages (C, Java, ML, ) Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 33 / 36

Functional programming The λ calculus is a prototypical functional programming language Higher-order functions (lots!) No side-effects In practice, many functional programming languages are not pure : they permit side-effects But you re supposed to avoid them Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 34 / 36

Functional programming today Two main camps Haskell Pure, lazy functional language; no side-effects ML (SML, OCaml) Call-by-value, with side-effects Old, still around: Lisp, Scheme Disadvantage/feature: no static typing Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 35 / 36

Influence of functional programming Functional ideas move to other langauges Garbage collection was designed for Lisp; now most new languages use GC Generics in C++/Java come from ML polymorphism, or Haskell type classes Higher-order functions and closures (used in Ruby, exist in C#, proposed to be in Java soon) are everywhere in functional languages Many object-oriented abstraction principles come from ML s module system Pratikakis (CSD) Untyped λ-calculus I CS49040, 2015-2016 36 / 36