Skip to content

Primitives and state

A. Arda UZAN edited this page Nov 24, 2026 · 1 revision

As of v1.0.0

A primitive is a term that a host executes, unless it is declared a state primitive (see Effects outside the model). In a body that declares a primitive, every constructor is a primitive, is rewritten by a rule, or is declared fact : <constructor> (see Declaration-time checks), and rules rewrite every term that is neither a primitive nor a fact into primitive terms (see Rules and plans). The primitives are therefore all a host has to carry out: a host implements its primitives and nothing else. In a body that declares no primitive, every constructor is a fact and its does builds data, as in the Catalog (see Terms and counts).

A primitive also declares what it does to a model of the world, the predicted state. A plan therefore means a final state, and that state can be read and checked without running a host.

The state lines

A primitive is declared by its signature and by the lines that give its effect on the predicted state:

primitive : <constructor> : <class> ...
writes : <primitive> : <cell> = put <width>
writes : <primitive> : <cell> = put <width> <literal>
writes : <primitive> : <cell> = delete <width>
cell : <name> = shadow | first | join <table>
reads : <primitive> : <fact> = skip | reject
ensure : <constructor> : <name> = <predicate>
effect : <primitive> = external | state

A join cell also needs the lines of its table (see Join tables).

Line What it declares
primitive the signature: the constructor and the class of each argument
writes the cell the primitive writes, and its write: a put or a delete of declared width
cell the cell's law, shadow, first or join <table>, optionally followed by stacking
join one line of a join table
reads a fact the primitive reads, and what an absent fact does: skip or reject
ensure a named invariant attached to a term, evaluated over the predicted state
effect a primitive with no writes line (external) or with no host action (state)

A cell line names no primitive. A primitive reaches its cell through its writes line, and one cell can be written by several primitives: in the Mounts example, mask and bind both write the cell mounts. An ensure line names the constructor of a term, which need not be a primitive: Mounts attaches its invariant to share.

Cell names are global across the languages of a root. Two languages that declare a cell of the same name share one cell only when they agree on its law, on stacking, on its key width, on the class of each key component, and on its join table; a disagreement is rejected with E_DECLARATION of kind conflict. The classes of a key component agree when they are the same built-in class in both cells, or a declared class of one name whose expression is identical as written in both bodies. The state's keys stay bare: in the state that plan prints, a cell is listed under its name alone, as mounts, with no language prefix.

A body may not declare a primitive or a cell, nor a class or a term, under the name of a library function, of a built-in class (letter, canonical-path or relative-path) or of a class of the meta body, such as name, width or law; such a line is rejected with E_DECLARATION of kind duplicate, with the name as actual.

When the asked word is a primitive that a form builds, the answer to ? : <word> holds its own lines, every line of the body whose first name after the introducer is the primitive, such as its writes, reads, guard and effect lines, in declaration order, then its ensure lines. A cell line is not among them: it prints in the answer to the cell's name. A primitive that no form builds, such as mask or bind in Mounts, is answered by its primitive line, the file line when the body declares one, the is lines of the classes its signature names, each once, in signature order, then its own lines, its rule, ensure and count lines, its missing line, its default lines and last its note lines; a cell, such as mounts, is answered by its cell line, then the join lines of the table it names, with no file line (see Questions).

Signature

primitive : <constructor> : <class> ... declares a constructor and the class of each of its arguments, as a term line does (see Terms and counts), and makes that constructor a primitive: its terms are what a host executes, unless it is declared state.

A primitive with no arguments is written without a trailing colon, primitive : ready, as a term line with no arguments is written term : ready. A trailing colon, as in primitive : ready :, is rejected with E_DECLARATION of kind syntax.

Writes and widths

A cell is a named part of the predicted state, holding entries under keys. writes : <primitive> : <cell> = put <width> says that each term of the primitive puts one entry into that cell. The width is the number of leading arguments that form the entry's key; the remaining arguments are its value. A key of width greater than 1 is the tuple of those arguments. A stacking cell's key has width 1 or more, and its last component is a path (see Stacking cells); a cell that is not stacking may have a tuple key of any width, at least as wide as the template's placeholders when a program located by a template writes it. A put with no value arguments, whose term has no argument after its key, writes the entry masked. A writes line may also give a quoted literal after the width, writes : <primitive> : <cell> = put <width> <literal>, as in writes : deny-access : denied = put 1 "denied": the literal is a value appended after the primitive's remaining arguments, and it stands in the state entry, not in the primitive's term. A cell's value is the put's values as written, zero or more, of no fixed arity: in one cell, a mask may put none, another primitive one and a bind two. The class of each value is the writing primitive's signature at that position, or the literal itself, and a literal value is admitted as written. Only a join cell fixes one value argument, over its table's domain (see Join tables).

