Skip to content

Coq proof of Bertrand's postulate on existence of primes [maintainer=@thery]

License

Notifications You must be signed in to change notification settings

coq-community/bertrand

Repository files navigation

Bertrand

Docker CI Contributing Code of Conduct Zulip DOI

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.

Meta

Building and installation instructions

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

Contents

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.

Extracting and running the OCaml partition program

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;;

Replaying the WhyML program correctness proof

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):

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