Skip to content

Examples

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

As of v1.0.0

This page works through two complete languages, from the declaration to what the reader prints. Catalog shows classes, terms, counts, questions, diagnostics and the tree view. Mounts adds a stacking cell, primitives, a rule and an invariant, and shows a plan with its predicted state. Every concept links to the page that owns it.

The commands assume the typify executable is installed; see Install.

Catalog

Catalog is a language with one kind of line: an item of a catalog, with a category and a title.

The declaration file

Save this as 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!

Line by line

  • #lang typify opens every declaration file and marks it as a Typify module. A program file has no header. See Language.
  • declare : Catalog opens a body that declares the language Catalog, and end! closes it. A declaration file may hold several bodies. See Language.
  • version : 1 states the version of the Catalog language itself.
  • file : catalog.typify says where a Catalog program lives in a directory tree: a file named catalog.typify under the root. A template may hold placeholders; this one has none. See Trees.
  • required! makes a missing Catalog program an error when tree walks a root. See Trees.
  • is : category = "book" | "journal" binds the class category to two quoted literals joined by |, the alternation. The placeholder <category> takes this class. Strings compare exactly, with no case folding, so Book is not a category. See Classes.
  • is : title = /[^=]+/ binds the class title to a pattern, written between slashes: one or more code points, none of them =. A pattern is anchored to the whole capture, so the whole title must match it. Like every class, title never matches a refused code point, such as a control character: the negated range [^=] takes every allowed code point except =. The ordinary space is allowed, so a title may hold spaces; it never holds =. See Classes.
  • term : item : category title declares the constructor item, with two arguments of the classes category and title. See Terms and counts.
  • form : item : <category> = <title> declares a legal line: the verb item, then a capture for <category>, the literal = (a space, an equals sign, a space), and a capture for <title>. See Language.
  • does : item <category> <title> gives the form's meaning: a term of the constructor item whose arguments are the two captures. Catalog declares no primitive, so item is a fact and its does builds data. See Terms and counts.
  • example : item : book = Algebra gives a line for the form above. The reader checks that it matches the form and builds its term, ["item", "book", "Algebra"], its captures fitting their classes; an example that fails is an error in the declaration. See Language.
  • count : item = 1..* says that a Catalog program holds at least one item term. The values are 1, 0..1, 1..* and *, and * applies when a constructor has no unkeyed count line. See Terms and counts.
  • end! closes the body.

The declaration file has a different name from the program file on purpose: tree reads the files that match a file line, and languages.typify matches none.

Checking the declaration

typify check languages.typify

With one file, check checks the declarations alone. Among its checks: no class can hold the literal that follows its placeholder in a form, so category can never take in the =; every does builds a term of a declared constructor with arguments of the declared classes; and every example fits its form and builds its term, the classes of its captures included. In text mode check prints diagnostics only, so a declaration with no diagnostic, as this one, prints nothing and exits with status 0; a note, such as N_CLASS_EMPTY, is printed and leaves the exit status at 0. See Command line.

The program file

Save this as catalog.typify, the name the file line gives:

item : journal = Market Research

Check it against its language:

typify check languages.typify Catalog catalog.typify

The program is clean, so check prints nothing and exits with status 0. To see the program's meaning, ask for JSON:

typify check languages.typify Catalog catalog.typify --json
{"ok": true, "terms": [["item", "journal", "Market Research"]], "diagnostics": []}

terms is the program's meaning, the list of its lines' terms followed by its defaulted terms; Catalog declares no default line, so this program has none. diagnostics is empty. The document has no bound member, which check prints only in the program form when the declaration has rules, and Catalog has none.

The reader strips the line of outer blanks and splits it at the first colon into the verb item and the argument journal = Market Research, each stripped of outer blanks. It matches the form: the literal between the captures, a space, an equals sign and a space, delimits them; each capture is the exact text the literals delimit, untrimmed, and each is checked against its class. journal is a category. Market Research is a title. The one item term meets count : item = 1..*. A consumer reads the terms and never parses the line. See Terms and counts.

