Skip to content

Rules and plans

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

As of v1.0.0

A rule rewrites one term into a sequence of terms. The forms of a language build terms (see Terms and counts); rules rewrite those terms, step by step, into primitive terms, which a host executes (see Primitives and state). typify plan applies the rules to every program and prints the result with its predicted state.

The rule line

rule : <constructor> <var> ... -> <term> ; <term> ...

The left side, before ->, is a constructor followed by variables. Each variable names the argument in its position. The right side is a sequence of one or more terms separated by ;, the only separator. Each term is a constructor followed by its arguments, and an argument is a variable, a literal, either a bare word or a quoted literal (see How a term is written), or a library call in parentheses, whose string result is checked against the class of the position it fills before the enclosing term is built. In a language with a file line, an argument may also be <name> of a placeholder of the language's template, a capture of that placeholder's class, as in a does line or an ensure line of the language (see Template placeholders in terms); a rule variable named like a template placeholder is refused with E_DECLARATION of kind duplicate, as a form placeholder of that name is, with the placeholder as written, angle brackets included, as actual. A nested term of a user constructor is rejected with E_TERM_NESTED when the declaration is read: terms are first-order in 1.0. A right side may also use three of the eight constructs: (context <constructor>), (if <fact-call> (<term>)) and (each <var> in <list> (<term>)); the inner term of (if ...) and (each ...) is written in parentheses, as in rule : try-bind <p> -> (if (exists <p>) (bind <p> ro)) and rule : batch <p> -> (each <x> in (split <p>) (reg-item <x>)).

The rule of the Mounts language:

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

It rewrites a share term with the path <p> and the mode <m> into two primitive terms: a mask of the path's parent, then a bind of the path with its mode. The whole declaration is below.

Declaration-time checks

The reader checks every constructor and every rule when it reads the declaration, before any program runs:

  • Every constructor is a fact or an effect. 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). In a body that declares a primitive, every constructor is a primitive, is rewritten by a rule, or is declared fact : <constructor>; any other constructor is rejected with E_CONSTRUCTOR_UNBOUND and a column. The meta language declares primitives, so this check binds it whole.
  • Variable-only left sides. A left side holds its constructor and variables only, never a literal, bare or quoted, and never a nested term, so a rule matches every term of its constructor. A left side with the wrong number of variables for its constructor's signature is rejected with E_DECLARATION, its data.kind being arity: the range covers the rule line whole, and actual is the left side as written.
  • One rule per constructor. The first matching rule wins, so a second rule for the same constructor could never apply. The reader rejects it with E_RULE_DUPLICATE.
  • Stratification by declaration order. A right side may use only primitives, library functions, and constructors declared earlier in the body than the constructor on its left side. A right side that breaks this is rejected with E_DECLARATION, its data.kind being stratification.
  • No cycles and no duplicates. Constructors whose rules reach one another form a cycle, rejected once with E_RULE_CYCLE, at the earliest rule among them in position order: the range covers that rule's left-side term, and constructor is its constructor. A rule on the cycle whose right side names a constructor declared later is also rejected for stratification, in a diagnostic of its own. A duplicate rule is rejected with E_RULE_DUPLICATE, with a column (see Diagnostics).

The range of E_CONSTRUCTOR_UNBOUND, E_RULE_DUPLICATE and E_RULE_CYCLE, and of E_TERM_NESTED and E_TERM_CLASS, covers the term's own span within its does or rule line, never the line from its start. The range of E_DECLARATION of kind stratification covers the rule line whole.

There are no conditional rules; they are deferred to version 1.1. A rule always matches every term of its constructor, and (if <fact-call> <term>) decides only whether one term of its right side is emitted.

In Mounts, mask and bind are primitives and share is rewritten by its rule, so Mounts needs no fact line. The rule for share uses the two primitives and the library function parent.

Library functions

Nine functions are declared once, in the meta language, and their names are taken: a body that declares a class, term, primitive or cell under the name of a library function is rejected with E_DECLARATION of kind duplicate, with the name as actual. They are pure: each but inherited is defined over strings on the domain the table gives, and a call outside that domain is refused; inherited reads the terms of the enclosing programs, which the reader has already read. None reads a file, starts a process or reaches the network, so the reader stays a function of its inputs.

