feat(rust-engine/lean): monadic phase#1746
Merged
clementblaudeau merged 22 commits intomainfrom Oct 23, 2025
Merged
Conversation
maximebuyse
reviewed
Oct 23, 2025
Contributor
maximebuyse
left a comment
There was a problem hiding this comment.
Looks good! I added a few comments about rust style but the documentation looks good and this phase is a great improvement!
jschneider-bensch
approved these changes
Oct 23, 2025
Contributor
jschneider-bensch
left a comment
There was a problem hiding this comment.
Looks good overall, I think 👍
Just some comments
maximebuyse
reviewed
Oct 23, 2025
maximebuyse
approved these changes
Oct 23, 2025
Contributor
Author
|
Thank you for the reviews! Let's merge. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a new phase to the rust-engine (and use it in the Lean backend) to handle the monadic encoding.
Overview
pure(for values when a computation is expected) andliftnodes (for computations when a value is expected)let x ← pure ..intolet x := ..Example
The following rust code (from the
tests/lean-tests/src/monadic.rsfile) displays interleaving of computations and values. Bellow is the extracted Lean code.Context
Monadic phase
The Lean backend translates rust programs in a monadic style, where each rust computation that could panic is wrapped inside a
Resultmonad : a functionfn f(x:u32) -> u32will be extracted todef f (x:u32) : Result u32. There are two challenges in this encoding :Some expressions cannot panic (literals, consts, constructors for enums, etc) and should be wrapped in the monad. This phase inserts explicit calls to a new Hax primitive
pure. The Lean backend translates it directly to thepureoperator. While lean also has coercions that could be inserted implicitely, they could badly interact with inference, so making them explicit is more robust.Language constructs (if-then-else,
match, etc.) and rust functions, when translated in Lean, still expect rust values as input, not monadic computations. This phase inserts explicit calls toliftto materialize the sub-expressions that return a monadic result where a value is expected. The Lean backend turns them into explicit lifts(← ..), which introduces a monadic bind.Resugaring
Let-bindings of the form
let x = pure e(after the monadic phase) are not extracted aslet x ← pure e, but shortened intolet x := e. The subexpressionewas already outside the do-dsl.Chacha example
The example has been revisited to remove the manually edited file. To circumvent a bug with quotes attached to macro items, and as
i32loops are not supported yet, a couple of manual edits were also made to the rust files (besides the backport of the proofs).