Language reference
This page holds the detailed rules of the language. Each section adds to the section with the same name in the guide.
Records and defaults
Section titled “Records and defaults”A default is checked where its type is defined. It must have the field’s type, and a literal default must satisfy the field’s refinement (n: Positive = 0 is an error). A default sees no variables, not even the other fields, and it is evaluated each time it is used. Generated values (mocks, fuzzing) still generate every field.
Key paths. A field written after its record type’s name, Todo.title, is a value of type KeyPath[Todo, String]. core defines type KeyPath[R, T] = { name: String, get: (R) -> T }: the field’s name and a function that reads it, so (Todo.title.get)(t) is t.title.
- The key path’s types come from the field, so code that takes one is checked against the record. In
std.orm’s queries,Todo.done.eq(false)needs aBooland gives a condition onTodos only. - A field the record doesn’t have is an error (
typeTodohas no fieldx``). - The checker expands it (
desugar.key_paths) intoKeyPath { name: "title", get: ($r: Todo) -> $r.title }.viewstill showsTodo.title, and nothing after the checker sees the short form. - Not supported: fields of a generic record (
Pair.first), paths through several fields (Todo.owner.name), and assigning through a key path.
Spreading a record into a call. The checker expands f(a, ...x, ...y, b) into the call written out by parameter name. Positional arguments before the spreads fill the first parameters, and those after the spreads fill the last ones. Each parameter in between takes the field with its name from the one spread record that has it (x.qty). Other fields are ignored. The call is stored written out (edit writes it, with the records’ and callees’ declarations), so everything after parsing sees an ordinary call. view shows f(...x) again when every argument is a field of the same variable (or field of one) named like its parameter, and a spread of a value that isn’t a variable (...make(), bound first) always; other calls with spreads are shown written out (f(5, x.qty, x.price)).
Spreads work in:
- calls to a function by name
- method calls on a value (
r.m(...x),mut r.m(...x)). For a trait value or a type parameter, the method is resolved through its trait. - method calls on a type (
Shop.new(...x)), whereselfcounts among the parameters
Each of these is an error that names the call:
- a parameter that no record has a field for (parameters have no defaults)
- a parameter that two records both have (
is given by both ...x and ...y). Spread only one of them, or pass that parameter positionally. - a
mutparameter left to a spread, since a spread only reads. Passmut vpositionally. - a spread of a value that isn’t a record (
...5) - a spread record that provides no parameter
- a positional argument between two spreads
- more positional arguments than parameters
- a spread into a function stored in a variable (its parameters have no names) or into a builtin
- a spread anywhere other than such a call, such as a contract or a constructor.
Pt(...p)is a parse error, andPt { ..p, x: 1 }is the record update syntax.
The spread value is read once. If it isn’t a variable or a field of one (...make()), it is bound before the call’s other arguments are evaluated. The same holds for a receiver that isn’t a variable or field.
Unions, variants and patterns
Section titled “Unions, variants and patterns”A union’s type parameters must be wrapped. They can appear inside a member the union declares (type Option[T] = Some(T) | None, type OrError[T] = Value(T) | Problem) or inside a type (type Many[T] = List[T] | Problem). A type parameter can’t be a member by itself (T | Problem), and two members can’t differ only in their type arguments (List[A] | List[B]), because their values couldn’t be told apart. A union of two types is written A | B where it is used; there is no Either[A, B]. A generic union is therefore identified by its members: List[Int] | Problem is Many[Int], and impl[T] Show for Many[T] applies to it.
A named union names a set of values. A member fits the union without wrapping. The same set written out (x: Circle | Square | Dot) is the same type. strata view shows a union as it is written, never as another union’s name.
- A named union can have
impls, methods andderives. A value typed by the union, or by its set, uses the union’s own; a value of a member uses the member’s. - A union that adds nothing is rejected, like a bare alias. That covers a union that normalizes to a single type (
type U = Int | Positive). distinct type Answer = Some(Int) | Noneis a separate type, built asAnswer(Some(3))and read with.value.
What a type definition must add (R1). A definition that is not distinct must be a type of its own:
- R1a. A union whose members, once flattened (a member that is itself a union counts as its members), all come from outside its module is another name for them, and an error. Its own members are the atoms, records and wrappers declared in the module, and generics applied to one of them (
List[Item],Option[Order]).type MyType = Int | Baris allowed whenBaris declared in the module. - R1b. Two unions whose flattened members are the same set are an error. One that holds a subset of another’s is allowed.
- R1c. A definition whose whole body is another type (
type K = List[Item],type M = Dict[String, Int],type R = Result[T, E]) hides what it is, and is an error. - R1d. A union doesn’t list another module’s atom by itself (
None | Baris an error, sinceNoneis core’s). It may list that module’s whole union or generic sum (Int | Option[Int] | Bar,std.io.IoError | ParseFailed).
Each of these is allowed as distinct type, which makes a separate type.
Constructors the types decide. The stored program holds every constructor. strata view leaves out the ones the rules below would put back, wherever they were written, and edit puts back every one the text leaves out before it stores a definition. Both decide from the type each place expects and the value’s type there.
- R2. At a place whose expected type is a generic sum, a value that isn’t one of the sum’s values is wrapped in the member whose single field is a type parameter, when exactly one such member accepts it. Otherwise the constructor is written.
f(3)for anOption[Int]parameter isf(Some(3)). Infn div(a: Int, b: Int) -> Result[Int, DivideByZero], the valueDivideByZero { a }isErr(DivideByZero { a })anda / bisOk(a / b). AResult[String, String]needsOkorErrwritten, because both accept aString. - R3. At a place whose expected type is a
distincttypeD, a value of one ofD’s members, or of its underlying type, becomes aD. A value of anotherdistincttype is never converted. - R2 and R3 apply at most once each at a place, R3 inside R2:
3where anOption[KeyValue]is expected isSome(KeyValue(3)).
The places are a call’s or a constructor’s arguments, a record’s fields, a list’s or a tuple’s elements, a function’s or a lambda’s result and a return’s value, the value of a let or mut with a written type, an assignment to a variable or parameter declared with a type (k = 1 after mut k: Key = ...), and an argument of a generic built-in whose type the arguments before it settle (contains(keys, 3) with keys: List[Key]). An if’s or a match’s branches, and a block’s last value, are places when the whole is one. Contracts are not places. Constructors at places inside a value whose own are left out are left out too, wherever edit puts them all back. A binding without a written type (x := Some(5)) is not a place: leaving its constructor out would change x’s type. A match whose arms would read back as !, ? or except without their constructors keeps them.
distinct type KeyValue = Int | Stringtype DivideByZero = { a: Int }
fn div(a: Int, b: Int) -> Result[Int, DivideByZero] { if b == 0 { DivideByZero { a } // stored as Err(DivideByZero { a }) } else { a / b // stored as Ok(a / b) }}
let keys: List[KeyValue] = [3, "x"] // stored as [KeyValue(3), KeyValue("x")]Unused variants (R4). A variant of a private sum that is never used is a warning in strata check and in a build, and an error in strata build --release.
strata view shows a type inline in the one union that lists it, unless the type has its own doc, impl or derive, in which case it is shown separately. --members separate shows every member separately. Both forms read back as the same program.
Exhaustiveness and reachability. The checker splits the matched type into variants, union members, and the ranges that the arms’ bounds cut out of an Int, a Float, or the length of a String or List. It reports values that no arm matches ( `where 3 <= it && it <= 5` is not covered) and arms that earlier arms already cover (unreachable match arm Positive``).
Conditions that compare it or len(it) with constants, combined with &&, ||, ! and ==>, are decided exactly. Any other condition (matches(it, …), a call, another variable) is left to strata verify. A match that only such arms might complete is not a checker error. The prover proves that some arm matches, or shows a value that none does (no match arm for 1), and the compiled code checks it at run time (a no match arm fault).
The matched value’s refinement is taken into account. With fn f(n: Positive), match n { 1 => …, where it > 1 => … } is exhaustive, and a where it <= 0 arm is unreachable (no Positive matches it). With xs: NonEmpty[Int], match xs { [x, ..] => … } is exhaustive. This applies to variables (including mutable ones, since a declared type holds after every assignment), their fields, a function’s result type, and the value inside a narrowing arm. Narrowing follows the code: an arm that assigns the matched mut variable anywhere does not narrow it, so the variable keeps its declared type in that arm. This also covers the next pass of a loop.
A match on a call of one of the program’s functions or methods is covered against the variants the call can give there: all of its result’s sum, or those its function’s result refinement and ensures leave given the arguments’ types (ensures o is Paid ==> it is Shipped, with o narrowed to Paid: only Shipped). The checker decides p is V conditions on the arguments, with !, && and ||; facts with any other condition ((it is Some) == d.contains(k)) leave the variants they would rule out to strata verify, which proves that some arm matches (some arm of the match matches), or the compiled match faults with no match arm. A match on a built-in’s call (get(d, k), xs.first()) or on a method of a type whose code the checker doesn’t see isn’t covered by the checker.
A field that every member of a union has can be read without a match. A field that only some variants of a sum have is read where the value is narrowed to those (a match arm, if x is V, a call narrowed as above); elsewhere it is an error ( `Pending` has no field `tracking`, so not every `Order` does). A trait method can be called on a union when every member implements the trait. Alternatives of different types (if branches, match arms, list elements) get their union type when no expected type says otherwise: if big { Square {…} } else { Circle {…} } is a Square | Circle.
Type arguments are checked member-wise. A List[Circle] can be passed where a List[Shape] is expected, because values are never shared and the callee works on its own copy. A function type fits when it accepts more and returns less. A mut parameter is the exception, because it writes back into the caller’s variable: its argument must have exactly the parameter’s type, since grow(mut shapes: List[Shape]) could put a Square into a List[Circle]. For the same reason, ! can pass an error into a wider error type (a TooBig into a Result[_, ShapeError]).
Traits
Section titled “Traits”Mocks.
verifyreports a mock’s methods as assumed, since the tests that use the mock check them.strata viewhides mocks and their implementations unless asked (view --mocks).- A mock holds data in a record (
mock FixedClock = {}for none).
Capabilities. std defines these capabilities, which main asks for by name:
Disk(std.fs), implementingFilesProcess(std.process), implementingEnvironmentLauncher(std.process), implementingCommands, for running other programs
Commands has one method, run(mut self, program: String, args: List[String], dir: String) -> Result[Finished, String], where Finished = { code: Int, out: String, err: String }. It starts program (looked up in PATH) with args, never through a shell, in the folder dir ("" for the current one), with an empty standard input. It waits for the program and returns its exit code (128 plus the signal number if a signal killed it) and its output. It returns Err only when the program can’t be started (no such program or folder). Its ensures says the code is never negative, and that is all verify assumes about a call. Code that takes Commands is tested with mock Commands, or with a mock type that answers the calls the test expects.
Console is the one capability the environment chooses, so main asks for the trait. Under strata run it is the terminal. A test can ask for it too (test "..." (mut console: Console) { ... }). What the test writes is its own output, shown only if it fails. Under strata run --trace, the console’s calls appear in the trace with what was written and read.
The run mode is hidden by default, so a program behaves the same everywhere. A package that needs it opts in through strata.toml:
[run]mode = truemain and tests can then ask for a RunMode (std.process): Running under strata run or in a built executable, Testing in a test, and Tracing under --trace. Without the opt-in, asking for it is an error that explains how to allow it.
Shared method names. Two traits can declare methods with the same name, and each implementation keeps its own body. A call is resolved from what is known about the receiver:
- Known only through a trait (
d: Describe,T: Describe): that trait’s implementation. - A known type: the type’s inherent method if it has one (
impl Item { fn describe(self) … }), otherwise the method of the one trait of that type that declares the name. This holds even when a function in another module has the same name. Inside your ownfn handle,app.handle(req)callsRouter’s method, whilehandle(app, req)calls the function. - Anything else is a compile error. Examples: a type that implements two traits declaring
describe, with no inherentdescribe, or a value known only asDescribe | LogorT: Describe + Log. In these last two cases an inherentdescribeon a type doesn’t help, because the call doesn’t say which trait’s meaning it wants.
This holds whichever modules the traits come from. With Console (std.io) and Files (std.fs) both imported, mut console.write(s) and mut files.write(path, s) each call their own trait’s write. A method named like a builtin (Files.remove) doesn’t hide the builtin: on a value whose type has no trait declaring the name (a dict, list, string, …), d.remove(k) and remove(d, k) are the builtin.
strata resolves each call during checking and records the result in the program it runs, so the same call always runs the same implementation. A name whose meaning depends on the receiver can’t be passed as a bare function value (map(describe)). Pass a lambda instead (x -> x.describe()).
Self versus the trait as a parameter type. fn bigger(self, other: Self) takes a value of the receiver’s own type, and fn bigger(self, other: Shape) takes any implementation. On a trait value (a Shape of any implementation), a method taking Self can only be given the value itself or a value made by its Self-returning methods (s.bigger(s.scaled(2))). xs[0].bigger(xs[1]) is an error, since xs[1] may be a different implementation.
Models: abstract state of a trait. A trait can declare abstract state with model name: Type, one per line before its methods. Each implementation defines the model from its own state with model name = e, an expression over self.
- Contracts refer to a model as a field of a value of the trait:
self.orders, ordb.ordersfordb: Dbordb: DwithD: Db.oldworks on models too. Code never reads a model directly; it uses the trait’s methods. invariant ein a trait, overself, refines every implementing type’s model asSelf where e. Every value of the trait satisfies it. Each method knows it holds forselfand for anySelfparameter, amut selfmethod must preserve it, and a method returningSelfmust establish it. A function takingdb: Dbknows it too.ensures ein a trait, which may useold, must hold after everymut selfmethod.- There is no trait-level
requires.
trait Db { model orders: Dict[Int, Order] invariant !self.orders.contains(0)
fn save(mut self, id: Int, o: Order) ensures self.orders == old self.orders.with(id, o) fn lookup(self, id: Int) -> Option[Order] ensures it == self.orders.get(id)}
impl Db for MemDb { model orders = self.all
fn save(mut self, id: Int, o: Order) { mut self.all.insert(id, o) } ...}d.with(k, v) and d.without(k) on a dict, and s.with(x) and s.without(x) on a set, build new values for use in contracts. Three more state common contracts directly:
d.excluding(keys):dwithout the entries ofkeys(a list or a set). “Nothing but these entries changed” isself.orders.excluding(ids) == (old self.orders).excluding(ids).d.key_set(): the keys ofdas a set (d.key_set().contains(k) == d.contains(k)).xs.unique(): no element twice (len(set(xs)) == len(xs)).
verify reads them as the solver’s own operations: excluding is the dict’s array with those keys masked, so a frame needs no quantifier over the entries; key_set is the array of the keys whose entry is present; set(xs) of a list holds exactly its elements, and unique says position to element is one to one.
verify proves each implementation’s methods against the trait’s contracts, reading the model through the model definition. Code that takes the trait is proven through the model alone (verification). A contract that names a model is checked like any other contract: left out of the compiled code where verify proves it, otherwise fuzzed and checked at run time, computing the model on each call. Written assumed, it is trusted and never checked.
An impl must define every model of its trait and no others. A mock type’s impl may omit them.
mock Db for a trait with models simulates the models. It stores each one, starting empty ({}, [], 0, "", false, None). A method whose ensures states the new value of a model (self.orders == e) and its result (it == e) does exactly that. Any other method returns a generated result that satisfies its contract, as with every mock. mock Db is therefore an in-memory reference implementation for tests.
Laws made of statements. A law is a Bool that every implementation must satisfy (law associative(a: Self, b: Self, c: Self): …). It can also be a block of statements, including mut method calls on copies of its parameters, that ends in the Bool that must hold:
law enqueue_once(q: Self, id: Int) { mut r := q mut r.enqueue(id) (mut r.enqueue(id)) == 0}verify proves the law for each implementation, through its code or through its methods’ contracts, assuming its Self parameters meet the trait’s invariants. A counterexample gives the values and the steps: law claim_after_enqueuedoes not hold forMemQueue with q = MemQueue { waiting: Set[Int] {}, taken: Set[Int] { 21 } }, id = 21: after mut r := q; mut r.enqueue(id); mut r.claim(id).
A law describes what one worker sees when it makes those calls in a row with nobody else in between. Callers of the trait can’t use a proven law as a fact. They reason only through the methods’ contracts and the invariants.
shared traits: many workers, one value. shared trait Queue { … } declares a trait whose value many workers use at once. It models a backend whose state is outside the program, such as a database or a remote queue, with each worker holding a handle to it.
- Each method is one atomic step on the model. Every
mut selfmethod preserves the trait’s invariants from any state where they hold, so the invariants hold however the workers’ calls interleave. - Callers can’t keep facts about the models from one call to the next, since another worker may act in between. Before each call on a value of a shared trait (
q: Queue,Q: Queue),verifytreats the model as any value the invariants allow. The call’srequiresmust therefore follow from its arguments and the invariants alone. Itsensuresrelates the state just before and after that call, and what it says about the model holds until the next call on the value. The call’s result keeps its facts:a := mut q.enqueue(id)is 0 or 1 whatever happened before. - An implementation’s method must be one step. On every path it changes its outside state with at most one call. Outside state means fields whose type is a capability, a
capability traitor another shared trait. A read followed by a write (self.db.select(id), thenmut self.db.update(id, 1)), or two writes, is an error: “another worker’s call can run betweenselectandupdate”. A method takingmut selfcounts as a write, one takingselfas a read, and every capability function as a write, since it reaches the outside world. Several reads are allowed, and so is a read after the write. A function passed into the one call (mut self.db.transaction(id, s -> …)) runs inside that step, so a transaction-style method is how an implementation reads and writes together.
Trait methods without self are called on a type, never on a value: Money.empty(), T.empty() in a generic function, Self.empty() in a trait’s default methods and laws. They are constructors and similar functions (fn empty() -> Self, fn parse(s: String) -> Result[Self, ParseError]). Their signature must mention Self; one that doesn’t couldn’t select an implementation and should be a plain function.
In a generic impl’s methods, they can be called on the impl’s type parameters too: impl[T: Zero] Zero for List[T] { fn zero() -> Self { [T.zero()] } }, or fn padded(self, n: Int) -> Self { .. push(out, T.zero()) .. } in impl[T: Zero] Pad for List[T].
Types are passed whole at run time. The type a method is called on (List[Int]), or the static type of the receiver, determines T. Only a value held as a trait type (d: Describe) doesn’t carry its full type.
A refined alias stays itself as a type argument. In read[List[Port]], T is Port, whose methods are Int’s. The same holds for an alias with type parameters (type Many[T] = List[T] where len(it) >= 2, where Many[Int] has List’s methods) and for a refinement written as a type argument (List[Int where it % 2 == 0], shown as written in messages).
T.check(x) works on any type, including a type parameter (checked against its run-time type). It returns Ok(()) when x satisfies the type’s refinements, and otherwise an Err describing what fails: Port.check(0) is Err("0 does not satisfy Port (where 1 <= it && it <= 65535)").
Because the type is always named, it is always clear which implementation runs. A generic function that calls such a method receives its type arguments at run time: each call passes the concrete type the checker inferred, or forwards the caller’s own type parameter. The type must therefore be known at each call. combine_any([]) alone is an error (annotate it: let xs: List[Money] = []), and such a function can’t be passed as a value (map(combine_any)); use a lambda. A trait-typed parameter (xs: List[Empty]) has no type name to call .empty() on, so write the type parameter when you need one.
Writing a derive. derive A, B for, written between a declaration’s /// docs and the declaration itself (pub, then fn or type), derives each trait for it:
/// A greeting for `name`.derive C, Python forpub fn greet(name: String) -> String { f"Hello, {name}!"}
derive CommandLine, Json fortype Options = { count: Int, loud: Bool, name: String = "world" }- This is only syntax. Each trait’s derive is stored as its own definition (
derive CommandLine for Options,derive Json for Options), exactly as the separate formderive Json for Optionsstores it.derive A, B for T, whereTis a type, a function ormodule, also gives one definition per trait. strata view, the textstrata editopens, andstrata docgenshow a derive on its declaration when it has no hand-written block of methods, no type parameters or trait arguments, and no comments of its own, and the declaration is in the same module. All of a declaration’s such derives go on one line, in their order in the module.- Other derives stay on lines of their own: one with a block (
derive Json for Point { ... }), one for a type in another module, andderive C for module. A derive with a block names a single trait. - An edit replaces and adds derives by name, like any definition. A declaration given in an edit brings its derive line. A stored derive that would be shown on that line but whose trait the line no longer names, or whose line is missing, is removed with a warning (
warning: removed derive std.toml.Toml for Options (no longer on its derive line)). - A declaration the edit doesn’t give keeps its derives. Derives with a block, derives for another module’s declaration, and
for modulederives are removed only withstrata edit --rm 'derive std.json.Json for Options'. strata view Options, and the editorstrata edit Optionsopens, show the declaration with its derive line, so editing what they show keeps the derives.
Operators
Section titled “Operators”An operator is a call of a trait’s method, from core.ops. A type gets an operator by implementing its trait, like any trait:
| Written | Is | Trait’s method |
|---|---|---|
a + b, a - b, a * b, a / b, a % b |
Add.add(a, b), Sub.sub, Mul.mul, Div.div, Rem.rem |
fn add(self, other: Self) -> Self |
-a |
Neg.neg(a) |
fn neg(self) -> Self |
a == b, a != b |
Eq.eq(a, b), !Eq.eq(a, b) |
fn eq(self, other: Self) -> Bool |
a < b, a <= b, a > b, a >= b |
Ord.lt(a, b), Ord.le, Ord.gt, Ord.ge |
fn lt(self, other: Self) -> Bool; le, gt and ge follow from it unless the impl gives them |
type Vec2 = { x: Int, y: Int }
impl Add for Vec2 { fn add(self, other: Vec2) -> Vec2 = Vec2 { x: self.x + other.x, y: self.y + other.y }}
fn total[T: Add](xs: List[T], zero: T) -> T = xs.fold(zero, (a, b) -> a + b)
print((Vec2 { x: 1, y: 2 } + Vec2 { x: 3, y: 4 }).x) // 4- The numbers have every operator built in,
Stringhas+and the orders (by code points),Listhas+. Core’simpls for them call intrinsics, one per operation and family:impl[T: SizedInt] Add for T { fn add(self, other: T) -> T = sized_add(self, other) }forI8…U64(SizedInt, a built-in trait),float_*(SizedFloat),integer_*forInt,string_*,bool_eq,list_add. An intrinsic compiles to its instruction inline for the concrete type (withInt’s overflow check), andverifyreads it as integer or real arithmetic; so does an operator on a built-in type, andx.add(y)from generic code on one (total([1, 2], 0)). - On another type,
a + bis the calla.add(b)of the type’simpl. Without one it is an error:`<` on `P`: it needs `impl Ord for P`.verifyproves such a call through theimpl’s contracts (itsensures). - Operands of two types: an
implnames the right operand’s type and the result’s:impl Mul[Int, Money] for Money { fn mul(self, k: Int) -> Money { … } }givesm * 3,impl Mul[Vec2, Vec2] for Intgives3 * v. The checker types both operands, then picks theimplthat joins their types; one per pair of types and operator (+ - * / %only). - Every type has
==built in, comparing values part by part.impl Eq for Treplaces it where==compares twoTs, or two values of a type parameter bound byEq; inside a list, a dictionary,containsorsort, values still compare part by part. &&,||,==>and!are not methods: they evaluate their right side only when needed. Nor, yet, are the bit operators (& | ^ << >>).- The stored tree holds the call (
Add.add(a, b));strata viewshowsa + b, and!Eq.eq(a, b)(writtena != bor!(a == b)) asa != b. - Cost: on a built-in type, the operator itself. On another, one method call. Checking a program with such
impls infers the types of the operands of each function using an operator once more.
Modules
Section titled “Modules”- Each
.stratafile in a folder (a binary module file) is a module named after its path:shop/orders.strataisshop.orders, andmain.stratais the root module. view MODULEprints one module as text, andview --split DIRwrites the codebase as one.stfile per module. A view that spans several modules (view a,b) labels each group with a// ── module mcomment.- In the database, definitions are stored under qualified names, so
orders.Lineandbilling.Linecan coexist.edit mtargets a module, andview module:mshows one. - Visibility. A definition marked
pubcan be used by other modules. Any other is private to its module and its submodules.- A
pubsum makes its memberspub. The atoms, records and wrappers it declares can be built and matched wherever the sum is visible:shapes.Circle { r: 2 }, orCircleafterfrom shapes use Circle. - A
pubdefinition may name onlypubtypes. That holds for apubfunction’s signature, apubtrait’s or capability’s methods, the methods of animplof apubtype, and apubtype’s fields and members.checkreports any other: “Secretis used by pubopen; makeSecretpub”.
- A
- core’s names. Every module sees core’s names as if it began with a
useof each core module’s names (from core.task use Task, Panic,from core.channel use Sender, Receiver, channel, …):Task[Int],Sender[T],channel(4)need no prefix and no import. Only core is seen so: std’s names are written by path or imported. - Shadowing a core name. A name a module defines, or imports explicitly, and that a core module also defines, means the module’s own there. Its definitions in the root module count in every module, since every module sees them. For that name the module sees core only as
use corewould give it: core’s definition is written by its path,core.task.Task[Int]ortask.Task[Int],channel.Sender[T], and always so in that module (a bareTaskthere is the module’s own, so writing it for a task’s handle is a type error). The core names the module doesn’t shadow stay bare.viewwrites them the same way, so what it prints reads back as the same code, and errors name a shadowed core type with its prefix in that module. Shadowing is allowed but warned: aneditthat adds such a definition, andcheck, say “warning: inTask:Taskshadows core’score.task.Task; core’s is now written by its path”./// A unit of work in a queue: not a task's handle.type Task[T] = { item: T, tries: Natural }fn wait(h: task.Task[Int]) -> Int {await h}fn run(t: Task[Int]) -> Int {scope {h := spawn t.item + 1wait(h)}} - core.types’
Option,Result, their variants and its refinements (Natural,Positive,NonEmpty, …) are shadowed the same way: a module with its ownOptionwrites core’stypes.Option[Int], whileSome(x),x?andget(xs, i)stay core’s. The built-in types (Int,Float,Bool,String,List,Dict,Set,Tuple,I8…U64) can’t be defined in the root module, since literals make them; another module’s ownListis its own, and core’s istypes.Listthere. - The compiler knows core’s types and traits by their full paths, whatever a module calls them: the handles (
core.task.Task[T],core.channel.Sender[T],core.channel.Receiver[T]),core.task.Panic,core.real.Real, the iterators (core.iter.Iterator,FiniteIterator,ListIter, the adapters), the operator traits (core.ops.Add, …Eq,Ord,SizedInt),core.types.DebugandDisplay,core.types.OptionandResultwith their variants (core.types.Some, …), the refinements (core.types.Natural,NonEmpty, …),core.keys.KeyPath,core.values.Rawand core.text’s units.spawngives acore.task.Task, a decimal literal is acore.real.Real,a + bcallscore.ops.Add’sadd,get(xs, i)gives acore.types.Option, and a program’s own type or trait of one of these names is an ordinary one. examples/tasks/has three modules (model,memory,print) that use each other only through their contracts.
Mutation
Section titled “Mutation”Only a mut variable, a mut parameter, or a field of one can be changed, either by passing it mut or by marking it as a receiver. A call’s result is a value, not a place, so its changes would be lost: mut first(xs).tick() is an error. Bind it first (mut x := first(xs)).
A variable’s declared type holds for its whole life. Every assignment is checked against it: line.qty = 120 fails if qty is a Quantity, and x = -1 fails if x was declared mut x: Positive. Literals are checked at compile time and other values at run time. The value that a mut argument writes back is checked too. Passing a Positive variable to a mut k: Int parameter is a compile error, because the function could store any Int in it. Closures can’t change outer variables.
A place passed mut is read after the call’s other arguments have run. In both(mut g, bump(mut g)), bump changes g first, and both receives g as bump left it. The same goes for a receiver (mut g.add(bump(mut g))) and for an argument block that assigns g. Passing the same variable mut twice in one call (swap(mut g, mut g)) is an error, since each would write back its own value and one would undo the other.
Changing lists, dicts and sets. A list, dict or set held by a mut variable, a mut parameter or a field of one changes through its mut methods, marked at the call like any method that changes its receiver. Expressions build new values. Each operation has one form:
| Change in place | Cost (when nothing else holds it) | Contract (ensures) |
New value instead |
|---|---|---|---|
mut xs.push(x) |
O(1) amortized | xs == old xs + [x] |
xs + [x] |
mut xs.pop() → Option[A] |
O(1) | xs == old xs.take(len(xs) - 1) |
xs[..len(xs) - 1] |
mut xs.insert(i, x) (0 <= i <= len(xs)) |
O(n) | xs == old xs.take(i) + [x] + old xs.drop(i) |
xs[..i] + [x] + xs[i..] |
mut xs.remove(i) (0 <= i < len(xs)) |
O(n) | xs == old xs.take(i) + old xs.drop(i + 1) |
xs[..i] + xs[i + 1..] |
mut xs.clear() |
O(n) (each element dropped) | len(xs) == 0 |
[] |
mut xs.sort(), mut xs.sort_by(key) |
O(n log n) | xs == old xs.sorted() |
xs.sorted(), xs.sorted_by(key) |
mut xs.reverse() |
O(n) | xs == old xs.reversed() |
xs.reversed() (strings too) |
mut d.insert(k, v), mut d.remove(k) |
O(1) average | d.get(k) == Some(v), !d.contains(k) |
a changed copy: { mut e := d; mut e.insert(k, v); e } |
mut s.insert(x), mut s.remove(x) |
O(1) average | s.contains(x), !s.contains(x) |
s.union(Set { x }), s.difference(Set { x }) |
mut d.clear(), mut s.clear() |
O(1) | len(d) == 0 |
Dict[K, V] {} |
- If something else holds the value (a copy in another variable, a
forover it), the first change copies it once (values and copying). - The methods return
(), exceptpop, which returns the removed element. That element is a value like any other, so dropping it is an error; write_ := mut xs.pop(). - Calling a method without the
mutmarker (xs.push(3)) or as a function (push(xs, 3)) is an error that shows the correct form.strata fixrewrites such code:xs = push(xs, x)becomesmut xs.push(x), andys := sort(xs)becomesys := xs.sorted(). - The receiver is a variable or a field of one. An element is assigned whole (
grid[r] = grid[r] + [x]). verifymodels each operation exactly, with dicts and sets as the solver’s arrays. An index out of range is a proof obligation, as forxs[i].
Ranges and slices. A slice is clamped to the length, as in Python. Finite iterators, such as a string’s units, slice the same way: s.graphemes()[..3]. There are no negative indices; the end of xs is len(xs), as in xs[..len(xs) - 1]. xs[i] requires i to be in range. This is checked at run time and proven by verify: xs[0] on a NonEmpty[Int] is proven, while xs[len(xs) - 1] on an arbitrary list is not. get(xs, i) returns an Option instead.
Assigning an element. On a list that can be changed (a mut variable, a mut parameter, or a field of one), xs[i] = v means xs = set_at(xs, i, v): xs with element i replaced by v, the other elements and the length unchanged.
ihas the same condition as inxs[i],0 <= i && i < len(xs). It is a fault at run time and an obligation forverify, and the check is left out of the build once proven.- Places nest.
s.xs[i] = viss.xs = set_at(s.xs, i, v).grid[r][c] = visgrid = set_at(grid, r, set_at(grid[r], c, v)).xs[i].f = vchanges the field in a copy of the element and stores it back. +=and the other compound assignments read the element at the same index. An index that isn’t a variable or literal is evaluated once.- When the element type is refined (
mut xs: List[Positive]), onlyvis checked against it, not the whole list. - It is O(1). Compiled code replaces the element in place when nothing else holds the list, or, for a field, the record. For
grid[r][c], the same applies to the row: only a shared row is copied, in O(its length). Otherwise the list is copied first (O(n)), as withpushandinsert. - A slice (
xs[a..b] = …), a unit of a string, and a dict key (d[k], which can’t be read either) can’t be assigned.
Ranges, slices and comprehensions are stored in the tree as written, so they render back as written. Name resolution turns them into the builtins (range, slice, map, filter, fold), so the checker, compiler and prover treat them exactly like those calls.
Functions that allocate nothing: noalloc
Section titled “Functions that allocate nothing: noalloc”Allocation is implicit: values are reference-counted and freed when the last holder releases them. A function can be marked noalloc after its contract, next to terminates. strata check then rejects it if it might allocate, naming what allocates and where (`sum` is `noalloc` but allocates: a list made at run time (`[a, b]`)). terminates is assumed, but noalloc is checked.
fn total(xs: List[I64]) -> I64 noalloc { mut acc: I64 = 0.to_i64() for x in xs { acc += x } acc}The check covers the body and the contracts, including loop invariants, since contracts run when the function runs. An assumed contract never runs, so it is skipped. The following allocate:
- Building a value at run time: a list, tuple, record or variant with fields (
Point { x, y },Some(x)), a dict, set, closure, f-string or comprehension, a big integer literal, a task (spawn,scope,await,select,race), amock,?andexcept. A value built only from literals is a constant, built once, and doesn’t count. - Arithmetic that builds a value:
+ - * / % <<and negation onInt, because results past 2^62 become big integers (compute onI64or another sized integer instead), and+on strings and lists. Arithmetic on sized integers andFloatis allowed. - Updates that are only sometimes in place:
xs[i] = v,x.f = v,push,pop,insert,removeand the othermutmethods of lists, dicts and sets. They update in place when nothing else holds the value, but that is known only at run time (a value passed in may be shared), and otherwise they copy. The check assumes nothing about a value’s uniqueness. - Calls to any function or method that isn’t
noallocitself (a program’s functions and methods by name; a trait method isnoallocwhen it is declared so and so is every implementation), to function values (parameters, closures), and to builtins other than those that never allocate:len,byte_len,byte_at,at(xs[i]),ord,contains,starts_with,ends_with,is_alpha,is_digit,is_space,compare,min,max,sqrt,float, and the conversions to sized integers (to_i64,wrap_u8, …). A builtin’s name called as a method means the builtin on a list, string, dict, set or number, and the type’s own method on any other type, which must then benoalloc. - A
forover anything other than a list orrange(a, b)(a..b): a set or dict is turned into a list, and an iterator’snextbuilds eachOption. Aforover a list borrows its elements, and aforover a range counts.a..=bisrange(a, b + 1), and that+onIntcounts.
Reading is free: fields, elements, match, comparisons, if, while, for, return, string literals and constants. xs[i] faults when out of range, and a fault ends the program or the task, so it isn’t counted. Faults and failed contract checks don’t count either. Values the code generator places on the stack still count (a constructor that doesn’t leave its block, an argument the callee doesn’t keep), because where it does so depends on the whole program, and noalloc doesn’t rely on it.
An extern c fn can be marked noalloc. Like terminates and blocking, that is the C code’s promise and isn’t checked.
A whole module can be declared noalloc in its package’s strata.toml: noalloc = ["fast", "mem"] lists modules by their names in the package. A module includes the modules below it, main is the root module, and * means every module.
- Every function in those modules (plain functions,
impl, trait and capability methods,externfunctions) isnoallocwithout being marked, including functions added later. - A function there that allocates is an error from
check,editandverify, naming the function and what allocates. - Calls into those modules, from anywhere, are calls to
noallocfunctions. - A name in the list that isn’t a module of the package is an error.
core’s own strata.toml declares its runtime modules (core.rt, the allocator, and the modules it uses) and core.mem this way.
Lemmas and proofs: lemma, assert … by, calc
Section titled “Lemmas and proofs: lemma, assert … by, calc”A lemma states a fact for verify to prove once and for proofs to use. It is written like a function with no result: its requires are its hypotheses, its ensures is the fact, and its body (optional) holds the proof’s steps. It is never compiled, and only proofs call it. A recursive lemma is a proof by induction, so it must be shown to terminate.
lemma double_le(a: Int, b: Int) requires a <= b ensures a + a <= b + bThe statements of a proof:
assert e by lemma(args)andassert e by { steps }:eis proven from the steps alone, and is then known. The steps are lemma calls,asserts, nestedassert … by,ifandmatchcase splits, andcalcs.calc { a == b; _ <= c }: a chain of steps, each proven alone. Each_is the right side of the step before it. The chain givesa <= c.
lemma, by and calc are contextual keywords: lemma before a definition’s name, by after an assert’s condition, and calc before { at the start of a statement. Proofs compile to nothing, and views hide them by default. See Verification.
Errors
Section titled “Errors”- Each operator works only on its own type.
excepton anOption, ororon aResult, is an error. AResult[Option[T], E]therefore reads one way:load(k) except None or defaulthandles the error, then the missing value.
A task handle stays in its scope. A scope can’t return one, a task can’t hold another task’s handle, and a handle can’t be stored in a variable declared outside the scope.
A fault in a task (an index out of range, a broken contract) ends the program immediately, as anywhere else. Nothing catches it, and the other tasks stop too, so a thread waiting for what the failed task would have sent doesn’t hang. The call stack shows where the task was started and where it failed: main -> spawn -> pick. Under strata test, the fault fails only that test: the test’s other tasks stop at their next wait, and the next test runs.
A deadlock is a panic. A deadlock is reached when every task is waiting (in a recv, send, select, await, at the end of a scope, or in a virtual clock’s sleep) and no wait can end on its own. A real clock’s sleep or after timer can end on its own, so it doesn’t count. The program stops and reports what each task is waiting on, and where: deadlock: every task is waiting, so none can go on: main waits in the end of a scope, at main; a task waits in recv, at main -> spawn -> Receiver.recv; …. Under strata test, the test fails with that report. A task reading the console or a file isn’t waiting in this sense, since it can proceed on its own.
A scope whose last line is a spawn (scope { spawn work() }) evaluates to (), not the handle. Each task runs on its own thread.
Channels.
- Closing is explicit, once per holder. A value whose type holds a
Resource(aSender, std.net’sListener, or a record, tuple, list or sum holding one) carries the duty to close it: on every path out of the block that made or received it, the block closes it (tx.close(), or aResourcerecord’s ownclose), returns it, or moves it into a value it returns.editrefuses a path that does none, acloseon one branch only, acloseinside a loop of a value made before it, and any use after aclose. A task’s own blocks needn’t close: its copies close when it ends. A parameter is borrowed: its caller holds the duty. AReplycarries none: itssendis its close. A task copies each sender it captures, alone or in a record or tuple. - Any number of tasks can send to and receive from one channel.
Sender[T]andReceiver[T]are the types of the two ends.
race. A cancelled task stops at its next wait (a recv, send, await, select, sleep, or a console’s read_line), however deep in the call stack, and unwinds as return would. What it holds is dropped, nothing after the wait runs, and the tasks it started stop too. Code that doesn’t wait runs to its end, so a long computation isn’t interrupted halfway. The race ends once every loser has stopped, then runs the winner’s arm. Each left side follows spawn’s rules: no return, ! or ? out of it, and a sender it uses is its own copy, which it must close.
Supervised tasks.
- A handler after
await happlies to the result:await h except p { … }is(await h) except p { … }, andawait h!is(await h)!. - When a supervised task panics, every task below it stops at its next wait. Its copies of senders are closed, so receivers see the channel end. What it held is freed, including after a division by zero or a bad builtin argument. Only a stack overflow or running out of memory leaves its memory allocated. Its siblings and the task that started it carry on. A panic in an ordinary task below it counts as the supervised task’s panic.
- A
racearm can be supervised the same way:race { supervised fetch(a) as r => …, … }.ris then aResult[T, Panic], and a panic is that arm’s result. supervisedis a keyword only directly beforespawnand at the start of aracearm. It is an ordinary name anywhere else.
Time. In a select, clock.after(d) => … is an arm that fires once d has passed on that clock since the select began. It binds nothing: select { jobs.recv() as j => run(j), clock.after(ms(100)) => flush() }. Any Clock works, so a test can pass a clock whose time it controls (one that stands still, or runs faster), and its after arms and sleep follow that time.
A virtual clock’s sleeps end in deadline order, each once everything else is waiting. Timer tests are therefore exact and take no real time.
std has no stopped clock, so a test that needs one writes its own mock of Clock. It takes a few lines:
/// The time on a receipt, in seconds.fn stamp(clock: Clock) -> String { f"at {clock.now().value / 1000000000} s"}
/// A clock stopped at one time.mock StoppedClock = { at: Instant }
// (`sleep` is left out: a mock's missing methods are generated)impl Clock for StoppedClock { fn now(self) -> Instant { self.at }}
test "a receipt shows the time" { clock := StoppedClock { at: Instant(seconds(5).value) } assert stamp(clock) == "at 5 s"}Built-in types
Section titled “Built-in types”| Type | Notes |
|---|---|
Int, Float, Bool, String |
Int is arbitrary-precision, like Python’s: a machine integer while it fits and a big integer when it doesn’t, literals included. Float is never NaN or infinite; an operation whose result would be is a fault (1e308 * 10.0). Where that can’t be ruled out, for example in code that runs other programs’ arithmetic, checked_add, checked_sub, checked_mul, checked_div, checked_rem and checked_sqrt return an Option[Float], and checked_floor, checked_ceil and checked_round an Option[Int], with None in place of the fault. Ints and Floats never mix implicitly (float(n), round(x)) |
Real |
exact rationals, core.real’s record { num: Int, den: Positive } in lowest terms. The checker refuses one built from its parts (Real { num: 2, den: 4 }) or a part assigned outside core.real, so ==, hashing and Dict keys are structural. The checker writes each decimal literal as Real { num: 1, den: 10 } (from the Float’s shortest digits, so a literal of more than 17 significant digits is read as its nearest Float first) and each integer literal where a Real is wanted as Real { num: n, den: 1 }. The code generator makes a literal a constant block and calls core.real’s real_add, real_mul, real_cmp … for the operators, a result of one put on the stack or into the variable it is assigned to; core.real has no call-stack frames. A division checks its divisor (“division by zero”) unless verify proved it nonzero. verify encodes a Real as an SMT Real (floor is to_int) everywhere but in core.real, whose own functions see the record and are proven. A decimal literal pattern is where it == 0.5, written back as 0.5. % is Euclidean, as on Int. {x:.2} rounds ties to even, as on Float. The fuzzer makes a Real as an Int over a Positive, reduced. std.json’s and std.toml’s Number hold a Real (beside Integer(Int)): read exactly, written with the shortest digits that are exact (Real.number_text); writing a Real a double can’t give back exactly (Real.fits_double: 1/3, too many digits, past the range) is an error, and -0.0 reads as 0.0. std.sqlite stores a Real as its text (Row.real reads it back), the ORM’s Real columns are TEXT |
I8 … I64, U8 … U64 |
Fixed-width machine integers, each a separate type compiled to its own width (U32 is an LLVM i32). Arithmetic wraps: each operation (+ - * / % & | ^ << >>, -x) computes the Int result, then takes it modulo 2^width (two’s complement for the signed types). / and % are Euclidean, as on Int. A shift by the width or more gives 0, or -1 for >> of a negative number. They never mix with Int or with each other: x + n with x: U32 and n: Int is an error. Convert with x.to_int(), n.to_u32() (n must fit; verify proves it or a run-time check enforces it) or n.wrap_u32() (n modulo 2^32). An integer literal is an Int unless its context makes it a sized integer: an annotation (let b: U8 = 200), a parameter, a result, a field, a list of them, or the other operand of an operator (x + 1, x >> 3). It must fit (let b: U8 = 300 is an error). verify models them as integers in their range with every operation wrapped, so it finds where an ensures breaks on overflow (x + 1 > x fails for x = 255) |
() |
the unit type, for “no meaningful value”. It plays the role of Python’s None as a type |
Option[T] |
None or Some(x): a value that may be absent |
Result[T, E] |
Ok(x) or Err(e): the outcome of an operation that may fail |
List[T] |
an ordered sequence backed by a growable array, [1, 2, 3]. Changed with mut xs.push(x), pop, insert, remove, clear, sort, sort_by, reverse. xs.sorted() and xs.reversed() return new lists |
(A, B, …) |
tuples, (word, 3), destructured with (w, n) := … and accessed with t.0 |
Dict[K, V] |
a hash map: Dict { "a": 1, "b": 2 }, Dict[String, Int] {}, dict(pairs) from a list of pairs. get, keys, values, items. Changed with mut d.insert(k, v), mut d.remove(k), mut d.clear(). A JSON object when the keys are strings |
Set[T] |
a hash set: Set { 1, 2 }, Set[Int] {}, set(xs) from a list. union, intersection, difference, to_list. Changed with mut s.insert(x), mut s.remove(x), mut s.clear() |
+ adds numbers and concatenates strings and lists ("a" + "b", [1] + [2]).
Bit operations. &, | and ^ work bitwise on Ints as infinitely sign-extended two’s complement, as in Python: -1 & x is x, big integers included. a << n is a times 2^n, and a >> n is a divided by 2^n, rounded down (-5 >> 1 is -3). A negative n is a fault, which verify looks for. Precedence follows Rust and Python, all tighter than comparisons: |, then ^, then &, then the shifts, then + and -. So x & 255 == x is (x & 255) == x. The printer parenthesizes a bit operation inside a comparison, (x & 255) == x, because C, and so most readers, binds == first. In a refinement or a where pattern, where | separates union members or pattern alternatives, a bitwise or must be parenthesized (where (it | 1) == 3 | 0).
Strings are UTF-8. A string isn’t counted or indexed directly; the unit is always named: s.bytes(), s.code_points() or s.graphemes(). These are iterators (len(s.graphemes()), s.graphemes()[i]; see Iterators), and len(s), s[i] and s[a..b] produce errors that say so.
- Escapes in a literal (and an f-string’s text):
\n,\t,\r,\0,\",\\,\x41(two hex digits, up to\x7F) and\u{e9}(one to six hex digits, a Unicode scalar value). Any other backslash is kept as written, so a regex’s"\d+"needs no doubling. - Positions in a string are byte offsets.
index_of(s, sub)returns one, andbyte_slice(s, a, b)cuts at byte offsets in constant time. The offsets are clamped to the string, and an offset inside a character moves back to the character’s start.byte_len(s)is the length in bytes. - String functions don’t fail.
split(s, "")splits at every character (["", "a", "b", ""]),replace(s, "", x)putsxaround every character, andrepeat(s, n)withn < 1is"". - Other string functions:
chars,contains,starts_with,ends_with,split,join,lines,trim,replace,repeat,reverse,to_upper,to_lower,is_digit,is_alpha,is_space,ord,chr,parse_int, andmatches(regex). - JSON is provided by
std.json. A type that derivesJson, or a built-in type, is written withx.to_json()andstringify. float_bitsandfloat_from_bitsconvert a float to its IEEE 754 bits as anInt, and back.float_from_bitsreturnsNonefor a NaN’s bits.
f-strings interpolate any expression, formatted by to_string (Display, below): f"{name} has {len(xs)} items". {{ and }} are literal braces. A format spec follows a :, as in Python: [[fill]align][+][0][width][.precision].
{price:.2}gives two decimal places, or keeps at most 2 characters of aString.{name:<12},{n:>5}and{title:*^20}pad to a width. Numbers align right by default and everything else left.{n:05}pads with zeros after the sign.{delta:+}always shows the sign.- A spec that doesn’t suit the type is a check error.
Showing values: Debug and Display. Every type implements both traits implicitly.
Debug(debug(x),x.debug()) writes a value as code:Some("hi"),Point { x: 1, y: 2 },[1, 2],(1, "a"),Dict { "a": 1 },Set { 1, 2 },UserId(7), with strings quoted.Display(to_string(x),x.to_string()) is whatprintand f-strings show. AStringdisplays as its text and anything else as itsdebugform. Parts inside a list, record or other value are shown withdebug, as Python’sstrof a list usesrepr, so["a b"]and["a", "b"]stay distinct.
A type can replace either with its own impl, which is then used everywhere the type is shown, nested values included:
impl Display for Money { fn to_string(self) -> String { f"${self.cents / 100}.{self.cents % 100:02}" } }impl Debug for Secret { fn debug(self) -> String { "Secret { key: *** }" } }print(Money { cents: 1234 }) // $12.34print([Money { cents: 5 }]) // [Money { cents: 5 }]: parts by their `debug`print(Some(Secret { key: "k" })) // Some(Secret { key: *** })Order. compare(a, b) is the order sort uses, on any two values of the same type. It returns -1 when a comes first, 0 when they are equal, and 1 when b comes first. Generic code, including std.collections, orders by compare, because < and the other comparison operators work only on numbers, strings and types that implement Ord (see Operators). The order is:
- numbers by value, strings by bytes
- a
distinctvalue as its underlying value - variants in declaration order, then field by field
- tuples, records and lists element by element, with a shorter list first
xs = take(xs, n) (or o.f = take(o.f, n)) changes the list in place when nothing else holds it, like xs[i] = v. It drops the elements past n, so a list used as a stack or heap shrinks without a copy.
All values are immutable. A mut variable changes through its methods (mut d.insert(k, v), mut s.remove(x)), and a copy held elsewhere keeps the old value.
Dicts and sets are hash tables with no defined order. Iterating one (for, keys, values, items, to_list) or printing it gives the entries in table order, which may change whenever the implementation does. Nothing should depend on it; sort the keys when order matters. Hashing is deterministic, so a program behaves the same on every run. Equality is by content (Dict { "a": 1, "b": 2 } == Dict { "b": 2, "a": 1 }). Where dicts or sets are themselves ordered (<, sort, a set of sets), they compare as their sorted entries.
len, contains, get, insert, remove, sum, min, max and abs work on every type where they make sense. strata builtins lists every builtin with its signature. strata view NAME (view insert, view Dict) shows one with its doc, which ends with its cost: “Cost: O(1) amortized in place: in the statement d = insert(d, k, v) when nothing else holds d; O(n) otherwise (a copy …)”. See examples/builtins.strata.
Iterators. A for loop over an iterator works on its own copy of the iterator, so c is unchanged afterwards. It supports break, continue and patterns like any for. Generic code asks for the trait: fn collect[T, I: Iterator[T]](it: I) -> List[T]. verify can’t prove such a loop terminates, as it can for a list, because the iterator may never return None.
An iterator that always ends also implements FiniteIterator, a subtrait whose len is the number of elements next will still return:
trait FiniteIterator[T]: Iterator[T] { fn len(self) -> Natural fn fold[B](self, init: B, f: (B, T) -> B) -> B // given fn to_list(self) -> List[T] // given}verify proves that a for loop over a FiniteIterator terminates, as it does for a list. It relies on len, which it does not check. Over an iterator that may be unbounded, it reports that the loop may not terminate. fold exists only on finite iterators, since on an unbounded one it would never return (fold needs a FiniteIterator: N is an Iterator but not a FiniteIterator).
Every iterator has map, filter and enumerate (enumerate(it) or it.enumerate(), pairing each element with its index as (i, x)). They are lazy: each returns another iterator that does the work as the loop asks for elements, so they work on unbounded iterators too. A list’s enumerate is the list builtin and returns a list. A finite iterator stays finite when mapped, filtered or enumerated; a filtered one’s len counts the elements kept. A function passed to them must terminate, like the code around it. verify follows a lambda’s calls from the function it is written in (it calls spin, which may not terminate), which is how it proves that fold over a mapped and filtered finite iterator terminates.
type Naturals = { at: Int } // impl Iterator[Int]: 0, 1, 2, … without end
squares := Naturals { at: 0 }.filter(n -> n % 2 == 0).map(n -> n * n)for s in squares { if s > 100 { break } print(s) // 0, 4, 16, 36, 64, 100}Count { at: 0, end: 5 }.map(n -> n + 1).fold(0, (a, b) -> a + b) // 15On a list, xs.map(f), filter and fold are the list builtins and return lists.
A comprehension over an iterator is a lazy iterator, so it can be unbounded, and it is finite when its source is. Set and dict comprehensions collect a finite one.
odd_squares := [n * n for n in Naturals { at: 0 } where n % 2 == 1] // 1, 9, 25, … on demand[x * 2 for x in Count { at: 0, end: 4 }].to_list() // [0, 2, 4, 6]Set { x % 2 for x in Count { at: 0, end: 4 } } // Set { 0, 1 }Lists, sets and dicts are not iterators (their map and filter are the list builtins), but iter() returns a finite one: the prelude’s ListIter, over a list’s or set’s elements or a dict’s entries in table order. A range is a list, so (0..n).iter() works too.
fn total[I: FiniteIterator[Int]](it: I) -> Int { it.fold(0, (a, b) -> a + b) }
total([3, 1, 2].iter()) // 6total((0..5).iter()) // 10for (k, v) in d.iter() { … } // a dict's entries, in no set orderA string is not an iterator either. Its units are always named, and each is a finite iterator:
s := "héllo 👋🏽"s.bytes() // its UTF-8 bytes, 0 to 255: 104, 195, 169, …s.code_points() // its code points (Unicode scalar values): 104, 233, …, 128075, 127997s.graphemes() // its grapheme clusters (UAX #29), as strings: "h", "é", …, "👋🏽"s.graphemes().len() // 7[chr(c) for c in s.code_points() where c > 127] // lazily: "é", "👋", "🏽"Each walks the string once. bytes() and code_points() are defined in the prelude and decode the UTF-8 bytes; the only native operations are byte_len and byte_at. graphemes() comes from std.unicode, which is loaded only for programs that call it. It implements Unicode 16.0’s extended grapheme clusters, including combining marks, emoji sequences, flags, Hangul syllables and Indic conjuncts, and passes every case of Unicode’s GraphemeBreakTest.txt.
A finite iterator supports the list syntax for length, indexing and slicing: len(it), it[i] (at, which requires i in range), get(it, i), it[a..b], it[a..]. Slicing is lazy (drop then take), and join collects a finite iterator of strings. Every iterator has take(n), which is finite even on an unbounded iterator, and drop(n). Each walks the iterator from the start, so indexing is O(i).
s := "héllo 👋🏽!"len(s.graphemes()) // 8 (len(s.bytes()) is 16)s.graphemes()[6] // "👋🏽"join(s.graphemes()[1..3], "") // "él"Naturals { at: 0 }.drop(3).take(4) // 3, 4, 5, 6Not supported: nested comprehensions whose inner source is an iterator, and generators.
Foreign functions: extern c
Section titled “Foreign functions: extern c”A C function is declared with extern c fn: a signature with contracts and no body. A pointer that C hands out is declared with extern c type. examples/fuzzy uses its own C file and libm this way:
extern c fn edit_distance(a: String, b: String) -> I32 ensures it >= 0
/// libm's cube root.extern c fn cbrt(x: Float) -> Float
/// The fewest single-character edits that turn `a` into `b`.pub fn distance(a: String, b: String) -> Natural { edit_distance(a, b).to_int()}# strata.toml: the C the declarations are linked to[native]sources = ["c/text.c"] # the package's own C fileslibs = ["m"] # libraries (-lm)include_dirs = [] # folders of headers the C files include (-I)lib_dirs = [] # folders the libraries are in (-L, and searched at run time)Paths are relative to the package’s folder, or absolute. compiler/strata.toml, for example, links LLVM’s C library from Homebrew’s /opt/homebrew/opt/llvm.
-
Types that cross. The sized integers (
I8…I64,U8…U64, C’sint8_t…uint64_t),Float(double),Bool,String(copied in each direction as a NUL-terminated string),()(void, as a result),extern c typehandles, andOptionof a handle (a pointer that may be NULL, asNone).Intdoesn’t cross, since its size isn’t fixed. Amutparameter is an out-pointer (mut db: Option[Sqlite3]is asqlite3 **), copied in and back out after the call, like anymut. AStringcan’t bemut. -
Handles. A handle (
extern c type Sqlite3) is a type with no constructor and no contents. Only C creates one, and Strata code passes it back. Strata doesn’t reference-count or free it; C frees it through a function you call. Two handles are equal when they are the same pointer. A C function that returns NULL where a handle is declared is a fault; declare that resultOption[Sqlite3]. -
Checks at the boundary. Arguments are checked against their types (an
I8is in range) and therequiresbefore the call, and the result against its type and theensuresafter it. A string containing a NUL character can’t be passed, since C would read it as the end. -
The name is the C symbol, so an extern function can’t be renamed. Wrap it instead.
-
Only its own module calls it. Extern definitions are never
pub, and other modules, submodules included, can’t call them. The declaring module exposes its own functions to the rest of the program, or a capability for anything that touches the outside world, so signatures still show what code can touch. -
Contracts are assumed.
verifynever proves foreign code. A foreign function’s contract (ensures it >= 0) is assumed at each call, and the calling code is verified like any other; fuzzing runs the compiled code, C included. Since nothing proves the contract, compiled programs check it at each call. -
Calls that wait:
blocking. A C function that may wait (a read, a sleep, a lock, SQLite’ssqlite3_step) is markedblockingafter its contract, asnoallocandterminatesare. A task’s call to it runs on one of up to 8 helper threads while the task is parked, so the other tasks on its carrier go on. Such a call costs a hand-off to a helper and back, about 5 to 50 µs, and can’t be cancelled: the task waits for it to return. An unmarked call runs on the carrier and holds it for as long as it takes. A marked function takes at most 8 integer or pointer arguments and 8Floats, and aStringit gives is copied on the helper thread. Only anextern cfunction can beblocking.extern c fn sqlite3_step(stmt: Sqlite3Stmt) -> I32blocking -
C calls work everywhere code runs: in
strata build, and instrata runandstrata testthrough the JIT, which compiles the[native]sources into a library with clang and loads it. The C standard library (strlen, …) needs no[native]section. -
A native module’s capability is requested like any other.
mainand tests name it (fn main(sqlite: Sqlite)), its functions receive it asself(sqlite.open(path)), and the program is linked with the package’s[native]libraries. std’s native libraries are the system’s: std.sqlite calls macOS’s libsqlite3.strata buildlinks it into the executables that use it, against a stub (compiler/c/libsqlite3.tbd) so no SDK is needed, andstrata runandstrata testload it for the JIT. std’s precompiled library excludes the code that calls C, so programs that don’t use it don’t link it.
Not supported: linking a dependency’s [native] C into the programs that use it, passing structs by value, callbacks, buffers (so std.sqlite has no blobs), and languages other than C, Python, Rust and TypeScript.
Foreign functions: extern python
Section titled “Foreign functions: extern python”A Python function is declared with extern python "MODULE" fn, followed by a signature with contracts and no body. MODULE is the Python module that defines it. Values cross as JSON through the derived Json trait (std.json), so parameter and result types must have a JSON form: Int, Float, Bool, String, List, Option, Dict[String, V], records and unions that derive Json, and aliases of those. examples/python calls text_utils.py, which sits next to its strata.toml:
from std.python use PythonError
/// A post summed up (Python builds the `Card` as a dict).extern python "text_utils" fn summary(post: Post) -> Result[Card, PythonError]
/// Text tools, in Python.pub capability TextTools { fn card(post: Post) -> Result[Card, PythonError] { summary(post) }}# strata.toml (both optional)[python]executable = "python3" # the Python to run (default: python3 on the PATH)path = ["py"] # where its modules are, relative to the package (default: the package's folder)-
Every call returns a
Result. The result type is writtenResult[T, PythonError](std.python). These failures are values, never faults:Raised: a Python exception, with its type’s name, message and traceback. A failed import is reported this way too.WorkerFailed: the worker couldn’t start, or exited while answering.BadReply: the result isn’t aT(a set, aNaN, an integer beyond 2^53, a missing field, a broken refinement).NotEncodable: an argument has no JSON form (an integer beyond 2^53).
Result[(), PythonError]accepts any return value. -
Encoding. Arguments are encoded with
to_jsonand passed positionally; a record becomes a dict with its fields and"type". The return value is decoded withT.from_json, checking its refinements. Pydantic models or TypedDicts on the Python side therefore match the derived JSON (see std.json). -
One worker per run. The first call starts
python3 -c DRIVER, with a driver supplied by the runtime. The worker stays up for the rest of the run, imports the modules, and answers each call with one JSON line. Calls from several tasks take turns. Anything the Python code prints goes to standard error. If the worker exits, the call it was answering fails and the next call starts a new worker. Module folders are fixed as absolute paths at compile time, so a built executable finds them wherever it runs. -
Only the module’s capabilities call it.
extern pythonfunctions are neverpub, and only capabilities in the same module may call them; the module’s other functions and tests can’t. Code that reaches Python therefore takes the capability, whichmainand tests ask for. -
verifynever runs Python. Anextern pythonfunction is assumed (≈). Its contracts are checked at run time:requiresbefore the call andensuresafter it.
Not supported: Python objects kept on the Python side (handles), keyword arguments, a Python function with a different name from its Strata declaration, a pool of workers, embedding libpython, and async functions.
Foreign functions: extern rust
Section titled “Foreign functions: extern rust”A Rust function is declared with extern rust "CRATE" fn, followed by a signature with contracts and no body. CRATE is a folder with a Cargo.toml, relative to the package ("text_tools", "crates/text"), or a crate of crates.io by name ("name", "name@1.2"). "text_tools::words" names a module in the crate. The Rust function has the Strata name. Values cross as JSON, as for extern python: through the derived Json trait on the Strata side and serde on the Rust side, so the Rust parameters are serde::Deserialize and the result serde::Serialize. examples/rust calls the crate text_tools/, next to its strata.toml:
from std.rust use RustError
/// A post summed up (Rust builds a `Card` struct).extern rust "text_tools" fn summary(post: Post) -> Result[Card, RustError]#[derive(serde::Deserialize)]pub struct Post { pub title: String, pub body: String, pub tags: Vec<String> }
pub fn summary(post: Post) -> Card { ... }- How it is built.
strata run,strata testandstrata buildwrite a crate into.strata/rustin the package: one#[no_mangle] extern "C"function per declaration, which reads the arguments from a JSON array (serde_json), calls the Rust function and writes its reply. They build it withcargo build --releaseinto a shared library,cargofrom thePATHor[rust] cargoin strata.toml. The first build compiles serde and the crates (seconds); after that cargo finds nothing to do (about 50 ms). Without cargo, these commands fail with an error saying what to install. A declared function the crate lacks is cargo’s compile error. - How it is called. cargo builds the crate twice over: a static library and a shared one.
strata buildlinks the static library into the executable (-force_load, then-dead_strip), together with the system libraries Rust’s std needs (-lSystem -lc), through the C compiler. The executable runs from anywhere, with.stratadeleted. On macOS, Rust’s std, serde_json and the shim add about 0.5 MB to it; std.rust and std.json add another 0.15 MB.strata runandstrata testload the shared library instead (dlopen) on the first call. Each call is a C call with a JSON string each way, and calls from several tasks run at once. - Every call returns a
Result. The result type is writtenResult[T, RustError](std.rust):Failed: the Rust function returnedErr(e), withe.to_string(). A Rust function returningResult<T, E>(E: Display) answers itsErrthis way; any other return type is the value.Panicked: the function panicked. The panic is caught, and the next call works. Rust prints the panic to standard error.BadArguments: the arguments aren’t what the Rust function takes (serde’s message).NotLoaded: the library couldn’t be loaded, or lacks the function.BadReply,NotEncodable: as forextern python.
- The same rules as
extern python: neverpub, called only from the module’s capabilities, assumed byverifyand never run by it, contracts checked at run time.
Not supported: a dependency’s extern rust (only the package’s own modules are read for the crate), linking with lld (lld’s stub of libSystem lacks what Rust’s std calls, so these executables are linked by clang), Rust values kept on the Rust side (handles), and generic Rust functions.
Foreign functions: extern ts
Section titled “Foreign functions: extern ts”A TypeScript (or JavaScript) function is declared with extern ts "MODULE" fn, followed by a signature with contracts and no body. MODULE is text_utils for text_utils.ts (or .mts, .js, .mjs) in the package’s folder or the folders of [ts] path, else a package in their node_modules. The exported function has the Strata name. Values cross as JSON, as for extern python. examples/ts calls text_utils.ts:
from std.ts use TsError
/// A post summed up (TypeScript builds the `Card` as an object).extern ts "text_utils" fn summary(post: Post) -> Result[Card, TsError]# strata.toml (both optional)[ts]executable = "node" # the runtime (default: node on the PATH; bun and deno work too)path = ["ts"] # where its modules are, relative to the package (default: the package's folder)- One worker per run, as for
extern python: the first call startsnode -e DRIVER(bun -e,deno eval), the driver a string in the runtime, answering each call with one JSON line. Node 22.18 and later run.tsfiles by stripping their types; an older Node needs.js. A function returning a promise is awaited. Calls from several tasks take turns. What the code prints (console.log) goes to standard error. If the worker exits, the call it was answering fails and the next call starts a new worker. - Every call returns a
Result. The result type is writtenResult[T, TsError](std.ts):Thrown(an error thrown, with its name, message and stack; a missing module or function too),WorkerFailed,BadReply(abigint,NaN, an integer beyond 2^53, a wrong shape, a broken refinement),NotEncodable.undefinedisnull. - The same rules as
extern python: neverpub, called only from the module’s capabilities, assumed byverifyand never run by it, contracts checked at run time.
Not supported: JavaScript objects kept on the worker’s side (handles), a pool of workers, and embedding a JavaScript engine.
Exporting functions: derive C for f
Section titled “Exporting functions: derive C for f”To let C call Strata, a trait can be derivable over functions (derivable(Function) trait C). derive C for add, with a function’s name after for where a type’s would go, makes add callable from C. C is defined in std.ffi:
from std.ffi use C
/// The sum of two numbers.pub fn add(a: I32, b: I32) -> I32 { a + b}
derive C for addstrata build --lib builds the program as a shared library (libNAME.dylib, or the name given with -o). The library exports each derived function under its own name. A header (NAME.h) is written next to it, declaring each function after its doc:
/// The sum of two numbers.int32_t add(int32_t a, int32_t b);strata build calc.strata --lib # libcalc.dylib, calc.hclang -o use use.c -L. -lcalc -Wl,-rpath,@executable_path-
Types that cross are those of
extern c, in the other direction: the sized integers,Float(double),Bool(bool),String(const char *in, copied;char *out, a copy the caller frees withfree),()(void), handles (extern c type,void *) andOptionof a handle (NULL forNone). A function with any other type, or with type parameters, is rejected at thederive, which names the parameter. -
The library starts its runtime when loaded. The host keeps its signal handlers and arguments. A library has no
main;--libleaves it out. A fault in Strata code ends the process, as in a program. -
A library exports only its derived functions, plus
strata_string_free(below). The program’s other functions, std’s and the runtime’s are hidden, so two Strata libraries can be loaded in one process. -
derive C for modulederivesCfor everypub fnin the module. A function that can’t cross is reported as an error, not skipped. -
A derive over a function produces top-level definitions, since a function has no type to attach an
implto. Eachderived fnof the trait is identified by its shape alone:-
derived fn export() -> Self = Selfexports the function itself under its own name. -
A
derived fnwith parameters (derived fn json(args: String) -> String { .. }) is a wrapper around the function, exported asNAME_METHOD(add_json). Its body calls the function withSelf.call(p -> e)orSelf.try_call(p -> e), whereegives each argument andpis its parameter, aField. -
derived fn h() -> String { comptime ... }produces, at compile time, the function’s part of the fileNAME.h. HereSelfis the function, as astd.typing.Typeof kindFunctionwith itsname,docandparameters. A file is its parts in order, with each paragraph (up to a blank line) included once. -
A
derived fntakingself(derived fn name(self) -> String { .. }) is a method of a type the derive makes for the function (Tool$add, a record of the parameters it binds), which implements the trait. Where the function is used as a value, not called, it is that type’s value, soaddis a value of the trait:std.agent’sderive Tool for addputsaddin aList[Tool]. Calls ofaddare unchanged.
Cis therefore written in Strata, likeJson(see “Derives on functions” in IDEAS.md). -
-
Arguments. A derive takes arguments after its trait, each a word and names:
derive Tool(bind: net) for search, several separated by;. They areSelf.clauseswhile deriving, as a type derive’s block clauses are.bindis read by the derive machinery itself. The parameters it names are left to the use: they are the fields of the type made for the function, given assearch.with(net)in their order inbind, andSelf.callpasses them fromself. A function with bound parameters used as a value without.withis an error: “searchneedsnet: writesearch.with(net)”. -
Written above the function:
derive Tool foron the line beforefn add(…)derives it for that function, asderive Json fordoes for a type.
derive Python for f
Section titled “derive Python for f”derive Python for add (std.ffi) exports char *add_json(const char *args).
- The arguments arrive as a JSON object keyed by name. Each is decoded with its type’s
Json, refinements checked, and an omittedOptionisNone. - The answer is
{"ok": RESULT}or{"error": {"kind", "message", "value"}}, never a crash.kindisjson,arguments,result, orerrfor the function’s ownErr, whose value is invalue. - The caller frees the answer with
strata_string_free.
strata build --lib writes NAME.py next to the library. It is a typed Python module that loads the library with ctypes, with one function per derived function, a dataclass per record, a class per union with a subclass per variant, and errors raised as StrataError:
import geo # geo.py and libgeo.dylib, side by sidegeo.middle(geo.Point(0.0, 0.0), geo.Point(2.0, 4.0)) # Point(x=1.0, y=2.0)geo.divide(1, 0) # raises StrataError("err", "ByZero", {"type": "ByZero"})Not supported: derive Rust (the same NAME_json export with another file), static libraries, mut parameters, and callbacks.
derive TypeScript for f
Section titled “derive TypeScript for f”derive TypeScript for clamp (std.ffi) exports clamp_json, the same JSON export as derive Python. strata build --target wasm32 builds a WebAssembly module, NAME.wasm, that exports it, and writes NAME.ts next to the module. NAME.ts loads the module with the standard WebAssembly API, so it runs in Node, Bun, Deno and browsers, with no native code. It has one typed function per derived function, an interface per record, a union type per union (variants tagged by "type", as their JSON is), and errors thrown as StrataError. examples/wasm holds a module and a test:
import { clamp, summary, StrataError } from "./stats.ts"; // stats.ts and stats.wasm, side by sideclamp(15, 0, 10); // 10summary([3, 9, 6]); // { type: "Summary", count: 3, mean: 6, best: 9 }summary([]); // throws StrataError: kind "err", value "no scores"clamp(1, 10, 0); // throws StrataError: kind "fault" (`requires lo <= hi` broken)- How the module is built.
- The program, the std code it uses and core.rt are compiled by LLVM for wasm64. That is the same IR as a native build, with 8-byte pointers.
- runtime.c is compiled without its threads, files and processes (
STRATA_WASM).runtime/wasm.cprovides the few C library functions the runtime needs: memory overmemory.grow, formatting, and the host’s imports. wasm-ldlinks these, keeping what the exports reach.wasm-optlowers the module to a 32-bit memory (--memory64-lowering, as Emscripten’sMEMORY64=2does). Pointers stay 8 bytes in memory, so the module runs where memory64 doesn’t: Node 22, Bun and Safari.- The build needs Homebrew’s
llvm,lldandbinaryen(STRATA_WASM_CLANG,STRATA_WASM_LDandSTRATA_WASM_OPTname others), and takes about 3 s.
- What it costs. A module exporting one small function is about 97 KB (34 KB gzipped). A call takes about 15 µs from TypeScript, most of it in the JSON export: the same
add_jsoncalled natively from C takes 12 µs. - The host. The module imports only five functions, which
NAME.tsprovides: output (toconsole), float text and parsing, and the end of a faulting call. Any other import is a fault when called. - Faults. A broken
requiresor another fault throwsStrataErrorwithkind"fault". The loader then starts the module again on the next call (fresh memory). A refinement broken by an argument is an"arguments"answer, as forderive Python.
Not supported in a module: tasks, files, processes, the clock, extern c, and main (a module only has exports). A module also doesn’t share std’s precompiled library, so each module carries the std code it uses.
Markup
Section titled “Markup”Markup is an expression, read by the parser into calls to std.web and shown by view as markup again. The stored tree has only the calls.
| Written | Read as |
|---|---|
<tag attrs>children</tag> |
std.web.element("tag", [attrs], std.web.children([children])) |
<tag attrs />, <tag attrs></tag> |
std.web.element("tag", [attrs], []) |
<>children</> |
std.web.children([children]), a List[Html[M]] |
name="text" |
std.web.attr("name", "text") |
name={e} |
std.web.attribute("name", e): e is a String, an Int, or a Bool (a flag, left out when false) |
name |
std.web.flag("name") |
on_click={e} |
std.web.on_click(e): any attribute starting with on_ is a call of the std.web function of that name, with e |
| text | a string literal |
{e} |
e: a String, an Int, an Html[M] or a List[Html[M]] (std.web.Child) |
- Markup starts at a
<where an operand goes (at the start of a line, or after a token that can’t end an operand, such as(,,,:=,->orreturn), followed at once by a letter or>.a < banda <bcompare. - A tag is lowercase letters, digits and dashes, other than
script. An attribute’s name is letters, digits,-,_,:and.; names starting withonother than theon_events, andsrcdoc, are refused. An attribute holding a URL (href,src,action, …) whose scheme isn’thttp,https,mailtoortel(javascript:,data:) is written#(std.web.safe_url); a relative URL is kept. Void elements (area,base,br,col,embed,hr,img,input,link,meta,source,track,wbr) are written<br />. - Text runs to the next
<or{, and can’t hold}(write{"}"},{"<"}). It is read as in JSX: where a line breaks, the spaces next to the break are dropped, lines left blank are dropped, and the rest are joined by one space. Text on one line is kept as written, soHello, {name}!keeps its spaces. viewwrites an element on one line when it fits, else each child (or run of children that fits) on a line of its own and, when the start tag is too long, each attribute too. Text whose spaces a line break would drop is written in braces ({" more"}). Calls that markup can’t write the same way (a non-literal tag, an attribute made by another function) are shown as calls.
Programs
Section titled “Programs”Command-line options are a record that derives CommandLine (std.cli). derive CommandLine for Options generates the record’s from_args and help at compile time.
- Each field is parsed according to its type:
- a
Boolis a flag (--loud) - an
Option[T]is an option with a value (--times 3or--times=3) - a
List[T]takes the remaining positional arguments - any other field is a required positional argument, in field order, or an option (
--times 3) if it has a default
- a
- An omitted field takes its default.
Tis anInt,FloatorString, a refinement of one, or a choice: a sum type whose variants carry no data (type Count = | Lines | Wordsacceptslinesorwords).- Names use dashes (
dry_runis--dry-run), and--ends the options. - Every refinement is checked (
--times: 0 does not satisfy …), and a usage error exits with code 2. --helpis generated from the record’s and fields’///doc comments.- A field whose doc starts with
-ncan also be given as-n. - An
Optionfield whose doc starts with[name]is an optional positional argument. - A
Listfield whose doc starts with its flag (/// --rm …) or a short name is a repeatable option (--rm a --rm b). strata checkrejects aCommandLinerecord that no command line could fill.
Subcommands are a union that derives CommandLine. Each member is a subcommand (Edit is edit), its fields are the subcommand’s options, and its /// doc is its help. An options record with a field of that union type parses its other fields as global options before the subcommand.
main’s parameters. main can also take Console (std.io, chosen by the environment: the terminal) and capabilities, in any order. std provides Disk (std.fs), Process and Launcher (std.process), Codebase (std.code: the codebase itself as the tools see it, a Code), Sqlite (std.sqlite) and Net (std.net: TCP connections and listeners); a program can define its own. main passes them on where Files or Environment is expected: fn main(mut console: Console, disk: Disk) { wc(mut console, disk) }, with wc taking any Files so tests can pass mem_files(...).
Exit codes. main returns () (exit code 0), an ExitCode (Int where 0 <= it && it <= 255), or a Result of either. An Err is written to standard error and the exit code is 1. A run-time fault exits with 101. strata run <fn> [ARGS] calls a single function instead, with its arguments written as code.
The standard library
Section titled “The standard library”The standard library is the std/ package. It is written in Strata and built into strata, so any program can use std.… without declaring a dependency. strata docgen generates a reference page for each module from its doc comments (the index), listing its types, methods, traits and functions with their signatures, contracts and docs. strata doc MODULE shows the same page in the terminal.
| Module | |
|---|---|
| std.array | N-dimensional arrays of Float, I64 and I32 over a flat list: views, broadcasting, reductions along an axis, dot and matmul, with shapes in contracts. |
| std.cli | Command-line options parsed into a record, and subcommands into a union. |
| std.code | A codebase as Strata’s tools see it: definitions as text with their dependencies and dependents, checker findings, cached verification results, and the package log. |
| std.collections | Sorted dictionaries and sets (AVL trees), and a priority queue (binary heap). |
| std.ffi | Calling Strata from other languages: derive C for f, derive Python for f. |
| std.fs | Files and folders. |
| std.agent | An LLM agent loop: a conversation of Messages, tools (the Tool trait, derived for functions: derive Tool for add), and run / run_streamed, which run the tools a Model calls (Claude, through Anthropic’s Messages API, streamed or not, or a mock). |
| std.http | HTTP/1.1: requests, responses and routes, a client (send, fetch, open for a body read as it arrives, TLS for https://) and a server (serve), over any Stream. |
| std.io | Console text input and output. |
| std.json | JSON text, and reading and writing records and sum types as JSON. |
| std.memory | Arenas, for programs that must be fast. |
| std.net | TCP connections and listeners (Net), read and written through the Stream trait; TLS for clients. |
| std.process | The running program’s arguments, environment variables and exit code, and running other programs. |
| std.orm | The mapping between records and SQLite tables: derive Table (the table, record to row and back, keys, typed conditions). It runs nothing: Database (std.sqlite) runs every operation. |
| std.regex | Regular expressions. |
| std.sqlite | SQLite databases through the system’s libsqlite3: connections, prepared statements and transactions. |
| std.term | Terminal text styles: bold, dim, colors. |
| std.time | The clock: the current time, sleeping, and timers in select. |
| std.toml | TOML 1.1 text, and reading and writing records and sum types as TOML. |
| std.typing | Types as values, for derivable traits. |
| std.unicode | Code point sets, Unicode properties and simple case folding (Unicode 16.0.0). |
| std.unicode_tables | Unicode 16.0.0 character data, for std.unicode. |
| std.utf8 | Encoding text as UTF-8 bytes and decoding it. |
| std.web | HTML pages built as values, written as markup. |
JSON (std.json) is read into records and sum types that derive Json, in the form a web front end sends:
- A record is an object. Writing adds
"type", the type’s name; when reading,"type"is optional. - An omitted field takes its default.
- An
Optionis the value, ornull, or omitted. - A sum’s variant is an object whose
"type"names it ({"type": "Circle", "r": 2}). A single positional field is written as"value". - A sum whose variants carry no data is a string (
"Quiet"). - A tuple is an array, and a
distincttype is its underlying value. - A union is its member’s value. Reading takes the first member the JSON fits. Unions whose members’ JSON forms overlap are rejected:
Int | Float, two list types,Stringnext to a choice, or two members with the same"type".
Every refinement is checked, and errors give the path: home.zip: "1234" does not satisfy …. Keys that the record doesn’t have are ignored, unless strata.toml sets unknown_keys = "error" under [json]. from_json_with(j, Reject) or from_json_with(j, Ignore) decides for a single call.
Integers must be within ±(2^53 − 1), the integers a JavaScript number holds exactly (std.json.SafeInt). Reading or writing a larger one is an error, which is why to_json returns a Result. A package that wants this proven instead sets integers = "proven" under [json]. Every integer field must then be shown to be a SafeInt, by its type or a refinement within it, and strata verify fails on any field that might not be.
A Real is written as a number only when a reader of IEEE doubles (JavaScript’s JSON.parse) gets exactly it back, Real.fits_double(): the nearest double’s shortest digits are the Real (0.1, 2.5), or the double is it exactly (2^60). Anything else is an error from to_json naming the field: 1/3 (no terminating decimal), 1.00000000000000001 (more digits than a double holds at that size), anything past 1.8 × 10^308. Nothing is rounded silently. Reading into a Real is exact, from the digits as written. reals = "proven" under [json] refuses a Real field at compile time instead: only a Float is allowed, converted with to_float() where the value is built. std.toml does the same for TOML’s floats ([toml]’s reals).
A type with no JSON form (a function, Option[Option[T]], a field named type, a variant with several positional fields) is an error at the derive, and so is a field whose type doesn’t derive Json. A generic type derives Json for every type argument that has it (derive Json for Pair covers Pair[T] for each T: Json). Through the generic impls, Some(x) where x is written as null (Some(None)) is an error from to_json, and from_json checks the elements’ refinements (read[List[Port]]("[70000]") is an Err naming Port).
from std.json use Json, parse, stringify
derive Json fortype Signup = { email: String where contains(it, "@"), age: Int where it >= 13, nickname: Option[String] }
signup := Signup.from_json(parse(body)!)!text := stringify(signup.to_json()!) // {"type":"Signup","email":…,"age":…,"nickname":null}TOML (std.toml) has a parser written in Strata, which the tools also use to read strata.toml. It enforces every rule of TOML 1.1:
- keys and tables defined twice
- tables closed once written: inline tables, and tables created by dotted keys can’t get a
[header], nor can header tables be extended by dotted keys - integers within 64 bits
- dates that exist (
2023-02-29is an error) - optional seconds (
07:32) - inline tables over several lines with a trailing comma
\eand\xHHescapes
A Strata Float is finite, so inf and nan are errors. The case toml-documents checks the parser’s readings of 104 documents (tests/data/toml-documents.out), and that what stringify writes reads back as the same value.
from std.toml use parse, lookup, Integer
doc := parse("[server]\nport = 8080\n")!port := match lookup(doc, ["server", "port"]) { Some(Integer(n)) => n, _ => 80,}derive Toml reads records and sum types from TOML as derive Json does from JSON, in the form of a configuration file:
- A record is a table of its fields, with no
typekey. - An omitted field takes its default, and an
Optionfield is omitted forNone. TOML has no null, so anOptionanywhere other than a field is an error at thederive. - A sum’s variant is a table whose
typenames it, with a single positional field written asvalue. A sum whose variants carry no data is a string. - A tuple is an array, a
Dict[String, V]is a table, andDate,TimeandDateTimeare TOML’s dates and times. - Every refinement is checked, and errors give the path (
servers[0].port: 70000 does not satisfy …). - Integers are 64-bit.
- Unknown keys are ignored unless
strata.tomlsetsunknown_keys = "error"under[toml], or the call usesfrom_toml_with(v, Reject).
The methods are named for TOML (to_toml, from_toml, read_toml, write_toml), so a type can derive both Toml and Json.
from std.toml use Toml
type Server = { host: String, port: Int where 0 < it && it < 65536 = 8080 }type Config = { name: String, servers: List[Server] = [] }derive Toml for Serverderive Toml for Config
config := Config.read_toml("name = \"app\"\n[[servers]]\nhost = \"a.example\"\n")!text := config.write_toml()!Databases (std.sqlite). Every database operation belongs to the Database capability, whose model rows is the set of each row’s table and key ((String, KeyValue)). impl Database for Connection is the real SQLite connection; any other type can implement it (a test’s database that holds no rows).
- Row operations work on a
Schema(table,columns,key,condition) with values in column order, without std.orm or SQL text:create_tableandupdate_row(rowsunchanged),insert_row(returns the key, which was no row’s before, and adds it torows),delete_row(Ok(true): the key was a row and is no longer;Ok(false): it wasn’t),load_row(Ok(Some): the key is a row),select_rows,count_rows, anddelete_rows(after whichrowsis unknown, as after SQL text). Every failure is anErrand changes nothing. OnlyConnection’s row operations areassumed(they call SQLite); a program’s contracts onrowsare proven from these contracts (read the model asdb.rows()in a caller). - Record operations are the trait’s default methods, over std.orm’s mapping, proven from the row operations:
db.create(Todo.table()),db.insert(t)(theKey[Todo]the database gave),db.update(t),db.delete(k),db.load(k)(Option[Todo]),db.select(q),db.first(q),db.count(q)anddb.delete_where(q). execute,queryandruntake SQL text, with values always passed as parameters;begin,commitandrollbackrun transactions.- Values. An
SqlValueis what SQLite stores:Int | Float | String | Bool. A value that may be NULL is anOption[SqlValue],Nonefor NULL: a row’s values (Row.values), parameters ([Some("apple"), sql(note), None], wheresql(x)isx.to_sql():Someof its value,Nonefor anOption’sNone) and the values ofinsert_rowandupdate_row. A key is never NULL:delete_rowandload_rowtake its values as aList[SqlValue], and the model holds it as aKeyValue(Int | String). - SQLite refuses a row naming a key of no row, a key in use and a row breaking a
CHECKasErr(Constraint); nothing requires them beforehand.
Tables (std.orm). A record that derives Table maps to an SQLite table, named after the type in snake case (TodoItem becomes todo_item). std.orm runs nothing and builds no SQL: Todo.table() describes the table (a TableOf[Todo] holding a std.sqlite.Schema), t.values() gives a record’s row, Todo.from_row(r) its record, t.key() its Key[Todo], and typed conditions build queries.
- Columns. Each field is a column.
IntandBoolareINTEGER,FloatisREAL, andStringisTEXT. AnOptionis the same column withoutNOT NULL, withNoneas NULL. Adistincttype is stored as its underlying type. A record, union, list or dict whose type derivesJsonis a JSONB column: written withjsonb(?), read withjson(..), decoded and checked against its refinements (a row it refuses isBadRow). A record field whose type neither is aKey[T]nor derivesJson, any other field type, andOption[Option[T]], is an error at thederive. - Keys. The key is written in the derive’s block:
derive Table for Item { key code }, orkey (shelf, slot)for a key of several columns (a tablePRIMARY KEY; such a key can’t yet be loaded or deleted through aKey). Without akey, the key is the fieldid; a table with neither is an error, and so is a key field that is anOption(a key is never NULL). The defaultidkey, when it is an integer, is SQLite’s row id:db.insertignores theidgiven (TodoId(0)by default) and returns the key the database gave. Any other key is inserted as given. - References. A field of type
Key[T](project: Key[Project], whereProjectderivesTablewith a key of one integer or text column) is a foreign key: its column isT’s key column withREFERENCES "project" ("id"), which SQLite enforces.Option[Key[T]]may be NULL.db.load(k)reads the row a key names, only when asked. - Checks. A refinement becomes a
CHECKwhere SQL can express it: comparisons,+,-,*,byte_len(it), numbers, strings,true,false,!,&&and||. For a refinement joined by&&, the parts SQL can express become checks (it != "" && !contains(it, "/")checks<> ''). A record’s own refinement (where it.start <= it.end) becomes a tableCHECK. ARealcolumn holds exact text (0.1,1/3), which SQL would compare as text, so noCHECKcompares it: reading checks it. A literal default becomes the column’sDEFAULT. - Reading back. Every row read is checked against the record’s refinements again. A row that SQL accepted but that breaks one is an error value,
BadRow { table, message }, never a fault.
from std.sqlite use Databasefrom std.orm use Table, Compare
type Todo = { id: TodoId = TodoId(0), title: String where it != "" && byte_len(it) <= 80, priority: Int where 1 <= it && it <= 5, done: Bool = false,}derive Table for Todo
mut db.create(Todo.table())! // CREATE TABLE IF NOT EXISTSk := (mut db.insert(Todo { title: "write docs", priority: 2 }))!found := db.load(k)! // Option[Todo]_ := (mut db.delete(k))! // whether it was thereopen := db.select( Todo.filter(Todo.done.eq(false).and(Todo.priority.le(3))) .order_by(Todo.priority) .limit(10),)!Queries are values: a Query[Todo], whose condition is a Cond[Todo].
- Start with
Todo.query()(every row) orTodo.filter(c), refine withfilter,order_by,order_by_descandlimit, and run withdb.select,db.first,db.countordb.delete_where. - A condition compares a column, named by its key path, with a value of the field’s type:
eqandne(SQL’sISandIS NOT, soTodo.due.eq(None)matches NULL), andlt,le,gtandge(an ordering againstNonematches no row). Conditions combine withand,either(oris a keyword) andnot. - The checker sees the types through, so a value of another type, a column of another table, or a field the record doesn’t have is an error.
- A
distinctkey compared in a query needsimpl Sql for TodoId(returningself.value.to_sql()).
See examples/todos_db.strata. Not supported: joins (a project’s to-dos as a query), referencing a key of several columns, migrations (a changed record against an existing table), transactions as a scope, LIKE and IN, blobs, and reading a record’s fields by column name.
Regular expressions (std.regex) are implemented in Strata. parse reads the syntax of Rust’s regex crate, with Unicode by default:
\d,\w,\sand.are Unicode-aware- Unicode properties:
\p{Greek},\p{gc=Lu},\p{age=6.0} - class operations
&&,--and~~, and[[:alpha:]] - named groups, the flags
imsUuRx, and the assertions\b,\b{start},\A,\z - no look-around or back-references
It produces the same tree as the regex-syntax crate: classes as ranges of code points with (?i) folded in, flags removed, and the same simplifications (ab|ac is a(?:b|c), a|b is [ab]). The case regex-curated checks the trees of curated patterns (tests/data/regex-curated.out). The Unicode data (std.unicode_tables) is generated by tools/unicode_tables.py from regex-syntax’s tables (Unicode 16.0.0).
from std.regex use parse, show
re := parse("(?i)ab|ac")!show(re) // cat(cls(41-41 61-61),cls(42-43 62-63))Capabilities in std. Each capability is a trait, so code that prints takes a Console, and its signature says so. The real implementations are capabilities the runtime creates, for main and for tests that name one; nothing else can create them. Other tests pass in-memory implementations (see examples/wc) or mock Console. Only std can call the native host_* operations underneath, and verify and fuzzing never run code that reaches them.