Expression problem explained

The expression problem is a challenging problem in programming languages that concerns the extensibility and modularity of statically typed data abstractions. The goal is to define a data abstraction that is extensible both in its representations and its behaviors, where one can add new representations and new behaviors to the data abstraction, without recompiling existing code, and while retaining static type safety (e.g., no casts). The statement of the problem exposes deficiencies in programming paradigms and programming languages, and is still considered unsolved, although there are many proposed solutions.

History

Philip Wadler formulated the challenge and named it "The Expression Problem"[1] in response to a discussion with Rice University's Programming Languages Team (PLT).He also cited three sources that defined the context for his challenge:

The problem was first observed by John Reynolds in 1975.[2] Reynolds discussed two forms of Data Abstraction: User-defined Types, which are now known as Abstract Data Types (ADTs) (not to be confused with Algebraic Data Types), and Procedural Data Structures, which are now understood as a primitive form of Objects with only one method. He argued that they are complementary, in that User-defined Types could be extended with new behaviors, and Procedural Data Structures could be extended with new representations. He also discussed related work going back to 1967. Fifteen years later in 1990, William Cook[3] applied Reynold's idea in the context of Objects and Abstract Data Types, which had both grown extensively. Cook identified the matrix of representations and behaviors that are implicit in a Data Abstraction, and discussed how ADTs are based on the behavioral axis, while Objects are based on the representation axis. He provides extensive discussion of work on ADTs and Objects that are relevant to the problem. He also reviewed implementations in both styles, discussed extensibility in both directions, and also identified the importance of static typing.Most importantly, he discussed situations in which there was more flexibility thanReynolds considered, including internalization and optimization of methods.

At ECOOP '98, Shriram Krishnamurthi et al.[4] presented a design pattern solution to the problem of simultaneously extending an expression-oriented programming language and its tool-set. They dubbed it the "expressivity problem" because they thought programming language designers could use the problem to demonstrate the expressive power of their creations. For PLT, the problem had shown up in the construction of DrScheme, now DrRacket, and they solved it[5] via a rediscovery of mixins.[6] [7] To avoid using a programming language problem in a paper about programming languages, Krishnamurthi et al. used an old geometry programming problem to explain their pattern-oriented solution. In conversations with Felleisen and Krishnamurthi after the ECOOP presentation, Wadler understood the PL-centric nature of the problem and he pointed out that Krishnamurthi's solution used a cast to circumvent Java's type system. The discussion continued on the types mailing list, where Corky Cartwright (Rice) and Kim Bruce (Williams) showed how type systems for OO languages might eliminate this cast. In response Wadler formulated his essay and stated the challenge, "whether a language can solve the expression problem is a salient indicator of its capacity for expression." The label "expression problem" puns on expression = "how much can your language express" and expression = "the terms you are trying to represent are language expressions".

Others co-discovered variants of the expression problem around the same time as Rice University's PLT, in particular Thomas Kühne[8] in his dissertation, and Smaragdakis and Batory[9] in a parallel ECOOP 98 article.

Some follow-up work used the expression problem to showcase the power of programming language designs.[10] [11]

The expression problem is also a fundamental problem in multi-dimensional Software Product Line design and in particular as an application or special case of FOSD Program Cubes.

Solutions

There are various solutions to the expression problem. Each solution varies in the amount of code a user must write to implement them, and the language features they require.

Example

Problem description

We can imagine we do not have the source code for the following library, written in C#, which we wish to extend:

public interface IEvalExp

public class Lit : IEvalExp

public class Add : IEvalExp

public static class ExampleOne

Using this library we can express the arithmetic expression as we did in and can evaluate the expression by calling . Now imagine that we wish to extend this library, adding a new type is easy because we are working with an Object-oriented programming language. For example, we might create the following class:

public class Mult : IEvalExp

