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

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

CMSC 330: Organization of Programming Languages

Introduction to the λ-calculus

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

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

The Untyped Lambda Calculus

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

Chapter 5: The Untyped Lambda Calculus

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

Lecture 5: The Untyped λ-calculus

CMSC330. Objects, Functional Programming, and lambda calculus

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lambda Calculus and Extensions as Foundation of Functional Programming

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

CMSC 330: Organization of Programming Languages

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

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

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

Lambda Calculus and Type Inference

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

Formal Systems and their Applications

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

CMSC 330: Organization of Programming Languages

Lambda Calculus. Lecture 4 CS /26/10

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

The Untyped Lambda Calculus

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

Lambda Calculus. Variables and Functions. cs3723 1

Concepts of programming languages

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

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

Type Systems Winter Semester 2006

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

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

CIS 500 Software Foundations Fall September 25

Fundamentals and lambda calculus

Elixir, functional programming and the Lambda calculus.

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

Implementing functional languages with abstract machines

VU Semantik von Programmiersprachen

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

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

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Handout 10: Imperative programs and the Lambda Calculus

CSE 505: Concepts of Programming Languages

Pure Lambda Calculus. Lecture 17

Introduction to lambda calculus Part 3

Introductory Example

1 Scope, Bound and Free Occurrences, Closed Terms

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

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

CITS3211 FUNCTIONAL PROGRAMMING

The Untyped Lambda Calculus

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

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

Recursive Definitions, Fixed Points and the Combinator

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

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

Lambda Calculus. Lambda Calculus

dynamically typed dynamically scoped

Programming Languages Lecture 14: Sum, Product, Recursive Types

Chapter 5: The Untyped Lambda Calculus

Advanced Seminar Softwareengineering - Denotational semantics

Lexicografie computationala Feb., 2012

Introduction to the Lambda Calculus

Untyped Lambda Calculus

The Untyped Lambda Calculus

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Programming Languages (CSCI 4430/6430) History, Syntax, Semantics, Essentials, Paradigms

10.6 Theoretical Foundations

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

Functional Programming

Untyped Lambda Calculus

Functional programming languages

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

3.1 λ-calculus: Syntax

Programming Languages

CIS 500 Software Foundations Fall October 2

Lambda Calculus-2. Church Rosser Property

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.

CSCI.4430/6969 Programming Languages Lecture Notes

Lecture 9: Typed Lambda Calculus

Lecture Notes on Program Equivalence

Introduction to the Lambda Calculus. Chris Lomont

Lambda Calculus. Concepts in Programming Languages Recitation 6:

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

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

Part I. Historical Origins

> module Lambda where > import Data.List -- I need some standard list functions

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

An Inverse Lambda Calculus Algorithm. For Natural Language Processing. Marcos Alvarez Gonzalez

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

Transcription:

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

Pure (untyped) λ-calculus An example of a simple formal language Invented by Alonzo Church (1936) Used as a core language (a calculus capturing the essential mechanisms of the language, with other features understood by way of translating them into the core) by Peter Landin (1964) Has seen widespread use for specification of programming language features, in language design and implementation, and in the study of type systems Is the underlying computational substrate for many type systems 2

Pure (untyped) λ-calculus Can be viewed both as a programming language in which computations can be described and as a mathematical object about which rigorous statements can be proved In its original form, all computation in λ-calculus is reduced to function definition and function application. But it can be easily enriched in a variety of ways, by adding special concrete syntax for features like numbers, booleans, tuples, etc. Inspired other core calculi used for similar purposes (π-calculus by Milner, Parrow and Walker, 1991, for defining semantics of message-based concurrent languages; object calculus by Abadi and Cardelli, 1996, for core features of object-oriented languages) 3

Pure (untyped) λ-calculus We will start by giving syntactical rules the rules that define the structure of terms of λ-calculus These rules can be used to generate new terms, or they can be used to verify whether a particular sequence of words is a valid term in λ-calculus We will discuss the meaning of the terms a bit later! 4

Syntax of λ-calculus We will define the syntax using a BNF grammar t ::= x v t t Each term is either a variable (x stands for any variable), or a value, or an application (two terms separated by a space) v ::= λx.t All values are λ-abstractions, which are built up by λ, followed by some variable name, followed by a dot, followed by some other term. For example, these are valid values: λx.x λx.y λx.(x y) λy.(λz.(y z)) The parentheses are usually omitted as an abstraction always stretches as far to the right as possible 5

Syntax of λ-calculus The same BNF grammar can also be written like this: t ::= v ::= x v t t λx.t 6

Syntax of λ-calculus Let us now extend the calculus by adding integers to values and addition and subtraction to terms t ::= x v t t t + t t t v ::= λx.t n We can now give some intuition behind λ-calculus 7

