# lambda calculus1 the untyped lambda-calculus hossein hojjat university of tehran formal methods...

Embed Size (px)

TRANSCRIPT

Lambda Calculus 1

The Untyped Lambda-Calculus

Hossein Hojjat

University of TehranFormal Methods Laboratory

Hossein Hojjat Lambda Calculus 2

“Whatever the next 700 languages turn out to be, they

will surely be variants of lambda calculus.”

Landin, Peter J. The next 700 programming languages.Communications of the ACM, 9(3):157–166, March 1966.

Hossein Hojjat Lambda Calculus 3

Part 1 : Introduction

Hossein Hojjat Lambda Calculus 4

History

David Hilbert (a German mathematician) delivered a famous speech to the Second International Congress of Mathematicians in Paris (1900)– The Problems of Mathematics

Perhaps the most influential speech ever given to mathematicians, given by a mathematician, or given about mathematics

Hossein Hojjat Lambda Calculus 5

History

Hilbert outlined 23 major mathematical problems to be studied in the coming century

David Hilbert's tenth problem was to devise an algorithm that tests whether a polynomial has an integral root– Entscheidungsproblem (English: decision

problem)

Find a process which determines the result in a finite number of operations

Hossein Hojjat Lambda Calculus 6

History

Church and Turing independently proved that the Entscheidungsproblem is unsolvable

Church proved that there is no algorithm (defined via recursive functions) which decides for two given lambda calculus expressions whether they are equivalent or not

Turing reduced the halting problem for Turing machines to the Entscheidungsproblem

Hossein Hojjat Lambda Calculus 7

λ-calculus strength

Although both models appear very different from one another, Turing later showed that were equivalent– They each pick out the same set of mathematical

functions Church-Turing thesis: any problem that has

an algorithmic solution can be solved on a Turing machine– It implies a universality among different models of

computation.

Hossein Hojjat Lambda Calculus 8

Suitability

Lambda-calculus is more suitable as an abstract model of programming language rather than as a practical programming language– It is used to study programming languages

semantics from a mathematical perspective called Denotational Semantics

Hossein Hojjat Lambda Calculus 9

Overview

In the mid 1960s, Peter Landin observed that a complex programming language can be understood by formulating it as– A tiny core calculus capturing the language's

essential mechanisms– A collection of convenient derived forms whose

behavior is understood by translating them into the core

Hossein Hojjat Lambda Calculus 10

Overview

The core language used by Landin was the lambda-calculus

Following Landin's insight, the lambda-calculus has seen widespread use in the specification of programming language features– in language design and implementation– in the study of type systems

Hossein Hojjat Lambda Calculus 11

Overview

The lambda-calculus is just one of a large number of core calculi that have been used for similar purposes– The pi-calculus has become a popular core

language for defining the semantics of message-based concurrent languages

– The object calculus distills the core features of object-oriented languages

Hossein Hojjat Lambda Calculus 12

Overview

Most of the concepts and techniques of the lambda-calculus can be transferred quite directly to other calculi

Hossein Hojjat Lambda Calculus 13

Enriching the calculus

The lambda-calculus can be enriched in a variety of ways

It is often convenient to add special concrete syntax for features like numbers, tuples, records, etc.

The behavior of these features can already be simulated in the core language

Hossein Hojjat Lambda Calculus 14

Enriching the calculus

We can add more complex features such as mutable reference cells or nonlocal exception handling

These features can be modeled in the core language only by using rather heavy translations

Such extensions lead eventually to languages such as ML, Haskell or Scheme

Hossein Hojjat Lambda Calculus 15

Basics

Procedural (or functional) abstraction is a key feature of essentially all programming languages

Instead of writing the same calculation over and over– Write a procedure or function that performs the

calculation generically in terms of one or more named parameters

– Instantiate this function as needed, providing values for the parameters in each case

Hossein Hojjat Lambda Calculus 16

Example

Consider(5*4*3*2*1) + (7*6*5*4*3*2*1) - (3*2*1)

