~/bend-docscommunity

proof_u32.bend checks

raw source on the hub · import bend-osdev-algo@0.1.0.0/proof_u32.bend as Proof_u32

Proof helpers for bend-osdev-algo: U32 arithmetic as Nat arithmetic. The value (Word.to_nat) of a U32 subtraction, addition, increment and halving is the Nat difference, sum, successor and half, when nothing wraps, by induction over the bits of Base's Word(n). The Array binary search's index arithmetic (lo + (hi - lo) / 2, mid + 1) rests on these; PROOF.bend uses them through proof_bsearch.bend.

1 import
import Base

Definitions

def uq.pw source · line 10 · raw

@n:Nat -> Nat

2^n

def uq.half source · line 18 · raw

@n:Nat -> Nat

n / 2, rounded down

def uq.bv source · line 30 · raw

@b:Bool -> @x:Nat -> Nat

the value of a word's lowest bit and the rest

def uq.bt source · line 39 · raw

@b:Bool -> Type

def uq.ft source · line 46 · raw

@e:{False{} == True{} : Bool} -> Empty

def uq.tf source · line 50 · raw

@e:{True{} == False{} : Bool} -> Empty

def uq.succ_cong source · line 56 · raw

@-a:Nat -> @-b:Nat -> @e:{a == b : Nat} -> {1n+a == 1n+b : Nat}

def uq.double_cong source · line 60 · raw

@-a:Nat -> @-b:Nat -> @e:{a == b : Nat} -> {Nat.double(a) == Nat.double(b) : Nat}

def uq.add_zero source · line 64 · raw

@x:Nat -> {Nat.add(x, 0n) == x : Nat}

def uq.add_succ source · line 71 · raw

@x:Nat -> @-y:Nat -> {Nat.add(x, 1n+y) == 1n+Nat.add(x, y) : Nat}

def uq.add_comm source · line 78 · raw

@x:Nat -> @+y:Nat -> {Nat.add(x, y) == Nat.add(y, x) : Nat}

def uq.double_add source · line 85 · raw

@x:Nat -> @-y:Nat -> {Nat.double(Nat.add(x, y)) == Nat.add(Nat.double(x), Nat.double(y)) : Nat}

def uq.sub_add_l source · line 92 · raw

@y:Nat -> @+r:Nat -> {Nat.sub(Nat.add(y, r), y) == r : Nat}

def uq.sub_add source · line 103 · raw

@+r:Nat -> @+y:Nat -> {Nat.sub(Nat.add(r, y), y) == r : Nat}

def uq.le_succ source · line 107 · raw

@y:Nat -> @x:Nat -> {Nat.is_le(1n+y, x) == Nat.is_lt(y, x) : Bool}

def uq.lt_dbl source · line 122 · raw

@x:Nat -> @y:Nat -> {Nat.is_lt(Nat.double(x), Nat.double(y)) == Nat.is_lt(x, y) : Bool}

def uq.lt_dbl1 source · line 133 · raw

@x:Nat -> @y:Nat -> {Nat.is_lt(1n+Nat.double(x), Nat.double(y)) == Nat.is_lt(x, y) : Bool}

def uq.lt_rw source · line 144 · raw

@-x:Nat -> @-x2:Nat -> @-y:Nat -> @e:{x == x2 : Nat} -> @h:{Nat.is_lt(x, y) == True{} : Bool} -> {Nat.is_lt(x2, y) == True{} : Bool}

def uq.val_lt source · line 150 · raw

@n:Nat -> @w:Word(n) -> {Nat.is_lt(Word.to_nat(n, w), uq.pw(n)) == True{} : Bool}

def uq.lt_succ_r source · line 161 · raw

@x:Nat -> @y:Nat -> @h:{Nat.is_lt(x, y) == True{} : Bool} -> {Nat.is_lt(x, 1n+y) == True{} : Bool}

def uq.le_lt_trans source · line 170 · raw

@x:Nat -> @y:Nat -> @z:Nat -> @h1:{Nat.is_le(x, y) == True{} : Bool} -> @h2:{Nat.is_lt(y, z) == True{} : Bool} -> {Nat.is_lt(x, z) == True{} : Bool}

def uq.lt_dbl_self source · line 187 · raw

@x:Nat -> @y:Nat -> @h:{Nat.is_lt(x, y) == True{} : Bool} -> {Nat.is_lt(x, Nat.double(y)) == True{} : Bool}

def uq.lt_succ_pw source · line 200 · raw

