type systems
play

Type Systems Lecture 6 Nov. 24th, 2004 Sebastian Maneth - PowerPoint PPT Presentation

Type Systems Lecture 6 Nov. 24th, 2004 Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004 Today towards Featherweight JAVA 1. Objects 2. Simple Classes 3. Open Recursion through Self 4. Featherweight Java (FJ)


  1. Type Systems Lecture 6 Nov. 24th, 2004 Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004

  2. Today … towards Featherweight JAVA 1. Objects 2. Simple Classes 3. Open Recursion through Self 4. Featherweight Java (FJ)

  3. 1. Objects What is an OBJECT? � a data structure, encapsulating some internal state, and offering access to it via a collection of methods. Internal state = mutable instance variables Consider our simply-typed λ -calculus with records (and Unit type, and sequences) and references. Allocation r ef t r = r ef 5 is of type Ref Nat Dereference ! t ! r is of type Nat Assignment t : =t ’ is of type Unit

  4. 1. Objects OBJECT = a data structure, encapsulating some internal state, and offering access to it via a collection of methods. l et x = r ef 1 i n { get = λ _: Uni t . ! x, i nc = λ _: Uni t . x: =succ( ! x) } mutable instance variable methods

  5. 1. Objects OBJECT = a data structure, encapsulating some internal state, and offering access to it via a collection of methods. c = l et x = r ef 1 i n { get = λ _: Uni t . ! x, i nc = λ _: Uni t . x: =succ( ! x) } mutable instance variable methods The type of the record c is { get : Unit � Nat, inc : Unit � Unit } � c. i nc uni t evaluates to uni t : Uni t � c. get uni t evaluates to 2 : Nat

  6. 1. Objects be the type { get : Unit � Nat, inc : Unit � Unit } Let Count er i nc3 = λ c: Count er . ( c. i nc uni t ; c. i nc uni t ; c. i nc uni t ) i nc3 Takes an argument of type Count er (“a counter object”) and applies three times its i nc method. Can i nc3 i nc3 be applied to the following Reset Count er record? l et x = r ef 1 i n { get = λ _: Uni t . ! x, i nc = λ _: Uni t . x: =succ( ! x) , r eset = λ _: Uni t . x: =1}

  7. 1. Objects Can you write a function that generates and returns a new counter object, each time it is called?

  8. 1. Objects Can you write a function that generates and returns a new counter object, each time it is called? Sure! c = l et x = r ef 1 i n { get = λ _: Uni t . ! x, i nc = λ _: Uni t . x: =succ( ! x) } : Count er newCount er = λ _: Uni t . c : Uni t � Count er

  9. 1. Objects Can you write a function that generates and returns a new counter object, each time it is called? Sure! c = l et x = r ef 1 i n { get = λ _: Uni t . ! x, i nc = λ _: Uni t . x: =succ( ! x) } : Count er newCount er = λ _: Uni t . c : Uni t � Count er nc = newCount er uni t

  10. 1. Objects Group all instance variables into a record c = l et r = { x=r ef 1} i n { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . r . x: =succ( ! ( r . x) ) } : Count er Count er Rep = { x: Ref Nat } The representation type of the object.

  11. 2. Simple Classes newCount er = λ _: Uni t . c : Uni t � Count er newReset Count er = λ _: Uni t . c : Uni t � Reset Count er Reset Count er <: Count er How can we define a ResetCounter, using the definition of Counter?

  12. 2. Simple Classes l et r = { x=r ef 1} i n { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . r . x: =succ( ! ( r . x) ) } : Count er l et r = { x=r ef 1} i n { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . r . x: =succ( ! ( r . x) ) r eset = λ_ : Uni t . r . x: =1} : Reset Count er Reset Count er <: Count er How can we define a ResetCounter, using the definition of Counter?

  13. 2. Simple Classes should NOT be shared! l et r = { x=r ef 1} i n { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . r . x: =succ( ! ( r . x) ) } : Count er l et r = { x=r ef 1} i n { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . r . x: =succ( ! ( r . x) ) r eset = λ_ : Uni t . r . x: =1} : Reset Count er Or rather, how to describe their common functionality?

  14. 2. Simple Classes should NOT be shared! l et r = { x=r ef 1} i n f ( x) : Count er l et r = { x=r ef 1} i n { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . r . x: =succ( ! ( r . x) ) r eset = λ_ : Uni t . r . x: =1} : Reset Count er Or rather, how to describe their common functionality? NOT f ( x) ! Cannot be in terms of x! � Use the object’s representation type! Count er Rep = { x: Ref Nat }

  15. 2. Simple Classes l et r = { x=r ef 1} i n count er Cl ass r : Count er count er Cl ass = λ r : Count er Rep. { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . r . x: =succ( ! ( r . x) ) } : Count er Rep � Count er � How to define r eset Count er Cl ass in terms of count er Cl ass??

  16. 2. Simple Classes l et r = { x=r ef 1} i n count er Cl ass r : Count er count er Cl ass = λ r : Count er Rep. { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . x: =succ( ! ( r . x) ) } : Count er Rep � Count er � How to define r eset Count er Cl ass in terms of count er Cl ass?? r eset Count er Cl ass = λ r : Count er Rep. { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . r . x: =succ( ! ( r . x) ) r eset = λ _: Uni t . r . x: =1} : Count er Rep � Reset Count er

  17. 2. Simple Classes l et r = { x=r ef 1} i n count er Cl ass r : Count er count er Cl ass = λ r : Count er Rep. { get = λ _: Uni t . ! ( r . x) , i nc = λ _: Uni t . x: =succ( ! ( r . x) ) } : Count er Rep � Count er � How to define r eset Count er Cl ass in terms of count er Cl ass?? r eset Count er Cl ass = λ r : Count er Rep. l et super super = count er Cl ass r i n { get = super super . get , i nc = super super . i nc, r eset = λ _: Uni t . r . x: =1} : Count er Rep � Reset Count er

  18. 2. Simple Classes r eset Count er Cl ass = λ r : Count er Rep. l et super super = count er Cl ass r i n { get = super super . get , i nc = super super . i nc, r eset = λ _: Uni t . r . x: =1} : Count er Rep � Reset Count er Can we instantiate r eset Count er Cl ass with a different record of instance variables? E.g. BackupCount er Rep = { x: Ref Nat , b: Ref Nat }

  19. 2. Simple Classes r eset Count er Cl ass = λ r : Count er Rep. l et super super = count er Cl ass r i n { get = super super . get , i nc = super super . i nc, r eset = λ _: Uni t . r . x: =1} : Count er Rep � Reset Count er Can we instantiate r eset Count er Cl ass with a different record of instance variable?? E.g. BackupCount er Rep = { x: Ref Nat , b: Ref Nat } backupCount er Cl ass = λ r : BackupCount er Rep. l et super super = r eset Count er Cl ass r i n { get = super super . get , i nc = super super . i nc, r eset = λ _: Uni t . r . x: =! ( r . b) , backup = λ _: Uni t . r . b: =! ( r . x) } : BackupCount er Rep � BackupCount er

  20. What is a Class? 2. Simple Classes l et r = { x=r ef 1} i n count er Cl ass r : Count er Class = collection of methods obtained from an object by abstracting its methods w.r.t its instance variables. (type: Count er Rep � Count er ) An object can be obtained from a Class by instantiating it. Only other use of a Class: extending (= “subtype”) it count er Cl ass Count er instantiate extend r eset Count er Cl ass

  21. 2. Simple Classes Set Count er = { get : Uni t � Nat , set : Nat � Uni t , i nc: Uni t � Uni t } set Count er Cl ass = λ r : Count er Rep. { get = λ _: Uni t . ! ( r . x) , set = λ i : Nat . r . x: =i , i nc = λ _: Uni t . set ( succ( get uni t ) ) } : Count er Rep � Set Count er not possible!! get / set come from Set Count er itself!

  22. Recall the f i x operator. Γ ` t 1 : T 1 � T 1 Γ ` f i x t 1 : T 1 T 1 need not be a function type!! � can be a record type!! f f = λ m ut r ec: { i seven: Nat � Bool , i sodd: Nat � Bool } . { i seven = λ x: Nat . i f i szer o x t hen t r ue el se m ut r ec. i sodd ( pr ed x) , i sodd = λ x: Nat . i f i sZer o x t hen f al se el se m ut r ec. i seven ( pr ed x) } ( f i x f f ) . i seven 7 � … � f al se

  23. 2. Simple Classes Set Count er = { get : Uni t � Nat , set : Nat � Uni t , i nc: Uni t � Uni t } set Count er Cl ass = λ r : Count er Rep. { get = λ _: Uni t . ! ( r . x) , set = λ i : Nat . r . x: =i , i nc = λ _: Uni t . set ( succ( get uni t ) ) } ) : Count er Rep � Set Count er not possible!! get / set come from Set Count er itself!

  24. 2. Simple Classes Set Count er = { get : Uni t � Nat , set : Nat � Uni t , i nc: Uni t � Uni t } set Count er Cl ass = λ r : Count er Rep. f i x ( λ sel f : Set Count er . { get = λ _: Uni t . ! ( r . x) , set = λ i : Nat . r . x: =i , i nc = λ _: Uni t . sel f . set ( succ( sel f . get uni t ) ) } ) : Count er Rep � Set Count er get / set come from Set Count er itself ! newSet Count er = λ _: Uni t . l et r ={ x=r ef 1} i n set Count er Cl ass r

  25. 3. Open Recursion through Self set Count er Cl ass = λ r : Count er Rep. f i x ( λ sel f : Set Count er . { get = λ _: Uni t . ! ( r . x) , set = λ i : Nat . r . x: =i , i nc = λ _: Uni t . sel f . set ( succ( sel f . get uni t ) ) } ) : Count er Rep � Set Count er newSet Count er = λ _: Uni t . l et r ={ x=r ef 1} i n set Count er Cl ass r

Recommend


More recommend