Normally rewrite it asfactorial(5) + factorial(7) - factorial(3),

where

factorial(n) = if n=0 then 1 else n * factorial(n-1)

Instantiating the function factorial (for n≥0) with the argument n yields the factorial of n as result

Hossein Hojjat Lambda Calculus 17

The λ Notation

We write "λn. ..." as a shorthand for

“The function that, for each n, yields...,” factorial =

λn. if n=0 then 1 else n * factorial(n-1) The lambda-calculus (or λ-calculus)

embodies this kind of function definition and application in the purest possible form

Everything is a function!

Hossein Hojjat Lambda Calculus 18

Why lambda?

Principia Mathematica : a three-volume work on the foundations of mathematics– written by Alfred North Whitehead and Bertrand

Russell in 1910-1913

In this book the notation for the function f with is

Church originally intended to use the notation

Hossein Hojjat Lambda Calculus 19

Why lambda?

The typesetter could not position the hat on the top of the x, and placed it in front of it

Another typesetter, changed it to lambda!

Hossein Hojjat Lambda Calculus 20

Syntax

A term is defined as follows:

e ::= x (identifier)

| e0 e1 (application)

| λx. e (abstraction) In an abstraction:

– x is the argument– e is the body of the function

Hossein Hojjat Lambda Calculus 21

λ Expression

The only keywords used in the language are λ and dot

An expression can be surrounded with parenthesis for clarity

Hossein Hojjat Lambda Calculus 22

Example

The identity function :

λx . x The name after the λ is the identifier of the

argument of this function The expression after the point is the “body” of

the definition

Hossein Hojjat Lambda Calculus 23

Application

Functions can be applied to expressions

(λx.x) y This is the identity function applied to y Parenthesis are used for clarity in order to

avoid ambiguity– Function application is left associative

The result is : y

Hossein Hojjat Lambda Calculus 24

Scope

In λ calculus all names are local to definitions λx.t : The scope of x is the term t An occurrence of the variable x is said to be

bound when it occurs in the body t of an abstraction λx.t

(λx. y x)(λy. x y)

bound

free

boundfree

Hossein Hojjat Lambda Calculus 25

Free variables

We can define formally the free variables of an expression E recursively as follows:

Free(x) = {x}

Free(E1 E2) = Free(E1) Free(E2)

Free(λx. E) = Free(E) – {x} Example:

Free(λx. x (λy.x y z)) = {z}

Hossein Hojjat Lambda Calculus 26

Computation

In its pure form, the lambda-calculus has no built-in constants or primitive operators– no numbers, arithmetic operations, conditionals,

records, loops, sequencing, I/O, etc. Computation is achieved by application of

functions to arguments (which themselves are functions)

Each step in the computation consists of abstraction the right-hand component for the bound variable in the abstraction's body

Hossein Hojjat Lambda Calculus 27

Computation

Graphically, we write:

– means: “the term obtained by replacing all free occurrences of x in M by N”

Hossein Hojjat Lambda Calculus 28

Example

What does λs.(s s) mean?– Self application function

(λs.(s s) λx.x)= (λx.x λx.x)

= λx.x

What does this lambda expression mean?

(λs. (s s) λs. (s s))

Hossein Hojjat Lambda Calculus 29

Example

λfunc. λarg. (func arg) This is the function application function It returns a second function which then

applies the first function's argument to the second function’s argument

Hossein Hojjat Lambda Calculus 30

Example

λfirst. λsecond. first– Selecting the first of two arguments– (λx. λy. x) a b = (λy. a) b = a

λfirst. λsecond. second– Selecting the second of two arguments– (λx. λy. y) a b = (λy. y) b = b

Hossein Hojjat Lambda Calculus 31

Naming Conventions

A term of the form (λx. M) N is called a redex ("reducible expression")

The operation of rewriting a redex according to the above rule is called beta-reduction

Hossein Hojjat Lambda Calculus 32

Evaluation Strategies

