~/bend-docscommunity

health.bend checks

raw source on the hub · import gralha-bendquest@0.1.0.0/health.bend as Health

Health rules used by the world simulation. Arithmetic is natural-number arithmetic; the machine-word boundary is kept separate below.

3 imports
import Base
import ./game.bend as G
import ./numeric.bend as N

Laws

law Health.le_refl provedsource · line 16 · raw

@+n:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(n, n) == True{} : Bool}

law Health.le_trans provedsource · line 27 · raw

@+a:Nat -> @+b:Nat -> @+c:Nat -> @+ab:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(a, b) == True{} : Bool} -> @+bc:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(b, c) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(a, c) == True{} : Bool}

law Health.spec_clamp_bounded provedsource · line 50 · raw

@+hp:Nat -> @+maximum:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.spec_clamp(hp, maximum), maximum) == True{} : Bool}

law Health.spec_clamp_unchanged provedsource · line 58 · raw

@+hp:Nat -> @+maximum:Nat -> @+valid:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, maximum) == True{} : Bool} -> {Health.spec_clamp(hp, maximum) == hp : Nat}

law Health.spec_hurt_decreases provedsource · line 76 · raw

@+hp:Nat -> @+damage:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.spec_hurt(hp, damage), hp) == True{} : Bool}

law Health.spec_hurt_preserves_bound provedsource · line 92 · raw

@+hp:Nat -> @+damage:Nat -> @+maximum:Nat -> @+valid:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, maximum) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.spec_hurt(hp, damage), maximum) == True{} : Bool}

law Health.spec_heal_bounded provedsource · line 102 · raw

@+hp:Nat -> @+amount:Nat -> @+maximum:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.spec_heal(hp, amount, maximum), maximum) == True{} : Bool}

law Health.le_add provedsource · line 111 · raw

@+hp:Nat -> @+amount:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, 0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.add(hp, amount)) == True{} : Bool}

law Health.le_min provedsource · line 123 · raw

@+hp:Nat -> @+a:Nat -> @+b:Nat -> @+ha:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, a) == True{} : Bool} -> @+hb:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, b) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, 0x6f51b0319916bc200dd5f3949f31668e/game.Bq.nmin(a, b)) == True{} : Bool}

law Health.spec_heal_never_harms provedsource · line 146 · raw

@+hp:Nat -> @+amount:Nat -> @+maximum:Nat -> @+valid:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, maximum) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, Health.spec_heal(hp, amount, maximum)) == True{} : Bool}

law Health.spec_hurt_survives provedsource · line 156 · raw

@+hp:Nat -> @+damage:Nat -> @+below:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.lt(damage, hp) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(1n, Health.spec_hurt(hp, damage)) == True{} : Bool}

law Health.pick_succ provedsource · line 194 · raw

@+order:Cmp -> @+a:Nat -> @+b:Nat -> {Health.pick(order, 1n+a, 1n+b) == 1n+Health.pick(order, a, b) : Nat}

law Health.clamp_agrees provedsource · line 209 · raw

@+hp:Nat -> @+maximum:Nat -> {Health.clamp(hp, maximum) == Health.spec_clamp(hp, maximum) : Nat}

law Health.add_agrees provedsource · line 230 · raw

@+a:Nat -> @+b:Nat -> {Nat.add(a, b) == 0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.add(a, b) : Nat}

law Health.hurt_agrees provedsource · line 243 · raw

@+hp:Nat -> @+damage:Nat -> {Health.hurt(hp, damage) == Health.spec_hurt(hp, damage) : Nat}

law Health.heal_agrees provedsource · line 263 · raw

@+hp:Nat -> @+amount:Nat -> @+maximum:Nat -> {Health.heal(hp, amount, maximum) == Health.spec_heal(hp, amount, maximum) : Nat}

law Health.heal_bounded provedsource · line 273 · raw

@+hp:Nat -> @+amount:Nat -> @+maximum:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.heal(hp, amount, maximum), maximum) == True{} : Bool}