A cell's key width is fixed by the first writes line that puts to it. Every other writes line to that cell must match it: a put with the same width, a delete with a width k from 0 to the key width. A mismatch is rejected with E_DECLARATION of kind conflict. Because cell names are global (see The state lines), the width is resolved when a root is composed over all its languages, from the first put writer in the declaration order of the languages, then in line order, never in tree order. A declaration file checked alone in which a cell has only delete writers passes check with the note N_CELL_WIDTH, at the cell's first writes line; a composed root in which a cell has no put writer at all is rejected at plan with E_DECLARATION of kind reference. No width is guessed for delete 0.

writes : <primitive> : <cell> = delete <width> says that each term of the primitive deletes instead. A delete of width k removes every entry whose first k key components equal the term's first k arguments. Components are compared whole, by equality, never by path ancestry inside a component: in a stacking cell keyed by one path, a delete of width 1 whose term holds /shares removes the entry at /shares and leaves the entry at /shares/research. delete 0 clears the whole cell when the cell is not partitioned, a stacking cell keyed by one path included; on a partitioned cell it is rejected with E_DECLARATION of kind partition, at the writes line whole, with the line as written as actual (see Stacking cells).

A program located by a file template writes only partitioned cells, stacking or not: every put or delete from it carries the template's placeholders, in template order, as the leading components of its key, so a language with a file line has no unpartitioned write (see Template placeholders in terms). With file : teams/<team>/shares.typify and writes : bind : mounts = put 1, a does or rule line that builds bind is rejected with E_DECLARATION of kind partition at that does or rule line whole, with the term as written as actual; when a write's width is smaller than the number of placeholders in the template, as writes : bind : mounts = put 1 is under file : teams/<team>/<kind>/shares.typify, the writes line itself is rejected at the writes line whole, with the line as actual. A template without placeholders supplies no component, so the writes of its language are unchanged.

Both Mounts primitives put with width 1, so the path alone is the key of the stacking cell mounts. For bind, the mode is the value; mask has no argument after the path, so a mask term writes the entry masked at its path.

The cell's law

cell : <name> = shadow | first | join <table> declares how the puts at one key combine:

  • shadow: a later put replaces the entry.
  • first: the first put stays.
  • join <table>: the puts are combined by the named join table.

Under shadow and first, the order of the puts at a key decides its entry. Two puts at different keys of a plain shadow cell commute. A loop over inherited emits in list order, nearest program first, so a shadow cell keeps the farthest program's value and a first cell the nearest: a consumer that wants the nearer program to win declares the cell first.

Stacking cells

A law may be followed by stacking, as in Mounts:

cell : mounts = shadow stacking

A stacking cell's key is a tuple of width 1 or more whose last component is a path, and a later put at an ancestor hides a descendant. Two puts at different keys of one partition therefore no longer commute when one key's path is an ancestor of the other's: their order decides whether the descendant is visible, which (visible <cell> <key> ...) reads (see Invariants). The reader then requires the class of the last component to be canonical: it must be the built-in class canonical-path, or a class the reader can show is a subset of it, and a stacking cell whose last key component has a class that is neither is rejected with E_KEY_CANONICAL. canonical-path matches an absolute path whose components are non-empty, none of them . or .., with no trailing slash, over the allowed code points other than U+0020, the space, so a path with a space fails the class (see Classes). The root /, which has no components, is canonical; the condition of no trailing slash applies to every other path. parent of / is an error even so, E_LIBRARY_PARENT (see Library functions). Mounts binds its path class to the built-in itself, is : path = canonical-path.

The earlier components of a stacking cell's key, if any, partition the cell. Ancestry, masking and visibility are judged only among entries whose earlier components are equal, so one partition's mask never hides another partition's path. The earlier components of a partitioned key are all the placeholders of the file template of the program that holds the term, in template order (see Template placeholders in terms): each such component of a put or delete term is a placeholder of that template, and a put with fewer components, or any other value in those places, is rejected with E_DECLARATION of kind partition, reported when the declaration is read, on the does or rule line whole whose term supplies the components, with the term as written as actual. The rule is not limited to stacking cells: a program located by a template writes only partitioned cells, every put or delete from it carrying the template's placeholders as the leading components of its key, so a language with a file line has no unpartitioned write, and a template without placeholders supplies no component (see Writes and widths). For example, take a language with file : teams/<team>/shares.typify, primitive : bind : team path mode, writes : bind : mounts = put 2 and a form whose does line is does : bind <team> <path> <mode>: the team is the template's placeholder, so each team's program writes its own partition. The key of a bind term is its team and its path, and an invariant on such a bind, (visible mounts <1> <2>), reads within the partition of its first argument, the team, so one team's mask never hides another team's path. A delete compares the first k components whole (see Writes and widths), so delete 1 on such a cell clears one partition. delete k on a partitioned cell, with k the number of its partition components (on a stacking cell, all key components but the last), clears one partition. A delete on a partitioned cell names at least its partition components: delete k with k below their number, delete 0 included, is rejected with E_DECLARATION of kind partition, reported at the writes line whole, with the line as written as actual. The key width is fixed by the cell's first put writer, as for any cell, and two languages that declare a stacking cell of one name agree on it as The state lines require. In Mounts, the key of mounts is the path alone, of width 1.

