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

CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

Dec 13, 2015

Download

Documents

Edith Moore
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

CSE-321 Programming Languages

-Calculus (II)

POSTECH

March 27, 2006

박성우

Page 2: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

2

Values and Reductions

redex = reducible expression

: -reduction

Page 3: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

3

Call-by-name Call-by-value

Page 4: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

4

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

Page 5: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

5

[e' / x] e• Informally

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

• Examples

Page 6: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

6

Easy Cases First

Page 7: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

7

Two Remaining Cases

Page 8: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

8

• First (stupid) attempt

• Second attempt

• But wait:

Page 9: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

9

• "Names of bound variables do not matter."

• Hence

• Because, for a fresh variable y,

Bound Variables

Page 10: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

10

One Remaining Case

Page 11: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

11

A Naive Attempt

• An anomaly:

something for y

Page 12: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

12

Free Variables• Those variables that are bound nowhere

FV(e) = set of free variables in e

Page 13: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

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:

Page 14: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

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

Page 15: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

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!

Page 16: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

16

Substitution Completed

Page 17: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

17

• We have to rename bound variables as necessary.

Capture-Avoiding Substitution in Action

Page 18: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

18

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

matter.• Examples

Page 19: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

19

Formalization of -Conversion• See the course notes!

– It's more interesting than you might think.

Page 20: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

20

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

– A LOT OF FUN!

Page 21: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

21

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

you!"

• Syntactic sugar

Booleans

Page 22: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

22

Examples• Under the call-by-name strategy,

Page 23: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

23

Logical Operators

Page 24: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

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 "

Page 25: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

25

Church Numerals

Page 26: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

26

Addition• Key observation:

Page 27: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

27

Multiplication• Key observation:

• Alternatively

Page 28: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

28

Others• Pairs• Natural numbers

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

• Lists

See Course Notes and Pierce Chapter 5 for more examples.

Page 29: CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.

29

Next Lecture• Recursion in the -calculus

– Read Course Notes!