cse-321 programming languages -calculus (ii) postech march 27, 2006 박성우

29
CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박박박

Upload: edith-moore

Post on 13-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

CSE-321 Programming Languages

-Calculus (II)

POSTECH

March 27, 2006

박성우

2

Values and Reductions

redex = reducible expression

: -reduction

3

Call-by-name Call-by-value

4

Outline• Abstract syntax of the -calculus V• Operational semantics of the -calculus V• Substitutions• Programming in the -calculus

5

[e' / x] e• Informally

"substitute e' for every occurrence of x in e."

• Examples

6

Easy Cases First

7

Two Remaining Cases

8

• First (stupid) attempt

• Second attempt

• But wait:

9

• "Names of bound variables do not matter."

• Hence

• Because, for a fresh variable y,

Bound Variables

10

One Remaining Case

11

A Naive Attempt

• An anomaly:

something for y

12

Free Variables• Those variables that are bound nowhere

FV(e) = set of free variables in e

13

Free Variables vs. Bound Variables

• An outside observer does care about free variables.

• An outside observer does not care about bound variables.

• From the point of view of an outside observer:

14

Free Variables Remain Free• From the point of view of an outside observer,

a free variable remains free until it is explicitly

replaced.

outside observer

? variable capture

15

Capture-Avoiding Substitution

• What happens if– the free variable y is captured and becomes a

bound variable. – To an outside observer, it suddenly disappears!

16

Substitution Completed

17

• We have to rename bound variables as necessary.

Capture-Avoiding Substitution in Action

18

Conversion• Renaming bound variables when necessary• Okay because the names of bound variables do not

matter.• Examples

19

Formalization of -Conversion• See the course notes!

– It's more interesting than you might think.

20

Outline• Abstract syntax of the -calculus V• Operational semantics of the -calculus V• Substitutions V• Programming in the -calculus

– A LOT OF FUN!

21

• A boolean value– "Give me two options and I will choose one for

you!"

• Syntactic sugar

Booleans

22

Examples• Under the call-by-name strategy,

23

Logical Operators

24

Natural Numbers• A natural number n

– has the capability to repeat a given process n

times.– "Give me a function f and I will return

f n = f o f ... f o f "

25

Church Numerals

26

Addition• Key observation:

27

Multiplication• Key observation:

• Alternatively

28

Others• Pairs• Natural numbers

– predecessor: DIFFICULT– subtraction– exponentiation– ...

• Lists

See Course Notes and Pierce Chapter 5 for more examples.

29

Next Lecture• Recursion in the -calculus

– Read Course Notes!