leanprover / leanprover/lean4

Default instances are not subject to the instance synthesis depth limit

Open
#5,902 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P-low
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
  1. Use the code above (available here)
  2. Note that much larger, slower instance synthesis tasks are attempted after the default_instance attribute 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.