Composition
A language in coeval is a machine: a world of kinds, and the code of their evaluators. A host builds a language from parts with mergeMachines. The parts do not know each other.
In this diagram, the base is the formula kit of the kernel. Three small languages can join it. The merge goes path by path, and each row is one path.
- 0BaseThe formula kit: 12 kinds and 13 evaluators.
- 1StatisticsThe kind mean, the alias avg, the operation ops.twice and the shared kind show.
- 2TextThe kind upper, the operation ops.join and the shared kind show. It does not know the statistics.
- 3Rival statisticsA different mean (the median) and a different ops.twice (three times). Alone, it composes with the base.
The call
const machine = mergeMachines(base, STATS, TEXT, RIVAL);
The merge, path by path
| Path | 0 Base | 1 Statistics | 2 Text | 3 Rival statistics | The merge |
|---|---|---|---|---|---|
| The world: kinds, aliases and namespaces | |||||
| mean | · | kind, eval bystats.meanThe mean of its terms. | · | kind, eval byrival.medianThe median of its terms. | ConflictTwo parts give two different kinds. |
| avg | · | alias ofmean | · | · | New entry |
| ops | namespace0 keys, fallback | namespace1 key | namespace1 key | namespace1 key | Key by key |
| ops › kinds › twice | · | kind, eval bystats.twiceTwo times its term. | · | kind, eval bystats.twiceThree times its term. | ConflictTwo parts give two different kinds. |
| ops › kinds › join | · | · | kind, eval bytext.joinIts terms as one text. | · | New entry |
| show | · | kind, eval byfmt.showIts term as a text. | kind, eval byfmt.showIts term as a text. | · | Same entry |
| upper | · | · | kind, eval bytext.upperIts term in upper case. | · | New entry |
| The code: evaluators | |||||
| stats.mean | · | codemean | · | · | New entry |
| stats.twice | · | codetwice | · | codethrice | ConflictTwo parts give two different evaluators for this name. |
| fmt.show | · | codeshow | codeshow | · | Same entry |
| text.upper | · | · | codeupper | · | New entry |
| text.join | · | · | codejoin | · | New entry |
| rival.median | · | · | · | codemedian | New entry |
| The base also gives the other kinds and evaluators of the formula kit. No language gives an entry at their paths, thus the merge keeps them. | |||||
MergeError The merge has 3 conflicts. The host gets it when it builds the machine, before a document evaluates.
["world","kinds","mean"]Parts 1 and 3: Two parts give two different kinds.["world","kinds","ops","kinds","twice"]Parts 1 and 3: Two parts give two different kinds.["evaluators","stats.twice"]Parts 1 and 3: Two parts give two different evaluators for this name.
Three documents, evaluated with this machine
{"type":"avg","value":[1,5,6]}No machine{"type":["ops","twice"],"value":[2]}No machine{"type":["ops","join"],"value":[{"type":"upper","value":"ab"},"=",{"type":"avg","value":[1,5]}]}No machine
replaces.Try these
Section titled “Try these”- Look at the row
show. Statistics and Text give the same kind with the same code. The merge keeps one entry. - Look at the row
ops. Each part gives a namespaceops, and the namespaces merge key by key. The fallback of the base stays. - The rival gives a different
meanand a differentops.twice. Select Mark each path in replaces. The rival wins at each path, andavggives the median. - Select Restrict it on the row
mean. The aliasavgpoints to a kind that is not there, thus it gives#KIND?. - Select Rival statistics to remove it from the merge. Statistics and Text compose without glue, and the last document uses both.
The rules of the merge
Section titled “The rules of the merge”mergeWorlds and mergeMachines use the same rules (decision D17).
- A new entry joins the result.
- Two entries that are equal give one entry. A function is equal only to itself.
- Two namespaces merge key by key, at each depth. Their fallbacks merge as other entries do.
- Each other clash at one path is a conflict. The merge goes on, and it collects each conflict.
- At the end, one
MergeErrorlists each conflict, with its path, its two parts and a reason. - A later part that names the path in
replaceswins at that path. A mark on the earlier part does not replace a later entry.
The path of a conflict is the mark that resolves it. Thus a host can read the error, decide which part wins, and write the paths into replaces.
A part can also be a function of the merge before it. Then it can read the entry that it replaces, and wrap it.
A restriction removes entries
Section titled “A restriction removes entries”restrictWorld and restrictMachine remove entries by path, with the same paths as replaces (decision D18). A removed kind gives #KIND?, also through an alias. Remove the code of a name too, because an own entry names an evaluator directly.
The modes of composition
Section titled “The modes of composition”The modes come from Erdweg, Giarrusso and Rendel (2012), through the study R6. Each mode has a spec test in packages/core/src/compose.test.ts.
| Mode | What it does | Today |
|---|---|---|
| Extension | A language adds kinds and code to a base. The base does not know it. | It works. |
| Restriction | A language removes kinds and their code from a base. | It works, with restrictMachine. |
| Unification | Two independent languages merge, and glue joins them. | It works. Two machines that each build the formula kit conflict. |
| Self-extension | A document gives kinds as data, and the host merges them. | Kinds work. An evaluator that is a document is open work. |
| Extension unification | Two extensions of one base compose without glue. | It works, in each order. |
| Incremental extension | A language extends an extension, and wraps the entry that it replaces. | It works. |
| Conflict | Two extensions that each compose with the base conflict together. | It works. No check finds the conflict before the merge. |
The file docs/COMPOSITION.md gives each mode with its code.