.nib guide std nib 0.3.1-pre

The toolchain at a glance

What happens between a .nib file and an answer — the stages every program passes, and the three ways it can then run.

nibc is one OCaml program. Every command starts the same way, and differs only in what it does once the program is known to be well formed.

The front end

  1. Parse. A Menhir grammar reads each file into an AST. A string literal with a {…} splice is rewritten into a ++ chain of its parts and one show per splice — the one implicit conversion in the language.
  2. Load. use "path.nib" loads files relative to the one that writes it (std/… from the compiler's own library); packages are resolved, names qualified, and a package's requires/offers wall enforced.
  3. Interface check. Every effector is held against the interfaces it says it answers, operation by operation, and every wire binding against the interface on its left.
  4. Type inference. Hindley–Milner with records as rows, plus two things most checkers do not track: the effect row (! Io.Print, what a function may perform) and the raises row (what it may raise). Both are upper bounds the body is held to, both ways.

A program that fails any stage is refused with a diagnostic that ends in a code, [nib-t006] say. The codes are a stable table: nibdoc --list-codes prints it and nibdoc --explain CODE the prose for one. nibc FILE alone checks and prints one ok line; exit status is 0 accepted, 1 refused, 2 a bad command line.

Three ways to run

commandwhat runswhen to use it
nibc --run FILE [--wire W]the walker: the AST, evaluated as writtenalways works; the reference for every answer
nibc build --target nativean executable, through LLVMserving; anything that should be fast
nibc build --target wasm32 / wasm32-wasip2a WebAssembly module or componenta host that runs wasm; sandboxing by construction

The walker is normative. It is an abstract machine whose continuation is a list of defunctionalised data frames, which is what lets a run be journalled, cut and adopted elsewhere (The journal). Both compilers are measured against it: a construct either compiles to code that answers what the walker answers — the same value, the same raise, the same bytes on a socket — or is refused by name. Nothing compiles to something close.

The compilers share one middle: lib/aot.ml lowers a wire's entry, with every function it calls inlined (or, for a recursive one, compiled out of line as a closure), to a small IR of ops over 64-bit locals. Two backends write that IR out — lib/aot.ml itself as WebAssembly, lib/native.ml as LLVM IR — and both link the same runtime, nibrt, a no_std Rust crate (system/tools/nibrt) holding the counted heap, the vector and map slow paths, and, natively, the fibre host.

Flags worth knowing

flagdoes
--run --arg name=valuepasses the entry's parameter (a String, as written)
--run --parallel Nlets parallel kernels use N cores
--run --kernelslists every Vec.map/Vec.fold and whether it can run in parallel
--run --journal-out F / --journal-in Frecords a run / replays one (The journal)
--run --capture F / --adopt Fcuts a run at an operation / continues it in another process
--transcript FILEchecks, then runs every wire and test, printing all of it
--fmt FILEprints the program back as the compiler reads it
build --target native --checklinks the runtime's checking build (traps a double free)