type systems
play

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


  1. Type Systems Lecture 7 Dec. 1st, 2004 Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004

  2. Today Featherweight Java 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)

  3. 1. Recall Syntax of FJ Example 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; } }

  4. 1. Recall Syntax of FJ 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; } }

  5. 1. Recall Syntax of FJ 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 ( ) ; } }

  6. 1. Recall Syntax of FJ 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)

  7. 1. Recall Syntax of FJ 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

  8. 2. Static Semantics Judgem ent f or m s: A <: B subt ypi ng Γ ` t : C t er m t ypi ng m ok i n C wel l - f or 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 , C) = C � C m m t ype( m et hod t ype l ookup

  9. 2. Static Semantics 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

  10. 2. Static Semantics ent Γ i s m Envi r onm 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 � Var i abl es m ust be decl ar ed

  11. 2. Static Semantics Fi el d sel ect i on: Γ ` t 0 : C f i el ds( C 0 ) = C f 0 Γ ` t 0 . f i : C i � f i el d f i m ust be pr esent i n C 0 � i t s t ype i s speci f i ed i n C 0

  12. 2. Static Semantics M et hod i nvocat i on ( m essage send) : Γ ` t 0 : C 0 ) = C’ � D Γ ` t : C m t ype( m , C C<: C’ 0 Γ ` t 0 . m ( t ) : D � m et hod m ust be pr esent � ar gum ent t ypes m ust be subt ypes of par am et er s

  13. 2. Static Semantics 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 � cl ass nam e m ust exi st s � i ni t i al i zer s m ust be of subt ypes of f i el ds

  14. 2. Static Semantics Cast i ng: ( up or down) Γ ` t 0 : C ( C<: D or D<: C) Γ ` ( D) t 0 : D � ALL ALL cast s ( up/ down) ar e st at i cal l y accept abl e! � st upi d ( si de) cast s can be det ect ed: Γ ` t 0 : C not ( D<: C or D<: D) gi ve war ni ng! Γ ` ( D) t 0 : D

  15. 2. Static Semantics 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 � At r un- t i m e, onl y up- cast s wi l l succeed.

  16. 2. Static Semantics 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

  17. 2. Static Semantics 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 0 Γ ` 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

  18. 2. Static Semantics 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 0 Γ ` t 0 . f i : C i 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 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

  19. 2. Static Semantics W i t h t he cast t ypi ng succeeds! Γ ` t 0 : C f i el ds( C 0 ) = C f 0 Γ ` t 0 . f i : C i ( 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 ( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r )

  20. 2. Static Semantics 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 : O bj Pai r <: 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 ( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r )

  21. 2. Static Semantics 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( ) ) , f i el ds( Pai r ) = new A( ) ) : Pai r O bj f st , O bj snd new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st : O bj Pai r <: 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 ( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r )

  22. 2. Static Semantics 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( ) ) , f i el ds( Pai r ) = new A( ) ) : Pai r O bj f st , O bj snd new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st : O bj Pai r <: 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 ( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r )

  23. 2. Static Semantics 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 OK, because fields(A) = [ ] 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 new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st : O bj Pai r <: 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 ( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r )

  24. 2. Static Semantics W i t h t he cast t ypi ng succeeds! 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 new Pai r ( new A( ) , new B( ) ) : Pai r Pai r <: O bj new A( ) : A A <: O OK, because fields(A) = [ ] 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 new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st : O bj Pai r <: 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 ( new Pai r ( new Pai r ( new A( ) , new B( ) ) , new A( ) ) . f st ) . snd : O bj ( Pai r )

  25. 2. Static Semantics 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

  26. 2. Static Semantics W el l - For m ed M et hods CT( C) = cl ass C ext ends D { … } , D) equal s C � C m t ype( m 0 or undef i ned x: C, t hi s: C ` t 0 : E 0 E 0 <: C 0 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

Recommend


More recommend