Skip to content

feat: tighten inductive FFI description#863

Open
Kha wants to merge 2 commits into
mainfrom
push-kwpruqwrqpxp
Open

feat: tighten inductive FFI description#863
Kha wants to merge 2 commits into
mainfrom
push-kwpruqwrqpxp

Conversation

@Kha

@Kha Kha commented Jun 12, 2026

Copy link
Copy Markdown
Member

Addresses both #13679 and more general compiler freedom


If an inductive type has exactly one constructor, and that constructor has exactly one run-time relevant parameter, then the inductive type is represented identically to its parameter.

For a public inductive type under the module system, any private information that prevents these conditions from being tested in other modules will disable this optimization even in the current module.

@david-christiansen david-christiansen Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This took me some time to parse out and think through, so let me unpack my understanding to check for correctness:

If a public inductive type is defined in a module and its constructor is private, then this optimization is not applied even in the module that defines it. This is because the ABI of a module's types is determined by the module's interface (that is, its public information). If the optimization were applied only in the defining module, then this would lead to an ABI mismatch. Furthermore, the structure of the private constructor could change without affecting the module's public information. Making the entire type private or its constructor public re-enables the optimization because all clients are able to decide this.

Is that a correct understanding? If so, then presumably one could work around it by having a non-exposed public def that names the type externally? If not, then why not?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct! But a non-exposed type def is not sufficient as judging its run-time relevancy still requires accessing private data

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which actually is the reason such a non-exposed type def is currently denied if the RHS is not publicly imported. It would be nice to lift both restrictions at once in the future...

Comment thread Manual/Language/InductiveTypes.lean Outdated
Co-authored-by: David Thrane Christiansen <david@lean-fro.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants