Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow noncomputable unsafe definitions #3647

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

tydeu
Copy link
Member

@tydeu tydeu commented Mar 10, 2024

Enables the combination of noncomputable unsafe to be used for definitions. Outside of pure theory, noncomputable is also useful to prevent Lean from compiling a definition which will be implemented with external code later. Such definitions may also wish to be marked unsafe if they perform morally impure or memory-unsafe functions.

@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 Mar 10, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Mar 10, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Mar 10, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Mar 10, 2024
@leanprover-community-mathlib4-bot
Copy link
Collaborator

leanprover-community-mathlib4-bot commented Mar 10, 2024

Mathlib CI status (docs):

leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Mar 10, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Mar 10, 2024
@tydeu tydeu marked this pull request as ready for review March 10, 2024 23:51
@tydeu
Copy link
Member Author

tydeu commented Mar 11, 2024

For future reference, here is some additional info on the motivation behind this PR. Alloy allow users to external (C) implementations to definitions via Lean code. This is done via metaprogramming utilities, but is essentially equivalent to a definition followed an attribute [extern] command. For example:

alloy c extern def myAdd (x y : UInt32) : UInt32 := {
  return x + y;
}

is essentially equivalent to:

noncomputable opaque myAdd (x y : UInt32) : UInt32
attribute [extern "alloy_myAdd"] myAdd
alloy c section
LEAN_EXPORT uint32_t alloy_myAdd ( uint32_t x , uint32_t y ) {
  return x + y;
}
end

Libraries which use Alloy may define functions that perform actions that are morally impure or memory-unsafe. Thus, the library creators would like to mark the definition unsafe. However, passing this unsafe through Alloy's macros results in a noncomputable unsafe definition, which Lean currently forbids. For instance, using the above example:

alloy c extern unsafe def myAdd (x y : UInt32) : UInt32

would produce:

noncomputable unsafe opaque myAdd (x y : UInt32) : UInt32

which is forbidden. The goal of this PR is to allow such a use case.


The use of noncomputable in the Alloy's definitions could be avoided by adding the @[extern] attribute to the definition when it is declared. However, this would require some special casing in Alloy, as it also wants to support providing implementations for already-existing definitions (e.g., constructors or projections). Thus, for consistency, it always produces the implementation separately from the definition.

@tydeu tydeu added the will-merge-soon …unless someone speaks up label Mar 11, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Mar 11, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Mar 11, 2024
@tydeu tydeu added this pull request to the merge queue Mar 12, 2024
Merged via the queue into leanprover:master with commit b2ae4bd Mar 12, 2024
25 checks passed
@tydeu tydeu deleted the noncomputable-unsafe branch March 14, 2024 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builds-mathlib CI has verified that Mathlib builds against this PR toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN will-merge-soon …unless someone speaks up
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants