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

Similar documents
Chapter 5: The Untyped Lambda Calculus

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

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.

Fundamentals and lambda calculus

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

A Tutorial Introduction to the Lambda Calculus

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

Type Systems Winter Semester 2006

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

Lecture 5: The Untyped λ-calculus

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

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

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

Lambda Calculus. Lecture 4 CS /26/10

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

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages

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

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

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

Lambda Calculus. Lambda Calculus

Pure Lambda Calculus. Lecture 17

Introduction to the Lambda Calculus

CIS 500 Software Foundations Fall September 25

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

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

Concepts of programming languages

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

Introductory Example

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

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

The Untyped Lambda Calculus

Recursive Definitions, Fixed Points and the Combinator

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

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

1 Scope, Bound and Free Occurrences, Closed Terms

CITS3211 FUNCTIONAL PROGRAMMING

Introduction to the λ-calculus

Part I. Historical Origins

CSE 505: Concepts of Programming Languages

dynamically typed dynamically scoped

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

Elixir, functional programming and the Lambda calculus.

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

The Untyped Lambda Calculus

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

Introduction to the Lambda Calculus. Chris Lomont

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

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

CMSC330. Objects, Functional Programming, and lambda calculus

Lambda Calculus. Variables and Functions. cs3723 1

VU Semantik von Programmiersprachen

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

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

Lambda Calculus-2. Church Rosser Property

301AA - Advanced Programming

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Lambda Calculus and Extensions as Foundation of Functional Programming

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.

Chapter 5: The Untyped Lambda Calculus

Functional Programming

More Lambda Calculus and Intro to Type Systems

An Introduction to the Lambda Calculus

CSCI.4430/6969 Programming Languages Lecture Notes

Untyped Lambda Calculus

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

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

Formal Systems and their Applications

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.

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Untyped Lambda Calculus

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

CS522 - Programming Language Semantics

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

CIS 500 Software Foundations Midterm I

Lexicografie computationala Feb., 2012

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

More Lambda Calculus and Intro to Type Systems

Lambda Calculus and Type Inference

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

Functional Languages and Higher-Order Functions

T λx (λy. x) F λx (λ y. y)) if λp (λc (λa. (pc)a)

CMSC 330, Fall 2018 Midterm 2

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages. Operational Semantics

Note regarding Projects... Every team will need to do a different project (within a section)

Advanced Seminar Softwareengineering - Denotational semantics

3.1 λ-calculus: Syntax

Transcription:

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

Outline 1 λ-calculus 2 Operational Semantics Syntax Conversions Normal Form 3 Lambda Calculus as a Functional Language Aritmethic Operations Boolean Expressions Tuples 4 Food for Tought

What is Computable? Computation is usually modelled as a mapping from inputs to outputs, carried out by a formal machine or program, which processes its input in a sequence of steps. An effectively computable function is one that can be computed in a finite amount of time using finite resources.

Computation Models Turing Machine (Turing, 1936) modifies the content of the memory and of an input tape whose movement is controllable corresponds to imperative languages where the state of the computation process is represented and explicitly processed Lambda Calculus (Church, 1932) works on unary functions relies on function application the substitution in the function body of the formal parameter with the effective parameter processing without state (1912-1954) (1903-1995) Church-Turing thesis If an algorithm (a procedure that terminates) exists then there is an equivalent Turing Machine or applicable λ-function for that algorithm.

Lambda Calculus 1 universal: any computable function can be expressed and evaluated using this formalism 2 to directly express higher-order functions 3 to study evaluation orders, termination, uniqueness of answers 4 to serve as a kernel language for functional languages 5 the smallest universal programming language of the world

Outline 1 λ-calculus 2 Operational Semantics Syntax Conversions Normal Form 3 Lambda Calculus as a Functional Language Aritmethic Operations Boolean Expressions Tuples 4 Food for Tought

Syntax Syntax E = x λx. E E 1 E 2 x - variable λx.e - functional abstraction (x - bound variable, E - body) E 1 E 2 - function application (E 1 function, E 2 argument) <expression> := <name> <function> <application> <function> := λ <name>.<expression> <application> := <expression><expression> Examples: x λ-expression reduced to a variable λx.x is a function taking an argument x, and returning x (identity fun) λx. λy.x function which selects the first value from a pair of values f x is a function f applied to an argument x (same as f(x)) (λx.x) y applying the function λx.x on the argument y

Syntax Examples (λx. x x) (λy. y) = (λy. y) (λx.(x + 5)) 3 = 3+5 = 8 ((λx.(λy.x + y)) 3) 5 = (λy.3 + y)) 5 = 3+5=8 (λf.λx.f(f x))(λy.y+1) = λx.(λy.y+1)((λy.y+1) x) = λx.(λy.y+1)(x+1) = λx.x+1+1

