Skip to main content

Int32

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

Int32 is a checked signed 32-bit surface. Treat fromInt, narrowing conversions, and non-wrapping arithmetic as proof obligations; use wrap helpers when modular machine arithmetic is the required behavior.

Import

mo:core/Int32

Status

  • Runtime module

Public API

Types

  • Int32

Values

  • minValue
  • maxValue

Functions

toBitsNatSpec(x : Int32) : Nat

Converts between the module type and the target representation.

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

toInt(x : Int32) : Int

Converts between the module type and the target representation.

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

fromInt(x : Int) : Int32

Converts between the module type and the target representation.

Contract

requires x >= -2_147_483_648;
requires x <= 2_147_483_647;

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

fromIntWrap(x : Int) : Int32

Converts between the module type and the target representation.

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

fromInt16(x : Int16) : Int32

Converts between the module type and the target representation.

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

toInt16(x : Int32) : Int16

Converts between the module type and the target representation.

Contract

requires x >= (-32_768 : Int32);
requires x <= (32_767 : Int32);

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

fromInt64(x : Int64) : Int32

Converts between the module type and the target representation.

Contract

requires x >= (-2_147_483_648 : Int64);
requires x <= (2_147_483_647 : Int64);

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

toInt64(x : Int32) : Int64

Converts between the module type and the target representation.

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

fromNat32(x : Nat32) : Int32

Converts between the module type and the target representation.

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

toNat32(x : Int32) : Nat32

Converts between the module type and the target representation.

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

toText(x : Int32) : Text

Converts between the module type and the target representation.

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

abs(x : Int32) : Int32

Returns the absolute value.

Contract

requires x != minValue;
ensures result == (if (x < 0) { -x } else { x });

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

min(x : Int32, y : Int32) : Int32

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

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

neg(x : Int32) : Int32

Returns the arithmetic negation.

Contract

requires x != (-2_147_483_648 : Int32);
ensures result == -x;
ensures inRange(result);

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

add(x : Int32, y : Int32) : Int32

Returns the sum of the two operands.

Contract

requires (-2_147_483_648 : Int32) <= x + y;
requires x + y <= (2_147_483_647 : Int32);
ensures result == x + y;
ensures inRange(result);

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

sub(x : Int32, y : Int32) : Int32

Returns the left operand minus the right operand.

Contract

requires (-2_147_483_648 : Int32) <= x - y;
requires x - y <= (2_147_483_647 : Int32);
ensures result == x - y;
ensures inRange(result);

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

mul(x : Int32, y : Int32) : Int32

Returns the product of the two operands.

Contract

requires (-2_147_483_648 : Int32) <= x * y;
requires x * y <= (2_147_483_647 : Int32);
ensures result == x * y;
ensures inRange(result);

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

div(x : Int32, y : Int32) : Int32

Returns the integer quotient, subject to the divisor precondition.

Contract

requires y != 0;
requires not (x == (-2_147_483_648 : Int32) and y == (-1 : Int32));
ensures y != 0 ==> result == x / y;
ensures inRange(result);

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

rem(x : Int32, y : Int32) : Int32

Returns the integer remainder, subject to the divisor precondition.

Contract

requires y != 0;
ensures y != 0 ==> result == x % y;
ensures inRange(result);

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

pow(x : Int32, y : Int32) : Int32

Returns the left operand raised to the supplied exponent.

Contract

requires y >= (0 : Int32);
requires y < (32 : Int32);
requires y <= (1 : Int32) or
(-2_147_483_648 <= Int.pow(toInt(x), toInt(y)) and Int.pow(toInt(x), toInt(y)) <= 2_147_483_647);
ensures (y >= 0 and y < (32 : Int32)) ==>
toBitsNatSpec(result) == Nat.pow(toBitsNatSpec(x), toBitsNatSpec(y)) % 4_294_967_296;
ensures y == 0 ==> result == 1;
ensures y == 1 ==> result == x;
ensures inRange(result);

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

bitnot(x : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);

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

bitand(x : Int32, y : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);

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

bitor(x : Int32, y : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);

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

bitxor(x : Int32, y : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);

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

bitshiftLeft(x : Int32, y : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures y == 0 ==> result == x;

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

bitshiftRight(x : Int32, y : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures y == 0 ==> result == x;

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

bitrotLeft(x : Int32, y : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures y == 0 ==> result == x;

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

bitrotRight(x : Int32, y : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures y == 0 ==> result == x;

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

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

Performs the corresponding bit-level operation.

Contract

ensures result == Prim.btstInt32(x, Prim.intToInt32(p));

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

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

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures p >= 32 ==> result == x;

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

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

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures p >= 32 ==> result == x;

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

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

Performs the corresponding bit-level operation.

Contract

ensures inRange(result);
ensures p >= 32 ==> result == x;

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

bitcountNonZero(x : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures result == Prim.popcntInt32(x);
ensures inRange(result);

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

bitcountLeadingZero(x : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures result == Prim.clzInt32(x);
ensures inRange(result);

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

bitcountTrailingZero(x : Int32) : Int32

Performs the corresponding bit-level operation.

Contract

ensures result == Prim.ctzInt32(x);
ensures inRange(result);

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

explode(x : Int32) : (Nat8, Nat8, 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 : Int32, y : Int32) : Int32

Returns wrapping addition for the fixed-width type.

Contract

ensures inRange(result);

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

subWrap(x : Int32, y : Int32) : Int32

Returns wrapping subtraction for the fixed-width type.

Contract

ensures inRange(result);

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

mulWrap(x : Int32, y : Int32) : Int32

Returns wrapping multiplication for the fixed-width type.

Contract

ensures inRange(result);

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

powWrap(x : Int32, y : Int32) : Int32

Returns wrapping exponentiation for the fixed-width type.

Contract

ensures (y >= 0 and y < (32 : Int32)) ==>
toBitsNatSpec(result) == Nat.pow(toBitsNatSpec(x), toBitsNatSpec(y)) % 4_294_967_296;
ensures y == 0 ==> result == 1;
ensures y == 1 ==> result == x;
ensures inRange(result);

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

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

Creates an iterator over the requested numeric range or domain.

Contract

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

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

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

Creates an iterator over the requested numeric range or domain.

Contract

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

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

allValues() : Iter.Iter<Int32>

Creates an iterator over the requested numeric range or domain.

Contract

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

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

Summary

  • Runtime module under mo:core/Int32.
  • Exposes 51 public functions.