Captain / Documentation / Part 3. The language

What Captain leaves out, and why

Contents ↓

Captain is a small language on purpose. Each feature below is missing because leaving it out keeps a property that matters: a program can be checked completely before it runs, a reviewer can read it without knowing hidden rules, and its plan says everything it will do. For each absence this chapter shows what happens if you write it anyway, what to write instead, and the reason.

Some of these are settled for good. Others are "not yet": they wait for real programs to show they are needed, and for a design that keeps the checker's properties. The chapter says which.

Floating point

There is no float or double, and no exponent notation. 1.5e3 reads as the number 1.5 with an unknown unit e3:

Refused by the checker: CAP-TYPE-016
module Floats.

export scaled x: Decimal -> Decimal [
  ret x * 1.5e3.
].

Write a Decimal (1500.0), an integer, or a quantity with a unit.

Why: binary floating point cannot represent 0.1, and its errors accumulate silently. Captain is for money, percentages, capacity and limits, where a wrong last digit is a wrong answer, and for calculations someone will audit later. A Decimal keeps every digit, and digits are dropped only where the program says how (Exact decimals). Settled.

Implicit conversion

No value changes type on its own. An i32 is not an i64, an integer is not a decimal, and text is not a path:

Refused by the checker: CAP-TYPE-010
module Widen.

export widen n: i32 -> i64 [
  ret n.
].
Refused by the checker: CAP-TYPE-015
module Mixed.

export add n: i32 to: Decimal -> Decimal [
  ret to + n.
].

There are exactly three ways to convert, each spelled once:

Conversion Spelling Can fail?
an exact numeric conversion n as: i64, n as: Decimal no; inexact directions are refused by the checker
text to a number or tagged value Int parse: t, Path parse: t, ... yes, so it gives a Result
any value to text, for display Text from: x no
Captain
module Converted.

export add n: i32 to: Decimal -> Decimal [
  ret to + (n as: Decimal).
].

Why: an implicit conversion is a rule the reader must know and the text does not show. Most of the bugs it causes (a truncated value, a rounded amount, a string taken as a path) are invisible in review. Settled (Integers, booleans and conversion).

Null

There is no nil or null. A value that may be absent has type Option[T], and you take it apart with ifSome:ifNone::

Refused by the checker: CAP-RESOLVE-001
module Nothing.

export orZero n: i32 -> i32 [
  ret n + nil.
].

Why: when any value might be null, every use is a possible failure the type does not mention. With Option, absence is in the type, and the checker makes the program handle it. Settled (Option and Result).

Exceptions, break and continue

Nothing is thrown. An operation that can fail returns a Result, and an effect that fails ends the run with a diagnostic. A loop ends when its condition is false; a procedure ends early with ret.

Why: an exception is an exit the text does not show at the call site. A Result is visible in the signature, so a reader knows which calls can fail, and the checker knows too. Settled (Blocks and control).

Blocks as values

A block, [ ... ], is code passed to a call. It cannot be stored, returned or bound to a name:

Refused by the checker: CAP-PARSE-BLOCKVALUE
module Stored.

export later x: i32 -> i32 [
  b := [ x ].
  ret x.
].

Why: a block that cannot escape runs only while its call runs. So the checker knows exactly where its effects happen, a frontier block's captured values can be listed in the plan, and there is no closure lifetime to reason about. Settled.

Loops that give values, and comparator sorts

Blocks give values to the forms that run them: a two-branch conditional gives the chosen block's value, and collect:, select:, reject:, detect:, inject:into: and sortedBy: build values from lists (Blocks and control). What is still left out:

  • loops that give values: whileTrue:, forEach: and parallelDo: give nothing, and neither does a one-branch ifTrue:;
  • a sort by a comparator block, sort: [ :a :b | … ]: sortedBy: takes a key instead;
  • count:, anySatisfy:, allSatisfy:, indexed forms and counted loops: write ((xs select: [ … ]) count), or isEmpty on a select: or reject:;
  • effects, ret and outer assignments in the blocks of the collection operations.

