`constructor` tactic applies a private constructor that `⟨...⟩` and `{ ... }` reject
Prerequisites
Please put an X between the brackets as you perform the following steps:
- [X] Check that your issue is not already filed: https://github.com/leanprover/lean4/issues
- [X] Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
- [X] Test your test case against the latest nightly release, for example on https://live.lean-lang.org/#project=lean-nightly (You can also use the settings there to switch to “Lean nightly”)
Description
A structure with a private constructor cannot be built from another module with ⟨...⟩, with { ... } or with the constructor's name. The constructor tactic builds it there without an error, and the resulting definition compiles and runs. So the term notations and the tactic disagree about whether the constructor is accessible.
The behavior is the same with the module header on both files and with no header on both files. left, right and solve_by_elim behave like constructor.
Context
A private constructor is the usual way to make every value of a type go through a smart constructor of the owning module.
There was a discussion of this on Zulip in 2022: Export smart constructor but also the type. Gabriel Ebner listed ⟨...⟩, { ... } and by constructor as ways around a private constructor. Leonardo de Moura fixed { ... } on that day, called ⟨...⟩ easy to fix, and wrote about by constructor: "Not sure about this one. The scenarios I had in mind are for people using Lean as a programming language. If people start using the tactic framework for coding we may want to support this one too." The two notations now reject the constructor; the tactic was not changed.
#8665 is about reading (casesOn gives access to a private field), not about building a value. #9648 would stop the export of the private parts of an inductive type, which would also take the constructor away from a module that imports the owner; it does not apply to files without the module header.
Steps to Reproduce
A.lean:
module
public structure Token where
private mk ::
value : Nat
public def Token.make (n : Nat) : Token := ⟨n⟩
B.lean:
module
import A
-- Rejected, as expected: the constructor is private.
example : Token := ⟨1⟩
example : Token := { value := 1 }
example : Token := Token.mk 1
-- Accepted: the tactic applies the private constructor.
def forged : Token := by constructor; exact 41
#print forged
#eval forged.value
lean -o A.olean A.lean, thenLEAN_PATH=. lean B.lean.
Expected behavior: by constructor is rejected in B.lean like the three terms above it, with an error that says that the constructor is private.
Actual behavior: The three terms are rejected and the tactic is accepted:
B.lean:5:19: error: Invalid `⟨...⟩` notation: Constructor for `Token` is marked as private
B.lean:6:19: error: invalid {...} notation, constructor for `Token` is marked as private
B.lean:7:19: error(lean.unknownIdentifier): Unknown constant `Token.mk`
private def forged : Token :=
{ value := 41 }
41
Versions
Lean (version 4.36.0-nightly-2026-10-10, arm64-apple-darwin24.6.0, commit b8182f6428135d8de524313001fffe7434bfacad, Release) macOS 27.0.1
Additional Information
Without the module header (remove module and public from both files) the output is the same, except that #print shows def forged.
The two notations call isInaccessiblePrivateName on the constructor (src/Lean/Elab/BuiltinNotation.lean, src/Lean/Elab/StructInst.lean). Lean.MVarId.constructorCore and Lean.MVarId.nthConstructor take the constructor names from the InductiveVal and apply them without that check.
The same check also answers for the public scope of the owning module: there @[expose] public def t : Token := by constructor; exact 1 is accepted, while ⟨1⟩, { value := 1 } and Token.mk 1 in the same position are rejected.
A metaprogram that names the constructor itself is a different matter and is not what this issue is about.
Impact
Add :+1: to issues you consider important. If others are impacted by this issue, please ask them to add :+1: to it.