typesafe extensible functional objects
play

Typesafe Extensible Functional Objects J o n a t h a n I m - PowerPoint PPT Presentation

Typesafe Extensible Functional Objects J o n a t h a n I m m a n u e l B r a c h t h u s e r U n i v e r s i t y o f Ma r b u r g , G e r m a n y j o n a t h a n @ b - s t u d


  1. Typesafe Extensible Functional Objects J o n a t h a n I m m a n u e l B r a c h t h ä u s e r U n i v e r s i t y o f Ma r b u r g , G e r m a n y j o n a t h a n @ b - s t u d i o s . d e HessPL, September 10 th 2014

  2. Why Encode Objects in an OO-Language? Ob j e c t Al g e b r a s [ 1 ] : A l l o w i n g m o d u l a r d e fi n e d f o l d s b y c h o o s i n g a fi r s t c l a s s r e p r e s e n t a t i o n o f a l g e b r a s . My o b j . e x t e n d L i b r a r y : A l l o w i n g m o d u l a r d e fi n e d u n f o l d s b y c h o o s i n g a fi r s t c l a s s e x t e n s i b l e r e p r e s e n t a t i o n o f c o a l g e b r a s . 1 O l i v e i r a , B r u n o C . D . S . , a n d Wi l l i a m R . C o o k . " E x t e n s i b i l i t y f o r t h e Ma s s e s . " 2 E C O O P 2 0 1 2 – O b j e c t - O r i e n t e d P r o g r a m m i n g . S p r i n g e r B e r l i n H e i d e l b e r g , 2 0 1 2 .

  3. Why do I need this, again? E v e r y t i m e y o u fi n d y o u r s e l f w i s h i n g t h a t d e c o r a t o r s w o u l d s u p p o r t l a t e b i n d i n g a n d c o u l d b e a r b i t r a r i l y c o mp o s e d : o b j . e x t e n d i s w h a t y o u a r e l o o k i n g f o r . 3

  4. An Example of Objects in Scala. T w o p l a i n S c a l a t r a i t s : trait Counter { private var i : Int def get : Int = i def inc : Unit = { i += 1 } } trait SkipCounter { self : Counter ⇒ def skip : Unit = { this . inc ; this . inc } } 4

  5. Static Mixin Composition in Scala ... … a l l o w s : val c = new Counter with SkipCounter { var i = 0 } … b u t i t d o e s n o t a l l o w : val c = new Counter { var i = 0 }; c extend SkipCounter Second Class Traits! … w h a t c a n b e a c h i e v e d w i t h o b j . e x t e n d : val c = unfold(Counter, 0); c. extend (SkipCounter, ()) First Class Values! 5

  6. Use Cases for Dynamic Specialization [2]. I n c r e me n t a l c o n s t r u c t i o n o f o b j e c t s p e r f o r m e d b y m o d u l a r i z e d b u i l d e r s A d d i n g me t h o d s f o r p r i n t i n g a n d t r a c i n g i n o r d e r t o f a c i l i t a t e d e b u g g i n g An n o t a t i n g o b j e c t s w i t h a d d i t i o n a l i n f o r m a t i o n , a c q u i r e d a f t e r o b j e c t c r e a t i o n 2 E. Ernst. gbeta – A Language with Virtual Attributes, Block Structure, and 6 Propagating, Dynamic Inheritance. PhD thesis, Department of Computer Science, University of Aarhus, Arhus, Denmark, 1999.

  7. obj .extend Enables Dynamic Specialization by ... … b u i l d i n g o n a c o a l g e b r a i c e n c o d i n g o f o b j e c t s [ 4 ] : – I F . n t e r f a c e s a r e e n c o d e d a s a n i n t e r f a c e e n d o f u n c t o r ⇒ – I S F[S] mp l e me n t a t i o n s a r e e n c o d e d a s c o a l g e b r a s . – I n s t a n t i a t i o n i s e n c o d e d b y u n f o l d i n g a c o a l g e b r a w i t h Fix[F] . a n i n i t i a l s t a t e t o t h e g r e a t e s t fi x e d p o i n t – Ob j e c t s a r e e n c o d e d a s t e r m i n a l c o a l g e b r a s o v e r t h e s e f u n c t o r s . val Counter = (i: Int) ⇒ new CounterF[Int] {...} val c = unfold(Counter, 0) trait CounterF[S] { def get : Int def inc : S 4 B. Jacobs. Objects and Classes, Coalgebraically , pages 83–103. 7 } Springer-Verlag, 1995.

  8. An Example of a Standard Terminal Coalgebra. val c 0 = unfold(Counter, 0) val c 1 = c 0 .inc 8

  9. Same Example with obj .extend. val c 0 = unfold(Counter, 0) val c 1 = c 0 .inc 9

  10. Same Example with obj .extend. val c 0 = unfold(Counter, 0) val c 1 = c 0 .inc val c 2 = c 1 extend (SkipCounter, ()) 10

  11. Same Example with obj .extend. val c 0 = unfold(Counter, 0) val c 1 = c 0 .inc val c 2 = c 1 extend (SkipCounter, ()) T T h h e e t t y y p p e e c c val c 3 = c 2 .skip h h a a n n g g e e s s ! ! 11

  12. obj .extend Enables Dynamic Specialization by ... compose(co 1 , co 2 ) … d e fi n i n g t h e f u n c t i o n o n c o a l g e b r a s a s ≅ ⇒ compose(co 1 , co 2 ) s mix(co 1 (s), co 2 (s)) extend compose , … i m p l e m e n t i n g i n t e r m s o f t h u s unfold(co 1 , s 1 ) extend (co 2 , s 2 ) i s i m p l e m e n t e d a s unfold(compose(co 1 , co 2 ), (s 1 , s 2 )) 12

  13. And there is more to obj .extend... – A l l o w s m u t u a l d e p e n d e n c i e s b e t w e e n c o a l g e b r a s b y e n c o d i n g s e l f - r e f e r e n c e s . – A l l o w s a c c e s s i n g p r i v a t e s l i c e s o f t h e s t a t e u s i n g l e n s e s . – A l l o w s r e f e r e n c e s t o t h e e x t e n d e d b a s e c o a l g e b r a , i m i t a t i n g s u p e r - c a l l s . – A l l o w s s e l e c t i v e o p e n - r e c u r s i o n [ 3 ] b y p a s s i n g t h e c u r r e n t a s w e l l a s t h e l a t e b o u n d s e l f - r e f e r e n c e . ⇒ S o m e o f t h e e x t e n s i o n s h a v e b e e n u s e d t o t r a n s l a t e a s u b s e t o f o p e n j d k w r i t e r s t o t h e e n c o d i n g . 3 J. Aldrich and K. Donnelly. Selective open recursion: Modular reasoning about 13 components and inheritance. SAVCBS 2004 Specification and Verification of Component-Based Systems , page 26, 2004.

  14. Conclusions. We h a v e s h o w n : – We c a n e n c o d e d y n a m i c s p e c i a l i z a t i o n o f o b j e c t s i n S c a l a . I t s e e ms : – O b j e c t a l g e b r a s c a n b e u s e f u l l y d u a l i z e d . F u t u r e w o r k : – O p t i m i z e p e r f o r m a n c e t o b e p r a c t i c a l l y u s e f u l . – D e v e l o p a c o n s i s t e n t a n d e a s y t o u s e d s l . – I n v e s t i g a t e d u a l i t y t o o b j e c t a l g e b r a s f o r m a l l y . 14

  15. Further Materials. S l i d e s : h t t p : / / fi l e s . b - s t u d i o s . d e / h e s s p l - s l i d e s . p d f I C F P S R C P o s t e r : h t t p : / / fi l e s . b - s t u d i o s . d e / i c f p 2 0 1 4 - p o s t e r . p d f Mi x i n C o mp o s i t i o n : h t t p s : / / g i t h u b . c o m / b - s t u d i o s / Mi x i n C o m p o s i t i o n 15

  16. EOS E n d o f S l i d e s , n o t h i n g t o s e e h e r e . 16

  17. obj .extend Enables Dynamic Specialization by ... A A b b s s t t r r a a c c t t i i o o n n B B a a r r r r i i Fix : e e … m a k i n g u s e o f t h e “ fi r s t - c l a s s y - n e s s ” o f r r – extend N o v e l m e t h o d Fix i s a d d e d t o – O r i g i n a l c o a l g e b r a a n d i n i t i a l s t a t e a r e k e p t Fix i n s i d e t h e c l o s u r e o f b u t n e v e r r e v e a l e d . trait Fix[ F [_]] { def out : F [Fix[ F ]] ⇒ G [ S 2 ], state 2 : S 2 ): Fix[ F WithF G ] def extend [ G [_], S 2 ]( co 2 : S 2 } 17 def unfold[ F [_], S 1 ]( co 1 : S 1 => F [S 1 ], state 1 : S 1 ): Fix[ F ] = new Fix[ F ] {…}

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend