leanprover / leanprover/lean4

Stack overflow during elaboration

Open
#6,229 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

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

Description

From this example, it there seems there could be an invalid level metavariable assignment during elaboration, one that forms a loop.

Context

Reported on Zulip

Steps to Reproduce
namespace CategoryTheory
universe v v₁ v₂ v₃ u u₁ u₂ u₃ w₀ w₁

class Quiver (V : Type u) where Hom : V → V → Sort v
structure Functor (V : Type u₁) [Quiver.{v₁} V] (W : Type u₂) [Quiver.{v₂} W] where
class Cat (obj : Type u) extends Quiver.{v + 1} obj : Type max u (v + 1) where
structure Iso {C : Type u} [Cat.{v} C] (X Y : C) where

def Functor.id (C : Type u₁) [Cat.{v₁} C] : Functor C C where

instance pi' {I : Type v₁} (C : I → Type u₁) [∀ i, Cat.{v₁} (C i)] :
    Cat (∀ i, C i) := sorry

variable {C : Type u₁} [Cat.{v₁} C] {D : Type u₂} [Cat.{v₂} D]
  (F : Functor C C)

instance Functor.category :
    Cat.{max u₁ v₂} (Functor C D) := sorry

#check Iso Functor.id F

Expected behavior: This raises a normal error.

Actual behavior: At #check there is a stack overflow.

Versions

Lean 4.15.0-nightly-2024-11-26
Target: x86_64-unknown-linux-gnuLean 4

Additional Information

I tested this locally too on macOS. There are tens of thousands of stack frames for libleanshared.dylib`lean::instantiate_lmvars_fn::visit(lean::level const&). This is what leads me to guess that there is an invalid universe level assignment somewhere.

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

Run the self-contained example against the reported Lean nightly version and inspect the #check expression where elaboration overflows. Start with the reported instantiate_lmvars_fn::visit stack frames and investigate the suspected looping universe-level metavariable assignment; done means the example produces a normal error instead of a stack overflow.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.