Signature Result Domain
parent p p without its last component; an error when p is / p is a canonical-path
join a b a, one slash, b; /b when a is / a is a canonical-path; b is one or more components joined by single slashes, none empty, . or .., and none holding U+0020
split p the list of the components of p; the empty list when p is / p is a canonical-path
ancestors p the list of the proper ancestors of p, from / to parent p; the empty list when p is / p is a canonical-path
inherited c the list of the first arguments of every term of constructor c in the program's enclosing programs, nearest program first and in line order within a program; the empty list when there is none c is a declared constructor of one argument; otherwise E_DECLARATION, of kind reference or arity
concat a b the string ab: a followed by b any allowed strings
prefix a b true when b begins with a any allowed strings
suffix a b true when b ends with a any allowed strings
equal a b true when b is a any allowed strings

parent, split and ancestors take a canonical-path (see Classes), the root / included, and join takes one as its first argument; any other string in those places is refused with the code E_LIBRARY_DOMAIN. join's second argument is a relative path: one or more components joined by single slashes, with no leading, trailing or repeated slash, no empty component, none . or .., and none holding U+0020, the space, as in canonical-path and relative-path; any other second argument is refused, an absolute one included. So a relative path joined under scope always yields a canonical path (see Scope). concat, prefix, suffix and equal take any allowed strings, the strings made of allowed code points (see Classes). inherited takes a declared constructor of one argument, named as Context lookup says; an undeclared one is rejected when the declaration is read, with E_DECLARATION of kind reference, and one of more than one argument with E_DECLARATION of kind arity. There are no other edge cases: outside the domain a call is an error, never a host default.

parent, join and concat return a string, and split, ancestors and inherited return a list. These six may be used on the right side of any rule, where a call is written in parentheses, as (parent <p>) or (inherited deny-exec). prefix, suffix and equal are predicates, usable only in (if ...) (see Optional term) and in an ensure line (see Invariants).

Some values that follow from the table: parent /shares/research is /shares, and parent /shares is /. join /shares research is /shares/research, and join /shares /research is refused, since its second argument has a leading slash. split /shares/research is the list shares, research. ancestors /shares/research is the list /, /shares. prefix /shares /shares/research is true. prefix compares strings and never stands for path containment, which is equal over ancestors, or covered on a stacking cell (see Invariants). split /shares/ is refused with the code E_LIBRARY_DOMAIN, since /shares/ is not canonical.

The root / is itself a path, and a canonical one (see Stacking cells); the error, E_LIBRARY_PARENT, belongs to parent of /, not to /.

Strings are sequences of Unicode code points and compare exactly, with no case folding and no normalisation. ancestors, split and inherited return lists, which a for-each consumes (see For-each under a fuel cap). The library function join is unrelated to the cell law join <table> and to the join lines of a table.

(inherited <constructor>) is the list of the first arguments of every term of that constructor in the program's enclosing programs (see Context lookup), nearest program first and in line order within a program; with none, it is the empty list, whose loop emits nothing. A constructor of more than one argument is refused under inherited, as under context, with E_DECLARATION of kind arity, never truncated to its first argument, and the constructor is named as Context lookup says. inherited is not a function over strings: it reads the terms of the enclosing programs, which the reader has already read, and it reads no file, starts no process and reaches no network. Its formation is checked whole when the declaration is read: the constructor it names exists, the expression's sort is a list under each and a string elsewhere, E_TERM_CLASS of kind sort otherwise, and the carrying constructor has its fuel cap when its rules carry an each (see For-each under a fuel cap); only its values are read when the located program is planned, against its enclosing programs. Its list is used on a rule's right side, written in parentheses, and as the list of a for-each, as in (each <x> in (inherited deny-exec) <term>) for a declared constructor deny-exec (see For-each under a fuel cap). An each over inherited emits in list order, nearest 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 (see The cell's law). (context <constructor>) is its first element (see Context lookup).

The eight constructs

Besides rewriting, a declaration has eight constructs. Three are written on a rule's right side; the others are lines beside a term, or are a cell's law, a write or an invariant of Primitives and state.

Stage

stage : <primitive> = <n>

Primitives run in ascending stage, and in plan order within a stage; the default stage is 0. In Mounts, these two lines would run every mask of a plan before every bind:

stage : mask = 0
stage : bind = 1

Over a root, the stage sort runs over the primitive terms of all the programs at once: the root plan is one list sorted by stage, stable in tree order (see Tree order) and then term order (see typify plan).

Context lookup

A program's enclosing programs are the programs whose located file lies in a proper ancestor directory of the program's own directory, nearest directory first; several programs in one enclosing directory are ordered by the declaration order of their languages, then by path. Programs in one directory are siblings and never enclose one another (see Enclosing programs). (context <constructor>) is the first element of (inherited <constructor>) (see Library functions): it searches the enclosing programs only, never the program itself or a sibling, and gives the first argument of the nearest enclosing program's first term of that constructor, in line order. When that list is empty, because no enclosing program has a term of that constructor, (context <constructor>) is an error, E_CONTEXT, and (inherited <constructor>) is the empty list, whose loop emits nothing. Both lookups take a constructor qualified by its language, Language.constructor. A bare name is resolved when the declaration is read, against the composition, the set of bodies of the declaration file, in the one-program forms as under a root: it is accepted when exactly one body of the composition declares it, and is otherwise refused with E_DECLARATION of kind reference, with the name as actual and the declaring bodies in related. Every command reads the declaration first and reports its diagnostics as Every error, in order says, so such a name is refused by tree, plan, ask and emit as by check. The formation of both lookups is checked whole when the declaration is read: the constructor they name exists, an undeclared one being E_DECLARATION of kind reference, and has one argument, a constructor of more than one argument being E_DECLARATION of kind arity, with the constructor as named as actual, never truncated to its first argument; the expression's sort is a list under each and a string elsewhere, E_TERM_CLASS of kind sort otherwise; and a constructor whose rules carry an each has its fuel cap, a context outside any each requiring none. Only their values are read when the located program is planned, against its enclosing programs. An example line builds its term, library calls included, and evaluates neither lookup (see Examples are run). A guarded program's terms are empty, so it is never an enclosing context: neither lookup finds a term in it, and whatever raw data of it is kept for audit is never searched (see Program guard); a lookup that crosses a guarded program adds to the reading program's skips the entry {"reason": "guard", "fact": ..., "args": [...], "program": ...}, fact and args being the guarded program's guard fact and that fact's evaluated arguments, and program the guarded program's portable path (see typify plan).

Keyed merge and reset

A keyed merge is a cell whose law is join <table>, and a reset is a write = delete <width> (see Primitives and state).

Optional term

(if <fact-call> <term>) emits the term only when the fact call holds; otherwise it emits nothing, and plan records the skip with its origin in the program's skips member, with the reason if and the suppressed term's constructor as constructor, never the constructor of the rule that carries the (if ...) (see typify plan). The fact is always written as a call in parentheses with its own arguments; a bare fact name is rejected with E_DECLARATION, its data.kind being syntax. The fact may be any of the six of 1.0, the derived clean included (see Program guard). Besides a fact call, (if ...) takes one of the library's predicates, prefix, suffix and equal (see Library functions), and never a state predicate such as visible, which is refused there with E_DECLARATION of kind syntax, on the rule line whole: the state predicates observe only the final predicted state and are usable in an ensure line only (see Invariants). A conditional over the state is deferred, and is not in 1.0.

Check over constants

A check over constants is an ensure line (see Invariants).

Program guard

guard : <constructor> = <fact>

A program holding a term of that constructor is planned only when the fact holds; otherwise the program is not planned, its terms are empty, tree reports it guarded (see Trees), and plan records the skip in the program's skips member, with the reason guard (see typify plan).

