xiaohongshu
xiaohongshu
ffi-bindings
Create FFI bindings between Lean 4 and C code. Use when working with foreign functions, native libraries, Metal, or system APIs.
Full skill instructions
Create Foreign Function Interface bindings between Lean 4 and C.
-- Define an opaque type backed by a native pointer
opaque WindowPointed : NonemptyType
def Window : Type := WindowPointed.type
instance : Nonempty Window := WindowPointed.property
-- Simple function
@[extern "lean_window_create"]
opaque Window.create : UInt32 → UInt32 → String → IO Window
-- Function returning a value
@[extern "lean_window_get_size"]
opaque Window.getSize : Window → IO (UInt32 × UInt32)
-- Function with no return value
@[extern "lean_window_close"]
opaque Window.close : Window → IO Unit
-- Pure function (no IO)
@[extern "lean_add_vectors"]
opaque Vec3.add : Vec3 → Vec3 → Vec3
#include <lean/lean.h>
// Global class pointer (initialized once)
static lean_external_class* g_window_class = NULL;
// Destructor called when Lean GC collects the object
static void window_finalizer(void* ptr) {
Window* window = (Window*)ptr;
window_destroy(window);
}
// Initialize the class (call once at startup)
static void ensure_class_initialized() {
if (g_window_class == NULL) {
g_window_class = lean_register_external_class(
window_finalizer, // destructor
NULL // foreach (for nested lean objects)
);
}
}
LEAN_EXPORT lean_obj_res lean_window_create(
uint32_t width,
uint32_t height,
lean_obj_arg title_obj,
lean_obj_arg world
) {
ensure_class_initialized();
// Extract string from Lean
const char* title = lean_string_cstr(title_obj);
// Create native object
Window* window = window_new(width, height, title);
// Wrap in Lean external object
lean_object* result = lean_alloc_external(g_window_class, window);
return lean_io_result_mk_ok(result);
}
LEAN_EXPORT lean_obj_res lean_window_close(
lean_obj_arg window_obj,
lean_obj_arg world
) {
// Extract native pointer
Window* window = (Window*)lean_get_external_data(window_obj);
window_close(window);
return lean_io_result_mk_ok(lean_box(0));
}
LEAN_EXPORT lean_obj_res lean_window_get_size(
lean_obj_arg window_obj,
lean_obj_arg world
) {
Window* window = (Window*)lean_get_external_data(window_obj);
uint32_t w = window_get_width(window);
uint32_t h = window_get_height(window);
// Create tuple (UInt32 × UInt32)
lean_object* pair = lean_alloc_ctor(0, 2, 0);
lean_ctor_set(pair, 0, lean_box_uint32(w));
lean_ctor_set(pair, 1, lean_box_uint32(h));
return lean_io_result_mk_ok(pair);
}
// Box a float for return
lean_object* boxed = lean_box_float(value);
// Unbox a float from argument
double value = lean_unbox_float(float_obj);
// Return Float × Float tuple
lean_object* pair = lean_alloc_ctor(0, 2, 0);
lean_ctor_set(pair, 0, lean_box_float(x));
lean_ctor_set(pair, 1, lean_box_float(y));
// Create a Lean array
lean_object* arr = lean_mk_empty_array();
for (int i = 0; i < count; i++) {
arr = lean_array_push(arr, lean_box_uint32(values[i]));
}
// Read from Lean array
size_t len = lean_array_size(arr);
for (size_t i = 0; i < len; i++) {
lean_object* elem = lean_array_get_core(arr, i);
uint32_t val = lean_unbox_uint32(elem);
}
#!/bin/bash
set -e
# Set compiler for FFI
export LEAN_CC=/usr/bin/clang
# For macOS frameworks
export LIBRARY_PATH="/opt/homebrew/lib:$LIBRARY_PATH"
# Build with Lake
lake build
# Or for Metal projects on macOS
# clang -c -o ffi.o ffi.c $(pkg-config --cflags lean4)
# clang -shared -o libffi.dylib ffi.o -framework Metal -framework Foundation
// Return none
return lean_io_result_mk_ok(lean_mk_option_none());
// Return some value
return lean_io_result_mk_ok(lean_mk_option_some(value));
// Return IO error
if (error) {
lean_object* err = lean_mk_io_user_error(
lean_mk_string("Error message")
);
return lean_io_result_mk_error(err);
}
Reference implementations:
graphics/afferent - Metal GPU renderinggraphics/terminus - Terminal I/Odata/quarry - SQLite bindingsaudio/fugue - AudioToolboxnetwork/legate - gRPCxiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience