Interactively optimize Lean 4 proofs by shortening length or runtime without sacrificing readability
Interactively optimize Lean 4 proofs by applying systematic golfing patterns that reduce length by 30-80%. Verifies safety of each change to avoid false positives, tests with lake build, and requires your approval before applying.
/plugin marketplace add cameronfreer/lean4-skills/plugin install lean4-theorem-proving@lean4-skillsApply systematic proof-golfing patterns to optimize Lean 4 proofs after compilation.
IMPORTANT: Proof-golfing scripts are bundled with this plugin - do not look for them in the current directory. Always use the full path with ${CLAUDE_PLUGIN_ROOT}.
Follow this systematic process to achieve 30-40% size reduction while avoiding the 93% false-positive trap:
Before starting, confirm:
lake build)Ask user:
Ready to optimize [file]? This will:
- Find optimization patterns (filtering out false positives)
- Apply changes interactively with your approval
- Test each change with lake build
Continue? (yes/no)
Check that the tool is available, then run pattern detection.
Verify tool is staged:
test -f .claude/tools/lean4/find_golfable.py || {
echo "ERROR: find_golfable.py not staged. Restart session after reinstalling plugin."
exit 1
}
Run pattern detection with false-positive filtering (replace MyFile.lean with your actual file):
python3 .claude/tools/lean4/find_golfable.py MyFile.lean --filter-false-positives --verbose
Fallback if script is not available or fails (replace MyFile.lean with your actual file):
# Manual pattern detection - search for common patterns
grep -n "let.*:=.*have.*:=.*exact" MyFile.lean # let+have+exact pattern
grep -n "by$" MyFile.lean | grep -A1 "exact" # by-exact pattern
IMPORTANT: Never use placeholders like <file> in executed commands. Always use the actual file path.
If 0 patterns found:
✅ No optimization opportunities found in [file].
This means either:
- File is already well-optimized
- Patterns are marginal (not worth the effort)
- Codebase has reached saturation
Recommendation: Move on to other files or declare victory!
If patterns found: Show summary and proceed to next step.
Group patterns by priority:
Focus strategy:
Found [N] patterns:
- [X] HIGH priority (60-80% reduction each)
- [Y] MEDIUM priority (30-50% reduction)
- [Z] LOW priority (10-30% reduction)
Recommendation: Focus on HIGH priority patterns first for best ROI.
Tackle HIGH patterns? (yes/no)
For each HIGH priority pattern:
a) Check if let binding is safe to inline (replace with your actual file and line number):
python3 .claude/tools/lean4/analyze_let_usage.py MyFile.lean --line 42
Fallback if script is not available or fails:
IMPORTANT: Replace MyFile.lean and 42 with your actual file path and line number.
b) Interpret results:
If false positive detected:
⚠️ Pattern at line [N] is a false positive!
Let binding '[name]' used [X] times.
Inlining would INCREASE code by ~[Y] tokens.
Skipping this pattern (would make code worse).
For each safe pattern:
a) Show user the before/after:
Pattern: [pattern_type] at line [N]
Estimated savings: [X] lines, [Y]% reduction
BEFORE:
[show 5-10 lines of current code]
PROPOSED CHANGE:
[show optimized version]
Apply this optimization? (yes/no/skip-remaining)
b) If user says yes:
lake build to verifyc) Track cumulative savings:
Applied [N] optimizations so far:
- Lines saved: [X]
- Estimated token reduction: [Y]%
After each batch of 5-10 optimizations, check indicators:
Saturation signs:
If saturation detected:
🏁 Saturation point reached!
Statistics:
- Optimizations applied: [N]
- Lines saved: [X]
- Token reduction: ~[Y]%
- Time invested: ~[Z] minutes
Remaining patterns are marginal (low ROI).
Recommendation: Declare victory and move on!
Continue optimizing anyway? (yes/no)
After all optimizations:
# Verify file still compiles
lake build [file]
# Show final statistics
Report results:
✅ Proof optimization complete!
Final Statistics:
- Patterns found: [total]
- Patterns applied: [applied] ([success_rate]%)
- False positives skipped: [skipped]
- Lines saved: [lines]
- Estimated token reduction: [percent]%
- Time invested: ~[minutes] minutes
File compiles successfully: ✓
Ready to commit these changes? See /check-axioms to verify quality.
Based on references/proof-golfing.md:
Pattern 1: let + have + exact (60-80% reduction)
Pattern 2: Smart ext (50% reduction)
apply Subtype.ext; apply Fin.ext → extPattern 3: simp closes goals directly (67% reduction)
simp only [...]; exact lemma → simp [...]Pattern 4: ext-simp chains (≥2 line savings)
ext x; simp; use y; simpIf lake build fails after optimization:
❌ Build failed after optimization at line [N]
Error: [show error message]
Action: Reverting change and continuing with next pattern.
This pattern requires manual investigation.
If analyze_let_usage.py fails:
⚠️ Could not analyze let binding usage.
Recommendation: Skip this pattern (safety first).
Use count_tokens.py for unclear cases (replace with your actual file and code):
python3 .claude/tools/lean4/count_tokens.py --before-file MyFile.lean:10-25 --after "optimized code here"
Fallback if script is not available or fails:
IMPORTANT: Replace MyFile.lean:10-25 and "optimized code here" with your actual values.
Use lean_multi_attempt (if MCP available):
For complex optimizations, generate 2-3 candidates and test in parallel
✅ Do:
❌ Don't:
/check-axioms - Verify axiom hygiene after optimization/build-lean - Quick build verification/analyze-sorries - Check for remaining sorries