Syntax Parsing Lambda Expressions 1 Lambda extends as far as possible to the right λf.x y λ f.(x y) λx.e1 E2 E3 λ x.(e1 E2 E3) λx.x λz.x z x λx.(x λz.(x z x)) 2 Application is left-associative x y z (x y) z 3 Parantheses are needed for (λx.e1 E2) E3 where E3 is an argument to the function λx.e1 E2, not part of the body of the function 4 Multiple lambdas may be suppressed λ f g. x λ f. λ g. x

Syntax 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 λx.λy.e A function that takes x and returns another function that takes y and returns e (λx.λy.e) a b (λy.e[a/x]) b e[a/x][b/y] This is called Currying Can represent any number of arguments

Syntax Free and Bound Variables In function λx.t, the variable x is bound; t is the body of the function A variable which is not bound is free. Examples: (λx. x y) (λx. x)(λy. y x ) (λz. (λx. (y x))) Observations: x is bound, y is free x is bound in the first expression, x is independen x is bound, y is free Name of bound var does not matter: λx.(3+x) = λy.(3+y) Name of free var does matter: λx.(x+y) λx.(x+z) Same identifier can occur free and bound in the same expression: (λx.xy)(λy.y)

Conversions α-conversion Conversion = rule for transforming a λ-expression into a another one with the same meaning. α-conversion: renaming bound variables (λx.t) α (λy.t[y/x]) Example (λx.ax) α (λy.ay) (λx.(x(λy.(yx)))) α (λz.(z(λy.(yz))))[z/x] (λx.x(λx.ax))b α (λy.y(λx.ax))b[y/x]

