Commit Graph

5 Commits

Author SHA1 Message Date
John Wiegley 94e0be3882
Reformat all sources with Brittany, to restore consistency 2019-03-17 14:47:38 -07:00
John Wiegley ba5b23a6e3
Bump minimum supported version to GHC 8.4.4 2018-11-19 09:46:02 -08:00
John Wiegley ee8ba02dfd Much more work on type inference; report multiple possible types
This is needed because of a function like this:

    x: y: x + y

This is polymorphic, but over exactly four possibilities:

    int   -> int
    int   -> float
    float -> int
    float -> float

The "type" is really the combination of the four, since we don't yet have a
mechanism like type classes, which could have rendered this as a single type:

    (Num a, Num b) => (x :: a): (y :: b): x + y

Going this route will require that we manage an implicit type classes
hierarchy, and perform unification on constraints as well as types. In the
interim, I just lifted the unification algorithm into the LogicT monad, and
use back-tracking search to find all the possible types an expression could
be.

The main problem with using LogicT, however, is that there are many types
it *couldn't* be, and in the case of a unification failure, it not yet clear
what the type should have been.  For example:

    "foo" + 2

Should the string have been a float or an integer or a path? Or should the
integer have been a string? So for now we report all the possibilities, since
it's not obvious which part of the expression is in error:

    hnix: Type error: TypeInferenceErrors
      [ UnificationFail (TCon "integer") (TCon "string")
      , UnificationFail (TCon "string") (TCon "path")
      , UnificationFail (TCon "string") (TCon "float")
      , UnificationFail (TCon "string") (TCon "integer")
      ]

This is a case where enumerating types rather than trying to make them compact
using type classes might actually be an improvement, since the errors here
would have been only slightly less numerous:

    string  != Num a => a
    string  != path
    integer != string

Clearly a better reporting mechanism is needed to clarify these problems. I
can imagine that in an IDE, there would be a squiggly under both sides of the
expression, each suggesting the type that was expected for that argument under
the assumption that the other argument (the one not be inspected) was the
correct one.
2018-05-01 20:49:10 -04:00
John Wiegley caf0a4a6a2 Add support for building with GHC 8.4.1, also on Travis 2018-04-23 16:39:37 -07:00
John Wiegley 709cc5247e Add code for doing Hindley-Milner type inference, and a repl
This code was written by Stephen Diehl and Kwang Yul Seo, which they released
under an MIT license as the project poly_constraints:

    https://github.com/kseo/poly_constraints

The code is added to hnix right now as mainly a placeholder, and will need to
be modified before it can be used.
2018-04-12 10:53:50 -07:00