Quilt on the GPU. Metal is Apple's shader language — the language that runs on every iPhone, iPad, and Mac GPU. It's a language for parallel computation, with
kernelfunctions,buffers, and athreadgroupmodel. When you express the Quilt cell model in Metal, the cells stop being nodes in a graph and become buffers on a GPU. The formulas are kernels that run in parallel. The reactive engine is a command buffer.
Why • Philosophy • What works • Workarounds • Deeper understanding
Most reactive engines are sequential. Even when they have parallel features, the parallelism is on the CPU, and the CPU has 8 or 16 or 64 cores. The GPU has thousands.
Metal is a language for writing code that runs on thousands of cores at once. It was designed for graphics, but the model — kernels that operate on buffers in parallel — is the same model Quilt needs. A cell value is a buffer. A formula is a kernel. A reactive engine is a command buffer that schedules the kernels.
When you express Quilt in Metal, you discover something profound: cells are inherently parallel. The reactive engine walks a graph, but each formula evaluation is independent. The GPU is the natural runtime. The cell model was always a GPU program; we just didn't know it.
This repo is the polyformalism port of Quilt to Metal. The same model, expressed in the language that runs on every Apple device's GPU. The polyformalism insight: cells are inherently parallel. The GPU is the natural runtime.
The Quilt model is language-independent. A cell is a typed unit of state. A formula is a function over dependencies. A listener is a callback. A sheet is a graph. The polyformalism question is: what does the model look like in a language where parallelism is the default?
The Metal version emphasizes:
- Cells are buffers. A
ValueCell<float>is abuffer<float>in Metal. The polyformalism insight: the cell value is a buffer; the buffer is a piece of GPU memory. - Formulas are kernels. A
FormulaCell<T>is akernelfunction that reads from input buffers and writes to an output buffer. The polyformalism insight: the formula is a kernel; the kernel is a parallel function. - Dependencies are buffer bindings. A formula's dependencies are the buffers it reads. The polyformalism insight: the dependency is a buffer binding; the binding is a runtime check.
- The reactive engine is a command buffer. A command buffer is a sequence of kernel dispatches. The polyformalism insight: the reactive engine is a command buffer; the command buffer is a sequence of GPU commands.
- Threads are cells. A Metal thread executes one cell. A
threadgroupexecutes a group of related cells. The polyformalism insight: the thread is the cell; the cell is the thread.
The polyformalism insight: Metal is the language where the cell model is literally parallel. A kernel runs on every thread at once. A sheet of 10,000 cells is a 10,000-thread dispatch. The polyformalism insight: cells want to be parallel; Metal says it in hardware.
A cell value in Metal is a buffer. The buffer is a piece of GPU memory, addressable by every thread.
#include <metal_stdlib>
using namespace metal;
// A cell value is a buffer
struct ValueCell {
device float* value; // the value, one per cell
constant char* path; // the path, one per cell
};
// A sheet is an array of cells
struct Sheet {
device ValueCell* cells;
uint count;
};The device float* is a pointer to GPU memory. The constant char* is a pointer to GPU-read-only memory. The polyformalism insight: the cell value is a buffer; the buffer is a piece of GPU memory.
Polyformalism insight: in TypeScript, the cell value is a property of an object. In Metal, the cell value is a buffer. The shapes are the same; Metal gives you the parallelism for free.
A formula in Metal is a kernel function. It takes buffers as arguments and writes to an output buffer.
// A formula is a kernel
kernel void compute_savings(
device float* income [[buffer(0)]],
device float* expenses [[buffer(1)]],
device float* savings [[buffer(2)]],
uint id [[thread_position_in_grid]])
{
savings[id] = income[id] - expenses[id];
}The [[buffer(0)]] attribute says: this is buffer 0. The [[thread_position_in_grid]] says: this thread is cell id. The kernel runs once per cell. The polyformalism insight: the formula is a kernel; the kernel is a parallel function.
Polyformalism insight: in TypeScript, the formula is a function that runs on one CPU. In Metal, the formula is a kernel that runs on thousands of GPU threads. The shapes are the same; Metal gives you the parallelism.
A command buffer is a sequence of kernel dispatches. The reactive engine is a command buffer.
// In the host code (Swift or C++)
MTLCommandBuffer* cmd = queue.commandBuffer();
// Schedule the formula kernels
id<MTLComputeCommandEncoder> encoder = cmd.computeCommandEncoder();
[encoder setComputePipelineState:savingsPipeline];
[encoder setBuffer:incomeBuffer offset:0 atIndex:0];
[encoder setBuffer:expensesBuffer offset:0 atIndex:1];
[encoder setBuffer:savingsBuffer offset:0 atIndex:2];
[encoder dispatchThreads:MTLSizeMake(numCells, 1, 1)
threadsPerThreadgroup:MTLSizeMake(64, 1, 1)];
[encoder endEncoding];
[cmd commit];
[cmd waitUntilCompleted];The polyformalism insight: the reactive engine is a command buffer; the command buffer is a sequence of GPU commands.
Polyformalism insight: in TypeScript, the reactive engine is a function call. In Metal, it's a command buffer. The shapes are the same; Metal gives you the GPU.
A threadgroup is a group of threads that can share memory. Cells that depend on each other can be in the same threadgroup.
kernel void compute_savings_threadgroup(
device float* income [[buffer(0)]],
device float* expenses [[buffer(1)]],
device float* savings [[buffer(2)]],
threadgroup float* shared [[threadgroup(0)]],
uint id [[thread_position_in_threadgroup]],
uint group_id [[threadgroup_position_in_grid]])
{
// Load to shared memory
shared[id] = income[group_id * 64 + id] - expenses[group_id * 64 + id];
threadgroup_barrier(mem_flags::mem_threadgroup);
// Write to output
savings[group_id * 64 + id] = shared[id];
}The polyformalism insight: the threadgroup is the cell cluster; the cell cluster is the threadgroup.
Polyformalism insight: in TypeScript, you batch cells with an array. In Metal, you batch them with a threadgroup. The shapes are the same; Metal gives you the shared memory.
Metal has SIMD types (float4, int4, etc.). A cell value can be a SIMD vector. The formula processes 4 floats at a time.
kernel void compute_savings_simd(
device float4* income [[buffer(0)]],
device float4* expenses [[buffer(1)]],
device float4* savings [[buffer(2)]],
uint id [[thread_position_in_grid]])
{
savings[id] = income[id] - expenses[id];
}The polyformalism insight: the cell value is a vector; the formula is SIMD.
Polyformalism insight: in Mojo, SIMD is a type parameter. In Metal, SIMD is the default. The shapes are the same; Metal gives you the SIMD without asking.
A Metal program has two parts: the host code (Swift or C++) that sets up the buffers and dispatches the kernels, and the device code (the kernels themselves) that runs on the GPU. The cell model has to be expressed in both.
Workaround: Use Swift (or Objective-C) for the host code. The @Quilt/Metal package provides the host-side API. The Metal Shading Language (.metal) files provide the device-side kernels.
Insight gained: the cell model is split across two languages. The polyformalism insight: the cell model is not a single-language thing; it's a model that crosses the host-device boundary.
A Metal kernel cannot call another kernel dynamically. You have to know at dispatch time which kernels to run.
Workaround: Build a "command queue" in the host code. The reactive engine walks the graph in the host, dispatches the appropriate kernel for each formula. The GPU is the compute engine; the CPU is the scheduler.
Insight gained: the cell model's reactive engine is a CPU-side graph walk. The polyformalism insight: the cell model is a hybrid: CPU for scheduling, GPU for compute.
Buffers have to be allocated, filled, and freed. The cell model has to manage GPU memory.
Workaround: Use a MTLBufferPool to recycle buffers. Or use the ResidencySet API (iOS 18+) to manage buffer residency automatically. The polyformalism insight: the cell model is a memory story.
Insight gained: GPU memory is precious. The polyformalism insight: the cell model is a memory story; Metal makes you write it.
A Metal kernel cannot recursively call itself. A formula cannot directly call another formula.
Workaround: Use multiple kernel dispatches. The reactive engine schedules them in topological order. The polyformalism insight: the cell model is a DAG of kernel dispatches; the DAG is the engine.
Insight gained: the cell model is a DAG. The polyformalism insight: Metal forces you to make the DAG explicit; other languages hide it.
After implementing Quilt in Metal, the following insights emerge:
-
Cells are inherently parallel. Every cell evaluation is independent. The polyformalism insight: the cell model is parallel; the GPU is the natural runtime.
-
The cell value is a buffer. A buffer is a piece of GPU memory. The polyformalism insight: the cell value is a buffer; the buffer is a parallel data structure.
-
The formula is a kernel. A kernel is a parallel function. The polyformalism insight: the formula is a kernel; the kernel is the cell's brain.
-
The reactive engine is a command buffer. A command buffer is a sequence of GPU commands. The polyformalism insight: the reactive engine is a command buffer; the command buffer is the engine.
-
The thread is the cell. Each thread executes one cell. The polyformalism insight: the thread is the cell; the cell is the thread.
-
The polyformalism is about parallelism. Other languages reveal different facets. Metal reveals the parallel facet. The polyformalism insight: the cell model is parallel; Metal is the language that says it in hardware.
🧠 Neural network inference — A neural network is a Quilt sheet. The cells are the activations. The formulas are the layer computations. The kernels are the layer implementations. The polyformalism insight: a neural network is a Quilt sheet on the GPU.
🎮 Real-time graphics — A game's lighting model is a Quilt sheet. The cells are the light sources. The formulas are the lighting calculations. The kernels are the shaders. The polyformalism insight: a game engine is a Quilt sheet running on the GPU.
📊 Financial simulation — A Monte Carlo simulation is a Quilt sheet. The cells are the scenarios. The formulas are the price updates. The kernels are the scenario evaluations. The polyformalism insight: a Monte Carlo is a Quilt sheet with 10,000,000 cells.
🧬 Scientific simulation — A molecular dynamics simulation is a Quilt sheet. The cells are the molecules. The formulas are the force calculations. The kernels are the MD kernels. The polyformalism insight: a molecular dynamics is a Quilt sheet where the cells move.
quilt-metal is the GPU layer of the Quilt polyformalism stack. The full set of 12 languages:
| Language | What it reveals | What needs workarounds |
|---|---|---|
| TypeScript (canonical) | Reactive evaluation is natural | Hides the state machine structure |
| Tutor (1970) | Multi-user cells are the default | No closures, no async, no nested data |
| Pydantic-AI (Python) | Type safety is the runtime contract | Async overhead, slow interpreter |
| Mojo | Cells are types, not values | Young language, moving target |
| Julia | The reactive engine is a type system | No first-class sheets, compile latency |
| Chapel | Cells are inherently parallel | Verbose syntax, small ecosystem |
| COBOL | Hierarchical cells map to divisions | No closures |
| C | The mathematical core | No type safety, manual memory |
| C++ | Type-erased cell kinds | Templates explode, inscrutable errors |
| C# | The enterprise-friendly version | Heavy runtime, immutable records |
| Metal (this) | Cells are inherently parallel; the GPU is the natural runtime | Host-device split, no dynamic dispatch, buffer management |
| Swift | Actors = cell isolation | Verbose for simple sheets |
The Metal version is for the GPU — the case where you have thousands of cells and need thousands of cores. The cell model is the same; the runtime is different.
If you've ever wanted a reactive engine that runs on the GPU. If you've ever wanted the cells to be buffers and the formulas to be kernels. If you've ever wanted to dispatch 10,000 cells in one command.
This is for you.
Metal is the language that says: the cell model is parallel. The GPU is the natural runtime. The polyformalism insight: cells are buffers. Formulas are kernels. The reactive engine is a command buffer. The thread is the cell. The cell is the thread.
Apache 2.0. See LICENSE.
Cells are buffers. Formulas are kernels. The reactive engine is a command buffer. The thread is the cell. The GPU is the runtime.