@+f:Nat -> @-g:Nat -> @eg:{1n+f == g : Nat} -> @+x:Nat -> @-x2:Nat -> @e:{x == x2 : Nat} -> @h:{Nat.is_lt(x, uq.pw(f)) == True{} : Bool} -> {Nat.is_lt(x2, uq.pw(g)) == True{} : Bool}

x < 2^f: x2 < 2^g, for x == x2 and g == 1 + f (stated so that a fixed g, such as the search's 33, is never expanded)

def uq.nc source · line 207 · raw

@x:Nat -> @y:Nat -> @a:Bool -> @b:Bool -> {Nat.cmp(uq.bv(a, x), uq.bv(b, y)) == Word.cmp.fin(a, b, Nat.cmp(x, y)) : Cmp}

def uq.cb source · line 252 · raw

@c:Bool -> Nat

def uq.fin_le_ff source · line 259 · raw

@c:Cmp -> {Cmp.is_le(Word.cmp.fin(False{}, False{}, c)) == Cmp.is_le(c) : Bool}

def uq.fin_le_tf source · line 268 · raw

@c:Cmp -> {Cmp.is_le(Word.cmp.fin(True{}, False{}, c)) == Cmp.is_lt(c) : Bool}

def uq.fin_le_ft source · line 277 · raw

@c:Cmp -> {Cmp.is_le(Word.cmp.fin(False{}, True{}, c)) == Cmp.is_le(c) : Bool}

def uq.le_bits source · line 286 · raw

@+y:Nat -> @+x:Nat -> @+a:Bool -> @+b:Bool -> @h:{Nat.is_le(uq.bv(a, y), uq.bv(b, x)) == True{} : Bool} -> {Cmp.is_le(Word.cmp.fin(a, b, Nat.cmp(y, x))) == True{} : Bool}

def uq.ha source · line 290 · raw

@+y:Nat -> @+x:Nat -> @h:{Nat.is_le(uq.bv(False{}, y), uq.bv(False{}, x)) == True{} : Bool} -> {Nat.is_le(y, x) == True{} : Bool}

def uq.hb source · line 294 · raw

@+y:Nat -> @+x:Nat -> @h:{Nat.is_le(uq.bv(True{}, y), uq.bv(False{}, x)) == True{} : Bool} -> {Nat.is_le(1n+y, x) == True{} : Bool}

def uq.hc source · line 299 · raw

@+y:Nat -> @+x:Nat -> @h:{Nat.is_le(uq.bv(False{}, y), uq.bv(True{}, x)) == True{} : Bool} -> {Nat.is_le(y, x) == True{} : Bool}

def uq.alg2 source · line 303 · raw

@+r:Nat -> @+y:Nat -> {Nat.double(Nat.add(r, 1n+y)) == Nat.add(1n+Nat.double(r), 1n+Nat.double(y)) : Nat}

def uq.alg4 source · line 308 · raw

@+r:Nat -> @+y:Nat -> {Nat.double(Nat.add(r, 1n+y)) == Nat.add(Nat.double(r), 2n+Nat.double(y)) : Nat}

def uq.alg6 source · line 314 · raw

@+r:Nat -> @+y:Nat -> {1n+Nat.double(Nat.add(r, y)) == Nat.add(Nat.double(r), 1n+Nat.double(y)) : Nat}

def uq.sub_w source · line 320 · raw

@n:Nat -> @a:Word(n) -> @b:Word(n) -> @c:Bool -> @h:{Nat.is_le(Nat.add(uq.cb(c), Word.to_nat(n, b)), Word.to_nat(n, a)) == True{} : Bool} -> {Word.to_nat(n, a) == Nat.add(Word.to_nat(n, Word.adc(n, a, b, True{}, c)), Nat.add(uq.cb(c), Word.to_nat(n, b))) : Nat}

for b <= a (b < a when the carry in c is False): a = adc(a, b, True, c) + (1 - c) + b

def uq.u32_sub source · line 379 · raw

@a:U32 -> @b:U32 -> @h:{Nat.is_le(U32.to_nat(b), U32.to_nat(a)) == True{} : Bool} -> {U32.to_nat(U32.sub(a, b)) == Nat.sub(U32.to_nat(a), U32.to_nat(b)) : Nat}

def uq.cv source · line 390 · raw

@c:Bool -> Nat

def uq.hk0 source · line 398 · raw

@+s2:Nat -> @-d:Nat -> @+q:Nat -> @e:{Nat.double(s2) == d : Nat} -> @h:{Nat.is_lt(d, Nat.double(q)) == True{} : Bool} -> {Nat.is_lt(s2, q) == True{} : Bool}

the bound for the bits above the lowest, from the bound for the word

def uq.hk1 source · line 401 · raw

@+s2:Nat -> @-d:Nat -> @+q:Nat -> @e:{Nat.double(s2) == d : Nat} -> @h:{Nat.is_lt(1n+d, Nat.double(q)) == True{} : Bool} -> {Nat.is_lt(s2, q) == True{} : Bool}

def uq.hk2 source · line 404 · raw

@+s2:Nat -> @-d:Nat -> @+q:Nat -> @e:{Nat.double(s2) == d : Nat} -> @h:{Nat.is_lt(2n+d, Nat.double(q)) == True{} : Bool} -> {Nat.is_lt(1n+s2, q) == True{} : Bool}

def uq.hk3 source · line 407 · raw

@+s2:Nat -> @-d:Nat -> @+q:Nat -> @e:{Nat.double(s2) == d : Nat} -> @h:{Nat.is_lt(3n+d, Nat.double(q)) == True{} : Bool} -> {Nat.is_lt(1n+s2, q) == True{} : Bool}

def uq.add_w source · line 411 · raw

@n:Nat -> @a:Word(n) -> @b:Word(n) -> @c:Bool -> @h:{Nat.is_lt(Nat.add(uq.cv(c), Nat.add(Word.to_nat(n, a), Word.to_nat(n, b))), uq.pw(n)) == True{} : Bool} -> {Word.to_nat(n, Word.adc(n, a, b, False{}, c)) == Nat.add(uq.cv(c), Nat.add(Word.to_nat(n, a), Word.to_nat(n, b))) : Nat}

c + a + b < 2^n: adc(a, b, False, c) is c + a + b

def uq.add_wz source · line 487 · raw

@+n:Nat -> @+a:Word(n) -> @+b:Word(n) -> @+c:Bool -> @+z:Word(n) -> @h:{Nat.is_le(Nat.add(uq.cv(c), Nat.add(Word.to_nat(n, a), Word.to_nat(n, b))), Word.to_nat(n, z)) == True{} : Bool} -> {Word.to_nat(n, Word.adc(n, a, b, False{}, c)) == Nat.add(uq.cv(c), Nat.add(Word.to_nat(n, a), Word.to_nat(n, b))) : Nat}

the bound given by a word's value, so that 2^n is never expanded

def uq.u32_add source · line 491 · raw

@a:U32 -> @b:U32 -> @c:U32 -> @h:{Nat.is_le(Nat.add(U32.to_nat(a), U32.to_nat(b)), U32.to_nat(c)) == True{} : Bool} -> {U32.to_nat(U32.add(a, b)) == Nat.add(U32.to_nat(a), U32.to_nat(b)) : Nat}

a + b <= c: U32.add(a, b) is a + b

def uq.inc_w source · line 498 · raw

@n:Nat -> @w:Word(n) -> @h:{Nat.is_lt(1n+Word.to_nat(n, w), uq.pw(n)) == True{} : Bool} -> {Word.to_nat(n, Word.inc(n, w)) == 1n+Word.to_nat(n, w) : Nat}

def uq.inc_wz source · line 510 · raw

@+n:Nat -> @+w:Word(n) -> @+z:Word(n) -> @h:{Nat.is_le(1n+Word.to_nat(n, w), Word.to_nat(n, z)) == True{} : Bool} -> {Word.to_nat(n, Word.inc(n, w)) == 1n+Word.to_nat(n, w) : Nat}

def uq.u32_inc source · line 514 · raw

@a:U32 -> @c:U32 -> @h:{Nat.is_le(1n+U32.to_nat(a), U32.to_nat(c)) == True{} : Bool} -> {U32.to_nat(U32.inc(a)) == 1n+U32.to_nat(a) : Nat}

1 + a <= c: U32.inc(a) is 1 + a

def uq.half_dbl source · line 521 · raw

@x:Nat -> {uq.half(Nat.double(x)) == x : Nat}

def uq.half_dbl1 source · line 528 · raw

@x:Nat -> {uq.half(1n+Nat.double(x)) == x : Nat}

def uq.pad source · line 536 · raw

@p:Nat -> @t:Word(p) -> {Word.to_nat(1n+p, Word.shr.pad(p, t)) == Word.to_nat(p, t) : Nat}

padding a word with a zero top bit keeps its value

def uq.shr_w source · line 547 · raw

@n:Nat -> @w:Word(n) -> {Word.to_nat(n, Word.shr(n, w)) == uq.half(Word.to_nat(n, w)) : Nat}

def uq.u32_shr source · line 558 · raw

@a:U32 -> {U32.to_nat(U32.shr(a)) == uq.half(U32.to_nat(a)) : Nat}