In a bare guard, where <fact> is a fact's name alone, the guard supplies the fact's arguments, which depend on the constructor:

  • on a primitive, the key arguments of its first writes line, as many leading arguments as that write's width, or, when that line writes a partitioned cell (see Stacking cells), 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;
  • on a constructor that a rule rewrites, the first argument of the constructor's own term as written, before any rule rewrites it;
  • on a fact constructor, its first argument.

A fact written as a call in parentheses supplies its own arguments. The guard is evaluated before any rule expansion, so no context error (E_CONTEXT), fuel error (E_RULE_FUEL) or library error comes before a false guard. The fact is one of the six of 1.0: the five the collector writes, exists, directory, symlink, mode and owner (see Facts and the facts file), and clean, which the reader derives from the facts file; a guard or a reads line naming any other is rejected with E_DECLARATION, its data.kind being reference.

(clean <path> <name>) holds when <path> belongs to the root the facts file observed 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 (it does not hold for a <path> outside that root). The entry of that name immediately below <path> is the path's own marker, not a nested one, and a regular file of that name is ignored; a marker in an ancestor of <path> is declared separately, over ancestors (see Library functions). No collector writes it, so it adds nothing to the facts file's wire, and it is usable wherever a fact call is: in a guard, in a reads line with reject or skip, and in (if ...). With guard : share = (clean <1> ".git"), a program holding a share is planned only when no directory or symbolic link named .git lies at depth two or more below the shared path; the .git immediately below the shared path is the share's own marker, not a nested one. No general query over the facts exists in 1.0.

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: when the declarations carry a guard or a reads line, tree takes --facts FILE or runs the collector as plan does (see Facts and the facts file); otherwise it needs no fact, and it accepts a --facts FILE and ignores it, without opening the file.

Scope

scope : <constructor> = program | parent | root

The scope names the program whose workdir term resolves the relative arguments of a term of that constructor; the default is program. It chooses only the workdir that joins a relative-path argument: a capture of a template placeholder reads the placeholders of the program that holds the term, whatever the term's scope (see Template placeholders in terms). A constructor's scope carries into every primitive its rules emit, until a constructor inside the expansion declares its own, and that effective scope resolves the primitives' relative arguments. The scope program names the program itself. The scope parent names the nearest enclosing program and requires one in a strict ancestor directory: a sibling in the program's own directory is never its parent (see Context lookup for the enclosing programs). The scope root names the shallowest program among the program itself and its enclosing programs, the one whose directory is nearest the root of the tree; its workdir term is used, and a shallowest program that holds none, an absent optional program included, is E_CONTEXT.

Resolution is declared on the receiving signature. An argument is resolved only when the signature of its primitive or term declares that position relative-path, the built-in class of relative paths (see Classes), or a class that is a bare reference to relative-path, directly or through further bare references. In a variant line primitive : bind : relative-path mode, the first position of bind is the one a scope resolves, and the second is not. The Mounts declaration below keeps primitive : bind : path mode, whose path is a canonical-path, so nothing in Mounts is resolved under a scope. A resolved argument is joined to the first argument of the workdir term of the program the scope names, and the joined value must then be a canonical-path. No other argument is resolved. A class written as a pattern, a literal alternation, a sequence or an except is an ordinary class, even when its members happen to be relative strings. A position of class canonical-path is never resolved: a relative capture that reaches it fails its class check, as any value outside its class does. A form's capture carries no role across a signature; only the class of the receiving position decides. A language with no workdir term admits no relative argument; a relative-path argument with no workdir term in scope is E_CONTEXT.

For-each under a fuel cap

fuel : <constructor> = <n>

(each <var> in <list> <term>), with a fuel line, emits the term once per element of the list; at most n elements are consumed, and more is an error, E_RULE_FUEL, which over a lookup carries in related the origin of the first term past the cap. The list comes from ancestors, split or inherited only: a fact carries a boolean or a text value, and no fact holds a list in 1.0. The inheritance loop, (each <x> in (inherited deny-exec) <term>), emits the term once for each first argument of a deny-exec term in the enclosing programs, nearest program first and in line order within a program, and nothing when there is none (see Library functions). Each each loop, a nested one and an inheritance loop included, is bounded on its own by the fuel cap of the constructor that carries it, and c_D takes every loop at its cap (see Termination and the bound).

Normalization

To normalize a term, the reader rewrites it by its constructor's rule and normalizes each term of the result, until only primitive terms remain. Library calls are evaluated as they are reached. The order is fixed: left to right, innermost first, and that term's first failure is reported.

The Mounts program share : /shares/research normalizes in four steps:

  1. The line matches the form share : <path>, whose does builds the term share /shares/research rw.
  2. The rule for share rewrites it into mask (parent /shares/research), then bind /shares/research rw.
  3. Leftmost first, and innermost first within it: (parent /shares/research) gives /shares, and mask /shares is a primitive term.
  4. bind /shares/research rw is a primitive term.

The result is the plan:

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

When the normalization of a term meets a failure, such as a call of parent on /, it reports that term's first failure in this order. A term that holds two failing calls therefore always reports the same one. This rule holds within one term only: otherwise the reader reports every error it can find, ordered by position (see Diagnostics).

The range of E_LIBRARY_DOMAIN, E_LIBRARY_PARENT and E_CONTEXT covers the program line whose term produced the failing step; a library code raised when checking an example covers the library call in the form's does line (see Examples are run). The range of E_RULE_FUEL covers the term's own span within its does or rule line.

Termination and the bound

Under stratification, variable-only left sides with one rule per constructor, a total library and declared fuel caps, every plan terminates. c_D bounds every folded event, executed and recorded alike: for a program of n terms, the terms of its plan and its recorded together number at most c_D times n, where c_D is a per-term constant the reader computes from the declaration with every for-each loop, nested ones included, at its declared fuel cap, and the state's size is at most k times the sum of the lengths of plan and recorded. Each rewrite replaces a term by terms of primitives, library functions or earlier constructors, so rewriting never returns to a constructor it has left, and the fuel caps bound every for-each.

The reader prints c_D with a checked program and the actual bound with each plan. check DECLARATIONS LANGUAGE PROGRAM --json carries c_D and k in its bound member, {"c": C, "k": K}, when the declaration has rules (check DECLARATIONS --json carries none, and check DECLARATIONS --bounds --json carries c and k in its bounds member). typify plan --json of one program carries the actual bound in its bound member, {"c": C, "n": N, "limit": L}: c_D, the number of terms the program holds (blank, comment and question lines build no term; a defaulted term counts), and c_D times that number. The bound is computed from the declaration and the input, and plan --json carries it as the member bound whenever both were read, a failed plan included, a failure within expansion among them; zeros are never printed in its place, and when the declaration has an error and nothing could be computed, bound is null, so the object keeps its shape. bound is a member of the JSON objects only: text mode prints no bound line, in either outcome.

The bound is relative to n. Where a constructor's count is *, the default, nothing bounds n, and nothing claims to (see Terms and counts).

typify plan

typify plan DECLARATIONS ROOT
typify plan DECLARATIONS ROOT --facts FILE
typify plan DECLARATIONS LANGUAGE PROGRAM
typify plan DECLARATIONS LANGUAGE PROGRAM --facts FILE

With a root, plan is tree followed by normalization. It walks ROOT as tree does, reads each program it finds against its language, and normalizes the terms of every program in the model, its defaulted terms included; an absent optional program holds only its defaults (see Terms and counts). A program whose guard fails is not planned.

Over a root, the plan is one list: the primitive terms of every planned program, taken in tree order (see Tree order) and, within a program, in term order, then sorted by stage (see Stage). The sort is stable, so terms of one stage keep tree order and then term order. The final fold that predicts the state follows that order, and every ensure is evaluated once per term that carries it, against the final root state after the whole fold. A primitive declared effect : <primitive> = state has its writes and no host action: plan lists its terms under recorded, in plan order, each with its origin, apart from the executed terms (see Modelled and unchecked).

A cell's key width is resolved when the root's languages are composed: over all of them, from the first writes line that puts to the cell, in the declaration order of the languages, then in line order, never in tree order. A root in which a cell has no put writer at all is refused at plan with E_DECLARATION, its data.kind being reference; no width is guessed for delete 0. A declaration file checked alone, in which a cell has only delete writers, passes check with the note N_CELL_WIDTH (see Writes and widths).

