cse-321 programming languages extensions to the simply typed -calculus

33
CSE-321 Programming Languages Extensions to the Simply Typed - Calculus POSTECH April 10, 2006 박박박

Upload: kirk-parrish

Post on 03-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

CSE-321 Programming Languages Extensions to the Simply Typed  -Calculus. 박성우. POSTECH April 10, 2006. Abstract Syntax. Operational Semantics. Type System. Outline. Product types pairs generalized product types unit type Sum types Fixed point construct. Pairs in SML. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

CSE-321 Programming Languages

Extensions to the Simply Typed -Calculus

POSTECH

April 10, 2006

박성우

Page 2: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

2

Abstract Syntax

Page 3: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

3

Operational Semantics

Page 4: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

4

Type System

Page 5: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

5

Outline• Product types

– pairs– generalized product types– unit type

• Sum types• Fixed point construct

Page 6: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

6

Pairs in SML- (1, true);

val it = (1,true) : int * bool

- #1 (1, true) ;

val it = 1 : int

- #2 (1, true) ;

val it = true : bool

Page 7: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

7

Abstract Syntax and Typing Rules

Page 8: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

8

Eager Reduction Rules

Page 9: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

9

Lazy Reduction Rules

Page 10: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

10

Outline• Product types

– pairs V– generalized product types– unit type

• Sum types• Fixed point construct

Page 11: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

11

Tuples in SML- (1, true, "hello", fn x : int => x);

val it = (1,true,"hello",fn) : int * bool * string * (int -> int)

- #1 (1, true, "hello", fn x : int => x);

val it = 1 : int

- #2 (1, true, "hello", fn x : int => x);

val it = true : bool

- #3 (1, true, "hello", fn x : int => x);

val it = "hello" : string

Page 12: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

12

Generalized Product Types

Page 13: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

14

Generalized Product Types

• We have n components.– n = 2

• pair types– n > 2

• tuple types– n = 1

• seems irrelevant– n = 0?

Page 14: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

15

n = 0

Page 15: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

16

Outline• Product types

– pairs V– generalized product types V– unit type

• Sum types• Fixed point construct

Page 16: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

17

Unit in SML- ();

val it = () : unit

Page 17: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

18

• We need n elements to build a tuple:

• I want to extract the i-th element where 1 · i :

n = 0: Nullary Product Type

No elimination rule for the unit type!

Page 18: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

19

Type unit: No Elim; Only Intro

Page 19: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

20

Outline• Product types V

– pairs– generalized product types– unit type

• Sum types• Fixed point construct

Page 20: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

21

Datatypes in SML- datatype t = Inl of int | Inr of bool;

datatype t = Inl of int | Inr of bool

- val x = Inl 1;

val x = Inl 1 : t

- val y = Inr true;

val y = Inr true : t

- case (Inl 1) of Inl x => x | Inr _ => 0;

val it = 1 : int

- case (Inr true) of Inl x => x | Inr _ => 0;

val it = 0 : int

Page 21: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

22

Datatypes in SML 2007- datatype 'int + bool' = Inl of int | Inr of bool;

datatype 'int + bool' = Inl of int | Inr of bool

- val x = Inl 1;

val x = Inl 1 : 'int + bool'

- val y = Inr true;

val y = Inr true : 'int + bool'

- case (Inl 1) of Inl x => x | Inr _ => 0;

val it = 1 : int

- case (Inr true) of Inl x => x | Inr _ => 0;

val it = 0 : int

Page 22: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

23

Sum Types: Two Alternatives

Page 23: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

24

Reduction Rules

Page 24: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

25

bool = unit + unit

Page 25: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

26

n = 0: Nullary Sum Type

Page 26: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

27

What about Elimination Rule?

Page 27: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

28

Type void: No Intro; Only Elim

Page 28: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

29

Why do we need the Elim rule then?

• void type is not found in SML.

Page 29: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

30

Outline• Product types V

– pairs– generalized product types– unit type

• Sum types V • Fixed point construct

Page 30: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

31

Recursion?• Why not use the fixed point combinator?

• It does not typecheck in the simply typed -calculus.– f has type A and also A ! A.

Page 31: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

32

Fixed Point Construct

Page 32: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

33

Recursive Functions in SML

Page 33: CSE-321 Programming Languages Extensions to  the Simply Typed   -Calculus

34

Mutually Recursive Functions in SML