Default instances are not subject to the instance synthesis depth limit
Nobody has claimed this yet.
- Dominant language
- Lean
- Stars
- 9.2k
- Forks
- 990
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 175
Description
Description
When working on examples for the reference manual, I noticed some interesting behavior.
Given this definition:
structure Even where
half : Nat
these (silly, inefficient, but instructive) instances allow literals up to 254 on the latest nightly:
instance ofNatEven0 : OfNat Even 0 where
ofNat := ⟨0⟩
instance ofNatEvenPlusTwo [OfNat Even n] : OfNat Even (n + 2) where
ofNat := ⟨(OfNat.ofNat n : Even).half + 1⟩
That is, these work:
#eval (0 : Even)
#eval (34 : Even)
#eval (254 : Even)
but this doesn't:
#eval (256 : Even)
But adding them as default instances changes things!
attribute [default_instance 100] ofNatEven0
attribute [default_instance 100] ofNatEvenPlusTwo
This now works, giving type Even:
#eval 500
Making that number bigger makes elaboration slower, and heartbeats run out at 892.
Steps to Reproduce
- Use the code above (available here)
- Note that much larger, slower instance synthesis tasks are attempted after the
default_instanceattribute is added.
Expected behavior:
I would expect the same size/depth limits on default instances that there are on other instances.
Actual behavior:
Much larger/deeper synthesis is performed when the instances involved are default.
Versions
The behavior is visible on all three Lean versions on live.lean-lang.org: nightly-2024-10-31, v4.13.0-rc4, and 4.11.0.
This is on live.lean-lang.org.
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 linked live.lean.org reproduction and compare instance synthesis before and after the default_instance attributes are added. Trace the instance-synthesis depth-limit handling for default instances; done means the examples observe the same limit and no longer attempt substantially deeper synthesis.
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