Typesafe Extensible Functional Objects
J
- 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
- f
Ma r b u r g , G e r m a n y j
- n
a t h a n @ b
- s
t u d i
- s
. d e HessPL, September 10th 2014
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
J
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
Ma r b u r g , G e r m a n y j
a t h a n @ b
t u d i
. d e HessPL, September 10th 2014
2
Ob j e c t Al g e b r a s [ 1 ] : A l l
i n g m
u l a r d e fi n e d f
d s b y c h
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
a l g e b r a s . My
j . e x t e n d L i b r a r y : A l l
i n g m
u l a r d e fi n e d u n f
d s b y c h
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
c
l g e b r a s .
1 O
l i v e i r a , B r u n
. D . S . , a n d Wi l l i a m R . C
. " E x t e n s i b i l i t y f
t h e Ma s s e s . " E C O O P 2 1 2 – O b j e c t
r i e n t e d P r
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 1 2 .
3
E v e r y t i m e y
fi n d y
r s e l f w i s h i n g t h a t d e c
a t
s w
l d s u p p
t l a t e b i n d i n g a n d c
l d b e a r b i t r a r i l y c
e d :
4
T w
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 } }
5
… a l l
s :
… b u t i t d
s n
a l l
:
… w h a t c a n b e a c h i e v e d w i t h
j . e x t e n d :
First Class Values! Second Class Traits!
6
I n c r e me n t a l c
s t r u c t i
j e c t s p e r f
m e d b y m
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
s f
p r i n t i n g a n d t r a c i n g i n
d e r t
a c i l i t a t e d e b u g g i n g An n
a t i n g
j e c t s w i t h a d d i t i
a l i n f
m a t i
, a c q u i r e d a f t e r
j e c t c r e a t i
2 E. Ernst. gbeta – A Language with Virtual Attributes, Block Structure, and
Propagating, Dynamic Inheritance. PhD thesis, Department of Computer Science, University of Aarhus, Arhus, Denmark, 1999.
7
… b u i l d i n g
a c
l g e b r a i c e n c
i n g
j e c t s [ 4 ] :
– I
n t e r f a c e s a r e e n c
e d a s a n i n t e r f a c e e n d
u n c t
F.
– I
mp l e me n t a t i
s a r e e n c
e d a s c
l g e b r a s S F[S] ⇒ .
– I
n s t a n t i a t i
i s e n c
e d b y u n f
d i n g a c
l g e b r a w i t h a n i n i t i a l s t a t e t
h e g r e a t e s t fi x e d p
n t Fix[F].
– Ob
j e c t s a r e e n c
e d a s t e r m i n a l c
l g e b r a s
e r t h e s e f u n c t
s .
4 B. Jacobs. Objects and Classes, Coalgebraically, pages 83–103.
Springer-Verlag, 1995.
val c = unfold(Counter, 0) trait CounterF[S] { def get: Int def inc: S } val Counter = (i: Int) ⇒ new CounterF[Int] {...}
8
val c0 = unfold(Counter, 0) val c1 = c0.inc
9
val c0 = unfold(Counter, 0) val c1 = c0.inc
10
val c0 = unfold(Counter, 0) val c1 = c0.inc val c2 = c1 extend(SkipCounter, ())
11
val c0 = unfold(Counter, 0) val c1 = c0.inc val c2 = c1 extend(SkipCounter, ()) val c3 = c2.skip
T T h h e e t t y y p p e e c c h h a a n n g g e e s s ! !
12
… d e fi n i n g t h e f u n c t i
compose(co1, co2)
c
l g e b r a s a s compose(co1, co2) s mix(co ≅ ⇒
1(s), co2(s))
… i m p l e m e n t i n g extend i n t e r m s
compose, t h u s unfold(co1, s1) extend(co2, s2) i s i m p l e m e n t e d a s unfold(compose(co1, co2), (s1, s2))
13
– A
l l
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
l g e b r a s b y e n c
i n g s e l f
e f e r e n c e s .
– A
l l
s a c c e s s i n g p r i v a t e s l i c e s
t h e s t a t e u s i n g l e n s e s .
– A
l l
s r e f e r e n c e s t
h e e x t e n d e d b a s e c
l g e b r a , i m i t a t i n g s u p e r
a l l s .
– A
l l
s s e l e c t i v e
e n
e c u r s i
[ 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
n d s e l f
e f e r e n c e . ⇒ S
e
t h e e x t e n s i
s h a v e b e e n u s e d t
r a n s l a t e a s u b s e t
e n j d k w r i t e r s t
h e e n c
i n g .
3 J. Aldrich and K. Donnelly. Selective open recursion: Modular reasoning about
components and inheritance. SAVCBS 2004 Specification and Verification of Component-Based Systems, page 26, 2004.
14
We h a v e s h
n :
– We
c a n e n c
e d y n a m i c s p e c i a l i z a t i
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
k :
– O
p t i m i z e p e r f
m a n c e t
e p r a c t i c a l l y u s e f u l .
– D
e v e l
a c
s i s t e n t a n d e a s y t
s e d s l .
– I
n v e s t i g a t e d u a l i t y t
j e c t a l g e b r a s f
m a l l y .
15
S l i d e s : h t t p : / / fi l e s . b
t u d i
. d e / h e s s p l
l i d e s . p d f I C F P S R C P
t e r : h t t p : / / fi l e s . b
t u d i
. d e / i c f p 2 1 4
t e r . p d f Mi x i n C
i t i
: h t t p s : / / g i t h u b . c
/ b
t u d i
/ Mi x i n C
p
i t i
16
E n d
S l i d e s , n
h i n g t
e e h e r e .
17
trait Fix[F[_]] { def out: F[Fix[F]] def extend[G[_], S2](co2: S2 ⇒ G[S2], state2: S2): Fix[F WithF G] } def unfold[F[_], S1](co1: S1 => F[S1], state1: S1): Fix[F] = new Fix[F] {…}
… m a k i n g u s e
t h e “ fi r s t
l a s s y
e s s ”
Fix:
–
N
e l m e t h
extend i s a d d e d t
–
O r i g i n a l c
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 i n s i d e t h e c l
u r e
Fix b u t n e v e r r e v e a l e d .
A A b b s s t t r r a a c c t t i i
n B B a a r r r r i i e e r r