proofs/bounded_u32_arithmetic.bend checks
raw source on the hub · import stelliferous@0.0.3.0/proofs/bounded_u32_arithmetic.bend as Bounded_u32_arithmetic
Machine operations -> unbounded Nat, with explicit no-overflow obligations.
9 imports
import Base import ./word_arithmetic.bend as WordArithmetic import ./word_multiplication.bend as Multiplication import ./word_subtraction.bend as Subtraction import ./nat_to_u32_bounds.bend as Bounds import ./natural_addition.bend as Addition import ./nat_order.bend as Order import ./u32_comparison.bend as Comparison import ./row_major_index.bend as RowMajor
Definitions
def count_within source · line 12 · raw
@+count:U32 -> @+length:Nat -> @+capacity:U32 -> @+upper:U32 -> @exact:{U32.to_nat(count) == length : Nat} -> @room:{Nat.is_le(length, U32.to_nat(capacity)) == True{} : Bool} -> @capacity_bound:{U32.is_le(capacity, upper) == True{} : Bool} -> {U32.is_le(count, upper) == True{} : Bool}
def native_roundtrip source · line 19 · raw
@+a:U32 -> {U32.from_nat(U32.to_nat(a)) == a : U32}
def native_add source · line 27 · raw
@+a:U32 -> @+b:U32 -> {U32.add(a, b) == U32.from_nat(Nat.add(U32.to_nat(a), U32.to_nat(b))) : U32}
def add_zero source · line 32 · raw
@+value:U32 -> {U32.add(value, 0) == value : U32}
def add_commutative source · line 37 · raw
@+left:U32 -> @+right:U32 -> {U32.add(left, right) == U32.add(right, left) : U32}
def add_zero_left source · line 46 · raw
@+value:U32 -> {U32.add(0, value) == value : U32}
def multiply_zero_left source · line 49 · raw
@+value:U32 -> {U32.mul(0, value) == 0 : U32}
def encoded_add_associative source · line 53 · raw
@+a:Nat -> @+b:Nat -> @+c:Nat -> {U32.add(U32.add(U32.from_nat(a), U32.from_nat(b)), U32.from_nat(c)) == U32.add(U32.from_nat(a), U32.add(U32.from_nat(b), U32.from_nat(c))) : U32}These are modular identities, so they require no no-overflow assumption.
def add_associative source · line 64 · raw
@+a:U32 -> @+b:U32 -> @+c:U32 -> {U32.add(U32.add(a, b), c) == U32.add(a, U32.add(b, c)) : U32}
def subtract_from_sum source · line 70 · raw
@+a:U32 -> @+b:U32 -> @+c:U32 -> {U32.sub(U32.add(a, b), c) == U32.add(a, U32.sub(b, c)) : U32}
def subtract_first source · line 75 · raw
@+a:U32 -> @+b:U32 -> {U32.sub(U32.add(a, b), a) == b : U32}
def add_after_subtract source · line 79 · raw
@+value:U32 -> @+padding:U32 -> @+offset:U32 -> {U32.add(U32.sub(value, padding), offset) == U32.sub(U32.add(value, offset), padding) : U32}
def subtract_sum source · line 86 · raw
@+value:U32 -> @+left:U32 -> @+right:U32 -> {U32.sub(U32.sub(value, left), right) == U32.sub(value, U32.add(left, right)) : U32}
def subtract_difference source · line 101 · raw
@+value:U32 -> @+tap:U32 -> @+offset:U32 -> {U32.add(U32.sub(value, tap), offset) == U32.sub(value, U32.sub(tap, offset)) : U32}
def native_subtract source · line 114 · raw
@+a:U32 -> @+b:U32 -> @h:{Nat.is_le(U32.to_nat(b), U32.to_nat(a)) == True{} : Bool} -> {U32.sub(a, b) == U32.from_nat(Nat.sub(U32.to_nat(a), U32.to_nat(b))) : U32}
def add source · line 120 · raw
@+a:U32 -> @+b:U32 -> @+cap:U32 -> @cap_ok:{U32.is_le(cap, 2147483648) == True{} : Bool} -> @h:{Nat.is_le(Nat.add(U32.to_nat(a), U32.to_nat(b)), U32.to_nat(cap)) == True{} : Bool} -> {U32.to_nat(U32.add(a, b)) == Nat.add(U32.to_nat(a), U32.to_nat(b)) : Nat}
def subtract source · line 126 · raw
@+a:U32 -> @+b:U32 -> @+cap:U32 -> @cap_ok:{U32.is_le(cap, 2147483648) == True{} : Bool} -> @ordered:{Nat.is_le(U32.to_nat(b), U32.to_nat(a)) == True{} : Bool} -> @h:{Nat.is_le(Nat.sub(U32.to_nat(a), U32.to_nat(b)), U32.to_nat(cap)) == True{} : Bool} -> {U32.to_nat(U32.sub(a, b)) == Nat.sub(U32.to_nat(a), U32.to_nat(b)) : Nat}
def remove_prefix source · line 133 · raw
@+value:U32 -> @+base:U32 -> @+local:Nat -> @+capacity:U32 -> @capacity_ok:{U32.is_le(capacity, 2147483648) == True{} : Bool} -> @+exact:{U32.to_nat(value) == Nat.add(U32.to_nat(base), local) : Nat} -> @room:{Nat.is_le(U32.to_nat(value), U32.to_nat(capacity)) == True{} : Bool} -> {U32.to_nat(U32.sub(value, base)) == local : Nat}
def subtract_zero source · line 146 · raw
@+value:U32 -> {U32.sub(value, 0) == value : U32}
def multiply source · line 151 · raw
@+a:U32 -> @+b:U32 -> @+cap:U32 -> @cap_ok:{U32.is_le(cap, 2147483648) == True{} : Bool} -> @h:{Nat.is_le(Nat.mul(U32.to_nat(a), U32.to_nat(b)), U32.to_nat(cap)) == True{} : Bool} -> {U32.to_nat(U32.mul(a, b)) == Nat.mul(U32.to_nat(a), U32.to_nat(b)) : Nat}
def from_nat_exact source · line 155 · raw
@+count:U32 -> @+length:Nat -> @exact:{U32.to_nat(count) == length : Nat} -> {U32.from_nat(length) == count : U32}
def multiply_add_exact source · line 161 · raw
@+a:U32 -> @+radix:U32 -> @+rest:U32 -> @+capacity:U32 -> @+capacity_ok:{U32.is_le(capacity, 2147483648) == True{} : Bool} -> @+room:{Nat.is_le(Nat.add(Nat.mul(U32.to_nat(a), U32.to_nat(radix)), U32.to_nat(rest)), U32.to_nat(capacity)) == True{} : Bool} -> {U32.to_nat(U32.add(U32.mul(a, radix), rest)) == Nat.add(Nat.mul(U32.to_nat(a), U32.to_nat(radix)), U32.to_nat(rest)) : Nat}Each mixed-radix step is exact when its complete result fits. The product bound is derived here, rather than assumed for an intermediate machine word.
def equal_of_is_eq source · line 172 · raw
@+left:U32 -> @+right:U32 -> @equal:{U32.is_eq(left, right) == True{} : Bool} -> {left == right : U32}
def to_nat_injective source · line 179 · raw
@+left:U32 -> @+right:U32 -> @equal:{U32.to_nat(left) == U32.to_nat(right) : Nat} -> {left == right : U32}
def word_zero source · line 184 · raw
@+value:U32 -> @zero:{U32.is_zero(value) == True{} : Bool} -> {value == 0 : U32}
def word_nonzero_positive source · line 189 · raw
@+extent:U32 -> @nonzero:{U32.is_zero(extent) == False{} : Bool} -> {Nat.is_lt(0n, U32.to_nat(extent)) == True{} : Bool}
def multiply_one source · line 196 · raw
@+word:U32 -> {U32.mul(word, 1) == word : U32}Zero channels, rows or width make the actual modular local count zero. These producers need no positive-width or whole-tensor indexing envelope.
def multiply_zero_right source · line 200 · raw
@+value:U32 -> {U32.mul(value, 0) == 0 : U32}
def multiply_commutative source · line 205 · raw
@+left:U32 -> @+right:U32 -> {U32.mul(left, right) == U32.mul(right, left) : U32}
def equal_reflexive source · line 212 · raw
@+word:U32 -> {U32.is_eq(word, word) == True{} : Bool}
def add_zero_subtract source · line 216 · raw
@+value:U32 -> @+padding:U32 -> {U32.add(value, U32.sub(0, padding)) == U32.sub(value, padding) : U32}
def negate_twice source · line 221 · raw
@+value:U32 -> {U32.sub(0, U32.sub(0, value)) == value : U32}
def multiply_successor source · line 228 · raw
@+index:U32 -> @+stride:U32 -> {U32.mul(U32.add(index, 1), stride) == U32.add(U32.mul(index, stride), stride) : U32}A modular channel counter advances both its tile and source-plane strides.
def multiply_additive source · line 248 · raw
@+left:U32 -> @+right:U32 -> @+factor:U32 -> {U32.mul(U32.add(left, right), factor) == U32.add(U32.mul(left, factor), U32.mul(right, factor)) : U32}Modular products preserve natural distributivity and associativity.
def multiply_associative source · line 265 · raw
@+left:U32 -> @+middle:U32 -> @+right:U32 -> {U32.mul(U32.mul(left, middle), right) == U32.mul(left, U32.mul(middle, right)) : U32}