However, if we wish to add a new function over the type (a new method in C# terminology) we have to change the interface and then modify all the classes that implement the interface. Another possibility is to create a new interface that extends the interface and then create sub-types for, and classes, but the expression returned in has already been compiled so we will not be able to use the new function over the old type. The problem is reversed in functional programming languages like F# where it is easy to add a function over a given type, but extending or adding types is difficult.

Problem Solution using Object Algebra

Let us redesign the original library with extensibility in mind using the ideas from the paper Extensibility for the Masses.

public interface ExpAlgebra

public class ExpFactory : ExpAlgebra

public static class ExampleTwo

We use the same implementation as in the first code example but now add a new interface containing the functions over the type as well as a factory for the algebra. Notice that we now generate the expression in using the interface instead of directly from the types. We can now add a function by extending the interface, we will add functionality to print the expression:

public interface IPrintExp : IEvalExp

public class PrintableLit : Lit, IPrintExp

public class PrintableAdd : Add, IPrintExp

public class PrintFactory : ExpFactory, ExpAlgebra

public static class ExampleThree

Notice that in we are printing an expression that was already compiled in, we did not need to modify any existing code. Notice also that this is still strongly typed, we do not need reflection or casting. If we would replace the with the in the we would get a compilation error since the method does not exist in that context.

See also

External links

Notes and References

  1. Web site: The Expression Problem.
  2. Book: User-defined Types and Procedural Data Structures as complementary approaches to Data Abstraction.. New Directions in Algorithmic Languages. 1975. Reynolds. John C.. IFIP Working Group 2.1 on Algol. 157–168.
  3. Book: Cook, William . William Cook (computer scientist). 1990 . Bakker . J.W. de. Roever . W.P. de . Rozenberg . G.. Foundations of Object-Oriented Languages (FOOL), REX School/Workshop. Noordwijkerhout, The Netherlands. Springer Berlin Heidelberg . 151–178 . Object-Oriented Programming versus Abstract Data Types . Lecture Notes in Computer Science . 489 . 10.1007/BFb0019443 . https://link.springer.com/chapter/10.1007/BFb0019443. 978-3-540-46450-1.
  4. Web site: Synthesizing Object-Oriented and Functional Design to Promote Re-Use.
  5. Modular object-oriented programming with units and mixins. 1999 . 10.1145/291251.289432 . Findler . Robert Bruce . Flatt . Matthew . ACM SIGPLAN Notices . 34 . 94–104 . free .
  6. PhD . Cook . William . 1989 . A Denotational Semantics of Inheritance . Brown University.
  7. Book: Classes and Mixins. 10.1145/268946.268961. Proceedings of the 25th ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '98. 171–183. 1998. Flatt. Matthew. Krishnamurthi. Shriram. Felleisen. Matthias. 978-0897919791. 5815257.
  8. Book: Kühne, Thomas. A Functional Pattern System for Object-Oriented Design. 1999. Verlag Dr. Kovac. Darmstadt. 978-3-86064-770-7.
  9. Book: Smaragdakis, Yannis. Don Batory. Implementing Reusable Object-Oriented Components. Lecture Notes in Computer Science . 1998. 1445.
  10. Extensible Algebraic Datatypes with Defaults . Matthias . Zenger. Martin . Odersky. 241–252 . 10.1.1.28.6778 . 2001 .
  11. Web site: Independently extensible solutions to the expression problem . Matthias . Zenger. Martin . Odersky. Foundations of Object-Oriented Languages (FOOL). ACM SIGPLAN. 10.1.1.107.4449 . 2005 .
  12. Chambers. Craig. Leavens. Gary T.. Type Checking and Modules for Multi-Methods. ACM Trans. Program. Lang. Syst.. November 1995. 17 . 6. 805–843. 10.1145/218570.218571 . free.
  13. Clifton. Curtis. Leavens. Gary T.. Chambers. Craig. Millstein. Todd. MultiJava: Modular Open Classes and Symmetric Multiple Dispatch for Java. Oopsla '00. 2000. 10.1145/353171.353181 . 7879645 .
  14. Wouter Swierstra. Data Types à La Carte. Journal of Functional Programming. 18. 4. 2008. 0956-7968. 423 - 436. Cambridge University Press. 10.1017/S0956796808006758. 21038598. free.
  15. Wehr. Stefan. Thiemann. Peter. JavaGI: The Interaction of Type Classes with Interfaces and Inheritance. ACM Trans. Program. Lang. Syst.. July 2011. 33. 10.1145/1985342.1985343 . 13174506 . free.
  16. Carette. Jacques. Kiselyov. Oleg. Chung-chieh. Shan. Finally Tagless, Partially Evaluated: Tagless Staged Interpreters for Simpler Typed Languages. J. Funct. Program.. 2009. 19 . 5 . 509–543 . 10.1017/S0956796809007205 . 6054319 .
  17. Oliveira. Bruno C. d. S.. Cook. William R.. Extensibility for the Masses: Practical Extensibility with Object Algebras. Ecoop '12. 2012.
  18. Garrigue. Jacques. Code Reuse Through Polymorphic Variants. 10.1.1.128.7169. 2000.