Docsity

Introduction to Lambda Calculus: Syntax, Semantics, and Computability, Papers of Programming Languages

Toggle sidebar
bg1
Com S 541
Introduction to the Lambda Calculus
Overview:
!What is Computability? – Church’s Thesis
!Primitive Recursive Functions
!The Lambda Calculus
!The Church-Rosser Property
!Modeling basic programming constructs
References:
!H.P. Barendregt, “The Lambda Calculus – Its Syntax and Semantics”, North-
Holland, 1984
!David A. Schmidt, “The Structure of Typed Programming Languages”, MIT
Press, 1994
!Carl A. Gunter, “Semantics of Programming Languages”, MIT Press, 1992
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Introduction to Lambda Calculus: Syntax, Semantics, and Computability and more Papers Programming Languages in PDF only on Docsity!

Com S 541

Introduction to the Lambda Calculus

Overview:

!^

What is Computability? – Church’s Thesis !^

Primitive Recursive Functions !^

The Lambda Calculus !^

The Church-Rosser Property !^

Modeling basic programming constructs

References:

!^

H.P. Barendregt, “The Lambda Calculus – Its Syntax and Semantics”, North-Holland, 1984 !^

David A. Schmidt, “The Structure of Typed Programming Languages”, MITPress, 1994 !^

Carl A. Gunter, “Semantics of Programming Languages”, MIT Press, 1992

Com S 541

!^

Computation is usually modeled as a mapping from inputs to outputs,carried out by a formal “machine”, or program, which processes its input ina sequence of steps. !^

An “effectively computable” function is one that can be computed in afinite amount of time using finite resources.

What Is Computable?

Problem input

yes no output

“effectivelycomputable”

function

program/machine

Com S 541

Uncomputability

!^

A problem that cannot be solved by any Turing machine in finite time (orany equivalent formalism) is called uncomputable.

Assuming Church’s thesis is true, an uncomputableproblem cannot be solved by any real computer.

The Halting Problem

Given an arbitrary Turing machine and its input tape, will the machineeventually halt?The Halting Problem is provably uncomputable – which means that itcannot be solved in practice.

Com S 541

!^

Primitive recursive functions are a total recursive functions (i.e. Turing computable)and they form the smallest class of functions that:^!

Includes the base functions

!^

  • the constant function, where

c

is introduced as

function (in most cases

c=

)

!^

  • the successor function that implements counting

!^

  • the projection function, where a variable is

represented as a function with arity

n

!^

Is closed under the production rules

!^

Composition: Let

h

be a function of arity

k

and

g

,
, 1

gk

functions with arity

n

, then

the function

denotes a function with arity

n

.

!^

Primitive recursion: Let

h

be a function with arity

n+

and

g

be a function with arity

n-

1 , then the following equation set denotes a function

f

with arity

n

:

Primitive Recursive Functions

n m

xm

xn

xm

x nm P^

≀ ≀

=^

1

,

)

,..

.,

,..

., 1 (

cons with

t

, )

,..

., 1 (

=

=^

c

c

xn

x nc F

with

1

) (^

=^

x x f

)]

,..

., 1 (

),.

..,

,..

., 1 ( 1 [ )

,..

., 1 (^

xn

x gk

xn

x g h

xn

x f^

=

)) , 1

,..

