coq-club AT inria.fr
Subject: The Coq mailing list
List archive
- From: Xavier Leroy <Xavier.Leroy AT inria.fr>
- To: coq-club AT inria.fr
- Subject: Re: [Coq-Club] large numbers in Gallina
- Date: Mon, 21 Dec 2020 14:14:41 +0100
- Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=Xavier.Leroy AT inria.fr; spf=Pass smtp.mailfrom=xavier.leroy AT gmail.com; spf=None smtp.helo=postmaster AT mail-wr1-f43.google.com
- Ironport-phdr: 9a23:PT2LjBBksTv37ylqwlt9UyQJP3N1i/DPJgcQr6AfoPdwSPT7p8bcNUDSrc9gkEXOFd2Cra4d1KyM6/qrBTVIyK3CmUhKSIZLWR4BhJdetC0bK+nBN3fGKuX3ZTcxBsVIWQwt1Xi6NU9IBJS2PAWK8TW94jEIBxrwKxd+KPjrFY7OlcS30P2594HObwlSizexfLF/IA+ooQnNqMUajoRvJrsswRbVv3VEfPhby3l1LlyJhRb84cmw/J9n8ytOvv8q6tBNX6bncakmVLJUFDspPXw7683trhnDUBCA5mAAXWUMkxpHGBbK4RfnVZrsqCT6t+592C6HPc3qSL0/RDqv47t3RBLulSwKMSMy/mPKhcxqlK9UrxyhqB5/zYDaY4+bKeRwcb/GcNwAWWZMRNxcWzBdDo6+aYYEEuoPPfxfr4n4v1YBrAGxBRetBOzx0D9Dm3n40rMg0+QmEQDNwQstENMUv3TKrdX6Kr0SXfqzwqbW1zXDaPNX1Cz86IjOaBAhoOuDUah+ccrL0EQiER7OgVqMp4L/JTyVyvgNvHaB7+pmTe+iiXIrpQFwrzWtxsohiZXEiI0Jxl7K9Sh03Zg5KNKlRENnfNKpFJpduiKUOYZ1Xs8uXWVltSg+x7ACuJO2fTUHxZI6zBDcc/yKa5aE7g7nWeqLIjp1hGhpdKyhixqv60StxePxW8+p21hQtCVFiMPDtnUV2hzT9MeHTvx981+k2TmV1gDT7vhIIE4ulabGMpIhzLE9m5QJvUTMGS/2n0r2jKuIeUk+5ueo7OHnbq3npp+aKYB0lhnzProylsG7G+g1MQgDU3KF9eiiybHv50L0TbtSgv0ziKbZsZTaJcoBpq6+Bg9Yyoki5AyhDzenztsYh2MLLFZbdxKdiIjoO0rDIP/9DfilglSslC1nyOzBPr3kGpnNNGTMkK/9fbZh7E5R0BY8zddG555NFr4BJO/zVVTqudzDDh45NhS0zPz9BNV80IMeQ2OPDbWDPKPcq1/brt4oduKLfcoevCv3A/kj/f/ny3EjynEHeqz87ZIJaX2iVtBrOUKIKS7ci8kAHH1MmgckS/3CiVuYUDcVaWzkDPF03S0yFI/zVdSLfYuqmrHUhH7mTK0TXXhPDxW3KVmtd4iAXK1ROiebI8skjSZdELb9EMku0hahsAK8wL1ifLKNq38o8Kn73d0w3NX90BQ79Dh6FcOYijjfQGR9n2dOTDgzjvkm/R5Nj2yb2K09uMR2UMRJ7qoRAAY8L5/VieJgWYj/
Le 20/12/2020 à 19:10, jonikelee AT gmail.com a écrit :
> What is the best performing way to use large numbers in Gallina? I am
> getting the following warning, which seems to imply I could have done
> something better with this definition:
>
> Definition fuel : nat := 10000.
>
> Warning: To avoid stack overflow, large numbers in nat are interpreted
> as applications of Nat.of_num_uint. [abstract-large-number,numbers]
> fuel is defined
The types N and Z from the standard library (modules NArith, ZArith) are exponentially more efficient than type nat, as N and Z use a base-2 representation internally.
Using N instead of nat in your code is usually easy. The tricky part is Peano-style recursion, which is a Fixpoint+pattern-matching in the case of nat and a library function in the case of N:
Fixpoint foo (n: nat) := match n with O => base | S m => f n (foo m) end
becomes
Definition foo (n: N) := N.recursion base f n.
Alternatively, you can use well-founded induction over N.
For additional performance, there is also the "Numbers" library, using a base-2^N representation, but it has so little documentation that I don't know how to use it.
On Mon, Dec 21, 2020 at 8:10 AM Guillaume Melquiond <guillaume.melquiond AT inria.fr> wrote:
Before trying to represent large numbers (e.g., using Z.to_nat), you
should first reconsider whether you actually need such large numbers.
Indeed, even to ensure termination, this is mightily inefficient.
Consider the following function as an example:
Fixpoint foo (n:nat) x :=
match n with
| S n => foo n (foo n x)
| O => bar x
end.
With n = 64, it will already run past the end of the solar system.
So, if you need more fuel than that, it presumably means that your
function was not properly written.
This is a nice trick when it applies, i.e. not that often in my experience. It's not a proper answer to the question "What is the best performing way to use large numbers in Gallina?".
Kind regards,
- Xavier Leroy
Best regards,
Guillaume
- [Coq-Club] large numbers in Gallina, jonikelee AT gmail.com, 12/20/2020
- Re: [Coq-Club] large numbers in Gallina, Guillaume Melquiond, 12/21/2020
- Re: [Coq-Club] large numbers in Gallina, Xavier Leroy, 12/21/2020
- Re: [Coq-Club] large numbers in Gallina, Guillaume Melquiond, 12/21/2020
- Re: [Coq-Club] large numbers in Gallina, Xavier Leroy, 12/21/2020
- Re: [Coq-Club] large numbers in Gallina, Guillaume Melquiond, 12/21/2020
Archive powered by MHonArc 2.6.19+.