Repository navigation
Terms and counts
As of v1.0.0
A program's meaning is a list of terms. This page covers how a term is written, how a language declares the constructors its terms use, how every form builds a term, how examples are run, which constructors are facts and which are effects, and how count, default, restricts! and order decide what a program must hold. For the classes that the arguments belong to, see Classes.
The examples on this page use the Catalog language, declared in the file languages.typify:
#lang typify
declare : Catalog
version : 1
file : catalog.typify
required!
is : category = "book" | "journal"
is : title = /[^=]+/
term : item : category title
form : item : <category> = <title>
does : item <category> <title>
example : item : book = Algebra
count : item = 1..*
end!
A term is a constructor followed by its arguments, separated by spaces: <constructor> <argument> .... Each argument is one of three things:
- a capture
<name>, which stands for a captured value: in adoesline, the text that the form's placeholder<name>captured, or the value of the template placeholder<name>(see Template placeholders in terms); - a literal, which stands for itself: a bare word, a maximal run of code points holding no blank, parenthesis, angle bracket or quote, or a quoted literal written as in a class (see Class expressions), whose decoded string is the argument's value, so
"ro"androare one argument; a quote that opens no well-formed quoted literal isE_DECLARATIONof kindsyntax; - a library call in parentheses, such as
(parent <p>), evaluated innermost first; its string result must belong to the class of the position it fills.
A nested term of a user constructor is rejected when the declaration is read, with the code E_TERM_NESTED: terms are first-order in 1.0, and term-sorted arguments come in 1.1.
A term with the wrong number of arguments for its constructor's signature, in a does line or on a rule's right side, is E_TERM_CLASS of kind arity: its range covers the term's own span, its class is the signature and its actual the number of arguments received. A rule's left side with the wrong number of variables is E_DECLARATION of kind arity instead, on the rule line whole (see Declaration-time checks).
On a rule's right side, three of the eight constructs may also appear: (context <constructor>), (if <fact-call> (<term>)) and (each <var> in <list> (<term>)), with the body term inside (if ...) and (each ...) written in parentheses (see Rules and plans). In (if ...) the fact is always a call in parentheses with its arguments; a bare fact name there is E_DECLARATION of kind syntax. (if ...) takes a fact call or one of the library predicates prefix, suffix and equal, never a state predicate such as visible, which only an ensure may use: a state predicate inside (if ...) is refused with E_DECLARATION of kind syntax, on the rule line whole (see Invariants).
Three terms from the examples on this wiki:
| Term | Constructor | Arguments |
|---|---|---|
item <category> <title> |
item |
two captures |
share <path> rw |
share |
a capture and the literal word rw
|
mask (parent <p>) |
mask |
the library call (parent <p>)
|
The first is the Catalog's. The other two come from the Mounts language on Rules and plans, where a rule uses captures for its variables and parent is a library function.
term : <constructor> : <class> ... declares a constructor of the language's signature, with the class of each of its arguments in order. The Catalog's term : item : category title declares the constructor item with two arguments: the first of class category, the second of class title.
A constructor with no arguments is written without the second colon: term : ready. A trailing colon, as in term : ready :, is refused with E_DECLARATION of kind syntax.
A does : <term> line follows every form and gives the form's meaning. When a program line matches the form, the reader builds the does term, with each capture replaced by the text its placeholder captured or, for a placeholder of the language's file template, by the value the tree supplies (see Template placeholders in terms). Every does builds a term of a declared constructor.
A placeholder name appears at most once in a form: form : rename : <title> = <title> is refused with E_DECLARATION of kind duplicate, with the placeholder as actual. Two captures of one class take two names that refer to it, is : old-title = title and is : new-title = title, and the form is written form : rename : <old-title> = <new-title>.
In a language with a file line, a does line may also use <name> of a placeholder of the template, as a capture of that placeholder's class, whose value the tree supplies, and so may a rule's right side and an ensure line; a form placeholder of the same name is refused with E_DECLARATION of kind duplicate (see Template placeholders in terms).
In the Catalog, the form item : <category> = <title> is followed by does : item <category> <title>. The line item : journal = Market Research therefore builds the term ["item", "journal", "Market Research"].
Two forms may build terms of the same constructor. In Mounts, the form share : <path> does share <path> rw and the form share-read : <path> does share <path> ro. The line share-read : /shares/archive builds ["share", "/shares/archive", "ro"]. A consumer sees one constructor, share, whichever form the program used. A question about share opens with both forms, each form line with its does line, in declaration order (see Questions).
example : <line> gives a line for the form above it. The reader checks that the line matches that form and builds its term, library calls included: each capture must belong to its class, and a template placeholder takes the shortest witness of its class (see Witnesses and the hostile corpus). An example that fails is an error in the declaration; an example whose library call fails carries the library's own code, E_LIBRARY_DOMAIN or E_LIBRARY_PARENT, with a range that covers the library call in the form's does line. Only the lookups (context <constructor>) and (inherited <constructor>) are not evaluated for an example. Their formation is checked whole when the declaration is read: the constructor they name exists, 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, while a context outside any each requires none; only their values are read when a located program is planned, against its enclosing programs (see Context lookup).
The Catalog's example : item : book = Algebra matches the form item : <category> = <title>, its captures book and Algebra belong to the classes category and title, and it builds ["item", "book", "Algebra"]. Had its line been item : magazine = Weekly, the declaration would be rejected, because the capture magazine fails the class category. An example also teaches: a question about a word answers with its example lines (see Questions).
The meaning of a program is the list of its lines' terms, in the order of the lines, followed by its defaulted terms, which the language's default lines supply (see default below). The reader builds each term and checks it against its constructor's signature (the number of arguments and the class of each), then adds the defaulted terms, and only then checks every count, so the defaulted terms count with the others. Blank lines and comment lines build no term.
A program is read only when its declaration has no diagnostic of error severity: check of a program under a declaration with an error reports the declaration's diagnostics and exits with status 1 without reading the program. The reader reports every error it can find, ordered by position (see Diagnostics).
The program catalog.typify:
item : journal = Market Research
typify check languages.typify Catalog catalog.typify prints nothing and exits with status 0: in text mode, check prints diagnostics only. typify check languages.typify Catalog catalog.typify --json prints the meaning as the terms member of one document:
{"ok": true, "terms": [["item", "journal", "Market Research"]], "diagnostics": []}Two lines give two terms, in the order of the lines:
item : book = Algebra
item : journal = Market Research
The terms member is then:
[["item", "book", "Algebra"], ["item", "journal", "Market Research"]]A term is data. The reader prints it as JSON; the Catalog's terms print as arrays whose first element is the constructor and whose other elements are the arguments, as strings.
An adapter, the code a consumer writes to act on a language's programs, receives terms, never raw lines. The typing happens once, in the declaration. The adapter is a map out of the terms and never parses a line.
A constructor is a fact or an effect. Which one it may be depends on the body that declares it:
- In a body that declares no primitive, every constructor is a fact, and every
doesbuilds data. The Catalog declares no primitive, soitemis a fact, and itsdoesbuilds data. - In a body that declares a primitive, every constructor is one of three things: a primitive, which a host executes unless it is declared
effect : <primitive> = state(see Primitives and state); a constructor that a rule rewrites (see Rules and plans); or a fact, declared by the linefact : <constructor>. A constructor that is none of the three is rejected when the declaration is read, with the codeE_CONSTRUCTOR_UNBOUND, and the diagnostic gives its column.
A primitive and a constructor that a rule rewrites are effects. A constructor declared by fact : <constructor> is a fact, as is every constructor of a body that declares no primitive.
Typify's own meta body declares primitives, so the same rule holds for every constructor in it. Its kernel primitives are declared external, so the reader treats each as external, and plan over the meta body is refused with E_USAGE.
primitive : <constructor> : <class> ... declares a primitive in the same shape as term; a primitive with no arguments is written primitive : ready, and a trailing colon there is E_DECLARATION of kind syntax too. A primitive also needs a writes line or the line effect : <primitive> = external; a primitive with neither is refused with E_DECLARATION, its data.kind being effect (see Primitives and state). Mounts declares the primitives mask and bind, declares share by term and rewrites it with a rule, and declares no fact, so mask, bind and share are effects. The meaning of the Mounts program shares.typify, the one line share : /shares/research, is still its one term; it is the terms member that typify check mounts.typify Mounts shares.typify --json prints:
[["share", "/shares/research", "rw"]]Its plan, the meaning rewritten until only primitive terms remain, is the plan member that typify plan mounts.typify Mounts shares.typify --json prints:
[["mask", "/shares"], ["bind", "/shares/research", "rw"]]Primitives and state says what a primitive declares; Rules and plans says how rules rewrite a meaning into a plan.
count : <constructor> = <multiplicity> states how many terms of that constructor a program holds, and a keyed count, count : <constructor> = <multiplicity> by <k>, how many each group of them holds (see below). The multiplicity is one of the four values below, its only spellings, read per program for the plain form and per group for the keyed one:
| Value | A program holds |
|---|---|
1 |
exactly one term of the constructor |
0..1 |
at most one |
1..* |
at least one |
* |
any number, none included |
A constructor with no unkeyed count line takes *: with only a keyed count line, the unkeyed bound is *.
The four values are multiplicities. A count limits how many terms a program holds; it says nothing about how a consumer uses a term.
A count belongs to a constructor, not to a form, so it counts the terms of every form that builds that constructor, defaulted terms included. In Mounts, a count on share would count the lines of both forms, share and share-read. Mounts declares no count, so share takes *.
A keyed count applies the multiplicity to each distinct tuple of the first k arguments among the constructor's terms of one program, defaulted terms included: the terms whose first k arguments are equal form one group, and each group is held to the multiplicity. The plain form, without by, is by 0: one tuple, the empty one, so the whole program is one group. The k is read under the meta class width, as the k of a delete <k> write is, so by 0 is legal (see Reserved names). A k greater than the number of arguments in the constructor's signature is refused with E_DECLARATION of kind syntax, with the count line whole as actual.
A constructor may carry one unkeyed count and one keyed count. The unkeyed one bounds the constructor's terms in a program, and the keyed one each group of them. A second count of either kind is refused with E_DECLARATION of kind duplicate, with that count line, as written, as actual; by 0 written out is the unkeyed form, so a count line ending in by 0 and a plain count line of one constructor are two unkeyed counts. A keyed count constrains only the groups that occur and requires none to exist, so it fails only by excess within a group, and it never satisfies restricts! on its own (see restricts! below).
With count : share = 0..1 by 1, Mounts would allow at most one share term per path. Two lines that build share for one path, whether both are share : /shares/research, building two rw terms, or one is share : /shares/research and the other share-read : /shares/research, building one rw and one ro, would fail the count on the second line, with key ["/shares/research"], the one-element tuple of that path. Shares of two different paths would not.
The Catalog declares count : item = 1..*. A catalog.typify with no item line, or with only blank and comment lines, builds no item term, so the count fails and the program is an error with the code E_PROGRAM_COUNT (see Diagnostics).
A count failure stands in the program file. When a program holds more terms of a constructor than its count allows, or, under a keyed count, more in one group, the diagnostic's range covers the line of the first term past the multiplicity within that group: under count : access = 0..1, a shelf program with two access lines is an error on the second of them. When a program holds too few, the range covers the program's first line, or runs from line 1, column 1 to line 1, column 1 when the program is empty. Under a keyed count, each failing group has its own diagnostic, anchored as above within its program; a failure of the unkeyed count has its own beside them, and all are reported in position order. The diagnostic's related member carries the origins of the counted terms, defaulted terms included, and its data carries constructor; the multiplicity as written, a string such as "0..1", as expected; the number of terms in the group, an integer, as actual; and the group's tuple as key, an array of decoded strings, empty ([]) under an unkeyed count.
Under *, nothing bounds how many lines a program holds. Rules and plans says what follows for the length of a plan.
The reader is closed-world. A line the program does not hold builds no term, and the reader never guesses one. A declaration can make a missing term an error with a count, or supply it with a default.
default : <line> gives a program line whose arguments are literal text, never captures of any kind: a default line whose form's does term uses a template placeholder is refused with E_DECLARATION of kind syntax, while text such as <name> written in a default line is read as literal text against the placeholder's class (refused with E_EXAMPLE when the class does not admit it). It stands after a form, and it is activated per constructor, not per form: when a program holds no term of that form's constructor, by any form, every default line that stands after a form of that constructor is added to the program. One line of the constructor in the program, whichever form it matches, suppresses all of them. In Mounts, where the forms share : <path> and share-read : <path> both build share, a default line after either form would be suppressed by a line of either form.
The rule is the same for a program that exists and for an absent one. An absent optional program holds no term of any constructor, so it holds only its defaults: the terms of every default line of its language, or no terms when the language has none. When its language has default lines, such a program is defaulted and its counts are checked; when the language has none, it is absent-empty and no count is checked for it. A program whose file exists is present, even when defaults supply some of its terms. Trees describes optional programs and their statuses.
The terms that activated default lines supply are the program's defaulted terms. They are added after the program's own terms, in declaration order, and each has 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. Activation precedes the count check, so defaulted terms count toward count, and a default satisfies count = 1.
Defaults alone never break a count. A declaration whose default lines for one constructor would by themselves exceed the constructor's multiplicity is refused when it is read, with E_DECLARATION of kind conflict and the first default line past the multiplicity as actual. Since a default line holds only literals, every default's group is known when the declaration is read, and under a keyed count the rule applies per group: the first default line past a group's multiplicity is kind conflict, as it is past the unkeyed multiplicity. No program therefore reports a count failure produced by defaults alone.
Some forms restrict something when their line is present: the line closes, denies or limits. Under a closed world, a program that omits such a line holds no term of it, and what the line would have restricted stays unrestricted: the form fails open when it is absent.
A declaration marks such a form with the bang line restricts!, written below the form line in the body. It marks the nearest form above it, so it need not be the next line: it may follow the form's does, example, default and count lines, up to the next form or end!. The form's constructor must then carry one of two things:
- the unkeyed count
1or1..*, so that a program with no term of the constructor is an error; - a
defaultline under any form that builds the constructor, not only the marked one, whose term is added when a program holds no term of the constructor.
A declaration with a restricts! form that carries neither is rejected with the code E_RESTRICTS, reported once for each marked form whose constructor is not satisfied. A constructor may carry both: activation precedes the count check, so a program with no term of the constructor then has the defaulted term and is not an error. A keyed count never satisfies restricts! on its own: it constrains only the groups that occur and requires none to exist, so E_RESTRICTS reads the unkeyed count and the default lines, as above.
The Shelf language, whose programs Trees reads from a directory tree, uses both restricts! and default:
#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!
The form access : <state> is marked restricts!, since its line can close a shelf. Its count 0..1 lets a program omit the line, so the form needs its default line, default : access : closed. Only that form builds access, so a shelf program with no access line holds no access term and has the defaulted term ["access", "closed"], after its own terms and with an origin of kind default: a shelf is never open by omission. The shelf program
volume : Algebra
volume : Number Theory
has its two own terms, then the defaulted one:
[["volume", "Algebra"], ["volume", "Number Theory"], ["access", "closed"]]Had Shelf declared count : access = 1 and no default, a shelf program without an access line would be an error instead. Had it declared count : access = 1 and kept the default, the defaulted term would satisfy the count, and the program above would have the same three terms. Had it kept count : access = 0..1 and dropped the default, the declaration would be rejected. Had it kept count : access = 0..1 and its default and added the line default : access : open after default : access : closed, its two default lines alone would exceed 0..1, and the declaration would be refused with E_DECLARATION of kind conflict, with that added line, the second default line in declaration order, as actual.
The form volume : <label> is not marked: a shelf program with no volume line holds no volume, and volume takes the count *.
A question ? : <word> answers with the lines that declare the word. One line of the answer, the missing line, placed directly after the count lines and before the default lines, states in one word what happens when a program holds no term of the word's constructor: ? : missing : refused when the program fails the unkeyed count, with the code E_PROGRAM_COUNT; ? : missing : default when the default lines supply the terms, the default lines that follow it stating the defaults themselves; or ? : missing : empty when the program holds no term and nothing fails. For the Catalog, typify ask languages.typify Catalog item prints:
? : form : item : <category> = <title>
? : file : catalog.typify
? : does : item <category> <title>
? : term : item : category title
? : is : category = "book" | "journal"
? : is : title = /[^=]+/
? : example : item : book = Algebra
? : count : item = 1..*
? : missing : refused
The count 1..* makes a missing item line an error, so the missing line says refused. For Shelf, the answer about access holds the line ? : missing : default, followed by ? : default : access : closed, and the answer about volume, a constructor with no default line after any of its forms and no count line, holds ? : count : volume = * and ? : missing : empty: when a body declares no unkeyed count for a constructor, the answer prints the default *, before the keyed count line when there is one. Questions gives the whole answer.
A body may declare order : strict or order : free:
-
order : freeallows a program's lines in any order. It is the default. -
order : strictrequires a program's lines to follow the declaration order of their forms, with all the lines of each form together. A program that breaks it is an error with the codeE_ORDER.
Had Shelf declared order : strict, a shelf program would have to put its access line before its volume lines, and keep its volume lines together. Whichever order is declared, a meaning lists its own terms in the order of the lines, then its defaulted terms in the declaration order of their default lines.