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.
History
The problem was first observed by John Reynolds in 1975. At ECOOP '98, Krishnamurthi et al. 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 via a rediscovery of mixins. 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 and Kim Bruce 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 in his dissertation, and Smaragdakis and Batory in a parallel ECOOP 98 article. Some follow-up work used the expression problem to showcase the power of programming language designs. 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.
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 expression1 + 2 as we did in ExampleOne.AddOneAndTwo and can evaluate the expression by calling .Eval. 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 we have to change the IEvalExp interface and then modify all the classes that implement the interface. Another possibility is to create a new interface that extends the IEvalExp interface and then create sub-types for Lit, Add and Mult classes, but the expression returned in ExampleOne.AddOneAndTwo 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 ExampleTwo.AddOneToTwo using the ExpAlgebra interface instead of directly from the types. We can now add a function by extending the ExpAlgebra 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 ExampleThree.Print we are printing an expression that was already compiled in ExampleTwo, 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 PrintFactory with the ExpFactory in the ExampleThree.Print we would get a compilation error since the .Print method does not exist in that context.