Skip to content

Conversation

@clementblaudeau
Copy link
Contributor

@clementblaudeau clementblaudeau commented Aug 1, 2025

This PR is a follow-up of #1590. It uses the visitors of #1585 to implement resugarings, i.e. passes over the AST that replace certain patterns with special resugared nodes, allowing for a special treatment by the backend.
The Lean extracted code is made much more readable and close to the Rust code. This PR only defines resugaring for some binary arithmetic operations, as a proof of concept.

Example

For instance, the barrett extracted code goes from

def barrett_reduce (value : i32) : Result i32 := do
  let (t : i64) ← (← hax_machine_int_mul
    (← convert_From_from value)
      BARRETT_MULTIPLIER);
  let (t : i64) ← (← hax_machine_int_add t (← hax_machine_int_shr BARRETT_R 1));
  let (quotient : i64) ← (← hax_machine_int_shr t BARRETT_SHIFT);
  let (quotient : i32) ← (← hax_cast_op quotient);
  let (sub : i32) ← (← hax_machine_int_mul quotient FIELD_MODULUS);
  let (_ : hax_Tuple0) ← hax_Tuple0; (← hax_machine_int_sub value sub)

to

def barrett_reduce (value : i32) : Result i32 := do
  let (t : i64) ← ((← convert_From_from value) *? BARRETT_MULTIPLIER);
  let (t : i64) ← (t +? (BARRETT_R >>>? 1));
  let (quotient : i64) ← (t >>>? BARRETT_SHIFT);
  let (quotient : i32) ← (← hax_cast_op quotient);
  let (sub : i32) ← (quotient *? FIELD_MODULUS); (value -? sub)

@clementblaudeau clementblaudeau changed the title Lean backend 2/3 - Resugaring Lean backend [M2] - 3/3 - Resugarings Aug 6, 2025
@clementblaudeau clementblaudeau linked an issue Aug 21, 2025 that may be closed by this pull request
@clementblaudeau clementblaudeau changed the base branch from main to lean-update-printer August 25, 2025 11:06
@clementblaudeau clementblaudeau marked this pull request as ready for review August 25, 2025 11:08
@clementblaudeau clementblaudeau requested a review from a team as a code owner August 25, 2025 11:08
@clementblaudeau clementblaudeau requested review from maximebuyse and removed request for a team August 25, 2025 11:08
Copy link
Member

@W95Psp W95Psp left a comment

Choose a reason for hiding this comment

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

Thanks, this looks OK to me.
The if/else if chain is a pity, we should probably offer a nicer API in the BinOp resugaring.
But let's address that later.

Base automatically changed from lean-update-printer to main August 25, 2025 11:56
@clementblaudeau clementblaudeau requested review from a team and jschneider-bensch and removed request for a team and maximebuyse August 27, 2025 09:32
@W95Psp W95Psp requested review from keks and removed request for jschneider-bensch August 27, 2025 09:33
@clementblaudeau clementblaudeau requested review from franziskuskiefer and removed request for keks August 27, 2025 09:47
Copy link
Member

@franziskuskiefer franziskuskiefer left a comment

Choose a reason for hiding this comment

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

lgtm, just a few questions and nits

@clementblaudeau clementblaudeau added this pull request to the merge queue Sep 1, 2025
Merged via the queue into main with commit 6b34a68 Sep 1, 2025
17 checks passed
@clementblaudeau clementblaudeau deleted the lean-dev-visitors branch September 1, 2025 12:06
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.

Rust Engine: add first resugaring

3 participants