cse-321 programming languages simply typed -calculus

29
CSE-321 Programming Languages Simply Typed -Calculus POSTECH April 3, 2006 박박박

Upload: kenda

Post on 09-Feb-2016

41 views

Category:

Documents


0 download

DESCRIPTION

CSE-321 Programming Languages Simply Typed  -Calculus. 박성우. POSTECH April 3, 2006.  -Calculus. Abstract syntax Operational semantics (call-by-value). Simulating Base Types. Booleans Natural numbers Fixed point combinator. Motivation. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE-321 Programming Languages Simply Typed   -Calculus

CSE-321 Programming Languages

Simply Typed -Calculus

POSTECHApril 3, 2006

박성우

Page 2: CSE-321 Programming Languages Simply Typed   -Calculus

2

-Calculus• Abstract syntax

• Operational semantics (call-by-value)

Page 3: CSE-321 Programming Languages Simply Typed   -Calculus

3

Simulating Base Types• Booleans

• Natural numbers

• Fixed point combinator

Page 4: CSE-321 Programming Languages Simply Typed   -Calculus

4

• The -calculus is equally expressive as Turing machines.– booleans, integers, lists, recursive functions, ...

• But it is highly inefficient to program in the -calculus.– Why not just use

instead of ?

• So we introduce a type system!

Motivation

Page 5: CSE-321 Programming Languages Simply Typed   -Calculus

5

Simply Typed -Calculus• An extension of the untyped -calculus with types

• Assumes a fixed set of base types– E.g.

• base type • primitive constructs

• A subset of Standard ML

Page 6: CSE-321 Programming Languages Simply Typed   -Calculus

6

Outline• Introduction V• The simply typed -calculus

– Abstract syntax– Operational semantics– Type system

• Type safety

Page 7: CSE-321 Programming Languages Simply Typed   -Calculus

7

Abstract Syntax

Page 8: CSE-321 Programming Languages Simply Typed   -Calculus

8

What if there is no base type?

No interesting expression!

Page 9: CSE-321 Programming Languages Simply Typed   -Calculus

9

Outline• Introduction V• The simply typed -calculus

– Abstract syntax V– Operational semantics– Type system

• Type safety

Page 10: CSE-321 Programming Languages Simply Typed   -Calculus

10

Simply Untyped -Calculus

Page 11: CSE-321 Programming Languages Simply Typed   -Calculus

11

Reduction Rules for Booleans

Page 12: CSE-321 Programming Languages Simply Typed   -Calculus

12

Capture-Avoiding Substitutions• Completely analogous

Page 13: CSE-321 Programming Languages Simply Typed   -Calculus

13

Free Variables• Completely analogous

Page 14: CSE-321 Programming Languages Simply Typed   -Calculus

14

Outline• Introduction V• The simply typed -calculus

– Abstract syntax V– Operational semantics V– Type system

• Type safety

Page 15: CSE-321 Programming Languages Simply Typed   -Calculus

What is the type of ?

Answer:

Page 16: CSE-321 Programming Languages Simply Typed   -Calculus

16

How to find the type of 1. Assume that the type of x is A.

• okay

2. Find the type of x.• A

3. Build a function type• A ! A

Need to make assumptions on types of variables!

Page 17: CSE-321 Programming Languages Simply Typed   -Calculus

17

Type System• Typing context

• Typing judgment

Page 18: CSE-321 Programming Languages Simply Typed   -Calculus

18

Typing Rules --- Top-down

Page 19: CSE-321 Programming Languages Simply Typed   -Calculus

19

Typing Rules --- Bottom-up

Page 20: CSE-321 Programming Languages Simply Typed   -Calculus

20

Typing Rules for Booleans

Page 21: CSE-321 Programming Languages Simply Typed   -Calculus

21

Typing Derivation

Page 22: CSE-321 Programming Languages Simply Typed   -Calculus

22

Typing Derivation

Page 23: CSE-321 Programming Languages Simply Typed   -Calculus

23

Outline• Introduction V• The simply typed -calculus V

– Abstract syntax V– Operational semantics V– Type system V

• Type safety

Page 24: CSE-321 Programming Languages Simply Typed   -Calculus

24

Unsafe Operations in C• Adding two pointers

• Subtracting an integer from a string– which is okay, but likely to be unintended

• Null-pointer dereferencing– Argh... segmentation fault!

• Using an integer as a destination address in a function call

• ...

Page 25: CSE-321 Programming Languages Simply Typed   -Calculus

25

Huffman Coding in SML• Suppose that you spent 10 hours before getting

your program to typecheck.– 0am: You start.– 10am: Your program compiles with no type

errors.

• How many more hours did you spend after that?

Page 26: CSE-321 Programming Languages Simply Typed   -Calculus

26

Type Safety• Slogan

"well-typed expressions never go wrong"

• Two theorems– Type preservation:

"A well-typed expression reduces to another expression of the same type."

– Progress:"A well-typed expression is not stuck: either it is a value or

reduces to another expression."

Page 27: CSE-321 Programming Languages Simply Typed   -Calculus

27

Type Preservation + Progress• A well-typed expression e:

– If it is a value, we are finished.– If it is not,

• It reduces to another e' [Progress]• e' has the same type as e. [Type

preservation]

Page 28: CSE-321 Programming Languages Simply Typed   -Calculus

28

Type Safety• Type preservation

• Progress

Page 29: CSE-321 Programming Languages Simply Typed   -Calculus

29

Proof of Type Safety• Use the rule induction.• In the next lecture!