plan plans from facts (see Facts and the facts file). It needs a fact when the declarations hold a guard line, a reads line, or a fact call inside (if ...) in a rule. With --facts FILE, a plan that needs a fact reads that facts file; without --facts, a plan over a root that needs a fact runs the collector itself and reports the file's hash in the facts member of --json (text mode does not print it), the digest of the collector's output exactly as typify facts ROOT writes it, and a plan of one program that needs a fact is refused with E_USAGE, with --facts as actual, since there is no root to collect from. The hash is the string sha256: followed by sixty-four lowercase hexadecimal digits and names the bytes read: whenever plan needs a fact and a facts document was read and accepted, plan --json carries that hash in every outcome, status failed included, and carries none in its place only when it needed 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 the second leaves the hash none although its bytes were read. A plan that needs no fact accepts a --facts FILE and ignores it: the file is not opened and nothing in it is validated.

For a language without a file line, typify plan DECLARATIONS LANGUAGE PROGRAM [--facts FILE] plans one program, as check checks one; both one-program forms take --facts.

With --json, plan prints one object in every outcome, the plan object below except on a usage error (E_USAGE), which prints {"ok": false, "diagnostics": [...]} and exits with status 2 (see Command line). For one program, typify plan DECLARATIONS LANGUAGE PROGRAM --json, the object has these members:

  • plan: the ordered primitive terms; a modelled term is an array, its constructor then its arguments, and a term of an external primitive is the object {"term": [...], "unchecked": true};
  • recorded: the terms of state primitives, declared effect : <primitive> = state, apart from plan: an array of {"term": [...], "origin": {"kind": ..., "path": ..., "line": ...}} objects, in plan order;
  • state: the predicted final state, one list per cell, in key order;
  • bound: the actual bound, {"c": C, "n": N, "limit": L};
  • modelled: how many primitives are modelled, as "k of n", counted over the executed terms only;
  • status: "failed" when a diagnostic of error severity exists; otherwise "ok" when k equals n, and "unchecked" when k is less than n;
  • facts: the hash of the facts file, sha256: followed by sixty-four lowercase hexadecimal digits, naming the bytes read: whenever plan needed a fact and a facts document was read and accepted, it is that hash in every outcome, status failed included, and it is none only when plan needed no fact or when no facts document was read and accepted, a facts file that cannot be read or does not follow the facts wire being E_FILE;
  • origins: the origin of every executed term of plan, never of a recorded one; each state entry carries its own origin;
  • ensure: one entry per evaluated invariant, an object with the invariant's name as ensure, ok, true or false, and an origin object with kind, path and line; a failing invariant also raises E_ENSURE, whose data holds ensure and key, the key as an array of decoded strings in key order, the shape of a state entry's key;
  • skips: one entry per skip, an object with the constructor, the fact, the fact's args, always present and empty when the fact took no argument, the reason, if, read or guard, and an origin object with kind, path and line. The reason names what skipped: an optional term, a reads line (see Facts it reads) or a program guard. For a false (if ...), constructor is the suppressed term's constructor, never the constructor of the rule that carries the (if ...). A lookup that crosses a guarded program adds an entry of its own shape, {"reason": "guard", "fact": ..., "args": [...], "program": ...}, fact and args being the guarded program's guard fact and that fact's evaluated arguments, and program the guarded program's portable path (see Context lookup);
  • diagnostics: every diagnostic reported, an empty array when there is none.

Over a root, the object holds one plan per program and one state for the whole root:

  • programs: one entry per program, each with its path, its language, the language that located the program, as in a tree entry, its status in the tree (see Trees), its plan, the ordered primitive terms of that program, its recorded, the terms of its state primitives, apart from plan, in the shape of one program's recorded, its ensure, the entries of the invariants of its terms, each evaluated against the final root state, and its skips;
  • state: the final predicted state, after the fold over the stage-sorted merge of all the programs' plans;
  • bound, modelled, status, facts, origins, ensure and diagnostics, for the root as a whole, plus schedule (the stage-sorted operations across the root) and inputs (the observations made during the walk and evaluation; see docs/plan-wire.md and docs/inputs.md in the repository); this status is failed, ok or unchecked, as for one program. recorded and skips stand only in each program's entry, recorded beside its plan, and nowhere at the top level.

