Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우."— Presentation transcript:

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

2 2 Polymorphism The same piece of code can be reused for different types of objects –parameteric polymorphism –ad hoc polymorphism –crafty polymorphism

3 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

4 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

5 5 Outline Polymorphism V System F - syntax and reduction rules –extension of the untyped -calculus with parametric polymorphism Crafty polymorphism System F - type system

6 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)

7 7 Idea: use "any type"  Read  as "any type" expressiontype

8 8 Ambiguity

9 9 How to distinguish between  and  ?

10 10 Identity Function Again Let's apply the identity function to true.

11 11 Type Application

12 12 System F Impredicative Polymorphic -calculus

13 13 Reduction Rules for System F

14 14 Outline Polymorphism V System F - syntax and reduction rules V Crafty polymorphism –extremely dangerous for programming System F - type system

15 15 Polymorph in Warcraft II Polymorph Cost: 2000 Gold Mana 200 Range: 10 Time to Upgrade: 200 Time Units. Permanent effect You may cast polymorph on your ally units!

16 16 Polymorph in Warcraft III Temporary effect –60 seconds

17 17 Polymorph in World of Warcraft?

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

19 19 Composing Two Functions  !  makes sense only after both  and  are declared. –the order of elements in a typing context matters.

20 20 Typing Contexts Ordered sets

21 21 Valid or Invalid?

22 22 Typing Rules

23 23 Example

24 24 Substitution Lemmas As usual, except:

25 25 Type Safety


Download ppt "CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우."

Similar presentations


Ads by Google