Delegate independent tasks to specialized agents working concurrently with isolated context.
Lean 4 FFI Constructor Memory Layout
lean4-ffi-constructor-layout
Lean 4 FFI memory layout for constructors with mixed object and scalar fields. Use when: (1) assertion violation "i < lean_ctor_num_objs(o)" accessing constructor fields, (2) assertion violation "offset >= lean_ctor_num_objs(o) * sizeof(void*)" with scalar getters, (3) lean_ctor_get_uint8/uint16/...
SKILL.md
Full skill instructions
Lean 4 FFI Constructor Memory Layout
Problem
When writing C FFI bindings for Lean 4 inductives or structures that contain both object fields (pointers to Lean objects) and scalar fields (small integers like UInt8, UInt16), the memory layout is non-obvious and incorrect assumptions cause assertion failures at runtime.
Context / Trigger Conditions
- Assertion:
i < lean_ctor_num_objs(o)when callinglean_ctor_get(obj, i) - Assertion:
offset >= lean_ctor_num_objs(o) * sizeof(void*)when calling scalar getters lean_ctor_get_uint8,lean_ctor_get_uint16, etc. return garbage values- Creating FFI for inductives like:
inductive SockAddr where | ipv4 (addr : IPv4Addr) (port : UInt16) -- 1 object field, 2 scalar bytes
Solution
Memory Layout Rules
Lean 4 constructors store fields in this order:
- Object fields (pointers): indices 0, 1, 2, ... accessible via
lean_ctor_get(obj, i) - Scalar fields (small integers): stored AFTER all object fields
Scalar Offset Calculation
The offset for scalar fields is relative to the START of the constructor's data, not the start of the scalar section. Therefore:
// For a constructor with N object fields:
scalar_offset = sizeof(void*) * num_object_fields + byte_offset_within_scalars
For the FIRST scalar field, the offset is sizeof(void*) * num_object_fields.
Which Types Are Scalars?
UInt8,UInt16,UInt32,UInt64,Float,Bool→ Scalar in inductivesString,Array, other inductives/structures → Object (pointer)- A structure with ONLY scalar fields (like 4x UInt8) → Still uses constructor, but with 0 object fields
Example: Correct FFI for Mixed Constructor
-- Lean definition
inductive SockAddr where
| ipv4 (addr : IPv4Addr) (port : UInt16) -- IPv4Addr is object, UInt16 is scalar
// WRONG - treats UInt16 as object field
uint16_t port = lean_unbox(lean_ctor_get(addr, 1)); // CRASHES!
// CORRECT - UInt16 is scalar at offset after 1 object field
uint16_t port = lean_ctor_get_uint16(addr, sizeof(void*)); // Works!
Creating Constructors with Mixed Fields
// lean_alloc_ctor(tag, num_object_fields, num_scalar_bytes)
// For SockAddr.ipv4: 1 object field (IPv4Addr), 2 scalar bytes (UInt16)
lean_obj_res result = lean_alloc_ctor(0, 1, 2);
lean_ctor_set(result, 0, ipv4_obj); // Object at index 0
lean_ctor_set_uint16(result, sizeof(void*), port); // Scalar at offset 8 (64-bit)
Structure with Only Scalars
structure IPv4Addr where
a : UInt8
b : UInt8
c : UInt8
d : UInt8
// 0 object fields, 4 scalar bytes - scalar offset starts at 0
lean_obj_res ipv4 = lean_alloc_ctor(0, 0, 4);
lean_ctor_set_uint8(ipv4, 0, a); // offset 0
lean_ctor_set_uint8(ipv4, 1, b); // offset 1
lean_ctor_set_uint8(ipv4, 2, c); // offset 2
lean_ctor_set_uint8(ipv4, 3, d); // offset 3
Verification
- Build passes without Lean assertion violations
- Run tests that exercise the FFI functions
- Values round-trip correctly (create in Lean, read in C, write in C, read in Lean)
Common Mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Treating scalar as object | i < lean_ctor_num_objs(o) assertion | Use lean_ctor_get_uintN instead of lean_ctor_get |
| Wrong scalar offset | offset >= ... assertion or garbage values | Use sizeof(void*) * num_obj_fields as base offset |
| Wrong num_scalar_bytes | Memory corruption, crashes | Count actual bytes: UInt8=1, UInt16=2, UInt32=4, etc. |
Notes
- On 64-bit systems,
sizeof(void*)is 8 - The scalar offset is in BYTES, not in field indices
- Multiple scalars are packed contiguously after the object fields
- Lean's runtime does NOT automatically box small integers in inductives—they're truly stored inline
References
- Lean 4 source:
lean.hcontains the constructor macros with assertions - FFI patterns observed in Lean's own C runtime and mathlib FFI code
More Other skills
brand-guidelines
Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with colors/fonts that you can apply to any artifact that has been creating, or can generate a new theme on-the-fly.
internal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill whenever asked to write some sort of internal communications (status reports, leadership updates, 3P updates, company newsletters, FAQs, incident reports, project updates, etc.).
Fundamental design principles for writing maintainable, testable Python code.
responsive-design
Modern responsive layouts using container queries, fluid typography, CSS Grid, and mobile-first strategies.
Generate Playwright tests from scenarios using interactive browser exploration and validation.
Automated website exploration and test case generation using Playwright.
Modern React state management with Redux Toolkit, Zustand, Jotai, and React Query for every state category.
gdpr-data-handling
GDPR-compliant data handling with consent management, data subject rights, and privacy controls.
Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.
refactor-plan
Detailed multi-file refactor planning with safety checks and rollback guidance.