law Health.heal_never_harms provedsource · line 283 · raw

@+hp:Nat -> @+amount:Nat -> @+maximum:Nat -> @+valid:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, maximum) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, Health.heal(hp, amount, maximum)) == True{} : Bool}

law Health.hurt_preserves_bound provedsource · line 294 · raw

@+hp:Nat -> @+damage:Nat -> @+maximum:Nat -> @+valid:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, maximum) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.hurt(hp, damage), maximum) == True{} : Bool}

law Health.hurt_survives provedsource · line 305 · raw

@+hp:Nat -> @+damage:Nat -> @+below:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.lt(damage, hp) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(1n, Health.hurt(hp, damage)) == True{} : Bool}

law Health.clamp_bounded provedsource · line 326 · raw

@+hp:Nat -> @+maximum:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.clamp(hp, maximum), maximum) == True{} : Bool}

law Health.hurt_decreases provedsource · line 335 · raw

@+hp:Nat -> @+damage:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.hurt(hp, damage), hp) == True{} : Bool}

law Health.clamp_word_exact provedsource · line 344 · raw

@+hp:U32 -> @+maximum:U32 -> {U32.to_nat(Health.clamp_word(hp, maximum)) == Health.clamp(U32.to_nat(hp), U32.to_nat(maximum)) : Nat}

law Health.heal_word_exact provedsource · line 352 · raw

@+hp:U32 -> @+amount:U32 -> @+maximum:U32 -> {U32.to_nat(Health.heal_word(hp, amount, maximum)) == Health.heal(U32.to_nat(hp), U32.to_nat(amount), U32.to_nat(maximum)) : Nat}

law Health.hurt_word_exact provedsource · line 361 · raw

@+hp:U32 -> @+damage:U32 -> {U32.to_nat(Health.hurt_word(hp, damage)) == Health.hurt(U32.to_nat(hp), U32.to_nat(damage)) : Nat}

law Health.clamp_word_bounded provedsource · line 369 · raw

@+hp:U32 -> @+maximum:U32 -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(U32.to_nat(Health.clamp_word(hp, maximum)), U32.to_nat(maximum)) == True{} : Bool}

law Health.heal_word_bounded provedsource · line 378 · raw

@+hp:U32 -> @+amount:U32 -> @+maximum:U32 -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(U32.to_nat(Health.heal_word(hp, amount, maximum)), U32.to_nat(maximum)) == True{} : Bool}

law Health.heal_word_never_harms provedsource · line 388 · raw

@+hp:U32 -> @+amount:U32 -> @+maximum:U32 -> @+valid:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(U32.to_nat(hp), U32.to_nat(maximum)) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(U32.to_nat(hp), U32.to_nat(Health.heal_word(hp, amount, maximum))) == True{} : Bool}

law Health.hurt_word_preserves_bound provedsource · line 399 · raw

@+hp:U32 -> @+damage:U32 -> @+maximum:U32 -> @+valid:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(U32.to_nat(hp), U32.to_nat(maximum)) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(U32.to_nat(Health.hurt_word(hp, damage)), U32.to_nat(maximum)) == True{} : Bool}

law Health.hurt_word_survives provedsource · line 410 · raw

@+hp:U32 -> @+damage:U32 -> @+below:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.lt(U32.to_nat(damage), U32.to_nat(hp)) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(1n, U32.to_nat(Health.hurt_word(hp, damage))) == True{} : Bool}

law Health.cap_nat_word_exact provedsource · line 424 · raw

@+value:Nat -> @+maximum:U32 -> {U32.to_nat(Health.cap_nat_word(value, maximum)) == Health.clamp(value, U32.to_nat(maximum)) : Nat}

law Health.spec_clamp_decreases provedsource · line 432 · raw

@+a:Nat -> @+b:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.spec_clamp(a, b), a) == True{} : Bool}

law Health.clamp_decreases provedsource · line 448 · raw

@+a:Nat -> @+b:Nat -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.clamp(a, b), a) == True{} : Bool}

law Health.clamp_monotone provedsource · line 457 · raw