Join tables

A cell whose law is join <table> combines the puts at one key by that table. A join cell has exactly one value argument, whose class is a class of literals; joins over tuples are not in v1. The table is declared by join lines, one line per unordered pair of distinct literals of the value class. A join line names its pair once and gives its result: join : modes : "ro" "rw" = "rw" says that "ro" joined with "rw" is "rw", and the reader derives the symmetric entry, "rw" joined with "ro". A table is therefore commutative by construction. A literal joined with itself need not be listed: the diagonal is implicit and idempotence holds by construction, so "ro" joined with "ro" is "ro". A listed diagonal pair whose result is its own literal, as in join : modes : "ro" "ro" = "ro", is legal and ignored; one whose result is another literal is rejected with E_JOIN_LAW (see below). The lines below declare a cell grants in which a path keeps rw over ro:

is : path = canonical-path
is : mode = "rw" | "ro"
join : modes : "ro" "rw" = "rw"
cell : grants = join modes
primitive : grant : path mode
writes : grant : grants = put 1

The one join line is the whole table: "ro" and "rw" form the only unordered pair of distinct literals, and "rw" "ro" would name that pair again. "ro" "ro" and "rw" "rw" are given by idempotence and need not be listed. A grant term's key is its path, of width 1, and its value is its mode, the one value argument. Two grants of one path, one ro and one rw, leave rw, in either order.

A table's domain is the literal class of the value argument of the cell that names it: in the lines above, the domain of modes is mode, the class of the value argument of grants. Two cells that name one table must share that class; otherwise the later cell line, in position order within the declaration file, is rejected with E_DECLARATION of kind conflict.

At declaration time the reader checks the table, and rejects a table that fails a law with E_JOIN_LAW, whose data names the table as cell, the law as expected and the values involved as actual. The laws are five:

  • complete: the table lists every pair of distinct literals of its domain; actual is a missing pair.
  • once: no pair is listed twice with different results; actual is the pair of the second listing.
  • associative: the table is associative; actual is a triple of literals over which it is not.
  • idempotent: no listed diagonal pair, a literal joined with itself, has another literal as its result; actual is the diagonal's own literal, not the result.
  • closed: every literal that a pair or a result names lies in the domain; actual is a literal outside it.

actual is decoded, as a quoted literal is read in a term (see How a term is written): a literal, or a pair or a triple as its decoded literals separated by one space, never the quoted lexeme. An authored pair keeps its authored order: the pair of join : modes : "ro" "rw" = "rw" is the actual ro rw. Where nothing authored the pair or the triple, a missing pair for complete or a triple for associative, actual lists its literals in the order of the value class's literals. For a table that no cell names, an unauthored triple lists its literals in the order of their first appearance in the table's join lines, each line read left to right, its result included. A table that no cell names is checked for once, associative and idempotent over the literals it lists, and for complete and closed only once a cell names it. A join table is named by its own join lines, and a defect of its pairs or laws is the table's: E_JOIN_LAW is anchored at the table's first join line, except that a pair listed twice with different results is anchored at the second listing, and it is reported once, whether two cells use the table or none does. A cell line that names an undeclared table is E_DECLARATION of kind reference. With commutativity and idempotence by construction and associativity checked, the joined entry depends neither on the order of the puts nor on how often one is repeated, which is what makes a join cell a keyed merge (see The eight constructs).

Facts it reads

reads : <primitive> : <fact> = skip | reject names a fact the primitive reads and says what an absent fact does: with skip the term is skipped, and with reject the absence is an error, E_FACT_REJECT, and the plan's fold stops at the rejected read (see Invariants, and typify plan DECLARATIONS ROOT for what plan prints). Named bare, the fact is read with the primitive's key, the first <width> arguments of its writes line, and, when that line puts to a partitioned cell (see Writes and widths), with the key components after the partition: a fact of one argument takes one such component, and a key with more is E_DECLARATION of kind arity on that line, which must then be a parenthesized call; a parenthesized fact call supplies its own arguments. A primitive that reads no fact has no reads line, and neither Mounts primitive has one. A line Mounts could add:

reads : bind : exists = reject

With it, the term bind /shares/research rw reads the fact exists of /shares/research, its key under put 1, and is an error when that fact is absent. The facts are six: the collector's five of the facts file, and clean, which the reader derives from it; a reads line that names another is rejected with E_DECLARATION of kind reference.

plan records each skip in a skips member of its JSON. Over a root, skips stands in each program's entry only, as recorded does, and never at the top level; the plan of one program carries skips at its top level, which is that program's own. Each skip is one object, {"constructor": ..., "fact": ..., "args": [...], "reason": "if" | "read" | "guard", "origin": {"kind": ..., "path": ..., "line": ...}}. The reason is read for a skip by a reads line, if for an optional term (see Optional term) and guard for a program guard (see Program guard). For a false (if ...), constructor is the constructor of the suppressed term, never that of the rule that carries the (if ...). args is always present, and empty when the fact took no arguments. A lookup that crosses a guarded program adds to the reading program's skips an entry of another shape, {"reason": "guard", "fact": ..., "args": [...], "program": ...}: its fact and args are the guarded program's guard fact and that fact's evaluated arguments, and program is the guarded program's portable path (see Context lookup).

Invariants

ensure : <constructor> : <name> = <predicate> declares a named invariant attached to the terms of a constructor, whether the constructor is rewritten, a primitive or a fact. The predicate's variables are bound by position to the term's arguments: <1> is its first argument, and so on to <n>. In a language with a file line, an ensure line may also use <name> of a template placeholder as a capture of that placeholder's class, as a does line and a rule's right side may (see Template placeholders in terms); a writes line, whose arguments are positional, takes none. The reader evaluates the invariant once per term that carries it, against the final predicted state after the whole plan. Mounts declares one:

ensure : share : child-visible = (visible mounts <1>)

Here mounts names the cell the predicate reads, and <1> is the first argument of share, the path. For the program line share : /shares/research, the invariant reads (visible mounts "/shares/research"). The built-in predicates name their cell, then every component of the key as a separate argument; there is no list-valued key expression in v1. The key of mounts has one component, so (visible mounts <1>) takes one key argument; for a cell whose key has width 2, a predicate takes two key arguments, as in (bound <cell> <1> <2>). In the list below, <key> ... stands for the components of the key:

  • (bound <cell> <key> ...): the latest put at the key in the cell has a value.
  • (masked <cell> <key> ...): the latest put at the key in the cell has no value, a put that wrote the entry masked.
  • (visible <cell> <key> ...): the key is bound in the cell, and no later put was made at a stacking ancestor in the same partition, an entry whose earlier key components equal the key's (see Stacking cells).
  • (absent <cell> <key> ...): the key has no entry in the cell.
  • (covered <cell> <key> ...), for a stacking cell: some proper ancestor of the key's last component, in the key's partition, satisfies visible.
  • (uncovered <cell> <key> ...), for a stacking cell: the negation of covered.

These six are the state predicates. They are usable in an ensure line only, where binary conjunction (and P Q) may also join state or library predicates (evaluated left to right, Q only when P holds; on E_ENSURE, data.key comes from the last state predicate evaluated, or [] when only library predicates were evaluated; see docs/plan-wire.md), and never in (if ...), which takes a fact call or one of the library predicates prefix, suffix and equal (see Optional term). The state predicates observe only the final predicted state, after the whole plan; a conditional over the state is deferred, and is not in v1. A state predicate inside (if ...), and covered or uncovered on a cell that is not stacking, are rejected with E_DECLARATION of kind syntax, on the rule or ensure line whole.

Every evaluated ensure produces one entry in the ensure array of plan's JSON, per program and at the root: {"ensure": <name>, "ok": true | false, "origin": {"kind": ..., "path": ..., "line": ...}}. A plan whose predicted state breaks an invariant is rejected: the invariant's entry has "ok": false, and the failure also raises E_ENSURE, whose data holds ensure, the invariant's name, and key, the key as an array of decoded strings in key order, as a state entry's key is, a one-element array for a key of width one and the empty array [] for an empty key, never a bare string, and which carries the origins of the terms it joins (see Diagnostics). The rejected plan is still printed whole: plan --json prints its arrays whole, ensure holding every evaluated entry; its diagnostics holds one E_ENSURE per false entry and any other diagnostic, its status is failed, and it exits with status 1. A complete expansion can fail only at the fold, with E_FACT_REJECT, or after it, with E_ENSURE. On E_FACT_REJECT the fold stops at the rejected read. state is {} and ensure is []: every invariant is evaluated against the final predicted state after the whole fold, which does not exist. skips holds the skips decided before the rejection, and plan and origins hold the expanded plan as a successful plan would print it up to the rejected read, the rejected term itself, and every term after the rejected read as expanded, undecided; a term skipped before the rejected read is in skips and out of plan, origins, recorded and modelled. recorded and modelled describe the expanded plan, whether or not the fold reached a term: recorded holds the terms of state primitives in plan order, before and after the rejected read, all but those skipped before it, and modelled counts every term of plan, the undecided ones included. status is failed, and the exit status is 1 (see typify plan DECLARATIONS ROOT). E_CONTEXT, E_LIBRARY_DOMAIN, E_LIBRARY_PARENT, E_RULE_FUEL and every other diagnostic raised while a plan is expanded or normalized are failures within expansion: lookups, inherited lists and library calls are evaluated as a rule's right side is rewritten. Such a failure prints the object of a plan where nothing was planned: plan, origins, recorded and skips empty, state {}, over a root programs [], modelled "0 of 0", status failed and its computed bound; plan exits with status 1. The failing term is named by the diagnostic's range and data alone, never by the arrays, and nothing of a partial rewrite is printed. A rule cycle is not a failure within expansion: E_RULE_CYCLE is found when the declaration is read, and a rewrite that runs away ends as E_RULE_FUEL. A check over constants is an ensure line too.

covered and uncovered let an invariant refuse a nesting. A state predicate reads the cell it names and nothing else, so a check about one kind of entry reads a stacking cell that only that kind writes, keyed by the resolved path. The unpartitioned illustrations of this section, granted, protected, protected-above and denied, suppose that the programs writing them belong to languages without a file line or whose template holds no placeholder; a program located by a template with placeholders writes every cell partitioned by them (see Writes and widths). Take a stacking cell granted that only a primitive grant writes, with the invariant attached to that primitive:

cell : granted = shadow stacking
primitive : grant : path mode
writes : grant : granted = put 1
ensure : grant : not-nested = (uncovered granted <1>)

With these lines, a grant term whose path lies under another grant's path, visible in the final state, fails not-nested; no other kind of entry is in granted to cover it. In a language whose file template has a <team> placeholder, and which declares primitive : grant : team path mode with writes : grant : granted = put 2, so that granted is partitioned by team as in Stacking cells, the invariant is ensure : grant : not-nested = (uncovered granted <team> <2>): the template placeholder <team> stands in the ensure line as a capture, and <2> is the path.

A grant below a protected path, at one, or above one is refused through two cells kept for the purpose, which the root's declarations write, for instance from a program at the root. protected, a stacking cell, holds the protected paths. protected-above holds each protected path and each of its proper ancestors. Two state primitives, protect and protect-above, write the two cells; in the language of the program at the root, a term guard-root puts each protected path <p> in both cells and, with (each <a> in (ancestors <p>) ...), each of its ancestors in protected-above, under a fuel line for guard-root. For the unpartitioned grant : path mode above, two ensure lines refuse such a grant; the predicates read the final root state whoever wrote it:

cell : protected = first stacking
cell : protected-above = first
primitive : protect : path
effect : protect = state
writes : protect : protected = put 1 "protected"
primitive : protect-above : path
effect : protect-above = state
writes : protect-above : protected-above = put 1 "protected"
term : guard-root : path
fuel : guard-root = 8
rule : guard-root <p> -> protect <p> ; protect-above <p> ; (each <a> in (ancestors <p>) (protect-above <a>))
ensure : grant : not-under-protected = (uncovered protected <1>)
ensure : grant : not-over-protected = (absent protected-above <1>)

The term and rule lines of guard-root belong to the language of the program at the root, and the two ensure lines to the language that declares grant.

Both writers put a value, the literal "protected" (see Writes and widths): a put with no value writes the entry masked, which is not visible, and uncovered would then pass every grant. (uncovered protected <1>) fails when some proper ancestor of the grant's path is visible in protected: when the grant lies below a protected path. (absent protected-above <1>) fails when the grant's path has an entry in protected-above: when it is a protected path or an ancestor of one.

Membership in a set inherited from an enclosing program needs nothing new either. The terms of an enclosing program can write a cell, as writes : deny-access : denied = put 1 "denied" does for the terms of deny-access, a primitive declared effect : deny-access = state (see Effects outside the model), and an ensure on a term of the program itself reads it: (bound denied <1>) is evaluated against the final root state, after the whole fold, whoever wrote it, and holds when the latest put at the term's first argument has a value. The literal "denied" is that value (see Writes and widths): a put with no value writes the entry masked, at which bound fails, and (masked denied <1>) is the read for a valueless put. On the rule side, the enclosing programs' terms are read with inherited.

Effects outside the model

A primitive may lack a writes line only when it is declared external, and a primitive that has its writes and no host action is declared state:

