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

typesafe extensible functional objects
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

2

Ob j e c t Al g e b r a s [ 1 ] : A l l

  • w

i n g m

  • d

u l a r d e fi n e d f

  • l

d s b y c h

  • 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

  • n
  • f

a l g e b r a s . My

  • b

j . e x t e n d L i b r a r y : A l l

  • w

i n g m

  • d

u l a r d e fi n e d u n f

  • l

d s b y c h

  • 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

  • n
  • f

c

  • a

l g e b r a s .

Why Encode Objects in an OO-Language?

1 O

l i v e i r a , B r u n

  • C

. D . S . , a n d Wi l l i a m R . C

  • k

. " E x t e n s i b i l i t y f

  • r

t h e Ma s s e s . " E C O O P 2 1 2 – O b j e c t

  • O

r i e n t e d P r

  • 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 1 2 .

slide-3
SLIDE 3

3

E v e r y t i m e y

  • u

fi n d y

  • u

r s e l f w i s h i n g t h a t d e c

  • r

a t

  • r

s w

  • u

l d s u p p

  • r

t l a t e b i n d i n g a n d c

  • u

l d b e a r b i t r a r i l y c

  • mp
  • s

e d :

Why do I need this, again?

  • b

j . e x t e n d i s w h a t y

  • u

a r e l

  • k

i n g f

  • r

.

slide-4
SLIDE 4

4

T w

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

An Example of Objects in Scala.

slide-5
SLIDE 5

5

val c = new Counter with SkipCounter { var i = 0 }

… a l l

  • w

s :

Static Mixin Composition in Scala ...

… b u t i t d

  • e

s n

  • t

a l l

  • w

:

val c = new Counter { var i = 0 }; c extend SkipCounter

… w h a t c a n b e a c h i e v e d w i t h

  • b

j . e x t e n d :

val c = unfold(Counter, 0); c.extend(SkipCounter, ())

First Class Values! Second Class Traits!

slide-6
SLIDE 6

6

I n c r e me n t a l c

  • n

s t r u c t i

  • n
  • f
  • b

j e c t s p e r f

  • r

m e d b y m

  • 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

  • d

s f

  • r

p r i n t i n g a n d t r a c i n g i n

  • r

d e r t

  • f

a c i l i t a t e d e b u g g i n g An n

  • t

a t i n g

  • b

j e c t s w i t h a d d i t i

  • n

a l i n f

  • r

m a t i

  • n

, a c q u i r e d a f t e r

  • b

j e c t c r e a t i

  • n

Use Cases for Dynamic Specialization [2].

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.

slide-7
SLIDE 7

7

… b u i l d i n g

  • n

a c

  • a

l g e b r a i c e n c

  • d

i n g

  • f
  • b

j e c t s [ 4 ] :

– I

n t e r f a c e s a r e e n c

  • d

e d a s a n i n t e r f a c e e n d

  • f

u n c t

  • r

F.

– I

mp l e me n t a t i

  • n

s a r e e n c

  • d

e d a s c

  • a

l g e b r a s S F[S] ⇒ .

– I

n s t a n t i a t i

  • n

i s e n c

  • d

e d b y u n f

  • l

d i n g a c

  • a

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

  • t

h e g r e a t e s t fi x e d p

  • i

n t Fix[F].

– Ob

j e c t s a r e e n c

  • d

e d a s t e r m i n a l c

  • a

l g e b r a s

  • v

e r t h e s e f u n c t

  • r

s .

  • bj.extend Enables Dynamic Specialization by ...

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] {...}

slide-8
SLIDE 8

8

An Example of a Standard Terminal Coalgebra.

val c0 = unfold(Counter, 0) val c1 = c0.inc

slide-9
SLIDE 9

9

Same Example with obj.extend.

val c0 = unfold(Counter, 0) val c1 = c0.inc

slide-10
SLIDE 10

10

Same Example with obj.extend.

val c0 = unfold(Counter, 0) val c1 = c0.inc val c2 = c1 extend(SkipCounter, ())

slide-11
SLIDE 11

11

Same Example with obj.extend.

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

slide-12
SLIDE 12

12

… d e fi n i n g t h e f u n c t i

  • n

compose(co1, co2)

  • n

c

  • a

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

  • f

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))

  • bj.extend Enables Dynamic Specialization by ...
slide-13
SLIDE 13

13

– A

l l

  • 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

  • a

l g e b r a s b y e n c

  • d

i n g s e l f

  • r

e f e r e n c e s .

– A

l l

  • w

s a c c e s s i n g p r i v a t e s l i c e s

  • f

t h e s t a t e u s i n g l e n s e s .

– A

l l

  • w

s r e f e r e n c e s t

  • t

h e e x t e n d e d b a s e c

  • 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

  • w

s s e l e c t i v e

  • p

e n

  • r

e c u r s i

  • 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

  • u

n d s e l f

  • r

e f e r e n c e . ⇒ S

  • m

e

  • f

t h e e x t e n s i

  • n

s h a v e b e e n u s e d t

  • t

r a n s l a t e a s u b s e t

  • f
  • p

e n j d k w r i t e r s t

  • t

h e e n c

  • d

i n g .

And there is more to obj.extend...

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.

slide-14
SLIDE 14

14

We h a v e s h

  • w

n :

– We

c a n e n c

  • d

e d y n a m i c s p e c i a l i z a t i

  • n
  • f
  • 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

  • r

k :

– O

p t i m i z e p e r f

  • r

m a n c e t

  • b

e p r a c t i c a l l y u s e f u l .

– D

e v e l

  • p

a c

  • n

s i s t e n t a n d e a s y t

  • 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

  • b

j e c t a l g e b r a s f

  • r

m a l l y .

Conclusions.

slide-15
SLIDE 15

15

S l i d e s : h t t p : / / fi l e s . b

  • s

t u d i

  • 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

  • s

t e r : h t t p : / / fi l e s . b

  • s

t u d i

  • s

. d e / i c f p 2 1 4

  • p
  • s

t e r . p d f Mi x i n C

  • mp
  • s

i t i

  • n

: h t t p s : / / g i t h u b . c

  • m

/ b

  • s

t u d i

  • s

/ Mi x i n C

  • m

p

  • s

i t i

  • n

Further Materials.

slide-16
SLIDE 16

16

E n d

  • f

S l i d e s , n

  • t

h i n g t

  • s

e e h e r e .

EOS

slide-17
SLIDE 17

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] {…}

  • bj.extend Enables Dynamic Specialization by ...

… m a k i n g u s e

  • f

t h e “ fi r s t

  • c

l a s s y

  • n

e s s ”

  • f

Fix:

N

  • v

e l m e t h

  • d

extend i s a d d e d t

  • Fix

O r i g i n a l c

  • 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 i n s i d e t h e c l

  • s

u r e

  • f

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

n B B a a r r r r i i e e r r