cubical-transport-hott-lean4/native/canvas-rs/shim.c
Maximus Gorog c2e3ecb3e3
Some checks are pending
Lean Action CI / build (push) Waiting to run
Initial commit: topolei — cubical-transport HoTT in Lean 4 + Rust FFI
Implements the cells-spec vision: a computation space that preserves
auditability, correctness, interactivity. Phase 1 (Lean kernel +
naga-IR Rust backend) is closed; foundation hypothesis stack
(Selection H1+H2, Subobject H3, Trace H5, Obs.Ctx C2, Cubical.Trace)
landed.

Highlights:
- Cubical-HoTT syntax + value/eval/readback in Lean
- naga-IR pipeline (no GLSL string crosses FFI; 17/17 probes pass)
- Honesty audit: every non-transport (sealed cells, vertex shader,
  Y-flip, presentation conventions) is documented as such
- Polymorphic Trace α as free monoid; Cubical.Trace gives
  CTerm → Trace CTerm by structural fold (homomorphism = definition)
- Selection as Huet zipper; Subobject as Boolean algebra over WCell
- All theorems proven; the proof IS the implementation

See STATUS.md for the resume guide.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 20:40:45 -06:00

64 lines
2.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* shim.c — expose Lean's static inline runtime helpers used by the
* canvas crate's FFI (lean_string_cstr, lean_io_result_mk_ok, etc.).
*
* Mirror of `native/cubical/shim.c` + `native/render/shim.c`.
* Names prefixed `topolei_canvas_shim_` to avoid collisions at link.
*/
#include <lean/lean.h>
#include <stdint.h>
#include <string.h>
const char* topolei_canvas_shim_string_cstr(b_lean_obj_arg s) {
return lean_string_cstr(s);
}
lean_obj_res topolei_canvas_shim_io_ok_unit(void) {
return lean_io_result_mk_ok(lean_box(0));
}
/* Build a Lean `RGBA` structure (tag 0) and wrap in `IO.Result.ok`.
*
* Representation: Lean compiles a structure whose fields are all
* `Float` (f64) using **inline unboxed scalar storage** — the four
* doubles are packed directly into the ctor's scalar area as 4 × 8 =
* 32 bytes. `lean_alloc_ctor(tag, num_objs=0, scalar_bytes=32)` +
* writes via `lean_ctor_scalar_cptr` match that layout.
*
* An earlier version of this shim used `lean_alloc_ctor(0, 4, 0)`
* with `lean_box_float` per field — that produces a ctor with 4
* boxed Float objects, which does not match the compiled layout.
* Lean-side reads then saw uninitialised scalar memory (all zeros)
* regardless of what the GPU rendered. If the Lean `RGBA` structure
* ever gains a non-Float field, this shim must be revisited.
*/
lean_obj_res topolei_canvas_shim_io_ok_rgba(double r, double g, double b, double a) {
lean_object* ctor = lean_alloc_ctor(0, 0, 32);
uint8_t* scalars = lean_ctor_scalar_cptr(ctor);
memcpy(scalars + 0, &r, 8);
memcpy(scalars + 8, &g, 8);
memcpy(scalars + 16, &b, 8);
memcpy(scalars + 24, &a, 8);
return lean_io_result_mk_ok(ctor);
}
/* Inductive-walk helpers — expose Lean's static-inline object accessors
* so Rust can traverse inductive data (EMLExpr, EMLPath, etc.) without
* a separate `lean-sys` dependency. Mirror of the same helpers in
* `native/cubical/shim.c`, scoped under the `topolei_canvas_shim_`
* prefix to avoid link collisions when a single binary pulls in both
* crates.
*/
uint32_t topolei_canvas_shim_obj_tag(b_lean_obj_arg o) {
return (uint32_t)lean_obj_tag(o);
}
b_lean_obj_res topolei_canvas_shim_ctor_get(b_lean_obj_arg o, uint32_t idx) {
return lean_ctor_get(o, idx);
}
/* Wrap a C NUL-terminated string in an `IO String` result object. */
lean_obj_res topolei_canvas_shim_mk_string_io(const char* s) {
lean_object* str = lean_mk_string(s);
return lean_io_result_mk_ok(str);
}