Joy of Coding
March 07, 2014
530

# Who’s afraid of Object Algebras? - Tijs van der Storm

March 07, 2014

## Transcript

1. ### Who’s afraid of Object  Algebras? Tijs van der Storm

@tvdstorm / storm@cwi.nl

4. ### 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))

3

“1 + 2”

9. ### 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
10. ### A design pattern inspired by abstract algebra to solve the

expression problem without fancy language features Object Algebras
11. ### Plan •Let the expression problem happen before your eyes •Make

it go away with Object Algebras
12. ### abstract class Exp { } ! class Lit extends Exp

{ int value; Lit(this.value); } ! class Add extends Exp { Exp lhs, rhs; Add(this.lhs, this.rhs); }
13. ### Standard OO style •+Natural encoding •+Easy to add variants •−Hard

to add operations •−Scattering of operations

15. ### Visitor style •+Easy to add operations •+Operation-speciﬁc state •−Hard to

add variants •−Double dispatch •−Breaks encapsulation

23. ### Extensibility for the Masses Practical Extensibility with Object Algebras Bruno

C. d. S. Oliveira1 and William R. Cook2 1National University of Singapore bruno@ropas.snu.ac.kr 2 University of Texas, Austin wcook@cs.utexas.edu Abstract. This paper presents a new solution to the expression problem (EP) that works in OO languages with simple generics (including Java

26. ### Multi-purpose objects Add print eval Lit: 1 print eval Lit:

2 print eval 1 + 2 =
27. ### 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 =
28. ### abstract class ExpFactory { Exp lit(int value); Exp add(Exp lhs,

Exp rhs); } Abstract factory

30. ### The link with algebra An algebraic signature [17] deﬁnes 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 peciﬁed in the signature . A given signat abstract class ExpAlg<E> { E lit(int n); E add(E lhs, E rhs); } Guttag, J. V., Horning, J.J.: The algebraic speciﬁcation of abstract datatypes. Acta Informatica (1978) Generic factory interfaces are algebraic signatures!
31. ### Extension! abstract class ExpAlg<E> { E lit(int n); E add(E

lhs, E rhs); } abstract class MulAlg<E> extends ExpAlg<E> { E mul(E lhs, E rhs); }

33. ### 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)

36. ### Extensibility print eval Lit Add Expression problem ExpAlg PrintExp MulAlg

EvalExp PrintMul EvalMul . . Object Algebras