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/):
knotsdescribes knots as paths: a path is a function that maps an angle from 0 to 360° to a point in space.torus_knot(p, q),turks_head(leads, bights),lissajous(…),figure_eightandcatalog(eight well-known knots as a list of{name, path}) give paths.scaled,moved,tilted,squashedandfitted(bring to a diameter) reshape them.knot(path, r)puts a tube around the path,rope(path, r, strands, twist)a twisted rope, andcarrick_bend(size)gives the two ropes of a Carrick bend.sweepstakes a profile along a closed path.frames(path, n)gives n frames that turn as little as possible; whatever twist is left after one round (the holonomy) is spread evenly.closed(path, profile, twist)draws a profile along it.knotsuses it for the ropes.
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
- A library only sees the built-in functions and the prelude, not the model that imports it. Otherwise its meaning would depend on who uses it.
- No
param, noexport, noreport: settings are arguments of its functions. The data flow stays visible in the model. - Once per run: if you write
import knotstwice, you get the same record both times. Libraries may import libraries (knotsusessweepsfor the ropes), but not in a cycle. A cycle is reported with the whole chain:p → q → p.
5. Cache and where libraries are found
- Cache (Lesson 4.2): the fingerprint of a statement
with
import knotscontains the text of the library and the text of the libraries it imports. If one of them changes, it is recomputed; otherwise not. - Where to look: the command line looks for
knots.kffirst next to the model, then in the standard librarylib/. That is built intokfand into the browser, soimport knotsworks everywhere. The model itself is never its own library:examples/knots.kfimports the standard libraryknots, not itself. - In the browser, instead of a folder next to the
model there are the project's libraries, as real files in a folder on
your computer if you like. An example with a folder of its own in
examples/(examples/differential/differential.kfwithgears.kf,bearings.kf,fasteners.kfnext to it) brings its libraries along; "reset" restores them. Lesson 4.1 shows how the file bar, errors in libraries and projects work there.
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
- Write your own library
mine.kfnext to a model, with a private helper function and apub fn. What does the language report when the model uses the private one? - 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?
- In the browser (Lesson 4.1), copy
knotsinto the project and put an error into it. Where is it marked: in the library's tab, in the model, or in both?