leanprover / leanprover/lean4

Typeclass search loops with `outParam` and inheritance

Open
#13,038 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P-medium
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

Prerequisites
Description

In the attached example, typeclass search repeatedly tries to apply an instance which is unrelated to the goal, causing a loop. This is the output of trace.Meta.synthInstance:

[Meta.synthInstance] 💥️ BotHomClass (SupHom α) α ▼
  [] new goal BotHomClass (SupHom α) _tc.0 ▶
  [] ✅️ apply @SupBotHomClass.toBotHomClass to BotHomClass (SupHom α) ?m.4 ▶
  [] ✅️ apply @BoundedLatticeHomClass.toSupBotHomClass to SupBotHomClass (SupHom α) ?m.7 ▶
  [] ✅️ apply @Pi.instLattice to Lattice ((i : ?m.10) → ?m.11 i) ▶
  [] ✅️ apply @Pi.instLattice to (i : ?m.10) → Lattice ((i_1 : ?m.13 i) → ?m.14 i i_1) ▶
  [] ✅️ apply @Pi.instLattice to (i : ?m.10) → (i_1 : ?m.13 i) → Lattice ((i_2 : ?m.16 i i_1) → ?m.17 i i_1 i_2) ▶
  [] ✅️ apply @Pi.instLattice to (i : ?m.10) →
        (i_1 : ?m.13 i) → (i_2 : ?m.16 i i_1) → Lattice ((i_3 : ?m.19 i i_1 i_2) → ?m.20 i i_1 i_2 i_3) ▶
  ...
Context

I was trying to redefine BoundedOrderHomClass using inheritance, but a simp timed out when it tried to rewrite using BotHomClass.map_bot.

Steps to Reproduce
class Lattice (α : Type) : Type where

instance Pi.instLattice {ι : Type} {α' : ι → Type} [∀ i, Lattice (α' i)] :
    Lattice (∀ i, α' i) where

class BotHomClass (F : Type) (α : outParam Type) : Prop where
  map_bot : True

class SupHomClass (F α : Type) : Prop where
  map_sup : True

class SupBotHomClass (F α : Type) : Prop
  extends SupHomClass F α, BotHomClass F α where

class BoundedLatticeHomClass (F α : Type) [Lattice α]  : Prop
  extends SupHomClass F α, SupBotHomClass F α where

structure SupHom (α : Type) where

instance (priority := low) SupHom.instBotHomClass {α : Type} : BotHomClass (SupHom α) α where
  map_bot := trivial

variable {α : Type}

/--
error: failed to synthesize
  BotHomClass (SupHom α) ?_
(deterministic) timeout at `typeclass`, maximum number of heartbeats (20000) has been reached

Note: Use `set_option synthInstance.maxHeartbeats <num>` to set the limit.

Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command.

Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command.
-/
#guard_msgs in
set_option pp.mvars false in
#synth BotHomClass (SupHom α) α

Expected behavior: The synthesis should succeed.

Actual behavior: The loop causes a timeout, preventing the low-priority instance from applying.

Versions

Lean 4.29.0-rc6
Target: x86_64-unknown-linux-gnu

Additional Information

[Additional information, configuration or data that might be necessary to reproduce the issue]

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 self-contained reproduction and the trace.Meta.synthInstance output, then run the #synth BotHomClass (SupHom α) α case with the stated heartbeat limit. Trace why the inherited instances and Pi.instLattice recur despite the low-priority instance; done means synthesis succeeds without timing out and the reproduction's expected behavior holds.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.