Polymorphic operators #55
Labels
🔧 compiler
Issue concerns the compiler
💬 discussion
Further discussion is needed
✨ enhancement
New feature or request
Right now, binary and unary operators are specialized depending on their type :
+$
for money addition,-@
for date substraction, etc. This is cumbersome and ideally we would want to write+
for adding money, dates, integers, decimals, etc.Why is it like this?
The need for specialized operators stems form Catala's type system, and more precisely from the default calculs typing algorithm. It uses a classical Hindley-Milner inference and the W algorithm. In classical Hindley-Milner inference, operators either operate on a fixed type, or are completely polymorphic. This is the typing procedure for Catala operators right now :
catala/src/catala/default_calculus/typing.ml
Lines 109 to 150 in 06803e4
Why can't we make all operators completely polymorphic ? Because it does not make any sense to
+
two enumerations together, or*
two dates.Hindley-Milner inference with constraints
What we really want is a type system where we say :
+
operates only on a fixed list of types. This corresponds to Hindley-Milner inference with constraints, a notoriously hard problem. Some libraries exist to perform the typing, like https://gitlab.inria.fr/fpottier/inferno, but overall it is much more complicated from the classical inference I'm doing right now.Because I don't want to spend too much time on Catala's type system, I prefer we stick to classical inference and hence we're stuck with the default calculus operators as they are.
A compromise
But this does not mean we can't have what we want ! Indeed, we can keep the differentiated default calculus operators but have undifferentiated surface syntax operator. We can write
|01/01/2020| - |01/01/2019|
and have the compiler figure out that-
is actually-@
during desugaring.This system is nice because it does not complicate the default calculus. Instead, we rely on a conservative desugaring pass that would sit somewhere in
Desugared
orScopelang
and that would specialize the operators with the information provided by a conservative, high-level additional type checking pass done inDesugared
orScopelang
.If this pass does not manage to determine the type of the operator, we send an error message back to the user saying that desambiguation is needed and the user actually has to write
-@
.What are your thoughts on this?
The text was updated successfully, but these errors were encountered: