https://github.com/tydeu/lean4-alloy
Write C shims from within Lean code.
https://github.com/tydeu/lean4-alloy
lean4
Last synced: 8 months ago
JSON representation
Write C shims from within Lean code.
- Host: GitHub
- URL: https://github.com/tydeu/lean4-alloy
- Owner: tydeu
- License: apache-2.0
- Created: 2021-11-19T01:54:10.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-07-22T17:10:15.000Z (almost 2 years ago)
- Last Synced: 2024-08-10T14:12:29.163Z (almost 2 years ago)
- Topics: lean4
- Language: Lean
- Homepage:
- Size: 225 KB
- Stars: 47
- Watchers: 4
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Alloy
Alloy is a Lean 4 library that allows one to embed external FFI code (currently just C) directly within Lean. For example, we can define an external C add function like so:
```lean
alloy c extern def myAdd (x y : UInt32) : UInt32 := {
return x + y;
}
```
And Alloy will generate the corresponding C function:
```c
LEAN_EXPORT uint32_t _alloy_c_l_myAdd ( uint32_t x , uint32_t y ) {
return x + y;
}
```
## Building Shims
Alloy exploits Lake's [module facets feature](https://github.com/leanprover/lake/tree/master#defining-new-facets) to automagically build the shim it produces when compiling the module. Combined with the new `precompileModules` feature, this allows the shim code to be directly used by importers in a interpreted context (e.g., for `#eval` or when editing).
To use Alloy with your project and build shims for a library, add the following to your Lakefile:
```lean
require alloy from git "https://github.com/tydeu/lean4-alloy.git"
module_data alloy.c.o.export : BuildJob FilePath
module_data alloy.c.o.noexport : BuildJob FilePath
lean_lib where
precompileModules := true
nativeFacets := fun shouldExport =>
if shouldExport then
#[Module.oExportFacet, `alloy.c.o.export]
else
#[Module.oNoExportFacet, `alloy.c.o.noexport]
-- and whatever other configuration options you wish to add
```
Take a look at the [examples](examples) to see how all of this works. The [my_add](examples/my_add) example provides a minimal setup whereas the [S](examples/S) example provides a more complete demonstration of Alloy's power.
## Disclaimer
Alloy is still a **work-in-progress**. However, it is now at the point where it can be feasible used to build FFIs. Its biggest TODOs are LSP support for the embedded C code and adding more utilities to help with common code patterns (e.g., defining wrapped C structures).