., 1 ( , , 1

,..

., 1 ( ) 1

, 1

,..

., 1 (

) 1

,..

., 1 ( ) 0 , 1

,..

. 1 (

y

xn

x f y

xn

x h

y

xn

x f

xn

x g

xn x f

−

−

=

−

−

=

−

Com S 541

Example I – The Haskell Code

-- base functions: prim_null, prim_succ, and prim_projprim_null :: Int -> Intprim_null x = 0prim_succ :: Int -> Intprim_succ x = x + 1-- The operator !! - list index subscript - is defined in Prelude.hsprim_proj :: ([Int], Int) -> Intprim_proj (tuple, index) = tuple!!(index - 1)-- add( x, y ) = x + yprim_add :: (Int, Int) -> Intprim_add (x, 0) = prim_proj([x], 1)prim_add (x, y+1) = h(x, y, prim_add(x, y))

where h(x, y, z) = (prim_succ. prim_proj)( [x, y, z], 3 )

Com S 541

Examples II – e(x, y) = x

y

exp(x, y) = x

y

!^

Recursive equations:exp(x, 0) = 1exp(x, y+1) = x * exp(x, y) !^

Primitive recursive functions:

(x) F

exp(x,

1 1

z)) y, (x, P z), y, (x,

mul(P )z y,

h(x,

with

y)

exp(x, y,

h(x,

y

exp(x,

(^33)

(^31)

Com S 541

Example III – pred(x) = x - 1

pred(x) = x - 1

!^

Recursive equations:pred(0) = 0pred(x+1) = x !^

Primitive recursive functions:

!

Note: for all x >= 1 it holds pred(succ(x))=succ(pred(x))=x

pred(0)

pred(x)) (x, P

pred(x

(^21)

Com S 541

The Lambda Calculus

!^

Lambda calculus is a language with clear operational and denotationalsemantics capable of expressing algorithms. Also it forms a compactlanguage to denote mathematical proofs. !^

Logic provides a formal language in which mathematical statements can beformulated and provides deductive power to derive these. Type theory is aformal system, based on lambda calculus and logic, in which statements,computable functions and proofs all can be naturally represented. !^

The lambda calculus is a good medium to represent mathematics on acomputer with the aim to exchange and store reliable mathematicalknowledge.

Com S 541

Beta Reduction

!^

Beta reduction is the computational engine of the lambda calculus:Define:

I

λ

x.x

Now consider:

II= (

λ

x.x)(

λ

x.x)

[(

λ

x.x)

/x]

x

ÎČ

-reduction

(λ

x.x)

substitution

I

!^

We can implement most lambda expressions directly in Haskell:i = \x -> x?^

i 5 5?^

i i 5 5

Com S 541

Free and Bound Variables

!^

The variable

x

is bound by the enclosing

λ

in the expression

λ

x.e

. A variable

that is not bound, is free:

fv(x)

= { x}

fv(

λ

x.e)

= f

v(e){ x}

fv(e

1

e^2

= f

v(e

f

v(e

2

!^

An expression with no free variables is closed (otherwise it is open). Forexample,

y

is bound and

x

is free in the (open) expression

λ

y.xy

Syntactic substitution will not always work:

(λ

x.

λ

y.xy)y

[y/

x]

(λ

y.xy)

ÎČ

  • reduction

(λ

y.yy)

incorrect substitution!

Since

y

is already bound in

λ

y .x y)

, we cannot directly substitute

y

for

x

Com S 541

Alpha Conversion

!

Alpha conversions allows one to rename bound variables.

!

A bound name

x

in the lambda abstraction

x.e)

may be

substituted by any other name

y

, as long as there are no free

occurrences of

y

in

e

Consider:

x.

y.xy)y

x.

z.xz

)y

-conversion

[

y/

x](

z.xz

-reduction

z.yz

= y

-reduction

Com S 541

Eta Reduction

!

-reductions allows one to remove “redundant lambdas”.

!

Suppose that

f

is a closed expression (i.e.,

x

does not occur

free in

f

). Then:

x.fx)y

([

y/

x]

f)(

[y/

x]

x)= fy

-reduction

More generally, this will hold whenever

x

does not occur free

in

f

. In such cases, we can always rewrite

x.fx)

as

f

Com S 541

Evaluation Order

!^

Most programming languages are

strict, that is, all expressions passed to a

function call are evaluated before control is passed to the function. !^

Most modern functional languages, on the other hand, use

lazy evaluation,

that is, expressions are only evaluated when they are needed.Consider:

square n = n * n

Applicative-order reduction:

square (2 + 5)

square 7

7 * 7

49

Normal-order reduction:

square (2 + 5)

(2 + 5) * (2 + 5)

7 * (2 + 5)

7 * 7

49

Com S 541

Applicative Order Reduction

!^

Motivation:^!

Modeling call-by-value in programming languages !^

In function calls, evaluate arguments then invoke function

!^

In the lambda-calculus, this means:^!

In (e

1

e^2

), reduce e

2

to normal form using applicative order reduction

!^

Then reduce e

1

to normal form using applicative order reduction

!^

If e

1

is a lambda abstraction, do beta reduction, and reduce the result to normal

form using applicative order reduction

!^

Syntax makes it easy:^!

Write expression using fully parenthesized notation !^

Always perform rightmost beta reduction by !^

Repeatedly scanning for rightmost (left parenthesis) occurrence of ((

λ x. e

) e 1

) 2

!^

Note, this includes reduction of primitives, e.g. ((add 1) 2)