google-research / dex-lang

Research language for array processing in the Haskell/ML family
BSD 3-Clause "New" or "Revised" License
1.58k stars 106 forks source link

Improve "ambiguous type" errors by tracking the purpose of each inference variable. #1259

Closed dougalm closed 1 year ago

dougalm commented 1 year ago

Previously:

def my_id(x:a) -> a given (a, b) = x
> Type error:Ambiguous type variables: [_.1]

def my_id2(x:a) -> a given (a, b:Type) = x

:p my_id2 1.0
> Type error:Ambiguous type variables: [_.2]

now

def my_id(x:a) -> a given (a, b) = x
> Type error:Couldn't infer type of unannotated binder b

def my_id2(x:a) -> a given (a, b:Type) = x

:p my_id2 1.0
> Type error:Couldn't infer implicit argument b of my_id2
>
> :p my_id2 1.0
>    ^^^^^^^^^^

These two cases -- unannotated binders and implicit args -- account for most of the inference variables we create, so those were the error messages I focused on. Now that the machinery is in place we can improve the rest of them incrementally as they occur.

apaszke commented 1 year ago

This is awesome. It was one of the worst errors we were producing and really hard to debug.