A failing invariant does not replace the object: plan --json prints its arrays whole, ensure holding every evaluated entry; its diagnostics holds one E_ENSURE for each entry whose ok is false 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_ENSURE the fold is complete, and state and the arrays are printed whole, as above. On E_FACT_REJECT the fold stops at the rejected read. state is {} and ensure is [], since every invariant is evaluated against the final state after the whole fold, which does not exist, and skips holds the skips decided before the rejection. plan and origins hold the expanded plan up to the rejected read as a successful plan would print it, the rejected term itself, and every term after the rejected read as expanded, undecided; a term skipped before the rejected read stays in skips and out of plan, origins, recorded and modelled. Such a plan's status is failed, and plan exits with status 1. So of two expanded terms, the first skipped by its reads line and the second rejected with E_FACT_REJECT, plan holds the rejected term alone, skips carries the first, origins and modelled count the rejected term alone, state is {} and ensure is []. 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. 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, and 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. When nothing was planned, because a declaration has an error, an input cannot be read, or the expansion fails, plan prints its object with status failed, its arrays empty, over a root programs included, state {}, modelled "0 of 0" and diagnostics holding the diagnostics.

In text mode, when the fold completed (E_ENSURE included), plan prints its status, failed, ok or unchecked, on its first line, then one primitive term per line, each line prefixed by the path of the term's program; a recorded term is prefixed by its program's path and the word recorded, and a term of an external primitive ends with the word unchecked. When nothing was planned (because a declaration has an error, an input cannot be read, the expansion fails, or E_FACT_REJECT stops the fold), standard output is empty and only the diagnostics' projections print on standard error.

Modelled and unchecked

A primitive whose writes line declares its effect on the predicted state is modelled. A primitive declared effect : <primitive> = state has its writes and no host action: its terms are listed under recorded, apart from the executed terms, and modelled counts the executed terms only. A primitive declared effect : <primitive> = external has no writes line: plan counts it unmodelled and prints it unchecked, never ok, and no plan that holds one reads green (see Effects outside the model).

Such a plan is not an error. With --json, each term of an external primitive is the object {"term": [...], "unchecked": true}, the member modelled gives "k of n" over the executed terms, and the member status, unless a diagnostic of error severity makes it "failed", is "unchecked" while k is less than n and "ok" when k equals n; in text mode the status is the first line. plan exits with status 0 unless a diagnostic of error severity exists.

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 program shares.typify holds the 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"]]

its modelled member is "2 of 2", and its status member is "ok". In the predicted state, in the stacking cell mounts, /shares maps to the entry masked, which a put with no value writes, and /shares/research to rw, each with origin line 1 of the program. The invariant child-visible is attached to share, and an ensure binds its variables by position to its constructor's arguments, <1> to <n>, whether the constructor is rewritten, as share is, primitive or a fact. Its <1> is therefore the first argument of share, the path, and (visible mounts <1>) asks whether that path is visible in the cell mounts. The rule masks the parent first and binds second, so (visible mounts "/shares/research") holds and child-visible passes: the ensure member holds one entry, for child-visible, whose ok is true. The reverse order puts at the ancestor after the child, which makes the child not visible: the rules alone accept it, and the invariant rejects it with E_ENSURE (see The order the rules cannot see).

Comparing plans

Plans compare as ordered argument lists, never as sets. Two plans are equivalent when they hold the same primitive terms, with the same arguments, in the same order. As sets, the Mounts plan and the plan with its two terms swapped are equal, yet the swapped order hides the bound child.

Applying a plan

A plan is total over its declaration. A build that stops part-way is discarded and rebuilt from nothing; a plan is never applied onto a half-built state.

Clone this wiki locally