<p>The <a href="https://en.wikipedia.org/wiki/Expression_problem" rel="nofollow noreferrer">Expression Problem</a> refers to add new representations and new behaviors to the original data types without modifying the source code.</p>
<p>This restriction is very common, the interface may be a precompiled library, or from an upstream module, or it is inconvenient to modify due to license and other reasons.</p>
<p>The functional programming and object-oriented programming cannot directly solve this problem.</p>
<p>Functional languages is easy to add new behaviors but difficult to add new representations.</p>
<p>Object-oriented languages is easy to add new representations but difficult to add new behaviors.</p>
<hr />
<p>I also encountered this troublesome problem in language design.</p>
<p>Some complex design models are raised to regulate this problem, and requires quite a bit of code.</p>
<p>But I want to know if anyone has tried to solve this problem from the language level?</p>
<p>For example, some additional magic annotation to greatly reduce the amount of code required for these extension.</p>

<syntax><language-design>
How do you solve Expression Problem in your language design?
<p>I think of it like a table where the rows are the data types, and the columns are the behaviours. (So e.g. rows are classes, columns are interfaces; or rows are algebraic data types, columns are functions which accept those types.)</p>
<p>An object-oriented language is organised by the &quot;rows&quot;:</p>
<ul>
<li>For every behaviour associated with a class, the implementation of that behaviour is written within the class.</li>
<li>Adding a new &quot;row&quot; is easy: you just write a new class and implement whatever interfaces it needs.</li>
<li>Adding a new &quot;column&quot; is hard: if you add a method to an interface, or create a new interface, you have to edit every class all over the project.</li>
</ul>
<p>A functional language is organised by the &quot;columns&quot;:</p>
<ul>
<li>For every data type that a behaviour is implemented for, the implementation of that behaviour is written where the function is declared.</li>
<li>Adding a new &quot;column&quot; is easy: you just write a new function and make it handle whatever data types it needs to.</li>
<li>Adding a new &quot;row&quot; is hard: e.g. if you add a new variant to some enum type, you have to edit every function that consumes that enum type.</li>
</ul>
<p>Of course this is just the broad idea, both OO and FP languages have design patterns which allow organising in the opposite way. For example, in an OO language the <a href="https://en.wikipedia.org/wiki/Visitor_pattern" rel="nofollow noreferrer">visitor pattern</a> groups by the implementations of one behaviour for many classes; in an FP language, first-class functions can be included as fields in a data type so that these functions are implemented together where the type is constructed. It's just that these design patterns are only needed when you want to organise code in a way that isn't directly encouraged by the paradigm you're writing in.</p>
<hr />
<p>So this is a fundamental problem in language design, and I don't think it can really be &quot;solved&quot;, there are just different options with different trade-offs. Ultimately, if one module in a program can add a new &quot;row&quot; and another can add a new &quot;column&quot; to the big &quot;table&quot;, then <em>someone</em> needs to decide who is responsible for filling in the &quot;cell&quot; where the new row and the new column overlap.</p>
<p>That is, when there is a new type and a new function, and the new type has to work with the new function, then the behaviour has to be defined <em>somewhere</em>.</p>
<hr />
<p>All of that said, there are some approaches in existing languages which do address the problem, even if they don't outright solve it. I'll mention traits in Rust: in the analogy, types (structs and enums) are the &quot;rows&quot;, and traits are the &quot;columns&quot;.</p>
<ul>
<li>A trait can be implemented on multiple types, and a type can implement multiple traits. So you can add either a new row or a new column.</li>
<li>An implementation for a trait can be written <em>either</em> where the type is declared, or where the trait is declared ─ or elsewhere, within reasonable limits. You aren't allowed to implement third-party traits for third-party types, because this would cause the code to break if one of those third parties ever decided to implement the same trait for the same type themselves.</li>
<li>There's no free lunch: the &quot;new row, new column&quot; problem still requires an implementation to be written somewhere. But at least it never requires changing third-party code, unless <em>both</em> the new &quot;row&quot; and the new &quot;column&quot; are in third-party libraries.</li>
</ul>
<p>On the other hand, this allows code to be organised <em>neither</em> by &quot;row&quot; nor by &quot;column&quot;, at least within a project you can scatter trait implementations across many files, which can lead to a messy project structure. This isn't exactly a language design flaw, because it's the user's responsibility to organise their code in a logical way; and it's also attenuated by modern IDEs which can efficiently locate all implementations of a trait. Still, some people might consider it a drawback that the language allows users to make more of a mess if they don't organise their code well.</p>

