cse-321 programming languages polymorphism postech may 15, 2006 박성우

25
CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박박박

Upload: benedict-mcdaniel

Post on 18-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

CSE-321 Programming Languages

Polymorphism

POSTECH

May 15, 2006

박성우

Page 2: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

2

Polymorphism• The same piece of code can be reused for different

types of objects– parameteric polymorphism– ad hoc polymorphism– crafty polymorphism

Page 3: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

3

Parametric Polymorphism• Operates on all types of objects in a uniform way.

• C++ templates– The same C++ template is instantiated to

different classes.– cannot exploit specific properties of a given type.

• Java generics (J2SE 5.0)– another name for polymorphism similar to C++

templates

Page 4: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

4

Ad hoc Polymorphism• Different behavior depending on the type of objects

it operates on

• Operator overloading• + operator in SML

- 1 + 1;val it = 2 : int- 1.0 + 1.0;val it = 2.0 : real

Page 5: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

5

Outline• Polymorphism V• System F - syntax and reduction rules

– extension of the untyped -calculus with parametric polymorphism

• Crafty polymorphism• System F - type system

Page 6: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

6

Type of • Two tasks:

1. bind variable x to some type

2. decide the type of the resultant expression

• Candidates (for the simply typed -calculus)

Page 7: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

7

Idea: use "any type" • Read as "any type"

expression type

Page 8: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

8

Ambiguity

Page 9: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

9

How to distinguish between and ?

Page 10: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

10

Identity Function Again

• Let's apply the identity function to true.

Page 11: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

11

Type Application

Page 12: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

12

System F• Impredicative Polymorphic -calculus

Page 13: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

13

Reduction Rules for System F

Page 14: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

14

Outline• Polymorphism V• System F - syntax and reduction rules V• Crafty polymorphism

– extremely dangerous for programming• System F - type system

Page 15: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

15

Polymorph in Warcraft IIPolymorph

             Cost: 2000 Gold

Mana 200Range: 10

Time to Upgrade: 200 Time Units.

• Permanent effect• You may cast polymorph

on your ally units!

Page 16: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

16

Polymorph in Warcraft III

• Temporary effect– 60 seconds

Page 17: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

17

Polymorph in World of Warcraft?

Page 18: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

18

Outline• Polymorphism V• System F - syntax and reduction rules V• Crafty polymorphism V• System F - type system

Page 19: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

19

Composing Two Functions

• ! makes sense only after both and are declared.– the order of elements in a typing context matters.

Page 20: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

20

Typing Contexts• Ordered sets

Page 21: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

21

Valid or Invalid?

Page 22: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

22

Typing Rules

Page 23: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

23

Example

Page 24: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

24

Substitution Lemmas• As usual, except:

Page 25: CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우

25

Type Safety