-
Notifications
You must be signed in to change notification settings - Fork 645
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
Overlapping implementations #4279
Comments
@nicolabotta I believe this is expected behavior, but the solution in your case should be quite simple. Just make |
@ahmadsalim What do you mean by making
as suggested in the stackoverflow post. This is still very annoying because the last two lines of the example have to be rewritten over and over again for every type that is enumerated. This is tedious and a program like
seems a very natural mechanism for avoiding these repetitions altogether, at least to me. I frankly do not understand why the original program should not be expected to type check. I understand that an implementation of The type checker complain when one tries to use |
@nicolabotta So Idris unlike Haskell, currently forbids overlapping instances. But in your case you could write something like interface Eq t => Enumerated (t : Type) (len : Nat) | t where
values : Vect len t
toFin : t -> Fin len
values_match_toFin : map toFin values = range You may also be able to give a default implementation of |
@ahmadsalim As explained in my previous post, the whole point of the exercise is not to derive implementations of I understand that Idris forbids overlapping instances but I believe that this basically sucks. I have tried to use interfaces in Idris for about 5 years and I have to conclude that I never managed to do so in a profitable way, see, among others #4194 and the many issue reports on interfaces preceding it. #4194 is also the reason why I am still stuck with Idris 0.99. The fact that Prelude implementations of Prelude interfaces prevent users from being able to derive implementations of those interfaces is, in my view, a major design mistake. |
@nicolabotta Maybe, this is something worth discussing with @edwinb regarding Blodwen, when the time arrives. I do understand the frustration and agree that there are things to improve regarding interfaces. Unfortunately I do not hold much sway regarding design and I am unsure how much effort is required regarding extending the interface implementation. |
@ahmadsalim I understand, thanks for looking into this issue! Best, Nicola |
It's expected behaviour, but you're right, it is annoying (I've been bitten by it myself a couple of times recently, so familiar with the annoyance...). The problem would be that, if you add an instance of Enumerated for a type which already has an Eq implementation, which should Idris choose? Incidentally, Haskell only allows this sort of thing with I have tried a hack around this, which is the
I'm not convinced it's a good idea, though. And I really don't think it's fair to say it sucks to prioritise predictable behaviour from the compiler over absolute convenience - in the past I have sometimes preferred the latter, but it has led to surprising problems too often. So, we should try to find a non hacky solution. Blodwen is gathering pace. It'll soon be time to think seriously about how interfaces should work. |
By the way, I do think this pattern is a legitimate thing to do as long as it's the only implementation of Eq for types which implement Enumerated. But I don't know a good, efficient, way to check that, and the elaborator is already too slow. |
@edwinb Would default super-interfaces not solve this issue? |
Another thing one could consider is to allow that super-interfaces are added later provided they are given a default implementation using sub-interfaces. This may be easier to check than arbitrary constraints, but still requires some work. |
@edwinb Thanks for looking into this issue Edwin! The reason why I am saying that forbidding overlapping interfaces sucks is that it makes it practically impossible to use interface derivation in a profitable way. Logically, it is as if a proof that I have probably developed the specific skill of trying to use Idris interfaces in ways they are not meant to be used. As a matter of fact, most of the times I try to use interfaces, I run into issues that seem to be hard to fix. That was the case with #3727 that kept me on 0.99 from April 2017 until November and then with #4194 that still keeps me on that Idris version. I have probably very naive expectations but, in the specific case, I would think that whenever the type checker runs into an overlapping implementation of an interface, it just turn it into a named implementation. If it subsequently happens to run into expressions that use a member of that interface, i would expect the type checker to report an ambiguous usage of that member and require the programmer to resolve the ambiguity with an |
Hmm, so this does work, and I think it's what @ahmadsalim is suggesting:
So, when you implement It's probably a better solution than my |
@edwinb Thanks, I was not aware of this possibility! I will have to understand what it means and how to use it. |
Elaborating on the example given in https://stackoverflow.com/questions/47464426/idris-eq-for-enumerated-type, I have tried to implement
This fails to type check with
Is this the expected behavior? Is it an Idris error? How can I state that every implementation of
Enumerated
is also an implementation ofEq
?The text was updated successfully, but these errors were encountered: