Skip to content

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.

After the base
  1. 0BaseThe formula kit: 12 kinds and 13 evaluators.
  2. 1StatisticsThe kind mean, the alias avg, the operation ops.twice and the shared kind show.
  3. 2TextThe kind upper, the operation ops.join and the shared kind show. It does not know the statistics.
  4. 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

Path0 Base1 Statistics2 Text3 Rival statisticsThe 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
opsnamespace0 keys, fallbacknamespace1 keynamespace1 keynamespace1 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·codeshowcodeshow· 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
Each row is one path. A dot means that the part has no entry there. Equal entries give one entry, and namespaces merge key by key. Each other clash is a conflict, until the later part names the path in replaces.
  • 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 namespace ops, and the namespaces merge key by key. The fallback of the base stays.
  • The rival gives a different mean and a different ops.twice. Select Mark each path in replaces. The rival wins at each path, and avg gives the median.
  • Select Restrict it on the row mean. The alias avg points 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.

mergeWorlds and mergeMachines use the same rules (decision D17).

  1. A new entry joins the result.
  2. Two entries that are equal give one entry. A function is equal only to itself.
  3. Two namespaces merge key by key, at each depth. Their fallbacks merge as other entries do.
  4. Each other clash at one path is a conflict. The merge goes on, and it collects each conflict.
  5. At the end, one MergeError lists each conflict, with its path, its two parts and a reason.
  6. A later part that names the path in replaces wins 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.

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 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.