CC BY-SA 4.0
2023-06-06T15:20:15.623Z
2023-06-06T15:26:53.967Z
2023-06-06T15:26:53.967Z
kaya3
CC BY-SA 4.0
2023-06-06T20:07:25.553Z
"You aren't allowed to implement third-party traits for third-party types, because this would cause the code to break if one of those third parties ever decided to implement the same trait for the same type themselves." -- This is allowed in Swift, but it's discouraged because the linker decides which implementation "wins" and that can cause problems. See https://forums.swift.org/t/retroactive-conformances-vs-swift-in-the-os/14393
Bbrk24
CC BY-SA 4.0
2023-06-07T10:45:17.973Z
"So this is a fundamental problem in language design, and I don't think it can really be "solved"" — you already mentioned Visitor pattern that basically solves the problem. Another approach if Final Tagless. Thus the obvious answer is to provide better syntax to define visitors. And that's just the tip of the iceberg, you can learn much more from Oliveira's papers linked from my answer.
Alex Chichigin
CC BY-SA 4.0
2023-06-07T13:33:56.473Z
@AlexChichigin The visitor pattern doesn't "solve" the expression problem, it merely flips it along a diagonal axis. Adding a new behaviour (i.e. a new column) becomes easy, you just write one more visitor ─ but then adding a new representation (i.e. a new row) becomes difficult because you need to change every visitor. The visitor pattern in OOP (or function-valued fields in FP) only address the problem by letting the programmer choose which kind of extension is easy and which is hard; they don't make both easy.
kaya3
CC BY-SA 4.0
2023-06-07T14:46:55.687Z
@kaya3 well, yeah, but visitor does solve the issues you mention with a bit of care. :) First, to solve the Expression Problem you can't change the old code, but you obviously allowed to add new one. So you can't go back and stick a new field into "data objects" you already have, you have to subclass them. Now your old visitors can't handle new subclasses but you're allowed to subclass them to add new behaviour. Now if you use your new visitors everywhere instead of the old ones, you solved the problem. And if you had "catch all" branch in all the visitors that "forwards" you can use them too.
Alex Chichigin
CC BY-SA 4.0
2023-06-07T14:57:03.577Z
@AlexChichigin It doesn't solve them, as I explained. Adding a new field to an existing type (i.e. changing a representation, not adding a new representation) is orthogonal to the expression problem.
kaya3
CC BY-SA 4.0
2023-06-07T15:46:04.310Z
@kaya3 so you're basically defining your own version of the Expression Problem and your definition of solution. And claiming Visitor doesn't fit it. That's curious. :)
Alex Chichigin
CC BY-SA 4.0
2023-06-07T16:00:18.933Z
@AlexChichigin No.
kaya3
<blockquote>
<p>But I want to know if anyone has tried to solve this problem from the language level?</p>
</blockquote>
<p>Sure! <a href="https://i.cs.hku.hk/%7Ebruno/" rel="nofollow noreferrer">Bruno C. d. S. Oliveira</a> is working on this for ages! He's best-known for work on <a href="https://i.cs.hku.hk/%7Ebruno/papers/oopsla2015.pdf" rel="nofollow noreferrer">Object Algebras</a> which are akin to <a href="https://oleksandrmanzyuk.wordpress.com/2014/06/18/from-object-algebras-to-finally-tagless-interpreters-2/" rel="nofollow noreferrer">Final Tagless approach</a>.</p>
<p>Their more recent <a href="https://i.cs.hku.hk/%7Ebruno/papers/toplas2021.pdf" rel="nofollow noreferrer">Compositional Programming</a> paper greatly expands upon Object Algebras addressing many shortcomings. The authors propose both (Scala-like) syntax and semantics for a compositional language. An implementation of the CP language in PureScript is available on the <a href="https://github.com/yzyzsun/CP-next" rel="nofollow noreferrer">Github</a>.</p>
<hr />
<p><strong>Update</strong></p>
<p>I forgot to mention pretty obvious and well-known: Multiple Dispatch aka Multi-methods in languages like Common Lisp, Clojure and Julia solve Expression Problem for a long time. <a href="https://en.wikipedia.org/wiki/Multiple_dispatch" rel="nofollow noreferrer">Wikipedia page</a> has several examples and many further references.</p>
<p>Multi-methods are not very popular and seldom used (particularly in Clojure) due to poor performance. But Julia made them run very fast thanks to run-time type specialization, devirtualization and aggressive inlining. So they are the main abstraction mechanism, and this way Julia indeed successfully avoids the Expression Problem and achieves high level of library interoperability most of the time.</p>
<p>Unfortunately Multi-methods rely on <em>run-time</em> dispatch and thus do not provide type-safety and do not necessarily adhere to the Liskov Substitution Principle. That's why I'm more interested in statically typed approaches like Compositional Programming mentioned above.</p>

CC BY-SA 4.0
2023-06-06T12:09:38.087Z
2023-06-08T13:12:40.683Z
2023-06-08T13:12:40.683Z
Alex Chichigin
CC BY-SA 4.0
2023-06-07T07:45:08.577Z
Several of these links don't work for me. Are they perhaps internal to your institution?
Is one of them https://www.cs.utexas.edu/~wcook/Drafts/2012/ecoop2012.pdf
Bruce Adams
CC BY-SA 4.0
2023-06-07T10:39:39.003Z
@BruceAdams unfortunately I have no idea what might be the problem. I'm not in an institution, but still can freely access all the links. At the very least you can try to search for alternative links on Google Scholar, I think...
Alex Chichigin
# How do you solve Expression Problem in your language design?
Posted by: username_0 on 2023-06-06
The [Expression Problem](https://en.wikipedia.org/wiki/Expression_problem) refers to add new representations and new behaviors to the original data types without modifying the source code.

This restriction is very common, the interface may be a precompiled library, or from an upstream module, or it is inconvenient to modify due to license and other reasons.

The functional programming and object-oriented programming cannot directly solve this problem.

Functional languages is easy to add new behaviors but difficult to add new representations.

Object-oriented languages is easy to add new representations but difficult to add new behaviors.

---

I also encountered this troublesome problem in language design.

Some complex design models are raised to regulate this problem, and requires quite a bit of code.

But I want to know if anyone has tried to solve this problem from the language level?

For example, some additional magic annotation to greatly reduce the amount of code required for these extension.
**Tags:** syntax language-design
---
## 2 answers
### Answer by username_3 on 2023-06-06. Score: 7 | Accepted answer
> But I want to know if anyone has tried to solve this problem from the language level?

Sure! [Bruno C. d. S. Oliveira](https://i.cs.hku.hk/%7Ebruno/) is working on this for ages! He's best-known for work on [Object Algebras](https://i.cs.hku.hk/%7Ebruno/papers/oopsla2015.pdf) which are akin to [Final Tagless approach](https://oleksandrmanzyuk.wordpress.com/2014/06/18/from-object-algebras-to-finally-tagless-interpreters-2/).

Their more recent [Compositional Programming](https://i.cs.hku.hk/%7Ebruno/papers/toplas2021.pdf) paper greatly expands upon Object Algebras addressing many shortcomings. The authors propose both (Scala-like) syntax and semantics for a compositional language. An implementation of the CP language in PureScript is available on the [Github](https://github.com/yzyzsun/CP-next).

---

**Update**

I forgot to mention pretty obvious and well-known: Multiple Dispatch aka Multi-methods in languages like Common Lisp, Clojure and Julia solve Expression Problem for a long time. [Wikipedia page](https://en.wikipedia.org/wiki/Multiple_dispatch) has several examples and many further references.

Multi-methods are not very popular and seldom used (particularly in Clojure) due to poor performance. But Julia made them run very fast thanks to run-time type specialization, devirtualization and aggressive inlining. So they are the main abstraction mechanism, and this way Julia indeed successfully avoids the Expression Problem and achieves high level of library interoperability most of the time.

Unfortunately Multi-methods rely on *run-time* dispatch and thus do not provide type-safety and do not necessarily adhere to the Liskov Substitution Principle. That's why I'm more interested in statically typed approaches like Compositional Programming mentioned above.
> **Comments:**
> **username_4** (2023-06-07): Several of these links don't work for me. Are they perhaps internal to your institution?
Is one of them https://www.cs.utexas.edu/~wcook/Drafts/2012/ecoop2012.pdf  
> **username_3** (2023-06-07): @BruceAdams unfortunately I have no idea what might be the problem. I'm not in an institution, but still can freely access all the links. At the very least you can try to search for alternative links on Google Scholar, I think...  
---
### Answer by username_1 on 2023-06-06. Score: 4
I think of it like a table where the rows are the data types, and the columns are the behaviours. (So e.g. rows are classes, columns are interfaces; or rows are algebraic data types, columns are functions which accept those types.)

An object-oriented language is organised by the "rows":

* For every behaviour associated with a class, the implementation of that behaviour is written within the class.
* Adding a new "row" is easy: you just write a new class and implement whatever interfaces it needs.
* Adding a new "column" is hard: if you add a method to an interface, or create a new interface, you have to edit every class all over the project.

A functional language is organised by the "columns":

* For every data type that a behaviour is implemented for, the implementation of that behaviour is written where the function is declared.
* Adding a new "column" is easy: you just write a new function and make it handle whatever data types it needs to.
* Adding a new "row" is hard: e.g. if you add a new variant to some enum type, you have to edit every function that consumes that enum type.

Of course this is just the broad idea, both OO and FP languages have design patterns which allow organising in the opposite way. For example, in an OO language the [visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern) groups by the implementations of one behaviour for many classes; in an FP language, first-class functions can be included as fields in a data type so that these functions are implemented together where the type is constructed. It's just that these design patterns are only needed when you want to organise code in a way that isn't directly encouraged by the paradigm you're writing in.

---

So this is a fundamental problem in language design, and I don't think it can really be "solved", there are just different options with different trade-offs. Ultimately, if one module in a program can add a new "row" and another can add a new "column" to the big "table", then *someone* needs to decide who is responsible for filling in the "cell" where the new row and the new column overlap.

That is, when there is a new type and a new function, and the new type has to work with the new function, then the behaviour has to be defined *somewhere*.

---

All of that said, there are some approaches in existing languages which do address the problem, even if they don't outright solve it. I'll mention traits in Rust: in the analogy, types (structs and enums) are the "rows", and traits are the "columns".

* A trait can be implemented on multiple types, and a type can implement multiple traits. So you can add either a new row or a new column.
* An implementation for a trait can be written *either* where the type is declared, or where the trait is declared ─ or elsewhere, within reasonable limits. You aren't allowed to implement third-party traits for third-party types, because this would cause the code to break if one of those third parties ever decided to implement the same trait for the same type themselves.
* There's no free lunch: the "new row, new column" problem still requires an implementation to be written somewhere. But at least it never requires changing third-party code, unless *both* the new "row" and the new "column" are in third-party libraries.

On the other hand, this allows code to be organised *neither* by "row" nor by "column", at least within a project you can scatter trait implementations across many files, which can lead to a messy project structure. This isn't exactly a language design flaw, because it's the user's responsibility to organise their code in a logical way; and it's also attenuated by modern IDEs which can efficiently locate all implementations of a trait. Still, some people might consider it a drawback that the language allows users to make more of a mess if they don't organise their code well.
> **Comments:**
> **username_2** (2023-06-06): "You aren't allowed to implement third-party traits for third-party types, because this would cause the code to break if one of those third parties ever decided to implement the same trait for the same type themselves." -- This is allowed in Swift, but it's discouraged because the linker decides which implementation "wins" and that can cause problems. See https://forums.swift.org/t/retroactive-conformances-vs-swift-in-the-os/14393  
> **username_3** (2023-06-07): "So this is a fundamental problem in language design, and I don't think it can really be "solved"" — you already mentioned Visitor pattern that basically solves the problem. Another approach if Final Tagless. Thus the obvious answer is to provide better syntax to define visitors. And that's just the tip of the iceberg, you can learn much more from Oliveira's papers linked from my answer.  
> **username_1** (2023-06-07): @AlexChichigin The visitor pattern doesn't "solve" the expression problem, it merely flips it along a diagonal axis. Adding a new behaviour (i.e. a new column) becomes easy, you just write one more visitor ─ but then adding a new representation (i.e. a new row) becomes difficult because you need to change every visitor. The visitor pattern in OOP (or function-valued fields in FP) only address the problem by letting the programmer choose which kind of extension is easy and which is hard; they don't make both easy.  
> **username_3** (2023-06-07): @kaya3 well, yeah, but visitor does solve the issues you mention with a bit of care. :) First, to solve the Expression Problem you can't change the old code, but you obviously allowed to add new one. So you can't go back and stick a new field into "data objects" you already have, you have to subclass them. Now your old visitors can't handle new subclasses but you're allowed to subclass them to add new behaviour. Now if you use your new visitors everywhere instead of the old ones, you solved the problem. And if you had "catch all" branch in all the visitors that "forwards" you can use them too.  
> **username_1** (2023-06-07): @AlexChichigin It doesn't solve them, as I explained. Adding a new field to an existing type (i.e. changing a representation, not adding a new representation) is orthogonal to the expression problem.  
> **username_3** (2023-06-07): @kaya3 so you're basically defining your own version of the Expression Problem and your definition of solution. And claiming Visitor doesn't fit it. That's curious. :)  
> **username_1** (2023-06-07): @AlexChichigin No.  
---
