Fixed-width integers
Release v0.4.0
Int stays unbounded. I8, I16, I32, I64, U8, U16, U32 and U64 exist where the width matters (spec/18).
- A literal takes the fixed-width type its context expects, including inside a constructor or a list (
Some(3)againstOption[U8]); otherwise it is anInt. A literal that does not fit is U2022. - Operands of an operator have one integer type; there is no implicit conversion (U2001). Each
+,-,*,/,modand unary-on a fixed-width type, and each narrowing conversion, is anoverflowobligation: the exact result fits. A refuted one is U5001 with a counterexample. - Conversions are methods:
to_i8()toto_u64()andto_int(). Widening always succeeds; narrowing carries anoverflowobligation. - Wrapping, saturating and bit arithmetic exist only by name:
wrapping_add(by: b),saturating_sub(by: b),bit_and(mask: m),shift_right(by: n)and the others of spec/18 §5. - Clauses (
where,requires,ensures) and the bodies ofreflectpredicates compute exactly, with no overflow obligation:ensures result <= 300on aU8result is proven.
una
module demo
--- A literal takes the type it is checked against: `Some(3)` here is an `Option[U8]`.
pub default_level() -> Option[U8] = Some(3)
--- The guard on `Int` proves that `a + b` fits a `U8`.
pub add_bytes(a: U8, b: U8) -> U8 = if a.to_int() + b.to_int() <= 255 then a + b else 255
--- Wrapping arithmetic only by name.
pub hash_step(h: U32, byte: U8) -> U32 = h.wrapping_mul(by: 31).wrapping_add(by: byte.to_u32())
--- A `reflect` body computes exactly, with no overflow obligation.
pub predicate reflect large(x: U32) -> Bool = x * 4294967295 > 4294967295una
-- fails with U5001
module demo
pub add(a: U8, b: U8) -> U8 = a + b