Nilo

Journal11

The first one bare

Soft layers of blue ridges under a dark sky, a pale sun low on the left

Hills again, from another token: the piece whose longest line is quoted below.

The worst find in entry 10 was a compiler step that ignored the names on a call, took the values in the order written, and drew the wrong art.

Behind that bug was a choice. Nilo let me write a call three ways: values in order, values by name, or a mix, the named ones in any order. Three ways to write one thing is two too many, and it had just cost a bug.

The first rule

My first decision: a call to a piece’s own function names every value, every time.

Before anything was built, every piece was rewritten that way in a scratch copy. In the shipped file it cost nothing. In the source it was heavy. Nearly a quarter of all values read as a name said twice: inEll(p: p, o: o, rx: rx, ry: ry), noise.at(field: field, point: p). Twelve lines that had fitted inside 120 characters no longer did. The longest line in the hills piece went from about 160 characters to about 230.

One of the four goals of Nilo is to write less to do more. This went the other way, so the rule changed before it went in.

The rule instead

The first value goes bare. The rest are named, in any order.

colour.mix(ramp[3], toward: #fbf8f1, amount: 0.6)
colour.darken(v, amount: 0.3)
noise.at(field, point: p)
spline.sample(drag, steps: 8)

The first value is nearly always the thing being worked on, and it is obvious what it is. A function that takes one value is simply called with it.

The same rule covers library functions and the effect a layer names. Built-in functions go the other way and take no names at all: vec(1, 2), clamp(5, 1, 9).

A record made from a kind stays strict: every field, every time, no defaults. A kind says what a thing is. A function says what to do, and that is where “use this unless I say otherwise” belongs. So a function’s values may have defaults: noise.octaves(field, point: p, layers: 3) leaves out rough, which is 0.5 unless I say otherwise.

The longest line in the hills piece, before and after:

brush.sweep(marks, spline.sample(drag, 8), layout.float(110 ..< 260), [colour.darken(v, 0.3), v, colour.lighten(v, 0.25)], opacity: 0.8, density: 0.6, body: 0.4)
brush.sweep(marks, path: spline.sample(drag, steps: 8), width: layout.float(110 ..< 260),
  ramp: [colour.darken(v, amount: 0.3), v, colour.lighten(v, amount: 0.25)],
  opacity: 0.8, density: 0.6, body: 0.4)

In the first, 8 and 0.3 could be anything. The second says what they are.

Real names

With names in every call, the names a function gives its values are part of how the language reads. In the libraries they had been throwaway letters: colour.mix(a, b, k). Now they are words: colour.mix(from, toward, amount), colour.lighten(from, amount), colour.at(ramp, along), noise.at(field, point), noise.octaves(field, point, layers, rough). The brush’s source of random numbers is rolls. The same idea gets the same word in all four libraries. One word could not be used: to is already a drawing command, hence toward.

The renamed libraries are a new version of each, and the old version is gone. It did not get to live alongside: this is a new language, with no one to keep an old way for.

What it cost

In the shipped file, nothing. Names are not sent to the browser. Every piece built from the old source and the new came out byte for byte the same.

PieceBeforeAfter
currents5,4495,449
hills7,0627,062
daily14,78014,780
One real layer10,17710,177

The source grew by about 2 percent in characters, with no lines added except where a long line was wrapped.

Found on the way

The compiler carries its own copy of the curve smoothing library, so a piece need not name it. A piece that did name the new version shipped the smoothing twice. The example in that library’s notes was 1,900 bytes for that reason, and is now 1,558. A piece that smooths a curve now ships it once, whatever it names.

The tests

The hard part was the tests. Several hundred test pieces were in the old style, and entry 10 had just made it impossible, on purpose, to change a test’s input.

So a second, narrower door was built. It rewrites the test pieces itself, so nobody edits them by hand. Everything built from them must come out the same; the only differences allowed are the new names in the text. It will not run if the compiler has any unsaved change, so the tests and the compiler never change in the same step.

Run for real, it rewrote 76 test pieces, 410 calls, with no line added or removed. Every recorded build came out identical: 536 of 536. One test makes 4,000 deliberately damaged pieces out of the others, to see how the compiler copes. Changing the others would have changed all 4,000, so that test now has its own fixed copy. And the whole change can be replayed from scratch and compared, to show nothing was touched by hand.

What reviewing caught

  • The rule meant to tell “only a name was added” from “something changed” sorted a call’s values before comparing, so it could not see two values swap places: the very mistake this work exists to prevent. Its own tests passed, because none of them tried a swap.
  • The rewriting tool may move a value to the front. It was allowed past things that looked harmless, like sin(x). But a piece can write its own sin, and if that one rolls a random number, moving past it changes the art. Now a value only moves past a plain number, colour or name. That cost one call in the whole set, left as it was.
  • Rewriting the tests of entry 10’s bug left three that still passed but no longer caught it. Against the old faulty compiler, the rewritten set caught 38 of 41 cases, where the original caught 40. Fixed, it catches all 41.

A test that passes is not a test that checks. Entry 9 said it, and it was true again.

The sin trap is the one already on entry 10’s list: a function given the name of a built-in one. The rewriting tool no longer falls for it. The compiler itself still can, and that is on the list below.

What is not right yet

The compiler does not insist on the new style yet. Every piece and nearly every test uses it, but the old ways still compile. Making it refuse them is next, with a message that writes the call out as it should be. For colour.mix(ramp[3], #fbf8f1, 0.6) the draft says:

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

Pairs are a weak spot. In corner2(x0, y: y0, g: ...) and field(cos(t), sinT: sin(t)), one half of a pair is bare and the other named, which looks lopsided. The pieces have about seven. For most, the tidy answer is one point instead of two numbers.

And my own pieces are full of one-letter names, p, r, k, j, that were fine while nobody saw them. Now they show in calls: jitter(lookR, v: 0, j: 0.11). They are still to be renamed.

  • Names written on built-in functions are still quietly ignored, until the compiler insists on the new style.
  • A function given the same name as a built-in one can still draw the wrong art.
  • A few sets of test pieces stored inside recordings are still to be rewritten.

Next

  • Make the compiler refuse the old ways.
  • Then the editor support.

The piece at the top

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

// hills: ridges of smooth curves under a gradient sky, each worked with brush sweeps cut to its own outline
plate(1920, 2400)
use noise v2, colour v2, brush v2, spline v2
random layout, paint, marks

// three ramps, dark to pale; the token takes one
let ramps = [#1d2b3a, #3e5c76, #9fb4c7, #ece6d8, #3b1f1a, #8c3b24, #d98e4a, #f2dfc2,
  #2a1f33, #6b3f6e, #c98fae, #f3e3ea]
let pick = paint.int(0 ..< 3) * 4
let ramp = [ramps[pick], ramps[pick + 1], ramps[pick + 2], ramps[pick + 3]]
let field = noise.table(layout)

// n points round a circle, the radius wobbling a little
fn blob(c: vec, r: num, n: int): vec[] {
  let out = vec[]
  for i in 0 ..< n { out.add(c + polar(TAU * i / n, r * layout.float(0.95 ..< 1.05))) }
  return out
}

// the sky, and a low sun with its glow
background(linear(vec(0, 0), vec(0, 1700),
  [colour.darken(ramp[0], amount: 0.35), ramp[1], colour.at(ramp, along: 0.85)]))
let sun = vec(layout.float(500 ..< 1400), layout.float(900 ..< 1250))
let glow = radial(sun, 700, [rgba(ramp[3], 0.85), rgba(colour.lighten(ramp[2], amount: 0.3), 0.3), rgba(ramp[2], 0)])
curve(blob(sun, r: 900, n: 8), closed: true, fill: glow)
curve(blob(sun, r: 170, n: 10), closed: true, fill: radial(sun, 170, [#ffffff, colour.lighten(ramp[3], amount: 0.5)]))

// four ridges, far to near, each lower and darker
let ridges = 4
for k in 0 ..< ridges {
  let near = k / (ridges - 1)
  let base = 1250 + near * 900
  let outline = vec[]
  for i in 0 ..= 12 {
    let x = -160 + i * 175
    outline.add(vec(x,
      base + 340 * noise.octaves(field, point: vec(x * 0.0011, k * 7 + 3), layers: 3) * (1 - near * 0.4)))
  }
  outline.add(vec(2100, 2600))
  outline.add(vec(-200, 2600))
  let shade = 0.78 - near * 0.62
  let ink = colour.mix(colour.at(ramp, along: shade), toward: ramp[0], amount: near * 0.55)
  let top = base - 380
  // the body: dark at the foot, lit along the crest
  curve(outline, smooth: 0.9, closed: true, fill: linear(vec(0, top), vec(0, top + 900),
    [colour.lighten(ink, amount: 0.18), ink, colour.darken(ink, amount: 0.4)]))
  // brush sweeps across it, faded together as one wash and cut to the same curve
  place(opacity: 0.55 + near * 0.3, clip: curve(outline, smooth: 0.9, closed: true)) {
    for s in 0 ..< 9 + k * 4 {
      let y = layout.float(top + 100 ..< base + 800)
      let drag = vec[]
      for i in 0 ..= 10 { drag.add(vec(-100 + i * 220,
        y + 90 * noise.at(field, point: vec(i * 0.5 + s, y * 0.004 + k)))) }
      let v = colour.mix(ink, toward: colour.at(ramp, along: layout.unit()), amount: 0.35)
      brush.sweep(marks, path: spline.sample(drag, steps: 8), width: layout.float(110 ..< 260),
        ramp: [colour.darken(v, amount: 0.3), v, colour.lighten(v, amount: 0.25)],
        opacity: 0.8, density: 0.6, body: 0.4)
    }
  }
}