Repository navigation
Replies: 5 comments 3 replies
|
This has been discussed a lot in the past, but no conclusion was reached. If I remember right, it's difficult to make it work for multiparameter type classes. |
|
Actually, I think your suggestion is a little different from what I've seen before. It might be a good idea. |
|
The thing I dislike about this proposal is that the new class has to rename the method, e.g., I'm also not sure how easy it would be to add this to MicroHs. There's no explicit check for an instance that all the superclasses instances are present. That check is instead performed as a normal part of the type checking (when trying to construct the superclass dictionaries). |
|
I think the idea is not very compatible with type level programming. data Zero
data Succ a
class (a + b) c -- a + b = c
instance (a + b) c => (Succ a + b) (Succ c) -- addition without base case
a :: (Succ Zero + Succ Zero) (Succ Zero) => ()
a = ()According to the rules above,
|
|
While Ghc does not like this overlapping instance formulation, it works in MicroHs and allows you to define more fine-grained, interoperable type classes without modifying existing interfaces. > :defs
module Interactive where
import qualified System.IO.PrintOrRun
default Num (Integer, Double)
default IsString (String)
default Show (())
class D a where d :: a -> a
class E a where e :: a -> a
instance D a => E a where e = d
instance E a => D a where d = e
data A = A
instance D A where d A = A
deriving instance Show A |
Uh oh!
There was an error while loading. Please reload this page.
Proposal to automatically instance super-classes
Goal
Reduce boilerplate
Some class hierarchies are very fine-grained. This proposal allows reducing boilerplate in class instance definitions.
Refinement after the fact
If we have a class hierarchy, sometimes we later decide that it is too coarse.
If the library is very much used, it becomes a high burden to introduce a super-class constraint, as all dependencies must implement such for each instance, even if it is trivial.
Motivation
Let us say we split
fromIntegerout ofNum(let's not discuss whether this is useful).So we had something like this
[1]:
and now want something like this
[2]:
While it is likely too much effort to change every
Numinstance,we can define a mostly equivalent version that is compatible with previous instances.
[3]:
Notice how
Numis the same as [1] except for aFromInteger a =>constraint?This (and the default implementation of
fromInteger') means any implementation site must only update to alsoinstance FromInteger a.+instance FromInteger Integer instance Num Integer where -- actual implementation unchangedThis is better, but the empty instance declaration seems unnecessary.
Note that the actual function of the super-class changed from [2] to [3].
A way to allow super-classes' function definitions in sub-class would be neat, but is out of scope for this proposal.
Rules
I propose that instance definitions automatically instance super-classes if
[Note def]:
The default keyword need not be present when the function does not require additional constraints.
[Note orphans]:
Since orphans should be the responsibility of the person writing the orphan instance, we assume all relevant instances are in scope.
Minimal example of rules
Non-issues
Existing code will always keep working.
Instances either failed before this change or were valid.
Only behavior for failing instances is changed.
Issues
Discussion
Some version of automatic instances have been discussed for a long time. I tried to make it minimal but still useful.
Any feedback is welcome.
All reactions