Files
lean-bench/lib/Goodman/Chapter01Section06.lean
T
claude-bot 69c5b788b9 Generate the citable premise database, and goal-driven retrieval over it
The database was the missing half: extraction and assembly existed, but
nothing turned the book's main-content statements into something an agent
could actually cite mid-proof, and nothing searched it. Two new scripts.

textbook/build_database.py takes the main-content statements and emits
both halves of a usable premise library:
- an importable Lean file where every entry is a named declaration, so a
  proof can cite goodman_1_6_20 by name;
- a JSONL search index carrying each entry's name, statement, mentioned
  identifiers and status.

An entry is emitted as an `axiom` (asserted from the textbook) until a
real lean-bench run proves it; passing --results upgrades those entries
to real theorems carrying the exact verified proof. This mirrors how the
book itself works -- its exercises are posed assuming its body results,
the same way using Mathlib does not mean re-proving Mathlib. The cost is
explicit: anything proven through an axiom inherits it, so a garbled
extraction asserted as an axiom makes downstream proofs worthless.
Guards: --require-checked refuses entries that have not passed
check_statements.py (all 21 section 1.6 entries have, and are now marked
accordingly), and the verifier's existing #print axioms check surfaces
what a finished proof rests on.

textbook/search_database.py is the retrieval step the proof loop calls at
each line: given the current goal, it ranks premises by identifier and
symbol overlap, weighting rare identifiers far above common ones
(IsGCDOfList matching means much more than Int matching), preferring a
proven premise over an asserted one, and formats the top few for the
prompt. Verified against three real goals: the exactly-applicable premise
tops each ranking by a wide margin (11.4 vs 1.6 for the gcd goal, 5.1 vs
3.7 for the prime-divides-product goal). It is a ranking heuristic, not a
decision procedure -- only Lean can say whether a premise applies.
Replacing score_premise() is the whole job of swapping in embeddings.

Also: renamed library_candidates/ to database/ to match what it is;
generated lib/Goodman/Chapter01Section06.lean (21 premises, 0 proven, 21
asserted) and database/goodman-1.6.index.jsonl; noted a latent bug in the
older build_library.py, which attaches each entry's non-import prelude
per-theorem and so would emit a shared `def` twice in one file --
build_database.py hoists and dedupes, and has a test pinning that.
13 new tests, all green (33 total, the one failure remains the
pre-existing unrelated lean_agent import error in test_dataset.py).

Still not done, and worth being clear about: nothing has been proven yet.
The bench targets have not been elaboration-checked, no lean-bench run
has happened over either file, and the goal-directed loop that would
consume this retrieval does not exist -- lean-bench's solve() is still
single-shot. Separately, lean-bench cannot currently run at all in this
checkout, because lean_bench/cli.py imports lean_agent at module load and
lean-agent is not installed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vbLFFJiHSZqGZHEkjvG5x
2026-09-18 19:36:18 +00:00

