lean4-htt/tests/elab/decideTactic.lean
Sebastian Ullrich 88b746dd48 feat: unfold and rewrap instances in inferInstanceAs and deriving
This PR adjusts the results of `inferInstanceAs` and the `def` `deriving` handler to conform to recently strengthened restrictions on reducibility. This change ensures that when deriving or inferring an instance for a semireducible type definition, the definition's RHS is not leaked when the instance is reduced at lower than semireducible transparency.

More specifically, given the "source type" and "target type" (the given and expected type for `inferInstanceAs`, the right-hand side and applied left-hand side of the `def` for `deriving`), we synthesize an instance for the source type and then unfold and rewrap its components (fields, nested instances) as necessary to make them compatible with the target type. The individual steps are represented by the following options, which all default to enabled and can be disabled to help with porting:
- `backward.inferInstanceAs.wrap`: master switch for instance adjustment in both `inferInstanceAs` and the default `deriving` handler
- `backward.inferInstanceAs.wrap.reuseSubInstances`: reuse existing instances for the target type for sub-instance fields to avoid non-defeq instance diamonds
- `backward.inferInstanceAs.wrap.instances`: wrap non-reducible instances in auxiliary definitions
- `backward.inferInstanceAs.wrap.data`: wrap data fields in auxiliary definitions (proof fields are always wrapped)

This PR is an extension and rewrite of prior work in Mathlib: https://github.com/leanprover-community/mathlib4/pull/36420

Last(?) part of fix for #9077

🤖 Prepared with Claude Code

# Breaking changes

Proofs that relied on the prior "defeq abuse" of these instance or that depended on their specific structure may need adjustments. As `inferInstanceAs A` now needs to know the source and target types exactly before it can continue, it cannot be used anymore as a synonym for `(inferInstance : A)`, use the latter instead when source and target type are identical.
2026-03-22 13:25:46 +01:00

121 lines
3.4 KiB
Text

/-!
# Tests of the `decide` tactic
-/
/-!
Success
-/
#guard_msgs in
example : 2 + 2 ≠ 5 := by decide
/-!
False proposition
-/
/--
error: Tactic `decide` proved that the proposition
1 ≠ 1
is false
-/
#guard_msgs in
example : 1 ≠ 1 := by decide
/-!
Irreducible decidable instance
-/
opaque unknownProp : Prop
/--
error: Tactic `decide` failed for proposition
unknownProp
because its `Decidable` instance
Classical.propDecidable unknownProp
did not reduce to `isTrue` or `isFalse`.
After unfolding the instance `Classical.propDecidable`, reduction got stuck at the `Decidable` instance
Classical.choice ⋯
Hint: Reduction got stuck on `Classical.choice`, which indicates that a `Decidable` instance is defined using classical reasoning, proving an instance exists rather than giving a concrete construction. The `decide` tactic works by evaluating a decision procedure via reduction, and it cannot make progress with such instances. This can occur due to the `open scoped Classical` command, which enables the instance `Classical.propDecidable`.
-/
#guard_msgs in
open scoped Classical in
example : unknownProp := by decide
/-!
Reporting unfolded instances and give hint about Eq.rec.
From discussion with Heather Macbeth on Zulip
-/
structure Nice (n : Nat) : Prop where
(large : 100 ≤ n)
theorem nice_iff (n : Nat) : Nice n ↔ 100 ≤ n := ⟨Nice.rec id, Nice.mk⟩
def baz (n : Nat) : Decidable (Nice n) := by
rw [nice_iff]
infer_instance
instance : Decidable (Nice n) := baz n
/--
error: Tactic `decide` failed for proposition
Nice 102
because its `Decidable` instance
instDecidableNice
did not reduce to `isTrue` or `isFalse`.
After unfolding the instances `baz` and `instDecidableNice`, reduction got stuck at the `Decidable` instance
⋯ ▸ inferInstance
Hint: Reduction got stuck on `▸` (Eq.rec), which suggests that one of the `Decidable` instances is defined using tactics such as `rw` or `simp`. To avoid tactics, make use of functions such as `«inferInstanceAs»` or `decidable_of_decidable_of_iff` to alter a proposition.
-/
#guard_msgs in
example : Nice 102 := by decide
/-!
Following `Decidable.rec` to give better messages
-/
/--
error: Tactic `decide` failed for proposition
¬Nice 102
because its `Decidable` instance
instDecidableNot
did not reduce to `isTrue` or `isFalse`.
After unfolding the instances `baz`, `instDecidableNice`, and `instDecidableNot`, reduction got stuck at the `Decidable` instance
⋯ ▸ inferInstance
Hint: Reduction got stuck on `▸` (Eq.rec), which suggests that one of the `Decidable` instances is defined using tactics such as `rw` or `simp`. To avoid tactics, make use of functions such as `«inferInstanceAs»` or `decidable_of_decidable_of_iff` to alter a proposition.
-/
#guard_msgs in
example : ¬ Nice 102 := by decide
/-!
Reverting free variables.
-/
/--
error: Expected type must not contain free variables
x + 1 ≤ 5
Hint: Use the `+revert` option to automatically clean up and revert free variables
-/
#guard_msgs in
example (x : Nat) (h : x < 5) : x + 1 ≤ 5 := by decide
example (x : Nat) (h : x < 5) : x + 1 ≤ 5 := by decide +revert
/--
Can handle universe levels.
-/
instance (p : PUnit.{u} → Prop) [Decidable (p PUnit.unit)] : Decidable (∀ x : PUnit.{u}, p x) :=
decidable_of_iff (p PUnit.unit) (by constructor; rintro _ ⟨⟩; assumption; intro h; apply h)
example : ∀ (x : PUnit.{u}), x = PUnit.unit := by decide