A Captain app is a whole application written in Captain, delivered as checked bytecode and drawn by a native UI host. It has no HTML, no CSS and no JavaScript. It can be served from a server and opened, or kept as a file and run later.
An app is pure. It never performs I/O. It receives values (the launch, each message from the view, each result of something it asked for) and returns values: its next state, a view tree to draw, and commands, requests the host carries out only if the app was granted them:
host app (pure Captain)
---- ------------------
launch ───────────────────────────────▶ start launch: ─┐
a press, a keystroke ─────────────────▶ update state:message: ├─▶ Update { state, commands }
a command's result ───────────────────▶ receive state:reply: ─┘
draws ◀─────────────────────────────── view state: ─▶ View treeThe profile is captain.app/1. Its specification is a starter draft; the
library, the runtime crate and a native window host are built.
Why commands are values
A command could have been an effect, which would let the checker say more
about each call. It is a value instead, by decision, for two reasons. If
update performed a request, it would wait on the network. And because
every input to an app is a value the host hands in, including the time, a
session is its launch plus its messages and replies, and replays exactly,
state for state and view for view. Every procedure stays pure, the checker
proves it, and the list of permissions the app declares bounds what the
host will do.
The library
An app uses the library module App::Ui, in
lib/app/ui.capt. It defines the shapes that cross
the boundary (View, Attr, Message, Command, Reply, Launch) and
builders for them. A module cannot build another module's struct with a
literal, so an app makes views and commands through the builders, called
qualified: Ui::column children:, Ui::button text:on:,
Ui::get path:id:.
A program
The smallest app keeps a number. It is
crates/captain-app/apps/counter.capt:
module Counter.
use App::Ui.
export struct State { count: i32 }.
export struct Update { state: State, commands: List[Ui::Command] }.
export wants -> List[Symbol] [ ret List empty: Symbol. ].
export start launch: Ui::Launch -> Update [
ret Update { state: State { count: 0 }, commands: #() }.
].
export update state: State message: Ui::Message -> Update [
next := (state count).
((message name) = #increment) ifTrue: [ next := next + 1 ].
((message name) = #decrement) ifTrue: [ next := next - 1 ].
((message name) = #reset) ifTrue: [ next := 0 ].
ret Update { state: State { count: next }, commands: #() }.
].
export receive state: State reply: Ui::Reply -> Update [
ret Update { state: state, commands: #() }.
].
export view state: State -> Ui::View [
ret Ui::column children: #(
(Ui::heading text: "Counter"),
(Ui::text text: (Text from: (state count))),
(Ui::row children: #(
(Ui::button text: "−" on: #decrement),
(Ui::button text: "Reset" on: #reset),
(Ui::button text: "+" on: #increment)
))
).
].An app exports five procedures, all pure:
| Procedure | Called | Returns |
|---|---|---|
wants |
once, at admission | the permissions the app may use: #home, #store, #clipboard, #openFile, #saveFile |
start launch: |
once, when the app opens | the first state and commands |
update state:message: |
for each message from the view | the next state and commands |
receive state:reply: |
for each result of a command | the next state and commands |
view state: |
after each change, before drawing | the view tree |
State is the app's own type. Update must be a struct with exactly the
fields state and commands, in that order. Captain cannot call a
procedure without arguments, so an update with no commands writes
commands: #(), and the counter's wants returns an empty list: it asks
for nothing.
An app is built together with the library, and every export comes out pure:
$ captain build counter.capt -I lib -o counter.capb
built counter.capb ([..])
$ captain inspect counter.capb
...
exports
receive state: Counter::State reply: App::Ui::Reply -> Counter::Update (pure)
start launch: App::Ui::Launch -> Counter::Update (pure)
update state: Counter::State message: App::Ui::Message -> Counter::Update (pure)
view state: Counter::State -> App::Ui::View (pure)
wants -> List[Symbol] (pure)
...Admission
A host admits an app before calling start, and the runtime crate,
captain-app, does it the same way for every host:
- the artifact verifies, and needs a VM generation the host runs;
- the manifest lists no effect, frontier, tool or secret: none at all;
- the five procedures are there with exactly their signatures, each with an empty effect set;
- the
App::Uitypes have exactly their published fields; Updatehas exactly its two fields;wantsreturns within limits and lists only known permissions;- the artifact is within its size limit.
captain-app check applies the same rules:
$ captain-app check counter.capb
counter.capb: admitted as captain.app/1, wants nothingThen the host shows what wants lists, before start runs. Opening an app
is granting what it lists, or less. A command needing a permission that was
not granted is answered #refused, and the app runs on. A #get or #post
can reach only the app's home, one https:// address and path prefix;
anything that would leave it is refused, never sent. Each call runs in a
fresh VM, with an instruction budget and a time limit, and a call that fails
leaves the last good state and view in place.