Skip to content
Kaleidl

LADL · MN 112 · a training guide

The guide

LADL is a small language, and this guide teaches it in six short lessons. Each lesson states one idea in a few sentences, shows one profile or one snippet of driver code, and prints the engine's output under it. Every snippet on this page was executed against the vendored Kaleidl engine, and every output below is transcribed from those runs. The reference for the full mn112/1 contract is the specification page, and the playground runs the same engine against any profile you write.

01 The mental model

LADL models a labeling scheme as data, and a profile states the data as typed instances. Labellers say what gets a label: each one serves one element type, such as clause or note, within one region of the document. Sequences are the counters, and a labeller draws every value from one of them. Templates give the label its shape, so the same number can print as 1, as 1.1, or as Clause 1. Contexts say where a scheme applies: the content tree declares a label context association on the elements that open a scope, and their descendants are labeled by the labellers that serve that context. One profile in, labels out.

schema: mn112/1
instances:
- name: MiniStyle
  type: LabelStyle
  properties:
    name: "Mini Style"
    labellers:
    - "id:clause_labeller"
    sequences:
    - ref: DecimalNumericSequence
    templates:
    - ref: ClauseTemplate
    contexts:
    - ref: DocumentContext

- name: DocumentContext
  type: LabelContext
  properties:
    name: "document"

- name: ClauseTemplate
  type: LabelTemplate
  properties:
    pattern: "{clause} %n"

- name: ClauseLabeller
  type: Labeller
  properties:
    id: "clause_labeller"
    sequence: {ref: DecimalNumericSequence}
    context: {ref: DocumentContext}
    type: "clause"
    template: {ref: ClauseTemplate}
ElementLabelReference
c1Clause 1Clause 1
c2Clause 2Clause 2

Engine output: the profile above walked over a document of two clause elements. The pattern's {clause} token resolves to the vocabulary word Clause, and %n carries the sequence value.

Try — in the playground, change the pattern to "%n)" and predict the two labels before you run.

02 Your first profile

The working profile for the rest of this guide is the playground's Compact demo preset, a YAML realization of the mn112/1 schema. It labels clauses, subclauses, annexes, figures and notes, and the head of it is reproduced below verbatim. The style instance references every labeller, sequence, template and context the scheme uses, and the engine refuses to run a profile whose references do not resolve.

schema: mn112/1
instances:
- name: DemoLabelStyle
  type: LabelStyle
  properties:
    name: "Demo Label Style"
    labellers:
    - "id:clause_labeller"
    - "id:subclause_labeller"
    - "id:annex_labeller"
    - "id:annex_clause_labeller"
    - "id:figure_labeller"
    - "id:note_labeller"
    sequences:
    - ref: DecimalNumericSequence
    - ref: AlphaUpperNumericSequence
    templates:
    - ref: ClauseTemplate
    - ref: SubclauseTemplate
    - ref: AnnexTemplate
    - ref: AnnexClauseTemplate
    - ref: FigureTemplate
    - ref: NoteSingleTemplate
    - ref: NoteMultipleTemplate
    contexts:
    - ref: DocumentContext
    - ref: ClauseContext
    - ref: AnnexContext

# Sixteen more instances follow, every binding the same
# shape: a labeller names a sequence, a context and a
# template, and nested labellers name a parent labeller.
# The full profile is src/lib/demo.ts and the playground's
# Compact demo preset.

Engine output: loadProfile(DEMO_PROFILE) loads 17 instances — one style, six labellers, seven templates, three contexts, and no sequence instances, because the style uses built-in sequences.

Try — count the instance types in the playground's Compact demo pane and check the total against the engine output above.

03 Label a document tree

The host supplies the content tree. Every element carries a type, which selects the labeller, an id, which keys the result, an optional context association, which opens a scope, and children. The engine call is one line: new Engine(loadProfile(DEMO_PROFILE)).label(tree), and the labels come back as a map keyed by element id, each entry carrying the assignment form and the cross-reference form. The demo tree below is the playground preset's tree.

{
  "type": "document",
  "id": "doc",
  "context": "DocumentContext",
  "children": [
    {
      "type": "clause", "id": "c1", "context": "ClauseContext",
      "children": [
        { "type": "subclause", "id": "c1s1" },
        {
          "type": "subclause", "id": "c1s2",
          "children": [
            { "type": "note", "id": "c1s2n1" },
            { "type": "note", "id": "c1s2n2" }
          ]
        },
        { "type": "figure", "id": "c1f1" }
      ]
    },
    { "type": "clause", "id": "c2", "context": "ClauseContext" },
    {
      "type": "annex", "id": "ax1", "context": "AnnexContext",
      "children": [
        { "type": "clause", "id": "ax1c1" }
      ]
    }
  ]
}
ElementLabelReference
c1Clause 1Clause 1
c1s11.11.1
c1s21.21.2
c1s2n1NOTE 1NOTE 1
c1s2n2NOTE 2NOTE 2
c1f1Figure 1Figure 1
c2Clause 2Clause 2
ax1Annex AAnnex A
ax1c1A.1A.1

