Content-Length: 313759 | pFad | http://github.com/purescript/purescript/pull/4216

21 Scope type vars when type checking typed values by rhendric · Pull Request #4216 · purescript/purescript · GitHub
Skip to content

Scope type vars when type checking typed values #4216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2022

Conversation

rhendric
Copy link
Member

When the compiler is checking an expression that is annotated with a
type against another expected type, and the annotation introduces a type
variable, the compiler needs to introduce that type variable to the
scope of any types used inside the expression.

One noteworthy case of this pattern is member signatures inside
instances. This fix allows type variables introduced in member
signatures to be used in the member declaration itself.

Description of the change

Fixes #2941.


Checklist:

  • Added a file to CHANGELOG.d for this PR (see CHANGELOG.d/README.md)
  • Added myself to CONTRIBUTORS.md (if this is my first contribution)
  • Linked any existing issues or proposals that this pull request should close
  • Updated or added relevant documentation
  • Added a test for the contribution (if applicable)

When the compiler is checking an expression that is annotated with a
type against another expected type, and the annotation introduces a type
variable, the compiler needs to introduce that type variable to the
scope of any types used inside the expression.

One noteworthy case of this pattern is member signatures inside
instances. This fix allows type variables introduced in member
signatures to be used in the member declaration itself.
@garyb
Copy link
Member

garyb commented Dec 20, 2021

Does this also fix it so you can use a type variable introduced in an instance head, or does it only apply to member annotations? If it fixes the former too it'd be nice to have a test for that!

@rhendric
Copy link
Member Author

If you're talking about forall in instance heads, as in #1120, no, this doesn't implement that. If not... example please? Or issue link?

@garyb
Copy link
Member

garyb commented Dec 20, 2021

Ah, actually looks like the thing I meant already works! I guess I'd just mixed it up in my head with the case you've fixed here. I meant this kind of thing:

data I a = I a  

class Foo a where
  something  Unit  a

instance Monoid a  Foo (I a) where
  something _ = I (mempty  a)

@JordanMartinez
Copy link
Contributor

I restarted CI, thinking it would include #4217. I guess it doesn't still.

Copy link
Contributor

@JordanMartinez JordanMartinez left a comment

Choose a reason for hiding this comment

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

Explaining this code to myself, all this PR is doing is running the same tvToExpr <$> check val ty1' code, except that the withScopedTypeVars now wraps it. And all that function does is amend the environment's types to include additional type variables that one passes into that function. Once the computation finishes, it sets the environment's types back to what it previously was. If any of the new type variables shadow current ones, a warning will be emitted.

These type variables come from kindOfWithScopedVars, which is used in multiple places throughout the TypeChecker/Types.hs file. So, we're not doing anything weird by using it here AFAICT. Lastly, the unsafeCheckCurrentModule just gets the current module's name (should this function be renamed to better indicate that in some future PR?). It's unsafe because if there isn't a current module, there's nothing to return. We use it in other places throughout this file, and our usage here is safe AFAICT.

@rhendric
Copy link
Member Author

Also worth noting: this PR effectively reverts part of a change made in #2643. But that PR did not change the analogous logic in infer, which survives to this day (and is how I got the idea for this fix in check), so I think it was a mistake. But that, and the evidence of passing tests, is about as far as I can go to justify this.

@rhendric rhendric merged commit 7bf9e61 into purescript:master Jan 30, 2022
@rhendric rhendric deleted the rhendric/fix-2941 branch January 30, 2022 09:06
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.

Instance signatures don't bring type variables into scope
3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/purescript/purescript/pull/4216

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy