# The laws of bend-ndarray. Broadcasting is characterised for every pair of shapes (sound, complete, least: together # they pin the result to the least shape both fit, so it is also symmetric); # the other shape rules and the arrays' values are pinned at literal inputs (integer dtypes: the checker does not run # float arithmetic; float32 results are tested against NumPy by test/compare.py). # Gate: ./bend-check PROOF.bend --verdict import Base import ./shape.bend as S # ---- reference definitions ---- def shape_of(r: Result<&2, &2, S.NdErr, List<&2, Nat>>) -> Maybe<&2, List<&2, Nat>>: match r: case Done{s}: Some{s} case Fail{e}: None{} def is_done(r: Result<&2, &2, S.NdErr, List<&2, Nat>>) -> Bool: match r: case Done{s}: True{} case Fail{e}: False{} # ---- broadcasting ---- # Guarantees: when a and b broadcast to s, each of them fits s: aligned at the last dimension, every one of its # dimensions is 1 or equal to s's, and it has no more dimensions than s. # Why it matters: the data of each operand is then stretched only where it has a 1, so no element is invented or # misplaced; a rule that stretched a 3 to a 6 would silently repeat data. law broadcast_sound: for +a: List<&2, Nat> for +b: List<&2, Nat> for +s: List<&2, Nat> for h: {S.broadcast(a, b) == Done{s} : Result<&2, &2, S.NdErr, List<&2, Nat>>} {Bool.and(S.fits(a, s), S.fits(b, s)) == True{} : Bool} # Guarantees: whenever a and b both fit some shape s, broadcasting them succeeds. # Why it matters: NumPy accepts every such pair (a (5, 1) and a (3,) both fit (5, 3)); refusing one would break code # ported from NumPy with a broadcasting error it never gets there. law broadcast_complete: for +a: List<&2, Nat> for +b: List<&2, Nat> for +s: List<&2, Nat> for +ha: {S.fits(a, s) == True{} : Bool} for +hb: {S.fits(b, s) == True{} : Bool} {is_done(S.broadcast(a, b)) == True{} : Bool} # Guarantees: the shape a and b broadcast to fits every shape they both fit: it is the smallest common shape. # Why it matters: with sound and complete, this pins the result exactly to NumPy's: a rule that answered a larger # shape both fit (stretching a 1 that NumPy keeps) would pass the other two laws and give wrongly sized arrays. law broadcast_least: for +a: List<&2, Nat> for +b: List<&2, Nat> for +s: List<&2, Nat> for +t: List<&2, Nat> for h: {S.broadcast(a, b) == Done{t} : Result<&2, &2, S.NdErr, List<&2, Nat>>} for +ha: {S.fits(a, s) == True{} : Bool} for +hb: {S.fits(b, s) == True{} : Bool} {S.fits(t, s) == True{} : Bool} # Guarantees: (2, 3) with (3,) gives (2, 3); (5, 1, 4) with (3, 1) gives (5, 3, 4); a 0 dimension against a 1 gives 0; # (2, 3) with (2,) is refused, naming both shapes. # Why it matters: these are NumPy's own documented examples and edge cases, at the public entry point. law broadcast_examples: {[S.broadcast([2n, 3n], [3n]), S.broadcast([5n, 1n, 4n], [3n, 1n]), S.broadcast([0n, 4n], [1n, 4n]), S.broadcast([2n, 3n], [2n])] == [Done{[2n, 3n]}, Done{[5n, 3n, 4n]}, Done{[0n, 4n]}, Fail{S.NoBroadcast{[2n, 3n], [2n]}}] : List<&2, Result<&2, &2, S.NdErr, List<&2, Nat>>>} # ---- the other shape rules ---- # Guarantees: reshape keeps the number of elements: (2, 3, 4) to (6, 4) and to (24,) succeed; to (5, 5) (more) and # to (2, 3) (fewer) are refused naming both shapes; a 0-element shape reshapes to another 0-element shape. # Why it matters: a reshape that changed the element count would drop or invent data. law reshape_rules: {[S.reshape([2n, 3n, 4n], [6n, 4n]), S.reshape([2n, 3n, 4n], [24n]), S.reshape([2n, 3n, 4n], [5n, 5n]), S.reshape([2n, 3n, 4n], [2n, 3n]), S.reshape([0n, 3n], [3n, 0n])] == [Done{[6n, 4n]}, Done{[24n]}, Fail{S.BadReshape{[2n, 3n, 4n], [5n, 5n]}}, Fail{S.BadReshape{[2n, 3n, 4n], [2n, 3n]}}, Done{[3n, 0n]}] : List<&2, Result<&2, &2, S.NdErr, List<&2, Nat>>>} # Guarantees: transposing (2, 3, 4) by (2, 0, 1) gives (4, 2, 3), and by its inverse (1, 2, 0) back to (2, 3, 4); a # perm that repeats an axis, misses one or has the wrong length is refused. # Why it matters: result axis k is input axis perm[k] (NumPy's meaning); the reverse reading silently scrambles axes. law transpose_rules: {[S.transpose([2n, 3n, 4n], [2n, 0n, 1n]), S.transpose([4n, 2n, 3n], [1n, 2n, 0n]), S.transpose([2n, 3n], [0n, 0n]), S.transpose([2n, 3n], [0n, 2n]), S.transpose([2n, 3n], [0n])] == [Done{[4n, 2n, 3n]}, Done{[2n, 3n, 4n]}, Fail{S.BadPerm{[0n, 0n], 2n}}, Fail{S.BadPerm{[0n, 2n], 2n}}, Fail{S.BadPerm{[0n], 2n}}] : List<&2, Result<&2, &2, S.NdErr, List<&2, Nat>>>} # Guarantees: reducing (2, 3, 4) along axis 1 gives (2, 4), or (2, 1, 4) keeping the axis; axis 3 is refused. # Why it matters: the reduced axis is the one named, counted from 0 (NumPy's axis=1). law reduce_rules: {[shape_of(Done{S.reduce_keep(False{}, [2n, 3n, 4n], 1n)}), shape_of(Done{S.reduce_keep(True{}, [2n, 3n, 4n], 1n)})] == [Some{[2n, 4n]}, Some{[2n, 1n, 4n]}] : List<&2, Maybe<&2, List<&2, Nat>>>} law axis_rules: {[S.axis_of(2n, 3n), S.axis_of(3n, 3n)] == [Done{2n}, Fail{S.BadAxis{3n, 3n}}] : List<&2, Result<&2, &2, S.NdErr, Nat>>} # Guarantees: slice lengths follow Python's a[start:stop:step] for positive steps: 10 elements [2:8:3] is 2 (2, 5), # [0:10:1] is 10, [8:2:1] is 0, [3:100:4] is 2 (3, 7), [12:15:1] is 0. # Why it matters: an off-by-one here drops or adds a row with no error. law slice_rules: {[S.slice_len(10n, 2n, 8n, 3n), S.slice_len(10n, 0n, 10n, 1n), S.slice_len(10n, 8n, 2n, 1n), S.slice_len(10n, 3n, 100n, 4n), S.slice_len(10n, 12n, 15n, 1n)] == [2n, 10n, 0n, 2n, 0n] : List<&2, Nat>} # Guarantees: (2, 3) and (2, 5) join along axis 1 into (2, 8); (2, 3) and (4, 3) along axis 0 into (6, 3); (2, 3) and # (3, 3) along axis 1 are refused, and so is an axis past the last. # Why it matters: every axis but the joined one must agree, or rows would be glued to rows of another length. law concat_rules: {[S.concat(1n, [2n, 3n], [2n, 5n]), S.concat(0n, [2n, 3n], [4n, 3n]), S.concat(1n, [2n, 3n], [3n, 3n]), S.concat(2n, [2n, 3n], [2n, 3n])] == [Done{[2n, 8n]}, Done{[6n, 3n]}, Fail{S.BadConcat{1n, [2n, 3n], [3n, 3n]}}, Fail{S.BadConcat{2n, [2n, 3n], [2n, 3n]}}] : List<&2, Result<&2, &2, S.NdErr, List<&2, Nat>>>}