Skip to main content

Nat16

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

Nat16 is an unsigned 16-bit surface. Use it when storage or protocol shape requires 16-bit values; prove bounds before checked construction or arithmetic.

Import

mo:core/Nat16

Status

  • Runtime module

Public API

Types

  • Nat16

Values

  • maxValue

Functions

toNatSpec(x : Nat16) : Nat

Converts between the module type and the target representation.

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

toNat(x : Nat16) : Nat

Converts between the module type and the target representation.

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

fromNat(n : Nat) : Nat16

Converts between the module type and the target representation.

Contract

requires n <= 65_535;

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

fromNat8(x : Nat8) : Nat16

Converts between the module type and the target representation.

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

toNat8(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.

fromNat32(x : Nat32) : Nat16

Converts between the module type and the target representation.

Contract

requires x <= (65_535 : Nat32);

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

toNat32(x : Nat16) : Nat32

Converts between the module type and the target representation.

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

fromIntWrap(x : Int) : Nat16

Converts between the module type and the target representation.

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

toText(x : Nat16) : Text

Converts between the module type and the target representation.

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

min(x : Nat16, y : Nat16) : Nat16

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 : Nat16, y : Nat16) : Nat16

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 : Nat16, y : Nat16) : 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 : Nat16, y : Nat16) : 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 : Nat16, y : Nat16) : 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 : Nat16, y : Nat16) : 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 : Nat16, y : Nat16) : 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 : Nat16, y : Nat16) : 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 : Nat16, y : Nat16) : 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 : Nat16, y : Nat16) : Nat16

Returns the sum of the two operands.

Contract

requires x + y <= (65_535 : Nat16);
ensures result == x + y;

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

sub(x : Nat16, y : Nat16) : Nat16

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 : Nat16, y : Nat16) : Nat16

Returns the product of the two operands.

Contract

requires x * y <= (65_535 : Nat16);
ensures result == x * y;

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

div(x : Nat16, y : Nat16) : Nat16

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 : Nat16, y : Nat16) : Nat16

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 : Nat16, y : Nat16) : Nat16

Returns the left operand raised to the supplied exponent.

Contract

requires y <= (1 : Nat16) or Nat.pow(toNatSpec(x), toNatSpec(y)) <= 65_535;
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 : Nat16) : Nat16

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 : Nat16, y : Nat16) : Nat16

Performs the corresponding bit-level operation.

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

bitor(x : Nat16, y : Nat16) : Nat16

Performs the corresponding bit-level operation.

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

bitxor(x : Nat16, y : Nat16) : Nat16

Performs the corresponding bit-level operation.

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

bitshiftLeft(x : Nat16, y : Nat16) : Nat16

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 : Nat16, y : Nat16) : Nat16

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 : Nat16, y : Nat16) : Nat16

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 : Nat16, y : Nat16) : Nat16

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 : Nat16, p : Nat) : Bool

Performs the corresponding bit-level operation.

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

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

Performs the corresponding bit-level operation.

Contract

ensures p >= 16 ==> result == x;

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

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

Performs the corresponding bit-level operation.

Contract

ensures p >= 16 ==> result == x;

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

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

Performs the corresponding bit-level operation.

Contract

ensures p >= 16 ==> result == x;

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

bitcountNonZero(x : Nat16) : Nat16

Performs the corresponding bit-level operation.

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

bitcountLeadingZero(x : Nat16) : Nat16

Performs the corresponding bit-level operation.

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

bitcountTrailingZero(x : Nat16) : Nat16

Performs the corresponding bit-level operation.

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

explode(x : Nat16) : (Nat8, Nat8)

Breaks the fixed-width integer into its byte components.

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

addWrap(x : Nat16, y : Nat16) : Nat16

Returns wrapping addition for the fixed-width type.

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

subWrap(x : Nat16, y : Nat16) : Nat16

Returns wrapping subtraction for the fixed-width type.

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

mulWrap(x : Nat16, y : Nat16) : Nat16

Returns wrapping multiplication for the fixed-width type.

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

powWrap(x : Nat16, y : Nat16) : Nat16

Returns wrapping exponentiation for the fixed-width type.

Contract

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

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

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

Creates an iterator over the requested numeric range or domain.

Contract

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

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

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

Creates an iterator over the requested numeric range or domain.

Contract

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

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

allValues() : Iter.Iter<Nat16>

Creates an iterator over the requested numeric range or domain.

Contract

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

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

Summary

  • Runtime module under mo:core/Nat16.
  • Exposes 47 public functions.