cse-321 programming languages -calculus (ii)

35
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박박박

Upload: rod

Post on 05-Jan-2016

32 views

Category:

Documents


2 download

DESCRIPTION

CSE-321 Programming Languages  -Calculus (II). 박성우. POSTECH March 26, 2007. :  -reduction. redex = reducible expression. Values and Reductions. Call-by-name Call-by-value. Outline. Abstract syntax of the  -calculus V Operational semantics of the l -calculus V Substitutions - PowerPoint PPT Presentation

TRANSCRIPT

CSE-321 Programming Languages

-Calculus (II)

POSTECH

March 26, 2007

박성우

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

– for a fresh variable y,

Bound Variables

10

One Remaining Case

11

A Naive Attempt

• An anomaly:

something for y

12

Free Variables• Variables that are bound nowhere

FV(e) = set of free variables in e

13

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

14

Capture-Avoiding Substitution

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

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

15

Substitution Completed

16

• We have to rename bound variables as necessary.

Capture-Avoiding Substitution in Action

17

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

matter.• Examples

18

Formalization of -Conversion• See the course notes!

– It's more interesting than you might think.

19

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

20

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

you!"

• Syntactic sugar

Booleans

21

Examples• Under the call-by-name strategy,

22

Logical Operators

23

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 "

24

Church Numerals

25

Addition• Key observation:

26

Multiplication• Key observation:

• Alternatively

27

Recursive Functions in -Calculus• Plan of attack

1. Assume a recursive function construct

fun f x. e

2. Rewrite it as an expression in the -calculusi.e., show that it is syntactic sugar.

28

• Plan of attack

1. Assume a recursive function construct

2. Rewrite it as an expression in the -calculusi.e., show that it is syntactic sugar.

Example: Factorial

29

fac to FAC

30

FAC produces a new function from f

31

Now we only need to find a fixed point of:

32

Fixed Point• Fixed point of function f

V such that V = f (V)

• Ex.

f (x) = 2 - x

fixed point of f = 1

1 = f (1)

33

Magic Revealed• Fixed point combinator / Y combinator

(call-by-value)

• fix F returns a fixed point of F.

34

Now we only need to find a fixed point of:

Now we only need to compute:

35

Answer