knotflow Lessons PDF ↓ Deutsch Handbook Modeller ↗ Source

Lesson 2.4 · II The language

Modules

What it's about: You should be able to use a library without copying it. In knotflow, a module is simply a value: a record of its pub definitions. The example is the standard library knots, a knot framework written in the language itself (Lesson 3.2).

1. A library is a record

Without modules, a library stays a template you copy, or it grows into the prelude. The module system adds exactly one expression to the language:

let knots = import knots                       // lib/knots.kf
export knots.knot(knots.fitted(knots.torus_knot(2, 3), 40), 2)

let {fitted, torus_knot} = import knots        // or take it apart right away

import knots evaluates the library and gives a record with its definitions. Everything else is already there: field access, taking apart with {a, b}, closures that carry their environment along. The inspiration for this is Nix, where import ./file.nix simply gives the value of the file. A module is not a new language concept, but a value like any other.

For knots.knot(…) to work, one rule holds: if a record has a field f, r.f(x) calls that field; otherwise it is the method syntax f(r, x). JavaScript does the same (Lesson 2.3, section 6). With modules, "call the field" is the only reading anyone expects.

2. The standard library: knots and sweeps

Two libraries are built in, into kf as into the browser, and both are written in the language itself (lib/):

let knots = import knots
export knots.rope(knots.fitted(knots.turks_head(3, 4), 60), r: 3)

That is the Turk's head with three strands and four bights, brought to 60 mm, as a rope. Lesson 3.2 builds larger knots from these building blocks; kf help lists every function of both libraries.

3. pub: private unless stated otherwise

Not everything in a library is meant for the outside. Helper functions are internals; if people use them from outside, the library can no longer be changed. So, as in Rust, everything is private unless it is marked pub:

fn torus_point(p, q, ratio, t) = …                        // private
pub fn torus_knot(p, q, ratio = 0.45) = torus_point(p, q, ratio, _)

torus_knot returns a path that uses torus_point internally. That works because the closure carries its environment (the library's) along. From outside, torus_point is invisible, and the message says so:

error: the library knots has no pub torus_point (private in knots.kf, or not there at all);
       it offers: curve_of, knot, rope, scaled, moved, tilted, squashed, reach, fitted, …

All equations of a function have the same visibility; pub fn f(0) followed by fn f(n) is an error.

4. Self-contained and loaded once

5. Cache and where libraries are found

6. A deeper look: errors in libraries

An error in a library should be shown where it is, with the path back into the model. A position is at first only a number: which character of a text it is. So that it also tells which text is meant, each library gets its own range of positions: the first starts at 2²⁸, each further one 2²⁴ later. (That also fits into the 32-bit numbers of WebAssembly.) So every position tells which file it comes from, even in the chain of calls:

error: interpolate: points 0 and 1 are the same: a curve through them has no direction there
  --> knots.kf:15:3
   |
15 |   interpolate([path(t) | t <- range(0, 360, step: 360 / samples)], closed: true)
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = in curve_of, called at knots.kf:19:48
   = in knot, called at model.kf:3:8

In the browser, the place is marked in the library's tab, and in the model's text the call that led there.

Try it yourself

  1. Write your own library mine.kf next to a model, with a private helper function and a pub fn. What does the language report when the model uses the private one?
  2. Let two libraries import each other. What does the message look like, and why is a cycle forbidden even though functions may call each other?
  3. In the browser (Lesson 4.1), copy knots into the project and put an error into it. Where is it marked: in the library's tab, in the model, or in both?