A program, from one function to a server
The shape of every nib program — modules, interfaces, effectors and the wire that joins them — built up one step at a time.
A nib program has two kinds of code, kept apart on purpose. Modules hold functions: they compute, and when they need the world — a clock, a file, a socket — they say so, and ask. Effectors are the only code that answers: they hold state and talk to the outside. A wire joins the two, naming which effector answers each request. Everything below is one of those three things.
A function and a wire
The smallest program is a module with one function, and a wire that names it as the entry:
module Shop where
func total :: -> Int =
let prices = #[3, 4, 5];
Vec.fold(prices, 0, { acc, p -> acc + p })
wire Main runs Shop.total
12
nibc --run FILE evaluates the wire's entry and prints its answer. A file can hold several wires; --wire NAME picks one. The types were inferred: total could be written func total :: -> Int, and here it is, because a signature is where a reader looks first.
Asking for the world
A function that needs something from outside does not reach for it. It performs an operation of an interface, written @Interface.op(args), and its signature says so after ! — its effect row. The checker refuses a function that performs something its row does not admit, so the row is the complete list of what a function can do besides compute.
use "std/io.nib"
module Shop where
func receipt :: -> () ! Io.Print =
let _ = @Io.Print.println("3 items");
@Io.Print.println("total: 12")
wire Main runs Shop.receipt where
Io.Print = Io.Stdout
3 items
total: 12
Io.Print is an interface; Io.Stdout is an instance the compiler ships that answers it. The wire is where the two meet, and it is the only place: nothing inside Shop can name Io.Stdout. Bind Io.Print to something else and the same function prints somewhere else, which is how a test captures output.
Your own effector
An effector declares the interface it answers and a body for each operation. It may keep state in var cells — the only mutable storage in the language, and only an effector has them.
use "std/io.nib"
interface Till where
ring(cents: Int) -> ();
total() -> Int
effector Register is Till where
var sum :: () => Int = 0;
ring(cents) = sum[()] := sum[()] + cents; ()
total() = sum[()]
module Shop where
func sale :: -> Int ! Till =
let _ = @Till.ring(300);
let _ = @Till.ring(450);
@Till.total()
wire Main runs Shop.sale where
Till = Register
750
A cell is keyed (sum[()] reads the slot at key ()), and a write is a statement: sum[()] := e; rest. Shop still only asks; whether a till is in memory, in a database or in another process (see Effectors in another process) is the wire's business.
When things go wrong
A function declares what it may raise after raises, and a caller either declares it too or catches it with try … rescue. Builtins raise named tags — Err.MissingField, Err.InvalidJson — that carry a message.
module Shop where
func price :: doc: Json -> Int raises Err.MissingField, Err.NotAnObject =
String.length(Json.stringify(Json.field(doc, "price")))
func safe :: -> String raises Err.InvalidJson =
let doc = Json.parse("\x7b\"name\": \"tea\"\x7d");
try "price has {price(doc)} digits"
rescue Err.MissingField(_) => "no price"
rescue Err.NotAnObject(msg) => msg
end
wire Main runs Shop.safe
no price
The checker holds every raises row to what the body can actually raise, both ways: an omitted tag is an error, and so is a promise the body cannot keep.
Serving
A server is the same shape. std/server.nib's Server.run is an accept loop written in nib: it opens a socket through the Net.Socket interface, runs a fibre per connection, parses each request and hands it to your function, which answers an Http.Response. The page below is a whole server; it is only checked here, because running it would listen on a port.
use "std/http.nib"
use "std/net.nib"
use "std/io.nib"
use "std/time.nib"
use "std/server.nib"
module Site where
func handle :: r: Http.Request -> Http.Response =
if r.target == "/" then Http.text(200, "hello from nib\n")
else Http.text(404, "nothing at {r.target}\n")
func main :: -> () ! Net.Socket, Io.Print, Server.Seats, Time.Clock raises BadHeader, BadStatus, Err.BindFailed, Err.IndexOutOfRange, HandleRefused =
Server.run("127.0.0.1", 8080, { r -> handle(r) })
wire Serve runs Site.main where
Net.Socket = Net.Native,
Io.Print = Io.Stdout,
Time.Clock = Time.System,
Server.Seats = Server.Count
nibc --run site.nib --wire Serve serves it with the walker. The same file compiles to a native executable:
nibc build --target native --wire Serve -o site site.nib
./site
which serves the same bytes at native speed — Compiling to native code says how. A real site built this way is a small notes app: a page and a JSON API over a notes file, with its responder written as an effector and its routes as pure functions.
Programs in more than one file
A larger program is split into packages: package Name requires A, B at the top of a file declares what the package may perform, and use "path.nib" loads another file. A wire then binds per package — App.Io.File = Io.Disk is Io.File as the App package reaches it — so two packages that need the same interface can be given different instances. The language reference in the compiler tree (docs/language/, and docs/seam.md for the rules) covers packages in full.