1Pricing is arithmetic, so the engine may split it
price, house and takings have no
! in their signatures, and the compiler holds them to that: each
reads only its arguments and changes nothing. That proof is what lets the
engine, rather than the author, cut a loop into chunks across cores.
use "std/iter.nib"module Box where func price :: seat: Int -> Int = if seat <= 2 then 4800 else 3200 func house :: seats: Vec<Int> -> Vec<Int> = Vec.map(seats, { s -> if s <= 2000 then 4800 else 3200 }) func takings :: prices: Vec<Int> -> Int = Vec.fold(prices, 0, { a, p -> a + p }) func season :: -> Int = takings(house(Iter.to_vec(Iter.range(1, 20001)))) wire Season runs Box.seasonNothing in the code asks for a core. --kernels shows what the
engine decided for each loop, --parallel 4 is the operator's
budget, and the answer is the same at any setting.
$ nibc --run box.nib --wire Season --kernels --parallel 4
kernel box.nib:6:5 Vec.map parallel may run in chunks on --parallel lanes
kernel box.nib:9:5 Vec.fold parallel may run in chunks on --parallel lanes
67200000
Today a Vec.map or
Vec.fold whose body is arithmetic.
Designed for whole pure functions run side by
side, or on another machine.
2Selling a seat needs the world, so the function says so
sell changes something, so it can't be pure. It doesn't
reach for a database: it performs @Seats.hold, an operation of
the Seats interface, and lists Seats after
!. An interface has no body, and nothing inside
Box can choose who answers it.
use "std/env.nib"use "std/io.nib"⋯interface Seats where hold(seat: Int, who: String) -> Int⋯ func sell :: seat: Int, who: String -> Int ! Seats, Io.Print = let s = @Seats.hold(seat, who); let _ = @Io.Print.println("{who}: seat {s}, {price(s)} cents"); price(s) func main :: -> Int ! Seats, Env.Vars, Io.Print = let who = try @Env.Vars.get("WHO") rescue Err.Missing(_) => "nobody" end; sell(2, who) + sell(3, "bo")The file compiles: it asks, and nobody has answered yet.
$ nibc box.nib
box.nib: ok — parsed, typed, effects and wires checked
3The hall holds the state, and the wire says who answers
Hall is an effector, the only kind of code that may own a
var. Its one cell, owner, is keyed by seat, so
every write names its address: owner[seat]. The runtime knows
which state each operation touches, and refuses a second operation on a key
while the first is still in flight.
effector Hall is Seats where var owner :: Int => String = {1: "", 2: "", 3: "", 4: ""}; hold(seat, who) = owner[seat] := who; seat⋯wire Box runs Box.main where Seats = Hall, Env.Vars = Env.Process, Io.Print = Io.StdoutThe wire Box is the one place an answer is chosen, and it
must cover everything main asks for.
$ WHO=ada nibc --run box.nib --wire Box
ada: seat 2, 4800 cents
bo: seat 3, 3200 cents
8000
Leave one line out of the wire and the program does not compile. The message names what nobody answered.
$ nibc box.nib
Type error at box.nib:35:1: Wire 'Box' entry 'Box.main' requires effectors [Box.Seats, Env.Vars, Io.Print] but the mapping supplies [Box.Seats, Io.Print]: add Env.Vars [nib-t000]
Today per-key claims make interleaving safe on one core. Designed for operations on different keys running on different cores at once.
4When a seat is taken, the caller decides
Bo asks for seat 2, which Ada already has. choose notices and
raises Taken, but it doesn't decide what happens next.
main does: resume(s + 1) hands seat 3 back to the
point that raised, and the sale carries on from there. Nothing unwinds.
data Taken where Taken(Int)⋯ free(seat: Int) -> Bool;⋯ free(seat) = owner[seat] == ""⋯ func choose :: seat: Int -> Int ! Seats raises Taken = if @Seats.free(seat) then seat else raise Taken(seat) func sell :: seat: Int, who: String -> Int ! Seats, Io.Print raises Taken = let s = @Seats.hold(choose(seat), who);⋯ try sell(2, who) + sell(2, "bo") rescue Taken(s) => resume(s + 1) end$ WHO=ada nibc --run box.nib --wire Box
ada: seat 2, 4800 cents
bo: seat 3, 3200 cents
8000
A handler can also answer for the whole try and drop the
work, or hold the rest of the work as a value and run it later, more than
once if it likes.
5Every answer from the world is on the record
The marked lines are where the program crosses the seam. Each crossing is
a declared operation, so --journal-out can write every one of
them down: the buyer's name, each seat check, each hold, each line
printed.
- 1Env.Vars.get("WHO")"ada"
- 2Box.Seats.free(2)true
- 3Box.Seats.hold(2, "ada")2
- 4Io.Stdout.println("ada: seat 2, 4800 cents")null
- 5Box.Seats.free(2)false
- 6Box.Seats.hold(3, "bo")3
- 7Io.Stdout.println("bo: seat 3, 3200 cents")null
- 8Run.end()null
reading effector call publication
A replay serves the readings from the file instead of asking again. Run
with WHO=bo, it still sells to Ada: the journal answered, not
the environment. Printed lines are performed again and compared.
$ WHO=ada nibc --run box.nib --wire Box --journal-out run.jsonl
ada: seat 2, 4800 cents
bo: seat 3, 3200 cents
8000
$ WHO=bo nibc --run box.nib --wire Box --journal-in run.jsonl
ada: seat 2, 4800 cents
bo: seat 3, 3200 cents
8000
Each entry is chained to the one before it by a digest, and the header names the program, so an edited line or a different program is refused instead of replayed wrongly. There is no journalling syntax in the source.
6A running program is a value
At every crossing, the work done is a list of answers and the work left
is a continuation. --capture cuts the run there and writes both
to a file, and another process --adopts it and finishes.
Below, the first process reads WHO and stops before it
checks seat 2. The second has WHO=zed and still sells to Ada:
the name rode across in the frame.
$ WHO=ada nibc --run box.nib --wire Box --capture cut.frame --capture-after 1
accepted — wrote 5926 bytes at cut.frame, at operation Box.Seats.free
$ WHO=zed nibc --run box.nib --wire Box --adopt cut.frame
ada: seat 2, 4800 cents
bo: seat 3, 3200 cents
adopted — continuing wire 'Box' at operation Box.Seats.free; the prefix was answered from the record and no operation before the cut was performed
8000
The frame carries the run, not the hall. The second process answers
Seats with its own Hall, so state that has to
outlive a process belongs in an effector that keeps it somewhere
lasting.
Today a frame is handed over as a file. Designed for frames sent over a network.
7The run is checked against a model
A journal is a trace of what the program did, so it can be laid against a
TLA+ model and asked whether the model allows it.
BoxTrace.tla rebuilds who holds each seat from the
hold entries, checks every free answer against
that, and refuses a second sale of one seat.
---- MODULE BoxTrace ----
EXTENDS Integers, Sequences, TLC, TraceData
Unmodeled == {"Env.Vars.get", "Io.Stdout.println"}
VARIABLES sold
Init == sold = [s \in {} |-> ""]
MapEvent(e) ==
\/ /\ e.op = "Box.Seats.free"
/\ e.vBool = (e.argv[1] \notin DOMAIN sold)
/\ sold' = sold
\/ /\ e.op = "Box.Seats.hold"
/\ e.argv[1] \notin DOMAIN sold
/\ sold' = sold @@ (e.argv[1] :> e.argv[2])
\/ /\ e.op \in Unmodeled
/\ sold' = sold
Ended == Ending.method = "returned"
====
rush is a careless sale of seat 2 to two people.
nib-tla verify runs both wires: the real one is admitted and the
careless one refused.
func rush :: -> Int ! Seats = let first = @Seats.hold(2, "ada"); @Seats.hold(2, "bo")⋯wire Rush runs Box.rush where Seats = Hall$ nib-tla verify .
nib-tla: scenario box: admitted (7 entries explained)
nib-tla: control rush: refused as designed (refused at seq=2)
nib-tla: verify .: 3.01s — 1 scenario(s) admitted (box), 1 control(s) refused as designed, 0 corpus run(s) still refused, 0 sweep(s) as expected, 4 unknown-effect probes refused
nib-tla: verdict=admitted code=0
$ nibc --run box.nib --wire Rush --journal-out rush.jsonl
2
$ nib-tla check --spec BoxTrace.tla --journal rush.jsonl
nib-tla: refused at seq=2 (op=Box.Seats.hold, kind=operation)
spec: BoxTrace.tla
journal: rush.jsonl (3 entries, chain verified, program 04f7f48a863c7c8752fd0bf73dbf5bad)
admitted prefix: 1/3 entries (seq=1 Box.Seats.hold 2)
frontier state: sold = (2 :> "ada"), nibtla_at = 2
reason: no enabled transition in MapEvent for Trace[2]
nib-tla: checked in 1.42s
nib-tla: verdict=refused code=1
Admitted means the model could not rule the run out, never that the
program is correct. The same check scales up:
Raft in nib runs three nodes in a
simulated world whose delays, crashes and schedule are choices the journal
records, and nib-tla checks every run.
8It ships as a native executable
The wire is also what a native build compiles. nibc build --target
native turns Box into an executable through LLVM, with
the answers the interpreter gives, and the executable says which nib built
it and from which program.
$ nibc build --target native -o box box.nib --wire Box
box.nib: Box.main: compiled natively (no LTO: clang does not say which LLVM it is (Apple's does not))
wrote box: run it with `box ARG'
$ WHO=ada ./box
ada: seat 2, 4800 cents
bo: seat 3, 3200 cents
8000
$ ./box --version
nib 0.3.0 (diagnostics 15f24566a233, build macos-arm64)
program 04f7f48a863c7c8752fd0bf73dbf5bad (wire Box of box.nib)
A web server is the same shape. A handler is a pure function from a request to a response, and the serve loop in the standard library is the only code that touches a socket. The guide builds one.
The whole of box.nib
use "std/env.nib"use "std/io.nib"use "std/iter.nib" data Taken where Taken(Int) interface Seats where free(seat: Int) -> Bool; hold(seat: Int, who: String) -> Int effector Hall is Seats where var owner :: Int => String = {1: "", 2: "", 3: "", 4: ""}; free(seat) = owner[seat] == "" hold(seat, who) = owner[seat] := who; seat module Box where func price :: seat: Int -> Int = if seat <= 2 then 4800 else 3200 func house :: seats: Vec<Int> -> Vec<Int> = Vec.map(seats, { s -> if s <= 2000 then 4800 else 3200 }) func takings :: prices: Vec<Int> -> Int = Vec.fold(prices, 0, { a, p -> a + p }) func season :: -> Int = takings(house(Iter.to_vec(Iter.range(1, 20001)))) func choose :: seat: Int -> Int ! Seats raises Taken = if @Seats.free(seat) then seat else raise Taken(seat) func sell :: seat: Int, who: String -> Int ! Seats, Io.Print raises Taken = let s = @Seats.hold(choose(seat), who); let _ = @Io.Print.println("{who}: seat {s}, {price(s)} cents"); price(s) func main :: -> Int ! Seats, Env.Vars, Io.Print = let who = try @Env.Vars.get("WHO") rescue Err.Missing(_) => "nobody" end; try sell(2, who) + sell(2, "bo") rescue Taken(s) => resume(s + 1) end func rush :: -> Int ! Seats = let first = @Seats.hold(2, "ada"); @Seats.hold(2, "bo") wire Box runs Box.main where Seats = Hall, Env.Vars = Env.Process, Io.Print = Io.Stdout wire Rush runs Box.rush where Seats = Hall wire Season runs Box.season