leanprover / leanprover/lean4

matcher created in `def f := ...` body is private but occurs in type

Open
#12,482 3 comments 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

Prerequisites
Description

Consider this situation:

module

public import Std
open Std

public section

def step : Nat × Nat → Nat × Nat
  | (x₁, x₂) => (x₂, x₁ + x₂)

def statesIterator := Iter.repeat (init := (0, 1)) step

/--
error: Unknown constant `_private.«external:file:///lean-nightly/lean-nightly.lean».0.step.match_1`

Note: A private declaration `step.match_1` (from the current module) exists but would need to be public to access here.
-/
#guard_msgs in
def fibIterator := statesIterator.map (fun (current, _) => current)

Here, the iterator fibIterator's type is inferred from its definition. This type also contains the lambda function in the map expression. Therefore, the matcher used by the lambda function, fibIterator.match_1, is going to be contained in fibIterator's type, which is public.

Expected behavior: [Clear and concise description of what you expect to happen]

No errors; fibIterator.match_1 should be public.

Actual behavior: [Clear and concise description of what actually happens]

Error: fibIterator.match_1 was made private and therefore can't occur in fibIterator's public type.

An even more minified example
module

/--
error: Unknown constant `_private.«external:file:///lean-nightly/lean-nightly.lean».0.vec.match_1`

Note: A private declaration `vec.match_1` (from the current module) exists but would need to be public to access here.
-/
#guard_msgs in
public def vec (n : Nat) := Vector.replicate (match n with | 0 => 0 | n' + 1 => n')
Versions
Lean 4.29.0-nightly-2026-02-14
Target: x86_64-unknown-linux-gnu
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

Reproduce the issue with the minimal public def vec example and the step/fibIterator example using the Lean nightly version described, observing the #guard_msgs errors. Trace how the generated matcher is assigned visibility when it appears in an inferred public type; done means both examples elaborate without the private-declaration 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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.