Repository navigation
Questions
As of v1.0.0
Every language accepts one reserved line, the question ? : <word>. The reader answers with Typify lines that say what the word is in that language. Someone who has never seen the language, a person or a model, can learn it this way, one word at a time.
The question line is reserved: every language accepts it, and its verb ? is the same in every language. The verb is declared once, in the meta body, and in no other body: a body that declares a form with the verb ? is refused when the declaration is read, with E_DECLARATION of kind duplicate and ? as actual (see Diagnostics).
A question is never counted, never ordered and never part of a program's meaning. check takes no term from a question line in a program and passes over it without answering it. A malformed ? line, such as one whose word fails the verb class (see The asked word is checked first), is an error with the code E_QUESTION, which covers the whole line and whose data carries the field actual. A host that answers a program's question lines runs ask for each word.
The question line is accepted wherever a line is read, in a declaration file as in a program. The reader of a declaration passes over a well-spelled question line, inside or outside a body: it declares nothing, is not reported and is not answered there. A question line whose word fails the verb class is E_QUESTION, covering the line, in a declaration file as in a program; it is an error of the declaration, so ask over that file prints the refusal (see Lines and JSON).
The question is asked from the command line with ask, which takes the declaration file, the language and the word (see Command line):
typify ask languages.typify Catalog itemIn text mode ask prints the answer lines. With --json it prints the answer as one JSON object (see Lines and JSON). ask reads the asked language alone: a word declared in another language of the same root is undeclared in the asked one.
A declared word that is a verb or a constructor is answered with the lines that declare it, its language's file line and a missing line. Each line is written under the reserved verb, as ? : <line>, and the lines come in a fixed order, whatever order the declaration uses.
The answer's forms are those whose verb is the asked word and those whose does line builds the constructor the word names, each once, in declaration order. Its constructors are the one the word names and those its forms build, each once, in declaration order. With one constructor, the lines come in this order:
- the
formline of the answer's first form; - the language's
fileline, when the language has one (see Trees); - that form's
doesline, then itsrestricts!line when it has one:restricts!follows a form and names no constructor, so it prints with its form; - each further form of the answer, in declaration order: its
formline, then itsdoesline, then itsrestricts!line when it has one; - the
termline, or theprimitiveline when the constructor is a primitive; - one
isline per class of the placeholders that thedoeslines name, the forms' and those of thefiletemplate (see Template placeholders in terms), each once, in the order of their first naming; a class that those classes reference is not followed, and is asked by its own name; - the constructor's lines: every line of the body whose first name after the introducer is the constructor,
writes,reads,guard,effect,stage,scope,fueland, for a fact constructor,fact, in declaration order, whatever its kind, so aguardon a rewritten or fact constructor prints here (see Primitives and state); acellline is not among them, and prints only in the answer to the cell's name (see A word that no form builds); - the
rulelines whose left side is the constructor (see Rules and plans); - the constructor's
ensurelines; - the
examplelines; - the
countlines: the unkeyedcountline, orcount : <constructor> = *when the body declares no unkeyedcountfor the constructor, as? : count : volume = *is for the Shelf language below, then the keyedcountline when one exists; with only a keyed count, the unkeyed bound is*; - the
missingline, which states in one word what happens when a program holds no term of the constructor, by any form:? : missing : default,? : missing : refusedor? : missing : empty(see What a missing line does); - the
defaultlines of the constructor, under any form that builds it, once each, in declaration order, which state the defaults themselves; - last, the
notelines attached to the constructor, each writtennote : <constructor> = <text>: a note records what v1's model does not check, such as a host precondition (see Writing a consumer host).
A form line and its does line are a pair. The pairs come first, in declaration order, the file line after the first form line, and the rest of the answer follows once: a constructor built by two forms is answered with both pairs, then one term line and the lines after it. A form joins the answer by its verb, or because its does line builds the constructor the word names: in Mounts the form share-read builds share, so its form line is part of the answer to ? : share (see Rules in the answer). Asked share-read, which names no constructor, Mounts answers the form share-read with its does line, then the lines of share, the one constructor that form builds; the form share is not part of that answer.
With more than one constructor, the pairs and the file line come first as above. The lines from the term or primitive line through the ensure lines are then printed for each constructor in turn, in the constructors' declaration order, each constructor with its own is lines: those of the placeholders that its own forms' does lines name, each once within its group, in the order of their first naming, so a class that two constructors name prints in each group; the example lines print once, in their place, in form order; later the count, missing and default lines are printed for each constructor in turn, in the same order, and last the note lines, for each constructor in turn. A word is answered by more than one constructor when, for example, it is the verb of a form that builds one constructor and the name of another.
The answer has no status line: its first line already says that the word is declared.
In the Catalog language (see Examples), 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 file line comes second, and the term and is lines come after the does line, although the declaration has them before the form: the order is set by the kind of line, not by the declaration. The line ? : missing : refused says that a Catalog program that holds no item term fails, because the count of item is 1..*.
The reader is closed-world: a line that is not in a program builds no term, and the reader never guesses one. Defaults are activated per constructor: when a program, present or absent, holds no term of a constructor by any form, every default line of that constructor's forms is added, in declaration order, after the program's own terms, with an origin of kind default; a present line of the constructor suppresses them all. Activation precedes the count check, so a default satisfies count = 1. An absent optional program holds only its defaults; when its language has no default line at all, the program is absent-empty and no count is checked for it (see Trees). The missing line names one of three effects in one word, and when the word is default, the answer's default lines that follow it state the defaults themselves (see Terms and counts):
| Answer line | Effect when the program holds no term of the constructor |
|---|---|
? : missing : empty |
No term, and nothing fails. |
? : missing : refused |
The program fails with the code E_PROGRAM_COUNT, because the unkeyed count of the constructor requires a term. |
? : missing : default |
Every default line of the constructor's forms is added: its terms follow the program's own terms, in declaration order, with an origin of kind default, which names the declaration file and the line of the default line, and count toward count. |
The missing line reads the word's constructor: default when the body holds a default line for it under any form that builds it; refused when its unkeyed count requires at least one term and no default exists; empty otherwise, since a keyed count alone requires nothing. required! governs the program file under tree, never a word's missing line: Catalog is required!, and the missing line of item says refused because of its count, 1..*.
In the Shelf language (see Trees), the form access is marked restricts!: its absence would fail open, so its constructor needs an unkeyed count of 1 or 1..*, or a default line. It carries a default line:
#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!
With DECLARATIONS standing for the declaration file that holds Shelf, typify ask DECLARATIONS Shelf access prints:
? : form : access : <state>
? : file : shelves/<shelf>/shelf.typify
? : does : access <state>
? : restricts!
? : term : access : state
? : is : state = "open" | "closed"
? : example : access : open
? : count : access = 0..1
? : missing : default
? : default : access : closed
The missing line states the case, default, and the body's default line for access, which follows it, states the default itself. For a Shelf program that holds no access term, the default line access : closed is added: its term follows the program's own terms, with an origin of kind default. A program with an access line gets no defaulted access term. Shelf is optional!, so an absent Shelf program holds only the term of access : closed.
The body declares no count for volume, and no default line follows its form. typify ask DECLARATIONS Shelf volume prints:
? : form : volume : <label>
? : file : shelves/<shelf>/shelf.typify
? : does : volume <label>
? : term : volume : label
? : is : label = /[A-Za-z0-9 ]+/
? : example : volume : Algebra
? : count : volume = *
? : missing : empty
The count line carries the default *. A Shelf program that holds no volume term gets no defaulted one, and nothing fails.
Because a rule is part of the answer, the asker learns what a form does, not only its shape. In the Mounts language (see Rules and plans), two forms build share: the form share, with the line does : share <path> rw, and the form share-read, with the line does : share <path> ro. The answer to ? : share opens with both forms, each form line with its does line, in declaration order. share is also rewritten by a rule and carries an ensure line, so the answer holds its rule line after its term and is lines, then its ensure line:
? : rule : share <p> <m> -> mask (parent <p>) ; bind <p> <m>
? : ensure : share : child-visible = (visible mounts <1>)
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, the first of them first. A constructor that no form builds, a term or a primitive, is answered by its term or primitive line, the file line when the body declares one, the is lines of the classes its signature names, each once, in signature order, then its own lines, its rule lines, its ensure lines, its count lines, its missing line, its default lines and last its note lines, in the order above. The file line prints only in an answer to a verb or a constructor. A class's answer is its is line alone, with no file line. In the Catalog language, category is a class that no form builds, so typify ask languages.typify Catalog category prints:
? : is : category = "book" | "journal"
A cell's answer is its cell line, then the join lines of the table it names, with no file, count, missing or default line. In the Mounts language, whose cell mounts names no join table, ? : mounts is answered:
? : cell : mounts = shadow stacking
The three built-in classes, letter, canonical-path and relative-path, are declared by no body. No class, term, primitive or cell may be declared under their names, but a form's verb may be spelled like one: that verb is a declared word of its body and a candidate like any other, and its answer holds no is line for the built-in class. So each of the three spellings is undeclared, and never a candidate for near, in a language none of whose forms has it as its verb.
In the Mounts language, mask is a primitive that no form builds: only the rule of share produces it. Mounts has no file line, so ? : mask is answered:
? : primitive : mask : path
? : is : path = canonical-path
? : writes : mask : mounts = put 1
? : count : mask = *
? : missing : empty
Its signature names the class path. The body declares no count for mask, so its count line carries the default *, and no default line, so its missing line says empty (see Primitives and state).
One spelling may be declared both as a verb or a constructor and as a class or a cell; the shared spelling is not refused. Its answer is the union of its answers, in this order: the forms and constructors, as A declared word orders them, then the class's is line, then the cell's cell line and its join lines. A line already printed is not printed again. So a word that is both a verb and a class is answered by its forms and their constructors' lines, then by its is line.
A word that nothing in the asked language declares is answered ? : <word> = undeclared, then by its near lines, ? : near : <word>, which name declared words close to the asked one (see Near words), and nothing more. The reader does not guess what was meant. An undeclared word is an answer, not an error: ask reports no diagnostic for it and exits with status 0. typify ask languages.typify Catalog itme prints:
? : itme = undeclared
? : near : item
When no word is near, the status line is the whole answer. typify ask languages.typify Catalog price prints:
? : price = undeclared
The near lines come from the asked language's candidates, even when another language of the same root declares the word.
The candidates are the verbs of the asked body's forms and the constructors of its term and primitive lines, each once. Classes and cells are never candidates. The meta body's words are candidates only when Typify is the asked language, and then, as for any body, they are its form verbs and the constructors of its primitives (see Asking the meta language). Catalog has one candidate, item; Mounts has four, share, share-read, mask and bind. A bang form, declared form : <verb>!, is declared under its verb without the !: that word is its candidate and the word to ask, and the word with the ! fails the verb class and is E_USAGE (see The asked word is checked first).
The distance is the optimal string alignment distance, over code points: inserting, deleting or substituting one code point, or exchanging two adjacent ones, costs 1, and no substring is edited twice. So ca is 3 from abc, not 2: exchanging c and a and then inserting b between them would edit the exchanged pair a second time. Case is significant: Item is one substitution from item.
A candidate lies within the bound when its distance from the asked word is at most max(n, 3) // 3, capped at 2, where n is the number of code points in the asked word and // divides and rounds down:
| Code points in the asked word | The bound |
|---|---|
| 1 to 5 | 1 |
| 6 or more | 2 |
A short word is held to one edit; a longer one is allowed two, and never more. Counting an exchange as one edit matters most for short words. Under plain Levenshtein distance an exchange costs two, so itme, four code points with a bound of 1, would be 2 away from item and outside the bound.
The near lines are chosen by the first of these rules that yields a word:
- When a candidate lies within the bound,
nearholds the candidates at the least distance, one? : near : <word>line apiece, in code-point order. Ties are not broken: every candidate at that distance is named. A candidate farther away is not named, even within the bound. - When none does,
nearholds the candidates that share a hyphen part with the asked word exactly, in code-point order. A word's hyphen parts are its non-empty runs between hyphens, and a word without a hyphen is one part:share-readhas the partsshareandread. - When neither rule yields a word, the answer has no
nearline, and the object'snearis empty.
Asked of Mounts, read has four code points, so its bound is 1, and no candidate is within one edit of it. Its one part, read, is a part of share-read, so the second rule names it:
? : read = undeclared
? : near : share-read
Asked of the Mounts language, here is the verb share-read with two pairs of adjacent letters exchanged:
? : shaer-raed = undeclared
? : near : share-read
The asked word has 10 code points, so its bound is max(10, 3) // 3, which is 3, capped at 2. Its distance from share-read is 2, one for each exchange, so share-read lies within the bound. No other candidate of Mounts is within 2; the next nearest, share, is 5 away. A candidate lies within the bound, so the hyphen parts are not consulted. Under plain Levenshtein distance each exchange would cost two and the distance would be 4, past the bound; the asked word's parts, shaer and raed, are parts of no candidate, so the answer would have no near line.
In v1 an undeclared word is answered by its status line and its near lines alone. The object's accepts member is always the empty array, and no ? : accepts line prints; accepts and the fixes of diagnostics are deferred to 1.1, and the member is kept so that 1.1 changes no shape.
In the Shelf language above, open is a literal of the class state, not a declared word. It has 4 code points, so its bound is 1. Neither candidate of Shelf, access or volume, is within one edit of it or shares a part with it, so nothing is near. typify ask DECLARATIONS Shelf open prints:
? : open = undeclared
A word the asked language declares is answered from its declaring lines, whatever its spelling. Any other word is checked against the verb class, /[A-Za-z0-9][A-Za-z0-9-]*/, before the answer repeats it: an ASCII letter or digit, then any number of ASCII letters, digits and hyphens. The same class governs the verb of a bang line (see Language). A word that fails the verb class is never echoed: ask reports E_USAGE, with file the empty string "", range null and the word as written as actual, and exits with status 2. Under --json it prints the object {"ok": false, "diagnostics": [...]}; in text mode it prints the diagnostic's projection, as every command does, and no answer line (see Command line).
The class tests a word's spelling, never its kind: the name of a class, a cell or a primitive is asked like any other word, and a declared one is answered by its declaring lines (see A word that no form builds).
A class's language is its pattern's language intersected with the allowed code points, so no class matches a control character (Cc), a format character (Cf, the zero-width joiner included), a line or paragraph separator (Zl, Zp), or any space separator (Zs) other than U+0020 (see Classes), and an answer cannot carry such a character back to the asker.
Every answer line has the verb ?, and the words undeclared, near and missing appear only after it. A language may declare its own verb near or missing; its lines never read like answer lines.
One answer is printed in two forms: as the Typify lines shown on this page, and, with --json, as one JSON object of this shape: {"word": "item", "status": "declared", "lines": [...], "near": [...], "accepts": [...], "diagnostics": [...]}. The status member is declared or undeclared. The lines member holds every answer line as a string; near holds the near words, and accepts is always empty in v1 (see No menu of forms). The diagnostics member carries what reading the declaration reports, notes included, and is an empty array when nothing is reported; in text mode the notes' projections print before the answer lines. A declaration with an error, or a file that cannot be read, gets no answer: under --json ask prints {"ok": false, "diagnostics": [...]}, with no answer fields, and in text mode the diagnostics' projections, as every command does, and no answer line; it exits with status 1 or 2 (see Exit status and JSON). A word that fails the verb class gets the same object, with status 2.
typify ask languages.typify Catalog itme --json prints (the reader prints JSON compactly with object keys sorted; the block below orders and spaces them for reading):
{"word": "itme", "status": "undeclared", "lines": ["? : itme = undeclared", "? : near : item"], "near": ["item"], "accepts": [], "diagnostics": []}A round-trip test parses every string of lines as a line under ?, rebuilds the status line and the near lines from word, status and near, and requires them equal to the printed lines, so both forms carry the same answer.
No diagnostic of v1 offers a fix: every diagnostic carries fix null, and none carries near words; near is printed by ask alone. A program line whose verb is the typo itme is E_FORM_NONE, with no fix; asked about itme, ask names item.
The meta language is named Typify, and the name is reserved: a declaration file whose text is the reader's own meta source, typify.typify, byte for byte, is the meta body itself, and any other body declared Typify is refused with E_DECLARATION of kind duplicate, with Typify as actual. typify ask DECLARATIONS Typify WORD answers from the meta body, after the declaration file is read, as every command reads it first (see Every error, in order); Near words says which words are candidates when Typify is the asked language. Asked of it, ? : form is answered from the reader's own table of meta forms, the table whose grammar typify emit Typify prints and whose declaration source the release build writes as typify/share/typify.typify (see Emission).
The meta body declares the question itself, in these lines:
is : verb = /[A-Za-z0-9][A-Za-z0-9-]*/
form : ? : <verb>
does : question <verb>
primitive : question : verb
effect : question = external
example : ? : form
The class verb is the verb class. check takes no term from a question line, so no program builds a question term, and a host performs the question by running ask. The meta body declares ?, so ? asked of Typify is answered although it fails the verb class. Over any declaration file that reads with no diagnostic, a note included, quoting the ? so that the shell does not expand it,
typify ask DECLARATIONS Typify '?'prints:
? : form : ? : <verb>
? : does : question <verb>
? : primitive : question : verb
? : is : verb = /[A-Za-z0-9][A-Za-z0-9-]*/
? : effect : question = external
? : example : ? : form
? : count : question = *
? : missing : empty
The answer is a self-representation: the language's self-declaration, written in the language from the reader's own table. It is not a self-interpreter. No Typify text interprets Typify; the reader, in its host, interprets every level, the meta language included.
A program's author may be a stranger to the language, or a frozen model that cannot be retrained on it. Either one learns the language from its answers. An answer is made of the declaration's own lines, so what the asker learns is what the reader checks, the rule and the effect of a missing line included. An undeclared word is called undeclared and is never guessed at. A model receives the answer lines, and the JSON form is there for a program that consumes it.