@+a:Nat -> @+b:Nat -> @+maximum:Nat -> @+grow:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(a, b) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Health.clamp(a, maximum), Health.clamp(b, maximum)) == True{} : Bool}

law Health.cap_nat_word_monotone provedsource · line 468 · raw

@+a:Nat -> @+b:Nat -> @+maximum:U32 -> @+grow:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(a, b) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(U32.to_nat(Health.cap_nat_word(a, maximum)), U32.to_nat(Health.cap_nat_word(b, maximum))) == True{} : Bool}

law Health.add_mono_left provedsource · line 480 · raw

@+amount:Nat -> @+a:Nat -> @+b:Nat -> @+grow:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(a, b) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(Nat.add(amount, a), Nat.add(amount, b)) == True{} : Bool}

law Health.maximum_word_monotone provedsource · line 499 · raw

@+a:Nat -> @+b:Nat -> @+bonus:U32 -> @+ceiling:U32 -> @+grow:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(a, b) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(U32.to_nat(Health.maximum_word(a, bonus, ceiling)), U32.to_nat(Health.maximum_word(b, bonus, ceiling))) == True{} : Bool}

law Health.cap_nat_word_positive provedsource · line 510 · raw

@+value:Nat -> @+maximum:U32 -> @+positive:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(1n, value) == True{} : Bool} -> @+limit_positive:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(1n, U32.to_nat(maximum)) == True{} : Bool} -> {0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(1n, U32.to_nat(Health.cap_nat_word(value, maximum))) == True{} : Bool}

law Health.spec_hurt_zero provedsource · line 522 · raw

@+hp:Nat -> @+damage:Nat -> @+lethal:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(hp, damage) == True{} : Bool} -> {Health.spec_hurt(hp, damage) == 0n : Nat}

law Health.hurt_word_zero provedsource · line 539 · raw

@+hp:U32 -> @+damage:U32 -> @+lethal:{0x6f51b0319916bc200dd5f3949f31668e/game.Bq.Nat.le(U32.to_nat(hp), U32.to_nat(damage)) == True{} : Bool} -> {U32.to_nat(Health.hurt_word(hp, damage)) == 0n : Nat}

Definitions

def Health.spec_clamp source · line 7 · raw

@+hp:Nat -> @+maximum:Nat -> Nat

def Health.spec_heal source · line 10 · raw

@+hp:Nat -> @+amount:Nat -> @+maximum:Nat -> Nat

def Health.spec_hurt source · line 13 · raw

@+hp:Nat -> @+damage:Nat -> Nat

def Health.pick source · line 176 · raw

@+order:Cmp -> @+a:Nat -> @+b:Nat -> Nat

These implementations use Base arithmetic, which the native compiler implements with big integers. The refinement proofs below tie the fast implementations to the inductive specification above.

def Health.clamp source · line 185 · raw

@+hp:Nat -> @+maximum:Nat -> Nat

def Health.heal source · line 188 · raw

@+hp:Nat -> @+amount:Nat -> @+maximum:Nat -> Nat

def Health.hurt source · line 191 · raw

@hp:Nat -> @damage:Nat -> Nat

def Health.clamp_word source · line 317 · raw

@hp:U32 -> @maximum:U32 -> U32

Existing U32 records enter the rules here. The refinement and preservation theorems below prove these exact word wrappers cannot wrap their results.

def Health.heal_word source · line 320 · raw

@hp:U32 -> @amount:U32 -> @maximum:U32 -> U32

def Health.hurt_word source · line 323 · raw

@hp:U32 -> @damage:U32 -> U32

def Health.cap_nat_word source · line 421 · raw

@value:Nat -> @maximum:U32 -> U32

Natural stat-derived maxima must be clamped before conversion, too.

def Health.maximum_word source · line 496 · raw

@+raw:Nat -> @bonus:U32 -> @ceiling:U32 -> U32

Add the charm bonus before conversion. The server supplies U32 maximum as the ceiling; keeping it explicit makes the theorem apply to any ceiling.