main.bend checks
raw source on the hub · import bend-ml-tensor-array@0.1.21.0/main.bend as Main
bend-ml-tensor-array: tensors over a flat Array<F32>, with the shape in the type.
import bend-ml-tensor-array@0.1.21.0/main.bend as TA
Mat<r, c> stores r*c numbers in an Array<F32>, row by row (index i*c + j). The dimensions are erased type parameters: the same guarantee as bend-ml-tensor (a product with wrong dimensions does not compile), but ~50x faster than lists: Array.get/set by index, and products in parallel blocks of rows.
An Array is affine (a single owner): every operation that reads an operand returns it together with the result (as Array.get does), in records MM, AR, ... Use Mat.clone when you need two copies.
What is NOT proved: the invariant "the Array has capacity >= r*c" holds because the constructors (Mat.zeros, Mat.of_list) allocate the right size; and F32 is validated by tests against PyTorch (reference/test_tensor_array.py), not by proof.
1 import
import Base
Laws
law cap_ok provedsource · line 737 · raw
@+n:Nat -> {True{} == leb(n, pow2(cap_depth(n))) : Bool}LAW (cap_ok): an Array with 2^cap_depth(n) slots always has room for n elements.
law half_cover provedsource · line 1576 · raw
@+n:Nat -> {n == Nat.add(half(n), Nat.sub(n, half(n))) : Nat}LAW (half_cover): the two bands of a node, half(r) rows and r - half(r) rows, add up to exactly r rows: no row is lost and none is counted twice.
law leaf_len provedsource · line 1833 · raw
@-r:Nat -> @-c:Nat -> @w:Array<F32> -> @+mleft:Nat -> @+idx:U32 -> @+acc:List<&2, F32> -> @rc:Pair(Array<F32>, F32) -> {bl_len(r, c, bv_g2(r, c, w, read_l(mleft, idx, acc, rc))) == Nat.add(1n+mleft, List.length(&2, F32, acc)) : Nat}a leaf's list: read_l reads mleft + 1 numbers onto acc. By induction on mleft: with 0 left it reads one number onto acc (1 + the length of acc, by the definitions); with 1 + p left it reads one and recurses, and len_step moves the 1 out of the sum.
law band_len provedsource · line 1859 · raw
@-r:Nat -> @+c:Nat -> @+rows:Nat -> @x:Array<F32> -> @w:Array<F32> -> {bl_len(r, c, bv_gemm(r, c, rows, rows, x, w)) == rows : Nat}LAW (band_len): the product of one band with rows rows gives exactly rows numbers.
0 rows give the empty list; 1 + p rows go through the gemm, and g_len counts what is read back.
law matvec_l_len provedsource · line 1990 · raw
@+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> @+x:List<&2, F32> -> {bl_len(r, c, Bands.matvec_l(r, c, b, x)) == r : Nat}LAW (matvec_l_len): Bands.matvec_l gives exactly r numbers, one per row of the matrix, whatever the bands' depth.
law to_list_len provedsource · line 2009 · raw
@+r:Nat -> @+c:Nat -> @m:Mat<r, c> -> {ML.len(r, c, Mat.to_list(r, c, m)) == Nat.mul(r, c) : Nat}LAW (to_list_len): Mat.to_list reads exactly r*c numbers, no more and no fewer.
law reshape_len provedsource · line 2022 · raw
@+r1:Nat -> @+c1:Nat -> @+r2:Nat -> @+c2:Nat -> @+p:{Nat.mul(r1, c1) == Nat.mul(r2, c2) : Nat} -> @m:Mat<r1, c1> -> {ML.len(r2, c2, Mat.to_list(r2, c2, Mat.reshape(r1, c1, r2, c2, p, m))) == Nat.mul(r1, c1) : Nat}LAW (reshape_len): a reshape reads the same number of elements. The new shape multiplies to the
old one by the proof p; the count then comes from to_list_len on both sides.
law size_set provedsource · line 2239 · raw
@-T:Data -> @a:Array<T> -> @+i:U32 -> @+v:T -> {ASet.sz(T, Array.set(T, a, i, v)) == ASet.sz(T, a) : U32}LAW: writing v at i keeps the array's size (any i: the index wraps around)
law get_after_set provedsource · line 2250 · raw
@-T:Data -> @a:Array<T> -> @+i:U32 -> @+v:T -> {Pair.snd(Array<T>, T, Array.get(T, Array.set(T, a, i, v), i)) == v : T}LAW: what Array.set writes at i, Array.get reads at i (any i: both wrap it the same way)
law get_other_set provedsource · line 2473 · raw
@-T:Data -> @a:Array<T> -> @+i:U32 -> @+k:U32 -> @+v:T -> @h:ASet.Apart(T, a, ASet.sz(T, a), ASet.J(i, ASet.sz(T, a)), ASet.Z(i, ASet.sz(T, a)), ASet.J(k, ASet.sz(T, a)), ASet.Z(k, ASet.sz(T, a))) -> {Pair.snd(Array<T>, T, Array.get(T, Array.set(T, a, i, v), k)) == Pair.snd(Array<T>, T, Array.get(T, a, k)) : T}LAW: a write at i leaves alone the cell a read at k reaches, when the two descents part before a leaf (any i and k: both wrap them; h is what makes them different cells, see the top of the file)
law pow_shr provedsource · line 2915 · raw
@+e:Nat -> @h:{Nat.is_lt(e, 31n) == True{} : Bool} -> {U32.shr(U32.shln(1, 1n+e)) == U32.shln(1, e) : U32}LAW (pow_shr): below the top bit, half of 2^(e+1) is 2^e (U32.shln(1, e) is 2^e, the size Array.size computes for a tree of depth e)
law pow_mask provedsource · line 2928 · raw
@+x:Word(32n) -> @+e:Nat -> @h:{Bits.below(32n, x, e) == True{} : Bool} -> {ASet.J(U32{x}, U32.shln(1, e)) == U32{x} : U32}LAW (pow_mask): an index x below 2^e is its own masked index: ASet.J, the mask Array.get and Array.set apply with a size of 2^e, leaves it alone
law pow_lt provedsource · line 2942 · raw
@+x:Word(32n) -> @+e:Nat -> @hb:{Nat.is_lt(e, 32n) == True{} : Bool} -> @h:{Bits.below(32n, x, 1n+e) == True{} : Bool} -> {U32.is_lt(U32{x}, U32.shln(1, e)) == Bool.not(Bits.at(32n, x, e)) : Bool}LAW (pow_lt): for x below 2^(e+1), x < 2^e exactly when bit e of x is zero: the branch a walk down the tree takes at a node of size 2^(e+1)
law pow_sub provedsource · line 2955 · raw
@+x:Word(32n) -> @+e:Nat -> @h:{Bits.at(32n, x, e) == True{} : Bool} -> {U32.sub(U32{x}, U32.shln(1, e)) == U32{Bits.clear(32n, x, e)} : U32}LAW (pow_sub): taking 2^e from an x whose bit e is set clears that bit: the index a walk down the tree carries into the right half
law new_apart provedsource · line 3020 · raw
@-T:Data -> @+e:Nat -> @+v:T -> @+he:{Nat.is_lt(e, 32n) == True{} : Bool} -> @+x:Word(32n) -> @+y:Word(32n) -> @+hx:{Bits.below(32n, x, e) == True{} : Bool} -> @+hy:{Bits.below(32n, y, e) == True{} : Bool} -> @+hd:{Bits.differ(32n, x, y, e) == True{} : Bool} -> ASet.Apart(T, Array.new(T, e, v), ASet.sz(T, Array.new(T, e, v)), ASet.J(U32{x}, ASet.sz(T, Array.new(T, e, v))), ASet.Z(U32{x}, ASet.sz(T, Array.new(T, e, v))), ASet.J(U32{y}, ASet.sz(T, Array.new(T, e, v))), ASet.Z(U32{y}, ASet.sz(T, Array.new(T, e, v))))LAW (new_apart): on the tree Array.new builds, of depth e < 32, a write at x and a read at y part before a leaf whenever x and y are below 2^e and differ: get_other_set's hypothesis, for Array.new's tree
law apart_set provedsource · line 3164 · raw
@-T:Data -> @a:Array<T> -> @+i:U32 -> @+v:T -> @+m:U32 -> @+j:U32 -> @+z:Bool -> @+k:U32 -> @+w:Bool -> @h:ASet.Apart(T, a, m, j, z, k, w) -> ASet.Apart(T, Array.set(T, a, i, v), m, j, z, k, w)
LAW (apart_set): a write keeps the tree's shape, so two walks that part on a part on the written array too (any tree, any index and value: Array.set replaces one leaf and leaves every node where it was)
Types
type R source · line 24 · raw
Type
R@a:Array<F32> -> @b:Array<F32> -> @x:F32 -> R
type G source · line 27 · raw
Type
G@a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
type D4 source · line 43 · raw
Type
dotr's state between two steps: the four partial sums and the two pending reads
D4@a0:F32 -> @a1:F32 -> @a2:F32 -> @a3:F32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> D4
type S3 source · line 119 · raw
Type
S3@a:Array<F32> -> @b:Array<F32> -> @out:List<&2, F32> -> S3
type P2 source · line 234 · raw
Type
P2@a:Array<F32> -> @b:Array<F32> -> P2
type AL source · line 237 · raw
Type
AL@c:Array<F32> -> @l:List<&2, F32> -> AL
type L2 source · line 378 · raw
Type
L2@loss:F32 -> @c:Array<F32> -> L2
type Mat source · line 391 · raw
@-r:Nat -> @-c:Nat -> Type
Mat@-r:Nat -> @-c:Nat -> @d:Array<F32> -> Mat<r, c>
type AF source · line 408 · raw
Type
AF@c:Array<F32> -> @x:F32 -> AF
type CR source · line 481 · raw
Type
CR@c:Array<F32> -> @n:Nat -> CR
type ML source · line 795 · raw
@-r:Nat -> @-c:Nat -> Type
ML@-r:Nat -> @-c:Nat -> @m:Mat<r, c> -> @l:List<&2, F32> -> ML<r, c>
type MM2 source · line 823 · raw
@-r:Nat -> @-c:Nat -> Type
MM2@-r:Nat -> @-c:Nat -> @a:Mat<r, c> -> @b:Mat<r, c> -> MM2<r, c>
type MMul source · line 849 · raw
@-n:Nat -> @-k:Nat -> @-m:Nat -> Type
MMul@-n:Nat -> @-k:Nat -> @-m:Nat -> @a:Mat<n, k> -> @b:Mat<k, m> -> @c:Mat<n, m> -> MMul<n, k, m>
type MMulNT source · line 854 · raw
@-n:Nat -> @-k:Nat -> @-m:Nat -> Type
MMulNT@-n:Nat -> @-k:Nat -> @-m:Nat -> @a:Mat<n, k> -> @b:Mat<m, k> -> @c:Mat<n, m> -> MMulNT<n, k, m>
type MMulTN source · line 859 · raw
@-n:Nat -> @-k:Nat -> @-m:Nat -> Type
MMulTN@-n:Nat -> @-k:Nat -> @-m:Nat -> @a:Mat<k, n> -> @b:Mat<k, m> -> @c:Mat<n, m> -> MMulTN<n, k, m>
type T8 source · line 871 · raw
Data
T8@v0:F32 -> @v1:F32 -> @v2:F32 -> @v3:F32 -> @v4:F32 -> @v5:F32 -> @v6:F32 -> @v7:F32 -> T8
type PK source · line 884 · raw
Type
PK@a:Array<F32> -> @t:Array<T8> -> PK
type TD source · line 956 · raw
Type
TD@a:Array<T8> -> @b:Array<T8> -> @s0:F32 -> @s1:F32 -> @s2:F32 -> @s3:F32 -> TD
type TR source · line 971 · raw
Type
TR@a:Array<T8> -> @b:Array<T8> -> @x:F32 -> TR
type TG source · line 982 · raw
Type
TG@a:Array<T8> -> @b:Array<T8> -> @c:Array<F32> -> TG
type TL source · line 1005 · raw
Type
rows i0 .. i0+cnt-1 of C as a list, in order (the parallel version's leaves)
TL@a:Array<T8> -> @b:Array<T8> -> @out:List<&2, F32> -> TL
type PKK source · line 1028 · raw
Type
PKK@a:Array<F32> -> @b:Array<F32> -> @ta:Array<T8> -> @tb:Array<T8> -> PKK
type PT source · line 1049 · raw
Type
PLeaf@t:Array<T8> -> PT
PNode@l:PT -> @r:PT -> PT
type PKT source · line 1053 · raw
Type
PKT@a:Array<F32> -> @t:PT -> PKT
type CB source · line 1090 · raw
Data
a block of C's columns j0 .. j0+w-1 for every row, row by row
CB@j0:U32 -> @w:Nat -> @xs:List<&2, F32> -> CB
type RW source · line 1103 · raw
Type
RW@c:Array<F32> -> @xs:List<&2, F32> -> RW
type SL source · line 1274 · raw
Type
SL@a:Array<F32> -> @s:Array<F32> -> SL
type MBias source · line 1326 · raw
@-n:Nat -> @-m:Nat -> Type
MBias@-n:Nat -> @-m:Nat -> @c:Mat<n, m> -> @b:Mat<1n, m> -> MBias<n, m>
type MPair source · line 1341 · raw
@-r:Nat -> @-c:Nat -> Type
MPair@-r:Nat -> @-c:Nat -> @a:Mat<r, c> -> @b:Mat<r, c> -> MPair<r, c>
type MColSum source · line 1380 · raw
@-n:Nat -> @-m:Nat -> Type
MColSum@-n:Nat -> @-m:Nat -> @a:Mat<n, m> -> @s:Mat<1n, m> -> MColSum<n, m>
type MCE source · line 1404 · raw
@-n:Nat -> @-c:Nat -> Type
MCE@-n:Nat -> @-c:Nat -> @loss:F32 -> @grad:Mat<n, c> -> MCE<n, c>
type MHits source · line 1419 · raw
@-n:Nat -> @-c:Nat -> Type
MHits@-n:Nat -> @-c:Nat -> @z:Mat<n, c> -> @hits:Nat -> MHits<n, c>
type MRow source · line 1434 · raw
@-r:Nat -> @-c:Nat -> Type
MRow@-r:Nat -> @-c:Nat -> @m:Mat<r, c> -> @row:List<&2, F32> -> MRow<r, c>
type Bands source · line 1583 · raw
@-r:Nat -> @-c:Nat -> Type
BLeaf@-r:Nat -> @-c:Nat -> @m:Mat<r, c> -> Bands<r, c>
BNode@-r:Nat -> @-c:Nat -> @x:Bands<half(r), c> -> @y:Bands<Nat.sub(r, half(r)), c> -> Bands<r, c>
type BV source · line 1657 · raw
@-r:Nat -> @-c:Nat -> Type
B (intact) and y = B · x, typed like Mat.matmul_nt: x is 1 x c, y is 1 x r
BV@-r:Nat -> @-c:Nat -> @b:Bands<r, c> -> @y:Mat<1n, r> -> BV<r, c>
type BL source · line 1661 · raw
@-r:Nat -> @-c:Nat -> Type
the bands and y as a list in row order (the result of Bands.matvec_l)
BL@-r:Nat -> @-c:Nat -> @b:Bands<r, c> -> @y:List<&2, F32> -> BL<r, c>
type BRow source · line 1726 · raw
@-r:Nat -> @-c:Nat -> Type
BRow@-r:Nat -> @-c:Nat -> @b:Bands<r, c> -> @row:List<&2, F32> -> BRow<r, c>
type WAL source · line 1904 · raw
@-s:AL -> @-n:Nat -> Type
a read's list y, with s == AL{s's array, y} and y of length n
WAL@-s:AL -> @-n:Nat -> @y:List<&2, F32> -> @e:{AL{al_c(s), y} == s : AL} -> @l:{List.length(&2, F32, y) == n : Nat} -> WAL<s, n>
type WBL source · line 1908 · raw
@-r:Nat -> @-c:Nat -> @-s:BL<r, c> -> @-n:Nat -> Type
a product's list y, with s == BL{s's bands, y} and y of length n
WBL@-r:Nat -> @-c:Nat -> @-s:BL<r, c> -> @-n:Nat -> @y:List<&2, F32> -> @e:{BL{bl_b(r, c, s), y} == s : BL<r, c>} -> @l:{List.length(&2, F32, y) == n : Nat} -> WBL<r, c, s, n>
Definitions
def dotr source · line 35 · raw
@k:Nat -> @+ia:U32 -> @+ib:U32 -> @+sa:U32 -> @+sb:U32 -> @a0:F32 -> @a1:F32 -> @a2:F32 -> @a3:F32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> R
A dot product with four partial sums. Each step adds its product to a0 and rotates the sums (a1, a2, a3, a0), so each sum gets every fourth product and the processor can work on the next addition before the last one has finished; at the end the sums are added in pairs. The order of the additions is fixed, so the result is the same on every run and every thread count, but it is not the order of a single running sum (NOTES.md, exp. 17).
def dstep source · line 47 · raw
@+ia:U32 -> @+ib:U32 -> @s:D4 -> D4
one step of dotr: adds the pending product to a0, rotates the sums, reads the next pair at ia, ib
def dfin source · line 54 · raw
@k:Nat -> @+ia:U32 -> @+ib:U32 -> @+sa:U32 -> @+sb:U32 -> @s:D4 -> R
def dot4 source · line 64 · raw
@q:Nat -> @+rem:Nat -> @+ia:U32 -> @+ib:U32 -> @+sa:U32 -> @+sb:U32 -> @s:D4 -> R
dotr with its loop unrolled by four: q rounds of four dstep, then the last rem (< 4) steps by dotr itself. Every product goes to the same sum in the same order as in dotr (four rotations bring the names back), so the result has dotr's bits (tests/dot4.bend). The compiled loop runs ~16% fewer instructions; one thread on a 50,257 x 768 matrix · vector goes from 26.7 to 20.0 ms, smaller ones and 16 threads stay within noise (NOTES.md, 2026-11-01). Eight steps a round were no faster.
def dotg source · line 71 · raw
@+k:Nat -> @+ia:U32 -> @+ib:U32 -> @+sa:U32 -> @+sb:U32 -> @acc:F32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> R
def cwr_add source · line 77 · raw
@+idx:U32 -> @+x:F32 -> @r:Pair(Array<F32>, F32) -> Array<F32>
def cwr source · line 82 · raw
@ac:Bool -> @+idx:U32 -> @+x:F32 -> @c:Array<F32> -> Array<F32>
def gcols source · line 91 · raw
@+ac:Bool -> @mleft:Nat -> @+kn:Nat -> @+ia0:U32 -> @+ib0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @+cidx:U32 -> @c:Array<F32> -> @r:R -> G
def grows source · line 98 · raw
@+ac:Bool -> @nleft:Nat -> @+mm1:Nat -> @+kn:Nat -> @+i:U32 -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @+mu:U32 -> @g:G -> G
def gemm_ac source · line 107 · raw
@+ac:Bool -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
def gemm source · line 110 · raw
@+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
def lcols source · line 122 · raw
@mleft:Nat -> @+kn:Nat -> @+ia0:U32 -> @+ib0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @out:List<&2, F32> -> @r:R -> S3
def lrows source · line 129 · raw
@nleft:Nat -> @+mm1:Nat -> @+kn:Nat -> @+i:U32 -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @st:S3 -> S3
def lblock_fin source · line 136 · raw
@st:S3 -> List<&2, F32>
def lblock source · line 141 · raw
@cnt:Nat -> @+mm1:Nat -> @+kn:Nat -> @+i:U32 -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> List<&2, F32>
def lpar source · line 144 · raw
@d:Nat -> @+cnt:Nat -> @+mm1:Nat -> @+kn:Nat -> @+i:U32 -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @ra:Pair(Array<F32>, Array<F32>) -> @rb:Pair(Array<F32>, Array<F32>) -> List<&2, F32>
def write_l source · line 152 · raw
@xs:List<&2, F32> -> @+idx:U32 -> @c:Array<F32> -> Array<F32>
def write_la source · line 161 · raw
@+ac:Bool -> @xs:List<&2, F32> -> @+idx:U32 -> @c:Array<F32> -> Array<F32>
def gp3 source · line 168 · raw
@+ac:Bool -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> @xs:List<&2, F32> -> G
def gp2 source · line 171 · raw
@d:Nat -> @+n:Nat -> @+mm1:Nat -> @+kn:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @c:Array<F32> -> @pa:Pair(Array<F32>, Array<F32>) -> @pb:Pair(Array<F32>, Array<F32>) -> G
def gemm_par source · line 178 · raw
@d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
def cblock_fin source · line 187 · raw
@st:S3 -> List<&2, F32>
def cblock_go source · line 194 · raw
@cnt1:Nat -> @+kn:Nat -> @+ia0:U32 -> @+j0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> List<&2, F32>
def cblock source · line 197 · raw
@cnt:Nat -> @+kn:Nat -> @+ia0:U32 -> @+j0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> List<&2, F32>
def cpar source · line 204 · raw
@d:Nat -> @+cnt:Nat -> @+kn:Nat -> @+ia0:U32 -> @+j0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @ra:Pair(Array<F32>, Array<F32>) -> @rb:Pair(Array<F32>, Array<F32>) -> List<&2, F32>
def cp2 source · line 212 · raw
@+ac:Bool -> @d:Nat -> @+m:Nat -> @+kn:Nat -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @c:Array<F32> -> @pa:Pair(Array<F32>, Array<F32>) -> @pb:Pair(Array<F32>, Array<F32>) -> G
def gemm_par_cols source · line 220 · raw
@+ac:Bool -> @d:Nat -> @+m:Nat -> @+k:Nat -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
def read_l source · line 244 · raw
@mleft:Nat -> @+idx:U32 -> @acc:List<&2, F32> -> @rc:Pair(Array<F32>, F32) -> AL
def addl source · line 253 · raw
@xs:List<&2, F32> -> @+idx:U32 -> @rc:Pair(Array<F32>, F32) -> Array<F32>
def bias_rows source · line 262 · raw
@nleft:Nat -> @+i:U32 -> @+mu:U32 -> @+bl:List<&2, F32> -> @c:Array<F32> -> Array<F32>
def relu_l source · line 271 · raw
@nleft:Nat -> @+t:U32 -> @rc:Pair(Array<F32>, F32) -> Array<F32>
def pos2 source · line 278 · raw
@b:Bool -> F32
def pos source · line 285 · raw
@+y:F32 -> F32
def mask_l source · line 290 · raw
@nleft:Nat -> @+t:U32 -> @rd:Pair(Array<F32>, F32) -> @rh:Pair(Array<F32>, F32) -> P2
def maxl2 source · line 299 · raw
@xs:List<&2, F32> -> @cur:F32 -> F32
def maxl source · line 306 · raw
@xs:List<&2, F32> -> F32
def expl source · line 313 · raw
@xs:List<&2, F32> -> @+m:F32 -> List<&2, F32>
def suml source · line 320 · raw
@xs:List<&2, F32> -> @acc:F32 -> F32
def divl source · line 327 · raw
@xs:List<&2, F32> -> @+s:F32 -> List<&2, F32>
def softmax2 source · line 334 · raw
@+es:List<&2, F32> -> List<&2, F32>
def softmax source · line 337 · raw
@+xs:List<&2, F32> -> List<&2, F32>
def sgd_l source · line 342 · raw
@nleft:Nat -> @+t:U32 -> @+lr:F32 -> @rw:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> P2
def pick source · line 353 · raw
@ps:List<&2, F32> -> @n:Nat -> F32
def hit source · line 364 · raw
@b:Bool -> F32
def grad_l source · line 371 · raw
@ps:List<&2, F32> -> @+label:Nat -> @+j:Nat -> @+nf:F32 -> List<&2, F32>
def ce_row2 source · line 381 · raw
@+ps:List<&2, F32> -> @+label:Nat -> @+nf:F32 -> @+idx:U32 -> @loss:F32 -> @c:Array<F32> -> L2
def ce_row source · line 384 · raw
@al:AL -> @+label:Nat -> @+nf:F32 -> @+idx:U32 -> @loss:F32 -> L2
def ce_rows source · line 394 · raw
@labels:List<&2, Nat> -> @+mm1:Nat -> @+mu:U32 -> @+nf:F32 -> @+i:U32 -> @r:L2 -> L2
def cea_max source · line 412 · raw
@k:Nat -> @+idx:U32 -> @+m:F32 -> @r:Pair(Array<F32>, F32) -> AF
the max of k + 1 cells from idx, from m (the first cell's value, already read in r)
def cea_exp source · line 420 · raw
@k:Nat -> @+idx:U32 -> @+m:F32 -> @+s:F32 -> @r:Pair(Array<F32>, F32) -> AF
cells idx..idx + k become exp(cell - m); returns their sum from s, added left to right
def cea_grad source · line 430 · raw
@k:Nat -> @+idx:U32 -> @+j:Nat -> @+label:Nat -> @+s:F32 -> @+nf:F32 -> @r:Pair(Array<F32>, F32) -> Array<F32>
cell idx (column j) becomes ((e / s) - onehot) / nf, for k + 1 cells
def cea_p2 source · line 437 · raw
@+s:F32 -> @r:Pair(Array<F32>, F32) -> AF
def cea_p source · line 443 · raw
@inside:Bool -> @+o:U32 -> @+label:Nat -> @+s:F32 -> @c:Array<F32> -> AF
p[label] = e[label] / s, or 0.0 for a label past the row (pick's Nil case)
def cea_row3 source · line 450 · raw
@+mm1:Nat -> @+o:U32 -> @+label:Nat -> @+s:F32 -> @+nf:F32 -> @+loss:F32 -> @q:AF -> L2
def cea_row2 source · line 455 · raw
@+mm1:Nat -> @+o:U32 -> @+label:Nat -> @+nf:F32 -> @+loss:F32 -> @q:AF -> L2
def cea_row1 source · line 460 · raw
@+mm1:Nat -> @+o:U32 -> @+label:Nat -> @+nf:F32 -> @+loss:F32 -> @q:AF -> L2
def cea_row0 source · line 466 · raw
@+mm1:Nat -> @+o:U32 -> @+label:Nat -> @+nf:F32 -> @+loss:F32 -> @r:Pair(Array<F32>, F32) -> L2
a row's max starts from its first cell (maxl), then folds over the k + 1 cells from it
def cea_rows source · line 472 · raw
@labels:List<&2, Nat> -> @+mm1:Nat -> @+mu:U32 -> @+nf:F32 -> @+i:U32 -> @r:L2 -> L2
one row per label, in order, as ce_rows
def gt_head source · line 484 · raw
@xs:List<&2, F32> -> @+v:F32 -> Bool
def argmax2 source · line 491 · raw
@xs:List<&2, F32> -> @g:Bool -> @+i:Nat -> @+bi:Nat -> @+bv:F32 -> Nat
def argmax source · line 500 · raw
@xs:List<&2, F32> -> Nat
def one source · line 507 · raw
@b:Bool -> Nat
def count_rows3 source · line 514 · raw
@+y:Nat -> @n:Nat -> @al:AL -> CR
def count_rows source · line 519 · raw
@labels:List<&2, Nat> -> @+mm1:Nat -> @+mu:U32 -> @+i:U32 -> @r:CR -> CR
def BD source · line 540 · raw
@b:Bool -> @t:Type -> @f:Type -> Type
a type chosen by a Bool: rewriting through it refutes True == False
def leb source · line 548 · raw
@a:Nat -> @b:Nat -> Bool
a <= b, by direct recursion
def pow2 source · line 557 · raw
@d:Nat -> Nat
def cap_pick source · line 566 · raw
@ok:Bool -> @d:Nat -> @+p:Nat -> Pair(Nat, Nat)
smallest power of two >= n, by structural recursion on n: returns (depth, 2^depth)
def cap_step source · line 573 · raw
@r:Pair(Nat, Nat) -> @+n:Nat -> Pair(Nat, Nat)
def cap_pair source · line 578 · raw
@n:Nat -> Pair(Nat, Nat)
def fst_nat source · line 585 · raw
@r:Pair(Nat, Nat) -> Nat
def snd_nat source · line 590 · raw
@r:Pair(Nat, Nat) -> Nat
def le_leb source · line 597 · raw
@a:Nat -> @b:Nat -> {Nat.is_le(a, b) == leb(a, b) : Bool}leb is Base's Nat.is_le, case for case. leb walks its arguments one step at a time in the compiled program; Base's comparison is one native comparison there (0.1.13.0).
def leb_of source · line 608 · raw
@+a:Nat -> @+b:Nat -> @e:{True{} == Nat.is_le(a, b) : Bool} -> {True{} == leb(a, b) : Bool}
def cap_dbl source · line 617 · raw
@f:Nat -> @ok:Bool -> @+n:Nat -> @+d:Nat -> @+p:Nat -> Pair(Nat, Nat)
the same pair by doubling (0.1.10.0): p doubles until n <= p, log2(n) steps in a tail loop, where cap_pair takes n steps of a recursion that is not a tail call, each with a comparison of up to n steps. Its fuel is n, which always suffices (2^n >= n); if it ran out, the answer would be cap_pair's, so the law below needs no arithmetic about how many doublings there are. ok is Nat.is_le(n, p), computed by the caller (match only inspects a parameter); with leb it was n steps in all, 152 ms for n = 786,432 on one GPU lane (0.1.13.0).
def cap_depth source · line 627 · raw
@+n:Nat -> Nat
the depth d such that an Array with 2^d slots holds n elements (the smallest such d)
def leb_succ source · line 633 · raw
@a:Nat -> @b:Nat -> @e:{True{} == leb(a, b) : Bool} -> {True{} == leb(a, 1n+b) : Bool}a <= b -> a <= 1 + b
def leb_up_l source · line 644 · raw
@+a:Nat -> @+b:Nat -> @c:Nat -> @+e:{True{} == leb(a, b) : Bool} -> {True{} == leb(a, Nat.add(c, b)) : Bool}a <= b -> a <= c + b
def leb_up source · line 652 · raw
@a:Nat -> @b:Nat -> @c:Nat -> @e:{True{} == leb(a, b) : Bool} -> {True{} == leb(a, Nat.add(b, c)) : Bool}a <= b -> a <= b + c
def leb_add source · line 663 · raw
@a:Nat -> @b:Nat -> @c:Nat -> @d:Nat -> @e1:{True{} == leb(a, b) : Bool} -> @e2:{True{} == leb(c, d) : Bool} -> {True{} == leb(Nat.add(a, c), Nat.add(b, d)) : Bool}a <= b and c <= d -> a + c <= b + d
def pow2_pos source · line 674 · raw
@d:Nat -> {True{} == leb(1n, pow2(d)) : Bool}1 <= 2^d
def pos_p source · line 682 · raw
@d:Nat -> @p:Nat -> @i1:{p == pow2(d) : Nat} -> {True{} == leb(1n, p) : Bool}1 <= p when p = 2^d
def step_eq source · line 687 · raw
@d:Nat -> @p:Nat -> @i1:{p == pow2(d) : Nat} -> {Nat.add(p, p) == pow2(1n+d) : Nat}p + p == 2^(1+d) when p == 2^d
def step_ok source · line 692 · raw
@ok:Bool -> @+d:Nat -> @+p:Nat -> @+m:Nat -> @+e:{ok == leb(1n+m, p) : Bool} -> @+i1:{p == pow2(d) : Nat} -> @+i2:{True{} == leb(m, p) : Bool} -> Pair({snd_nat(cap_pick(ok, d, p)) == pow2(fst_nat(cap_pick(ok, d, p))) : Nat}, {True{} == leb(1n+m, snd_nat(cap_pick(ok, d, p))) : Bool})one step: given the invariant for m, the invariant for 1+m holds whichever way the comparison goes
def step_pair_ok source · line 701 · raw
@+m:Nat -> @r:Pair(Nat, Nat) -> @i1:{snd_nat(r) == pow2(fst_nat(r)) : Nat} -> @i2:{True{} == leb(m, snd_nat(r)) : Bool} -> Pair({snd_nat(cap_step(r, 1n+m)) == pow2(fst_nat(cap_step(r, 1n+m))) : Nat}, {True{} == leb(1n+m, snd_nat(cap_step(r, 1n+m))) : Bool})the invariant for cap_step on a pair, opened
def cap_succ source · line 706 · raw
@+m:Nat -> @ih:Pair({snd_nat(cap_pair(m)) == pow2(fst_nat(cap_pair(m))) : Nat}, {True{} == leb(m, snd_nat(cap_pair(m))) : Bool}) -> Pair({snd_nat(cap_pair(1n+m)) == pow2(fst_nat(cap_pair(1n+m))) : Nat}, {True{} == leb(1n+m, snd_nat(cap_pair(1n+m))) : Bool})
def cap_inv source · line 712 · raw
@n:Nat -> Pair({snd_nat(cap_pair(n)) == pow2(fst_nat(cap_pair(n))) : Nat}, {True{} == leb(n, snd_nat(cap_pair(n))) : Bool})the invariant of cap_pair: the second component is 2^(first) and is >= n
def dbl_inv source · line 721 · raw
@f:Nat -> @ok:Bool -> @+n:Nat -> @+d:Nat -> @+p:Nat -> @e:{ok == Nat.is_le(n, p) : Bool} -> @i1:{p == pow2(d) : Nat} -> Pair({snd_nat(cap_dbl(f, ok, n, d, p)) == pow2(fst_nat(cap_dbl(f, ok, n, d, p))) : Nat}, {True{} == leb(n, snd_nat(cap_dbl(f, ok, n, d, p))) : Bool})the invariant of cap_dbl: whichever way it ends (n <= p found, or the fuel out and cap_pair's answer), the second component is 2^(first) and is >= n
def dbl_final source · line 730 · raw
@n:Nat -> @pr:Pair({snd_nat(cap_dbl(n, Nat.is_le(n, 1n), n, 0n, 1n)) == pow2(fst_nat(cap_dbl(n, Nat.is_le(n, 1n), n, 0n, 1n))) : Nat}, {True{} == leb(n, snd_nat(cap_dbl(n, Nat.is_le(n, 1n), n, 0n, 1n))) : Bool}) -> {True{} == leb(n, pow2(cap_depth(n))) : Bool}
def Mat.zeros source · line 746 · raw
@+r:Nat -> @+c:Nat -> Mat<r, c>
def Mat.fill source · line 749 · raw
@+r:Nat -> @+c:Nat -> @+v:F32 -> Mat<r, c>
def fill_list source · line 752 · raw
@xs:List<&2, F32> -> @+i:U32 -> @a:Array<F32> -> Array<F32>
def len_is source · line 759 · raw
@xs:List<&2, F32> -> @n:Nat -> Bool
def Mat.of_list2 source · line 768 · raw
@-r:Nat -> @-c:Nat -> @+rr:Nat -> @+cc:Nat -> @ok:Bool -> @xs:List<&2, F32> -> Maybe<&1, Mat<r, c>>
def Mat.of_list source · line 777 · raw
@+r:Nat -> @+c:Nat -> @+xs:List<&2, F32> -> Maybe<&1, Mat<r, c>>
def Mat.from_list source · line 785 · raw
@+r:Nat -> @+c:Nat -> @xs:List<&2, F32> -> Mat<r, c>
def Mat.fill_at source · line 790 · raw
@-r:Nat -> @-c:Nat -> @+i:U32 -> @xs:List<&2, F32> -> @m:Mat<r, c> -> Mat<r, c>
writes the numbers of xs into m starting at flat index i (row-major); numbers past the capacity are dropped. For loading a large matrix block by block without building one big list.
def Mat.to_list2 source · line 798 · raw
@-r:Nat -> @-c:Nat -> @al:AL -> ML<r, c>
def ML.len source · line 803 · raw
@-r:Nat -> @-c:Nat -> @ml:ML<r, c> -> Nat
def Mat.to_list_count source · line 809 · raw
@-r:Nat -> @-c:Nat -> @d:Array<F32> -> @+n:Nat -> ML<r, c>
n numbers from index 0, in row order; n = 0 gives the empty list (a Mat with no numbers)
def Mat.to_list source · line 818 · raw
@+r:Nat -> @+c:Nat -> @m:Mat<r, c> -> ML<r, c>
def Mat.clone2 source · line 826 · raw
@-r:Nat -> @-c:Nat -> @p:Pair(Array<F32>, Array<F32>) -> MM2<r, c>
def Mat.clone source · line 835 · raw
@-r:Nat -> @-c:Nat -> @m:Mat<r, c> -> MM2<r, c>
def Mat.reshape source · line 843 · raw
@-r1:Nat -> @-c1:Nat -> @+r2:Nat -> @+c2:Nat -> @-p:{Nat.mul(r1, c1) == Nat.mul(r2, c2) : Nat} -> @m:Mat<r1, c1> -> Mat<r2, c2>the same numbers under a new shape: the array is row-major and unchanged, and the equal size is
an erased proof, so r1*c1 == r2*c2 is checked at compile time. Use Mat.to_list to read them
back in row order (tests/roundtrip.bend); the law below counts them.
def t8_zero source · line 874 · raw
T8
def pick0 source · line 877 · raw
@b:Bool -> @+v:F32 -> F32
def pk_g7 source · line 888 · raw
@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+v3:F32 -> @+v4:F32 -> @+v5:F32 -> @+v6:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK
one cell: the 8 numbers of a at idx, idx + st, ..., those at positions q0 + q >= lim (past the end of the line) as 0
def pk_g6 source · line 893 · raw
@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+v3:F32 -> @+v4:F32 -> @+v5:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK
def pk_g5 source · line 898 · raw
@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+v3:F32 -> @+v4:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK
def pk_g4 source · line 903 · raw
@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+v3:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK
def pk_g3 source · line 908 · raw
@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK
def pk_g2 source · line 913 · raw
@+v0:F32 -> @+v1:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK
def pk_g1 source · line 918 · raw
@+v0:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK
def pk_g0 source · line 923 · raw
@+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK
def pk_line source · line 929 · raw
@tl:Nat -> @+q0:U32 -> @+lim:U32 -> @+base:U32 -> @+st:U32 -> @+o:U32 -> @r:PK -> PK
the cells of one line, cell o.. of t; q0 counts the line's numbers before the cell
def pk_lines source · line 937 · raw
@cnt:Nat -> @+tk:Nat -> @+tku:U32 -> @+ku:U32 -> @+i:U32 -> @+o:U32 -> @+lstep:U32 -> @+st:U32 -> @r:PK -> PK
lines count lines; source line i starts at i * lstep (in a), and goes to cell row o (cells o * tku .. in t)
def pk_pack source · line 944 · raw
@+cnt:Nat -> @+tk:Nat -> @+k:Nat -> @+lstep:U32 -> @+st:U32 -> @a:Array<F32> -> PK
def t8_dot source · line 949 · raw
@tk:Nat -> @+ia:U32 -> @+ib:U32 -> @s0:F32 -> @s1:F32 -> @s2:F32 -> @s3:F32 -> @ra:Pair(Array<T8>, T8) -> @rb:Pair(Array<T8>, T8) -> TD
the dot of two lines of cells: tk cells from ia (in a) and ib (in b); ra, rb hold the arrays and the cells at ia, ib, already read. Product q of a cell goes to sum q mod 4, the first four before the last four, as dotr adds them.
def t8_comb source · line 960 · raw
@+rot:Nat -> @+s0:F32 -> @+s1:F32 -> @+s2:F32 -> @+s3:F32 -> F32
dotr's final sum after k products: the sums sit rotated by k mod 4 when its loop ends
def t8_fin source · line 974 · raw
@+rot:Nat -> @d:TD -> TR
def t8_cell source · line 979 · raw
@+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+j:U32 -> @a:Array<T8> -> @b:Array<T8> -> TR
def t8_set source · line 985 · raw
@+ac:Bool -> @+idx:U32 -> @c:Array<F32> -> @r:TR -> TG
def t8_cols source · line 990 · raw
@+ac:Bool -> @mleft:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+j:U32 -> @+cidx:U32 -> @g:TG -> TG
def t8_rows source · line 997 · raw
@+ac:Bool -> @nleft:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+mu:U32 -> @g:TG -> TG
def t8_lcols source · line 1009 · raw
@mleft:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+j:U32 -> @out:List<&2, F32> -> @r:TR -> TL
cells j.. of row i, m - j of them; r is cell j, already computed
def t8_lrows source · line 1016 · raw
@nleft:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @l:TL -> TL
def t8_lfin source · line 1023 · raw
@l:TL -> List<&2, F32>
def t8_pk2 source · line 1031 · raw
@pa:PK -> @pb:PK -> PKK
def t8_g source · line 1036 · raw
@a:Array<F32> -> @b:Array<F32> -> @g:TG -> G
def t8_out_seq source · line 1041 · raw
@+ac:Bool -> @+n:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @c:Array<F32> -> @p:PKK -> G
def pkt_leaf source · line 1056 · raw
@r:PK -> PKT
def pkt_then source · line 1061 · raw
@r:PKT -> @k:(@_:Array<F32> -> @_:PT -> PKT) -> PKT
def pk_tree source · line 1067 · raw
@+d:Nat -> @+cnt:Nat -> @+i:U32 -> @+tk:Nat -> @+tku:U32 -> @+ku:U32 -> @+lstep:U32 -> @+st:U32 -> @a:Array<F32> -> PKT
lines i .. i+cnt-1 of a, packed into 2^d pieces (halves of the line count, as the fork tree splits them)
def t8_with2 source · line 1074 · raw
@-R:Type -> @p:Pair(Array<T8>, Array<T8>) -> @k:(@_:Array<T8> -> @_:Array<T8> -> R) -> R
def t8_rpar source · line 1080 · raw
@t:PT -> @+cnt:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @b:Array<T8> -> List<&2, F32>
split by rows of C: the pieces are A's rows, B is shared; the result is C's rows in order
def t8_cpar source · line 1094 · raw
@t:PT -> @+cnt:Nat -> @+j0:U32 -> @+n:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @a:Array<T8> -> List<&2, CB>
split by columns of C: the pieces are B's columns, A is shared
def cb_row source · line 1107 · raw
@+ac:Bool -> @w:Nat -> @+idx:U32 -> @c:Array<F32> -> @xs:List<&2, F32> -> RW
w numbers of xs into c from idx; what is left of xs comes back
def cb_rows source · line 1114 · raw
@+ac:Bool -> @n:Nat -> @+i:U32 -> @+j0:U32 -> @+w:Nat -> @+mu:U32 -> @r:RW -> Array<F32>
def cb_write source · line 1121 · raw
@+ac:Bool -> @bs:List<&2, CB> -> @+n:Nat -> @+mu:U32 -> @c:Array<F32> -> Array<F32>
def t8_pr2 source · line 1128 · raw
@+ac:Bool -> @+n:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @c:Array<F32> -> @ra:PKT -> @rb:PK -> G
def t8_pc2 source · line 1133 · raw
@+ac:Bool -> @+n:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @c:Array<F32> -> @ra:PK -> @rb:PKT -> G
def t8_par2 source · line 1139 · raw
@+ac:Bool -> @byrows:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
2^d blocks along the larger of n (rows of C, pieces of A) and m (columns of C, pieces of B)
def gemm_t8b source · line 1146 · raw
@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
def gemm_t8_ac source · line 1155 · raw
@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
C (n x m) = A · B (or C + A · B when ac) in cells of 8, sequential (d = 0) or with 2^d blocks of rows in parallel; A and B come back intact
def gemm_t8 source · line 1158 · raw
@d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
def gemm_any3 source · line 1166 · raw
@+ac:Bool -> @one:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
one row (matrix · vector): the scalar loop at every depth; two or more rows: cells of 8. Up to 0.1.11.0 one row in parallel ran gemm_par_cols, which clones both operands at every node of the fork tree: a 50,257 x 768 weight matrix at depth 4 ran out of the default heap (5.1 GB) and 8192 x 2048 took 124 ms against 10 ms sequentially. Packing the matrix into cells of 8 instead costs more than the product, which reads each weight once (64 ms against 24 at 50,257 x 768). Matrix · vector in parallel is Bands.matvec. Measured 2026-10-16 (NOTES.md).
def gemm_any2 source · line 1173 · raw
@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
def gemm_any source · line 1176 · raw
@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
def mmul_fin source · line 1179 · raw
@-n:Nat -> @-k:Nat -> @-m:Nat -> @g:G -> MMul<n, k, m>
def mmul_nt_fin source · line 1184 · raw
@-n:Nat -> @-k:Nat -> @-m:Nat -> @g:G -> MMulNT<n, k, m>
def mmul_tn_fin source · line 1189 · raw
@-n:Nat -> @-k:Nat -> @-m:Nat -> @g:G -> MMulTN<n, k, m>
def split_pick source · line 1202 · raw
@big:Bool -> @d:Nat -> Nat
---- the split depth of a product (0.1.11.0): par is an upper bound, and a product too small to gain splits 0 times ----
A product of n x k by k x m splits into 2^par blocks only when it does at least 2^23 multiply-adds and each cell of C sums at least 64 of them; otherwise it runs sequentially. Measured on 16 threads (2026-10-16, 18 shapes): below 2^23 the split costs more than it saves, and with a short inner dimension (a weight gradient over a few tokens) every split was 2-3.6x slower, because each block hands its rows of C back as a list that is then written into C, a cost per cell of C that k multiply-adds per cell do not pay for. The result does not depend on the depth: the same bits at every depth (tests/t8.bend, tests/acc.bend, tests/split.bend).
def split_depth source · line 1209 · raw
@d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> Nat
def mm_a source · line 1212 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @d:Nat -> @a:Array<F32> -> @b:Array<F32> -> MMul<n, k, m>
def Mat.matmul source · line 1217 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<k, m> -> MMul<n, k, m>
def Mat.matmul_acc source · line 1226 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<k, m> -> @c:Mat<n, m> -> MMul<n, k, m>
def mm_nt source · line 1231 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @d:Nat -> @a:Array<F32> -> @b:Array<F32> -> MMulNT<n, k, m>
def Mat.matmul_nt source · line 1236 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<m, k> -> MMulNT<n, k, m>
def Mat.matmul_nt_acc source · line 1243 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<m, k> -> @c:Mat<n, m> -> MMulNT<n, k, m>
def mm_tn source · line 1248 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @d:Nat -> @a:Array<F32> -> @b:Array<F32> -> MMulTN<n, k, m>
def Mat.matmul_tn source · line 1253 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<k, n> -> @b:Mat<k, m> -> MMulTN<n, k, m>
def Mat.matmul_tn_acc source · line 1260 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<k, n> -> @b:Mat<k, m> -> @c:Mat<n, m> -> MMulTN<n, k, m>
def sl_row source · line 1278 · raw
@k:Nat -> @+si:U32 -> @+di:U32 -> @d:Array<F32> -> @r:Pair(Array<F32>, F32) -> SL
d[di..] = src[si..], k numbers; r holds src[si], already read
def sl_rows source · line 1284 · raw
@r:Nat -> @+i:U32 -> @+w:Nat -> @+w32:U32 -> @+st:U32 -> @+o:U32 -> @sl:SL -> SL
rows i.. of a slice: w numbers a row from src[i*st + o], into d[i*w]
def blk_len source · line 1289 · raw
@+bi:U32 -> @+k:U32 -> Nat
def mp_s4 source · line 1292 · raw
@a:Array<F32> -> @b:Array<F32> -> @g:G -> G
def mp_s3 source · line 1296 · raw
@+ac:Bool -> @+par:Nat -> @+n:Nat -> @+m:Nat -> @+w:Nat -> @+w32:U32 -> @+m32:U32 -> @a:Array<F32> -> @as:Array<F32> -> @c:Array<F32> -> @sb:SL -> G
def mp_s2 source · line 1300 · raw
@+ac:Bool -> @+par:Nat -> @+n:Nat -> @+m:Nat -> @+w:Nat -> @+w32:U32 -> @+m32:U32 -> @+o:U32 -> @c:Array<F32> -> @b:Array<F32> -> @sa:SL -> G
def mp_step source · line 1305 · raw
@+ac:Bool -> @+par:Nat -> @+n:Nat -> @+m:Nat -> @+k32:U32 -> @+bi:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G
block bi: A's columns o..o+w (A is n x k) and B's rows o..o+w (B is k x m), multiplied into C (added when ac)
def mp_go source · line 1308 · raw
@nb:Nat -> @+bi:U32 -> @+par:Nat -> @+n:Nat -> @+m:Nat -> @+k32:U32 -> @g:G -> G
def mm_precise2 source · line 1313 · raw
@long:Bool -> @+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<k, m> -> MMul<n, k, m>
def Mat.matmul_precise source · line 1319 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @+par:Nat -> @a:Mat<n, k> -> @b:Mat<k, m> -> MMul<n, k, m>
C = A · B (n x k) · (k x m), k summed in blocks of 1024 (above); opt-in
def add_row2 source · line 1329 · raw
@-n:Nat -> @-m:Nat -> @+nn:Nat -> @+mm:Nat -> @cd:Array<F32> -> @al:AL -> MBias<n, m>
def Mat.add_row source · line 1334 · raw
@+n:Nat -> @+m:Nat -> @c:Mat<n, m> -> @b:Mat<1n, m> -> MBias<n, m>
def mpair_fin source · line 1344 · raw
@-r:Nat -> @-c:Nat -> @p:P2 -> MPair<r, c>
def relu_fin source · line 1349 · raw
@-r:Nat -> @-c:Nat -> @d:Array<F32> -> Mat<r, c>
def Mat.relu source · line 1352 · raw
@+r:Nat -> @+c:Nat -> @a:Mat<r, c> -> Mat<r, c>
def Mat.relu_bwd source · line 1359 · raw
@+r:Nat -> @+c:Nat -> @dy:Mat<r, c> -> @h:Mat<r, c> -> MPair<r, c>
def Mat.sgd source · line 1366 · raw
@+r:Nat -> @+c:Nat -> @+lr:F32 -> @w:Mat<r, c> -> @dw:Mat<r, c> -> MPair<r, c>
def Mat.add source · line 1373 · raw
@+r:Nat -> @+c:Nat -> @a:Mat<r, c> -> @b:Mat<r, c> -> MPair<r, c>
def colsum_fin source · line 1383 · raw
@-n:Nat -> @-m:Nat -> @g:G -> MColSum<n, m>
def Mat.col_sums source · line 1388 · raw
@+n:Nat -> @+m:Nat -> @a:Mat<n, m> -> MColSum<n, m>
def Mat.col_sums_acc source · line 1396 · raw
@+n:Nat -> @+m:Nat -> @a:Mat<n, m> -> @s:Mat<1n, m> -> MColSum<n, m>
def ce_fin source · line 1407 · raw
@-n:Nat -> @-c:Nat -> @+nf:F32 -> @r:L2 -> MCE<n, c>
def Mat.softmax_ce source · line 1412 · raw
@+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @labels:List<&2, Nat> -> MCE<n, c>
def hits_fin source · line 1422 · raw
@-n:Nat -> @-c:Nat -> @r:CR -> MHits<n, c>
def Mat.count_correct source · line 1427 · raw
@+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @labels:List<&2, Nat> -> MHits<n, c>
def row_fin source · line 1437 · raw
@-r:Nat -> @-c:Nat -> @al:AL -> MRow<r, c>
def Mat.read_row source · line 1442 · raw
@+r:Nat -> @+c:Nat -> @m:Mat<r, c> -> @+i:U32 -> MRow<r, c>
def Mat.write_row source · line 1447 · raw
@-r:Nat -> @+c:Nat -> @m:Mat<r, c> -> @+i:U32 -> @xs:List<&2, F32> -> Mat<r, c>
def labels_len_is source · line 1455 · raw
@xs:List<&2, Nat> -> @n:Nat -> Bool
does the list of labels have exactly n entries (one per row)?
def ce_checked source · line 1464 · raw
@ok:Bool -> @+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @labels:List<&2, Nat> -> Maybe<&1, MCE<n, c>>
def Mat.softmax_ce_checked source · line 1472 · raw
@+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @+labels:List<&2, Nat> -> Maybe<&1, MCE<n, c>>
like Mat.softmax_ce, but returns None unless labels has exactly n entries
def hits_checked source · line 1475 · raw
@ok:Bool -> @+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @labels:List<&2, Nat> -> Maybe<&1, MHits<n, c>>
def Mat.count_correct_checked source · line 1483 · raw
@+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @+labels:List<&2, Nat> -> Maybe<&1, MHits<n, c>>
like Mat.count_correct, but returns None unless labels has exactly n entries
def half.go source · line 1502 · raw
@n:Nat -> @acc:Nat -> Nat
n / 2 rounded down. A tail loop with an accumulator: a non-tail recursion here would put a continuation on every node of the band tree and keep the bands from running in parallel.
def half source · line 1513 · raw
@n:Nat -> Nat
def leb_refl source · line 1517 · raw
@+a:Nat -> {True{} == leb(a, a) : Bool}a <= a
def add_succ_r source · line 1525 · raw
@+a:Nat -> @-b:Nat -> {1n+Nat.add(a, b) == Nat.add(a, 1n+b) : Nat}1 + (a + b) == a + (1 + b)
def add_zero_r source · line 1534 · raw
@+a:Nat -> {Nat.add(a, 0n) == a : Nat}a + 0 == a
def half_go_le source · line 1543 · raw
@+n:Nat -> @+acc:Nat -> {True{} == leb(half.go(n, acc), Nat.add(n, acc)) : Bool}half.go(n, acc) <= n + acc
def half_le source · line 1556 · raw
@+n:Nat -> {True{} == leb(half(n), n) : Bool}half(n) <= n
def add_sub source · line 1561 · raw
@+a:Nat -> @+r:Nat -> @e:{True{} == leb(a, r) : Bool} -> {r == Nat.add(a, Nat.sub(r, a)) : Nat}a <= r -> r == a + (r - a)
def Bands.zeros source · line 1588 · raw
@+d:Nat -> @+r:Nat -> @+c:Nat -> Bands<r, c>
r x c of zeros in 2^d bands (d = 0: a single band, the same as a Mat)
def eq_half source · line 1598 · raw
@-a:Nat -> @-b:Nat -> @e:{a == b : Nat} -> {half(a) == half(b) : Nat}the row counts of the two bands follow the row count of the node: if rr == r at run time, the same holds for both halves (the bands carry r only in their type)
def eq_rest source · line 1602 · raw
@-a:Nat -> @-b:Nat -> @e:{a == b : Nat} -> {Nat.sub(a, half(a)) == Nat.sub(b, half(b)) : Nat}
def bfill_l source · line 1608 · raw
@+r:Nat -> @+c:Nat -> @+i:Nat -> @+n:Nat -> Bool
does a block of n numbers starting at flat index i end before the second band of a node with r rows? does it start in the second band?
def bfill_r source · line 1611 · raw
@+r:Nat -> @+c:Nat -> @+i:Nat -> Bool
def fill_n source · line 1615 · raw
@n:Nat -> @xs:List<&2, F32> -> @+i:U32 -> @a:Array<F32> -> Array<F32>
writes the first n numbers of xs at index i (and nothing past them)
def fill_band source · line 1622 · raw
@-r:Nat -> @-c:Nat -> @n:Nat -> @xs:List<&2, F32> -> @+i:U32 -> @m:Mat<r, c> -> Mat<r, c>
def bfill source · line 1630 · raw
@+c:Nat -> @-r:Nat -> @b:Bands<r, c> -> @l:Bool -> @rt:Bool -> @+rr:Nat -> @-e:{rr == r : Nat} -> @+i:Nat -> @+n:Nat -> @+xs:List<&2, F32> -> Bands<r, c>writes the first n numbers of xs at flat index i; l and rt say which bands they touch (bfill_l, bfill_r); rr is the row count r at run time. A block that crosses into the second band is not copied: the first band writes only its part, the second gets the rest by List.drop.
def bfill_top source · line 1642 · raw
@ok:Bool -> @+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> @+i:Nat -> @+n:Nat -> @+xs:List<&2, F32> -> Bands<r, c>
def Bands.fill_at source · line 1651 · raw
@+r:Nat -> @+c:Nat -> @+i:Nat -> @+n:Nat -> @+xs:List<&2, F32> -> @b:Bands<r, c> -> Bands<r, c>
writes the n numbers of xs into b starting at flat index i (row-major, as Mat.fill_at); numbers past r*c are dropped. For loading a large matrix block by block.
def bv_g2 source · line 1664 · raw
@-r:Nat -> @-c:Nat -> @w:Array<F32> -> @al:AL -> BL<r, c>
def bv_g source · line 1670 · raw
@-r:Nat -> @-c:Nat -> @+m:Nat -> @g:G -> BL<r, c>
the band's 1 + m rows came out in an Array (one number per row); read them back as a list
def bv_gemm source · line 1678 · raw
@-r:Nat -> @+c:Nat -> @rows:Nat -> @+rr:Nat -> @x:Array<F32> -> @w:Array<F32> -> BL<r, c>
the rows of one band, one dot product each, with the gemm of Mat.matmul_nt (n = 1), so the results are identical. Writing the results into an Array and reading them back is faster than building the list row by row (NOTES.md, exp. 14).
def bv_mat source · line 1685 · raw
@-r:Nat -> @+c:Nat -> @+rr:Nat -> @x:Array<F32> -> @m:Mat<r, c> -> BL<r, c>
def bv_join source · line 1690 · raw
@-r:Nat -> @-c:Nat -> @p:BL<half(r), c> -> @q:BL<Nat.sub(r, half(r)), c> -> BL<r, c>
def bmv source · line 1699 · raw
@+c:Nat -> @-r:Nat -> @b:Bands<r, c> -> @+rr:Nat -> @-e:{rr == r : Nat} -> @xp:Pair(Array<F32>, Array<F32>) -> BL<r, c>the two bands of a node run in parallel, each with its own copy of x (c numbers); xp is a pair of copies of x (a leaf uses one). xp comes after the erased e: up to Bend 2.0.36 an erased parameter at the end of the list turned the parallel let into two sequential calls (NOTES.md, exp. 11; fixed in 2.0.37 by bendlang/bend#1377). The order is kept, and harmless.
def bv_fin source · line 1707 · raw
@+r:Nat -> @-c:Nat -> @bl:BL<r, c> -> BV<r, c>
def Bands.matvec source · line 1714 · raw
@+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> @x:Mat<1n, c> -> BV<r, c>
y = B · x for B (r x c, one row per output) and x (1 x c); returns B intact and y (1 x r). The same product as Mat.matmul_nt(1n, c, r, ..), and the same numbers, bit for bit.
def Bands.matvec_l source · line 1721 · raw
@+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> @+x:List<&2, F32> -> BL<r, c>
the same product with x and y as lists (x: c numbers, y: r numbers in row order), without the Mat conversions of Bands.matvec: for callers that already hold the vector as a list
def brow_fin source · line 1729 · raw
@-r:Nat -> @-c:Nat -> @al:AL -> BRow<r, c>
def brow_leaf2 source · line 1735 · raw
@-r:Nat -> @-c:Nat -> @+n:Nat -> @+i:U32 -> @d:Array<F32> -> BRow<r, c>
n numbers starting at flat index i of one band (a band may have no rows)
def brow_leaf source · line 1742 · raw
@-r:Nat -> @-c:Nat -> @m:Mat<r, c> -> @+i:U32 -> @n:Nat -> BRow<r, c>
def brow_l source · line 1747 · raw
@-r:Nat -> @-c:Nat -> @p:BRow<half(r), c> -> @y:Bands<Nat.sub(r, half(r)), c> -> BRow<r, c>
def brow_r source · line 1752 · raw
@-r:Nat -> @-c:Nat -> @x:Bands<half(r), c> -> @q:BRow<Nat.sub(r, half(r)), c> -> BRow<r, c>
def brow source · line 1758 · raw
@+c:Nat -> @-r:Nat -> @b:Bands<r, c> -> @left:Bool -> @+rr:Nat -> @-e:{rr == r : Nat} -> @+i:Nat -> BRow<r, c>row i; left says whether i is in the first band of the node (i < half(rr))
def Bands.read_row source · line 1768 · raw
@+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> @+i:Nat -> BRow<r, c>
row i (c numbers), for example an embedding lookup
def bl_join source · line 1771 · raw
@-r:Nat -> @-c:Nat -> @p:BRow<half(r), c> -> @q:BRow<Nat.sub(r, half(r)), c> -> BRow<r, c>
def blist source · line 1776 · raw
@+c:Nat -> @-r:Nat -> @b:Bands<r, c> -> @+rr:Nat -> @-e:{rr == r : Nat} -> BRow<r, c>
def Bands.to_list source · line 1785 · raw
@+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> BRow<r, c>
all r*c numbers in row order (in BRow.row)
def bl_len source · line 1793 · raw
@-r:Nat -> @-c:Nat -> @bl:BL<r, c> -> Nat
def succ_out source · line 1799 · raw
@-s:Nat -> @+a:Nat -> @+b:Nat -> @e:{s == Nat.add(a, 1n+b) : Nat} -> {s == 1n+Nat.add(a, b) : Nat}s == a + (1 + b) -> s == 1 + (a + b)
def len_step source · line 1804 · raw
@-s:Nat -> @+p:Nat -> @+l:Nat -> @e:{s == 1n+Nat.add(p, 1n+l) : Nat} -> {s == 2n+Nat.add(p, l) : Nat}s == 1 + (p + (1 + l)) -> s == 2 + (p + l)
def zero_out source · line 1809 · raw
@-s:Nat -> @+n:Nat -> @e:{s == Nat.add(n, 0n) : Nat} -> {s == n : Nat}s == n + 0 -> s == n
def app_len source · line 1814 · raw
@+xs:List<&2, F32> -> @+ys:List<&2, F32> -> {List.length(&2, F32, List.append(&2, F32, xs, ys)) == Nat.add(List.length(&2, F32, xs), List.length(&2, F32, ys)) : Nat}appending adds the lengths
def read_ml_len source · line 1823 · raw
@-r:Nat -> @-c:Nat -> @+mleft:Nat -> @+idx:U32 -> @+acc:List<&2, F32> -> @rc:Pair(Array<F32>, F32) -> {ML.len(r, c, Mat.to_list2(r, c, read_l(mleft, idx, acc, rc))) == Nat.add(1n+mleft, List.length(&2, F32, acc)) : Nat}the same count for the list of a whole Mat: to_list2 wraps read_l's answer, and ML.len counts it
def g_len source · line 1852 · raw
@-r:Nat -> @-c:Nat -> @+p:Nat -> @g:G -> {bl_len(r, c, bv_g(r, c, p, g)) == 1n+p : Nat}whatever the gemm wrote, reading 1 + p rows back gives 1 + p numbers (G has one constructor, so matching g opens it, and leaf_len counts the list)
def join_len source · line 1876 · raw
@-r:Nat -> @-c:Nat -> @-h1:Nat -> @-h2:Nat -> @p:BL<half(r), c> -> @q:BL<Nat.sub(r, half(r)), c> -> @hp:{bl_len(half(r), c, p) == h1 : Nat} -> @hq:{bl_len(Nat.sub(r, half(r)), c, q) == h2 : Nat} -> {bl_len(r, c, bv_join(r, c, p, q)) == Nat.add(h1, h2) : Nat}joining two halves of h1 and h2 numbers gives h1 + h2 numbers. (The law for the whole tree, matvec_l_len below, cannot use it: it would need each band's Array twice. It goes through witnesses instead.)
def al_c source · line 1893 · raw
@al:AL -> Array<F32>
def bl_b source · line 1898 · raw
@-r:Nat -> @-c:Nat -> @bl:BL<r, c> -> Bands<r, c>
def wal_step source · line 1912 · raw
@-s:AL -> @+p:Nat -> @+l:Nat -> @w:WAL<s, 1n+Nat.add(p, 1n+l)> -> WAL<s, 2n+Nat.add(p, l)>
moves the 1 out of the length's sum (len_step on the witness)
def read_w source · line 1918 · raw
@+mleft:Nat -> @+idx:U32 -> @+acc:List<&2, F32> -> @rc:Pair(Array<F32>, F32) -> WAL<read_l(mleft, idx, acc, rc), Nat.add(1n+mleft, List.length(&2, F32, acc))>
read_l reads mleft + 1 numbers onto acc: by induction on mleft, as leaf_len
def wal_zero source · line 1926 · raw
@-s:AL -> @+n:Nat -> @w:WAL<s, Nat.add(n, 0n)> -> WAL<s, n>
n + 0 -> n in the length
def g2_w source · line 1932 · raw
@-r:Nat -> @-c:Nat -> @-w:Array<F32> -> @-al:AL -> @-n:Nat -> @a:WAL<al, n> -> WBL<r, c, bv_g2(r, c, w, al), n>
a band's list is the read's list: rewrite the read into its constructor, then bv_g2 unfolds
def g_w source · line 1939 · raw
@-r:Nat -> @-c:Nat -> @+p:Nat -> @g:G -> WBL<r, c, bv_g(r, c, p, g), 1n+p>
whatever the gemm wrote, reading 1 + p rows back gives 1 + p numbers
def gemm_w source · line 1945 · raw
@-r:Nat -> @+c:Nat -> @rows:Nat -> @+rr:Nat -> @x:Array<F32> -> @w:Array<F32> -> WBL<r, c, bv_gemm(r, c, rows, rr, x, w), rows>
one band of rows rows: no numbers for 0 rows, else g_w
def mat_w source · line 1953 · raw
@-r:Nat -> @+c:Nat -> @+rr:Nat -> @x:Array<F32> -> @m:Mat<r, c> -> WBL<r, c, bv_mat(r, c, rr, x, m), rr>
a leaf of the tree is one band of rr rows
def join_n source · line 1959 · raw
@+rr:Nat -> @+y1:List<&2, F32> -> @+y2:List<&2, F32> -> @l1:{List.length(&2, F32, y1) == half(rr) : Nat} -> @l2:{List.length(&2, F32, y2) == Nat.sub(rr, half(rr)) : Nat} -> {List.length(&2, F32, List.append(&2, F32, y1, y2)) == rr : Nat}two halves of half(rr) and rr - half(rr) numbers give rr numbers
def node_w source · line 1966 · raw
@-r:Nat -> @-c:Nat -> @+rr:Nat -> @-p:BL<half(r), c> -> @-q:BL<Nat.sub(r, half(r)), c> -> @w1:WBL<half(r), c, p, half(rr)> -> @w2:WBL<Nat.sub(r, half(r)), c, q, Nat.sub(rr, half(rr))> -> WBL<r, c, bv_join(r, c, p, q), rr>
a node: both halves rewritten into constructors (e1, e2), so bv_join unfolds to BL{BNode{..}, y1 ++ y2}
def bmv_w source · line 1974 · raw
@+c:Nat -> @-r:Nat -> @b:Bands<r, c> -> @+rr:Nat -> @-e:{rr == r : Nat} -> @xp:Pair(Array<F32>, Array<F32>) -> WBL<r, c, bmv(c, r, b, rr, e, xp), rr>the tree, by induction on the bands; each band and each copy of x is used once, by its own call
def w_len source · line 1983 · raw
@-r:Nat -> @-c:Nat -> @-s:BL<r, c> -> @-n:Nat -> @w:WBL<r, c, s, n> -> {bl_len(r, c, s) == n : Nat}a witness gives the count
def to_list_count_len source · line 2001 · raw
@-r:Nat -> @-c:Nat -> @d:Array<F32> -> @+n:Nat -> {ML.len(r, c, Mat.to_list_count(r, c, d, n)) == n : Nat}Mat.to_list_count reads exactly n numbers, and n = 0 reads none.
def ASet.sz source · line 2077 · raw
@-T:Data -> @a:Array<T> -> U32
ASet.sz(T, a): the size of a as a plain U32, the second component of the pair Array.size returns
def ASet.G source · line 2081 · raw
@-T:Data -> @a:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+z:Bool -> Pair(Array<T>, T)
the three results the descents build: the swapped pair, the written array, and the read pair
def ASet.NA source · line 2084 · raw
@-T:Data -> @a:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+z:Bool -> Array<T>
def ASet.K source · line 2087 · raw
@-T:Data -> @a:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+z:Bool -> Pair(Array<T>, T)
def ASet.Facts source · line 2097 · raw
@-T:Data -> @-a:Array<T> -> @-n:U32 -> @-j:U32 -> @-v:T -> @-z:Bool -> Type
ASet.Facts: what one pass over the tree a proves, for any n, j, v, z (n need not be a's real size): 1. Array.size(a) is the pair (a, ASet.sz(a)); 2. the pair the write's descent returns is (the written array, the old value); 3. Array.size of the written array is the pair (written array, its sz); 4. the written array has a's sz; 5. reading the written array along the same path gives v; 6. the pair the read returns is (its first component, its second).
def ASet.size_pair source · line 2101 · raw
@-T:Data -> @a:Array<T> -> {(a, ASet.sz(T, a)) == Array.size(T, a) : Pair(Array<T>, U32)}Array.size hands back the array it measured, next to ASet.sz(T, a)
def ASet.node_t source · line 2109 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @f:ASet.Facts(T, xs, U32.shr(n), j, v, U32.is_lt(j, U32.shr(U32.shr(n)))) -> ASet.Facts(T, ANode{xs, ys}, n, j, v, True{})
def ASet.node_f source · line 2124 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+s:{(xs, ASet.sz(T, xs)) == Array.size(T, xs) : Pair(Array<T>, U32)} -> @f:ASet.Facts(T, ys, U32.shr(n), U32.sub(j, U32.shr(n)), v, U32.is_lt(U32.sub(j, U32.shr(n)), U32.shr(U32.shr(n)))) -> ASet.Facts(T, ANode{xs, ys}, n, j, v, False{})
def ASet.facts source · line 2139 · raw
@-T:Data -> @a:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+z:Bool -> ASet.Facts(T, a, n, j, v, z)
def ASet.Copy source · line 2150 · raw
@-T:Data -> @-a:Array<T> -> Type
ASet.copy(T, a): one pass over a hands back the same array b, its size n as a value, and the facts that tie them to a: b is a, n is ASet.sz(T, b), and Array.size(b) is the pair (b, ASet.sz(T, b))
def ASet.copy_node source · line 2153 · raw
@-T:Data -> @-xs:Array<T> -> @ys:Array<T> -> @r:ASet.Copy(T, xs) -> ASet.Copy(T, ANode{xs, ys})
def ASet.copy source · line 2170 · raw
@-T:Data -> @a:Array<T> -> ASet.Copy(T, a)
def ASet.J source · line 2178 · raw
@+i:U32 -> @+n:U32 -> U32
the masked index and the first branch, as Array.swap.at and Array.get.at compute them from a size n
def ASet.Z source · line 2181 · raw
@+i:U32 -> @+n:U32 -> Bool
def ASet.close_size source · line 2184 · raw
@-T:Data -> @-b:Array<T> -> @+n:U32 -> @+i:U32 -> @+v:T -> @+q:{n == ASet.sz(T, b) : U32} -> @f:ASet.Facts(T, b, n, ASet.J(i, n), v, ASet.Z(i, n)) -> {ASet.sz(T, Array.set(T, b, i, v)) == ASet.sz(T, b) : U32}
def ASet.close_get source · line 2197 · raw
@-T:Data -> @-b:Array<T> -> @+n:U32 -> @+i:U32 -> @+v:T -> @+q:{n == ASet.sz(T, b) : U32} -> @f:ASet.Facts(T, b, n, ASet.J(i, n), v, ASet.Z(i, n)) -> {Pair.snd(Array<T>, T, Array.get(T, Array.set(T, b, i, v), i)) == v : T}
def ASet.top_size source · line 2214 · raw
@-T:Data -> @-a:Array<T> -> @+i:U32 -> @+v:T -> @r:ASet.Copy(T, a) -> {ASet.sz(T, Array.set(T, a, i, v)) == ASet.sz(T, a) : U32}
def ASet.top_get source · line 2226 · raw
@-T:Data -> @-a:Array<T> -> @+i:U32 -> @+v:T -> @r:ASet.Copy(T, a) -> {Pair.snd(Array<T>, T, Array.get(T, Array.set(T, a, i, v), i)) == v : T}
def ASet.Apart source · line 2263 · raw
@-T:Data -> @a:Array<T> -> @+n:U32 -> @+j:U32 -> @+z:Bool -> @+k:U32 -> @+w:Bool -> Type
ASet.Apart(T, a, n, j, z, k, w): the write's descent (index j, first branch z) and the read's descent (k, w), both started from the size n, take different branches at some node of a before either reaches a leaf. It is Empty when both reach the same leaf, and Unit at the node where they split.
def ASet.Side source · line 2278 · raw
@-T:Data -> @-a:Array<T> -> @-n:U32 -> @-k:U32 -> @-w:Bool -> Type
ASet.Side: what a pass over a subtree the write does not enter proves: Array.size(a) is (a, ASet.sz(a)), and the pair a read along (n, k, w) returns is (its first component, its second)
def ASet.side_node source · line 2281 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+k:U32 -> @+w:Bool -> @sx:ASet.Side(T, xs, U32.shr(n), k, U32.is_lt(k, U32.shr(U32.shr(n)))) -> @sy:ASet.Side(T, ys, U32.shr(n), U32.sub(k, U32.shr(n)), U32.is_lt(U32.sub(k, U32.shr(n)), U32.shr(U32.shr(n)))) -> ASet.Side(T, ANode{xs, ys}, n, k, w)
def ASet.side source · line 2304 · raw
@-T:Data -> @a:Array<T> -> @+n:U32 -> @+k:U32 -> @+w:Bool -> ASet.Side(T, a, n, k, w)
def ASet.Reads source · line 2317 · raw
@-T:Data -> @-a:Array<T> -> @-n:U32 -> @-j:U32 -> @-v:T -> @-z:Bool -> @-k:U32 -> @-w:Bool -> Type
ASet.Reads: what one pass over the tree a proves about a write along (n, j, v, z) and a read along (n, k, w): 1. the pair the write's descent returns is (the written array, the old value); 2. the read of a returns a pair (first component, second); 3. so does the read of the written array; 4. if the descents split, the read finds the same value in the written array as in a.
def ASet.WL source · line 2321 · raw
@-T:Data -> @xs:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> Array<T>
the written low half ASet.WL and the written high half ASet.WH of a node of size n
def ASet.WH source · line 2324 · raw
@-T:Data -> @ys:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> Array<T>
def ASet.RL source · line 2328 · raw
@-T:Data -> @xs:Array<T> -> @+n:U32 -> @+k:U32 -> Pair(Array<T>, T)
the read's descent into the low half and into the high half of a node of size n
def ASet.RH source · line 2331 · raw
@-T:Data -> @ys:Array<T> -> @+n:U32 -> @+k:U32 -> Pair(Array<T>, T)
def ASet.no_leaf source · line 2334 · raw
@-T:Data -> @-x:T -> @-n:U32 -> @-j:U32 -> @-v:T -> @-z:Bool -> @-k:U32 -> @-w:Bool -> @h:ASet.Apart(T, [x], n, j, z, k, w) -> {Pair.snd(Array<T>, T, Array.get.go(T, ASet.NA(T, [x], n, j, v, z), n, k, w)) == Pair.snd(Array<T>, T, Array.get.go(T, [x], n, k, w)) : T}
def ASet.reads_t source · line 2338 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+k:U32 -> @+w:Bool -> @+g:{(Pair.fst(Array<T>, T, ASet.RH(T, ys, n, k)), Pair.snd(Array<T>, T, ASet.RH(T, ys, n, k))) == ASet.RH(T, ys, n, k) : Pair(Array<T>, T)} -> @r:ASet.Reads(T, xs, U32.shr(n), j, v, U32.is_lt(j, U32.shr(U32.shr(n))), k, U32.is_lt(k, U32.shr(U32.shr(n)))) -> ASet.Reads(T, ANode{xs, ys}, n, j, v, True{}, k, w)the write goes into the low half xs
def ASet.reads_f source · line 2370 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+k:U32 -> @+w:Bool -> @+g:{(Pair.fst(Array<T>, T, ASet.RL(T, xs, n, k)), Pair.snd(Array<T>, T, ASet.RL(T, xs, n, k))) == ASet.RL(T, xs, n, k) : Pair(Array<T>, T)} -> @r:ASet.Reads(T, ys, U32.shr(n), U32.sub(j, U32.shr(n)), v, U32.is_lt(U32.sub(j, U32.shr(n)), U32.shr(U32.shr(n))), U32.sub(k, U32.shr(n)), U32.is_lt(U32.sub(k, U32.shr(n)), U32.shr(U32.shr(n)))) -> ASet.Reads(T, ANode{xs, ys}, n, j, v, False{}, k, w)the write goes into the high half ys
def ASet.Both source · line 2402 · raw
@-T:Data -> @-a:Array<T> -> @-n:U32 -> @-j:U32 -> @-v:T -> @-z:Bool -> @-k:U32 -> @-w:Bool -> Type
ASet.Both: the six facts of the first two laws and the four of the third, from one pass
def ASet.both_t source · line 2405 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+k:U32 -> @+w:Bool -> @sy:ASet.Side(T, ys, U32.shr(n), U32.sub(k, U32.shr(n)), U32.is_lt(U32.sub(k, U32.shr(n)), U32.shr(U32.shr(n)))) -> @f:ASet.Both(T, xs, U32.shr(n), j, v, U32.is_lt(j, U32.shr(U32.shr(n))), k, U32.is_lt(k, U32.shr(U32.shr(n)))) -> ASet.Both(T, ANode{xs, ys}, n, j, v, True{}, k, w)
def ASet.both_f source · line 2414 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+k:U32 -> @+w:Bool -> @sx:ASet.Side(T, xs, U32.shr(n), k, U32.is_lt(k, U32.shr(U32.shr(n)))) -> @f:ASet.Both(T, ys, U32.shr(n), U32.sub(j, U32.shr(n)), v, U32.is_lt(U32.sub(j, U32.shr(n)), U32.shr(U32.shr(n))), U32.sub(k, U32.shr(n)), U32.is_lt(U32.sub(k, U32.shr(n)), U32.shr(U32.shr(n)))) -> ASet.Both(T, ANode{xs, ys}, n, j, v, False{}, k, w)
def ASet.both source · line 2423 · raw
@-T:Data -> @a:Array<T> -> @+n:U32 -> @+j:U32 -> @+v:T -> @+z:Bool -> @+k:U32 -> @+w:Bool -> ASet.Both(T, a, n, j, v, z, k, w)
def ASet.close_other source · line 2433 · raw
@-T:Data -> @-b:Array<T> -> @+n:U32 -> @+i:U32 -> @+k:U32 -> @+v:T -> @+q:{n == ASet.sz(T, b) : U32} -> @f:ASet.Both(T, b, n, ASet.J(i, n), v, ASet.Z(i, n), ASet.J(k, n), ASet.Z(k, n)) -> @_:ASet.Apart(T, b, ASet.sz(T, b), ASet.J(i, ASet.sz(T, b)), ASet.Z(i, ASet.sz(T, b)), ASet.J(k, ASet.sz(T, b)), ASet.Z(k, ASet.sz(T, b))) -> {Pair.snd(Array<T>, T, Array.get(T, Array.set(T, b, i, v), k)) == Pair.snd(Array<T>, T, Array.get(T, b, k)) : T}
def ASet.top_other source · line 2459 · raw
@-T:Data -> @-a:Array<T> -> @+i:U32 -> @+k:U32 -> @+v:T -> @r:ASet.Copy(T, a) -> @_:ASet.Apart(T, a, ASet.sz(T, a), ASet.J(i, ASet.sz(T, a)), ASet.Z(i, ASet.sz(T, a)), ASet.J(k, ASet.sz(T, a)), ASet.Z(k, ASet.sz(T, a))) -> {Pair.snd(Array<T>, T, Array.get(T, Array.set(T, a, i, v), k)) == Pair.snd(Array<T>, T, Array.get(T, a, k)) : T}
def Bits.disc source · line 2516 · raw
@b:Bool -> Type
def Bits.tf_e source · line 2523 · raw
@h:{True{} == False{} : Bool} -> Empty
def Bits.ft_e source · line 2527 · raw
@h:{False{} == True{} : Bool} -> Empty
def Bits.ft source · line 2531 · raw
@-A:Type -> @h:{False{} == True{} : Bool} -> A
def Bits.tf source · line 2534 · raw
@-A:Type -> @h:{True{} == False{} : Bool} -> A
def Bits.pow source · line 2542 · raw
@m:Nat -> @e:Nat -> Word(m)
Bits.pow(m, e): the m-bit word with only bit e set, the number 2^e (zero when e >= m)
def Bits.mask source · line 2554 · raw
@m:Nat -> @e:Nat -> Word(m)
Bits.mask(m, e): bits 0 .. e-1 set, the number 2^e - 1
def Bits.at source · line 2566 · raw
@m:Nat -> @w:Word(m) -> @e:Nat -> Bool
Bits.at(m, w, e): bit e of w
def Bits.below source · line 2580 · raw
@m:Nat -> @w:Word(m) -> @e:Nat -> Bool
Bits.below(m, w, e): every bit of w from e up is zero, i.e. w < 2^e
def Bits.clear source · line 2594 · raw
@m:Nat -> @w:Word(m) -> @e:Nat -> Word(m)
Bits.clear(m, w, e): w with bit e cleared
def Bits.differ source · line 2608 · raw
@m:Nat -> @a:Word(m) -> @b:Word(m) -> @e:Nat -> Bool
Bits.differ(m, a, b, e): a and b differ at some bit below e
def Bits.put_f source · line 2626 · raw
@+p:Nat -> @w:Word(p) -> {Word.shl.put(p, False{}, w) == Word.shl(p, w) : Word(p)}shifting in a zero is Word.shl
def Bits.shl_zero source · line 2635 · raw
@+p:Nat -> {Word.shl(p, Word.zero(p)) == Word.zero(p) : Word(p)}
def Bits.put_one source · line 2645 · raw
@+p:Nat -> {Word.shl.put(p, True{}, Word.zero(p)) == Bits.pow(p, 0n) : Word(p)}shifting a one into zero gives 2^0
def Bits.shl_p source · line 2655 · raw
@+m:Nat -> @+e:Nat -> {Word.shl(m, Bits.pow(m, e)) == Bits.pow(m, 1n+e) : Word(m)}shifting 2^e left gives 2^(e+1)
def Bits.shln_one source · line 2670 · raw
@+e:Nat -> {U32.shln(1, e) == U32{Bits.pow(32n, e)} : U32}U32.shln(1, e), the size Array.size computes for a tree of depth e, is the word with bit e set
def Bits.pad_zero source · line 2683 · raw
@+p:Nat -> {Word.shr.pad(p, Word.zero(p)) == Word.zero(1n+p) : Word(1n+p)}
def Bits.pad_p source · line 2691 · raw
@+p:Nat -> @+e:Nat -> @h:{Nat.is_lt(e, p) == True{} : Bool} -> {Word.shr.pad(p, Bits.pow(p, e)) == Bits.pow(1n+p, e) : Word(1n+p)}
def Bits.shr_p source · line 2705 · raw
@+p:Nat -> @+e:Nat -> @h:{Nat.is_lt(e, p) == True{} : Bool} -> {Word.shr(1n+p, Bits.pow(1n+p, 1n+e)) == Bits.pow(1n+p, e) : Word(1n+p)}below the top bit, shifting 2^(e+1) right gives 2^e
def Bits.cmp_zero source · line 2712 · raw
@+p:Nat -> @t:Word(p) -> @h:{Bits.below(p, t, 0n) == True{} : Bool} -> {Word.cmp(p, t, Word.zero(p)) == EQ{} : Cmp}
def Bits.fin_lt source · line 2724 · raw
@b:Bool -> @c:Cmp -> {Cmp.is_lt(Word.cmp.fin(b, False{}, c)) == Cmp.is_lt(c) : Bool}
def Bits.fin_t source · line 2739 · raw
@b:Bool -> {Cmp.is_lt(Word.cmp.fin(b, True{}, EQ{})) == Bool.not(b) : Bool}
def Bits.lt_p source · line 2747 · raw
@+m:Nat -> @+x:Word(m) -> @+e:Nat -> @hb:{Nat.is_lt(e, m) == True{} : Bool} -> @h:{Bits.below(m, x, 1n+e) == True{} : Bool} -> {Cmp.is_lt(Word.cmp(m, x, Bits.pow(m, e))) == Bool.not(Bits.at(m, x, e)) : Bool}when x < 2^(e+1), x < 2^e exactly when bit e of x is zero
def Bits.sub_zero source · line 2763 · raw
@+p:Nat -> @x:Word(p) -> {Word.adc(p, x, Word.zero(p), True{}, True{}) == x : Word(p)}
def Bits.sub_p source · line 2774 · raw
@+m:Nat -> @x:Word(m) -> @+e:Nat -> @h:{Bits.at(m, x, e) == True{} : Bool} -> {Word.adc(m, x, Bits.pow(m, e), True{}, True{}) == Bits.clear(m, x, e) : Word(m)}
def Bits.dec_p source · line 2790 · raw
@+p:Nat -> @+q:Nat -> {Word.adc(p, Bits.pow(p, q), Word.zero(p), True{}, False{}) == Bits.mask(p, q) : Word(p)}
def Bits.dec source · line 2802 · raw
@+m:Nat -> @+e:Nat -> {Word.adc(m, Bits.pow(m, e), Bits.pow(m, 0n), True{}, True{}) == Bits.mask(m, e) : Word(m)}2^e - 1 is the mask of bits 0 .. e-1
def Bits.and_zero source · line 2813 · raw
@+p:Nat -> @t:Word(p) -> @h:{Bits.below(p, t, 0n) == True{} : Bool} -> {Word.and(p, t, Word.zero(p)) == t : Word(p)}
def Bits.and_mask source · line 2824 · raw
@+m:Nat -> @x:Word(m) -> @+e:Nat -> @h:{Bits.below(m, x, e) == True{} : Bool} -> {Word.and(m, x, Bits.mask(m, e)) == x : Word(m)}a word below 2^e is unchanged by the mask
def Bits.hz_down source · line 2844 · raw
@+m:Nat -> @x:Word(m) -> @+e:Nat -> @h1:{Bits.below(m, x, 1n+e) == True{} : Bool} -> @h2:{Bits.at(m, x, e) == False{} : Bool} -> {Bits.below(m, x, e) == True{} : Bool}
def Bits.hz_clr source · line 2855 · raw
@+m:Nat -> @x:Word(m) -> @+e:Nat -> @h:{Bits.below(m, x, 1n+e) == True{} : Bool} -> {Bits.below(m, Bits.clear(m, x, e), e) == True{} : Bool}
def Bits.dif_zero source · line 2864 · raw
@+m:Nat -> @a:Word(m) -> @b:Word(m) -> {Bits.differ(m, a, b, 0n) == False{} : Bool}
def Bits.dif_clr source · line 2871 · raw
@+m:Nat -> @x:Word(m) -> @y:Word(m) -> @+e:Nat -> {Bits.differ(m, Bits.clear(m, x, e), Bits.clear(m, y, e), e) == Bits.differ(m, x, y, e) : Bool}
def Bits.dif_down source · line 2881 · raw
@+m:Nat -> @x:Word(m) -> @y:Word(m) -> @+e:Nat -> @h:{Bits.differ(m, x, y, 1n+e) == True{} : Bool} -> @hb:{Bits.at(m, x, e) == Bits.at(m, y, e) : Bool} -> {Bits.differ(m, x, y, e) == True{} : Bool}
def Bits.lt_pred source · line 2902 · raw
@+p:Nat -> @+m:Nat -> @h:{Nat.is_lt(1n+p, m) == True{} : Bool} -> {Nat.is_lt(p, m) == True{} : Bool}
def Bits.size_new source · line 2971 · raw
@-T:Data -> @+e:Nat -> @+v:T -> {ASet.sz(T, Array.new(T, e, v)) == U32.shln(1, e) : U32}Array.size of Array.new(T, e, v) is 2^e
def Bits.size_p source · line 2980 · raw
@-T:Data -> @+e:Nat -> @+v:T -> {ASet.sz(T, Array.new(T, e, v)) == U32{Bits.pow(32n, e)} : U32}
def Bits.Apart source · line 2984 · raw
@-T:Data -> @+e:Nat -> @+v:T -> @+x:Word(32n) -> @+y:Word(32n) -> Type
the two descents over Array.new(T, e, v), from its size 2^e, for x and y below 2^e
def Bits.IH source · line 2987 · raw
@-T:Data -> @+p:Nat -> @+v:T -> Type
def Bits.step source · line 2992 · raw
@-T:Data -> @+p:Nat -> @+v:T -> @+x:Word(32n) -> @+y:Word(32n) -> @bx:Bool -> @+ex:{Bits.at(32n, x, p) == bx : Bool} -> @by:Bool -> @+ey:{Bits.at(32n, y, p) == by : Bool} -> @+he:{Nat.is_lt(1n+p, 32n) == True{} : Bool} -> @+hx:{Bits.below(32n, x, 1n+p) == True{} : Bool} -> @+hy:{Bits.below(32n, y, 1n+p) == True{} : Bool} -> @+hd:{Bits.differ(32n, x, y, 1n+p) == True{} : Bool} -> @ih:Bits.IH(T, p, v) -> ASet.Apart(T, Array.new(T, 1n+p, v), U32{Bits.pow(32n, 1n+p)}, U32{x}, Bool.not(bx), U32{y}, Bool.not(by))one node of the tree: bit p of x and of y pick the branches; when they agree the descents go on into one half, with x and y cleared of bit p on the right, and the induction hypothesis ih finishes
def Bits.apart_new source · line 3008 · raw
@-T:Data -> @+e:Nat -> @+v:T -> @+he:{Nat.is_lt(e, 32n) == True{} : Bool} -> @+x:Word(32n) -> @+y:Word(32n) -> @+hx:{Bits.below(32n, x, e) == True{} : Bool} -> @+hy:{Bits.below(32n, y, e) == True{} : Bool} -> @+hd:{Bits.differ(32n, x, y, e) == True{} : Bool} -> Bits.Apart(T, e, v, x, y)LAW: on Array.new's tree of depth e < 32, the descents for two different x and y below 2^e part
def Shape.Keep source · line 3052 · raw
@-T:Data -> @-a:Array<T> -> @-n:U32 -> @-j0:U32 -> @-v:T -> @-z0:Bool -> Type
what a pass over a proves for a write along (n, j0, z0): the six facts of ASet.Facts, and that every walk that parts from another on a parts on the written array too (a write changes a leaf, not the tree's shape)
def Shape.leaf source · line 3056 · raw
@-T:Data -> @-x:T -> @-n:U32 -> @-j0:U32 -> @-v:T -> @-z0:Bool -> @-m:U32 -> @-j:U32 -> @-z:Bool -> @-k:U32 -> @-w:Bool -> @h:ASet.Apart(T, [x], m, j, z, k, w) -> ASet.Apart(T, ASet.NA(T, [x], n, j0, v, z0), m, j, z, k, w)
a leaf: no walk parts on it, before or after
def Shape.imp_t source · line 3061 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j0:U32 -> @+v:T -> @+c2:{(ASet.NA(T, xs, U32.shr(n), j0, v, U32.is_lt(j0, U32.shr(U32.shr(n)))), Pair.snd(Array<T>, T, ASet.G(T, xs, U32.shr(n), j0, v, U32.is_lt(j0, U32.shr(U32.shr(n)))))) == ASet.G(T, xs, U32.shr(n), j0, v, U32.is_lt(j0, U32.shr(U32.shr(n)))) : Pair(Array<T>, T)} -> @kx:(@m:U32 -> @j:U32 -> @z:Bool -> @k:U32 -> @w:Bool -> @_:ASet.Apart(T, xs, m, j, z, k, w) -> ASet.Apart(T, ASet.NA(T, xs, U32.shr(n), j0, v, U32.is_lt(j0, U32.shr(U32.shr(n)))), m, j, z, k, w)) -> @+m:U32 -> @+j:U32 -> @z:Bool -> @+k:U32 -> @w:Bool -> @h:ASet.Apart(T, ANode{xs, ys}, m, j, z, k, w) -> ASet.Apart(T, ASet.NA(T, ANode{xs, ys}, n, j0, v, True{}), m, j, z, k, w)a node the write enters on the left: the written array is ANode{the written left half, ys} (c2 opens the pair the left half's write returns), so a walk into the left half goes on with kx and a walk into ys is unchanged
def Shape.imp_f source · line 3077 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j0:U32 -> @+v:T -> @+c2:{(ASet.NA(T, ys, U32.shr(n), U32.sub(j0, U32.shr(n)), v, U32.is_lt(U32.sub(j0, U32.shr(n)), U32.shr(U32.shr(n)))), Pair.snd(Array<T>, T, ASet.G(T, ys, U32.shr(n), U32.sub(j0, U32.shr(n)), v, U32.is_lt(U32.sub(j0, U32.shr(n)), U32.shr(U32.shr(n)))))) == ASet.G(T, ys, U32.shr(n), U32.sub(j0, U32.shr(n)), v, U32.is_lt(U32.sub(j0, U32.shr(n)), U32.shr(U32.shr(n)))) : Pair(Array<T>, T)} -> @ky:(@m:U32 -> @j:U32 -> @z:Bool -> @k:U32 -> @w:Bool -> @_:ASet.Apart(T, ys, m, j, z, k, w) -> ASet.Apart(T, ASet.NA(T, ys, U32.shr(n), U32.sub(j0, U32.shr(n)), v, U32.is_lt(U32.sub(j0, U32.shr(n)), U32.shr(U32.shr(n)))), m, j, z, k, w)) -> @+m:U32 -> @+j:U32 -> @z:Bool -> @+k:U32 -> @w:Bool -> @h:ASet.Apart(T, ANode{xs, ys}, m, j, z, k, w) -> ASet.Apart(T, ASet.NA(T, ANode{xs, ys}, n, j0, v, False{}), m, j, z, k, w)the same, for a write that enters on the right
def Shape.node_t source · line 3092 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j0:U32 -> @+v:T -> @r:Shape.Keep(T, xs, U32.shr(n), j0, v, U32.is_lt(j0, U32.shr(U32.shr(n)))) -> Shape.Keep(T, ANode{xs, ys}, n, j0, v, True{})
def Shape.node_f source · line 3111 · raw
@-T:Data -> @-xs:Array<T> -> @-ys:Array<T> -> @+n:U32 -> @+j0:U32 -> @+v:T -> @+s:{(xs, ASet.sz(T, xs)) == Array.size(T, xs) : Pair(Array<T>, U32)} -> @r:Shape.Keep(T, ys, U32.shr(n), U32.sub(j0, U32.shr(n)), v, U32.is_lt(U32.sub(j0, U32.shr(n)), U32.shr(U32.shr(n)))) -> Shape.Keep(T, ANode{xs, ys}, n, j0, v, False{})
def Shape.keep source · line 3130 · raw
@-T:Data -> @a:Array<T> -> @+n:U32 -> @+j0:U32 -> @+v:T -> @+z0:Bool -> Shape.Keep(T, a, n, j0, v, z0)
def Shape.close source · line 3139 · raw
@-T:Data -> @-b:Array<T> -> @+n:U32 -> @+i:U32 -> @+v:T -> @+q:{n == ASet.sz(T, b) : U32} -> @r:Shape.Keep(T, b, n, ASet.J(i, n), v, ASet.Z(i, n)) -> @+m:U32 -> @+j:U32 -> @+z:Bool -> @+k:U32 -> @+w:Bool -> @_:ASet.Apart(T, b, m, j, z, k, w) -> ASet.Apart(T, Array.set(T, b, i, v), m, j, z, k, w)
def Shape.top source · line 3150 · raw
@-T:Data -> @-a:Array<T> -> @+i:U32 -> @+v:T -> @r:ASet.Copy(T, a) -> @+m:U32 -> @+j:U32 -> @+z:Bool -> @+k:U32 -> @+w:Bool -> @_:ASet.Apart(T, a, m, j, z, k, w) -> ASet.Apart(T, Array.set(T, a, i, v), m, j, z, k, w)