Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

Joy of Coding

March 07, 2014
Tweet

More Decks by Joy of Coding

Other Decks in Technology

Transcript

  1. 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))
  2. 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
  3. A design pattern inspired by abstract algebra to solve the

    expression problem without fancy language features Object Algebras
  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); }
  5. Standard OO style •+Natural encoding •+Easy to add variants •−Hard

    to add operations •−Scattering of operations
  6. Visitor style •+Easy to add operations •+Operation-specific state •−Hard to

    add variants •−Double dispatch •−Breaks encapsulation
  7. 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
  8. 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 =
  9. 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> { 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!
  10. 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); }
  11. 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)