Asking about a word

Every language accepts the reserved question line ? : <word>; for item it is ? : item. The ask command answers it:

typify ask languages.typify Catalog item
? : 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 answer is the declaring lines of the word, each under the reserved verb ?, so no verb of a language can collide with an answer line. For a word built by one form, as item is, the lines come in a fixed order: the form line; the language's file line, when it has one; the does line, then the form's restricts! line, when it has one; the term or primitive line; one is line per class of the placeholders the does line names, the form's and the template's, each once, a class that those classes reference being asked by its own name; the constructor's own lines, every line whose first name after the introducer is the constructor (writes, reads, guard, effect, stage, scope, fuel and, for a fact constructor, fact), in declaration order, the rule lines whose left side is the constructor and its ensure lines, when the word has them; the example lines; the count lines, the unkeyed one, or count : <constructor> = * when the body declares none, then the keyed one when it exists; the missing line; the default lines; and last the constructor's note lines. A form line and its does line are a pair: a constructor built by two forms, such as share in Mounts below, is answered with every pair first, in declaration order, the file line after the first form line, and the rest of the answer follows once. A word declared as a primitive, a class or a cell and built by no form is answered by its declaring lines of that kind, first line first: the class category by its is line alone. A declared word gets no status line, because its first line already shows that it is declared; for item that line is the form. ? : missing : refused says what happens when a program holds no term of the constructor item, by any form: the count asks for at least one, and no form of item carries a default line to add one, so the program is in error. The missing line states the case in one word; the other cases are ? : missing : empty and ? : missing : default, which the answer's default lines follow, stating the defaults themselves. With --json, ask prints the same answer as one object whose word is item, whose status is declared, and whose lines hold the nine answer lines as strings, and a round-trip test checks the lines against the object (see Lines and JSON).

A word that nothing declares gets a different answer:

typify ask languages.typify Catalog itme
? : itme = undeclared
? : near : item

With --json:

{"word": "itme", "status": "undeclared", "lines": ["? : itme = undeclared", "? : near : item"], "near": ["item"], "accepts": [], "diagnostics": []}

undeclared says that no line of the language declares itme. The candidates are the verbs of the language's forms and the constructors of its term and primitive lines; Catalog's one candidate is item. The near lines name the candidates at the least optimal string alignment distance, when that distance is within max(n, 3) // 3, capped at 2, where n is the number of code points in the asked word and an exchange of two adjacent code points costs one; every tie is named, in code-point order. When no candidate lies within that bound, they name the candidates that share a hyphen part with the asked word, and when none does, the answer is its status line alone. itme has four code points, so the bound is 1, and item is one exchange away. The answer ends there: ask prints no accepts line in v1, so accepts is empty, and an undeclared word exits with status 0. See Questions.

A failing line

Replace the line of catalog.typify with this one and run the same check:

item : magazine = Weekly

The line has the shape of the form, but its first capture, magazine, is not in the class category. This is a class failure, with the code E_CLASS. The reader reports an error on line 1 of catalog.typify whose range starts at column 8, where magazine begins, and ends at column 16, just past its last code point, since the end of a range is exclusive. Columns count code points from one, with a byte offset from zero beside them, here 7 at the start and 15 at the end. The diagnostic is one JSON document whose fields include its code, here E_CLASS, its severity, the file, the range, a message, and its data, here {"class": "category", "token": "magazine"}, the class and the token that fails it; in text mode check prints its projection, and it exits with status 1. Because the failed line builds no item term, the program also fails count : item = 1..*, and check reports a second diagnostic, E_PROGRAM_COUNT, from line 1, column 1. A line that fits no form at all, such as item : book Algebra, which has no =, is a different kind of error, with the code E_FORM_NONE (also accompanied by E_PROGRAM_COUNT when no other line supplies an item term). See Diagnostics.

The tree view

The file line places a Catalog program in a directory tree. Put back the line item : journal = Market Research in catalog.typify; with it and languages.typify in the current directory, walk the directory:

typify tree languages.typify . --json

tree fills each file template wherever its placeholders match under the root, a placeholder in a directory matching existing directories and one in the file name existing files only, and reads each program it finds against its language. This template has no placeholders, so it names one file, catalog.typify. The result is one JSON object, {"ok": ..., "programs": [...], "diagnostics": [...], "facts": ..., "inputs": {...}}, where facts is the facts digest or "none" and inputs records what the walk observed (see Command line), whose programs member is the model, with one entry per program: its path, its language, here Catalog, its placeholders (none here), its status, its terms, here [["item", "journal", "Market Research"]], and its origins. The status of this program is present: its file exists and was read. If catalog.typify were removed, the program would be missing, and since the body says required!, that is an error, with the code E_REQUIRED. In all, a program's status is present, defaulted, absent-empty, missing or guarded. Catalog carries no guard or reads line, so tree needs no facts here, and a --facts FILE given to it would be ignored, the file not opened. tree executes nothing and writes nothing. See Trees.

Mounts

Mounts is a language for sharing a path, read-write or read-only. Its meaning goes one step further than Catalog's: a rule rewrites each term into primitives, the terms a host executes, and the reader predicts the state those primitives leave and checks an invariant over it.

The declaration

Save this as mounts.typify:

#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!

Line by line

#lang typify, declare : Mounts, version : 1 and end! work as in Catalog.

The classes (Classes):

  • is : path = canonical-path binds path to canonical-path, a class the reader provides. A bare name in a class refers to another class, here a built-in one. canonical-path matches the root /, which has zero components, and every other absolute path whose components are non-empty, none of them . or .., with no trailing slash, all over allowed code points other than U+0020, the space. /shares/research is a path, and so is /; shares, /shares/, /shares//research, /shares/../research and /shares/team notes are not: a path with a space fails the class. The cell below needs a key class of this kind.
  • is : mode = "rw" | "ro" is read-write or read-only.

The cell (Primitives and state):

  • cell : mounts = shadow stacking declares the cell mounts, a part of the predicted state. Its law is shadow: a later put at a key replaces an earlier one. The other laws are first, where the first put stays, and join with a table. stacking marks a cell whose keys end in a path, where a later put at an ancestor hides a descendant. The key of mounts is the path alone, of width 1; a stacking cell's key may also be a tuple whose last component is the path and whose earlier components partition the cell (see Stacking cells). A cell that is not stacking may have a key of any width, at least as wide as the template's placeholders when a program located by a template writes it. Partitions are not limited to stacking cells: a program located by a template with placeholders writes every cell, plain or stacking, partitioned by them (see Writes and widths); Mounts has no file line.

A stacking cell needs a canonical class for the last component of its key, the path, because the reader works out which path is an ancestor of which from the paths as strings, without looking at a file system. If /shares/../research could be a key, the reader would count /shares among its ancestors, though it names /research, outside /shares; and /shares/research/ would be a second key for the path /shares/research, which a put at one spelling would not replace at the other. So the reader requires the class of a stacking cell's last key component to be canonical-path or a class it can show is a subset of canonical-path. The key of mounts is a path, and path is canonical-path itself.

The primitives and their writes (Primitives and state):

  • primitive : mask : path declares the primitive mask, with one argument of class path. A primitive is a term a host executes, unless it is declared effect : <primitive> = state, and a host implements its primitives and nothing else.
  • writes : mask : mounts = put 1 says that mask writes the cell mounts with a put of width 1. The width is the number of leading arguments that form the key, and the rest are the value: the key of a mask is its path. A mask has no argument after its path, so its put has no value arguments and writes the entry masked.
  • primitive : bind : path mode and writes : bind : mounts = put 1 do the same for bind, which takes a path and a mode. With width 1, the path is the key and the mode is the value.