74 lines
8.4 KiB
Lean4
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Generated by textbook/build_database.py -- do not hand-edit.
-- Citable premise database from the textbook's main content.
-- `axiom` entries are asserted from the book, NOT proven here: anything
-- proven through one inherits it, so check `#print axioms` downstream.
import Mathlib
def IsGCDOfList (d : ℕ) (as : List ℤ) : Prop :=
(∀ a ∈ as, (d:ℤ) ∣ a) ∧ ∀ e : ℤ, (∀ a ∈ as, e ∣ a) → e ∣ (d:ℤ)
/-- Proposition 1.6.1 (Goodman, Algebra: Abstract and Concrete). Addition and multiplication on the integers are commutative and associative, 0 and 1 are their respective identities, the distributive law holds, and a product of integers is nonzero iff both factors are nonzero. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_1 : ∀ (a b c : ℤ), (a + b = b + a) ∧ (a + (b + c) = (a + b) + c) ∧ (0 + a = a) ∧ (a + (-a) = 0) ∧ (a * b = b * a) ∧ (a * (b * c) = (a * b) * c) ∧ (1 * a = a) ∧ (a * (b + c) = a * b + a * c) ∧ (∀ (x y : ℤ), x * y ≠ 0 → x ≠ 0 ∧ y ≠ 0)
/-- Proposition 1.6.4 (Goodman, Algebra: Abstract and Concrete). Any natural number greater than 1 can be written as a product of prime numbers. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_4 : ∀ (n : ℕ), n > 1 → ∃ (p : ℕ) (h : Nat.Prime p), p ∣ n
/-- Theorem 1.6.6 (Goodman, Algebra: Abstract and Concrete). There are infinitely many prime numbers. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_6 : ∃ (p : ℕ → ℕ), ∀ (n : ℕ), Nat.Prime (p n) ∧ ∀ (i j : ℕ), i ≠ j → p i ≠ p j
/-- Proposition 1.6.7 (Goodman, Algebra: Abstract and Concrete). Given integers a and d with d ≥ 1, there exist unique integers q and r such that a = qd + r and 0 ≤ r < d. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_7 : ∀ (a d : ℤ), d ≥ 1 → ∃! qr : ℤ × ℤ, a = qr.1 * d + qr.2 ∧ 0 ≤ qr.2 ∧ qr.2 < d
/-- Proposition 1.6.9 (Goodman, Algebra: Abstract and Concrete). I(m,n) = {am+bn} is closed under addition and negation, absorbs multiplication by any integer, and any common divisor of m and n divides every element of I(m,n). ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_9 : ∀ (m n : ℤ), let I := {v : ℤ | ∃ a b : ℤ, v = a * m + b * n}; (∀ x ∈ I, ∀ y ∈ I, x + y ∈ I) ∧ (∀ x ∈ I, -x ∈ I) ∧ (∀ x : ℤ, ∀ z ∈ I, x * z ∈ I) ∧ (∀ b : ℤ, b ∣ m ∧ b ∣ n → ∀ z ∈ I, b ∣ z)
/-- Lemma 1.6.10 (Goodman, Algebra: Abstract and Concrete). If a natural number d is a common divisor of m and n and an element of I(m, n), then d is the greatest common divisor of m and n. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_10 : ∀ (m n d : ℤ), d ∣ m ∧ d ∣ n ∧ d ∈ {v : ℤ | ∃ a b : ℤ, v = a * m + b * n} → ∀ (x : ℤ), x ∣ m ∧ x ∣ n → x ∣ d
/-- Proposition 1.6.11 (Goodman, Algebra: Abstract and Concrete). The natural number d, which is the greatest common divisor of the absolute values of m and n, is also the greatest common divisor of m and n, and d is an element of I(m, n). ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_11 : ∀ (m n : ℤ), m ≠ 0 ∧ n ≠ 0 → let d := Nat.gcd (Int.natAbs m) (Int.natAbs n); (d : ℤ) = Int.gcd m n ∧ (d : ℤ) ∈ {v : ℤ | ∃ a b : ℤ, v = a * m + b * n}
/-- Corollary 1.6.13 (Goodman, Algebra: Abstract and Concrete). gcd(m,n) is the least positive element of I(m,n), and I(m,n) is exactly the set of integer multiples of gcd(m,n). ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_13 : ∀ (m n : ℤ), m ≠ 0 → n ≠ 0 → let I := {v : ℤ | ∃ a b : ℤ, v = a * m + b * n}; let d := Int.gcd m n; IsLeast {x : ℤ | 0 < x ∧ x ∈ I} (d : ℤ) ∧ I = {v : ℤ | ∃ k : ℤ, v = k * d}
/-- Corollary 1.6.15 (Goodman, Algebra: Abstract and Concrete). Two nonzero integers m and n are relatively prime if and only if there exist integers s and t such that sm + tn = 1. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_15 : ∀ (m n : ℤ), m ≠ 0 ∧ n ≠ 0 → Int.gcd m n = 1 ↔ ∃ (s t : ℤ), s * m + t * n = 1
/-- Corollary 1.6.17 (Goodman, Algebra: Abstract and Concrete). If a and b are relatively prime natural numbers and both divide an integer x, then their product ab also divides x. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_17 : ∀ (a b : ℕ) (x : ℤ), Nat.Coprime a b ∧ (a:ℤ) ∣ x ∧ (b:ℤ) ∣ x → (a:ℤ) * b ∣ x
/-- Proposition 1.6.18 (Goodman, Algebra: Abstract and Concrete). If p is a prime number and a is any nonzero integer, then either p divides a or p and a are relatively prime. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_18 : ∀ (p : ℕ) (a : ℤ), Nat.Prime p ∧ a ≠ 0 → (p:ℤ) ∣ a ∨ IsCoprime (p:ℤ) a
/-- Proposition 1.6.19 (Goodman, Algebra: Abstract and Concrete). If p is a prime number and a and b are nonzero integers, and p divides the product ab, then p divides a or p divides b. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_19 : ∀ (p : ℕ) (a b : ℤ), Nat.Prime p ∧ a ≠ 0 ∧ b ≠ 0 → (p:ℤ) ∣ a * b → (p:ℤ) ∣ a ∨ (p:ℤ) ∣ b
/-- Corollary 1.6.20 (Goodman, Algebra: Abstract and Concrete). If a prime number p divides a product of nonzero integers, then p divides one of the factors. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_20 : ∀ (p : ℕ) (as : List ℤ), Nat.Prime p → (∀ (a : ℤ), a ∈ as → a ≠ 0) → (p:ℤ) ∣ List.prod as → ∃ (a : ℤ), a ∈ as ∧ (p:ℤ) ∣ a
/-- Theorem 1.6.21 (Goodman, Algebra: Abstract and Concrete). The prime factorization of a natural number is unique. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_21 : ∀ (n : ℕ), n > 1 → ∃! (ps : List ℕ), (∀ (p : ℕ), p ∈ ps → Nat.Prime p) ∧ n = List.prod ps
/-- Lemma 1.6.23 (Goodman, Algebra: Abstract and Concrete). Given n >= 2 nonzero integers a_1..a_n, there is a natural number d and a unimodular (det a unit, i.e. det = +-1) integer n-by-n matrix Q such that the row vector a times Q equals (d, 0, ..., 0). ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_23 : ∀ (n : ℕ) (a : Fin n → ℤ), 2 ≤ n → (∀ i, a i ≠ 0) → ∃ (d : ℕ) (Q : Matrix (Fin n) (Fin n) ℤ), IsUnit Q.det ∧ ∀ j : Fin n, Matrix.vecMul a Q j = if j.val = 0 then (d : ℤ) else 0
/-- Proposition 1.6.24 (Goodman, Algebra: Abstract and Concrete). The greatest common divisor of a list of at least two nonzero integers exists and is an integer linear combination of the integers in the list. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_24 : ∀ (as : List ℤ), as.length ≥ 2 → (∀ (a : ℤ), a ∈ as → a ≠ 0) → ∃ (d : ℕ) (cs : List ℤ), IsGCDOfList d as ∧ (d:ℤ) = List.sum (List.zipWith (· * ·) cs as)
/-- Lemma 1.6.26 (Goodman, Algebra: Abstract and Concrete). If a list of integers is pairwise relatively prime and each integer in the list divides an integer z, then the product of the integers in the list also divides z. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_26 : ∀ (as : List ℤ) (z : ℤ), as.length ≥ 2 → (∀ (i j : ℕ), i ≠ j → i < as.length → j < as.length → IsCoprime (as[i]) (as[j])) → (∀ (a : ℤ), a ∈ as → a ≠ 0) → (∀ (a : ℤ), a ∈ as → a ∣ z) → List.prod as ∣ z
/-- Proposition 1.6.2 (Goodman, Algebra: Abstract and Concrete). If two integers multiply to 1, both are 1 or both are -1. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_2_a : ∀ (u v : ℤ), u * v = 1 → (u = 1 ∧ v = 1) ∨ (u = -1 ∧ v = -1)
/-- Proposition 1.6.2 (Goodman, Algebra: Abstract and Concrete). If two integers divide each other, they are equal or negatives of each other. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_2_b : ∀ (a b : ℤ), a ∣ b ∧ b ∣ a → a = b ∨ a = -b
/-- Proposition 1.6.2 (Goodman, Algebra: Abstract and Concrete). Divisibility of integers is transitive. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_2_c : ∀ (a b c : ℤ), a ∣ b ∧ b ∣ c → a ∣ c
/-- Proposition 1.6.2 (Goodman, Algebra: Abstract and Concrete). If an integer divides two others, it divides any integer linear combination of them. ASSERTED from the textbook, not proven here. -/
axiom goodman_1_6_2_d : ∀ (a b c s t : ℤ), a ∣ b ∧ a ∣ c → a ∣ (s * b + t * c)