Repository navigation
Trees
As of v1.0.0
Some languages keep their programs in a directory tree, at paths that follow a pattern. Such a language declares where its programs live, and typify tree reads the whole tree in one call and returns one model of it: one line per program, or, with --json, one JSON document.
On this page a tree is always a directory tree. A program's syntax tree is its list of terms, which Terms and counts describes.
file : <template> gives a path relative to the tree's root. A template may hold placeholders, and the class of each placeholder is an is line of the same body (see Classes). A body holds one file line: a second is refused with E_DECLARATION of kind duplicate, and so is a placeholder name repeated within one template.
A program file has no header. Its language is each language whose template locates it, so a file that two languages locate gives two entries, one per language, adjacent in tree order (see Tree order); a program checked or planned on its own has the language that the check or plan command names (see Command line). Only a language that declares file takes part in a tree; a program of a language without one is checked and planned one at a time (see From a tree to a plan below).
The Catalog of Terms and counts declares file : catalog.typify. Its template holds no placeholder, so it locates one program, catalog.typify at the root. The Catalog's declarations live in a file named languages.typify, not catalog.typify, so that tree never takes the declaration file for a program.
The placeholders of a language's file template are bound in every program the template locates. A does line, a rule's right side or an ensure line of that language may use <name> of a template placeholder as a capture of that placeholder's class; a writes line, which is positional, takes none. A form placeholder of the same name is refused with E_DECLARATION of kind duplicate, and so is a rule variable of the same name, with the placeholder as written, angle brackets included, as actual. A language without a file line has no template placeholders. So a value the tree supplies, such as a directory's name, reaches a primitive as an ordinary argument, and no primitive is declared once per directory. A capture of a template placeholder reads the placeholders of the program that holds the term, whatever the term's scope, which chooses only the workdir that joins a relative-path argument (see Scope). A template placeholder's class is checked against the receiving signature when the declaration is read.
A program the template locates writes only partitioned cells, stacking or not: every put or delete from it carries all the placeholders of the holding program's file template, in template order, as the leading components of its key, so each program writes its own partition, and a language with a file line has no unpartitioned write. In a stacking cell these are all the components of the key but the last, the path. A put with fewer components, or any other value in those places, is refused when the declaration is read, with E_DECLARATION of kind partition on the does or rule line whole whose term supplies the components, and the term as written as actual (see Writes and widths and Stacking cells). With file : teams/<team>/shares.typify and writes : bind : mounts = put 1, Mounts' rule rule : share <p> <m> -> mask (parent <p>) ; bind <p> <m> (or a does : bind <path> <mode> line) is refused with E_DECLARATION of kind partition on that rule or does line whole, with the term as written as actual; the writes line itself is refused at the writes line whole, with the line as actual, when its 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. A template without placeholders supplies no component, so the writes of its language are unchanged.
The Shelf language below has the template shelves/<shelf>/shelf.typify. In a variant of Shelf that declares term : volume : shelf label, the line does : volume <shelf> <label> gives each volume's term the name of its shelf, which the program never writes: the line volume : Algebra in shelves/mathematics/shelf.typify then builds ["volume", "mathematics", "Algebra"]. In that variant, a form : volume : <shelf> would be refused with E_DECLARATION of kind duplicate, since <shelf> already names a placeholder of the template.
Since an ensure line takes template placeholders too, ensure : grant : not-nested = (uncovered granted <team> <2>) is well formed in a language whose template has a <team> placeholder and whose primitive grant : team path mode puts its team and path as the key of a partitioned stacking cell granted: <team> is the value the tree supplies, and <2> the second argument of the grant term, its path (see Invariants). A default line takes no template placeholder: its arguments are literal text, never captures. A default line whose form's does term uses a template placeholder is refused with E_DECLARATION of kind syntax, while text such as <shelf> written in a default line is read as literal text against the placeholder's class, building that text when the class accepts it and failing with E_EXAMPLE when it does not (see default).
A program checked or planned on its own, by the one-program forms of check and plan (see Command line), binds the template placeholders of a language with a file line by matching the program's path, as given on the command line, against the template. A path that does not match is refused with E_USAGE, with the path as actual.
Two bang lines in a body with a file line say whether its programs must exist:
-
required!: every program the template locates must exist. A missing one is an error with the codeE_REQUIRED, whosefileis the declaration file that carries therequired!line, as given on the command line, and whosedata.path, and no other field, holds the missing program's path. A placeholder matches only what exists, so when arequired!template with a placeholder locates nothing, because no directory or no file matches a placeholder, the error isE_REQUIREDwith the template as itsdata.path, and no program is invented. -
optional!: a program may be missing. A missing optional program holds only its defaults: the terms of its language'sdefaultlines, in declaration order, or no terms when the language has none (see Terms and counts).
A body with a file line that carries neither line is optional!. A third kind, derived! program files, is deferred to version 1.1.
The Shelf language describes one shelf of a library: whether it is open, and the volumes it holds. Each shelf is a directory under shelves/, and its program is the file shelf.typify inside that directory.
The declaration file shelf-languages.typify, which holds only the Shelf body:
#lang typify
declare : Shelf
version : 1
file : shelves/<shelf>/shelf.typify
optional!
is : shelf = /[a-z][a-z0-9-]*/
is : state = "open" | "closed"
is : label = /[A-Za-z0-9 ]+/
term : access : state
term : volume : label
form : access : <state>
restricts!
does : access <state>
example : access : open
default : access : closed
count : access = 0..1
form : volume : <label>
does : volume <label>
example : volume : Algebra
end!
What the lines say:
- The template
shelves/<shelf>/shelf.typifyholds one placeholder,<shelf>. Its class is the lineis : shelf: a lowercase letter fromatoz, then any number of such letters, digits and hyphens. -
optional!lets a shelf directory hold no program. - A label is one or more letters from
AtoZoratoz, digits and spaces, soNumber Theoryis one label. -
accessis markedrestricts!: its line can close a shelf, so a missing line must not leave the shelf open. Its count0..1allows at most oneaccessline and lets a program omit it, so the form carriesdefault : access : closed. Only that form buildsaccess, so a program with noaccessline holds noaccessterm and gets the defaulted term["access", "closed"]after its own terms, with an origin of kinddefault(see Terms and counts). -
volumehas nocountline, so it takes*: a shelf holds any number of volumes, none included.
The tree, under a root directory named library:
-
library/shelves/mathematics/shelf.typify, a program; -
library/shelves/science/, a directory with noshelf.typifyin it.
The program library/shelves/mathematics/shelf.typify:
access : open
volume : Algebra
volume : Number Theory
typify tree shelf-languages.typify library --jsontree needs facts only for a guard or a reads line, and Shelf carries neither, so this call needs no fact; a tree that needs no fact accepts a --facts FILE and ignores it, without opening the file (see Guarded programs).
tree does four things:
- It walks the root.
- It fills every template where its placeholders match: a placeholder in a directory of the template matches existing directories, and a placeholder that its file name introduces matches existing files only. Here
<shelf>, a directory, matchesmathematicsandscience, which locates two programs. - It reads each program that exists against its language and builds its terms, as
checkdoes. - It prints the model. With
--json, it prints one object in every outcome,{"ok": true | false, "programs": [...], "diagnostics": [...], "facts": ..., "inputs": {...}}(seedocs/inputs.mdin the repository), whoseprogramsmember holds the model, one entry per program in tree order, and whosediagnosticsmember holds every diagnostic, notes included, and is an empty array when nothing is reported; when a declaration has an error or an input cannot be read,okisfalseandprogramsis empty. No command prints a bare array. In text mode, it is one line per program: the program's status, then its path.
Each entry of the model has these members:
| Member | What it holds |
|---|---|
path |
the program's path |
language |
the name of the language whose template located the program |
placeholders |
the value of each placeholder of the template |
status |
one of present, defaulted, absent-empty, missing and guarded
|
terms |
the program's terms |
origins |
the origins of the program's terms |
For the library, the model holds two entries, both of the language Shelf.
The first is for the program at shelves/mathematics/shelf.typify. Its placeholder shelf has the value mathematics, its status is present, and its terms are those of its three lines; its access line suppresses the default, so it has no defaulted term:
[["access", "open"], ["volume", "Algebra"], ["volume", "Number Theory"]]The second is for shelves/science/shelf.typify. Its placeholder shelf has the value science, and its status is defaulted: the file is missing, Shelf is optional!, and a missing optional program holds only its defaults. Shelf has one default line, so the program's one term is that line's, and the model gives that term an origin of kind default:
[["access", "closed"]]Without --json, the same call prints two lines, one per program: the status present then the mathematics shelf's path, and the status defaulted then the science shelf's path.
Over a root, programs are taken in tree order: by the depth of their directory, then by their portable path, written with / on every platform, then by the declaration order of their languages. One file that two languages locate gives two entries, one per language, adjacent in tree order. A plan over a root follows this order (see From a tree to a plan). The enclosing programs of a program keep an order of their own: nearest directory first and, within one directory, by the declaration order of their languages, then by path (see Enclosing programs).
Every program in the model carries one of five statuses:
| Status | When | Its terms |
|---|---|---|
present |
the program file exists | read from the file, then the defaulted terms of each constructor it holds no term of, in declaration order |
defaulted |
the file is missing; the language is optional! and has default lines |
only its defaults: the terms of every default line of the language, in declaration order |
absent-empty |
the file is missing; the language is optional! and has no default line |
none |
missing |
the file is missing; the language is required!
|
an error: tree reports it |
guarded |
the program holds a term of a constructor that a guard line names, and the guard's fact does not hold |
none: its terms are empty |
The statuses also decide which counts are checked. No count is checked for an absent-empty program. The counts of a defaulted program are checked over its defaulted terms. A program whose file exists is present even when defaults supply some of its terms.
Had Shelf declared required! in place of optional!, the science shelf would be missing, and tree would report the error E_REQUIRED, with shelf-languages.typify as its file and the science shelf's path as its data.path, and exit with status 1 (see Diagnostics). A missing program of an optional! language with no default line is absent-empty, with no terms. Shelf cannot drop its default line to show this status: its restricts! form access has the count 0..1, so it needs its default line, and without that line the declaration is rejected (see Terms and counts). A keyed count would not take the line's place: it requires no group to exist, so it never satisfies restricts! on its own (see Counts).
A default line also stands in inside a program that exists. Defaults are activated per constructor, for a present program as for an absent one: when a program holds no term of a constructor, by any form, every default line after a form of that constructor is added after the program's own terms, in declaration order, and one line of the constructor in the program suppresses them all. The model gives each defaulted term an origin of kind default, which names the declaration file and the line of the default line, so a consumer can tell a term that a program wrote from one that a default supplied. A shelf program that lists volumes but has no access line still gets the term ["access", "closed"], after its volumes: the program volume : Algebra alone has the terms [["volume", "Algebra"], ["access", "closed"]]. Activation precedes the count check, so defaulted terms count toward the constructor's count, and a default satisfies count = 1 (see Terms and counts).
guard : <constructor> = <fact> makes a program's plan depend on a fact. A program that holds a term of that constructor is planned only when the fact holds. When the fact does not hold, the program is not planned, its terms are empty, and tree reports it with the status guarded. Program guard says which arguments the fact is checked on. Primitives and state describes facts, and Rules and plans describes guard with the other constructs that shape a plan.
Facts record what was tested about the filesystem, and a facts file carries them. When a declaration carries a guard line or a reads line, tree takes a facts file, as plan does:
typify tree DECLARATIONS ROOT --facts FILEWithout --facts, a tree that needs a fact runs the facts collector itself, as plan does. 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. Shelf reads no fact, so a tree over it needs none, and it accepts a --facts FILE and ignores it, without opening the file. From a tree to a plan says how a facts file is written.
sees : <Name> states that this language's programs name things that another language's programs declare. A language whose programs lend volumes by their labels, for example, would declare sees : Shelf. A sees line names one body; a language that names several bodies takes several sees lines, one per body. sees : Config Notes is one name, not two, and an invalid one: the meta form's class refuses it with E_CLASS on the sees line, whose class is name, the meta class of names (see Reserved names). Version 1.0 checks only that the named language exists among the declarations. Resolving the names themselves, from one program to another, comes in version 1.1.
tree executes nothing and writes nothing. It reads the declarations, the files under the root and any facts it needs, and it prints the model. Acting on the model is the consumer's work. In a language that declares no primitive, such as Shelf, every constructor is a fact, and the consumer reads the terms as data. In a language that declares primitives, the declaration decides what each term calls for: the consumer runs plan, and its host executes the plan's primitive terms, in order, and nothing else (see From a tree to a plan below).
plan builds on tree:
typify plan DECLARATIONS ROOT --facts FILEIt reads the tree as tree does, then normalizes each program that is not guarded: the rules rewrite the program's terms until only primitive terms remain. Over the root, the plan is one list: the primitive terms of every planned program, in tree order (see Tree order) and, within a program, in term order, sorted by stage; the sort is stable, so terms of one stage keep that order (see Stage). 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. The output gives the ordered primitive terms per program, with the state the whole list is predicted to leave and the origin of every executed term and of every state entry, which carries its own.
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 its program's path; a recorded term is prefixed by its program's path and the word recorded, and an external term ends with unchecked (when nothing was planned, standard output is empty and only the diagnostics print on standard error). With --json, it prints one object of this shape, in every outcome (except a usage error, which prints {"ok": false, "diagnostics": [...]}):
{"programs": [{"path": ..., "language": ..., "status": ..., "plan": [...], "recorded": [...], "ensure": [...], "skips": [...]}, ...], "schedule": [...], "inputs": {...}, "state": ..., "bound": ..., "modelled": ..., "status": ..., "facts": ..., "origins": [...], "ensure": [...], "diagnostics": [...]}
Each entry of programs holds one program's path, its language, the name of the language whose template located the program, as in a tree entry, its status in the tree, its plan, its recorded, the terms of its primitives declared effect : <primitive> = state, apart from plan, as an array of {"term": [...], "origin": {"kind": ..., "path": ..., "line": ...}} objects in plan order, its ensure and its skips; recorded and skips stand only there, recorded beside plan, and nowhere at the top level. The top-level origins covers the executed terms of plan only, and each state entry carries its own origin. state is the final state after the fold over the stage-sorted merge of all the programs' plans. The top-level status is failed when a diagnostic of error severity exists, and otherwise ok or unchecked, by how many primitives are modelled (see Modelled and unchecked); diagnostics holds every diagnostic, an empty array when nothing is reported; and typify plan gives the shape of every member. A plan of one program, below, keeps its own shape, with plan, recorded, ensure and skips at the top level (see Command line).
A plan also depends on facts about the filesystem, which a facts file carries. The command typify facts ROOT writes the facts file for a root. The facts of version 1.0 are six: the collector's five, exists, directory, symlink, mode and owner, and clean, which the reader derives from the facts file and no collector writes (see Program guard); a collected fact the file does not hold is false. --facts FILE gives a plan that needs a fact a facts file to read; 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 plan --json (text mode does not print it), the digest of the collector's output exactly as typify facts ROOT writes it. plan needs a fact when a declaration holds a guard line, a reads line, or a fact call inside (if ...) in a rule; a plan that needs none accepts a --facts FILE and ignores it, without opening the file. The hash, the facts member of plan --json, is sha256: followed by sixty-four lowercase hex digits and names the bytes read: whenever plan needs a fact and a facts document was read and accepted, facts carries that hash in every outcome, status failed included, and facts 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 that does not follow the facts wire, is E_FILE. tree reads the same file, or runs the same collector, when a declaration carries a guard or a reads line (see Guarded programs). A plan made with --facts does not depend on the machine that makes it. For that reason every case of the public corpus that plans over a root whose declaration reads a fact carries its own facts.json, and the runner plans it with --facts (see Test your own host).
A language without a file line has no programs in a tree. plan plans one program of it at a time, as check checks one:
typify plan DECLARATIONS LANGUAGE PROGRAM [--facts FILE]Both one-program forms, of check and of plan, take --facts FILE. When the declaration reads a fact and no --facts is given, the one-program plan is refused with E_USAGE, with --facts as actual: there is no root to collect from.
Rules and plans plans the Mounts program this way, and Primitives and state describes facts and the predicted state.
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. Programs in one directory are siblings and never enclose one another. Several programs in one enclosing directory are ordered by the declaration order of their languages, then by path. A guarded program's terms are empty, so it is never an enclosing context, and whatever raw data of it is kept for audit is never searched; 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). The library function inherited and two of the constructs that shape a plan can read the enclosing programs (see Library functions and The eight constructs):
-
(inherited <constructor>)is the list of the first arguments of every term of that constructor in the enclosing programs, nearest program first and in line order within a program, and the empty list when there is none, whose loop emits nothing; a constructor of more than one argument is refused under either lookup withE_DECLARATIONof kindarity, with the constructor as named asactual, never truncated to its first argument. -
(context <constructor>)is the first element of that list: 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; it is an error,E_CONTEXT, when no enclosing program has one (see Context lookup). Both lookups are evaluated when the program is planned, against its enclosing programs. They 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 withE_DECLARATIONof kindreference, with the name asactualand the declaring bodies inrelated. Every command reads the declaration first and reports its diagnostics as Every error, in order says, so such a name is refused bytree,plan,askandemitas bycheck. - A scope resolves the relative arguments of a term: an argument at a signature position declared
relative-path, or a class that is a bare reference to it, directly or through further bare references, is joined to the first argument of theworkdirterm of the program the scope names, and the joined value must then be acanonical-path. The scope names the program itself underprogram, the default, the nearest enclosing program underparent, and the shallowest program among the program itself and its enclosing programs underroot(see Scope, which also says how a scope carries into the primitives a rule emits and why no other argument is resolved). The scopeparentrequires an enclosing program in a strict ancestor directory, so a sibling is never a program's parent. A language with noworkdirterm admits no relative argument; arelative-pathargument with noworkdirterm in scope isE_CONTEXT.