A write delete <k>, with k from 0 up to the cell's key width, would instead remove every entry whose first k key components equal the first k arguments of the deleting primitive's term, never by path ancestry inside a component, so a delete 1 at /shares would remove the entry at /shares and leave the one at /shares/research; delete 0 clears the whole cell when the cell is not partitioned, as mounts is not, while on a partitioned cell a delete names at least the partition components, so delete 0 there is refused (see Stacking cells). Mounts has none. Neither primitive reads a fact, so the body has no reads line. Both primitives have a writes line, so the reader models both; a primitive may go without one only when it is declared effect : <primitive> = external.

The term and its rule (Rules and plans):

  • term : share : path mode declares the constructor share, with a path and a mode. share is not a primitive, so no host executes it directly.
  • rule : share <p> <m> -> mask (parent <p>) ; bind <p> <m> rewrites a share term into two primitive terms, in order: mask the parent of the path, then bind the path with the mode. The left side is the constructor and variables only. The right side is a sequence of terms separated by ;. (parent <p>) is a nested term that calls parent, a pure library function over strings: parent of a path is the path without its last component, so parent of /shares/research is /shares and parent of /shares is /; parent of / is an error, with the code E_LIBRARY_PARENT. The argument of parent must be a canonical-path: any other string is refused with the code E_LIBRARY_DOMAIN, never given a host's default, and here <p> is a path, which is canonical-path itself. A nested term is allowed only as such a library call; its result is checked against the class of the position that receives it, here the path of mask. A right side may use only primitives, library functions and constructors declared earlier in the body than the left side's constructor, and each constructor has at most one rule, so normalization always ends: a term reaches a normal form of primitives, or stops at its first failure, as share : / below does.

The root / is a path, so the line share : / is well formed: it fits the form share : <path>, and the grammar that typify emit writes for Mounts accepts it. Its plan is still an error, because the rule calls parent of /. The result of a library call is judged by the reader after the line's form, and an emitted grammar does not carry it; see Emission.

Mounts declares primitives, so each of its constructors must be a primitive, be rewritten by a rule, or be declared fact : <constructor>; any other constructor is rejected at declaration time with a column. mask and bind are primitives and share is rewritten by its rule, so the body passes.

The invariant (Primitives and state):

  • ensure : share : child-visible = (visible mounts <1>) attaches an invariant named child-visible to the constructor share. Its variables are bound by position to the arguments of share: <1> is the first argument of share, the path, and <2> would be the second, the mode. The binding is the same whether the constructor is rewritten, as share is, a primitive or a fact, so the invariant does not use the rule's names <p> and <m>. The reader evaluates it once per share term, over the predicted state after the whole plan, so <1> is the path the program shares. Each state predicate names its cell, then every component of the key as a separate argument, and the key of mounts has one component: (visible mounts <1>) holds when the key <1> is bound in the cell mounts and no later put landed at a stacking ancestor of it. The other built-in predicates take the same arguments: bound, which holds when the latest put at the key has a value, masked, which holds when the latest put at the key has none, absent, and, for a stacking cell, covered, which holds when some proper ancestor of the key's path, in the key's partition, is visible, and its negation uncovered. The state predicates are usable in an ensure line only (where binary conjunction (and P Q) may join predicates), never in (if ...) (see Invariants).

The two forms (Language, Terms and counts):

  • form : share : <path> declares a line with the verb share and a path. does : share <path> rw gives its meaning: a share term of the captured path and the literal mode rw. example : share : /shares/research is a line that fits it.
  • form : share-read : <path>, does : share <path> ro and example : share-read : /shares/archive declare a second line, whose meaning is a term of the same constructor with the mode ro.

Two forms can build terms of one constructor: a form is the shape of a line, and its term is what the line means.

The program and its plan

Save this one-line program as shares.typify:

share : /shares/research

The line fits the form share : <path>, so its meaning is one term, share /shares/research rw. typify check mounts.typify Mounts shares.typify prints nothing and exits with status 0; with --json, its terms member is [["share", "/shares/research", "rw"]], and since Mounts has a rule the document also carries bound.

Mounts has no file line, so tree cannot find its programs under a root. It is planned one program at a time, the way check checks one:

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

Normalization rewrites the term by the rule. The rule binds <p> to /shares/research and <m> to rw, then evaluates its right side left to right, innermost first: parent of /shares/research is /shares, a path, which gives mask /shares, then bind /shares/research rw. Both are primitives, so the term has reached its normal form. Mounts has no stage line, so both primitives are at the default stage 0 and keep the order the rule gives them. The plan member is that normal form:

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

The modelled member is "2 of 2" and the status member is "ok": no diagnostic of error severity exists, and both primitives write a declared cell, so no term of the plan is marked unchecked. A plan that holds an unmodelled primitive has the status "unchecked" and never reads green, and a plan with a diagnostic of error severity has the status "failed". Among the document's other members are the predicted state; the bound, with c, the per-line constant the reader computes from the declaration, n, the number of terms the program holds, and limit, c times n; the origins of every executed term, each state entry carrying its own origin; recorded, empty here, since Mounts declares no state primitive; ensure, one entry per evaluated invariant with its name, ok and its origin, here one entry, for child-visible, whose ok is true; skips, one entry per skip, each with its constructor, fact, args (empty when the fact took none), reason (if, read or guard) and origin, or, for a lookup that crossed a guarded program, its reason guard, fact, args and program, empty here; and diagnostics, empty too, since nothing is reported. A plan is a pure function of the declarations and the facts; the reader executes nothing. See Rules and plans and Command line.

The predicted state

The document's 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}}]}

The stacking cell mounts holds two entries, each with its key, its value, masked, true exactly when the put that wrote the entry carried no value, its origin and, on a stacking cell, its sequence, the zero-based position of the write that established the entry. 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, the line whose term the rule rewrote. The program has one share term, so child-visible is evaluated once, with <1> bound to /shares/research: (visible mounts "/shares/research") holds because the put at the ancestor /shares came before the put at the child, so child-visible passes. See Primitives and state.

Why the order matters

Suppose the rule were written with its right side in the other order:

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

The plan of the same program would be:

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

Every term of this plan is a declared primitive with arguments of the right classes, so the rules alone accept it. In this plan the put at the ancestor /shares comes after the put at the child, and in a stacking cell a later put at an ancestor hides the descendant: (bound mounts "/shares/research") still holds, but (visible mounts "/shares/research") does not. The invariant child-visible rejects the plan, and its entry in ensure, whose ok is false, is where the reversed order shows. Its failure has the code E_ENSURE and, like that of every failing check, carries its name, its data and the origins of the terms it joins; see Diagnostics. With --json the plan is still printed whole: its diagnostics holds that E_ENSURE, its status is "failed", and plan exits with status 1.

Two choices in the declaration let the model see this. The predicate is visible, not bound: (bound mounts "/shares/research") holds in both orders, since in both the latest put at /shares/research is the bind, which has a value. And the cell is stacking: in a stacking cell, (visible <cell> <key>) reads the order of the puts at a key and at its ancestors in that cell, within the key's partition. In a plain shadow cell, two puts on different keys commute and no key has a stacking ancestor, so (visible mounts "/shares/research") would hold in both orders and child-visible could not tell them apart.

The two plans hold the same terms and differ only in their order. Plans compare as ordered argument lists, never as sets, so they are different plans, and only the first passes the invariant. See Rules and plans.

Where next

  • Language for lines, bodies and forms.
  • Command line for every command, --json and the exit statuses.
  • Emission for typify emit, which writes a language's grammar for a constrained decoder.

Clone this wiki locally