Natural Numbers

Basic Operations

isNat

(isNat n)
> n : a
> Bool

Checks if a value is a natural number.

isNat 5       == 1
isNat 0       == 1
isNat SOME    == 0
isNat b#hi    == 0

toNat

(toNat n)
> n : a
> Nat

Converts a value to a natural number. If the input is already a natural number, it returns it unchanged.

toNat 5       == 5
toNat 0       == 0
toNat SOME    == 0 ; Non-numbers become 0
toNat b#hi    == 0

times

Applies a function f to an initial value z, x times.

inc

Increments a natural number by 1.

dec

Decrements a natural number by 1. Returns 0 if the input is 0.

add

Adds two natural numbers.

sub

Subtracts one natural number from another. Returns 0 if the result would be negative.

mod

Calculates the modulus of two natural numbers.

mul

Multiplies two natural numbers.

div

Performs integer division on two natural numbers. Division by zero returns 0.

divMod

Performs both division and modulus in one operation. Returns a tuple of (quotient, remainder).

isOne

Checks if a natural number is equal to 1.

Bitwise Operations

lsh

Left-shifts a natural number by a given amount.

rsh

Right-shifts a natural number by a given amount.

con

Performs a bitwise AND operation on two natural numbers.

mix

Performs a bitwise XOR operation on two natural numbers.

dis

Performs a bitwise OR operation on two natural numbers.

pow

Raises a base to a power.

bex

Calculates 2 raised to a given power.

bix

Extracts a specific bit from a natural number.

natEql

Checks if two natural numbers are equal.

natCmp

Compares two natural numbers, returning an ordering result.

Advanced Bitwise Operations

bitwise

Applies a bitwise operation to two natural numbers.

natFold

Folds a function over the bits of a natural number.

met

Calculates the number of bits required to represent a natural number.

popCount

Counts the number of set bits in a natural number.

trunc

Truncates a natural number to a given bit width.

bitSlice

Extracts a slice of bits from a natural number.

setBit

Sets a specific bit in a natural number.

clearBit

Clears a specific bit in a natural number.

testBit

Tests if a specific bit is set in a natural number.

Miscellaneous

roundUp

even

Checks if a natural number is even.

odd

Checks if a natural number is odd.

Last updated