Skip to content

2374: Add error message for ambiguous type variables in inferred contexts - #2410

Merged
paf31 merged 4 commits into
purescript:masterfrom
bbqbaron:master
Nov 7, 2016
Merged

2374: Add error message for ambiguous type variables in inferred contexts#2410
paf31 merged 4 commits into
purescript:masterfrom
bbqbaron:master

Conversation

@bbqbaron

Copy link
Copy Markdown
Contributor

This adds ambiguous inferred type errors for 1.0. How does the error message look? It's my first time mucking about in the compiler; did I infer correctly when to throw the error?

let constraintTypeVars = nub . foldMap (unknownsInType . fst) . filter ((`notElem` solved) . snd) $ zip (constraintArgs con) [0..]
when (any (`notElem` unsolvedTypeVars) constraintTypeVars) $ do
throwError . onErrorMessages (replaceTypes currentSubst) . errorMessage $ NoInstanceFound con
throwError . onErrorMessages (replaceTypes currentSubst) . errorMessage $ AmbiguousTypeVariables ident generalized con

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops; this code doesn't confirm that there are multiple instances of the constraint typeclass in existence. It seems like the strictly most correct response would be:

No instances -> NoInstanceFound
One instance -> Try to solve the type signature with that instance's type argument
Two instances -> AmbiguousTypeVariables

I'm not sure I have the expertise for that just yet, though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be handled elsewhere, so you don't need to worry about it here. An ambiguous type means something else, where a type variable appears in a constraint, but not in the body of the type, so it can't be inferred.

@paf31

paf31 commented Oct 30, 2016

Copy link
Copy Markdown
Contributor

What you have looks correct to me. Thanks!

Sent from my iPhone

On Oct 30, 2016, at 1:16 PM, bbqbaron notifications@github.com wrote:

@bbqbaron commented on this pull request.

In src/Language/PureScript/TypeChecker/Types.hs:

@@ -111,7 +111,7 @@ typesOf bindingGroupType moduleName vals = withFreshSubstitution $ do
let solved = foldMap (S.fromList . fdDetermined) typeClassDependencies
let constraintTypeVars = nub . foldMap (unknownsInType . fst) . filter ((notElem solved) . snd) $ zip (constraintArgs con) [0..]
when (any (notElem unsolvedTypeVars) constraintTypeVars) $ do

  •        throwError . onErrorMessages (replaceTypes currentSubst) . errorMessage $ NoInstanceFound con
    
  •        throwError . onErrorMessages (replaceTypes currentSubst) . errorMessage $ AmbiguousTypeVariables ident generalized con
    
    Whoops; this code doesn't confirm that there are multiple instances of the constraint typeclass in existence. It seems like the strictly most correct response would be:

No instances -> NoInstanceFound
One instance -> Try to solve the type signature with that instance's type argument
Two instances -> AmbiguousTypeVariables

I'm not sure I have the expertise for that just yet, though.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Comment thread src/Language/PureScript/Errors.hs Outdated
go TUnknown{} = True
go _ = False
renderSimpleErrorMessage (AmbiguousTypeVariables ident t (Constraint nm ts _)) =
paras [ line $ markCode (showIdent ident) ++ " doesn't have a type declaration, so it was inferred to be of type: "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would omit this line, since it will just duplicate the information from the value declaration level (... in declaration x ...) anyway. In fact, I'd remove the Ident from the SimpleErrorMessage constructor.

@paf31

paf31 commented Oct 31, 2016

Copy link
Copy Markdown
Contributor

Looks great so far, thanks! Please see my one comment, and update CONTRIBUTORS.md.

… since it's redundant with the enclosing compiler error's ident
Comment thread src/Language/PureScript/Errors.hs Outdated
renderSimpleErrorMessage (AmbiguousTypeVariables t (Constraint nm ts _)) =
paras [ line "Inferred type"
, markCodeBox $ typeAtomAsBox t
, line " has constraints, but we don't know what type values they require. Please add an explicit type declaration for: "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be picky, but can this be just "The inferred type ... has type variables which are not mentioned in the body of the type. Consider adding a type annotation"? Thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem; picky is good. Incoming.

Comment thread src/Language/PureScript/Errors.hs Outdated
go TUnknown{} = True
go _ = False
renderSimpleErrorMessage (AmbiguousTypeVariables t (Constraint nm ts _)) =
paras [ line $ "The inferred type " ++ (markCode (prettyPrintTypeAtom t)) ++ " has type variables which are not mentioned in the body of the type. Consider adding a type annotation."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one more 😄 :

Please put the type on its own line, indent it, and use prettyPrintType not prettyPrintTypeAtom. This will make it more consistent with other errors which display full types. The problem right now is that you risk it spilling over multiple lines.

Thanks!

@paf31
paf31 merged commit 5ed9384 into purescript:master Nov 7, 2016
go _ = False
renderSimpleErrorMessage (AmbiguousTypeVariables t _) =
paras [ line "The inferred type"
, indent $ line $ markCode $ prettyPrintType t

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized this should use typeAsBox, since this way, the indentation gets messed up if the type takes up multiple lines.

I can put together a PR for this later, unless you want to take a look @bbqbaron ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to; I'll set a reminder for tomorrow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants