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

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

A846fc46522b396026adcb62e162b7dc?s=128

Joy of Coding

March 07, 2014
Tweet

Transcript

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

    @tvdstorm / storm@cwi.nl
  2. Rascal _ Enso

  3. Extensibility Language with addition Multiplication feature

  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))
  5. Add Mul Lit ... Variants print check eval ... Operations

  6. Evaluate operation Add Lit: 1 Lit: 2 eval( ) =

    3
  7. Print operation Add Lit: 1 Lit: 2 print( ) =

    “1 + 2”
  8. Variants Operations OO languages FP languages

  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
  14. Visitor pattern

  15. Visitor style •+Easy to add operations •+Operation-specific state •−Hard to

    add variants •−Double dispatch •−Breaks encapsulation
  16. Decomposition print eval Lit Add

  17. Object-oriented print eval Lit Add

  18. Object-oriented print eval Lit Add

  19. Visitor print eval Lit Add

  20. Visitor print eval Lit Add

  21. Variants Operations Variants Operations Standard style Visitor style

  22. Solving the  expression problem print eval Lit Add

  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
  24. Objects

  25. Credit: Things Come Apart, by Todd McLellan

  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
  29. Generic factory abstract class ExpFactory<E> { E lit(int n); E

    add(E lhs, E rhs); }
  30. 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!
  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); }
  32. Object Algebra style

  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)
  34. ExpAlg PrintExp MulAlg EvalExp PrintMul EvalMul ... ...

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

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

    EvalExp PrintMul EvalMul . . Object Algebras
  37. Object Algebras because extensibility