leanprover / leanprover/lean4

Instant stack overflow upon defining type class instance

Open
#6,258 3 comments 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

Please put an X between the brackets as you perform the following steps:

Description

When defining a type class instance (Proper), the program immediately runs into a stack overflow error. It may be related to https://github.com/leanprover/lean4/issues/6229 but the error occurs instantly.

Context

I'm translating the Coq type classes and instances proposed in the paper "A New Look at Generalized Rewriting in Type
Theory" by Matthieu Sozeau.

This relates to this channel: https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Rewriting.20congruent.20relations

Steps to Reproduce
def Relation (α : Sort u) := α → α → Prop

def respectful {α β : Sort u} (r : Relation α) (r' : Relation β) : Relation (α → β) :=
  fun f g => ∀ x y, r x y → r' (f x) (g y)

notation:55 r " ⟹ " r' => respectful r r'

class Proper {α : Sort u} (r : Relation α) (m : α) where
  proper : r m m

class Subrel {α : Sort u} (q r : Relation α) : Prop where
  subrelation : ∀ x y, q x y → r x y

instance : Proper (@Subrel β ⟹ @Subrel (α → β)) sorry := sorry

Expected behaviour: Lean shows error "error: stuck at solving universe constraint"

Actual behavior: Lean Server crashes with "Lean server printed an error: Stack overflow detected. Aborting."

Versions

Lean 4.13.0, Lean 4.14.0-rc1, Lean 4.14.0-rc2, Lean 4.14.0-rc3
Target: arm64-apple-darwin23.6.0 macOS

live.lean-lang.org:
Lean 4.15.0-nightly-2024-11-28
Target: x86_64-unknown-linux-gnu

Additional Information

I tested this locally on linux as well and ran into the same 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 minimal self-contained reproducer in the issue and confirm the behavior in live.lean-lang.org or a current nightly build. Investigate the type class instance elaboration and universe-constraint handling; done means the example reports the expected stuck universe constraint instead of causing a stack overflow or server crash.

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
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.