Skip to content
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

Implement partial support for promoting scoped type variables #573

Merged
merged 2 commits into from
Aug 26, 2023

Conversation

RyanGlScott
Copy link
Collaborator

@RyanGlScott RyanGlScott commented Jul 25, 2023

singletons-th can now promote a good number of uses of scoped type variables, save for the two exceptions now listed in the singletons README. This is accomplished by a combination of:

  1. Bringing scoped type variables into scope on the left-hand sides of promoted type family equations through invisible @ arguments when a function uses an outermost forall in its type signature (and does not close over any local variables).

  2. Bringing scoped type variables into scope on the left-hand sides of promoted type family equations through explicit kind annotations on each of the type family's arguments when a function has an outermost forall in its type signature.

  3. Closing over scoped type variables when lambda-lifting to ensure that the type variables are accessible when scoping over local definitions.

See the new Note [Scoped type variables] in Data.Singletons.TH.Promote.Monad for more about how this is implemented.

Fixes #433.

@RyanGlScott RyanGlScott requested a review from goldfirere July 25, 2023 23:06
Whenever we promote a function that closes over one or more local variables, we
now use `noExactTyVars` to ensure that none of the local variables reuse unique
`Name`s.  This isn't strictly necessary right now, but this will be required in
a future patch that will panic (triggering
https://gitlab.haskell.org/ghc/ghc/-/issues/11812) unless `noExactTyVars` is
used.

While I was in the neighborhood, I took the opportunity to write `Note
[Pitfalls of NameU/NameL]` with more context about why `noExactTyVars` is
necessary in the first place, and I added references to the Note in all the
places where we use it.
`singletons-th` can now promote a good number of uses of scoped type variables,
save for the two exceptions now listed in the `singletons` `README`. This is
accomplished by a combination of:

1. Bringing scoped type variables into scope on the left-hand sides of promoted
   type family equations through invisible `@` arguments when a function uses
   an outermost `forall` in its type signature (and does not close over any
   local variables).

2. Bringing scoped type variables into scope on the left-hand sides of promoted
   type family equations through explicit kind annotations on each of the type
   family's arguments when a function has an outermost `forall` in its type
   signature.

3. Closing over scoped type variables when lambda-lifting to ensure that the
   type variables are accessible when scoping over local definitions.

See the new `Note [Scoped type variables]` in
`Data.Singletons.TH.Promote.Monad` for more about how this is implemented.

Fixes #433.
@RyanGlScott RyanGlScott marked this pull request as ready for review August 26, 2023 12:49
@RyanGlScott RyanGlScott merged commit a61bb77 into master Aug 26, 2023
@RyanGlScott RyanGlScott deleted the T433-take-two branch August 26, 2023 12:57
RyanGlScott added a commit that referenced this pull request May 19, 2024
Although #573 added some support for promoting/singling uses of scoped type
variables, it did not properly support scoped type variables in class or
instance declarations due to an oversight. This patch aims to correct that
oversight.

The key tricks to making this work are:

* When promoting class or instance methods, we explicitly quantify the type
  variables in the "helper" type family so that we can bind them on the
  left-hand sides of the promoted type family equations.
* In addition, we take care to only bring the _scoped_ type variables into
  scope over the right-hand sides of the promoted type family equations.

See the new `Note [Scoped type variables and class methods]` in
`Data.Singletons.TH.Promote.Monad` for the full details.

Fixes #581.
RyanGlScott added a commit that referenced this pull request May 19, 2024
Although #573 added some support for promoting/singling uses of scoped type
variables, it did not properly support scoped type variables in class or
instance declarations due to an oversight. This patch aims to correct that
oversight.

The key tricks to making this work are:

* When promoting class or instance methods, we explicitly quantify the type
  variables in the "helper" type family so that we can bind them on the
  left-hand sides of the promoted type family equations.
* In addition, we take care to only bring the _scoped_ type variables into
  scope over the right-hand sides of the promoted type family equations.

See the new `Note [Scoped type variables and class methods]` in
`Data.Singletons.TH.Promote.Monad` for the full details.

Fixes #581.
RyanGlScott added a commit that referenced this pull request May 19, 2024
Although #573 added some support for promoting/singling uses of scoped type
variables, it did not properly support scoped type variables in class or
instance declarations due to an oversight. This patch aims to correct that
oversight.

The key tricks to making this work are:

* When promoting class or instance methods, we explicitly quantify the type
  variables in the "helper" type family so that we can bind them on the
  left-hand sides of the promoted type family equations.
* In addition, we take care to only bring the _scoped_ type variables into
  scope over the right-hand sides of the promoted type family equations.

See the new `Note [Scoped type variables and class methods]` in
`Data.Singletons.TH.Promote.Monad` for the full details.

Fixes #581.
RyanGlScott added a commit that referenced this pull request Jun 6, 2024
Although #573 added some support for promoting/singling uses of scoped type
variables, it did not properly support scoped type variables in class or
instance declarations due to an oversight. This patch aims to correct that
oversight.

The key tricks to making this work are:

* When promoting class or instance methods, we explicitly quantify the type
  variables in the "helper" type family so that we can bind them on the
  left-hand sides of the promoted type family equations.
* In addition, we take care to only bring the _scoped_ type variables into
  scope over the right-hand sides of the promoted type family equations.

See the new `Note [Scoped type variables and class methods]` in
`Data.Singletons.TH.Promote.Monad` for the full details.

Fixes #581.
RyanGlScott added a commit that referenced this pull request Jun 6, 2024
Although #573 added some support for promoting/singling uses of scoped type
variables, it did not properly support scoped type variables in class or
instance declarations due to an oversight. This patch aims to correct that
oversight.

The key tricks to making this work are:

* When promoting class or instance methods, we explicitly quantify the type
  variables in the "helper" type family so that we can bind them on the
  left-hand sides of the promoted type family equations.
* In addition, we take care to only bring the _scoped_ type variables into
  scope over the right-hand sides of the promoted type family equations.

See the new `Note [Scoped type variables and class methods]` in
`Data.Singletons.TH.Promote.Monad` for the full details.

Fixes #581.
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.

Promoting expression signatures can fail on GHC 8.10+
1 participant