leanprover / leanprover/lean4

Strange error "invalid reassignment, value has type ?m.926 : Type u but is expected to have type Nat : Type"

Open
#8,119 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug do-notation P-low
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

All mutable variables in do blocks seem to be required to have the same universe as the containing Monad (unless unused). Some strange errors may be reported (not sure if they will in all cases), which are neither very helpful nor clear, in cases where this is not true.

Steps to Reproduce

Here is a MWE:

def test.{u, v} {m : Type u → Type v} [Monad m] : m PUnit.{u + 1} := do
  let mut i : Nat := 0
  while true do i := i + 1

Expected behavior: Either of the following:

  • The mutable variable i could be (automatically) boxed into a ULift.{u} that gives it the correct universe (perhaps hard to achieve?), or
  • A clearer error should be printed, hinting at the fact that i has a type that isn't a Type u (required by forIn and bind) therefore is "incompatible" with the containing Monad.

Actual behavior: The following error is reported on the while loop, which isn't clear or helpful unless you know the cause (although it seems to hint at a universe problem)

invalid reassignment, value has type
  ?m.369 : Type u
but is expected to have type
  Nat : Type
Versions

Tested on https://live.lean-lang.org/#project=lean-nightly at version 4.20.0-nightly-2025-04-26 as well as locally at version 4.18.0.

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 reproducer in the issue and run it against the listed Lean nightly and local versions. Investigate mutable variables in do blocks and the while/forIn/bind universe handling; done means either the value is accepted through boxing or the compiler reports a clear universe-related error.

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.