effect : <primitive> = external | state

A primitive without a writes line that is not declared external, one declared effect : <primitive> = state included, is refused by check with E_DECLARATION of kind effect. An external primitive is not an error for plan, which counts it unmodelled: a predicted state cannot vouch for a primitive it does not model. In the plan, a modelled term is an array, and a term of an external primitive is the object {"term": [...], "unchecked": true}. The top level of plan's JSON carries "modelled": "k of n", how many primitives are modelled, and "status", which is "failed" when a diagnostic of error severity exists, and otherwise "ok" when k equals n and "unchecked" when k is less than n; in text mode, plan prints the status on its first line. plan exits 0 unless a diagnostic of error severity exists (see Modelled and unchecked). In Mounts, both primitives are modelled. The meta body declares its kernel primitives external, so the reader treats each as external, and plan over the meta body is refused with E_USAGE.

A state primitive, declared effect : <primitive> = state, writes the predicted state through its writes lines, and no host executes its terms. plan --json lists them apart from the executed terms, never in plan, under the member recorded, an array of {"term": [...], "origin": {"kind": ..., "path": ..., "line": ...}} objects in plan order. The top-level origins refers to the executed terms of plan only, each state entry carrying its own origin (see The predicted state), and modelled counts the executed terms only. In text mode, plan prints a recorded term prefixed by its program's path and the word recorded.

The predicted state

Beside the ordered primitive terms, plan prints the predicted final state: the entries of every cell once the plan's writes are applied in the order its primitives run, each under its cell's law. Over a root there is one predicted state: the root's plan is one list sorted by stage, stable in tree order and then term order, and the final state is the fold over that list (see typify plan). Every entry carries the origin of the term that wrote it, as one object, {"kind": "program" | "default", "path": ..., "line": ...}; the next paragraph gives the JSON. With --json, plan also gives, as the member bound, the bound on the folded events, the terms of plan and recorded together, c_D times the number of terms the program holds; the state's size is at most k times the sum of the lengths of plan and recorded (see Termination and the bound).

In the JSON that plan prints, the state member holds one list per cell, under the cell's name, with the cell's entries in key order. Key order is the order of the decoded key tuples, component by component: components compare as strings, by code point, the earliest differing component decides, and a tuple that is a proper prefix of another comes first. The JSON serialization never decides the order, so ["/a"] precedes ["/a/child"]. Each entry carries its key, the arguments that form the key; its value, the put's values as written, zero or more: the remaining arguments, then the literal of a writes line that gives one; masked, true or false; its origin, one object: {"kind": "program", "path": ..., "line": ...} names the program file and the line whose term wrote the entry, and kind default names the declaration file and the line of the default line that supplied it; and, on a stacking cell, sequence, the zero-based position of the write that established the entry (entries of non-stacking cells carry no sequence). The entry masked has an empty value and masked true; an entry written by a put with a value has masked false. The flag is that of the entry's own put: an entry that a later put at a stacking ancestor hides keeps its own masked, since that hiding is visibility, which (visible <cell> <key> ...) judges when an invariant is evaluated and which is serialized nowhere: a state entry has no visibility field. The Mounts example shows one.

The predicted state exists because a list of primitive terms says what a host will do, not what the result will be. Two plans that hold the same terms in a different order can mean different things. Under shadow, two puts at one key leave the entry of whichever comes later. In a stacking cell, the order of a put at a key and a put at its ancestor in the same partition decides whether the key is visible, which (visible <cell> <key>) reads and an ensure line can require. In The order the rules cannot see, the reversed order shows in the ensure result: child-visible fails. With the predicted state, a plan has a meaning of its own without running a host: it can be read before anything runs, checked by invariants, and compared with the state a host observes afterwards.

Facts and the facts file

Typify has no host predicates: a class never calls into a host (see Classes). Facts carry what a host tested. The filesystem's truth enters plan as data, in a facts file, and a plan is a pure function of the declarations and the facts: given the same declarations, programs and facts, plan prints the same plan and the same predicted state.

typify facts ROOT writes the facts file:

typify facts ROOT
{"collector": "typify 1.0", "root": "...", "facts": [{"fact": "exists", "args": ["/shares/research"]}, {"fact": "mode", "args": ["/shares/research"], "value": "0700"}]}

Each entry names a fact and its arguments, and a fact with a value carries it as value, as the mode fact does. A collected fact the file does not hold is false. The facts of v1 are six: the collector's five, exists, directory, symlink, mode and owner, and clean, which the reader derives from the facts file, as described below. A fact carries a boolean or a text value: exists and directory are booleans, true when the file holds the entry, and symlink, mode and owner carry their text as value. No fact carries a list in v1, and each iterates only the lists that ancestors, split and inherited return (see For-each under a fuel cap).

