Skip to content

Instantly share code, notes, and snippets.

@mgsloan
Created September 11, 2019 04:11
Show Gist options
  • Save mgsloan/e0db1a83810e05cc160c5e8cdde09031 to your computer and use it in GitHub Desktop.
Save mgsloan/e0db1a83810e05cc160c5e8cdde09031 to your computer and use it in GitHub Desktop.
Iffy desugaring idea for local types

Context: ghc-proposals/ghc-proposals#273 (comment)

I have not reviewed this proposal thoroughly, however I would really like this feature and have considered proposing it in the past. To make this more clearly defined and understandable for users, I'd like to propose a simple rule for what is allowed:

Local types could desugar to top level declarations

My intuition says that if this holds true then the feature becomes much easier to implement than if it does not. As far as I can tell this rule is mostly already followed in the proposal. Specifically:

All instances remain (conceptually) global, but they may involve local types and values

I think the following is sufficient for desugaring:

  • Local type / data / newtype get name mangled and floated to the top level

  • Referenced type variables can be appended to the beginning of the type / data / newtype declarations.

  • Referenced values get passed via reflection-ey tricks.

I did find one mismatch between this rule and the proposal:

Locally defined types will not be given Typeable instances automatically

In order to follow the rule, how about using mangled names in the Typeable instances, as if the types were desugared to top level declarations? The appended type variables would also appear in the typeable instances. To me this seems more uniform.

Though, this does mean that the order of the appended type variables becomes inspectable - hmm! This isn't entirely unprecedented, though, TypeApplications also cares about the order of appearance of type variables.

The proposal also asks

Should instances be allowed to float out before type checking? In some cases that would allow related code to be kept together. But it would become more complex to explain the rules.

I think if the "could desugar" rule holds, then yes instances should float out before type checking.

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