Several different evaluation strategies for the lambda-calculus have been studied over the years by programming language designers and theorists

Each strategy defines which redex or redexes in a term can fire on the next step of evaluation

Hossein Hojjat Lambda Calculus 33

Evaluation Strategies

For example, consider:

(λx.x) ((λx.x) (λz. (λx.x) z)) We can write it more readable as:

id (id (λz. id z)) This term contains three redexes:

id (id (λz. id z))

Hossein Hojjat Lambda Calculus 34

Full beta-reduction

Any redex may be reduced at any time At each step we pick some redex, anywhere

inside the term we are evaluating, and reduce it

For example, we can begin with the innermost redex, then do the one in the middle, then the outermostid (id (λz. id z)) → id (id (λz.z)) → id (λz.z) → λz.z →N

Hossein Hojjat Lambda Calculus 35

Normal Order

The leftmost, outermost redex is always reduced first

id (id (λz. id z)) →

id (z. id z) →

λz. id z →

λz.z →N

Hossein Hojjat Lambda Calculus 36

Why Not Normal Order?

In most (all?) programming languages, functions are considered values (fully evaluated)

Thus, no reduction is done inside of functions (under the lambda)

No popular programming language uses normal order

Hossein Hojjat Lambda Calculus 37

Call by Name-Call by Value

Consider the application e0 e1

Call by value: evaluate the argument e1 to a value before β reduction

Call by name: reduce the application, without evaluating e1

In both cases: A value is an abstraction: λx. e

Hossein Hojjat Lambda Calculus 38

Call by Name-Call by Value

Call by name:

id (id (λz. id z)) →

id (λz. id z) →

λz. id z →N Call by value:

id (id (λz. id z)) →

id (λz. id z) →

λz. id z →N

Hossein Hojjat Lambda Calculus 39

Comparison

The call-by-value strategy is strict The arguments to functions are always

evaluated, whether or not they are used by the body of the function

Non-strict (or lazy) strategies evaluate only the arguments that are actually used– call-by-name– call-by-need

Hossein Hojjat Lambda Calculus 40

Call by Value vs. Name

The debate about whether languages should be by value or by name is nearly 20 years old

This debate is confined to the functional programming community

Outside the functional community call by name is rarely considered

Hossein Hojjat Lambda Calculus 41

Languages Hierarchy

Call-by-name lambda calculus– Haskell– Miranda

Call-by-value lambda calculus– Lisp– Scheme– ML

Hossein Hojjat Lambda Calculus 42

Name Clashes

Consider the function application function– def apply = λfunc. λarg. (func arg)

Consider:

((apply arg) salam) =

(( (λfunc. λarg. (func arg)) arg) salam) arg is used both as a

– function bound variable– free variable

Hossein Hojjat Lambda Calculus 43

Name Clashes

If we carry out β reduction:

(( (λfunc. λarg. (func arg)) arg) salam) →

(λarg. (arg arg) salam) →

(salam salam) This was not intended at all! The argument arg has been substituted in the

scope of the bound variable arg, and appears to create a new occurrence of that variable

Hossein Hojjat Lambda Calculus 44

Renaming

We can avoid this problem by using consistent renaming

For example, we can replace the bound variable arg with arg1

(( (λfunc. λarg1. (func arg1)) arg) salam) →

(λarg1. (arg arg1) salam) →

(arg salam)

Hossein Hojjat Lambda Calculus 45

α-Conversion

A name clash arises when– A β-reduction places an expression with a free

variable in the scope of a bound variable with the same name as the free variable

The names of bound variables are unimportant (as far as the meaning of the computation goes)

We will treat λx. x and λy. y as if they are (absolutely and totally) indistinguishable– we can always use one in place of the other

Hossein Hojjat Lambda Calculus 46

Equivalence Relation

An equivalence relation is defined that captures the intuition that two expressions denote the same function

This equivalence relation is defined by the so-called alpha-conversion rule and the beta-reduction rule

There is a third rule, η-conversion, which may be added to these two to form a new equivalence relation

Hossein Hojjat Lambda Calculus 47

η-Conversion

η-Conversion is useful to eliminate redundant lambda abstractions

If the sole purpose of a lambda abstraction is to pass its argument to another function, then the lambda abstraction is redundant

λx. (E x) E, if x Free(E)

Hossein Hojjat Lambda Calculus 48

η-Conversion Usage

η-Conversion is similar to the proxy pattern in OO programming

In “eager evaluation” languages is used (like Scheme)

It creates a wrapper around a lambda expression to prevent immediate evaluation

Hossein Hojjat Lambda Calculus 49

Examples

Valid conversion

λx. add x →η add

λy. add x y →η add x

Invalid conversion

λx. add x x →η add x

Valid conversion

λx. (λy. y) x →η λy. y

What about λx. λy. y x

Hossein Hojjat Lambda Calculus 50

Part 2 :

Programming in the Lambda-Calculus

Hossein Hojjat Lambda Calculus 51

The lambda-calculus is much more powerful than its tiny definition might suggest

In this part, we develop a number of standard examples of programming in the lambda-calculus

How to make– Booleans and conditional functions– Numerals and arithmetic functions

Hossein Hojjat Lambda Calculus 52

Church Booleans

We represent TRUE by select_first function

def true = λt. λf. t We represent FALSE by select_second

function

def false = λt. λf. f As a motivation for this representation,

consider the C conditional expression:

<condition>?<expression>:<expression>

Hossein Hojjat Lambda Calculus 53

Conditional Expression

The definition of TRUE and FALSE suggest the definition for a conditional construct

def cond = λe1. λe2. λc. ((c e1) e2) We will use the conditional definition with true

and false to model some logical operators

Hossein Hojjat Lambda Calculus 54

Not

The Not operator can be written using a conditional expression:

x ? FALSE : TRUE def not = λx. ( ( (cond false) true) x) This expression can be written as follows:

λx. ((x false) true) Exercise: Use the above result to calculate

NOT FALSE

Hossein Hojjat Lambda Calculus 55

And

x y ( x ? y) : FALSE def and = λx. λy. ( ( (cond y) false) x) This expression can be written as follows:

λx. λy. ( (x y) false) Exercise: Use the above result to calculate

AND TRUE FALSE Exercise: Find an expression for OR

Hossein Hojjat Lambda Calculus 56

Church Numerals

There are several possible ways to define the natural numbers in lambda calculus

The most common are the Church numerals First we define fn x which means n application

of f to x

f5 x = f ( f ( f ( f ( f x) ) ) )

Hossein Hojjat Lambda Calculus 57

Church Numerals

The intuition behind Church numerals is to represent a number n as a function that transforms any function f into the function fn

0 := λ f. λ x. x1 := λ f. λ x. f x2 := λ f. λ x. f( f x)…n := λ f. λ x. fn x

Hossein Hojjat Lambda Calculus 58

Successor Function

Given this definition of the Church numerals, we can define a successor function

This implementation uses the argument number n to construct the function fn, then applies f one more time to get fn+1

def succ = λ n. λ f. λ x. f (n f x) Exercise: Try succ 1 == 2

Hossein Hojjat Lambda Calculus 59

Addition

Adding m to n is simply taking the successor of n, m times

add m n = m succ n = λm. λn. λf. λx. m f ( n f x)

Let’s check it:add m n → λf. λx. m f ( n f x) → λf. λx. fm ( fn x)

→ λf. λx. fm+n x m + n

Hossein Hojjat Lambda Calculus 60

Multiplication

Since add takes its arguments one at a time, applying it to just one argument n yields the function that adds n to whatever argument it is given

Passing this function as the first argument to m and 0 as the second argument means:– apply the function that adds n to its argument,

iterated m times, to zero, i.e., add together m copies of n.

def mult = λm. λn. m (add n) 0

Hossein Hojjat Lambda Calculus 61

Exercise

Define a term for raising one number to the power of another.