Type Systems Lecture 7 Dec. 1st, 2004 Sebastian Maneth - - PowerPoint PPT Presentation

type systems
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Type Systems

Lecture 7 Dec. 1st, 2004 Sebastian Maneth

http://lampwww.epfl.ch/teaching/typeSystems/2004

slide-2
SLIDE 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)

slide-3
SLIDE 3

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

  • 1. Recall Syntax of FJ
slide-4
SLIDE 4

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; } }

  • 1. Recall Syntax of FJ

Example

slide-5
SLIDE 5

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 ( ) ; } }

  • 1. Recall Syntax of FJ

Example

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

slide-7
SLIDE 7

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

  • 1. Recall Syntax of FJ
slide-8
SLIDE 8

Judgem ent f or m s: A <: B subt ypi ng Γ ` t : C t er m t ypi ng m

  • k 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 m t ype( m , C) = C C m et hod t ype l ookup

  • 2. Static Semantics
slide-9
SLIDE 9

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

  • 2. Static Semantics
slide-10
SLIDE 10

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

  • Var i abl es m

ust be decl ar ed

  • 2. Static Semantics
slide-11
SLIDE 11

Fi el d sel ect i on: Γ ` t 0: C f i el ds( C

0) = C f

Γ ` t 0. f i : C

i

  • f i el d f i

m ust be pr esent i n C

  • i t s t ype i s speci f i ed i n C
  • 2. Static Semantics
slide-12
SLIDE 12

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

  • m

et hod m ust be pr esent

  • ar gum

ent t ypes m ust be subt ypes of par am et er s

  • 2. Static Semantics
slide-13
SLIDE 13

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

  • 2. Static Semantics
slide-14
SLIDE 14

Cast i ng: Γ ` 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:

( up or down) Γ ` t 0: C not ( D<: C or D<: D) gi ve war ni ng! Γ ` ( D) t 0 : D

  • 2. Static Semantics
slide-15
SLIDE 15

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.

  • 2. Static Semantics
slide-16
SLIDE 16

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

  • 2. Static Semantics
slide-17
SLIDE 17

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

  • 2. Static Semantics
slide-18
SLIDE 18

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

  • 2. Static Semantics
slide-19
SLIDE 19

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

  • 2. Static Semantics
slide-20
SLIDE 20

( 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!

  • 2. Static Semantics
slide-21
SLIDE 21

( 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

  • 2. Static Semantics
slide-22
SLIDE 22

( 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

  • 2. Static Semantics
slide-23
SLIDE 23

( 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) = [ ]

  • 2. Static Semantics
slide-24
SLIDE 24

( 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

  • 2. Static Semantics
slide-25
SLIDE 25

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

  • 2. Static Semantics
slide-26
SLIDE 26

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

  • 2. Static Semantics
slide-27
SLIDE 27

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

  • 2. Static Semantics
slide-28
SLIDE 28

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 )

  • 2. Static Semantics
slide-29
SLIDE 29

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

  • 2. Static Semantics
slide-30
SLIDE 30

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

  • 3. Dynamic Semantics (Evaluation)
slide-31
SLIDE 31

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

  • 3. Dynamic Semantics (Evaluation)

st uck, i f C i s not a subt ype

  • f D! ! !
slide-32
SLIDE 32

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)

  • 3. Dynamic Semantics (Evaluation)

… pl us usual CBV eval uat i on r ul es!

slide-33
SLIDE 33

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”

  • cl i m

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!

  • 3. Dynamic Semantics (Evaluation)
slide-34
SLIDE 34

Easy Questions:

  • 1. How can you (Church-) encode Booleans in FJ?
  • 2. What is the smallest nonterminating FJ program?
  • 3. Why is FJ Turing complete?
  • 4. Why can casts not be (fully) statically checked?
slide-35
SLIDE 35
  • 4. Type Safety

Theorem (Preservation) Let CT be a well-formed class table. If t : C and t t ’ then t ’ : C’ for some C’ <: C.

  • Proof by induction on the length of evaluations.
  • Type may get “smaller” during execution, due to casting!

how?

slide-36
SLIDE 36

Canonical Forms Lemma. If v: C, then v = new D( t 0) with D<: C and t 0 value.

  • Values of class type are objects (instances)
  • The dynamic class of an object may be lower in the subtype

hierarchy than the static class.

  • 4. Type Safety
slide-37
SLIDE 37

Theorem (Progress) Let CT be a well-formed class table. If t : C then either

  • 1. t is a value, or
  • 2. t

= ( C) new D( v0) and not( D <: C ), or

  • 3. there exists t ’

such that t t ’ .

  • Proof by induction on typing derivations.
  • Well-typed programs CAN GET STUCK!! But only because of casts..
  • Precludes “message not understood” error.
  • 4. Type Safety
slide-38
SLIDE 38
  • 5. Extensions

W hi ch st at i c t ype check can we easi l y gener al i ze?

slide-39
SLIDE 39
  • 5. Extensions

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

slide-40
SLIDE 40
  • 5. Extensions

A m

  • r e f l exi bl e st at i c sem

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 .

slide-41
SLIDE 41
  • 5. Extensions

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 )

  • Type of m

essage send i s D and D<: D’ , so of t ype D’ .

  • Type of t

m i ght be D’ , hence D, so m essage send i s O K.

slide-42
SLIDE 42
  • 5. Extensions

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

  • f t he subt ype. Subsequent r et r i val at supt ype unsound!

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

slide-43
SLIDE 43
  • 5. Extensions

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.

slide-44
SLIDE 44
  • 5. Extensions

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

slide-45
SLIDE 45
  • 5. Extensions

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!

slide-46
SLIDE 46
  • 5. Extensions

Cl ass Tabl es: Type checki ng r equi r es t he ent i r e ent i r e pr ogr am !

  • Cl ass t abl e i s a gl obal pr oper t y of

t he pr ogr am and l i br ar i es

  • Cannot t ype check cl asses separ at el y f r om

anot her