-
Notifications
You must be signed in to change notification settings - Fork 108
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
Support inferred superclass instances #481
Comments
I strongly favour more verbose syntax and would discourage implicit behaviour. Hiding the inferred derivation by default could result in very subtle issues for the end-user. It's worth pointing out Deriving Via or, How to Turn Hand-Written Instances into an Anti-pattern as the closest Haskell equivalent. |
Nicely written. Just so I'm sure I understand correctly, should your |
Nice catch, thanks @duvenaud. |
An interesting property of inferred superclass instances: it allows refining the class hierarchy without breaking user code. In particular you could lift operators from an interface into a new parent interface, and user definitions would remain valid (now defining two instances). |
This is a good idea and we should do it. Especially now that our class hierarchies are growing deeper, with the recent addition of the Let's implement the first version Dan proposed, where the superclass names are implicit. I like Daniel's argument that it lets you refine the class hierarchy without breaking user code. As for implementation, I think we can do it entirely within |
Motivation
Haskell has fine-grained typeclass hierarchies. Consider:
I want to implement
Monoid
for[a]
. To do so, I must provide two instances: one forMonoid
, and one for all the superclasses (Semigroup
).When implementing a typeclass with a long chain of superclasses, it may be nice to define all methods within the instance for the leaf typeclass — instead of writing one instance per typeclass in the chain.
Design
In Dex, we could support inferred superclass instances. Consider the following typeclass hierarchy:
The proposed feature is: we can directly provide an instance for
Abelian Float
like so, inferring superclass instances:An extension is to allow explicit declarations of superclass instances if desired, so they can be found in the source code:
Alternatives considered
Don't do it: favor clarity in source code
One downside of inferred superclass instances is that the superclass instances no longer appear in the source code. With
instance Abelian Float
: we cannot grep forinstance Semigroup Float
in the source code.This concern seems mitigated with proper tooling support:
dex web
and rendered HTML pages: hover tooltips on the instance declaration could show inferred superclass instance information.The text was updated successfully, but these errors were encountered: