Nilo

Journal12

One way to write a call

A long navy and straw ribbon falling from the top of a pale sheet and gathering in a loose knot of twisted loops, its back showing wherever it turns over

Ribbon: one long ribbon let fall from above, gathering in loops and turning over as it goes, so that its back shows. It is 43 lines.

Entry 11 ended on something unfinished. Calls in Nilo give the first value bare and name the rest, and the pieces and tests had been brought into that style, but the compiler still took the old ways without a word.

Now it refuses them. I chose the rule. A model wrote it into the compiler, as it writes the rest. This entry is what happened when it was switched on.

What it says

A call written any other way is refused, and the message writes the call out as it should be, in my own names. For colour.mix(ramp[3], #fbf8f1, 0.6):

colour.mix takes the values after the first by name: colour.mix(ramp[3], toward: #fbf8f1, amount: 0.6)

That is the draft entry 11 showed. It is now what the compiler prints.

A first value given a name is refused the same way. For noise.at(field: field, point: p):

noise.at takes its first value first, without a name: noise.at(field, point: p)

Built-in functions take no names at all. Entry 10 ended with clamp(lo: 1, hi: 9, x: 5) quietly giving 9 where 5 was meant. Now it gets:

clamp takes its values in order, unnamed: clamp(5, 1, 9)

That one comes off the list of things still wrong.

The rule holds in the sketchbook too. There, some of the rules of a finished piece are only warnings, so that I can draw first and tidy later. I decided against a warning for this one. A warning would keep the old way of writing a call alive, just in a corner.

Rolls that move

There is a trap in writing the call out as it should be. The values of a call are worked out in the order it writes them, and some of them are rolls from the token. Move one to the front and the rolls come in a different order: same token, different art.

spot(r: layout.float(4 ..< 40), x: layout.float(0 ..< 1920))

Here the size is rolled before the place. The corrected call puts the place first, so the message says so:

spot takes its first value first, without a name: spot(layout.float(0 ..< 1920), r: layout.float(4 ..< 40)) (moving the first value to the front changes the order the values are worked out in, so values that roll may come in a different order)

Moved past a plain number, a value changes nothing, and nothing is said.

Note the “may”. The first version said the order would change, flatly. A reviewer showed that this was sometimes false: hand the call a function of the piece that never rolls, such as fn jitter(x: num) = x, and the note still claimed it. The compiler does not look inside a piece’s own functions to see whether they roll. Until it does, “may” is the honest word.

The same reviews found that built-in functions gave no note at all. A clamp written with three named rolls out of order was written back in order, which changes the order of the rolls, without a word. It has the note now.

The program that is gone

The pieces and tests were brought into the new style by a program, not by hand. Entry 11 told how it was built and held to account. With the rule switched on, it had a problem: to read old code at all, it needed a second, looser way of reading calls, kept beside the strict one. That is the old way, kept alive inside the compiler.

Its job was done. Everything that existed when it ran had been converted: the pieces, the tests, the libraries and the reference. So it was deleted: eight files, 3,445 lines with its tests, and the parts of the compiler’s reader that only it used.

If I find old code now, the compiler tells me what to write, one call at a time. That is the same choice as the libraries in entry 11. This is a new language, with no one to keep an old way for.

What reviewing caught

Before the rule went in, reviewers attacked it, three rounds of them. The rule itself had holes.

  • A layer still took a bare value after a named one. layer wet(seedA: 7645, 12, seedB: 9063) compiled. The layer gave the bare 12 to its first value, push, wherever it was written. The check then saw one bare value and no name for push, and was satisfied. It also worked out seedA before push, so with values that roll, the order would have changed without a word. Now it gets the same message as any other call:

    wet takes its first value first, without a name: wet(12, seedA: 7645, seedB: 9063)
  • A function handed in took names. Say a function has a parameter keep of type fn(vec, num): bool, and the piece hands it inside(q: vec, limit: num). Then keep(q, limit: 300) compiled. The compiler turns such a call into a call of the function handed in, and inside happens to have a value called limit. But a function type has no names, so the call worked or failed depending on which function was handed in. Names there are now refused:

    keep takes its values in order, unnamed: keep(vec, num)

And the messages, meant to be the helpful part, sometimes gave advice the compiler itself would refuse.

  • A library call inside another call came back with names that have a $ in them, which only the compiler uses and no piece can write.
  • A call written inside a library was corrected the way a piece would write it, with the library’s name in front. The library cannot write that.
  • The hint for handing loop time to a function suggested sway(t: t), which the new rule refuses. It now says sway(t).

A message that tells me to write something the compiler then refuses is worse than no message. There is now a test that takes the corrected call out of the messages, puts it where the old call was, and builds it.

One of mine

Then the rule found something of mine. field, the piece at the top of entry 6, was written after the conversion had run, so nothing had converted it. One line was in the old form, and nothing had objected. Switched on, the compiler refused it:

field.n:40: colour.hsl takes the values after the first by name: colour.hsl([0.75, 0.08, 0.53, 0.95][ink.int(0 ..< 4)], s: 0.7, l: 0.8)

Two listings in this journal had the same fault: field’s own, in entry 6, and a short one in entry 5. The piece and both listings are corrected, and field builds to the same bytes as before.

That is the whole case for the rule. Without it, the old way comes back one new piece at a time.

What it cost

Nothing that ships. Every one of the 228 builds in the byte comparison came out the same, byte for byte.

Of the recorded test results, 20 changed, and every one is about how a call is written: a message, a hint, or a test program written the old way. Two of the test programs behind them went from built to refused, both written the old way on purpose.

Beside it

The search for the smallest file now runs side by side. It builds a piece many ways, 122 for the daily piece, and keeps the smallest, and it used to build them one after another. Now a pool of workers builds ahead, while the old loop still judges each build in its own order, so the winner is the same build whichever worker finishes first. Checked against the old compiler on 10,660 builds, with one worker, with three and with all of them: the same, byte for byte.

PieceBeforeAfter
hills1.5 s0.35 s
daily9.5 s1.6 s

Entry 9 expected it to be several times quicker, and it is. It costs memory, about 73 MB at most for the daily piece where it was 21, and about a quarter of the builds are for guesses that turn out wrong.

What is not right yet

  • A call with two mistakes at once can still get a wrong correction. f(b: 1, 2), for a function of three values, gets this, which still leaves one out:

    f takes its first value first, without a name: f(2, b: 1)

    The next build says which value is missing.

  • The roll note says “may”, because the compiler does not look inside a piece’s own functions.

  • Only code that is compiled is checked. A call inside a function that nothing calls is not looked at until something calls it.

Next

  • Fix what is still on the list.
  • Then the editor support.

The piece at the top

The image at the top comes from this token, built for the svg target: 0x4992a2f9199f69113ad0e932d18151eead94b7faa571929bbd22cfdc7e949a46

// ribbon: a long ribbon let fall from above, gathering in loops and turning over as it goes
plate(1920, 2400)
use colour v2
random layout, fall

// a front and a back, one pair of four
let pairs = [#7a1f2b, #ecd2c0, #1d3557, #e2cf9a, #2f4a3a, #e9dcc4, #3a2a4d, #edbfa8]
let pick = layout.int(0 ..< 4) * 2
let front = pairs[pick], back = pairs[pick + 1]
let centre = vec(layout.float(880 ..< 1040), layout.float(1350 ..< 1550))
let reach = layout.float(420 ..< 560)
let half = layout.float(50 ..< 100)               // half the ribbon's width

// its two edges, step by step, and how squarely it faces up (below zero, its back is up)
let left = vec[], right = vec[], face = num[]
var at = vec(centre.x + layout.float(-250 ..< 250), -150), heading = PI / 2
var turn: num = 0, spin: num = 0, twist: num = 0
for i in 0 ..< 1300 {
  // how sharply it turns holds for a while, then changes; how fast it twists drifts, and settles back
  if fall.chance(0.04) { turn = fall.float(-0.04 ..< 0.04) }
  spin = spin * 0.97 + fall.float(-0.02 ..< 0.02)
  // when it strays, it turns back in toward the middle, the shorter way round
  let dir = polar(heading, 1), away = at - centre
  if len(away) > reach && dot(dir, away) > -0.6 * len(away) {
    heading -= 0.07 * (dot(perp(dir), away) > 0 ? 1 : -1)
  }
  heading += turn
  twist += spin
  at = at + polar(heading, half / 7)
  left.add(at + perp(dir) * half * cos(twist))
  right.add(at - perp(dir) * half * cos(twist))
  face.add(cos(twist))
}

background(#efe9de)
// a soft shadow, faded as one so that where it crosses itself it is no darker
place(vec(16, 24), opacity: 0.16) {
  for i in 1 ..< left.len { polygon([left[i - 1], right[i - 1], right[i], left[i]], fill: #2b2420) }
}
// darker where it turns edge on, a sheen where it lies flat
for i in 1 ..< left.len {
  let lit = abs(face[i])
  let tone = colour.darken(face[i] > 0 ? front : back, amount: 0.6 * (1 - lit))
  let ink = colour.lighten(tone, amount: 0.25 * pow(lit, 8))
  polygon([left[i - 1], right[i - 1], right[i], left[i]], fill: ink, stroke: ink, width: 1)
}