The collector writes every path as a canonical absolute path, in root and in every argument. It omits an entry whose path is not a canonical-path, a space in a component included, as for an entry named team notes, and writes "omitted": n beside facts, n counting each omitted entry once, the descendants of an omitted directory included, since the traversal is not pruned, and never once per fact, when n is positive, and no omitted member otherwise. A fact over an omitted entry is absent, and clean does not see below it. omitted is part of the facts wire: when present it is a positive integer, checked as the rest of the file is, and when absent it is zero. The collector writes a mode as a string of four octal digits, as "0700" above, and an owner as the decimal uid, in text. It never follows a symbolic link: it reads every entry as lstat does, and a symlink fact carries the link's target text as its value. exists holds for every entry that exists and is not omitted, a dangling link included. directory holds only for a real directory, never for a link whose target is one. mode and owner are the entry's own metadata, a link's own included. The collector includes the root itself among the paths it reports. The sample above is shortened: it elides the root's path as "..." and leaves out facts the collector writes, among them the root's own facts and the other facts of /shares/research, such as its owner.

Canonical is lexical. root and every fact argument are canonical-path strings, and nothing resolves a symbolic link, neither the collector, which reads entries as lstat does, nor the reader; an ancestor of the root that is a link elsewhere on the machine concerns neither. A relative root or file given on the command line is joined to the current directory as the operating system reports it, never to the PWD environment variable; the reader then normalizes a root lexically, and no symbolic link in the joined path is resolved. Wherever the collector runs (typify facts ROOT, or tree or plan over a root that needs a fact), a root that is not a canonical-path after that normalization is E_USAGE, with the root as supplied as actual (nothing is stripped, and no other root is invented), and a symbolic-link root is E_FILE. A facts file given with --facts whose collector string, root, fact names, arguments, modes or owners do not follow this wire is refused with E_FILE, which carries that file's path. When facts, plan or tree runs the collector, an E_FILE from that run carries the path of the root or entry that could not be read.

clean is the one derived fact. (clean <path> <name>) holds when <path> belongs to the observed facts root and no entry of the facts file at depth two or more below <path>, for which directory or symlink holds, has <name> as its last component. A root's own immediate marker, the entry <name> directly under <path>, is not a nested one, and a regular file of that name is ignored. An ancestor marker is declared separately, over ancestors. The reader derives it from the facts file: no collector writes it, and the wire above is unchanged. It is usable wherever a fact call is: in a guard, in a reads line with reject or skip, and in (if ...). For example, guard : share = (clean <1> ".git") plans a program holding a share only when no directory or symbolic link named .git lies two or more levels below the shared path: the shared path's own .git is not a nested marker, and a regular file named .git is ignored. No general query over the facts exists in v1.

A guard : <constructor> = <fact> line reads a fact too (see Program guard). Named bare, its fact takes the first argument of the constructor's own term as written when the constructor is rewritten, the key arguments of its first writes line when it is a primitive, or, when that line puts to a partitioned cell, the key components after the partition: a fact of one argument takes one such component, and a key with more is E_DECLARATION of kind arity on that line, which must then be a parenthesized call, and its first argument when it is a fact constructor; a parenthesized fact call supplies its own arguments. A guard on a rewritten constructor is evaluated before any rule expansion, so no context, fuel or library error comes before a false guard. A reads or guard line that names a fact other than these six is rejected with E_DECLARATION of kind reference.

A plan that needs a fact reads a facts file given with --facts:

typify plan DECLARATIONS ROOT --facts FILE

Without --facts, a plan over a root that needs a fact runs the collector itself and carries the file's hash as the facts member of plan --json, the digest of the collector's output exactly as typify facts ROOT writes it. The hash is the string sha256: followed by sixty-four lowercase hexadecimal digits, and it names the bytes read: whenever the declaration reads a fact and a facts document was read and accepted, plan prints it in every outcome, a status of failed included, and it prints none in its place only when the declaration reads no fact or when no facts document was read and accepted. A facts file that cannot be read, or that does not follow the facts wire, is E_FILE, and a file that does not follow the wire gives none although its bytes were read.

A declaration reads a fact when it holds a guard line, a reads line, or a fact call inside (if ...) in a rule. tree needs facts for the first two only, and plan for all three. When the declarations carry a guard or a reads line, tree reads facts too: it takes --facts FILE, as in typify tree DECLARATIONS ROOT --facts FILE, or runs the collector as plan does (see Trees). A tree or plan that needs no fact accepts a --facts FILE and ignores it, without opening the file, and nothing in it is validated. One over a root that needs a fact and gets no --facts runs the collector. The one-program plan, typify plan DECLARATIONS LANGUAGE PROGRAM [--facts FILE], has no root to collect from: when the declaration reads a fact and no --facts is given, it is E_USAGE, with --facts as actual.