Conversions β-conversion β-conversion: applying abstractions to arguments ((λx.t)u) β (t[u/x]) Example ((λx.((fx)x)(ga) β ((f (ga))(ga)) ((λz.(za)(λx.x) β (λx.x)a β a ((λx.λy.y)a)b) β ((λy.y))b (λy.y)b β b β-conversion represents the computational engine of the lambda calculus.

Conversions η-conversion η-conversion: eliminates redundant lambda-expressions (λx.fx)y η fy Every time when x is not free in f, we can write: (λ x. f x ) = f Example (λ b. y b) y (λ z. (λ x. x y) z) λ x. x y

Conversions Applying Conversions Conversions 1 (λx.t) α (λy.t[y/x]) 2 ((λx.t)u) β (t[u/x]) 3 (λx.fx)y η fy (λ x y. x y) (λ x. x y) (λ a b. a b) NB: left assoc α (λ x z. x z) (λ x. x y) (λ a b. a b) α-conversion [z/x] β (λ z. (λ x. x y) z) (λ a b. a b) β-reduction [(λ x. x y)/x] η (λ x. x y) (λ a b. a b) η-reduction (λ z. f z )= f β (λ a b. a b) y β-reduction [(λ a b. a b)/x] β (λ b. y b) β-reduction [(y/a] η y η-reduction(λ z. f z)= f

Conversions Nested expressions Syntax simplification: (λx 1. (λx 2....(λx n. t)...)) (λx 1 x 2...x n. t) (λx y.y x) y b (λx y.y x) y b (λx. (λy. y x)) y b α (λx. (λz. z x)) y b β (λz. z y) b β b y (λx. (λy. y x)) y b β (λy. y y) b β b b Don t accidentally bind a free variable!

Normal Form Normal Form A λ-expression is said to be in the normal form if no further reduction steps could be applied to it. Example (λ x. a x)((λ y. b y) c) α (λ z. a z)((λ y. b y) c) β a ((λ y. b y) c) a (b c) Not all expressions have a normal form (λ x. x x) (λ x. x x) β (x x)[(λ x. x x)/x] (λ x. x x) (λ x. x x)... the program never ends.

Normal Form Church-Rosser Thesis Thesis Two sequences of reductions applied to a λ-expressions will always result in equivalent normal forms. Thus the normal form does not depend on the order the reductions are performed. (λ x. a x)((λ y. b y) c) α (λ z. a z)((λ y. b y) c) β a ((λ y. b y) c) a (b c) (λ x. a x)((λ y. b y) c) β (λx. a x)(b c) β a (b c)

Normal Form Call by name and call by value in λ-calculus Call by name (λ y. a) ((λ x. x x) (λ x. x x)) β a argument is not evaluated and it is substitued in the body of the function. Call by value (λ y. a) ((λ x. x x) (λ x. x x)) β (λ y. a) ((λ x. x x) (λ x. x x)) This type of substitution always finds the normal form, provided that such a form actually exists. (\x -> \y -> x) 1 (5/0) = 1

Outline 1 λ-calculus 2 Operational Semantics Syntax Conversions Normal Form 3 Lambda Calculus as a Functional Language Aritmethic Operations Boolean Expressions Tuples 4 Food for Tought

Aritmethic Operations λ-calculus Expressivity In the lambda calculus we can only define new functions. How can we program with only functions? Idea Encode the behavior of values and not their structure. Introducing variables let x = e1 in e2 (λx.e1) e2

Aritmethic Operations Aritmethic Can we represent numbers in λ-calculus? Numbers will be defined as... functions: 0 := λsz.z 1 := λsz.s(z) 2 := λsz.s(s(z))... Definition A natural number is a function that given an operation s and a starting value z, applies s a number of times to z.

Aritmethic Operations Successor Function Our first interesting function... S λwyx.y(wyx) S 0 λw y x. y(wyx) (λsz. z) β λy x. y ((λsz. z) y x) β λy x. y ((λz. z) x) β λy x. y (x) α λs x. s(x) 1

Aritmethic Operations Summing numbers Our second interesting function... PLUS λ mnfx. nf (mfx) PLUS 1 2 (λ mnfx.nf (mfx))1 2 ββ λ fx.2f (1fx) λ fx.2f ((λ fx.f (x)) fx) η λ fx.2f (f (x)) λ fx.(λ fx.f (f (x)))f (f (x)) λ fx.f (f (f (x))) α 3

Boolean Expressions Boolean Expressions What is true? True is something that used as the first parameter of if makes the result of if to be its second parameter if T M N M if λ p. λthen.λelse. p then else if λp. (λ c. (λ a. pca))) λpca. pca A boolean is a function that given two choices selects one of them T λx. (λy. x) λx y. x F λx. (λy. y) λx y. y if T M N λp c a. p c a (λx y. x) M N β (λc a. (λx y. x) c a) M N β (λa. (λx y. x) a) M N β (λx y. x) M N β (λy. M) N β M

Boolean Expressions Logical Operators And, Or, Not and λx (λy if x y F) λxy. x y F or λx (λy if x T y) λxy. x T y not λx. x F T and T F (λx y. x y F) T F β (λy. T y F) F β T F F β (λx y. x) F F β (λy. F) F β F 1 (λx.t) α (λy.t[y/x]) 2 ((λx.t)u) β (t[u/x]) 3 (λx.fx)y η fy

Boolean Expressions Logical Operators And, Or, Not and λx (λy if x y F)... λxy. x y F or λx (λy if x T y) λxy. x T y not λx. x F T 1 (λx.t) α (λy.t[y/x]) 2 ((λx.t)u) β (t[u/x]) 3 (λx.fx)y η fy and T F (λx y. x y F) T F β (λy. T y F) F β T F F β (λx y. x) F F β (λy. F) F β F not T (λ b. b F T ) (λ x y. x ) β (λ x y. x) F T β F

Boolean Expressions Testing if a number is zero Another interesting function... Z λx.xf F Z 0 λx.xf F 0 0F F (λs z. z) F F F T

Boolean Expressions Testing if a number is zero Z 0 λx.xf F 0 0F F (λs z. z) F F F T Z 1 λx.xf F 1 1F F (λsz.s(z))f F F( )F (λxy.y)( )F F Z λx.xf F

Tuples Defining pairs PAIR λxyf.fxy FIRST λ p. p T SECOND λ p. p F First (Pair (a b)) (λp.p T) (PAIR a b) (PAIR a b) T (λxyf.fxy a b) T (λf. f a b) T T a b a

Tuples Recursion A self-regenerating function... Y λy.(λx.y(xx))(λx.y(xx)) YR λy.(λx.y(xx))(λx.y(xx))r (λx.r(xx))(λx.r(xx)) R((λx.R(xx))(λx.R(xx))) R(YR) When Y is applied on an arbitrary function R, R will call itself on (YR). Subsequent equivalent forms appear when R gets expanded according to its definition. It is R s responsibility to break the recursive loop at some moment by providing a condition for stopping.

Tuples Recursion - Example fact = λ f. λ n.if n = 0 then 1 else n * (f (n-1)) (Y fact) 1 = (fact (Y fact)) 1 if 1 = 0 then 1 else 1 * ((Y fact) 0) 1 * ((Y fact) 0) 1 * (fact (Y fact) 0) 1 * (if 0 = 0 then 1 else 0 * ((Y fact) (-1)) 1 * 1 1

Outline 1 λ-calculus 2 Operational Semantics Syntax Conversions Normal Form 3 Lambda Calculus as a Functional Language Aritmethic Operations Boolean Expressions Tuples 4 Food for Tought

Readings 1 Mandatory reading: Chapter 8: pages 107-119 2 Optional reading: A Tutorial Introduction to the Lambda Calculus, Raul Rojas, FU Berlin, Germany, WS-97/98