The expression problem is a challenging problem in
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s 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 paradigm
A programming paradigm is a relatively high-level way to conceptualize and structure the implementation of a computer program. A programming language can be classified as supporting one or more paradigms.
Paradigms are separated along and descri ...
s and
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s, and is still considered unsolved, although there are many proposed solutions.
History
Philip Wadler
Philip Lee Wadler (born April 8, 1956) is a UK-based American computer scientist known for his contributions to programming language design and type theory. He holds the position of Personal Chair of theoretical computer science at the Laborato ...
formulated the challenge and named it "The Expression Problem"
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.
[
] Reynolds discussed two forms of Data Abstraction: User-defined Types, which are now known as
Abstract Data Types
In computer science, an abstract data type (ADT) is a mathematical model for data types, defined by its behavior (semantics) from the point of view of a '' user'' of the data, specifically in terms of possible values, possible operations on data ...
(ADTs) (not to be confused with
Algebraic Data Types
In computer programming, especially functional programming and type theory, an algebraic data type (ADT) is a kind of composite data type, i.e., a data type formed by combining other types.
Two common classes of algebraic types are product type ...
), 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 William, Will, Willie, Bill or Billy Cook may refer to:
Sportsmen
Association football (soccer)
* William Cook (footballer) (1907–1968), English footballer who played for Darlington and Gateshead
* Willie Cook (footballer) (1906–1981), Sc ...
[
]
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 than
Reynolds considered, including internalization and optimization of methods.
At ECOOP '98,
Shriram 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
Racket is a general-purpose, multi-paradigm programming language. The Racket language is a modern dialect of Lisp and a descendant of Scheme. It is designed as a platform for programming language design and implementation. In addition to the co ...
, and they solved it
[
] via a rediscovery of
mixin
In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depe ...
s.
[
] 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
[
] 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.
*
Multiple dispatch
Multiple dispatch or multimethods is a feature of some programming languages in which a Subroutine, function or Method (computer programming), method can be dynamic dispatch, dynamically dispatched based on the run time (program lifecycle phase), ...
*
*
Coproduct
In category theory, the coproduct, or categorical sum, is a construction which includes as examples the disjoint union of sets and of topological spaces, the free product of groups, and the direct sum of modules and vector spaces. The cop ...
s of
functor
In mathematics, specifically category theory, a functor is a Map (mathematics), mapping between Category (mathematics), categories. Functors were first considered in algebraic topology, where algebraic objects (such as the fundamental group) ar ...
s
*
Type class
In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T a ...
es
* Tagless-final
/ Object algebras
* Polymorphic Variants
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:
interface IEvalExp
class Lit : IEvalExp
class Add : IEvalExp
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
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impleme ...
. For example, we might create the following class:
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.''
interface ExpAlgebra
class ExpFactory : ExpAlgebra
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:
interface IPrintExp : IEvalExp
class PrintableLit : Lit, IPrintExp
class PrintableAdd : Add, IPrintExp
class PrintFactory : ExpFactory, ExpAlgebra
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
*
Applications of FOSD program cubes
*
Generic programming
Generic programming is a style of computer programming in which algorithms are written in terms of data types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneer ...
*
POPLmark challenge
References
External links
The Expression Problemby
Philip Wadler
Philip Lee Wadler (born April 8, 1956) is a UK-based American computer scientist known for his contributions to programming language design and type theory. He holds the position of Personal Chair of theoretical computer science at the Laborato ...
.
Lecture: The Expression Problemby
Ralf Lämmell.
C9 Lectures: Dr. Ralf Lämmel - Advanced Functional Programming - The Expression Problemat
Channel 9.{{dead link, date=September 2022
Independently Extensible Solutions to the Expression Problem, Matthias Zenger and Martin Odersky, EPFL Lausanne
Programming language design
Unsolved problems in computer science