More efficient implementation of /, % and GCD #1258
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The definition of
gcd
that uses well-founded induction and%
fromCubical.Data.Fin
has some performance issues.I also noticed that
quotient_/_
andremainder_/_
take quite some time to normalize, so I added alternative implementations, calledquotient'_/_
andremainder'_/_
, using the built-in helper functions fromAgda.Builtin.Nat
. Lemmas about these helper functions are adapted from the standard library.I only proved the necessary properties (
≡remainder'+quotient'
andmod'<
) required to define the efficient version of GCD.I have already tested whether the performance issues come solely from this, but it seems there are other factors as well, particularly the use of well-founded induction over the naturals.
In this PR, the new proposed implementation becomes the default one, but the previous implementation can still be found at the end of the
GCD
file, with a prime ('
) symbol in the name.The extensional equality between the two implementations is easily proved by the fact that
GCD m n
is a proposition.This improvement enables fast fraction reduction and, combined with PR #1245, addresses efficiency for rational arithmetic (#1124)
@felixwellen, please let me know if this new implementation is appropriate.