lean4-htt/tests/lean/run/decEqNonInjIndex.lean
Joachim Breitner ccb8568756
feat: linear-size DecidableEq instance (#10152)
This PR introduces an alternative construction for `DecidableEq`
instances that avoids the quadratic overhead of the default
construction.

The usual construction uses a `match` statement that looks at each pair
of constructors, and thus is necessarily quadratic in size. For
inductive data type with dozens of constructors or more, this quickly
becomes slow to process.

The new construction first compares the constructor tags (using the
`.ctorIdx` introduced in #9951), and handles the case of a differing
constructor tag quickly. If the constructor tags match, it uses the
per-constructor-eliminators (#9952) to create a linear-size instance. It
does so by creating a custom “matcher” for a parallel match on the data
types and the `h : x1.ctorIdx = x2.ctorIdx` assumption; this behaves
(and delaborates) like a normal `match` statement, but is implemented in
a bespoke way. This same-constructor-matcher will be useful for
implementing other instances as well.

The new construction produces less efficient code at the moment, so we
use it only for inductive types with 10 or more constructors by default.
The option `deriving.decEq.linear_construction_threshold` can be used to
adjust the threshold; set it to 0 to always use the new construction.
2025-09-03 06:31:49 +00:00

58 lines
2 KiB
Text

/-!
This test checks what deriving `DecidableEq` does when the inductive type has
non-injective indices, and just how bad the error messages are.
-/
opaque f : Nat → Nat
set_option deriving.decEq.linear_construction_threshold 0
/--
error: Tactic `cases` failed with a nested error:
Dependent elimination failed: Failed to solve equation
f n✝¹ = f n✝
at case `T.mk1` after processing
_, (T.mk1 _ _), _
the dependent pattern matcher can solve the following kinds of equations
- <var> = <term> and <term> = <var>
- <term> = <term> where the terms are definitionally equal
- <constructor> = <constructor>, examples: List.cons x xs = List.cons y ys, and List.cons x xs = List.nil
---
error: Dependent elimination failed: Failed to solve equation
f n✝ = f n
-/
#guard_msgs(pass trace, all) in
inductive T : (n : Nat) → Type where
| mk1 : Fin n → T (f n)
| mk2 : Fin (2*n) → T (f n)
deriving BEq, DecidableEq
set_option deriving.decEq.linear_construction_threshold 10000
/--
error: Tactic `cases` failed with a nested error:
Dependent elimination failed: Failed to solve equation
f n✝¹ = f n✝
at case `T'.mk1` after processing
_, (T'.mk1 _ _), _
the dependent pattern matcher can solve the following kinds of equations
- <var> = <term> and <term> = <var>
- <term> = <term> where the terms are definitionally equal
- <constructor> = <constructor>, examples: List.cons x xs = List.cons y ys, and List.cons x xs = List.nil
---
error: Tactic `cases` failed with a nested error:
Dependent elimination failed: Failed to solve equation
f n✝¹ = f n✝
at case `T'.mk1` after processing
_, (T'.mk1 _ _), _
the dependent pattern matcher can solve the following kinds of equations
- <var> = <term> and <term> = <var>
- <term> = <term> where the terms are definitionally equal
- <constructor> = <constructor>, examples: List.cons x xs = List.cons y ys, and List.cons x xs = List.nil
-/
#guard_msgs(pass trace, all) in
inductive T' : (n : Nat) → Type where
| mk1 : Fin n → T' (f n)
| mk2 : Fin (2*n) → T' (f n)
deriving BEq, DecidableEq