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
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 Ad hoc Polymorphism Different behavior depending on the type of objects it operates on Operator overloading + operator in SML ; val it = 2 : int ; val it = 2.0 : real
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 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 Idea: use "any type" Read as "any type" expressiontype
8 Ambiguity
9 How to distinguish between and ?
10 Identity Function Again Let's apply the identity function to true.
11 Type Application
12 System F Impredicative Polymorphic -calculus
13 Reduction Rules for System F
14 Outline Polymorphism V System F - syntax and reduction rules V Crafty polymorphism –extremely dangerous for programming System F - type system
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 Polymorph in Warcraft III Temporary effect –60 seconds
17 Polymorph in World of Warcraft?
18 Outline Polymorphism V System F - syntax and reduction rules V Crafty polymorphism V System F - type system
19 Composing Two Functions ! makes sense only after both and are declared. –the order of elements in a typing context matters.
20 Typing Contexts Ordered sets
21 Valid or Invalid?
22 Typing Rules
23 Example
24 Substitution Lemmas As usual, except:
25 Type Safety