The law of staves
A word is code if and only if it carries the alliteration. Everything else is ornament, there for sense and sound. The compiler holds you to it in both directions:
- a program word that does not alliterate is an error,
- so is an unknown word that does.
In the editor, program words are red, gods who only lend a stave are blue, and small words that act are underlined. Hover over any word, or press F1 with the cursor in it, to see what it does, which words it takes from and gives to, and, after a run, the values.
Debug runs a poem word by word and shows the stack, the nouns and the parts under way. s steps, o steps over a part or a map, b goes back, c continues to a breakpoint, Esc ends.
Where it comes from
The verse is that of Hávamál, the sayings of the High One; mál also means language. The programming is inspired by Joy, where a program is words in a row working on a stack of values, with lists as values. Arithmetic on whole lists, and small words that change the next word, come from APL. We use Kjarni as our intermediate representation.
A stanza
Six lines, two halves. Each half is a long line split over two lines, and a full line that alliterates with itself. A poem may open with one line that names it, after §: § Hávamál 5. Nothing else in a poem is outside the verse.
Greiðir sá best
er greinir á milli,
Sif ann þeim er safnar;
kveður sá satt
er Kvasi treystir,
þó mun Dvalinn deyja.
- Long line: one or two staves in its first part, exactly one in its second.
- Full line: two or three staves.
- Sounds: all vowels and j alliterate together. sp st sk sl sm sn each only with themselves. hv goes with k.
- Gods carry a stave and do nothing, at most one to a long line or a full line. They are listed under Names.
- Small words (og, er, sá, skal, þó…) never count.
How it runs
Words act in reading order on a stack. The stanza above, given two lists of numbers as a pair:
| Greiðir | unpack the pair |
| greinir | absolute differences |
| safnar | sum |
| kveður | print |
| deyja | halt |
Any inflected form will do: safna, safnar, safnaði, safni. Each operation has several synonyms, its heiti; find one with the right initial under Heiti. Arithmetic and comparison work on whole lists.
Numbers
Cardinals push a number: þrír, fimmtíu, hundrað. Ordinals take an element, counting from 1: fyrsti, annar, þriðji.
Nouns are variables
The indefinite form takes the top value and names it; the definite form gives it back. Garð skal reisa og garðsins vitja binds, then reads. A noun bound in the main program is shared with every part. A noun a part binds is its own, and a noun it reads without binding is its caller's. The nouns are listed under Names.
Small words that act
| hver, hvor (any form but hvort) | map the next program word over a list |
| sem | filter by it |
| æ | fold with it |
| ey | scan: a fold that keeps every step |
| ekki, eigi, skalat, erat | negate its answer |
The word they act on must come in the same half.
Choice and repetition
A half that opens with Ef is an if: the long line is the condition, the full line the consequence. If the next half opens with En, that whole half is the else. Meðan repeats its full line while the long line holds; Unz repeats until it does.
Ef hermir „out“
hæfir gesti,
afneitar einn gumi;
en brunninn skal býta
og benda á,
hverjum skal sæ safna.
Parts
A heading such as Dómsþáttur opens a part named by the noun dómur. Any form of that noun then calls it. Stanzas before the first heading are the main program.
To filter or map with a local noun, put the test in a part of its own that reads the noun: þeim sem vík keeps what Víkurþáttur answers 1 for.
Memory: Muninn
Add Munins to a heading, after Óðinn's raven of memory, and the part remembers its answers: Fossþáttur Munins. When it is called again with a value it has seen, it gives back the answer at once, without running. Use it for a recursive part that meets the same values many times, such as counting paths through a grid. The example 2024-10a does this. In Kjarni, start the part with memo foss:.
- It takes one value and leaves one. For two or more, pass a pair or a list.
- The value is the key to its memory: a number, a text, or a list of these. A map or a queue is an error.
- It may read shared nouns, but not its caller's. It may not print, read input, halt or bind a shared noun, and nor may any part it calls.
- Binding a shared noun anywhere makes every part forget.
Characters
Some names, in the genitive, are a character: Hárbarðs is -, lyngorms is @. In other cases they are gods like any other. A compound in -rún is its first letter: limrún is l, limrúnin is L. Longer text goes in „…“, outside the metre.
Dísir and shape-shifters
A dís is a priority queue: dýrka or blóta puts a value in, kalla or vekja takes out the smallest. Lists compare item by item, so queue pairs of cost and place.
For bits, hamskipti is xor: shift shape twice and you are yourself again. Svaðilfari is and, what the parents share; Sleipnir is or, what the foal gets from either.
Filler that reads well
When a half needs a line with nothing to do, undo yourself: geymir maðr og gleymir (copy, drop), neitar sá glaðr er neitar aftur (negate twice, for numbers), sundrar sá er sameinar (unpack, pair).
Prototype in Kjarni
Tick Prototype in Kjarni to write the program first as Kjarni, the plain form the verse compiles to, beside the poem. Hover a Kjarni word to see the heiti that say it in verse, grouped by stave. Above the editor, the planner says how many stanzas the program needs, or where no stave fits. Run and Debug act on the side you choose; Compare runs both on the input and says whether they print the same. ← Poem as Kjarni writes the poem as Kjarni, one line for each line of verse.
In Kjarni, words run left to right on a stack and are named as under Heiti. 5 pushes a number and "ab" a text. #2 takes element 2. >x names the top value and $x gives it back. f() calls a part, which part f: or memo f: starts. map: filter: fold: scan: not: act on the next word. if( cond | then | else ), while( cond | body ) and until( cond | body ) choose and repeat. A line that starts with ; is a comment.