Skip to main content

Nat8

Reference for mo:core/Nat8 in the core library.

Nat8 is an unsigned 8-bit surface. Checked arithmetic carries upper-bound preconditions, while fromIntWrap and wrap helpers model byte-style modular behavior.

Import

mo:core/Nat8

Status

  • Runtime module

Public API

Types

  • Nat8

Values

  • maxValue

Functions

toNatSpec(x : Nat8) : Nat

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

toNat(x : Nat8) : Nat

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

fromNat(n : Nat) : Nat8

Converts between the module type and the target representation.

Contract

requires n <= 255;

Use when: crossing between this module's value and another representation.

fromNat16(x : Nat16) : Nat8

Converts between the module type and the target representation.

Contract

requires x <= (255 : Nat16);

Use when: crossing between this module's value and another representation.

toNat16(x : Nat8) : Nat16

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

fromIntWrap(x : Int) : Nat8

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

toText(x : Nat8) : Text

Converts between the module type and the target representation.

Use when: crossing between this module's value and another representation.

min(x : Nat8, y : Nat8) : Nat8

Returns the selected bound according to the module's ordering.

Contract

ensures result == (if (x < y) { x } else { y });

Use when: code or specifications need this operation with the documented contract.

max(x : Nat8, y : Nat8) : Nat8

Returns the selected bound according to the module's ordering.

Contract

ensures result == (if (x < y) { y } else { x });

Use when: code or specifications need this operation with the documented contract.

equal(x : Nat8, y : Nat8) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x == y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

notEqual(x : Nat8, y : Nat8) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x != y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

less(x : Nat8, y : Nat8) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x < y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

lessOrEqual(x : Nat8, y : Nat8) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x <= y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

greater(x : Nat8, y : Nat8) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x > y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

greaterOrEqual(x : Nat8, y : Nat8) : Bool

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == (x >= y);

Use when: code or contracts need an explicit comparison helper instead of an operator.

compare(x : Nat8, y : Nat8) : Types.Order

Compares the supplied values and relates the result to the underlying order.

Contract

ensures result == #less ==> x < y;
ensures result == #equal ==> x == y;
ensures result == #greater ==> x > y;
ensures x < y ==> result == #less;
ensures x == y ==> result == #equal;
ensures x > y ==> result == #greater;

Use when: code or contracts need an explicit comparison helper instead of an operator.

add(x : Nat8, y : Nat8) : Nat8

Returns the sum of the two operands.

Contract

requires x + y <= (255 : Nat8);
ensures result == x + y;

Use when: the postcondition must relate the updated value to the previous one.

sub(x : Nat8, y : Nat8) : Nat8

Returns the left operand minus the right operand.

Contract

requires x >= y;
ensures result == x - y;

Use when: code or specifications need this operation with the documented contract.

mul(x : Nat8, y : Nat8) : Nat8

Returns the product of the two operands.

Contract

requires x * y <= (255 : Nat8);
ensures result == x * y;

Use when: code or specifications need this operation with the documented contract.

div(x : Nat8, y : Nat8) : Nat8

Returns the integer quotient, subject to the divisor precondition.

Contract

requires y != 0;
ensures y != 0 ==> result == x / y;

Use when: code or specifications need this operation with the documented contract.

rem(x : Nat8, y : Nat8) : Nat8

Returns the integer remainder, subject to the divisor precondition.

Contract

requires y != 0;
ensures y != 0 ==> result == x % y;
ensures y != 0 ==> result < y;

Use when: code or specifications need this operation with the documented contract.

pow(x : Nat8, y : Nat8) : Nat8

Returns the left operand raised to the supplied exponent.

Contract

requires y <= (1 : Nat8) or Nat.pow(toNatSpec(x), toNatSpec(y)) <= 255;
ensures toNatSpec(result) == Nat.pow(toNatSpec(x), toNatSpec(y));
ensures y == 0 ==> result == 1;
ensures y == 1 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitnot(x : Nat8) : Nat8

Performs the corresponding bit-level operation.

Contract

ensures result + x == maxValue;

Use when: code or specifications need this operation with the documented contract.

bitand(x : Nat8, y : Nat8) : Nat8

Performs the corresponding bit-level operation.

Use when: code or specifications need this operation with the documented contract.

bitor(x : Nat8, y : Nat8) : Nat8

Performs the corresponding bit-level operation.

Use when: code or specifications need this operation with the documented contract.

bitxor(x : Nat8, y : Nat8) : Nat8

Performs the corresponding bit-level operation.

Use when: code or specifications need this operation with the documented contract.

