-
Notifications
You must be signed in to change notification settings - Fork 644
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
Make algebra interfaces verified by default (#4739) #4841
Conversation
Not everything can be proven in the type system, e.g. when dealing with external things like String, Int, Float, etc. Think I'd prefer |
@LeifW Sounds good, I'll replace the |
I don't see how we can merge this. It's a very invasive API change that will break a lot of code. It has also been discussed and rejected before, as Idris is about allowing people to write verified code, not forcing them to (@edwinb can correct me on the formulation of that). And finally, Idris 2 is where the action happens and any big changes should be done there. I'll leave this open for a while for feedback, but I'd need an OK from @edwinb to merge it in any case. |
Do you have any links to past discussions? All I could find was #4739, which doesn't have much. |
Previously there were interfaces for Semigroup, Monoid, etc, that were just syntactic in nature, requiring only that operations like <+> or elements like neutral exist. There was a corresponding set of "verified" interfaces requiring that the expected laws actually hold. Thus Semigroup required that the <+> operation exist, while VerifiedSemigroup required that that operation actually be associative. Such an arrangement is annoying for two reasons. First, extra paperwork. If I want to show that something really is a semigroup, I have to write two implementation blocks, the "plain" version and the "verified" version. On the other side, if I want to add a new interface, then in order to keep with the existing style I need to write both a "plain" version and a "verified" version. In some cases, like AbelianGroup, the "plain" interface is empty, since it doesn't add any new syntactic elements. This is pointless. Second, proof. Suppose a type has been declared to be a Semigroup, but VerifiedSemigroup hasn't been implemented for it. What can be assumed about that type? Is its <+> operation associative or not? If it isn't, why call it a semigroup? If it is, why not prove it? And if it can't be proved, how do we know that it's true? The big selling point of Idris is its expressive type system and strong compile-time guarantees, so let's take advantage of that and verify by default. Fixing the interfaces is easy -- just move the "verified" requirements into the "plain" interface, and that's it. In some cases there already existed implementations of the "verified" interfaces, and those could also easily be copied over. More challenging are the cases for which there do not exist verified implementations. There are four ways of dealing with them: (1) Come up with the required proofs. This was done, for example, with `Semigroup a => Semigroup (Vect n a)` etc in contrib/Data/Matrix/Algebraic.idr, and also with easy ones like Maybe and Endomorphism. A really nice one is the new implementation of `Ring t => Ring (Complex t)`. (2) Assert without proof that the laws hold using believe_me. This was done for all primitive types, including String and Integer, since primitive types are not amenable to proof. (3) Add postulates asserting that the laws hold. This was done for nonprimitive datatypes for which I couldn't figure out proofs, including Isomorphism and Morphism. I am pretty sure that the laws hold for these cases. (4) Revoke the name Semigroup, Monoid, etc. This was done for a few data structures in contrib, like SortedMap and its descendants. I am skeptical that these structures really are what they say they are. Note that updating an existing interface to be verified does not require writing any proofs -- it can be done just by asserting that the required properties hold, either with explicit postulates or with belive_me (or even really_believe_me). There are a variety of examples of updated implementations in this PR. This change only applies to those interfaces that belong to "abstract algebra": semigroups, monoids, groups, and so on. There is a whole other set of "plain"/"verified" interfaces dealing with structures from "category theory", like monad and functor. Those should also undergo this unification, but I don't know how to do it just yet.
@LeifW @melted @Sventimir Updated to do away with unverified interfaces, use explicit postulates for nonprimitive types, fill in a bunch more proofs, and fix a few errors. |
@melted This change does not require anyone to write proofs. Instead, it's perfectly possible to write postulates or use |
@nickdrozd I perfectly understand your point and agree with it with my whole heart! However, backwards compatibility is important too. Perhaps one of the reasons of success of Haskell is that its creators did not change things too dramatically at any point. Any wrong decision they made (and they made quite a lot of them), it stuck. Annoying as it may be, it's probably necessary if you want people to actually use the language. Fortunately, Idris2 is already being developed and I believe we can fix this mistake in that release. |
The proofs are cool and I can see you put a lot of work into this. However, I feel that there are some contradictions in the "merge the interfaces" part of the pull request.
The two implementations are not really two versions of one thing. The implementation blocks are disjoint and they implement different things; one implements the operations, the other implements the properties. Regarding the paperwork, an extra implementation block costs you 1) one blank line plus 2) one implementation header line. So you complain about having to add two lines of code, but at the same time you propose forcing everyone else to add (many lines of) proofs in their code. Are mandatory proof obligations not extra paperwork with size significantly greater than two lines of code for each verified interface?
I don't think you would add a "plain" interface for abelian groups. Why not More generally,
It's called "semigroup" so the library author wants you to think about it as associative. However, the lack of a formal proof says that it's either hard to prove, or noone has done it yet, or maybe it's false.
For the same reason that your pull request is full of
I don't believe you should do things just because something lets you do them. And I'm quite sure you should not force other people to do these things. Verification is costly, so you usually want to find the best balance of verification and productivity. I find that usually the best approach is to pick an invariant that is a frequent source of bugs, have the type system keep track of it for you, and let the rest be.
semigroupOpIsAssociative = believe_me Integer Oh, this is not the right way to use
I'll take an unverified non-segfaulting program over a verified segfaulting one anytime. ;) I get what you're saying, though; I suggest using postulates for this.
By the nature of your proposal, I'd expect that you would be the first person to reject "I am pretty sure" in lieu of a formal proof.
...so let's not subvert them by postulating stuff haphazardly!
Once these structures are filled with Names are are meant to be suggestive. Proofs are meant to be reliable (and, rightly, do not depend on how you name things). The name Thus I see nothing wrong in naming something
What's the point of having proofs at all if you're going to fill them with assertions by default, anyway? To sum up, the proposal is to have verified properties included in interfaces because:
I feel that the two points above are in contradiction with the later parts of the same proposal:
Saying something that you can't rely on is worse than saying nothing. And if this landed in the stdlib, you could never be sure that any proof based on this is sound. You've implemented some cool proofs, though; those about the structure of the complex numbers look quite neat, so even though I can't back the interface merging proposal, I suggest that you pack the proofs up and submit them in the stdlib. They'd be nice to have! |
I support the arguments listed before to not merge this into master namely:
|
The
I don't think anybody needs to be forced to prove anything. Likewise,
Yeah, I'm not wed to
Not at all! Stating a postulate makes it explicit in the code that The current system makes the same assurance, but implicitly. If the As the Zen of Python says, "Explicit is better than implicit." :)
Well, it would mean that whoever added those In the particular case of
I don't actually advocate doing that; I only did it here so as to not
A certain amout of trust is always required. How do we know the
That's fair. I'll get the nonbreaking changes together in a separate |
Previously there were interfaces for Semigroup, Monoid, etc, that were
just syntactic in nature, requiring only that operations like <+> or
elements like neutral exist. There was a corresponding set of
"verified" interfaces requiring that the expected laws actually hold.
Thus Semigroup required that the <+> operation exist, while
VerifiedSemigroup required that that operation actually be
associative.
Such an arrangement is annoying for two reasons.
First, extra paperwork. If I want to show that something really is a
semigroup, I have to write two implementation blocks, the "plain"
version and the "verified" version. On the other side, if I want to
add a new interface, then in order to keep with the existing style I
need to write both a "plain" version and a "verified" version. In some
cases, like AbelianGroup, the "plain" interface is empty, since it
doesn't add any new syntactic elements. This is pointless.
Second, proof. Suppose a type has been declared to be a Semigroup, but
VerifiedSemigroup hasn't been implemented for it. What can be assumed
about that type? Is its <+> operation associative or not? If it isn't,
why call it a semigroup? If it is, why not prove it? And if it can't
be proved, how do we know that it's true? The big selling point of
Idris is its expressive type system and strong compile-time
guarantees, so let's take advantage of that and verify by default.
Fixing the interfaces is easy -- just move the "verified" requirements
into the "plain" interface, and that's it.
In some cases there already existed implementations of the "verified"
interfaces, and those could also easily be copied over. More
challenging are the cases for which there do not exist verified
implementations. There are four ways of dealing with them:
(1) Come up with the required proofs. This was done, for example,
with
Semigroup a => Semigroup (Vect n a)
etc incontrib/Data/Matrix/Algebraic.idr, and also with easy ones like
Maybe and Endomorphism. A really nice one is the new
implementation of
Ring t => Ring (Complex t)
.(2) Assert without proof that the laws hold using believe_me. This
was done for all primitive types, including String and Integer,
since primitive types are not amenable to proof.
(3) Add postulates asserting that the laws hold. This was done for
nonprimitive datatypes for which I couldn't figure out proofs,
including Isomorphism and Morphism. I am pretty sure that the
laws hold for these cases.
(4) Revoke the name Semigroup, Monoid, etc. This was done for a few
data structures in contrib, like SortedMap and its descendants.
I am skeptical that these structures really are what they say
they are.
Note that updating an existing interface to be verified does not
require writing any proofs -- it can be done just by asserting that
the required properties hold, either with explicit postulates or with
belive_me (or even really_believe_me). There are a variety of examples
of updated implementations in this PR.
This change only applies to those interfaces that belong to "abstract
algebra": semigroups, monoids, groups, and so on. There is a whole
other set of "plain"/"verified" interfaces dealing with structures
from "category theory", like monad and functor. Those should also
undergo this unification, but I don't know how to do it just yet.
[updated]