Files
skills/public/formal-lean-mathlib4.html
zeekayandHanzo Dev 9e10c1c10e Each llms index points at the other two
hanzoskills.com/llms.txt now opens with the install line, the API
reference index and the company index, so an agent that lands on any
one of the three can find the rest.

robots.txt claimed 537 documents while llms.txt counts 920. The number
is gone from robots.txt; llms.txt derives it.

public/ is regenerated from scripts/site.py, which it had fallen
behind: the copy-prompt menu and agent bar were in the generator and
not in the committed pages. tests/site.mjs passes against the result.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-11 00:27:59 -07:00

699 lines
31 KiB
HTML
Raw Permalink 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.
<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>formal-lean-mathlib4</title>
<meta name="color-scheme" content="dark">
<style>
/* Committed to one visual world: a dark instrument panel. No light variant —
the dot field and the hairline rules only read on black, and a naive
inversion would wash both out. Every colour is painted explicitly so the page
never borrows a host background. */
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
:root{
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
--font-zen-pixel-square:'Zen Pixel Square',monospace;
--mono:var(--font-zen-mono);
--sans:var(--font-zen-sans);
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
--r:999px; --r-soft:14px;
}
*{box-sizing:border-box}
html{-webkit-text-size-adjust:100%}
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
/* The dot field. A halftone made of one repeating radial-gradient, faded out
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
/* No overflow clip here: the dot field is inset to the box already, and a clip
would cut off the copy-prompt menu. */
.hero{position:relative;border-bottom:1px solid var(--line)}
.hero::before{content:"";position:absolute;inset:0;
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
background-size:7px 7px;
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
pointer-events:none}
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
nav{border-bottom:1px solid var(--line)}
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
justify-content:space-between;align-items:center;gap:1rem}
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--ink);text-decoration:none;
display:inline-flex;align-items:center;gap:.6rem}
.brand i{font-style:normal;color:var(--faint)}
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--soft);text-decoration:none}
nav a.up:hover{color:var(--ink)}
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
h1 em{font-style:normal;color:var(--faint)}
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
line-height:1.2;font-weight:400}
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
.btn.solid:hover{background:#d8d8d4}
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
/* Copy-prompt selector. Each row copies the prompt for its own target. */
.copy{position:relative;display:inline-flex}
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
padding:.4rem;display:flex;flex-direction:column;
box-shadow:0 12px 32px rgba(0,0,0,.75)}
.menu[hidden]{display:none}
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
white-space:nowrap}
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
.mi:hover .mark{color:var(--ink)}
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
letter-spacing:.1em;text-transform:uppercase}
/* Works-with bar: one scrolling row, name and mark per agent. */
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
display:flex;align-items:center}
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
-webkit-overflow-scrolling:touch;
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
.bar::-webkit-scrollbar{display:none}
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
text-decoration:none;background:var(--ground)}
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
.agent .mark{width:.95rem;height:.95rem}
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
p{margin:0 0 1rem;max-width:70ch}
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
li{margin:.22rem 0}
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
text-decoration-color:var(--line-hi)}
a:hover{text-decoration-color:var(--ink)}
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
display:block;overflow-x:auto}
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
.curl{display:block;background:var(--panel);border:1px solid var(--line);
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
white-space:pre;line-height:1.9}
.curl b{color:var(--ink);font-weight:400}
.curl i{font-style:normal;color:var(--faint)}
/* index */
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
#q:focus{outline:none;border-color:var(--soft)}
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
text-transform:uppercase;margin:0 0 2rem}
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
padding-bottom:.45rem}
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
border-radius:var(--r-soft);text-decoration:none}
.row:hover{background:#0a0a0a}
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
color:var(--faint);font-size:.85rem}
footer a{color:var(--soft)}
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
margin:0 0 2rem;font-size:.85rem}
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
border-bottom:1px solid var(--line);padding-bottom:.4rem}
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
.fmap a:hover{text-decoration:underline}
.fmap div{color:var(--faint);line-height:1.5}
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
@media (max-width:640px){
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
.btns{gap:.5rem}
.btn{padding:.75rem 1.15rem;font-size:.68rem}
.copy,.copy .btn{width:100%;justify-content:center}
.menu{left:0;right:0;min-width:0}
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
.strip b{border-right:0;padding-right:0}
.bar{width:100%}
}
</style>
<link rel="alternate" type="text/markdown" href="/formal-lean-mathlib4.md">
<link rel="icon" href="/favicon.svg">
<link rel="canonical" href="https://hanzoskills.com/formal-lean-mathlib4">
</head>
<!-- Reading this with a machine? Every page here is also plain markdown.
This one: https://hanzoskills.com/formal-lean-mathlib4.md
Index: https://hanzoskills.com/llms.txt
Corpus: https://hanzoskills.com/all.md
Append .md to any path for the markdown twin. -->
<body>
<nav><div class="in">
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
<a class="up" href="/">All skills</a>
</div></nav>
<div class="wrap">
<h1>formal-lean-mathlib4</h1><p class="lede">Formalizing mathematics using existing results</p><h1>Lean 4 mathlib4 Library</h1>
<h2>Overview</h2>
<p>Comprehensive guide to using, navigating, and contributing to mathlib4, Lean 4&#x27;s extensive mathematics library covering algebra, analysis, topology, category theory, and more.</p>
<h2>Scope</h2>
<p><strong>Included:</strong></p>
<ul><li>mathlib4 structure and organization</li><li>Library search strategies</li><li>Import management</li><li>Type class system and instance synthesis</li><li>Naming conventions</li><li>Common mathematical domains (algebra, analysis, topology)</li><li>Contributing to mathlib4</li><li>Performance and compilation best practices</li></ul>
<p><strong>Excluded:</strong></p>
<ul><li>Basic proof tactics (see <code>lean-proof-basics.md</code>)</li><li>Advanced tactic development (see <code>lean-tactics.md</code>)</li><li>Formalization architecture (see <code>lean-theorem-proving.md</code>)</li><li>Lean 3 mathlib (outdated)</li></ul>
<h2>When to Use This Skill</h2>
<p><strong>Primary use cases:</strong></p>
<ul><li>Formalizing mathematics using existing results</li><li>Finding theorems and definitions in mathlib4</li><li>Understanding type class hierarchies</li><li>Setting up mathlib4 projects</li><li>Contributing definitions and theorems</li><li>Resolving import and instance issues</li></ul>
<p><strong>Indicators you need this:</strong></p>
<ul><li>&quot;How do I find theorem X in mathlib4?&quot;</li><li>&quot;What&#x27;s the right import for this?&quot;</li><li>&quot;How do I get this instance to synthesize?&quot;</li><li>&quot;What&#x27;s the mathlib naming convention?&quot;</li><li>&quot;How do I contribute to mathlib4?&quot;</li></ul>
<p><strong>When to use other skills instead:</strong></p>
<ul><li>Learning basic proofs → <code>lean-proof-basics.md</code></li><li>Advanced tactics → <code>lean-tactics.md</code></li><li>Formalization strategy → <code>lean-theorem-proving.md</code></li></ul>
<h2>Prerequisites</h2>
<p><strong>Required knowledge:</strong></p>
<ul><li>Lean 4 basics (see <code>lean-proof-basics.md</code>)</li><li>Basic mathematics (domain-dependent)</li><li>Type classes and instance resolution</li></ul>
<p><strong>Required setup:</strong></p>
<pre><code class="lang-bash"># Create project with mathlib4
lake +leanprover/lean4:nightly-2024-01-15 new my_project math
cd my_project
# Get mathlib4 cache (fast!)
lake exe cache get
# Build project
lake build
# Update mathlib4
lake update mathlib
lake exe cache get
lake build</code></pre>
<p><strong>Key files:</strong></p>
<ul><li><code>lakefile.lean</code>: Project configuration</li><li><code>lean-toolchain</code>: Lean version</li><li><code>lake-manifest.json</code>: Dependency lock file</li></ul>
<h2>Core Concepts</h2>
<h3>1. Library Organization</h3>
<p>mathlib4 is organized by mathematical domain.</p>
<pre><code>Mathlib/
├── Algebra/ # Algebraic structures
│ ├── Group/ # Groups, subgroups
│ ├── Ring/ # Rings, ideals
│ └── Field/ # Fields
├── Analysis/ # Real/complex analysis
│ ├── Calculus/ # Differentiation, integration
│ └── Topology/ # Topological spaces
├── Data/ # Data structures
│ ├── List/ # List operations
│ ├── Finset/ # Finite sets
│ └── Set/ # Set theory
├── Logic/ # Logic and foundations
├── NumberTheory/ # Number theory
├── Topology/ # General topology
└── CategoryTheory/ # Category theory</code></pre>
<h3>2. Naming Conventions</h3>
<p>mathlib4 follows strict naming conventions.</p>
<p><strong>Structure:</strong></p>
<pre><code>[namespace.]subject_description[_variant]</code></pre>
<p><strong>Examples:</strong></p>
<pre><code class="lang-lean">-- Theorems about operations
Nat.add_comm : ∀ n m, n + m = m + n
Nat.add_assoc : ∀ n m k, (n + m) + k = n + (m + k)
Nat.mul_comm : ∀ n m, n * m = m * n
-- Type class instances
instAddNat : Add Nat
instMulNat : Mul Nat
-- Lemmas with variants
List.append_nil : xs ++ [] = xs
List.nil_append : [] ++ xs = xs</code></pre>
<p><strong>Patterns:</strong></p>
<ul><li><code>_comm</code>: Commutativity</li><li><code>_assoc</code>: Associativity</li><li><code>_left</code>/<code>_right</code>: Left/right versions</li><li><code>_iff</code>: Equivalence</li><li><code>_of_</code>: Implication (← direction)</li><li><code>_inv</code>: Inverse operation</li><li><code>_neg</code>: Negation</li></ul>
<h3>3. Type Classes</h3>
<p>mathlib4 uses type classes extensively.</p>
<pre><code class="lang-lean">-- Algebraic hierarchy
class Add (α : Type u) where
add : α → α → α
class Semigroup (α : Type u) extends Mul α where
mul_assoc : ∀ a b c : α, (a * b) * c = a * (b * c)
class Monoid (α : Type u) extends Semigroup α, One α where
one_mul : ∀ a : α, 1 * a = a
mul_one : ∀ a : α, a * 1 = a
class Group (α : Type u) extends Monoid α, Inv α where
inv_mul_cancel : ∀ a : α, a⁻¹ * a = 1</code></pre>
<p><strong>Using type classes:</strong></p>
<pre><code class="lang-lean">-- Generic theorem using type classes
theorem mul_left_cancel [Group α] {a b c : α} (h : a * b = a * c) : b = c := by
have : a⁻¹ * (a * b) = a⁻¹ * (a * c) := by rw [h]
simp [mul_assoc, inv_mul_cancel] at this
exact this
-- Instance synthesis
#check (1 + 1 : Nat) -- Uses instAddNat
#check (1 * 1 : Int) -- Uses instMulInt</code></pre>
<h3>4. Import Management</h3>
<p>Organize imports for fast compilation.</p>
<pre><code class="lang-lean">-- Import specific modules
import Mathlib.Algebra.Group.Defs
import Mathlib.Data.List.Basic
-- Import whole directories (slower)
import Mathlib.Algebra.Group
-- Import everything (very slow, avoid!)
import Mathlib</code></pre>
<h2>Library Search Strategies</h2>
<h3>Strategy 1: Exact Name Search</h3>
<p>Use VS Code autocomplete and <code>#check</code>.</p>
<pre><code class="lang-lean">-- Autocomplete: type partial name
#check Nat.add_ -- Shows: add_comm, add_assoc, add_zero, ...
-- Check full name
#check List.append_nil
#check Ring.mul_comm</code></pre>
<h3>Strategy 2: Docstring Search</h3>
<p>Use Lean 4 web docs.</p>
<pre><code>1. Visit: https://leanprover-community.github.io/mathlib4_docs/
2. Search: &quot;continuous&quot; → finds continuity definitions
3. Browse: Mathlib.Topology.Constructions</code></pre>
<h3>Strategy 3: Type-Based Search</h3>
<p>Use <code>exact?</code> tactic (formerly <code>library_search</code>).</p>
<pre><code class="lang-lean">example (a b : Nat) : a + b = b + a := by
exact?
-- Try this: exact Nat.add_comm a b
example [Group G] (a : G) : a * a⁻¹ = 1 := by
exact?
-- Try this: exact mul_inv_cancel a</code></pre>
<h3>Strategy 4: Pattern Matching Search</h3>
<p>Use <code>apply?</code> for applicable theorems.</p>
<pre><code class="lang-lean">example (xs ys : List α) : (xs ++ ys).length = xs.length + ys.length := by
apply?
-- Try this: exact List.length_append xs ys</code></pre>
<h3>Strategy 5: Local Context Search</h3>
<p>Use <code>assumption</code> and <code>simp</code>.</p>
<pre><code class="lang-lean">example (h : p) : p := by
assumption -- Searches local context
example : 0 + n = n := by
simp -- Searches simp lemmas</code></pre>
<h2>Common Mathematical Domains</h2>
<h3>Domain 1: Algebra</h3>
<pre><code class="lang-lean">import Mathlib.Algebra.Group.Defs
import Mathlib.Algebra.Ring.Defs
-- Groups
example [Group G] (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := by
exact mul_inv_rev a b
-- Rings
example [Ring R] (a b : R) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by
ring
-- Fields
example [Field F] (a b : F) (h : b ≠ 0) : a / b * b = a := by
field_simp</code></pre>
<h3>Domain 2: Analysis</h3>
<pre><code class="lang-lean">import Mathlib.Analysis.Calculus.Deriv.Basic
import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic
-- Continuity
example : Continuous (fun x : ℝ =&gt; x ^ 2) := by
exact continuous_pow 2
-- Derivatives
example : deriv (fun x : ℝ =&gt; x ^ 2) = (fun x =&gt; 2 * x) := by
ext x
simp [deriv_pow]</code></pre>
<h3>Domain 3: Topology</h3>
<pre><code class="lang-lean">import Mathlib.Topology.Basic
import Mathlib.Topology.MetricSpace.Basic
-- Open sets
example [TopologicalSpace X] (s : Set X) (hs : IsOpen s) :
IsOpen sᶜ ↔ IsClosed s := by
exact isOpen_compl_iff
-- Metric spaces
example [MetricSpace X] (x y : X) : dist x y = 0 ↔ x = y := by
exact dist_eq_zero</code></pre>
<h3>Domain 4: Data Structures</h3>
<pre><code class="lang-lean">import Mathlib.Data.List.Basic
import Mathlib.Data.Finset.Basic
import Mathlib.Data.Set.Basic
-- Lists
example (xs ys : List α) : (xs ++ ys).reverse = ys.reverse ++ xs.reverse := by
exact List.reverse_append xs ys
-- Finite sets
example (s t : Finset α) : s ∪ t = t ∪ s := by
exact Finset.union_comm
-- Sets
example (s t : Set α) : s ∩ t = t ∩ s := by
exact Set.inter_comm s t</code></pre>
<h3>Domain 5: Number Theory</h3>
<pre><code class="lang-lean">import Mathlib.NumberTheory.Divisors
import Mathlib.Data.Nat.Prime.Basic
-- Primes
example : Nat.Prime 7 := by
norm_num
-- Divisibility
example (a b c : Nat) (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by
exact Nat.dvd_trans h1 h2</code></pre>
<h2>Type Class Patterns</h2>
<h3>Pattern 1: Instance Synthesis</h3>
<p>Let Lean infer type class instances.</p>
<pre><code class="lang-lean">-- Automatic instance synthesis
example [Add α] (a b : α) : α := a + b -- Uses [Add α]
-- Manual instance specification (rare)
example : Nat := @Add.add Nat instAddNat 1 2
-- Check instance
#check (inferInstance : Add Nat)</code></pre>
<h3>Pattern 2: Instance Diamonds</h3>
<p>mathlib4 uses careful design to avoid instance diamonds.</p>
<pre><code class="lang-lean">-- Safe: single path to instance
class A (α : Type)
class B (α : Type) extends A α
class C (α : Type) extends A α
-- No D extending both B and C (would create diamond)
-- If diamonds exist, use priority
instance [A α] : B α := sorry
instance (priority := 100) [A α] : C α := sorry</code></pre>
<h3>Pattern 3: Outparam and Infer Instance</h3>
<pre><code class="lang-lean">-- Outparam: output parameter (determined by other args)
class HMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hMul : α → β → γ
-- Example: Nat * Nat = Nat
instance : HMul Nat Nat Nat where
hMul := Nat.mul
-- Infer instance in structures
structure Point (α : Type) [Add α] where
x : α
y : α
add (other : Point α) : Point α :=
⟨x + other.x, y + other.y⟩ -- Uses [Add α]</code></pre>
<h2>Import Best Practices</h2>
<h3>Strategy 1: Minimal Imports</h3>
<p>Only import what you need.</p>
<pre><code class="lang-lean">-- BAD: Import everything (slow!)
import Mathlib
-- GOOD: Import specific modules
import Mathlib.Algebra.Group.Defs
import Mathlib.Data.List.Basic</code></pre>
<h3>Strategy 2: Import Organization</h3>
<p>Group related imports.</p>
<pre><code class="lang-lean">-- Core Lean 4
import Lean
import Std
-- mathlib4: Data structures
import Mathlib.Data.List.Basic
import Mathlib.Data.Finset.Basic
-- mathlib4: Algebra
import Mathlib.Algebra.Group.Defs
import Mathlib.Algebra.Ring.Defs
-- mathlib4: Topology
import Mathlib.Topology.Basic</code></pre>
<h3>Strategy 3: Precompiled Imports</h3>
<p>Use precompiled modules for speed.</p>
<pre><code class="lang-lean">-- Create precompiled import file
-- In MyProject/Imports.lean:
import Mathlib.Algebra.Group.Defs
import Mathlib.Algebra.Ring.Defs
import Mathlib.Data.List.Basic
-- In other files:
import MyProject.Imports</code></pre>
<h2>Contributing to mathlib4</h2>
<h3>Contribution Workflow</h3>
<pre><code class="lang-bash"># 1. Fork mathlib4 on GitHub
# 2. Clone your fork
git clone https://github.com/YOUR_USERNAME/mathlib4.git
cd mathlib4
# 3. Create branch
git checkout -b my-contribution
# 4. Make changes
# Edit files, add theorems
# 5. Build and test
lake exe cache get
lake build
# 6. Lint
./scripts/lint-style.sh
# 7. Commit and push
git add .
git commit -m &quot;feat: add theorem about groups&quot;
git push origin my-contribution
# 8. Create PR on GitHub</code></pre>
<h3>Style Guidelines</h3>
<pre><code class="lang-lean">-- Use snake_case for theorems
theorem nat_add_comm (n m : Nat) : n + m = m + n := Nat.add_comm n m
-- Document with docstrings
/-- The sum of two natural numbers is commutative. -/
theorem add_comm (n m : Nat) : n + m = m + n := Nat.add_comm n m
-- Use type classes
theorem mul_comm [CommMagma α] (a b : α) : a * b = b * a := CommMagma.mul_comm a b
-- Prefer term mode for simple proofs
theorem zero_add (n : Nat) : 0 + n = n := Nat.zero_add n
-- Use tactic mode for complex proofs
theorem complex_theorem : P := by
intro h
cases h
· sorry
· sorry</code></pre>
<h3>Naming Conventions</h3>
<pre><code class="lang-lean">-- Operations
add, mul, neg, inv, sub, div
-- Properties
comm, assoc, left_distrib, right_distrib
zero, one, inv_mul_cancel
-- Implications
of (← direction), iff (↔)
-- Variants
left, right, neg, inv</code></pre>
<h2>Quick Reference</h2>
<h3>Common Imports</h3>
<pre><code class="lang-lean">-- Algebra
import Mathlib.Algebra.Group.Defs
import Mathlib.Algebra.Ring.Defs
import Mathlib.Algebra.Field.Defs
-- Data structures
import Mathlib.Data.List.Basic
import Mathlib.Data.Finset.Basic
import Mathlib.Data.Set.Basic
-- Topology
import Mathlib.Topology.Basic
import Mathlib.Topology.MetricSpace.Basic
-- Analysis
import Mathlib.Analysis.Calculus.Deriv.Basic
import Mathlib.Analysis.Calculus.FDeriv.Basic
-- Number theory
import Mathlib.NumberTheory.Divisors
import Mathlib.Data.Nat.Prime.Basic</code></pre>
<h3>Instance Resolution</h3>
<pre><code class="lang-lean">#check (inferInstance : Add Nat) -- Find instance
#synth Add Nat -- Synthesize instance
set_option trace.Meta.synthInstance true -- Debug synthesis</code></pre>
<h3>Library Search</h3>
<pre><code class="lang-lean">exact? -- Find exact proof in library
apply? -- Find applicable theorem
simp? -- Show which simp lemmas used
#check_failure expr -- Why doesn&#x27;t expr typecheck?</code></pre>
<h2>Anti-Patterns</h2>
<h3>Anti-Pattern 1: Reinventing Wheels</h3>
<pre><code class="lang-lean">-- BAD: Define your own addition commutativity
theorem my_add_comm (n m : Nat) : n + m = m + n := by
induction n &lt;;&gt; simp [Nat.add_succ, *]
-- GOOD: Use mathlib
#check Nat.add_comm -- Already exists!</code></pre>
<h3>Anti-Pattern 2: Ignoring Naming Conventions</h3>
<pre><code class="lang-lean">-- BAD: Non-standard names
theorem addComm (n m : Nat) : n + m = m + n := Nat.add_comm n m
theorem AdditionCommutative : ∀ n m, n + m = m + n := Nat.add_comm
-- GOOD: Follow conventions
theorem add_comm (n m : Nat) : n + m = m + n := Nat.add_comm n m</code></pre>
<h3>Anti-Pattern 3: Poor Import Organization</h3>
<pre><code class="lang-lean">-- BAD: Single massive import
import Mathlib
-- BAD: Disorganized imports
import Mathlib.Topology.Basic
import Mathlib.Data.List.Basic
import Mathlib.Algebra.Group.Defs
import Mathlib.Data.Finset.Basic
-- GOOD: Organized by domain
-- Data structures
import Mathlib.Data.List.Basic
import Mathlib.Data.Finset.Basic
-- Algebra
import Mathlib.Algebra.Group.Defs
-- Topology
import Mathlib.Topology.Basic</code></pre>
<h3>Anti-Pattern 4: Over-Specified Instances</h3>
<pre><code class="lang-lean">-- BAD: Manually specify instances
example : Nat := @Add.add Nat instAddNat 1 2
-- GOOD: Let Lean infer
example : Nat := 1 + 2</code></pre>
<h3>Anti-Pattern 5: Not Using Library Search</h3>
<pre><code class="lang-lean">-- BAD: Implement proof from scratch
theorem list_append_nil (xs : List α) : xs ++ [] = xs := by
induction xs with
| nil =&gt; rfl
| cons x xs ih =&gt; simp [List.append, ih]
-- GOOD: Search first
theorem list_append_nil (xs : List α) : xs ++ [] = xs := by
exact?
-- Try this: exact List.append_nil xs</code></pre>
<h3>Anti-Pattern 6: Ignoring Type Class Hierarchy</h3>
<pre><code class="lang-lean">-- BAD: Duplicate type class constraints
theorem bad [Add α] [Monoid α] (a b : α) : a + b = b + a := by
sorry -- Monoid doesn&#x27;t imply commutativity anyway!
-- GOOD: Use correct type class
theorem good [AddCommMonoid α] (a b : α) : a + b = b + a :=
add_comm a b</code></pre>
<h2>Related Skills</h2>
<ul><li><strong>lean-proof-basics.md</strong>: Fundamental proof techniques</li><li><strong>lean-tactics.md</strong>: Advanced tactics and automation</li><li><strong>lean-theorem-proving.md</strong>: Formalization strategies</li><li><strong>lean-metaprogramming.md</strong>: Custom tactics and elaborators</li></ul>
<h2>References</h2>
<ul><li><a href="https://leanprover-community.github.io/mathlib4_docs/">mathlib4 Documentation</a></li><li><a href="https://github.com/leanprover-community/mathlib4">mathlib4 GitHub</a></li><li><a href="https://leanprover-community.github.io/contribute/index.html">Contributing Guide</a></li><li><a href="https://leanprover-community.github.io/contribute/naming.html">mathlib4 Naming Conventions</a></li><li><a href="https://leanprover-community.github.io/undergrad.html">Undergrad Math in Lean</a></li></ul>
<h2>Examples</h2>
<h3>Example 1: Using Algebraic Hierarchy</h3>
<pre><code class="lang-lean">import Mathlib.Algebra.Group.Defs
import Mathlib.Algebra.Ring.Defs
-- Generic group theorem
theorem inv_unique [Group G] (a b : G) (h : a * b = 1) : b = a⁻¹ := by
have : a⁻¹ * (a * b) = a⁻¹ * 1 := by rw [h]
rw [mul_assoc, inv_mul_cancel, one_mul, mul_one] at this
exact this.symm
-- Ring theorem using type classes
theorem ring_square [Ring R] (a b : R) :
(a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by
ring</code></pre>
<h3>Example 2: List Theorems</h3>
<pre><code class="lang-lean">import Mathlib.Data.List.Basic
-- Using existing theorems
theorem list_reverse_append (xs ys : List α) :
(xs ++ ys).reverse = ys.reverse ++ xs.reverse :=
List.reverse_append xs ys
-- Building on mathlib
theorem list_reverse_involutive (xs : List α) :
xs.reverse.reverse = xs :=
List.reverse_reverse xs</code></pre>
<h3>Example 3: Type Class Instance Synthesis</h3>
<pre><code class="lang-lean">import Mathlib.Algebra.Group.Defs
-- Let Lean find instances
example : 1 + 1 = 2 := rfl -- Uses instAddNat
-- Check what instance is used
#check (1 + 1 : Nat) -- Uses Add.add via instAddNat
-- Synthesize instance explicitly
#synth Add Nat -- instAddNat
#synth Monoid Nat -- instMonoidNat</code></pre>
<hr>
<p><strong>Skill Metadata:</strong></p>
<ul><li>Scope: mathlib4 library usage and contribution</li><li>Complexity: Intermediate</li><li>Prerequisites: lean-proof-basics.md</li><li>Related: lean-tactics.md, lean-theorem-proving.md</li></ul>
<footer>
<div class="fmap">
<div><b>Build</b>
<a href="https://hanzo.app">hanzo.app</a> app builder
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
<div><b>Run</b>
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
<div><b>Account</b>
<a href="https://hanzo.id">hanzo.id</a> sign in
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
<div><b>Company</b>
<a href="https://hanzo.ai">hanzo.ai</a> canonical
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
</div>
<div class="fend"><a href="/formal-lean-mathlib4.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
</footer>
</div></body></html>