CS 611: Lecture 33 - PowerPoint PPT Presentation

About This Presentation
Title:

CS 611: Lecture 33

Description:

Cornell University Computer Science Department. Andrew Myers ... where {a1,...,an} = FTV(t) - FTE(G) FTE(G) is removed to avoiding capturing type variables: ... – PowerPoint PPT presentation

Number of Views:133
Avg rating:3.0/5.0
Slides: 12
Provided by: kay86
Category:
Tags: ftv | lecture

less

Transcript and Presenter's Notes

Title: CS 611: Lecture 33


1
CS 611 Lecture 33
  • Parametric Polymorphism
  • November 15, 1999
  • Cornell University Computer Science Department
  • Andrew Myers

2
ML type reconstruction
  • Last time introduced idea of type
    reconstruction, started on Algorithm W
  • ML-like languagee n t f x e0 e1
    fn x e let (x e0) e1 letrec ((x1 e1)(xn
    en)) eb if e0 e1 e2
  • Typest int bool t0?t1 a
    (monomorphic)s ?a1,...,an.t
    t (polymorphic)
  • Typing context extended to allow polymorphic
    types
  • G Var?s
  • But expressions have monomorphic type
  • G ? e t

3
Typing rules
  • Correctness of algorithm W is judged with respect
    to typing rules
  • W(e, G, S) ?t, S?
  • Reconstructing type of e in typing context G
    with respect to substitution S yields type t,
    stronger substitution S
  • Correctness SG ? e St
  • Rules of interest

4
Making types generic
  • Generic(t, G) converts a type into a polymorphic
    form
  • Generic(t, G) ?a1,...,an.twhere a1,...,an
    FTV(t) - FTE(G)
  • FTE(G) is removed to avoiding capturing type
    variables FTE(G) ?t?range(G) FTV(t)
  • FTV(a) a FTV(int) ?
  • FTV(t0?t1) FTV(t0)?FTV(t1)

5
Algorithm W
  • W(e, G, S) ?t, S? SG ? e St
  • Unify(t1t2, S) S
  • W(n, G, S) ?int, S?
  • W(t, G, S) ?bool, S?
  • W(x, G, S) case Gx of t ? ?t, S?
  • ?a1,...,an.t ? ?ta1f/a1,..., S?
  • W(e0 e1, G, S) let ?t0, S0? W(e0, G, S) in
  • let ?t1, S1? W(e0, G, S0) in
  • ?af, Unify(t1?af t0, S1)?
  • W( fn x e, G, S) let ?t, S? W(e, Gx ? af,
    S) in ?af?t, S?

6
Remainder of algorithm
  • W(if e0 e1 e2, G, S) let ?t0, S0? W(e0, G, S)
    in
  • let ?t1, S1? W(e1, G, Unify(t0bool, S0)) in
  • let ?t2, S2? W(e2, G, S1) in
  • ?t1,Unify(t1t2, S2)?
  • W(let (x e0) e1, G, S) let ?t0, S0? W(e0, G,
    S) in
  • W(e1, Gx ? Generic(S0t, S0G), S0)
  • W(letrec ((x1 e1)(xn en)) eb, G, S0)
  • let G Gx1 ? a1f, in
  • let ?t1, S1? W(e1, G, S) in let ?tn, Sn?
    W(en, G, Sn-1) in
  • let S1 Unify(a1f t1, Sn) in...let Sn
    Unify(anf tn, Sn-1) in
  • let G Gx1 ? Generic(Snt1, SnG), in
  • W(eb, G, Sn)

7
Parametric polymorphism
  • Polymorphism expression has multiple types
  • Parametric polymorphism (?a1,...,an.t)
  • types appear as parameters
  • expression is written the same way for all types
  • polymorphic value is instantiated on some types
  • Predicative polymorphism polymorphic values are
    not first class (can only be instantiated)
  • ML (implicit), CLU, PolyJ (explicit)
  • Bounded parametric polymorphism (?aP.t) -- can
    only be instantiated on types satisfying P. P may
    be a kind or a fancier bound (CLU, PolyJ)
  • Templates (C, Modula-3) polymorphic
    expression is not type checked until
    instantiated a macro

8
Ad-hoc polymorphism
  • Same name can be used to denote values with
    different types
  • e.g. refers to one operator on int, another
    on float, yet another on String
  • Examples C, Java overloading
  • Can be modeled by allowing overloading in the
    type context G
  • Not true polymorphism no polymorphic values

9
Dependent types
  • Types polymorphic with respect to a value
  • Example CLU, some varieties of Pascal
  • procedure quicksort(a array1..n of
    integer, n int)
  • A generalization of bounded parametric
    polymorphism
  • ?n int, a type. t
  • Have to be careful about defining type
    equivalence if n can change...

10
Subtype polymorphism
  • One type S is a subtype of another type T if all
    the values of S are also values of T
  • A value is polymorphic because it is a member of
    all types that are supertypes of its type
  • Object-oriented languages support subtype
    polymorphism -- we will discuss in more detail
    later

11
First-class polymorphism
  • Polymorphic values to be used as first-class
    values impredicative parametric polymorphism
  • 2nd-order polymorphic lambda calculus
  • Examples GJ, Haskell
Write a Comment
User Comments (0)
About PowerShow.com