Failed to synthesize instance with a reducible definition, despite the exact instance existing
Nobody has claimed this yet.
- Dominant language
- Lean
- Stars
- 9.2k
- Forks
- 990
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 175
Description
Prerequisites
Please put an X between the brackets as you perform the following steps:
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”)
Description
In the following MWE I've defined an instance instToStringToTypeFoo (foo : Foo) : ToString (foo.toType), where Foo.toType is a reducible definition. Then, when attempting to synthesize an instance of type ToString (?foo.toType), which should match exactly the instance just defined, we get a "failed to synthesize error".
Note that if we remove the reducible annotation on Foo.toString, then synthesis does work as expected.
inductive Foo
| nat
| int
@[reducible]
def Foo.toType : Foo → Type
| .nat => Nat
| .int => Int
instance instToStringToTypeFoo (foo : Foo) : ToString (foo.toType) :=
match foo with
| .nat => inferInstanceAs <| ToString Nat
| .int => inferInstanceAs <| ToString Int
/- This is fine; the instance certainly exists -/
#check (instToStringToTypeFoo _ : ToString <| Foo.toType ?foo)
/-
Yet, the following fails to synthesize, despite it matching exactly the type of the instance above
failed to synthesize
ToString (Foo.toType ?foo)
-/
#synth ToString (Foo.toType ?foo)
Steps to Reproduce
- Define a typeclass instance where the type uses a reducible definition
- Attempt to synthesize an instance of exactly the same type
- Observe failure
Expected behavior: I'd expect typeclass synthesis to succeed, given that an instance of exactly the expected type exists.
Actual behavior: "failed to synthesize" error
Versions
On live.lean-lang.org:
Lean 4.19.0-rc3
Target: x86_64-unknown-linux-gnu
and
Lean 4.20.0-nightly-2025-04-16
Target: x86_64-unknown-linux-gnu
Both have the same behaviour.
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the self-contained Foo example in the issue and run it against the listed Lean versions or the linked live.lean.org reproduction. Compare synthesis with the reducible and non-reducible definitions, then verify that the expected ToString instance is synthesized without regressing the existing #check case.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100