NLA-lib Explorer

The program builder: named variables, elaborated to plain trees

Library · Source module

The program builder: named variables, elaborated to plain trees

ops% [n, k] (Q, w) → (w, R) { … } is a term whose value is a plain NLAlib.Ops.Stmt Op Tst: the names are resolved to positions of the environment when the term is elaborated, by a macro, and nothing of the notation survives in the tree. So the evaluator, the static count, linking and every rule see exactly the tree that would be written by hand, and a tree built with the notation is rfl-equal to the positional one (the examples are in NLAlib.Ops.Examples.Built). There is no compile function in the kernel and no kernel rfl of a compiled tree.

The names are kept beside the tree. A declaration foo whose value is written with ops% or routine% gets a second definition foo._noted, the noted copy: the same tree with the names of the source as notes (NLAlib.Ops.Stmt.note), namely the names of the header, of each loop variable, scratch buffer and index binding, and of the routine or tree that each run frames. noted_of% foo is that copy, NLAlib.Ops.Stmt.pretty reads it as pseudocode, and erasing its notes (NLAlib.Ops.Stmt.erase) gives the tree of foo. The value of foo has no note, so no rule, count or certificate sees one. noted% t puts the notes in the value itself; the library does not use it.

The header

[n, k] are the dimensions, (Q, w) the input operands, (w, R) the output operands, in the order of NLAlib.Ops.Env.frame: the index variables are the dimensions and then the offsets of the operands, the buffer variables are the operands. A name repeated in the header is one operand updated in place (it is passed twice by the caller, [Q, w, w, R]); it denotes its first occurrence.

Statements

call op [dims] (ins) → (outs)          -- Stmt.call
run R [dims] (operands)               -- a routine, by its interface (one operand per operand)
run body [dims] (args)                -- a tree, by its buffer variables (Stmt.frame)
let t ← allocAs fmt len                -- explicit StorageFormat, scoped over the rest
let t ← alloc len                      -- one-byte StorageFormat.cell, scoped over the rest
let v ← index q (ins) ≤ bound          -- Stmt.bindIdx, scoped over the rest of the block
for i < n { … }                        -- Stmt.loop
if q [dims] (ins) { … } else { … }     -- Stmt.ite
while i < fuel, q [dims] (ins) { … } then { … }   -- Stmt.whileFuel; `i` is the round index,
                                       -- and in the `then` block the number of rounds done
ifPos e { … }   ifZero e { … }         -- Stmt.ifPos, Stmt.ifZero
skip   abort   { … }

run takes either a routine or a tree. A routine (NLAlib.Ops.Routine) is run by its interface: one operand per operand of the routine, and the frame is laid out by the routine's layout, so an operand updated in place is named once. This is the frame call expects. A tree (Stmt, for instance a fragment of a routine) is run by its buffer variables, which the caller lists in the tree's own order. A routine with an operand that continues another has no frame of its own operands (the continued position is computed from lengths, which are not index expressions) and is not run by run.

Statements are separated by ;; a sequence is nested to the right. An operand is X or X[e]: for an operand of the header, the offset is the offset variable of X plus the summands of e, added from the left (R[j * p + i] is .var oR + .var j * .var p + .var i; a parenthesised summand stays one: C[(i * p + j)] is .var oC + (.var i * .var p + .var j)); for a scratch buffer it is e (and X alone is offset 0). Index expressions are names, numerals, +, - (truncated), *, / and parentheses. Operation and query names are Lean terms ((Has.inj Blas1Op.dot)); sizes are never Lean terms, so a tree is one tree for all sizes.

The keywords call, alloc, index, abort, ifPos, ifZero are not reserved: they stay usable as identifiers elsewhere.

An unknown name, a buffer used as an index or an index used as a buffer is an error at the name; so is a dimension declared twice or also an operand, and a binder (for, let, while) that shadows a name of the header. The offset variable of an operand X is named X_off and may be used in index expressions.

The offset convention is frozen (it is part of the identity of every registered tree): X[e] for an operand is .var X_off plus the top-level +-summands of e, added from the left; a parenthesised summand, a -, * or / stays one summand; X alone is .var X_off. For scratch, X[e] is e and X is .const 0. Numerals are .const n; + - * / are the IExp instances.

Main results

* ops%: a program with named variables, elaborated to a plain Stmt.

Declarations