Intuition We can think of each λ-abstraction as a function: λx.x+4 takes x and returns x+4 Then each application becomes an application of the function: (λx.x+4) 5 becomes 5+4 The beauty of λ-calculus is that a function can equally well be applied on any term, for example, another function: (λx.x y) (λz.z+3) becomes (λz.z+3) y becomes y+3 And we can combine several λ-abstractions to define functions of several arguments: λx.λy.x+y takes x and y and returns x+y (This is sometimes written as λx y.x+y) 8

Examples of terms A term in λ-calculus A mathematical expression 5+x 5+x a b a-b λx.x+4 f(x), where f(x)=x+4 λx.λy.x+y+z g(x,y), where g(x,y)=x+y+z (λx.x+4) 12 f(12), where f(x)=x+4 (λx.λy.x+y+z) 1 3 g(1,3), where g(x,y)=x+y+z (λx.λy.x+y+z) 2 k(y) = g(2,y), where g(x,y)=x+y+z λx.λy.x (y z) h(x,y), where h(x,y)=x(y(z)) * We say that an abstraction binds a variable * Each occurrence of a variable in a term is either bound or free * A term with no free variables is called closed, or a combinator 9

Examples of terms A term in λ-calculus A mathematical expression 5+x 5+x a b a-b λx.x+4 f(x), where f(x)=x+4) λx.λy.x+y+z application g(x,y), is left-associative, where g(x,y)=x+y+z so we leave out (λx.x+4) 12 parentheses f(12), from where ((λx.λy.x+y+z) f(x)=x+4 1) 3 (λx.λy.x+y+z) 1 3 g(1,3), where g(x,y)=x+y+z (λx.λy.x+y+z) 2 g 1 (y) = g(2,y), where g(x,y)=x+y+z λx.λy.x (y z) h(x,y), where h(x,y)=x(y(z)) * We say that an abstraction binds a variable * Each occurrence of a variable in a term is either bound or free * A term with no free variables is called closed, or a combinator 10

β-reduction β-reduction of an application: (λx.t 1 ) t 2 [x t 2 ] t 1 substitution of all free occurrences of x in t 1 by t 2 It is important that only free occurrences are substituted! For example: (λx. x + (λx. x - 3) 4) 10 [x 10] x + (λx. x - 3) 4 10 + (λx. x - 3) 4 which should clearly give us 11 If we replaced all occurrences of x with 10, we would have 10 + (λx. 10-3) 4 which would give us 17 11

α-conversion α-conversion: λx.t 1 λy.[x y] t 1 change of the name of a bound variable Two terms are normally considered equivalent up to α-conversion : λx.x + 4 λy.y + 4 It is often handy (but not strictly required) to perform α-conversion to distinguish between free and bound occurrences of the same variable: x (λx.x + 4) 7 x (λy.y + 4) 7 12

Evaluation Operational semantics of λ-calculus is given by defining an evaluation relation, denoted by In general, evaluation may not be deterministic, i.e. there may be more than one place in the term that can be reduced Even when evaluation is not deterministic, it is possible that any evaluation path ultimately leads to the same value Evaluation function is typically a partial function, i.e. some terms may be stuck, for example 3 + (λx.x + 4) 13

Operational Semantics t 2 t 2 t 1 t 2 t 1 t 2 (App1) (λx.t 1 ) v [x v] t 1 (App2) t 1 t 1 t 1 + t 2 t 1 + t 2 (Add1) t 1 t 1 t 1 t 2 t 1 t 2 (Sub1) t 2 t 2 v + t 2 v + t 2 (Add2) t 2 t 2 v t 2 v t 2 (Sub2) v 1 + v 2 = v 3 v 1 + v 2 v 3 (Add3) v 1 v 2 = v 3 v 1 v 2 v 3 (Sub3) 14

Operational Semantics t 2 t 2 t 1 t 2 t 1 t 2 (λx.t 1 ) v [x v] t 1 (App1) (App2) Is this evaluation relation deterministic? t 1 t 1 t 1 + t 2 t 1 + t 2 (Add1) t 1 t 1 t 1 t 2 t 1 t 2 (Sub1) t 2 t 2 v 1 + t 2 v + t 2 (Add2) t 2 t 2 v 1 t 2 v t 2 (Sub2) v 1 + v 2 = v 3 v 1 + v 2 v 3 (Add3) v 1 v 2 = v 3 v 1 v 2 v 3 (Sub3) 15

Encoding booleans The original λ-calculus is in fact sufficiently expressive in itself. Practically any other language constructs can be encoded in it, including natural numbers and booleans. However, this encoding is often cumbersome and difficult to work with, so it is common to add other constructs as primitives (as we did with integers and operations on integers). At the same time, people normally try to give added primitives the same semantics as their encodings would have in the original λ- calculus. 16

Encoding booleans Booleans can be encoded as follows: True = λx.λy.x False = λx.λy.y if z then x else y = z x y For example: if True then 5 else 6 becomes (λx.λy.x) 5 6 ([x 5](λy.x)) 6 = (λy.5) 6 [y 6]5 = 5 and if False then 5 else 6 becomes (λx.λy.y) 5 6 ([x 5](λy.y)) 6 = (λy.y) 6 [y 6]y = 6 17