Skip to content

feat(lean): Use FunctionsToConstants#1738

Merged
clementblaudeau merged 4 commits intomainfrom
lean-const-resugaring
Oct 21, 2025
Merged

feat(lean): Use FunctionsToConstants#1738
clementblaudeau merged 4 commits intomainfrom
lean-const-resugaring

Conversation

@clementblaudeau
Copy link
Copy Markdown
Contributor

This PR improves lean support for constants, using the FunctionToConstants resugaring. It removes the ad-hoc treatment of litteral consts.

Overview

A rust const can contain possibly panicking computation, like:

const C3: u32 = if true { 890 } else { 9 / 0 };

However, this is computed at compile time by rustc, so we can expect panic-freedom at extraction-time. In the Lean prelude, we add the two functions:

def isOk {α : Type} (x: Result α) : Bool := match x with
| .ok _ => true
| _ => false

def of_isOk {α : Type} (x: Result α) (h: Result.isOk x): α :=
  by cases x <;> try simp_all <;> assumption

The extracted code hardcodes a proof of panic-freedom by computation (by rfl), which should always work:

def Lean_tests.Constants.C3 :  u32 :=
  Result.of_isOk
  (do if true then pure (890 : u32) else (← (9 : u32) /? (0 : u32)))
  (by rfl)

Closes #1614

@clementblaudeau clementblaudeau added this to the Lean backend v1.0 milestone Oct 9, 2025
@clementblaudeau clementblaudeau requested a review from a team as a code owner October 9, 2025 10:35
@clementblaudeau clementblaudeau added the backend Issue in one of the backends (i.e. F*, Coq, EC...) label Oct 9, 2025
@clementblaudeau clementblaudeau added proof-lib Issues related the backend-specific definitions (in the proof-lib folder) lean Related to the Lean backend or library labels Oct 9, 2025
Copy link
Copy Markdown
Contributor

@maximebuyse maximebuyse left a comment

Choose a reason for hiding this comment

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

Looks good, only one comment.

@clementblaudeau
Copy link
Copy Markdown
Contributor Author

Rebased on main, let's merge.

@clementblaudeau clementblaudeau added this pull request to the merge queue Oct 20, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 20, 2025
@clementblaudeau clementblaudeau added this pull request to the merge queue Oct 21, 2025
Merged via the queue into main with commit 7ec218f Oct 21, 2025
18 checks passed
@clementblaudeau clementblaudeau deleted the lean-const-resugaring branch October 21, 2025 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Issue in one of the backends (i.e. F*, Coq, EC...) lean Related to the Lean backend or library proof-lib Issues related the backend-specific definitions (in the proof-lib folder)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use a resugaring to identify literal consts

2 participants