Closed FordArthur closed 1 week ago
Since the AST for function definitions contain a pointer to the declaration of the function (which is null if is unspecified, and thus we need to infer it) and a pointer to all of the different function "implementations", we need to traverse all of the "implementations" and type check it against the declared type or/and it's other implementations
What it's not so clear to me is how I'm going to infer the type of a single function implementation. Specifically it's return type
Okay so,
Types will be represented with a vector of 64 bit values, in which each value either represents:
With this type representation, types are compared like:
a
could be b
, then we check if b
has at least all of a
's constraints)How to iterate over declarations and implementations is trivial. The tricky part is how to infer the type of implementations. (I think) The following example covers every non-trivial case inferring implementations involve:
Suppose f a b c = a (b + c)
. We must find the types of a
, b
, c
, and a (b + c)
. Call this last type value d
.
We begin by solving for the expression's first argument, that is (b + c)
. +
has type Num a => a, a -> a
. We perform a substitution: b
has type b
, so after substituting it +
is of type Num b => b, b -> b
. c
has type c
. We cannot perform another substitution since all of the values in +
have already been substituted. Therefore b
is of type Num b => b
, and so is c
. Now, go back to a (b + c)
. +
produces a Num b => b
. Therefore a
must take said type. Furthermore, a
must produce a value of type d
(Recall d
was the type of the whole expression). Therefore a
is of type Num b => b -> d
. Solving for f
's type is trivial now and may be even be exact in the implementation of this algorithm
QED
This will involve rewriting some parts of the parser, so I'll probably also go ahead and try to fix making so many micro-allocations
Tfw it was just the Hindley–Milner typesystem
Since this is just an experimental project, it will be better if instead of copying some standard algorithm I try to develop my own
I will post the algorithm I have in mind here as I develop it