The collector is gated by a second, independent collector, a shell script under test/, which writes the same wire and omits the same entries, with the same omitted count. The gate compares the two files as canonical JSON and requires zero disagreements.

Example: Mounts

#lang typify

declare : Mounts
version : 1
is : path = canonical-path
is : mode = "rw" | "ro"
cell : mounts = shadow stacking
primitive : mask : path
writes : mask : mounts = put 1
primitive : bind : path mode
writes : bind : mounts = put 1
term : share : path mode
rule : share <p> <m> -> mask (parent <p>) ; bind <p> <m>
ensure : share : child-visible = (visible mounts <1>)
form : share : <path>
does : share <path> rw
example : share : /shares/research
form : share-read : <path>
does : share <path> ro
example : share-read : /shares/archive
end!

The language has two primitives, mask and bind. Both write the stacking cell mounts, whose law is shadow, with a put of width 1, and neither reads a fact. The path class is the built-in canonical-path, as the last key component of a stacking cell requires. The term share, which both forms build, is rewritten into primitive terms by its rule (see Rules and plans), and the invariant child-visible is attached to it: (visible mounts <1>) reads the cell mounts at <1>, the first argument of share, its path.

The program shares.typify holds one line:

share : /shares/research

Mounts has no file line, so it is planned one program at a time:

typify plan mounts.typify Mounts shares.typify --json

Its plan member is:

[["mask", "/shares"], ["bind", "/shares/research", "rw"]]

and its modelled member is "2 of 2", with the status member "ok". Its state member is the predicted state, one list per cell, in key order:

{"mounts": [{"key": ["/shares"], "value": [], "masked": true, "sequence": 0, "origin": {"kind": "program", "path": "shares.typify", "line": 1}}, {"key": ["/shares/research"], "value": ["rw"], "masked": false, "sequence": 1, "origin": {"kind": "program", "path": "shares.typify", "line": 1}}]}

In the stacking cell mounts, the term mask /shares has no value arguments, so it writes the entry masked at /shares: its value is empty, masked is true, and (masked mounts "/shares") holds. The term bind /shares/research rw puts the value rw at /shares/research: its masked is false, and (bound mounts "/shares/research") holds. Both entries have their origin at line 1 of shares.typify. (visible mounts "/shares/research") holds because the put at the ancestor /shares came before the put at the child, so child-visible passes. Its ensure member holds the one evaluated invariant:

[{"ensure": "child-visible", "ok": true, "origin": {"kind": "program", "path": "shares.typify", "line": 1}}]

The order the rules cannot see

Suppose the rule were written the other way round, binding first and masking the parent second:

rule : share <p> <m> -> bind <p> <m> ; mask (parent <p>)

Every check on rules passes it: its left side is variable-only, its right side uses only primitives and a library function, and share has one rule. Its plan for the same program holds the same two terms in the other order:

[["bind", "/shares/research", "rw"], ["mask", "/shares"]]

That order puts at the ancestor /shares after the put at the child, which makes /shares/research not visible in the stacking cell, while its entry keeps masked false, the flag of its own put. The rules alone accept it, and the invariant child-visible rejects it: its entry in the ensure member has "ok": false, and plan raises E_ENSURE. This is also why plans compare as ordered argument lists, never as sets (see Comparing plans).

Writing a consumer host

A consumer host is the program that executes plans. It is a loop over primitives: it reads a plan as JSON and executes each primitive term in order. No form, rule or other term reaches it. In the plan, a modelled term is an array, and a term of an external primitive is the object {"term": [...], "unchecked": true}, whose term member holds the array the host executes (see Effects outside the model). The terms of a state primitive are not in plan: they stand under recorded, apart from the executed terms, and the host executes the terms of plan only.

The only checks a consumer host keeps are about the filesystem's truth, such as existence, symbolic links, modes and ownership, and it keeps them as preconditions of its primitives. Everything else a plan does is declared, in rules and state lines, where it can be read, asked about with ? : <word> and checked before anything runs. No primitive reads a program's terms, and no validator stands beside the plan: a consumer's refusals are the reader's judgments.

A check across the lines of one program, a fact beyond the six (see Facts and the facts file), and a mount whose path the host supplies are not in the model of v1. Each is a host precondition, or a primitive declared effect : <primitive> = external, and stands in the consumer's declaration as a note, note : <constructor> = <text>, attached to a constructor, <text> being any run of allowed code points; ? : <word> prints the constructor's note lines last (see Questions).

A plan is never applied onto a half-built state (see Applying a plan).

Clone this wiki locally