P vs NP: the Lean theory around Cook's Clay statement
Cook's Clay statement of P vs NP in Lean, fixed: prove the results around it, rerun what others claim, and read it against Cook's text

| 1 | # Lean image for the p-vs-np `build` check (checks/build.py). |
| 2 | # |
| 3 | # Build with a checkout of the project's main as the context, not this folder |
| 4 | # (docker/build.sh does it, with the mirror a host in Beijing needs): |
| 5 | # git clone <the project's repository> /tmp/pnp-main |
| 6 | # sh /tmp/pnp-main/docker/build.sh /tmp/pnp-main |
| 7 | # |
| 8 | # The project is not a copy of a repository that can be cloned: it is the |
| 9 | # import closure of the P vs NP files of lean-dojo/LeanMillenniumPrizeProblems |
| 10 | # at 603053dc (3 of its Lean files, see upstream.json), with this project's own |
| 11 | # lakefile.toml and lake-manifest.json, which require Mathlib alone. Cloning |
| 12 | # upstream would bring the other six problems and its Physlib dependency. So the |
| 13 | # image is built from the project's own tree: the context is a checkout of |
| 14 | # main, and COPY puts it in /opt/proof. The check runs with --network none: it |
| 15 | # copies a commit's tree to a scratch directory, gives it /opt/proof/.lake, and |
| 16 | # runs lake build. Nothing is downloaded at check time. |
| 17 | # |
| 18 | # P vs NP is open: checks/build.json names no theorem and no Comparator |
| 19 | # configuration, so the image holds no Comparator, exporter, sandbox or second |
| 20 | # kernel. When a person who maintains the project adds a Comparator challenge |
| 21 | # (a claimed solution), the image gains them, as problems/navier-stokes's does |
| 22 | # for a toolchain before v4.35.0. |
| 23 | # |
| 24 | # Every pin is a full commit hash in an ARG below, and each is checked. See |
| 25 | # README.md for where each pin comes from. |
| 26 | |
| 27 | FROM ubuntu:24.04 |
| 28 | |
| 29 | # --- Pins ------------------------------------------------------------------- |
| 30 | |
| 31 | # lean-dojo/LeanMillenniumPrizeProblems, whose P vs NP files the project |
| 32 | # starts from, and its lean-toolchain at that commit. The context must hold |
| 33 | # that toolchain. |
| 34 | ARG UPSTREAM_REPO=https://github.com/lean-dojo/LeanMillenniumPrizeProblems |
| 35 | ARG UPSTREAM_COMMIT=603053dc267cf3efe422f438eb78098c0ececd6f |
| 36 | ARG TOOLCHAIN=leanprover/lean4:v4.31.0 |
| 37 | # From the project's lake-manifest.json (upstream's pin of Mathlib, tag |
| 38 | # v4.31.0, and Mathlib's own dependencies as upstream's manifest lists them); |
| 39 | # checked after the dependencies are fetched. |
| 40 | ARG MATHLIB_COMMIT=fabf563a7c95a166b8d7b6efca11c8b4dc9d911f |
| 41 | ARG BATTERIES_COMMIT=fa08db58b30eb033edcdab331bba000827f9f785 |
| 42 | ARG AESOP_COMMIT=e3cb2f741431ce31bf73549fb52316a57368b06f |
| 43 | |
| 44 | # --- System packages -------------------------------------------------------- |
| 45 | |
| 46 | # On the Beijing host: --build-arg APT_MIRROR=http://mirrors.aliyun.com/ubuntu |
| 47 | # (plain http: ca-certificates is not installed yet at this step). |
| 48 | ARG APT_MIRROR= |
| 49 | # python3, curl and zstd are what Lake and Mathlib's cache tool use; python3 is |
| 50 | # also what runs checks/build.py and checks/challenge.py. |
| 51 | ENV DEBIAN_FRONTEND=noninteractive |
| 52 | RUN if [ -n "$APT_MIRROR" ]; then sed -i "s#http://archive.ubuntu.com/ubuntu#$APT_MIRROR#; s#http://security.ubuntu.com/ubuntu#$APT_MIRROR#" /etc/apt/sources.list.d/ubuntu.sources; fi \ |
| 53 | && apt-get update && apt-get install -y --no-install-recommends \ |
| 54 | ca-certificates curl git python3 build-essential zstd \ |
| 55 | && rm -rf /var/lib/apt/lists/* |
| 56 | |
| 57 | # The check may run as another user than the one that built; let git (and |
| 58 | # Lake, which calls git in each package) read every repository in the image. |
| 59 | RUN git config --system --add safe.directory '*' |
| 60 | |
| 61 | # --- Lean ------------------------------------------------------------------- |
| 62 | |
| 63 | # elan from its GitHub release: raw.githubusercontent.com, where elan-init.sh |
| 64 | # lives, is unreachable from the Beijing host. The check compiles a commit as |
| 65 | # `nobody`, so everything under /opt is made readable in the layer that makes it. |
| 66 | # The toolchain comes through elan from github.com (where releases.lean-lang.org |
| 67 | # sends it): SJTU's mirror, which the other problems' images use from Beijing, |
| 68 | # does not hold v4.31.0. On 12-02 the 558 MB took 23 minutes there. |
| 69 | ENV ELAN_HOME=/opt/elan |
| 70 | ENV PATH=/usr/local/bin:/opt/elan/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin |
| 71 | RUN curl -sSfL --retry 5 https://github.com/leanprover/elan/releases/download/v4.1.2/elan-x86_64-unknown-linux-gnu.tar.gz | tar -xz -C /tmp \ |
| 72 | && /tmp/elan-init -y --no-modify-path --default-toolchain none && rm /tmp/elan-init |
| 73 | RUN (elan toolchain install "${TOOLCHAIN}" || elan toolchain install "${TOOLCHAIN}" || elan toolchain install "${TOOLCHAIN}") \ |
| 74 | && elan default "${TOOLCHAIN}" \ |
| 75 | && chmod -R a+rX /opt/elan |
| 76 | |
| 77 | # clone-at <url> <dir> <commit>: that one commit of a repository, without its history, checked out and checked. From |
| 78 | # Beijing a connection to github.com times out, stalls or breaks off every few tries (12-02), and Lake clones each |
| 79 | # package once and does not try again: so every package of the manifest is fetched this way before Lake runs, a fetch |
| 80 | # that stalls is cut off, and each is tried ten times. |
| 81 | RUN printf '%s\n' '#!/bin/sh' 'set -eu' \ |
| 82 | 'n=0; until rm -rf "$2" && git init -q "$2" && git -C "$2" remote add origin "$1" && timeout 600 git -C "$2" -c http.lowSpeedLimit=1000 -c http.lowSpeedTime=30 fetch -q --depth 1 origin "$3"; do n=$((n+1)); [ "$n" -lt 10 ]; sleep 5; done' \ |
| 83 | 'git -C "$2" -c advice.detachedHead=false checkout -q FETCH_HEAD' \ |
| 84 | 'test "$(git -C "$2" rev-parse HEAD)" = "$3"' > /usr/local/bin/clone-at \ |
| 85 | && chmod 755 /usr/local/bin/clone-at |
| 86 | |
| 87 | # --- The dependencies ------------------------------------------------------- |
| 88 | |
| 89 | # From the three files that name them, so that a change elsewhere in main does not fetch Mathlib's cache (5 GB) again. |
| 90 | # Each package of the manifest at its commit first (clone-at), then Mathlib's oleans: a few of the cache's downloads |
| 91 | # fail now and then, and each try fetches only what is missing. Its download store (~/.cache/mathlib) is unpacked |
| 92 | # into .lake by then and removed. The check compiles a commit as `nobody`, and the cache unpacks its files 0600: they |
| 93 | # are made readable here, in the layer that makes them (a chmod in a later layer copies every file it visits). |
| 94 | # GIT_MIRROR (docker/build.sh with MIRRORS=cn: https://mirror.sjtu.edu.cn/git/lean4-packages) serves the seven |
| 95 | # packages it holds, Mathlib among them, at the same commits (each is checked below or by Lake against the manifest); |
| 96 | # the others still come from github.com. The packages keep their github.com addresses, and the setting, root's own, |
| 97 | # is removed once they are fetched. On 12-02 github.com could not be reached from Beijing for minutes at a time. |
| 98 | ARG GIT_MIRROR= |
| 99 | WORKDIR /opt/proof |
| 100 | COPY lakefile.toml lake-manifest.json lean-toolchain /opt/proof/ |
| 101 | RUN test "$(cat lean-toolchain)" = "${TOOLCHAIN}" \ |
| 102 | && if [ -n "$GIT_MIRROR" ]; then for r in leanprover-community/mathlib4 leanprover-community/batteries leanprover-community/aesop \ |
| 103 | leanprover-community/quote4 leanprover-community/ProofWidgets4 leanprover-community/import-graph leanprover/lean4-cli; do \ |
| 104 | git config --global url."$GIT_MIRROR/${r#*/}".insteadOf "https://github.com/$r"; done; fi \ |
| 105 | && python3 -c 'import json; [print(p["url"], ".lake/packages/" + p["name"], p["rev"]) for p in json.load(open("lake-manifest.json"))["packages"]]' \ |
| 106 | | while read -r url dir rev; do clone-at "$url" "$dir" "$rev" || exit 1; done \ |
| 107 | && rm -f /root/.gitconfig \ |
| 108 | && test "$(git -C .lake/packages/mathlib rev-parse HEAD)" = "${MATHLIB_COMMIT}" \ |
| 109 | && test "$(git -C .lake/packages/batteries rev-parse HEAD)" = "${BATTERIES_COMMIT}" \ |
| 110 | && test "$(git -C .lake/packages/aesop rev-parse HEAD)" = "${AESOP_COMMIT}" \ |
| 111 | && (lake exe cache get || lake exe cache get || lake exe cache get) \ |
| 112 | && test "$(git -C .lake/packages/mathlib rev-parse HEAD)" = "${MATHLIB_COMMIT}" \ |
| 113 | && rm -rf /root/.cache/mathlib \ |
| 114 | && chmod -R a+rX /opt/proof |
| 115 | |
| 116 | # --- The project ------------------------------------------------------------ |
| 117 | |
| 118 | # The context is a checkout of the project's main (see the top of this file): the closure of upstream's P vs NP files |
| 119 | # at UPSTREAM_COMMIT with this project's files. A full build of every library of lakefile.toml (Problems, Tests: both default targets) follows, so a |
| 120 | # pull request's build recompiles only what it changed; Lake must not have rewritten the manifest; |
| 121 | # and the build becomes `nobody`'s (checks/build.py can then build a commit on top of it in place, `in_place` in |
| 122 | # checks/build.json, instead of copying it for every job). Only what lacks read access is chmod-ed: a chmod of all of |
| 123 | # /opt/proof here would copy Mathlib into this layer. |
| 124 | COPY . /opt/proof |
| 125 | RUN rm -rf .git \ |
| 126 | && test -f lakefile.toml && test ! -e lakefile.lean \ |
| 127 | && test -f Problems/PVersusNP/Millennium.lean \ |
| 128 | && grep -q "${UPSTREAM_COMMIT}" docker/lean.Dockerfile \ |
| 129 | && lake build Problems Tests \ |
| 130 | && grep -q "${MATHLIB_COMMIT}" lake-manifest.json \ |
| 131 | && chown -R nobody:nogroup .lake/build \ |
| 132 | && find /opt/proof \( -type f ! -perm -004 -o -type d ! -perm -005 \) -exec chmod a+rX {} + |
| 133 | |
| 134 | RUN for t in lake lean git python3 which; do \ |
| 135 | command -v "$t" >/dev/null || { echo "missing: $t"; exit 1; }; \ |
| 136 | done |
| 137 | |
| 138 | ENV PROOF_BASE=/opt/proof |
| 139 | WORKDIR /opt/proof |
| 140 | CMD ["/bin/bash"] |