mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
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]>
777 lines
31 KiB
HTML
777 lines
31 KiB
HTML
<!doctype html>
|
||
<html lang="en"><head>
|
||
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||
<title>formal-lean-tactics</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-tactics.md">
|
||
|
||
<link rel="icon" href="/favicon.svg">
|
||
<link rel="canonical" href="https://hanzoskills.com/formal-lean-tactics">
|
||
</head>
|
||
<!-- Reading this with a machine? Every page here is also plain markdown.
|
||
This one: https://hanzoskills.com/formal-lean-tactics.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-tactics</h1><p class="lede">Optimizing proof efficiency</p><h1>Lean 4 Tactics</h1>
|
||
<h2>Overview</h2>
|
||
<p>Advanced tactics, tactic combinators, automation strategies, and custom tactic development for efficient proof construction in Lean 4.</p>
|
||
<h2>Scope</h2>
|
||
<p><strong>Included:</strong></p>
|
||
<ul><li>Core tactic repertoire (rw, simp, ring, linarith, omega)</li><li>Tactic combinators and control flow</li><li>Simplification and normalization</li><li>Automation tactics (aesop, tauto, decide)</li><li>Conversion mode (conv)</li><li>Structured proofs (calc, have, show)</li><li>Tactic debugging and inspection</li><li>Custom tactic basics</li></ul>
|
||
<p><strong>Excluded:</strong></p>
|
||
<ul><li>Basic proof structure (see <code>lean-proof-basics.md</code>)</li><li>mathlib4 library search (see <code>lean-mathlib4.md</code>)</li><li>Advanced metaprogramming (separate skill)</li><li>Complex formalization strategies (see <code>lean-theorem-proving.md</code>)</li></ul>
|
||
<h2>When to Use This Skill</h2>
|
||
<p><strong>Primary use cases:</strong></p>
|
||
<ul><li>Optimizing proof efficiency</li><li>Automating repetitive reasoning</li><li>Complex equational reasoning</li><li>Arithmetic and algebraic proofs</li><li>Debugging stuck proofs</li><li>Developing domain-specific tactics</li></ul>
|
||
<p><strong>Indicators you need this:</strong></p>
|
||
<ul><li>"How do I simplify this expression?"</li><li>"What tactic should I use for arithmetic?"</li><li>"How do I automate this proof pattern?"</li><li>"My proof is too verbose"</li><li>"How do I use conv mode?"</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>Finding mathlib theorems → <code>lean-mathlib4.md</code></li><li>Formalization architecture → <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>Tactic mode fundamentals</li><li>Basic mathematical reasoning</li></ul>
|
||
<p><strong>Required setup:</strong></p>
|
||
<pre><code class="lang-bash"># Lean 4 with mathlib4 (for advanced tactics)
|
||
lake new my_project math
|
||
cd my_project
|
||
lake update
|
||
lake build</code></pre>
|
||
<p><strong>Key dependencies:</strong></p>
|
||
<ul><li>Lean 4.0.0+</li><li>mathlib4 (for tactics like ring, linarith, omega)</li></ul>
|
||
<h2>Core Concepts</h2>
|
||
<h3>1. Tactic Monad and Goal State</h3>
|
||
<p>Tactics operate in a monadic context, transforming goal states.</p>
|
||
<pre><code class="lang-lean">-- Goal state structure
|
||
-- ⊢ goal
|
||
-- Context:
|
||
-- h1 : assumption1
|
||
-- h2 : assumption2
|
||
-- Goal: conclusion
|
||
|
||
-- Tactics transform this state
|
||
example (h : p) : p ∨ q := by
|
||
-- State: h : p ⊢ p ∨ q
|
||
left
|
||
-- State: h : p ⊢ p
|
||
exact h
|
||
-- State: (no goals)</code></pre>
|
||
<h3>2. Rewriting and Simplification</h3>
|
||
<p>The workhorse of equational reasoning.</p>
|
||
<pre><code class="lang-lean">-- rw: Directed rewriting
|
||
example (h : a = b) : f a = f b := by
|
||
rw [h]
|
||
|
||
-- simp: Simplification with simp set
|
||
example : 0 + n = n := by
|
||
simp
|
||
|
||
-- simp with arguments
|
||
example : xs.reverse.reverse = xs := by
|
||
simp [List.reverse_reverse]</code></pre>
|
||
<h3>3. Unification and Pattern Matching</h3>
|
||
<p>Tactics use unification to match patterns and apply theorems.</p>
|
||
<pre><code class="lang-lean">-- apply unifies with goal
|
||
example (h : p → q) : p → q := by
|
||
apply h -- Unifies goal with conclusion of h
|
||
|
||
-- refine for partial terms with holes
|
||
example : ∃ n, n + 0 = n := by
|
||
refine ⟨?_, ?_⟩
|
||
· exact 5
|
||
· simp</code></pre>
|
||
<h3>4. Simp Lemmas and Normal Forms</h3>
|
||
<p>The <code>simp</code> tactic uses lemmas marked with <code>@[simp]</code>.</p>
|
||
<pre><code class="lang-lean">-- Declaring simp lemmas
|
||
@[simp]
|
||
theorem zero_add (n : Nat) : 0 + n = n := by
|
||
induction n <;> simp [*, Nat.add_succ]
|
||
|
||
-- Using simp
|
||
example (n : Nat) : 0 + (0 + n) = n := by
|
||
simp -- Applies zero_add twice</code></pre>
|
||
<h2>Advanced Tactics</h2>
|
||
<h3>Tactic 1: rw (Rewrite)</h3>
|
||
<p>Directed equational reasoning.</p>
|
||
<pre><code class="lang-lean">-- Basic rewrite
|
||
example (h : a = b) : a + c = b + c := by
|
||
rw [h]
|
||
|
||
-- Multiple rewrites
|
||
example (h1 : a = b) (h2 : b = c) : a = c := by
|
||
rw [h1, h2]
|
||
|
||
-- Rewrite backwards
|
||
example (h : a = b) : b = a := by
|
||
rw [←h]
|
||
|
||
-- Rewrite in hypothesis
|
||
example (h1 : a = b) (h2 : f a = g a) : f b = g a := by
|
||
rw [h1] at h2
|
||
exact h2
|
||
|
||
-- Rewrite everywhere
|
||
example (h : a = b) (h1 : f a = c) (h2 : g a = d) : f b = c ∧ g b = d := by
|
||
rw [h] at *
|
||
exact ⟨h1, h2⟩</code></pre>
|
||
<h3>Tactic 2: simp (Simplification)</h3>
|
||
<p>Normalize expressions using simp lemmas.</p>
|
||
<pre><code class="lang-lean">-- Basic simplification
|
||
example : [1, 2] ++ [] = [1, 2] := by simp
|
||
|
||
-- With specific lemmas
|
||
example : xs.reverse.reverse = xs := by
|
||
simp only [List.reverse_reverse]
|
||
|
||
-- With hypotheses
|
||
example (h : x = 0) : x + y = y := by
|
||
simp [h]
|
||
|
||
-- Simplify everywhere
|
||
example (h : x = 0) : x + y = y ∧ y + x = y := by
|
||
simp [h] at *
|
||
exact ⟨h, h⟩
|
||
|
||
-- Simplify with arithmetic
|
||
example : (n + 1) * 2 = n * 2 + 2 := by
|
||
simp [Nat.add_mul, Nat.mul_comm]</code></pre>
|
||
<p><strong>Key simp variants:</strong></p>
|
||
<pre><code class="lang-lean">simp -- Use full simp set
|
||
simp only [...] -- Use only specified lemmas
|
||
simp [h1, h2] -- Add hypotheses to simp set
|
||
simp at h -- Simplify hypothesis
|
||
simp at * -- Simplify all
|
||
simp? -- Show which lemmas were used (debugging)</code></pre>
|
||
<h3>Tactic 3: ring (Ring Arithmetic)</h3>
|
||
<p>Decide equality in commutative (semi)rings.</p>
|
||
<pre><code class="lang-lean">-- Polynomial equality
|
||
example (a b : Nat) : (a + b) * (a + b) = a * a + 2 * a * b + b * b := by
|
||
ring
|
||
|
||
-- Complex expressions
|
||
example (x y z : Int) :
|
||
(x + y) * z - x * z = y * z := by
|
||
ring
|
||
|
||
-- Works with variables and constants
|
||
example (n : Nat) : (n + 1) ^ 2 = n ^ 2 + 2 * n + 1 := by
|
||
ring</code></pre>
|
||
<h3>Tactic 4: linarith (Linear Arithmetic)</h3>
|
||
<p>Solve linear arithmetic goals.</p>
|
||
<pre><code class="lang-lean">-- From linear inequalities
|
||
example (h1 : a ≤ b) (h2 : b < c) : a < c := by
|
||
linarith
|
||
|
||
-- With arithmetic
|
||
example (h : x ≤ 3) : 2 * x + 1 ≤ 7 := by
|
||
linarith
|
||
|
||
-- Complex linear reasoning
|
||
example (h1 : x + y ≤ 10) (h2 : x ≥ 3) (h3 : y ≥ 4) : x + y ≤ 10 := by
|
||
linarith</code></pre>
|
||
<h3>Tactic 5: omega (Integer/Natural Arithmetic)</h3>
|
||
<p>Decision procedure for Presburger arithmetic (linear integer arithmetic).</p>
|
||
<pre><code class="lang-lean">-- Natural number arithmetic
|
||
example (n m : Nat) (h : n + m = 5) (h2 : n ≥ 3) : m ≤ 2 := by
|
||
omega
|
||
|
||
-- Integer arithmetic
|
||
example (x y : Int) (h1 : x + y = 10) (h2 : x - y = 2) : x = 6 := by
|
||
omega
|
||
|
||
-- With modular arithmetic
|
||
example (n : Nat) : n % 2 = 0 ∨ n % 2 = 1 := by
|
||
omega</code></pre>
|
||
<h3>Tactic 6: conv (Conversion Mode)</h3>
|
||
<p>Fine-grained rewriting control.</p>
|
||
<pre><code class="lang-lean">-- Rewrite specific subterm
|
||
example : (1 + 2) + 3 = 3 + 3 := by
|
||
conv_lhs =>
|
||
arg 1 -- Select first argument
|
||
rw [Nat.add_comm]
|
||
rfl
|
||
|
||
-- Navigate expression tree
|
||
example : f (g (h a)) = f (g (h b)) := by
|
||
conv_lhs =>
|
||
arg 1 -- Enter f's argument
|
||
arg 1 -- Enter g's argument
|
||
arg 1 -- Enter h's argument
|
||
rw [hab]
|
||
|
||
-- Using for: target specific occurrences
|
||
example : a + (a + b) = (a + a) + b := by
|
||
conv =>
|
||
lhs
|
||
arg 2 -- Second argument of outer +
|
||
rw [Nat.add_comm]</code></pre>
|
||
<h3>Tactic 7: calc (Calculational Proofs)</h3>
|
||
<p>Chain equalities and inequalities.</p>
|
||
<pre><code class="lang-lean">-- Chain equalities
|
||
example (h1 : a = b) (h2 : b = c) : a = c := by
|
||
calc a = b := h1
|
||
_ = c := h2
|
||
|
||
-- Chain inequalities
|
||
example (h1 : a ≤ b) (h2 : b < c) : a < c := by
|
||
calc a ≤ b := h1
|
||
_ < c := h2
|
||
|
||
-- Mixed operations
|
||
example (n : Nat) : (n + 1) ^ 2 = n ^ 2 + 2 * n + 1 := by
|
||
calc (n + 1) ^ 2 = (n + 1) * (n + 1) := by rfl
|
||
_ = n * n + n * 1 + 1 * n + 1 * 1 := by ring
|
||
_ = n ^ 2 + 2 * n + 1 := by ring</code></pre>
|
||
<h3>Tactic 8: aesop (Automated Search)</h3>
|
||
<p>Automated proof search using a tactic library.</p>
|
||
<pre><code class="lang-lean">-- Solves goals by searching
|
||
example (h : p ∧ q) : q ∧ p := by
|
||
aesop
|
||
|
||
-- With custom rule sets
|
||
example : ∀ x, x ∈ xs → x ∈ xs ++ ys := by
|
||
aesop (add norm simp List.mem_append)</code></pre>
|
||
<h3>Tactic 9: tauto (Tautology)</h3>
|
||
<p>Decide propositional tautologies.</p>
|
||
<pre><code class="lang-lean">-- Propositional logic
|
||
example : p ∧ q → q ∧ p := by
|
||
tauto
|
||
|
||
-- Complex tautologies
|
||
example : (p → q) → (q → r) → (p → r) := by
|
||
tauto</code></pre>
|
||
<h3>Tactic 10: decide (Decision Procedures)</h3>
|
||
<p>Decide decidable propositions by evaluation.</p>
|
||
<pre><code class="lang-lean">-- Decidable equality
|
||
example : (5 : Nat) ≠ 3 := by
|
||
decide
|
||
|
||
-- Boolean conditions
|
||
example : (10 < 20) = true := by
|
||
decide
|
||
|
||
-- List membership
|
||
example : 3 ∈ [1, 2, 3, 4] := by
|
||
decide</code></pre>
|
||
<h2>Tactic Combinators</h2>
|
||
<h3>Sequential and Parallel</h3>
|
||
<pre><code class="lang-lean">-- Sequential (;)
|
||
example : p → p ∧ p := by
|
||
intro h; constructor; exact h; exact h
|
||
|
||
-- All goals (<;>)
|
||
example : p → p ∧ p := by
|
||
intro h
|
||
constructor <;> exact h -- Apply to both goals
|
||
|
||
-- Focus first goal (·)
|
||
example : p ∧ q → q ∧ p := by
|
||
intro ⟨hp, hq⟩
|
||
constructor
|
||
· exact hq -- First goal
|
||
· exact hp -- Second goal</code></pre>
|
||
<h3>Control Flow</h3>
|
||
<pre><code class="lang-lean">-- Alternative (first success)
|
||
example : p ∨ q := by
|
||
first | left; assumption | right; assumption
|
||
|
||
-- Try (don't fail)
|
||
example : p → p := by
|
||
try rw [some_lemma] -- Continue even if rw fails
|
||
intro h
|
||
exact h
|
||
|
||
-- Repeat
|
||
example : 0 + (0 + n) = n := by
|
||
repeat rw [Nat.zero_add]
|
||
-- Applies until no longer applicable</code></pre>
|
||
<h3>Conditional</h3>
|
||
<pre><code class="lang-lean">-- By cases
|
||
example : p ∨ ¬p → q → q := by
|
||
intro h hq
|
||
cases h <;> exact hq
|
||
|
||
-- If-then-else pattern
|
||
by
|
||
if h : condition then
|
||
-- Proof with h : condition
|
||
sorry
|
||
else
|
||
-- Proof with h : ¬condition
|
||
sorry</code></pre>
|
||
<h2>Structured Proof Patterns</h2>
|
||
<h3>Pattern 1: have (Intermediate Results)</h3>
|
||
<pre><code class="lang-lean">-- Name intermediate facts
|
||
example (h1 : a = b) (h2 : b = c) : f a = f c := by
|
||
have hab : a = b := h1
|
||
have hbc : b = c := h2
|
||
have hac : a = c := by rw [hab, hbc]
|
||
rw [hac]
|
||
|
||
-- Anonymous have
|
||
example (h : a = b) : f a = f b := by
|
||
have : a = b := h
|
||
rw [this]</code></pre>
|
||
<h3>Pattern 2: show (Make Goal Explicit)</h3>
|
||
<pre><code class="lang-lean">-- Explicitly show what you're proving
|
||
example : p → q → p ∧ q := by
|
||
intro hp hq
|
||
show p ∧ q
|
||
exact ⟨hp, hq⟩
|
||
|
||
-- Useful for type-directed proof
|
||
example : (fun x => x + 0) = (fun x => x) := by
|
||
show (fun x => x + 0) = (fun x => x)
|
||
funext x
|
||
simp</code></pre>
|
||
<h3>Pattern 3: suffices (Backwards Reasoning)</h3>
|
||
<pre><code class="lang-lean">-- Prove it suffices to show something simpler
|
||
example (h : p) : p ∧ p := by
|
||
suffices hp : p from ⟨hp, hp⟩
|
||
exact h
|
||
|
||
-- Chain backwards
|
||
example : a = d := by
|
||
suffices a = c by rw [this, hcd]
|
||
suffices a = b by rw [this, hbc]
|
||
exact hab</code></pre>
|
||
<h3>Pattern 4: obtain (Destructure Existentials)</h3>
|
||
<pre><code class="lang-lean">-- Clean existential elimination
|
||
example (h : ∃ x, P x ∧ Q x) : ∃ x, P x := by
|
||
obtain ⟨x, hPx, hQx⟩ := h
|
||
exact ⟨x, hPx⟩
|
||
|
||
-- With pattern matching
|
||
example (h : ∃ x y, x + y = 10) : ∃ z, z ≤ 10 := by
|
||
obtain ⟨x, y, hsum⟩ := h
|
||
use x
|
||
omega</code></pre>
|
||
<h2>Debugging Tactics</h2>
|
||
<h3>Inspection</h3>
|
||
<pre><code class="lang-lean">-- Show current goal state
|
||
example : p → p := by
|
||
intro h
|
||
trace "{h}" -- Print hypothesis
|
||
exact h
|
||
|
||
-- Show term
|
||
#check (rfl : a = a)
|
||
#print Nat.add -- Print definition
|
||
|
||
-- simp? shows which lemmas were used
|
||
example : xs.reverse.reverse = xs := by
|
||
simp?
|
||
-- Try this: simp only [List.reverse_reverse]</code></pre>
|
||
<h3>Common Issues</h3>
|
||
<pre><code class="lang-lean">-- Tactic failed: goal doesn't match
|
||
example : p := by
|
||
exact q -- Error: type mismatch
|
||
|
||
-- Fix: check types
|
||
example : p := by
|
||
have hq : q := sorry
|
||
-- Can't use hq to prove p directly
|
||
|
||
-- Unknown identifier
|
||
example : p := by
|
||
exact h -- Error: h not found
|
||
|
||
-- Fix: intro first
|
||
example : p → p := by
|
||
intro h
|
||
exact h</code></pre>
|
||
<h2>Quick Reference</h2>
|
||
<h3>Essential Tactics</h3>
|
||
<pre><code class="lang-lean">-- Equality/Rewriting
|
||
rfl -- Reflexivity
|
||
rw [h] -- Rewrite with h
|
||
simp -- Simplify
|
||
|
||
-- Arithmetic
|
||
ring -- Ring equality
|
||
linarith -- Linear arithmetic
|
||
omega -- Presburger arithmetic
|
||
|
||
-- Structure
|
||
constructor -- Build constructor
|
||
cases h -- Case analysis
|
||
induction n -- Induction
|
||
|
||
-- Automation
|
||
aesop -- Automated search
|
||
tauto -- Propositional tautology
|
||
decide -- Decision procedure
|
||
|
||
-- Control
|
||
exact t -- Provide exact term
|
||
apply t -- Apply theorem
|
||
refine t -- Partial term with holes</code></pre>
|
||
<h3>Tactic Combinators</h3>
|
||
<pre><code class="lang-lean">t1; t2 -- Sequential
|
||
t1 <;> t2 -- All goals
|
||
· t -- Focus first
|
||
first | t1 | t2 -- Alternative
|
||
try t -- Don't fail
|
||
repeat t -- Repeat until failure
|
||
all_goals t -- Apply to all goals</code></pre>
|
||
<h3>Conversion Mode</h3>
|
||
<pre><code class="lang-lean">conv =>
|
||
lhs -- Left-hand side
|
||
rhs -- Right-hand side
|
||
arg n -- nth argument
|
||
args -- All arguments
|
||
enter [1, 2] -- Navigate path
|
||
rw [h] -- Rewrite
|
||
simp -- Simplify</code></pre>
|
||
<h2>Anti-Patterns</h2>
|
||
<h3>Anti-Pattern 1: Tactic Soup</h3>
|
||
<pre><code class="lang-lean">-- BAD: Long sequence of low-level tactics
|
||
example : complicated_goal := by
|
||
intro h1
|
||
intro h2
|
||
intro h3
|
||
have x := h1
|
||
have y := h2
|
||
rw [x]
|
||
rw [y]
|
||
simp
|
||
ring
|
||
sorry
|
||
|
||
-- GOOD: Structure and automation
|
||
example : complicated_goal := by
|
||
intro h1 h2 h3
|
||
simp [h1, h2]
|
||
ring</code></pre>
|
||
<h3>Anti-Pattern 2: Ignoring calc</h3>
|
||
<pre><code class="lang-lean">-- BAD: Manual chaining
|
||
example (h1 : a = b) (h2 : b = c) (h3 : c = d) : a = d := by
|
||
have hab := h1
|
||
rw [hab]
|
||
have hbc := h2
|
||
rw [hbc]
|
||
exact h3
|
||
|
||
-- GOOD: Use calc
|
||
example (h1 : a = b) (h2 : b = c) (h3 : c = d) : a = d := by
|
||
calc a = b := h1
|
||
_ = c := h2
|
||
_ = d := h3</code></pre>
|
||
<h3>Anti-Pattern 3: Over-Reliance on sorry</h3>
|
||
<pre><code class="lang-lean">-- BAD: Sorry as crutch
|
||
theorem main_result : important_property := by
|
||
have lemma1 := sorry
|
||
have lemma2 := sorry
|
||
sorry
|
||
|
||
-- GOOD: Explicit admits with tracking
|
||
-- In separate file or section
|
||
axiom lemma1 : auxiliary_fact_1 -- TODO: issue #123
|
||
axiom lemma2 : auxiliary_fact_2 -- TODO: issue #124
|
||
|
||
theorem main_result : important_property := by
|
||
have h1 := lemma1
|
||
have h2 := lemma2
|
||
-- Actual proof using h1, h2
|
||
sorry -- Requires h1 and h2, tracked in #125</code></pre>
|
||
<h3>Anti-Pattern 4: Not Using Automation</h3>
|
||
<pre><code class="lang-lean">-- BAD: Manual propositional reasoning
|
||
example : (p ∧ q) ∧ r → r ∧ (q ∧ p) := by
|
||
intro ⟨⟨hp, hq⟩, hr⟩
|
||
constructor
|
||
· exact hr
|
||
· constructor
|
||
· exact hq
|
||
· exact hp
|
||
|
||
-- GOOD: Let automation handle it
|
||
example : (p ∧ q) ∧ r → r ∧ (q ∧ p) := by
|
||
tauto</code></pre>
|
||
<h3>Anti-Pattern 5: Verbose Rewrites</h3>
|
||
<pre><code class="lang-lean">-- BAD: Rewrite one at a time
|
||
example (h1 : a = b) (h2 : b = c) (h3 : c = d) : f a = f d := by
|
||
rw [h1]
|
||
rw [h2]
|
||
rw [h3]
|
||
|
||
-- GOOD: Batch rewrites
|
||
example (h1 : a = b) (h2 : b = c) (h3 : c = d) : f a = f d := by
|
||
rw [h1, h2, h3]</code></pre>
|
||
<h3>Anti-Pattern 6: Ignoring conv</h3>
|
||
<pre><code class="lang-lean">-- BAD: Rewrite everything (may fail)
|
||
example : f (g a + h a) = f (h a + g a) := by
|
||
rw [add_comm] -- Might rewrite wrong occurrence
|
||
|
||
-- GOOD: Target specific subterm
|
||
example : f (g a + h a) = f (h a + g a) := by
|
||
conv_lhs =>
|
||
arg 1
|
||
rw [add_comm]</code></pre>
|
||
<h2>Related Skills</h2>
|
||
<ul><li><strong>lean-proof-basics.md</strong>: Foundational proof techniques</li><li><strong>lean-mathlib4.md</strong>: Using mathlib4 library and theorems</li><li><strong>lean-theorem-proving.md</strong>: Advanced formalization strategies</li><li><strong>lean-metaprogramming.md</strong>: Custom tactic development (advanced)</li></ul>
|
||
<h2>References</h2>
|
||
<ul><li><a href="https://lean-lang.org/theorem_proving_in_lean4/tactics.html">Lean 4 Tactics Index</a></li><li><a href="https://leanprover-community.github.io/mathlib4_docs/tactics.html">Mathlib4 Tactics Documentation</a></li><li><a href="https://github.com/leanprover-community/aesop">Aesop Documentation</a></li><li><a href="https://github.com/leanprover-community/batteries">Batteries Tactics</a></li></ul>
|
||
<h2>Examples</h2>
|
||
<h3>Example 1: Algebraic Manipulation</h3>
|
||
<pre><code class="lang-lean">-- Expanding and simplifying
|
||
example (a b : Nat) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by
|
||
ring
|
||
|
||
-- Using calc for clarity
|
||
example (x y : Int) : (x + y) * (x - y) = x ^ 2 - y ^ 2 := by
|
||
calc (x + y) * (x - y)
|
||
= x * x - x * y + y * x - y * y := by ring
|
||
_ = x ^ 2 - y ^ 2 := by ring</code></pre>
|
||
<h3>Example 2: Linear Arithmetic</h3>
|
||
<pre><code class="lang-lean">-- Combining linarith with other tactics
|
||
example (x y z : Nat) (h1 : x ≤ y) (h2 : y < z) : x < z := by
|
||
linarith
|
||
|
||
-- Complex linear reasoning
|
||
example (a b c : Int)
|
||
(h1 : 2 * a + b ≤ 10)
|
||
(h2 : a + 2 * b ≤ 11)
|
||
(h3 : a ≥ 0)
|
||
(h4 : b ≥ 0) :
|
||
3 * a + 3 * b ≤ 21 := by
|
||
linarith</code></pre>
|
||
<h3>Example 3: Simplification Strategies</h3>
|
||
<pre><code class="lang-lean">-- Controlled simplification
|
||
example (xs ys : List α) :
|
||
(xs ++ ys).reverse = ys.reverse ++ xs.reverse := by
|
||
simp only [List.reverse_append]
|
||
|
||
-- Simplification with arithmetic
|
||
example (n m : Nat) : (n + m) + 0 = n + m := by
|
||
simp
|
||
|
||
-- Using simp to normalize before other tactics
|
||
example (n : Nat) : (n + 1) * 2 = n * 2 + 2 := by
|
||
simp [Nat.add_mul]
|
||
ring</code></pre>
|
||
<h3>Example 4: Conversion Mode for Precise Rewriting</h3>
|
||
<pre><code class="lang-lean">-- Target specific subexpression
|
||
example (h : a = b) : f (g a + c) + d = f (g b + c) + d := by
|
||
conv_lhs =>
|
||
arg 1 -- Enter first argument of +
|
||
arg 1 -- Enter first argument of f
|
||
arg 1 -- Enter first argument of +
|
||
arg 1 -- Enter argument of g
|
||
rw [h]
|
||
|
||
-- Multiple targeted rewrites
|
||
example : (a + b) + (c + d) = (b + a) + (d + c) := by
|
||
conv =>
|
||
lhs
|
||
arg 1
|
||
rw [add_comm]
|
||
conv =>
|
||
lhs
|
||
arg 2
|
||
rw [add_comm]</code></pre>
|
||
<hr>
|
||
<p><strong>Skill Metadata:</strong></p>
|
||
<ul><li>Scope: Advanced Lean 4 tactics and automation</li><li>Complexity: Intermediate to advanced</li><li>Prerequisites: lean-proof-basics.md</li><li>Related: lean-mathlib4.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-tactics.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
||
</footer>
|
||
</div></body></html> |