LAWS.bend open laws/TODOs
raw source on the hub · import bend-ndarray@0.1.1.0/LAWS.bend as LAWS
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
3 imports
import Base import ./shape.bend as S import ./ndarray.bend as NP
Laws
law broadcast_sound provedin PROOF.bendsource · line 39 · raw
@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+s:List<&2, Nat> -> @h:{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.broadcast(a, b) == Done{s} : Result<&2, &2, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NdErr, List<&2, Nat>>} -> {Bool.and(0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.fits(a, s), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.fits(b, s)) == True{} : Bool}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_complete provedin PROOF.bendsource · line 49 · raw
@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+s:List<&2, Nat> -> @+ha:{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.fits(a, s) == True{} : Bool} -> @+hb:{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.fits(b, s) == True{} : Bool} -> {is_done(0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.broadcast(a, b)) == 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_least provedin PROOF.bendsource · line 60 · raw
@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+s:List<&2, Nat> -> @+t:List<&2, Nat> -> @h:{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.broadcast(a, b) == Done{t} : Result<&2, &2, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NdErr, List<&2, Nat>>} -> @+ha:{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.fits(a, s) == True{} : Bool} -> @+hb:{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.fits(b, s) == True{} : Bool} -> {0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.fits(t, s) == 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_examples provedin PROOF.bendsource · line 73 · raw
{[0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.broadcast([2n, 3n], [3n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.broadcast([5n, 1n, 4n], [3n, 1n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.broadcast([0n, 4n], [1n, 4n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.broadcast([2n, 3n], [2n])] == [Done{[2n, 3n]}, Done{[5n, 3n, 4n]}, Done{[0n, 4n]}, Fail{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NoBroadcast{[2n, 3n], [2n]}}] : List<&2, Result<&2, &2, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NdErr, List<&2, Nat>>>}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 reshape_rules provedin PROOF.bendsource · line 82 · raw
{[0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.reshape([2n, 3n, 4n], [6n, 4n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.reshape([2n, 3n, 4n], [24n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.reshape([2n, 3n, 4n], [5n, 5n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.reshape([2n, 3n, 4n], [2n, 3n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.reshape([0n, 3n], [3n, 0n])] == [Done{[6n, 4n]}, Done{[24n]}, Fail{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.BadReshape{[2n, 3n, 4n], [5n, 5n]}}, Fail{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.BadReshape{[2n, 3n, 4n], [2n, 3n]}}, Done{[3n, 0n]}] : List<&2, Result<&2, &2, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NdErr, List<&2, Nat>>>}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 transpose_rules provedin PROOF.bendsource · line 91 · raw
{[0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.transpose([2n, 3n, 4n], [2n, 0n, 1n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.transpose([4n, 2n, 3n], [1n, 2n, 0n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.transpose([2n, 3n], [0n, 0n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.transpose([2n, 3n], [0n, 2n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.transpose([2n, 3n], [0n])] == [Done{[4n, 2n, 3n]}, Done{[2n, 3n, 4n]}, Fail{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.BadPerm{[0n, 0n], 2n}}, Fail{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.BadPerm{[0n, 2n], 2n}}, Fail{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.BadPerm{[0n], 2n}}] : List<&2, Result<&2, &2, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.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 reduce_rules provedin PROOF.bendsource · line 99 · raw
{[shape_of(Done{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.reduce_keep(False{}, [2n, 3n, 4n], 1n)}), shape_of(Done{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.reduce_keep(True{}, [2n, 3n, 4n], 1n)})] == [Some{[2n, 4n]}, Some{[2n, 1n, 4n]}] : List<&2, Maybe<&2, 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 axis_rules provedin PROOF.bendsource · line 103 · raw
{[0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.axis_of(2n, 3n), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.axis_of(3n, 3n)] == [Done{2n}, Fail{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.BadAxis{3n, 3n}}] : List<&2, Result<&2, &2, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NdErr, Nat>>}
law slice_rules provedin PROOF.bendsource · line 109 · raw
{[0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.slice_len(10n, 2n, 8n, 3n), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.slice_len(10n, 0n, 10n, 1n), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.slice_len(10n, 8n, 2n, 1n), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.slice_len(10n, 3n, 100n, 4n), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.slice_len(10n, 12n, 15n, 1n)] == [2n, 10n, 0n, 2n, 0n] : List<&2, 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 concat_rules provedin PROOF.bendsource · line 116 · raw
{[0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.concat(1n, [2n, 3n], [2n, 5n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.concat(0n, [2n, 3n], [4n, 3n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.concat(1n, [2n, 3n], [3n, 3n]), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.concat(2n, [2n, 3n], [2n, 3n])] == [Done{[2n, 8n]}, Done{[6n, 3n]}, Fail{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.BadConcat{1n, [2n, 3n], [3n, 3n]}}, Fail{0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.BadConcat{2n, [2n, 3n], [2n, 3n]}}] : List<&2, Result<&2, &2, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NdErr, 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 math_dtypes provedin PROOF.bendsource · line 127 · raw
{[is_fail(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.exp(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.i32, 3n))), is_fail(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.sqrt(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.u8, 3n))), is_fail(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.exp(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.f32, 3n))), is_fail(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.square(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.i32, 3n))), is_fail(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arctan2(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.f32, 3n), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.i32, 3n))), is_fail(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arctan2(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.i32, 3n), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.i32, 3n))), is_fail(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.hypot(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.i32, 3n), 0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.arange(0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.i32, 3n)))] == [True{}, True{}, False{}, False{}, True{}, True{}, True{}] : List<&2, Bool>}Guarantees: exp of int32 and sqrt of uint8 are refused, arctan2 of mixed dtypes and of int32, and hypot of int32, are refused; exp of float32 and square of int32 are not. Why it matters: decision 2 (one dtype, explicit astype): a float function applied to integer cells would read the integers' bits as floats and give garbage with no error.
Definitions
def shape_of source · line 12 · raw
@r:Result<&2, &2, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NdErr, List<&2, Nat>> -> Maybe<&2, List<&2, Nat>>
def is_fail source · line 19 · raw
@r:Result<&1, &1, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NdErr, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/ndarray.ND> -> Bool
def is_done source · line 26 · raw
@r:Result<&2, &2, 0x2b8aa54cdabf25f47f4d6f43f90edd8f/shape.NdErr, List<&2, Nat>> -> Bool