Why: a comparator's order depends on the sort algorithm unless the comparator keeps laws the checker cannot prove, while a stable sort by a key depends only on the keys. The collection blocks are pure so that a reader finds effects in statements and calculations in expressions, and so that a plan can go past a calculation over a list it does not know yet. The rest waits for evidence that it is needed (DR-0037).

A match form

There is no general pattern match. Option and Result have their combinators (ifSome:ifNone:, ifOk:ifFail:). Not yet: a general match waits for evidence that it is needed and for a design that keeps every case checked.

Generic procedures

You can declare generic types (struct Box[T], enum Choice[T]), but not generic procedures: every procedure you write has concrete parameter types. A procedure that would need a type parameter takes the type as an argument only in the standard library (List empty: Text, Json decode: t as: List[Item]). Not yet: this is the largest deliberate restriction, kept until real programs show what generic procedures must do (Types as arguments, and generics).

Overloading and operators on your own types

Operators work on the built-in types they are defined for, and you cannot give them new meanings. Procedures are not overloaded by type: two procedures differ only by their selectors. + does not join text (use append:), there is no % operator (% is the percent unit), no bit operators, and no unary minus (write (0 - x)):

Refused by the checker: CAP-PARSE-EXPR
module Negative.

export negate x: i32 -> i32 [
  ret -x.
].

Why: one spelling per operation. When + always means arithmetic on numbers or quantities of one dimension, a reader never has to ask which + this is. Settled.

Your own units and tags

The units (GiB, min, %, currency codes) and tags (#path, #url, #sha256, ...) form a fixed registry. A unit or tag the registry does not know is refused:

Refused by the checker: CAP-TYPE-016
module Widgets.

export stock -> i32 [
  ret 5widgets.
].

Why: a unit carries a dimension the checker reasons about, and a tag carries a validator that runs at compile time. Extending them is a library and provider interface, versioned with the toolchain, not something one program does privately. Not yet open to user code.

Secrets inside data

A Secret may be bound to a local, passed to an effect that takes one, and captured by a frontier block. It may not be put in a struct, a list or a shape, compared, or turned into text:

Refused by the checker: CAP-SECRET-040
module Login.

struct Login { user: Text, password: Secret[Opaque] }.

Why: a secret that can enter ordinary data can leak through a log, an error message or a result. Keeping it in three narrow places lets the checker prove it never does, and lets the runtime keep the secret's bytes outside the virtual machine entirely. Settled (Secrets, receipts and grants).

Open records and dynamic values

Every value has a declared, closed, nominal type. There is no map from strings to anything, no record with fields added at run time, no value that skips the checker. Data whose fields come from elsewhere, such as an API's JSON, gets a declared Captain struct, and decoding checks it (JSON).

Why: a value the checker cannot see is a value a reviewer cannot trust. Settled.

A clock, and other ambient inputs

A pure Captain program has no clock, no random numbers and no environment. "Now" is always an argument, and anything else from the world arrives through a declared effect, which is recorded in the run log.

Why: the same inputs must always give the same result, and a replay must reproduce a run exactly (Determinism, run records and replay). Settled.

Dialects and domain effects

Captain has no per-product dialect, and no clause schema invented for one consumer. A pure entry cannot reach a database or any other domain system: the host performs effects, and the pure entry calculates. A product that needs something Captain does not offer builds it on its own side, in the host.

Why: one language, checked one way, is something every reviewer can learn once. A change becomes part of Captain only when Captain's own direction or at least two of its own tools need it, it keeps the checker's properties, and it is worth keeping for ever, because released formats and VM generations are never dropped.

Shadowing, and zero-argument calls

Two smaller absences that make programs easier to read:

  • A local may not reuse a name already visible in its scope. One name means one thing throughout a procedure.
  • Every call has at least one keyword argument. A member is read with parentheses, (server publicAddress), so x y never has to be guessed as either a call or a field (Procedures and keyword selectors).