Docsity

Understanding Bound Variables and Substitution in λ-Calculus - Prof. Jon Rugaber, Study notes of Programming Languages

Toggle sidebar
bg1
c 2005 by Spencer Rugaber
MOTIVATION
• One way to understand basic programming language
design issues is to look at very simple languages
• The λ-calculus has traditionally served this role. It
was invented before computers by the logician
Church and refined by Kleene and Landin
• It contains only one type of value, the function, and its
syntax is minimal
• Moreover, its semantics consist of nothing more than
string rewriting
• Functional programming languages, like SML, provide
the features of the λ-calculus with a more traditional
syntax
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a

Partial preview of the text

Download Understanding Bound Variables and Substitution in λ-Calculus - Prof. Jon Rugaber and more Study notes Programming Languages in PDF only on Docsity!

c 2005 by Spencer Rugaber

MOTIVATION

•^

One way to understand basic programming languagedesign issues is to look at very simple languages

-^

The

λ

-calculus has traditionally served this role. It

was invented before computers by the logicianChurch and refined by Kleene and Landin

-^

It contains only one type of value, the function, and itssyntax is minimal

-^

Moreover, its semantics consist of nothing more thanstring rewriting

-^

Functional programming languages, like SML, providethe features of the

λ

-calculus with a more traditional

syntax

c 2005 by Spencer Rugaber

OVERVIEW

•^

The

λ

-calculus, like any language, is a notation for

denoting computations. The denotations are called

λ

expressions

-^

As such, it provides ways of expressing operations andoperands– The only operations are function definition and

application

  • The only operands are functions -^

Syntactically, the formal operands are denoted byidentifiers (usually a single character variable name).The actual operands are themselves

λ

-expressions

•^

Semantically, the meaning of a

λ

-expression is defined

by three simple rules describing how to textuallysimplify (rewrite) it

c 2005 by Spencer Rugaber

SYNTAX OF THE

- CALCULUS

Syntactic sugar

  • Left-associative application to avoid parentheses– Addition and deletion of parentheses– Alternative brackets

( ), { }, [ ]

  • Definitions: allowing letters and operator symbols to

stand for other

λ

-expressions

  • Currying:

λx, y. x

2

  • y

instead of

λx. (

λy. x

2

  • y

:=:

()

(λ

.)

role:

variables

applications

abstractions

examples:

x^

(f x)

(λ

x. (f x))

c 2005 by Spencer Rugaber

VALUES

•^

Although there are no literals in the

λ

-calculus,

λ

expressions can be given that denote all of theconstructs from traditional programming languages,such as truth values, numbers, and lists

-^

For the time being, we will assume that this has beendone, and abbreviate the results with literals like

T

(for

TRUE

) and

•^

Consequently, many of the examples will evaluate tonumbers or truth values

-^

Formally, however, the only values in the

λ

-calculus

are functions - another reason it is so simple

c 2005 by Spencer Rugaber

EXAMPLES

-expressions may occur as the arguments of

other

-expressions:

f. f(3) + f(4)}[

y. y * (y + 1)]

-expressions may be nested:

a. (

x. a * x * (a + x))[4]}[3]

x. 3 * x * (3 + x))[4]

c 2005 by Spencer Rugaber

INTRODUCTION TO SEMANTICS

The semantics of the

λ

-calculus are defined in terms of

simple textual manipulation operations

  1. The concepts of

bound

and

free

variables must first be

presented

  1. Then, the exact meaning of

substitution

must be given

  1. Three rules,

α

-conversion,

β

-reduction, and

η

reduction, are then presented

  1. We also have to discuss the order in which the rules

are applied

  1. And the meaningfulness of recursive

λ

-abstractions

must be addressed

c 2005 by Spencer Rugaber

BOUND VARIABLES - 2

•^

Variables are bound in a

-calculus expression

by listing them at the beginning of theexpression:

x, y. x

2

+ y

2

+ c}

− "That function of

x

and

y

which is

x

2

  • y

2

  • c

•^

Variables that are not bound are

free

c

in the

above example). They must ultimately be boundin a more encompassing scope

c 2005 by Spencer Rugaber

2. SUBSTITUTION

-^

expressions are defined in terms of textual

substitution

•^

The notation

{N / X}M

should be read as

substitute expression

N

for free occurrences

of variable

X

in expression

M

•^

Substitution would be nothing more thansyntactic replacement if bound variables werenot involved− Bound variables require a careful analysis

depending on which of the three possiblesyntactic forms

M

takes

c 2005 by Spencer Rugaber

2.1. Variables

• M

is a variable: There are two subcases:

M

is

X

:^

{N / X} X

N

•^

That is, just replace

X

with

N

M

is a variable (

V

) different from

X

{N / X} V

V

•^

That is, there is no substitution to be made

c 2005 by Spencer Rugaber

2.2. Applications

• What is the meaning of

{N / X}M

if

M

is

an application?

{N / X}(J K)

c 2005 by Spencer Rugaber

2.3a. Abstractions

• What is the meaning of

{N /

X}M

if

M

is an abstraction?

{N /

X}(

V. B)

c 2005 by Spencer Rugaber

2.3a. Abstractions

• M

is an abstraction

V. B)

: There

are two subcases:^ −

X

is

V

:^

{N / X}(

X. B)

X. B)

•^

That is,

X

is bound in the abstraction, so

the substitution has no effect

c 2005 by Spencer Rugaber

2.3b2a. Abstractions

{N /

X} (

V. B)

• X

occurs free in

B

There are two subcases:^ – V

does not occur free in

N:

{N / X} (

V. B)

V. {N / X} B)

·^

That is, there are no conflicts with parameternames to worry about, so just substitute into thebody of the abstraction

c 2005 by Spencer Rugaber

2.3b2b. Abstractions

{N /

X} (

V. B)

• V

occurs free in

N

{N / X} (

V. B)

Z. {N / X} ({Z / V} B))

-^

That is, there is a naming conflict with the parameterof the abstraction

(V)

. The parameter must be

renamed and the new name

(Z)

substituted into

B

Then the substitution of

N

for

X

can be made