expression problem
   HOME

TheInfoList



OR:

The expression problem is a challenging problem in
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
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). It exposed deficiencies in
programming paradigm Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms. Some paradigms are concerned mainly with implications for the execution model of the language, suc ...
s and
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s, and it is still not definitively solved, although there are many proposed solutions.


History

Philip Wadler Philip Lee Wadler (born April 8, 1956) is an American computer scientist known for his contributions to programming language design and type theory. He is the chair of Theoretical Computer Science at the Laboratory for Foundations of Computer S ...
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. An abstract data type is defined by its behavior (semantics) from the point of view of a ''user'', of the data, specifically in terms of possible values, pos ...
(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 type, i.e., a type formed by combining other types. Two common classes of algebraic types are product types (i.e., t ...
), 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. However, Reynold's conclusions based on this early analysis turned out to be completely wrong: he wrote that adding a second method to an object "is more a tour de force than a specimen of clear programming," which completely missed the Object-Oriented paradigm and its great success. He also believed the two forms of data abstraction "are inherently distinct and complementary." Fifteen years later in 1990,
William Cook William, Will, Willie, Bill or Billy Cook may refer to: Sportsmen * William Cook (billiards player), World Champion of English billiards in the 19th century * W. T. Cook (William Thomas Cook, 1884–1970), American college sports coach * Willi ...
applied Reynold's seminal 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 Shriram Krishnamurthi is a computer scientist, currently a professor of computer science at Brown University and a member of the core development group for the Racket programming languages, responsible for creation of software packages including ...
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 and a multi-platform distribution that includes the Racket language, compiler, large standard library, IDE, development tools, and a set of additional languages including Typed R ...
, 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 depend ...
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 Kim B. Bruce is an American computer scientist. He is the Reuben C. and Eleanor Winslow Professor of Computer Science at Pomona College, and was previously the Frederick Latimer Wells Professor of Computer Science at Williams College. He helped ...
(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 function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of ...
* Open classes *
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 coprodu ...
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 and ...
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: 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 Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
. 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

* Applications of FOSD program cubes *
Generic programming Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered b ...
*
POPLmark challenge In programming language theory, the POPLmark challenge (from "Principles of Programming Languages benchmark", formerly Mechanized Metatheory for the Masses!) (Aydemir, 2005) is a set of benchmarks designed to evaluate the state of automated reas ...


References


External links


The Expression Problem
by
Philip Wadler Philip Lee Wadler (born April 8, 1956) is an American computer scientist known for his contributions to programming language design and type theory. He is the chair of Theoretical Computer Science at the Laboratory for Foundations of Computer S ...
.
Lecture: The Expression Problem
by Ralf Lämmell.
C9 Lectures: Dr. Ralf Lämmel - Advanced Functional Programming - The Expression Problem
at 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