File size: 5,916 Bytes
698969a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | #!/usr/bin/env python3
"""Compile the pinned official FPS layer and instantiate both macros.
The archived Basic.lean imports Mathlib, but the file itself uses only the
Lean core/elaborator API. The temporary Mathlib module below is therefore a
dependency shim, not a rewritten copy of Basic.lean: the source file is
extracted byte-for-byte and compiled as the imported module.
"""
from __future__ import annotations
import hashlib
import json
import os
import shutil
import subprocess
import tarfile
import tempfile
from pathlib import Path
ARCHIVE = Path("source/official-current.tar.gz")
SOURCE_SUFFIX = "/data/formal_problem_solving/FormalProblemSolving/Basic.lean"
AUDIT = r'''import FormalProblemSolving.Basic
open Lean Elab Command Term Meta Tactic
open scoped FPS DFPS
set_option autoImplicit false
set_option relaxedAutoImplicit false
problem source_level_direct
find (n : Nat) s.t.
: n = 2 := solve
exact rfl
example : source_level_direct.Answer = 2 := by rfl
problem source_level_find_all
find_all (answer_predicate : Prop) iff
(hH : True)
: ((True : Prop) ↔ answer_predicate) := solve
case Forward => exact _h_conclusion
case Backward => exact _h_answer
example : (source_level_find_all True).Answer := by
exact ((source_level_find_all True).Proof True.intro).mp Iff.rfl
example : (True : Prop) ↔ (source_level_find_all True).Answer := by
simpa [ProblemTarget] using (source_level_find_all True).Proof True.intro
theorem constructor_soundness {α : Type} {P : α → Prop}
(s : ProblemSol P) : P s.Answer := s.Proof
theorem find_all_has_both_directions {H C : Prop}
(s : ProblemSol (fun answer : Prop => H → (C ↔ answer)))
(hH : H) :
(C → s.Answer) ∧ (s.Answer → C) := by
constructor
· intro hC
exact (s.Proof hH).mp hC
· intro hA
exact (s.Proof hH).mpr hA
example : (2 : Nat) = 2 :=
constructor_soundness source_level_direct
example : (True : Prop) := by
have h := find_all_has_both_directions (source_level_find_all True) True.intro
have hA : (source_level_find_all True).Answer :=
((source_level_find_all True).Proof True.intro).mp Iff.rfl
exact (h.2 hA).mp True.intro
'''
def extract_basic(destination: Path) -> tuple[bytes, str]:
with tarfile.open(ARCHIVE, "r:gz") as archive:
member = next(
item for item in archive.getmembers() if item.name.endswith(SOURCE_SUFFIX)
)
handle = archive.extractfile(member)
if handle is None:
raise RuntimeError(member.name)
raw = handle.read()
target = destination / "FormalProblemSolving" / "Basic.lean"
target.parent.mkdir(parents=True)
target.write_bytes(raw)
return raw, member.name
def main() -> None:
lean = shutil.which("lean") or "/Users/sshpro/.elan/bin/lean"
if not Path(lean).exists():
raise RuntimeError("Lean 4.15.0 executable is unavailable")
with tempfile.TemporaryDirectory(prefix="fps-source-audit-") as temp:
root = Path(temp)
raw, member_name = extract_basic(root)
# Basic.lean imports Mathlib, but all of its declarations use Lean's
# bundled API. This shim makes that dependency boundary explicit.
(root / "Mathlib.lean").write_text("import Lean\n", encoding="utf-8")
audit = root / "official_audit.lean"
audit.write_text(AUDIT, encoding="utf-8")
environment = os.environ.copy()
environment["LEAN_PATH"] = str(root)
compile_shim = subprocess.run(
[lean, "-o", str(root / "Mathlib.olean"), str(root / "Mathlib.lean")],
cwd=root,
env=environment,
capture_output=True,
text=True,
check=False,
)
if compile_shim.returncode:
raise RuntimeError(
"Mathlib dependency shim compilation failed\n"
+ compile_shim.stdout
+ compile_shim.stderr
)
basic = root / "FormalProblemSolving" / "Basic.lean"
compile_basic = subprocess.run(
[lean,
"-o",
str(root / "FormalProblemSolving" / "Basic.olean"),
str(basic)],
cwd=root,
env=environment,
capture_output=True,
text=True,
check=False,
)
if compile_basic.returncode:
raise RuntimeError(
"official Basic.lean compilation failed\n"
+ compile_basic.stdout
+ compile_basic.stderr
)
command = [lean, str(audit)]
completed = subprocess.run(
command,
cwd=root,
env=environment,
capture_output=True,
text=True,
check=False,
)
if completed.returncode:
raise RuntimeError(
"official Basic.lean audit failed\n"
+ completed.stdout
+ completed.stderr
)
version = subprocess.run(
[lean, "--version"], capture_output=True, text=True, check=True
).stdout.strip()
print(
json.dumps(
{
"basic_member": member_name,
"basic_sha256": hashlib.sha256(raw).hexdigest(),
"lean": version,
"exit_status": completed.returncode,
"tests": [
"official Basic.lean imported byte-for-byte",
"source-level FPS problem/solve macro",
"source-level DFPS problem/find_all/solve macro",
"kernel-checked ProblemSol soundness projection",
"kernel-checked forward and backward find-all directions",
],
},
sort_keys=True,
)
)
if __name__ == "__main__":
main()
|