@@ -8,6 +8,31 @@ import Lean.AddDecl
88import Lean.Meta.AppBuilder
99import Lean.Meta.CompletionName
1010
11+ /-!
12+ This module produces a construction for the `noConfusionType` that is linear in size in the number of
13+ constructors of the inductive type. This is in contrast to the previous construction (definde in
14+ `no_confusion.cpp`), that is quadratic in size due to nested `.brecOn` applications.
15+
16+ We still use the old construction when processing the prelude, for the few inductives that we need
17+ until below (`Nat`, `Bool`, `Decidable`).
18+
19+ The main trick is to use a `withCtor` helper that is like a match with one constructor pattern and
20+ one catch-all pattern, and thus linear in size. And the helper itself is a single function
21+ definition, rather than one for each constructor, using a `withCtorType` helper in the function.
22+
23+ See the `linearNoConfusion.lean` test for exemplary output of this translation (checked to be
24+ up-to-date).
25+
26+ The `withCtor` functions could be generally useful, but for that they should probably eliminate into
27+ `Sort _` rather than `Type _`, and then writing the `withCtorType` function runs into universe level
28+ confusion, which may be solvable if the kernel had more complete univere level normalization.
29+ Until then we put these helper in the `noConfusionType` namespace to indicate that they are not
30+ general purpose.
31+
32+ This module is written in a rather manual style, constructing the `Expr` directly. It's best
33+ read with the expected output to the side.
34+ -/
35+
1136namespace Lean
1237
1338open Meta
0 commit comments