Engine output: the demo profile against the tree above — nine labels. The demo labellers declare no global templates, so the reference form equals the assignment form.

Try — add a second clause child to the annex in the playground and predict its label before you run.

04 Sequences

A labeller draws every value from one sequence, and the sequence decides the characters. The built-in sequences are referenced by name, and the annex labeller below binds AlphaUpperNumericSequence, so annexes number A, B, C. A profile can also declare its own sequence instance, as UpperRomanSequence does, and repointing the labeller at a declared instance changes the rendering without touching anything else.

schema: mn112/1
instances:
- name: AnnexStyle
  type: LabelStyle
  properties:
    name: "Annex Style"
    labellers:
    - "id:annex_labeller"
    sequences:
    - ref: AlphaUpperNumericSequence
    templates:
    - ref: AnnexTemplate
    contexts:
    - ref: DocumentContext

# A custom sequence is a profile-declared NumericSequence
# instance. Repoint the labeller's sequence at it and the
# same walk renders roman upper instead of letters.
- name: UpperRomanSequence
  type: NumericSequence
  properties:
    format: roman_upper

- name: DocumentContext
  type: LabelContext
  properties:
    name: "document"

- name: AnnexTemplate
  type: LabelTemplate
  properties:
    pattern: "{annex} %n"

- name: AnnexLabeller
  type: Labeller
  properties:
    id: "annex_labeller"
    sequence: {ref: AlphaUpperNumericSequence}
    context: {ref: DocumentContext}
    type: "annex"
    template: {ref: AnnexTemplate}
ElementLabel
ax1Annex A
ax2Annex B
ax3Annex C

Engine output: three annex elements under the profile above. The same labeller bound to DecimalNumericSequence prints Annex 1, Annex 2, Annex 3, and bound to UpperRomanSequence it prints Annex I, Annex II, Annex III — both verified with the same run.

Try — change the custom instance's format to roman_lower and predict the three labels before you run.

05 Conditional templates

A labeller may carry an ordered list of templates, and a template may carry a condition. The engine plans and counts first and renders afterwards, so each condition is evaluated against the final count of like-labeled siblings in the same scope. The pair below is carried by the demo profile and, with global templates added, by the ISO/IEC preset in src/lib/presets.ts. It prints a lone note as NOTE and numbers notes only from the second.

- name: NoteSingleTemplate
  type: LabelTemplate
  properties:
    pattern: "{note}"

- name: NoteMultipleTemplate
  type: LabelTemplate
  properties:
    pattern: "{note} %n"
    condition:
      type: "element_count_gt"
      threshold: 1

- name: NoteLabeller
  type: Labeller
  properties:
    id: "note_labeller"
    sequence: {ref: DecimalNumericSequence}
    context: {ref: ClauseContext}
    type: "note"
    template:
    - ref: NoteSingleTemplate
    - ref: NoteMultipleTemplate
ElementSituationLabel
c1n1the only note of its clauseNOTE
c2n1first note of a two-note clauseNOTE 1
c2n2second note of a two-note clauseNOTE 2

Engine output: the demo profile against a one-note tree and a two-note tree.

Try — add a third note to the two-note clause in the playground and predict all three labels before you run.

06 Where next

The profile you have followed is the YAML realization of mn112/1, and the same profile has a second, textual realization: LML, the LutaML instance syntax shown on the realizations section of the specification page. The Ruby reference engine reads both forms, and the TypeScript engine behind this site now reads both too: the playground accepts LML profiles, parsed in the browser by a WebAssembly parser that loads on first use. Write profiles as YAML or LML, run them in the playground, and read the full contract on the specification page.

import { DEMO_PROFILE, DEMO_TREE } from '../lib/demo'
import { Engine, loadProfile } from '../lib/kaleidl-engine'

const engine = new Engine(loadProfile(DEMO_PROFILE))
engine.label(JSON.parse(DEMO_TREE))

for (const [id, label] of engine.labels) {
  console.log(id, label.value)
}
c1 Clause 1
c1s1 1.1
c1s2 1.2
c1s2n1 NOTE 1
c1s2n2 NOTE 2
c1f1 Figure 1
c2 Clause 2
ax1 Annex A
ax1c1 A.1

Engine output: the capstone driver above, executed verbatim against the vendored engine; the pane is its console output.

Try — paste a profile of your own into the playground, break one reference, and read the error the engine raises instead of guessing.