lean4-htt/src/Lean
Leonardo de Moura d57f71c1c0
perf: optimize kernel type-checking for have-telescope simplification in Sym.simp (#11967)
This PR implements a new strategy for simplifying `have`-telescopes in
`Sym.simp` that achieves linear kernel type-checking time instead of
quadratic.

## Problem

When simplifying deep `have`-telescopes, the previous approach using
`have_congr'` produced proofs that type-checked in quadratic time. The
simplifier itself was fast, but the kernel became the bottleneck for
large telescopes.

For example, at n=100:
- **Before**: simp = 2.4ms, kernel = **225ms**
- **After**: simp = 3.5ms, kernel = **10ms**

The quadratic behavior occurred because the kernel creates fresh free
variables for each binder when type-checking, destroying sharing and
producing O(n²) intermediate terms.

## Solution

We transform sequential `have`-telescopes into a parallel
beta-application form:

```
have x₁ := v₁; have x₂ := v₂[x₁]; b[x₁, x₂]
  ↓ (definitionally equal)
(fun x₁ x₂' => b[x₁, x₂' x₁]) v₁ (fun x₁ => v₂[x₁])
```

This parallel form leverages the efficient simplifier for lambdas in
`Sym.simp`. This form enables:
1. Independent simplification of each argument
2. Proof construction using standard congruence lemmas
3. Linear kernel type-checking time

The algorithm has three phases:
1. **`toBetaApp`**: Transform telescope → parallel beta-application
2. **`simpBetaApp`**: Simplify using `congr`/`congrArg`/`congrFun'` and
`simpLambda`
3. **`toHave`**: Convert back to `have` form

## Benchmark Results

### Benchmark 1: Chain with all variables used in body

| n | Before (simp) | Before (kernel) | After (simp) | After (kernel) |
|---|---------------|-----------------|--------------|----------------|
| 50 | 1.2ms | 32ms | 1.6ms | 4.4ms |
| 100 | 2.4ms | **225ms** | 3.5ms | **10ms** |
| 200 | 4.5ms | — | 8.4ms | 27ms |
| 500 | 11.7ms | — | 33.6ms | 128ms |

### Benchmark 3: Parallel declarations (simplified values)

| n | Before (simp) | Before (kernel) | After (simp) | After (kernel) |
|---|---------------|-----------------|--------------|----------------|
| 50 | 0.5ms | 24ms | 0.8ms | 1.8ms |
| 100 | 1.2ms | **169ms** | 1.8ms | **5.3ms** |
| 200 | 2.2ms | — | 3.9ms | 17ms |
| 500 | 5.9ms | — | 12.3ms | 93ms |

### Benchmark 5: Chain with single dependency

| n | Before (simp) | Before (kernel) | After (simp) | After (kernel) |
|---|---------------|-----------------|--------------|----------------|
| 100 | 1.6ms | 6.2ms | 1.8ms | 6.2ms |
| 200 | 2.8ms | 21.6ms | 4.4ms | 16.5ms |
| 500 | 7.3ms | **125ms** | 12.8ms | **72ms** |

Key observations:
- Kernel time is now **linear** in telescope depth (previously
quadratic)
- Simp time increases slightly due to the transformation overhead
- Total time (simp + kernel) is dramatically reduced for large
telescopes
- The improvement is most pronounced when the body depends on many
variables

## Trade-offs

- Proof sizes are larger (more congruence lemma applications)
- Simp time has ~1.5x overhead from the transformation
- For very small telescopes (n < 10), the overhead may not pay off

The optimization targets the critical path: kernel type-checking was the
bottleneck preventing scaling to realistic symbolic simulation
workloads.
2026-01-11 02:20:47 +00:00
..
Compiler refactor: preparatory change from structure to inductive on LCNF (#11934) 2026-01-08 09:56:41 +00:00
Data perf: add PersistentHashMap.findKeyD and PersistentHashSet.findD (#11907) 2026-01-05 20:04:49 +00:00
DocString chore: minor String API improvements (#11439) 2025-12-01 11:44:14 +00:00
Elab feat: add first_par combinator for try? with grind +locals (#11949) 2026-01-09 21:09:41 +00:00
Language feat: module system is no longer experimental (#11637) 2025-12-12 21:20:26 +00:00
LibrarySuggestions fix: allow private proof-valued structure fields in library suggestions (#11962) 2026-01-10 08:40:54 +00:00
Linter feat: lint coercions that are deprecated or banned in core (#11511) 2025-12-12 15:09:13 +00:00
Meta perf: optimize kernel type-checking for have-telescope simplification in Sym.simp (#11967) 2026-01-11 02:20:47 +00:00
Parser refactor: move error explanation text to the manual (#11688) 2025-12-26 17:14:58 +00:00
ParserCompiler chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
PrettyPrinter refactor: move Std.Range to Std.Legacy.Range (#11438) 2025-12-18 02:07:33 +00:00
Server refactor: move error explanation text to the manual (#11688) 2025-12-26 17:14:58 +00:00
Util perf: ensure withTraceNodeBefore message is created lazily (#11893) 2026-01-04 20:38:39 +00:00
Widget chore: rename String.Range to Lean.Syntax.Range (#10852) 2025-10-21 07:32:25 +00:00
AddDecl.lean chore: use backticks for sorry in diagnostic messages (#11608) 2025-12-15 14:30:21 +00:00
Attributes.lean chore: fix spelling (#11531) 2025-12-06 13:54:27 +00:00
AuxRecursor.lean perf: handle per-constructor noConfusion in toLCNF (#11566) 2025-12-10 09:03:55 +00:00
BuiltinDocAttr.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Class.lean refactor: update and consolidate attribute-related error messages (#9495) 2025-07-26 02:03:18 +00:00
Compiler.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00
CoreM.lean perf: remove obsolete old codegen workaround (#9311) 2025-12-12 20:51:34 +00:00
Data.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00
Declaration.lean chore: >6 month old deprecations (#10969) 2025-10-26 22:48:41 +00:00
DeclarationRange.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
DefEqAttrib.lean perf: clarify and granularize access to async env ext state (#9587) 2025-08-02 17:01:08 +00:00
DocString.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
Elab.lean feat: add #import_path, assert_not_exists, assert_not_imported commands (#11726) 2025-12-19 04:09:33 +00:00
EnvExtension.lean fix: avoid panic in TagDeclarationExtension.tag on partial elaboration (#11882) 2026-01-05 00:36:58 +00:00
Environment.lean fix: avoid panic in async elaboration for theorems with docstrings in where (#11896) 2026-01-10 09:39:31 +00:00
ErrorExplanation.lean refactor: move error explanation text to the manual (#11688) 2025-12-26 17:14:58 +00:00
Exception.lean refactor: move error explanation text to the manual (#11688) 2025-12-26 17:14:58 +00:00
Expr.lean fix: nonstandard instances in grind and simp +arith (#11758) 2025-12-21 17:56:49 +00:00
ExtraModUses.lean feat: shake: make Mathlib-ready (#11496) 2025-12-05 09:37:58 +00:00
HeadIndex.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
Hygiene.lean chore: do not set unused Option.Decl.group (#11307) 2025-11-21 16:44:38 +00:00
IdentifierSuggestion.lean chore: remove NameMapExtension abbreviation (#11632) 2025-12-12 12:34:53 +00:00
ImportingFlag.lean chore: miscellaneous documentation typos (#10009) 2025-08-20 21:39:03 +00:00
InternalExceptionId.lean chore: reorganize Init imports around strings (#10289) 2025-09-07 17:09:14 +00:00
KeyedDeclsAttribute.lean fix: some ExtraModUses (#10620) 2025-10-03 15:50:40 +00:00
LabelAttribute.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Level.lean fix: move the monad argument for ForIn, ForIn', and ForM (#10204) 2025-11-25 12:20:37 +00:00
LibrarySuggestions.lean fix: make library suggestions available in module files (#11373) 2025-11-26 05:39:27 +00:00
Linter.lean feat: lint coercions that are deprecated or banned in core (#11511) 2025-12-12 15:09:13 +00:00
LoadDynlib.lean refactor: use String.Slice in String.take and variants (#11180) 2025-11-18 16:13:48 +00:00
LocalContext.lean feat: support for incrementally processing hypotheses in grind (#11787) 2025-12-24 02:50:22 +00:00
Log.lean refactor: move error explanation text to the manual (#11688) 2025-12-26 17:14:58 +00:00
Message.lean fix: teach Exception.isRuntime to detect nested errors (#11490) 2025-12-10 10:19:33 +00:00
Meta.lean refactor: have telescope support (#11914) 2026-01-06 19:20:25 +00:00
MetavarContext.lean fix: remove superfluous Monad instances from some spec lemmas (#10564) (#10618) 2025-09-29 15:02:43 +00:00
Modifiers.lean chore: more module system fixes and refinements for finishing batteries port (#10819) 2025-10-21 08:19:50 +00:00
MonadEnv.lean feat: grind support for .ctorIdx (#11652) 2025-12-13 13:32:19 +00:00
Namespace.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
Parser.lean feat: hexnum parser (#10716) 2025-10-08 21:12:03 +00:00
ParserCompiler.lean doc: correct typos in documentation and comments (#11465) 2025-12-02 06:38:05 +00:00
PrettyPrinter.lean chore: do not set unused Option.Decl.group (#11307) 2025-11-21 16:44:38 +00:00
PrivateName.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
ProjFns.lean chore: delete obsolete C++ file (#11561) 2025-12-09 15:47:54 +00:00
ReducibilityAttrs.lean feat: allow setting reducibilityCoreExt in async contexts (#11301) 2025-11-21 09:23:14 +00:00
Replay.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
ReservedNameAction.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
ResolveName.lean chore: more module system fixes and refinements for finishing batteries port (#10819) 2025-10-21 08:19:50 +00:00
Runtime.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
ScopedEnvExtension.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Server.lean feat: revamp server logging (#10787) 2025-10-28 16:26:59 +00:00
Setup.lean fix: symbol clashes between packages (#11082) 2025-11-19 02:24:44 +00:00
Shell.lean refactor: port shell option processing to Lean (v2) (#11434) 2025-12-02 17:41:51 +00:00
Structure.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
SubExpr.lean refactor: use String.split instead of String.splitOn or String.splitToList (#11250) 2025-11-19 09:35:19 +00:00
Syntax.lean fix: move the monad argument for ForIn, ForIn', and ForM (#10204) 2025-11-25 12:20:37 +00:00
ToExpr.lean feat: support for Rat scientific literals (#10961) 2025-10-26 02:05:26 +00:00
ToLevel.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
Util.lean feat: instantiate tactic parameter optimizer (#10916) 2025-10-23 01:22:33 +00:00
Widget.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00