Skip to content
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

Judgmental rational addition #1340

Open
lowasser opened this issue Feb 21, 2025 · 8 comments
Open

Judgmental rational addition #1340

lowasser opened this issue Feb 21, 2025 · 8 comments

Comments

@lowasser
Copy link
Contributor

I was mentioning to someone or another the difficulty of taking reciprocals of natural numbers, or doing anything significant with them, and part of the problem is that things may be a little too abstract -- in particular, one-Q +Q one-Q is not judgmentally equal to rational-Z (int-N 2).

Making more things abstract for rational numbers is definitely good for build performance, but this makes constants very hard to work with, e.g. one-half-Q + one-half-Q = one-Q. This is, in particular, why we have double-le-Q the way it is.

This is mostly a reminder to work on this myself.

@fredrik-bakke
Copy link
Collaborator

I recently mentioned to Egbert that there are serious performance drawbacks to our current definition of rational numbers, since the fractions have to be reduced between every operation (a discussion I saw happen before over at 1lab). He mentioned he is contributing another definition of the rational numbers in #1211, and there are other definitions to explore too, such as the set-quotient definition. I'm not saying that we should change our definition, I'm just pointing out the performance drawback.

I don't personally see a problem with unabstracting certain definitions such as constants, but I don't work much directly with rationals, so don't have a good sense for which things can safely be non-abstract.

@EgbertRijke
Copy link
Collaborator

While it's true that our current rational numbers are slow, I think we have to be careful about how we come to our conclusions about why that is.

Bringing fractions to lowest terms is probably not the issue, I would find that hard to believe, because that can be done very fast with Euclid's algorithm. The problem is more likely not that we're bringing fractions to lowest terms, but that we don't use Euclid's algorithm to do so.

Another thing that I would be more inclined to believe as a cause for slowing down type checking is that we've set up our definitions so that Agda has to check definitional equality too often. This was an issue with the structure identity principle too, which is why it looks like you have to provide unnecessarily many arguments. This is to avoid the type checker to check for judgmental equality when it doesn't have to.

I'm including some performance related changes in #1211, but I haven't gotten to the rational numbers yet. I think I'd rather first finish up #1211

@EgbertRijke
Copy link
Collaborator

The data definition of the rational numbers represents rational numbers as Farey fractions. I think this should be fast, but I have no idea how hard it is to show that the rational numbers are a discrete field with this definition.

@fredrik-bakke
Copy link
Collaborator

While it's true that our current rational numbers are slow, I think we have to be careful about how we come to our conclusions about why that is.

Bringing fractions to lowest terms is probably not the issue, I would find that hard to believe, because that can be done very fast with Euclid's algorithm. The problem is more likely not that we're bringing fractions to lowest terms, but that we don't use Euclid's algorithm to do so.

Right, apologies, I should have been more clear with assumptions. Agda has an arsenal of performance analysis tools that can help us determine what is slowing down type-checking. That is something to look into to avoid acting on false assumptions.

@EgbertRijke
Copy link
Collaborator

It would be very nice to incorporate more performance analysis in our work flow, to see what are the bottle necks in agda-unimath. This page on the readthedocs site gives some tools

https://agda.readthedocs.io/en/latest/tools/performance.html

which we could start using more.

@lowasser
Copy link
Contributor Author

The problem is more likely not that we're bringing fractions to lowest terms, but that we don't use Euclid's algorithm to do so.

This is correct. I've observed goals reducing down to calls here.

@EgbertRijke
Copy link
Collaborator

My PR about ideals in semirings is an attempt towards refactoring the definition of the GCD.

@VojtechStep
Copy link
Collaborator

Do you plan on keeping the current version around for expository reasons? The implementation details are documented in section 8.4 in your book.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants