Skip to content

feat: pluggable pure/bind builders for do elaboration#13507

Merged
sgraf812 merged 2 commits into
masterfrom
sg/pluggable-do-ops
Apr 23, 2026
Merged

feat: pluggable pure/bind builders for do elaboration#13507
sgraf812 merged 2 commits into
masterfrom
sg/pluggable-do-ops

Conversation

@sgraf812
Copy link
Copy Markdown
Contributor

This PR exposes the Pure.pure / Bind.bind applications emitted by the do elaborator as pluggable closures, so external surface syntaxes (e.g. an ido notation for indexed monads) can reuse the full do machinery while emitting alternate constants.

Context carries a new DoOps record (wrapped via an opaque DoOpsRef to break the cycle with DoElabM) with mkPureApp, mkBindApp, and isPureApp? fields. mkPureApp and mkBindApp become thin dispatchers; the original bodies move to DoOps.default. isPureApp? returns the pure value as an Expr rather than a Bool, so overrides aren't locked into Pure.pure's 4-argument layout. A new elabDoWith entry point takes a DoOps plus a doSeq; elabDo is now elabDoWith .default applied to a matched (do $doSeq).

Control-flow features (mut, return, break, continue, for) and the transformer stack (StateT, OptionT, ExceptT, EarlyReturnT, BreakT, ContinueT) remain hard-coded to Monad; generalising them is deferred to a follow-up. A new tests/elab/doNotationPluggableOps.lean registers an Atkey-style indexed monad and an ido surface syntax that drives elabDoWith, covering the forms of do that are supported under the minimal scope.

This PR exposes the `Pure.pure` / `Bind.bind` applications emitted by the `do` elaborator as pluggable closures, so external surface syntaxes (e.g. an `ido` notation for indexed monads) can reuse the full `do` machinery while emitting alternate constants.

`Context` carries a new `DoOps` record (wrapped via an opaque `DoOpsRef` to break the cycle with `DoElabM`) with `mkPureApp`, `mkBindApp`, and `isPureApp?` fields. `mkPureApp` and `mkBindApp` become thin dispatchers; the original bodies move to `DoOps.default`. `isPureApp?` returns the pure value as an `Expr` rather than a `Bool`, so overrides aren't locked into `Pure.pure`'s 4-argument layout. A new `elabDoWith` entry point takes a `DoOps` plus a `doSeq`; `elabDo` is now `elabDoWith .default` applied to a matched ``(do $doSeq)``.

Control-flow features (`mut`, `return`, `break`, `continue`, `for`) and the transformer stack (`StateT`, `OptionT`, `ExceptT`, `EarlyReturnT`, `BreakT`, `ContinueT`) remain hard-coded to `Monad`; generalising them is deferred to a follow-up. A new `tests/elab/doNotationPluggableOps.lean` registers an Atkey-style indexed monad and an `ido` surface syntax that drives `elabDoWith`, covering the forms of `do` that are supported under the minimal scope.
@sgraf812 sgraf812 added the changelog-language Language features and metaprograms label Apr 22, 2026
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Apr 22, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase cae4decead7dae180b50b25f2225419a1ed4280e --onto b6f5892e2247b3ca3c9a535e286f26121656d10f. You can force Mathlib CI using the force-mathlib-ci label. (2026-04-22 21:55:09)

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase cae4decead7dae180b50b25f2225419a1ed4280e --onto 3fc99eef102549c743c6c63547983319bfae6f01. You can force reference manual CI using the force-manual-ci label. (2026-04-22 21:55:11)

@sgraf812 sgraf812 added this pull request to the merge queue Apr 23, 2026
@sgraf812 sgraf812 removed this pull request from the merge queue due to a manual request Apr 23, 2026
@sgraf812
Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented Apr 23, 2026

Benchmark results for 72ef5c4 against cae4dec are in. No significant results found. @sgraf812

  • 🟥 build//instructions: +320.9M (+0.00%)

Small changes (1🟥)

  • 🟥 build/module/Lean.Elab.Do.Basic//instructions: +289.3M (+2.16%) (reduced significance based on *//lines)

@sgraf812 sgraf812 added this pull request to the merge queue Apr 23, 2026
Merged via the queue into master with commit 525021c Apr 23, 2026
19 checks passed
@sgraf812 sgraf812 deleted the sg/pluggable-do-ops branch April 23, 2026 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-language Language features and metaprograms toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants