Type Systems
Lecture 7 Dec. 1st, 2004 Sebastian Maneth
http://lampwww.epfl.ch/teaching/typeSystems/2004
Type Systems Lecture 7 Dec. 1st, 2004 Sebastian Maneth - - PowerPoint PPT Presentation
Type Systems Lecture 7 Dec. 1st, 2004 Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004 Today Featherweight Java 1. Recall Syntax of FJ 2. Static Semantics 3. Dynamic Semantics (Evaluation) 4. Type
Lecture 7 Dec. 1st, 2004 Sebastian Maneth
http://lampwww.epfl.ch/teaching/typeSystems/2004
1. Recall Syntax of FJ 2. Static Semantics 3. Dynamic Semantics (Evaluation) 4. Type Safety 5. Extensions
Many of today’s slides come from CS510 (2003 at Princeton by D.Walker) CMPSCI530 (2004/2002 at UMass Amherst by R. Harper)
cl ass Pt ext ends O bj ect { i nt x; i nt y; Pt ( i nt x, i nt y) { super ( ) ; t hi s. x = x; t hi s. y = y; } i nt get x( ) { r et ur n t hi s. x; } i nt get y( ) { r et ur n t hi s. y; } }
Example
cl ass CPt ext ends Pt { col or c; CPt ( i nt x, i nt y, col or c) { super ( x, y) ; t hi s. c = c; } col or get c ( ) { r et ur n t hi s. c; } }
Example
cl ass CPt ext ends Pt { col or c; CPt ( i nt x, i nt y, col or c) { super ( x, y) ; t hi s. c = c; } col or get c ( ) { r et ur n t hi s. c; } } cl ass i nt ext ends O bj ect { i nt ( ) { super ( ) ; } } cl ass col or ext ends O bj ect { col or ( ) { super ( ) ; } }
Example
Cl asses C : : = cl ass C ext ends D { C f ; K M } Const r uct or s K : : = C ( C x) { super ( x) ; t hi s. f =x; } M et hods M : : = C m ( C x) { r et ur n t ; } Ter m s t : : = x | t . f | t . m ( t ) | new C( t ) | ( C) t Underlining indicates a sequence of arbitrary length (≥ 0)
Objects are immutable: no mutatation of fields! ( cannot do a ‘set method’) FJ Pr ogr am FJ Pr ogr am = ( CT, t ) CT: cl ass t abl e ( e. g. , CT( i nt ) =cl ass i nt ext ends . . ) t : t er m t o be eval uat ed
Judgem ent f or m s: A <: B subt ypi ng Γ ` t : C t er m t ypi ng m
ed m et hod C ok wel l - f or m ed cl ass T ok wel l - f or m ed cl ass t abl e f i el ds( C) = C f f i el d l ookup m t ype( m , C) = C C m et hod t ype l ookup
Subt ypi ng Subt ype r el at i on <: det er m i ned by CT onl y! CT( C) = cl ass C ext ends D { … } C : < D r ef l exi ve C <: C t r ansi t i ve C <: D D : < E C : < E
Envi r onm ent Γ i s m appi ng f r om var i abl es t o t ypes ( cl asses) . Var i abl es can onl y appear i n m et hod bodi es. Γ( x) = T Γ ` x : T
ust be decl ar ed
Fi el d sel ect i on: Γ ` t 0: C f i el ds( C
0) = C f
Γ ` t 0. f i : C
i
m ust be pr esent i n C
M et hod i nvocat i on ( m essage send) : Γ ` t 0: C m t ype( m , C
0) = C’ D Γ ` t : C
C<: C’ Γ ` t 0. m ( t ) : D
et hod m ust be pr esent
ent t ypes m ust be subt ypes of par am et er s
I nst ant i at i on ( obj ect cr eat i on) : Γ ` t : C C<: C’ f i el ds( D) = C’ f Γ ` new D( t ) : D
e m ust exi st s
ust be of subt ypes of f i el ds
Cast i ng: Γ ` t 0: C ( C<: D or D<: C) Γ ` ( D) t 0 : D
ALL cast s ( up/ down) ar e st at i cal l y accept abl e!
( up or down) Γ ` t 0: C not ( D<: C or D<: D) gi ve war ni ng! Γ ` ( D) t 0 : D
W hy do we al l ow down- cast s? Needed f or appl yi ng cl ass- speci f i c m et hods, e. g. :
( ( Pai r ) new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd
e, onl y up- cast s wi l l succeed.
W i t hout t he cast , t ypi ng of t er m f ai l s:
( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj
W i t hout t he cast , t ypi ng of t er m f ai l s: Γ ` t 0: C f i el ds( C
0) = C f
Γ ` t 0. f i : C
i
new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) . f st : Pai r O bj f st , O bj snd ( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj
W i t hout t he cast , t ypi ng of t er m f ai l s:
( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) . f st : Pai r O bj f st , O bj snd new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) : Pai r O bj f st , O bj snd
Γ ` t 0: C f i el ds( C
0) = C f
Γ ` t 0. f i : C
i
W i t h t he cast t ypi ng succeeds!
( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r ) new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) . f st : Pai r O bj f st , O bj snd
( Pai r ) Γ ` t 0: C f i el ds( C
0) = C f
Γ ` t 0. f i : C
i
( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r ) new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) . f st : Pai r O bj f st , O bj snd
( Pai r )
new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st : O bj Pai r <: O bj
W i t h t he cast t ypi ng succeeds!
( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r ) new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) . f st : Pai r O bj f st , O bj snd
( Pai r )
new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st : O bj Pai r <: O bj new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) : Pai r O bj f st , O bj snd
W i t h t he cast t ypi ng succeeds! Γ ` t : C C<: C’ f i el ds( D) = C’ f Γ ` new D( t ) : D
( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r ) new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) . f st : Pai r O bj f st , O bj snd
( Pai r )
new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st : O bj Pai r <: O bj new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) : Pai r O bj f st , O bj snd
W i t h t he cast t ypi ng succeeds!
new Pai r ( new A( ) , new B( ) ) : Pai r Pai r <: O bj new A( ) : A A <: O bj
( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r ) new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) . f st : Pai r O bj f st , O bj snd
( Pai r )
new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st : O bj Pai r <: O bj new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) : Pai r O bj f st , O bj snd
W i t h t he cast t ypi ng succeeds!
new Pai r ( new A( ) , new B( ) ) : Pai r Pai r <: O bj new A( ) : A A <: O bj OK, because fields(A) = [ ]
( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r ) new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) . f st : Pai r O bj f st , O bj snd
( Pai r )
new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st : O bj Pai r <: O bj new Pai r ( new Pai r ( new A( ) , new B( ) ) , f i el ds( Pai r ) = new A( ) ) : Pai r O bj f st , O bj snd
W i t h t he cast t ypi ng succeeds!
new Pai r ( new A( ) , new B( ) ) : Pai r Pai r <: O bj new A( ) : A A <: O bj OK, because fields(A) = [ ] new A( ) : A A <: O bj f i el ds( Pai r ) = new B( ) : B B <: O bj O bj f st , O bj snd
W el l - For m ed Cl asses
K = C( D g, C f ) { super ( ) ; t hi s. f = f ; } f i el ds( D) = D g M ok i n C Cl ass C ext ends D { C f ; K M } ok const r uct or has ar gum ent s f or al l super - cl ass f i el ds and f or al l new f i el ds i ni t i al i ze super - cl ass bef or e new f i el ds new m et hods m ust be wel l - f or m ed
W el l - For m ed M et hods
CT( C) = cl ass C ext ends D { … } m t ype( m , D) equal s CC
0 or undef i ned
x: C, t hi s: C ` t 0 : E0 E0 <: C C
0 M
( C x) { r et ur n t 0; } ok i n C m ust r et ur n a subt ype of t he r esul t t ype i f over r i di ng, t hen t ype of m et hod m ust be sam e as bef or e
W el l - For m ed Cl ass Tabl e
f or al l C ∈ dom ( CT) , T( C) ok CT ok Al l cl asses i n CT m ust be wel l - f or m ed CT ok ` t : C ( CT, t ) ok
W el l - For m ed Pr ogr am
M et hod Type Lookup
CT( C) = cl ass C ext ends D { C f ; K M } B m ( B x) { r et ur n t ; } ∈ M m t ype( m , C) = B B CT( C) = cl ass C ext ends D { C f ; K M } m not def i ned i n M m t ype( m , C) = m t ype( m , D) M et hod Body Lookup wor ks exact l y t he sam e. r et ur ns ( x, t )
Fi el d Lookup
CT( C) = cl ass C ext ends D { C f ; K M } f i el ds( D) = D g f i el ds( m , C) = D g, C f f i el ds( O bj ect ) = [ ] Concat enat i on of super - cl ass f i el ds, pl us new ones
O bj ect val ues have t he f or m new c( s, t ) wher e s ar e t he val ues of super - cl ass f i el ds and t ar e t he val ues of C’ s f i el ds. f i el ds( C) = C f ( new C( v) ) . f i v i m body( m , C) = ( x, t 0) ( new C( v) ) . m ( u) [ xu, t hi snew C( v) ] t 0 C <: D ( D) ( new C( v) ) new C( v) f i el d sel ect i on m et hod i nvocat i on cast i ng
O bj ect val ues have t he f or m new c( s, t ) wher e s ar e t he val ues of super - cl ass f i el ds and t ar e t he val ues of C’ s f i el ds. f i el ds( C) = C f ( new C( v) ) . f i v i m body( m , C) = ( x, t 0) ( new C( v) ) . m ( u) [ xu, t hi snew C( v) ] t 0 C <: D ( D) ( new C( v) ) new C( v) f i el d sel ect i on m et hod i nvocat i on cast i ng
st uck, i f C i s not a subt ype
O bj ect val ues have t he f or m new c( s, t ) wher e s ar e t he val ues of super - cl ass f i el ds and t ar e t he val ues of C’ s f i el ds. f i el ds( C) = C f ( new C( v) ) . f i v i m body( m , C) = ( x, t 0) ( new C( v) ) . m ( u) [ xu, t hi snew C( v) ] t 0 C <: D ( D) ( new C( v) ) new C( v)
… pl us usual CBV eval uat i on r ul es!
M et hod Body Lookup
CT( C) = cl ass C ext ends D { C f ; K M } B m ( B x) { r et ur n t ; } ∈ M m body( m , C) = ( x, t ) CT( C) = cl ass C ext ends D { C f ; K M } m not def i ned i n M m body( m , C) = m body( m , D) “ Dynam i c Di spat ch”
bs up t he cl ass hi er ar chy sear chi ng f or t he m et hod st at i c sem ant i cs guar ant ees t hat m et hod exi st s!
Easy Questions:
Theorem (Preservation) Let CT be a well-formed class table. If t : C and t t ’ then t ’ : C’ for some C’ <: C.
how?
Canonical Forms Lemma. If v: C, then v = new D( t 0) with D<: C and t 0 value.
hierarchy than the static class.
Theorem (Progress) Let CT be a well-formed class table. If t : C then either
= ( C) new D( v0) and not( D <: C ), or
such that t t ’ .
W hi ch st at i c t ype check can we easi l y gener al i ze?
W hi ch st at i c t ype check can we easi l y gener al i ze? M et hod O ver r i de! W el l - For m ed M et hods
CT( C) = cl ass C ext ends D { … } m t ype( m , D) equal s CC
0 or undef i ned
x: C, t hi s: C ` t 0 : E0 E0 <: C C
0 M
( C x) { r et ur n t 0; } ok i n C m ust r et ur n a subt ype of t he r esul t t ype i f over r i di ng, t hen t ype of m et hod m ust be sam e sam e as bef or e
A m
ant i cs of over r i de: r esul t t ype i s subt ype subt ype of super cl ass r esul t t ype ar gum ent t ypes ar e super t ypes super t ypes of t he cor r espondi ng super cl ass ar gum ent t ypes. j ust as f or f unct i ons! covar i ant i n r esul t , cont r avar i ant i n ar gum ent .
W hy does t hi s wor k out ? Assum e C <: C’ and t 0: C. W e want t hat al so t 0: C’ . m t ype( m , C) = D D m t ype( m , C’ ) = D’ D’ Consi der t 0. m ( t )
essage send i s D and D<: D’ , so of t ype D’ .
m i ght be D’ , hence D, so m essage send i s O K.
Java adds ar r ay covar i ance: C <: D C [ ] <: D [ ]
No pr obl em f or FJ, whi ch does not suppor t assi gnm ent . W i t h assi gnm ent , m i ght st or e a super t ype val ue i n an ar r ay
Java i nser t s a per - assi gnm ent per - assi gnm ent r un- t i m e check t o ensur e saf et y
St at i c Fi el ds: M ust be i ni t i al i zed as par t of t he cl ass def i ni t i on ( not by t he const r uct or ) I n what or der ar e i ni t i al i zer s eval uat ed? – coul d r equi r e i ni t i al i zat i on t o a const ant . St at i c M et hods: Essent i al l y j ust r ecur si ve f unct i ons no over r i di ng st at i c di spat ch t o t he cl ass, no t he i nst ance.
Fi nal M et hods: Pr ecl ude over r i de i n a subcl ass Fi nal Fi el ds: O nl y sensi bl e i n t he pr esence of m ut at i on! Abst r act M et hods: Som e m et hods ar e undef i ned ( but decl ar ed) Cannot f or m an i nst ance i f any m et hod i s abst r act
I nt er f aces: Essent i al l y “ f ul l y abst r act ” cl asses No i nst ances adm i t t ed Al l ow “ m ul t i pl e i nher i t ance” . No di spat ch am bi gui t y because no i nst ance!
Cl ass Tabl es: Type checki ng r equi r es t he ent i r e ent i r e pr ogr am !
t he pr ogr am and l i br ar i es
anot her