bitshiftLeft(x : Nat8, y : Nat8) : Nat8

Performs the corresponding bit-level operation.

Contract

ensures y == 0 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitshiftRight(x : Nat8, y : Nat8) : Nat8

Performs the corresponding bit-level operation.

Contract

ensures y == 0 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitrotLeft(x : Nat8, y : Nat8) : Nat8

Performs the corresponding bit-level operation.

Contract

ensures y == 0 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitrotRight(x : Nat8, y : Nat8) : Nat8

Performs the corresponding bit-level operation.

Contract

ensures y == 0 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bittest(x : Nat8, p : Nat) : Bool

Performs the corresponding bit-level operation.

Use when: code or specifications need this operation with the documented contract.

bitset(x : Nat8, p : Nat) : Nat8

Performs the corresponding bit-level operation.

Contract

ensures p >= 8 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitclear(x : Nat8, p : Nat) : Nat8

Performs the corresponding bit-level operation.

Contract

ensures p >= 8 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitflip(x : Nat8, p : Nat) : Nat8

Performs the corresponding bit-level operation.

Contract

ensures p >= 8 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

bitcountNonZero(x : Nat8) : Nat8

Performs the corresponding bit-level operation.

Use when: code or specifications need this operation with the documented contract.

bitcountLeadingZero(x : Nat8) : Nat8

Performs the corresponding bit-level operation.

Use when: code or specifications need this operation with the documented contract.

bitcountTrailingZero(x : Nat8) : Nat8

Performs the corresponding bit-level operation.

Use when: code or specifications need this operation with the documented contract.

addWrap(x : Nat8, y : Nat8) : Nat8

Returns wrapping addition for the fixed-width type.

Use when: the postcondition must relate the updated value to the previous one.

subWrap(x : Nat8, y : Nat8) : Nat8

Returns wrapping subtraction for the fixed-width type.

Use when: code or specifications need this operation with the documented contract.

mulWrap(x : Nat8, y : Nat8) : Nat8

Returns wrapping multiplication for the fixed-width type.

Use when: code or specifications need this operation with the documented contract.

powWrap(x : Nat8, y : Nat8) : Nat8

Returns wrapping exponentiation for the fixed-width type.

Contract

ensures toNatSpec(result) == Nat.pow(toNatSpec(x), toNatSpec(y)) % 256;
ensures y == 0 ==> result == 1;
ensures y == 1 ==> result == x;

Use when: code or specifications need this operation with the documented contract.

range(fromInclusive : Nat8, toExclusive : Nat8) : Iter.Iter<Nat8>

Creates an iterator over the requested numeric range or domain.

Contract

ensures Iter.Spec.forall<Nat8>(pure func (k : Nat8) : Bool =
Iter.Spec.contains<Nat8>(result, k) ==> (k >= fromInclusive and k < toExclusive));
ensures Iter.Spec.forall<Nat8>(pure func (k : Nat8) : Bool =
(k >= fromInclusive and k < toExclusive) ==> Iter.Spec.contains<Nat8>(result, k));

Use when: iteration boundaries should be explicit in code and examples.

rangeInclusive(from : Nat8, to : Nat8) : Iter.Iter<Nat8>

Creates an iterator over the requested numeric range or domain.

Contract

ensures (from > to) ==> Iter.Spec.forall<Nat8>(pure func (k : Nat8) : Bool = not Iter.Spec.contains<Nat8>(result, k));
ensures (from <= to) ==> Iter.Spec.contains<Nat8>(result, from);
ensures Iter.Spec.forall<Nat8>(pure func (k : Nat8) : Bool =
Iter.Spec.contains<Nat8>(result, k) ==> (k >= from and k <= to));
ensures Iter.Spec.forall<Nat8>(pure func (k : Nat8) : Bool =
(k >= from and k <= to) ==> Iter.Spec.contains<Nat8>(result, k));

Use when: iteration boundaries should be explicit in code and examples.

allValues() : Iter.Iter<Nat8>

Creates an iterator over the requested numeric range or domain.

Contract

ensures Iter.Spec.forall<Nat8>(pure func (k : Nat8) : Bool =
Iter.Spec.contains<Nat8>(result, k) ==> (k <= maxValue));
ensures Iter.Spec.forall<Nat8>(pure func (k : Nat8) : Bool =
(k <= maxValue) ==> Iter.Spec.contains<Nat8>(result, k));

Use when: iteration boundaries should be explicit in code and examples.

Summary

  • Runtime module under mo:core/Nat8.
  • Exposes 44 public functions.