Slide 1

Slide 1 text

Who’s afraid of Object Algebras? Tijs van der Storm @tvdstorm / [email protected]

Slide 2

Slide 2 text

Rascal _ Enso

Slide 3

Slide 3 text

Extensibility Language with addition Multiplication feature

Slide 4

Slide 4 text

abstract class Exp { } ! class Lit extends Exp { int value; Lit(this.value); } ! class Add extends Exp { Exp lhs, rhs; Add(this.lhs, this.rhs); } Abstract syntax 1 + 2 new Add(new Lit(1), new Lit(2))

Slide 5

Slide 5 text

Add Mul Lit ... Variants print check eval ... Operations

Slide 6

Slide 6 text

Evaluate operation Add Lit: 1 Lit: 2 eval( ) = 3

Slide 7

Slide 7 text

Print operation Add Lit: 1 Lit: 2 print( ) = “1 + 2”

Slide 8

Slide 8 text

Variants Operations OO languages FP languages

Slide 9

Slide 9 text

The Expression Problem ! Philip Wadler, 12 November 1998 ! The Expression Problem is a new name for an old problem. The goal is to define a datatype by cases, where one can add new cases to the datatype and new functions over the datatype, without recompiling existing code, and while retaining static type safety (e.g., no casts). For the concrete example, we take expressions as the data type, begin with one case (constants) and one function (evaluators), then add one more construct (plus) and one more function (conversion to a string). http://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt

Slide 10

Slide 10 text

A design pattern inspired by abstract algebra to solve the expression problem without fancy language features Object Algebras

Slide 11

Slide 11 text

Plan •Let the expression problem happen before your eyes •Make it go away with Object Algebras

Slide 12

Slide 12 text

abstract class Exp { } ! class Lit extends Exp { int value; Lit(this.value); } ! class Add extends Exp { Exp lhs, rhs; Add(this.lhs, this.rhs); }

Slide 13

Slide 13 text

Standard OO style •+Natural encoding •+Easy to add variants •−Hard to add operations •−Scattering of operations

Slide 14

Slide 14 text

Visitor pattern

Slide 15

Slide 15 text

Visitor style •+Easy to add operations •+Operation-specific state •−Hard to add variants •−Double dispatch •−Breaks encapsulation

Slide 16

Slide 16 text

Decomposition print eval Lit Add

Slide 17

Slide 17 text

Object-oriented print eval Lit Add

Slide 18

Slide 18 text

Object-oriented print eval Lit Add

Slide 19

Slide 19 text

Visitor print eval Lit Add

Slide 20

Slide 20 text

Visitor print eval Lit Add

Slide 21

Slide 21 text

Variants Operations Variants Operations Standard style Visitor style

Slide 22

Slide 22 text

Solving the expression problem print eval Lit Add

Slide 23

Slide 23 text

Extensibility for the Masses Practical Extensibility with Object Algebras Bruno C. d. S. Oliveira1 and William R. Cook2 1National University of Singapore [email protected] 2 University of Texas, Austin [email protected] Abstract. This paper presents a new solution to the expression problem (EP) that works in OO languages with simple generics (including Java

Slide 24

Slide 24 text

Objects

Slide 25

Slide 25 text

Credit: Things Come Apart, by Todd McLellan

Slide 26

Slide 26 text

Multi-purpose objects Add print eval Lit: 1 print eval Lit: 2 print eval 1 + 2 =

Slide 27

Slide 27 text

Single purpose objects Add print Lit: 1 print Lit: 2 print printable 1 + 2 = Add eval Lit: 1 eval Lit: 2 eval evaluable 1 + 2 =

Slide 28

Slide 28 text

abstract class ExpFactory { Exp lit(int value); Exp add(Exp lhs, Exp rhs); } Abstract factory

Slide 29

Slide 29 text

Generic factory abstract class ExpFactory { E lit(int n); E add(E lhs, E rhs); }

Slide 30

Slide 30 text

The link with algebra An algebraic signature [17] defines the n perate over one or more abstract types, call f some primitive built-in sorts for integers a signature E lit: Int ⇥ E add: E E ⇥ E A general algebraic signature can contain he abstract set, as well as observations tha his paper we restrict signatures to only conta iven above. We call such signatures construc An -algebra is a set together with a co pecified in the signature . A given signat abstract class ExpAlg { E lit(int n); E add(E lhs, E rhs); } Guttag, J. V., Horning, J.J.: The algebraic specification of abstract datatypes. Acta Informatica (1978) Generic factory interfaces are algebraic signatures!

Slide 31

Slide 31 text

Extension! abstract class ExpAlg { E lit(int n); E add(E lhs, E rhs); } abstract class MulAlg extends ExpAlg { E mul(E lhs, E rhs); }

Slide 32

Slide 32 text

Object Algebra style

Slide 33

Slide 33 text

Object Algebras •Data type is a generic factory interface •Operation is a factory implementation •Easy to add variants (extend interface) •Easy to add operations (implement interface)

Slide 34

Slide 34 text

ExpAlg PrintExp MulAlg EvalExp PrintMul EvalMul ... ...

Slide 35

Slide 35 text

ExpAlg PrintExp MulAlg EvalExp PrintMul EvalMul Variants Operations ... ...

Slide 36

Slide 36 text

Extensibility print eval Lit Add Expression problem ExpAlg PrintExp MulAlg EvalExp PrintMul EvalMul . . Object Algebras

Slide 37

Slide 37 text

Object Algebras because extensibility