Expression problem

The expression problem is a term used in discussing strengths and weaknesses of various programming paradigms and programming languages.

Philip Wadler coined the term[1] in response to a discussion with Rice University's Programming Languages Team (PLT):

The expression problem is a new name for an old problem.[2][3] 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).

History

The problem was first observed by John Reynolds in 1975.[2]

At ECOOP '98, 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] 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[7] in his dissertation, and Smaragdakis and Batory[8] in a parallel ECOOP 98 article.

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

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:

 1 public interface IEvalExp
 2 {
 3     int Eval();
 4 }
 5 public class Lit : IEvalExp
 6 {
 7     public Lit(int n)
 8     {
 9         N = n;
10     }
11     public int N { get; }
12     public int Eval()
13     {
14         return N;
15     }
16 }
17 public class Add : IEvalExp
18 {
19     public Add(IEvalExp left, IEvalExp right)
20     {
21         Left = left;
22         Right = right;
23     }
24     public IEvalExp Left { get; }
25     public IEvalExp Right { get; }
26     public int Eval()
27     {
28         return Left.Eval() + Right.Eval();
29     }
30 }
31 
32 public static class ExampleOne
33 {
34     public static IEvalExp AddOneAndTwo() => new Add(new Lit(1), new Lit(2));
35 
36     public static int EvaluateTheSumOfOneAndTwo() => AddOneAndTwo().Eval();
37 }

Using this library we can express the arithmetic expression 1 + 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:

 1 public class Mult : IEvalExp
 2 {
 3 
 4     public Mult(IEvalExp left, IEvalExp right)
 5     {
 6         Left = left;
 7         Right = right;
 8     }
 9 
10     public IEvalExp Left { get; }
11     public IEvalExp Right { get; }
12 
13     public int Eval()
14     {
15         return Left.Eval() * Right.Eval();
16     }
17 }

However, if we wish to add a new function over the type (a new method in C# terminology) 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 Extensibiltiy for the Masses.[16]

 1 public interface ExpAlgebra<T>
 2 {
 3     T Lit(int n);
 4     T Add(T left, T right);
 5 }
 6 
 7 public class ExpFactory : ExpAlgebra<IEvalExp>
 8 {
 9     public IEvalExp Add(IEvalExp left, IEvalExp right)
10     {
11         return new Add(left, right);
12     }
13 
14     public IEvalExp Lit(int n)
15     {
16         return new Lit(n);
17     }
18 }
19 
20 public static class ExampleTwo<T>
21 {
22     public static T AddOneToTwo(ExpAlgebra<T> ae) => ae.Add(ae.Lit(1), ae.Lit(2));
23 }

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<T> interface instead of directly from the types. We can now add a function by extending the ExpAlgebra<T> interface, we will add functionality to print the expression:

 1     public interface IPrintExp : IEvalExp
 2     {
 3         string Print();
 4     }
 5 
 6     public class PrintableLit : Lit, IPrintExp
 7     {
 8         public PrintableLit(int n) : base(n)
 9         {
10             N = n;
11         }
12 
13         public int N { get; }
14 
15         public string Print()
16         {
17             return N.ToString();
18         }
19     }
20 
21     public class PrintableAdd : Add, IPrintExp
22     {
23         public PrintableAdd(IPrintExp left, IPrintExp right) : base(left, right)
24         {
25             Left = left;
26             Right = right;
27         }
28 
29         public new IPrintExp Left { get; }
30         public new IPrintExp Right { get; }
31 
32         public string Print()
33         {
34             return Left.Print() + " + " + Right.Print();
35         }
36     }
37 
38     public class PrintFactory : ExpFactory, ExpAlgebra<IPrintExp>
39     {
40         public IPrintExp Add(IPrintExp left, IPrintExp right)
41         {
42             return new PrintableAdd(left, right);
43         }
44 
45         public new IPrintExp Lit(int n)
46         {
47             return new PrintableLit(n);
48         }
49     }
50 
51     public static class ExampleThree
52     {
53         public static int Evaluate() => ExampleTwo<IPrintExp>.AddOneToThree(new PrintFactory()).Eval();
54         public static string Print() => ExampleTwo<IPrintExp>.AddOneToThree(new PrintFactory()).Print();
55     }

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.

See also

References

  1. "The Expression Problem".
  2. Reynolds, John C. (1975). "User-defined types and procedural data as complementary approaches to data abstraction.". New Directions in Algorithmic Languages. IFIP Working Group 2.1 on Algol. pp. 157–168.
  3. "Object-Oriented Programming versus Abstract Data Types" (PDF).
  4. "Synthesizing Object-Oriented and Functional Design to Promote Re-Use".
  5. "Modular object-oriented programming with units and mixins".
  6. Flatt, Matthew; Krishnamurthi, Shriram; Felleisen, Matthias (1998). "Classes and Mixins". Proceedings of the 25th ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '98. pp. 171–183. doi:10.1145/268946.268961. ISBN 978-0897919791.
  7. Kühne, Thomas (1999). A Functional Pattern System for Object-Oriented Design. Darmstadt: Verlag Dr. Kovac. ISBN 978-3-86064-770-7.
  8. Smaragdakis, Yannis; Don Batory (1998). "Implementing Reusable Object-Oriented Components". Lecture Notes in Computer Science. 1445.
  9. "Extensible Algebraic Datatypes with Defaults". 2001: 241–252. CiteSeerX 10.1.1.28.6778. Cite journal requires |journal= (help)
  10. "Independently extensible solutions to the expression problem". 2005. CiteSeerX 10.1.1.107.4449. Cite journal requires |journal= (help)
  11. Chambers, Craig; Leavens, Gary T. (November 1995). "Type Checking and Modules for Multi-Methods". ACM Trans. Program. Lang. Syst. (17): 805–843.
  12. Clifton, Curtis; Leavens, Gary T.; Chambers, Craig; Millstein, Todd (2000). "MultiJava: Modular Open Classes and Symmetric Multiple Dispatch for Java" (PDF). Oopsla '00.
  13. Wouter Swierstra (2008). "Data Types à La Carte". Journal of Functional Programming. 18 (4). Cambridge University Press. pp. 423–436. doi:10.1017/S0956796808006758. ISSN 0956-7968.
  14. Wehr, Stefan; Thiemann, Peter (July 2011). "JavaGI: The Interaction of Type Classes with Interfaces and Inheritance". ACM Trans. Program. Lang. Syst. (33).
  15. Carette, Jacques; Kiselyov, Oleg; Chung-chieh, Shan (2009). "Finally Tagless, Partially Evaluated: Tagless Staged Interpreters for Simpler Typed Languages" (PDF). J. Funct. Program.
  16. Oliveira, Bruno C. d. S.; Cook, William R. (2012). "Extensibility for the Masses: Practical Extensibility with Object Algebras" (PDF). Ecoop '12.
  17. Garrigue, Jacques (2000). "Code Reuse Through Polymorphic Variants". CiteSeerX 10.1.1.128.7169. Cite journal requires |journal= (help)
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.