A Coq proof of Bertrand's postulate, which says that there always exists a prime between n and 2n for n greater than 2. Includes an application of the postulate to compute partitions.
- Author(s):
- Laurent Théry (initial)
- Coq-community maintainer(s):
- Laurent Théry (@thery)
- License: GNU Lesser General Public License v2.1 or later
- Compatible Coq versions: Coq 8.18 or later
- Additional dependencies: none
- Coq namespace:
Bertrand
- Related publication(s):
The easiest way to install the latest released version of Bertrand is via OPAM:
opam repo add coq-released https://coq.inria.fr/opam/released
opam install coq-bertrand
To instead build and install manually, do:
git clone https://github.com/coq-community/bertrand.git
cd bertrand
make # or make -j <number-of-cores-on-your-machine>
make install
This project consists of:
- A Coq proof of Bertrand's postulate: there always exists a prime between
n and 2n for n greater than 2 (
Bertrand.v
). - A little program in OCaml, based on code extracted from Coq,
that generates a partition of 1..2n in pairs (i,j) such that i+j
is always prime (
run_partition.ml
). The proof of correctness of this program is a direct consequence of Bertrand's postulate (Partition.v
). This nice application of Bertrand's postulate was suggested by Gérard Huet. - A proof of correctness of an implementation of the algorithm for computing primes described in "The Art of Computer Programming: Fundamental Algorithms" by Knuth, pages 147-149. The proof uses the Why3 tool to generate verification conditions for the WhyML program that implements the algorithm. These verification conditions can then be discharged by Coq and the Alt-Ergo prover.
To extract code and obtain the program, run
make run_partition.ml
Next, open an OCaml toplevel (e.g., ocaml
) and do
#use "run_partition.ml";;
To get a partition from 1...30:
let part30 = part 15;;
To replay the proof of correctness for the WhyML program for computing primes, first make sure the following packages are installed (in addition to Coq 8.18.0 and the proof of Bertrand's postulate):
- Alt-Ergo 2.5.4
- Why3 1.4.1 and its Coq library
These packages can be installed via OPAM using the following command:
opam install alt-ergo.2.5.4 why3.1.7.2 why3-coq.1.7.2
Then, the proof can be replayed by running
make why