Haskell newbie here.
I wrote an evaluator for a minimal assembly-like language.
Now, I want to extend that language to support some syntactic sugar which, I will then compile back to use only the primitive operators. The ideia is that I do not want to touch the evaluator module again.
In the OO way of doing things, I think, one could extend the original module so to support the syntactic sugar operators, providing here the translation rules.
Other than that, I can only think of rewriting the datatype constructors in both modules so that they would not name-collide, and proceed from there, as if they were complete different things, but that implies some redundancy, for I would have to repeat (just with other names) the operators in common. Again, I think the keyword here is extend.
Is there a functional way of accomplishing this?
Thanks for taking the time to read this question.
This problem was named “the expression problem” by Phil Wadler, in his words:
One solution to have extensible data type is to use type classes.
As an example let’s assume we have a simple language for arithmetics:
e.g.
If we wanted to implement it in an extensible way, we should switch to type classes:
Now let’s extend the language adding subtractions:
e.g.
For more info on this approach, and in general on the expression problem, check Ralf Laemmel’s videos 1 and 2 on Channel 9.
However, as noticed in the comments, this solution changes the semantics. For example lists of expressions are no longer legal:
A more general solution using coproducts of type signatures is presented in the functional pearl “Data types a la carte”. See also Wadler’s comment on the paper.