NLA-lib Explorer

Writing a routine and its specification

Library · Source module

Writing a routine and its specification

routine% [n, k] (b : n, x : n) → (x, r : n, p : n) { … } is a term whose value is a plain NLAlib.Ops.Routine: the header is the interface, and the body is a program of the builder (NLAlib/Ops/Logic/Builder.lean) in which the dimensions and the operands are named. Nothing of the notation survives in the value. The names are kept in the noted copy of the declaration (foo._noted, read with noted_of% foo): the same routine whose tree carries the names as notes (NLAlib.Ops.Stmt.note). (noted_of% foo).pretty reads it as pseudocode.

The header

[n, k] are the dimensions. Each operand is declared once, with its length, an index expression of the builder in the dimensions (b : n, A : n * n); a length that is not an index expression is a Lean term in which the dimensions are natural numbers, written ↑(…) (data : ↑(dataLength n n)).

* An operand named only on the left of → is read (NLAlib.Ops.Port.read). * An operand named only on the right is written (NLAlib.Ops.Port.write). * An operand named on both sides is updated in place (NLAlib.Ops.Port.update). It is declared on the left and named again, without its length, on the right.

The operands of the interface are those of the left and then the new ones of the right. The buffer variables of the tree are the left operands and then the right operands, the convention of the contract of an operation and of ops%; so an operand updated in place is two buffer variables, and the layout of the routine (NLAlib.Ops.Routine.binds) is generated.

An operand may be named again on the left, without its length: the tree then has one more buffer variable on the same operand, as when a tree written for distinct operands is used with two of them equal ((a : 1, x : n, x) → (x) runs axpy with x as both vectors and the output).

The block of an operand need not be contiguous. x : n every inc is n cells, one every inc cells from the position of the operand (a vector with an increment, a row of a matrix stored by columns); A : m × n every lda is n runs of m cells, one run every lda cells (a submatrix of a matrix with leading dimension lda). The block is the cells in order, run after run (NLAlib.Ops.Port.stride). The tree addresses the cells by index arithmetic on the operand (x[inc * i]); the blocks of an operation contract are contiguous.

An operand may be marked: isolated A : n * n (read, and apart from every other operand), unreported w : n (written, with final contents that the judgment does not report). An operand whose block continues another's (NLAlib.Ops.Port.continuing) is not written in a header: it comes directly after the operand it continues in the interface, which is not the order of a header, so such a routine gives its operands as a list (NLAlib.Ops.extendRoutine).

Clauses

Between the header and the body:

* draws e (an index expression or ↑(…)): the tape positions the routine consumes; draws unspecified: their number depends on the data. Without the clause the routine draws nothing. * by blocks: operands are separated by block (NLAlib.Ops.Separation.blocks). Without the clause they are separated by buffer. * one variable: the tree has one buffer variable for each operand, in the order of the interface, so an operand updated in place is one variable and no layout is generated. The operands of a block are then numbered that way. A routine without a layout is its own program: NLAlib.Ops.Routine.close adds no frame, so a top-level routine that updates an operand in place is written with this clause when the frame is not to be part of the certified tree.

The body

A block { … } of the builder, or := t for a tree t : Stmt Op Tst defined elsewhere with the variables in the order above.

Specifications

spec% [n, k] (bs, xs) → (xs', rs, ps) requires P ensures Q is a NLAlib.Ops.Routine.Spec: the dimensions, the blocks read and the blocks reported are named (any patterns), P is the precondition and Q the postcondition. At dimensions or blocks of another shape the postcondition is False, and so is a stated precondition; with requires P omitted the precondition is True at every shape. tape ω before the blocks names the draws, the tape seen from the position of the call.

Main results

* routine%: a routine with a named interface, elaborated to a plain Routine. * spec%: a specification on named dimensions and blocks, elaborated to a plain Routine.Spec.

Declarations