nat.h

Natural numbers: [0; 255].

Most of the time, natural numbers are used for iteration; they are not meant for CPU-bound tasks such as Fibonacci numbers or factorials.

Defines

ML99_inc(x)

\(x + 1\)

Examples

#include <metalang99/nat.h>

// 6
ML99_inc(v(5))

Note

If x is ML99_NAT_MAX, the result is 0.

ML99_dec(x)

\(x - 1\)

Examples

#include <metalang99/nat.h>

// 4
ML99_dec(v(5))

Note

If x is 0, the result is ML99_NAT_MAX.

ML99_natMatch(x, matcher)

Matches x against the two cases: if it is zero or positive.

Examples

#include <metalang99/nat.h>

#define MATCH_Z_IMPL()  v(Billie)
#define MATCH_S_IMPL(x) v(Jean ~ x)

// Billie
ML99_natMatch(v(0), v(MATCH_))

// Jean ~ 122
ML99_natMatch(v(123), v(MATCH_))

Note

This function calls f with ML99_call, so no partial application occurs, and so arity specifiers are not needed.

ML99_natMatchWithArgs(x, matcher, ...)

The same as ML99_natMatch but provides additional arguments to all branches.

Examples

#include <metalang99/nat.h>

#define MATCH_Z_IMPL(x, y, z)    v(Billie ~ x y z)
#define MATCH_S_IMPL(n, x, y, z) v(Jean ~ n ~ x y z)

// Billie ~ 1 2 3
ML99_natMatchWithArgs(v(0), v(MATCH_), v(1, 2, 3))

// Jean ~ 122 ~ 1 2 3
ML99_natMatchWithArgs(v(123), v(MATCH_), v(1, 2, 3))
ML99_natEq(x, y)

\(x = y\)

Examples

#include <metalang99/nat.h>

// 1
ML99_natEq(v(5), v(5))

// 0
ML99_natEq(v(3), v(8))
ML99_natNeq(x, y)

\(x \neq y\)

Examples

#include <metalang99/nat.h>

// 0
ML99_natNeq(v(5), v(5))

// 1
ML99_natNeq(v(3), v(8))
ML99_greater(x, y)

\(x > y\)

Examples

#include <metalang99/nat.h>

// 1
ML99_greater(v(8), v(3))

// 0
ML99_greater(v(3), v(8))
ML99_greaterEq(x, y)

\(x \geq y\)

Examples

#include <metalang99/nat.h>

// 1
ML99_greaterEq(v(8), v(8))

// 0
ML99_greaterEq(v(3), v(8))
ML99_lesser(x, y)

\(x < y\)

Examples

#include <metalang99/nat.h>

// 1
ML99_lesser(v(3), v(8))

// 0
ML99_lesser(v(8), v(3))
ML99_lesserEq(x, y)

\(x \leq y\)

Examples

#include <metalang99/nat.h>

// 1
ML99_lesserEq(v(8), v(8))

// 0
ML99_lesserEq(v(8), v(3))
ML99_add(x, y)

\(x + y\)

Examples

#include <metalang99/nat.h>

// 11
ML99_add(v(5), v(6))
ML99_sub(x, y)

\(x - y\)

Examples

#include <metalang99/nat.h>

// 6
ML99_sub(v(11), v(5))
ML99_mul(x, y)

\(x * y\)

Examples

#include <metalang99/nat.h>

// 12
ML99_mul(v(3), v(4))
ML99_div(x, y)

\(\frac{x}{y}\)

Examples

#include <metalang99/nat.h>

// 3
ML99_div(v(12), v(4))

Note

A compile-time error if \(\frac{x}{y}\) is not a natural number.

ML99_divChecked(x, y)

Like ML99_div but returns ML99_nothing() is x is not divisible by y, otherwise ML99_just(result).

Examples

#include <metalang99/nat.h>

// ML99_just(3)
ML99_divChecked(v(12), v(4))

// ML99_nothing()
ML99_divChecked(v(14), v(5))

// ML99_nothing()
ML99_divChecked(v(1), v(0))
ML99_mod(x, y)

Computes the remainder of division.

Examples

#include <metalang99/nat.h>

// 2
ML99_mod(v(8), v(3))

Note

A compile-time error if y is 0.

ML99_add3(x, y, z)

\(x + y + z\)

Examples

#include <metalang99/nat.h>

// 15
ML99_add3(v(1), v(6), v(8))
ML99_sub3(x, y, z)

\(x - y - z\)

Examples

#include <metalang99/nat.h>

// 3
ML99_sub3(v(8), v(2), v(3))
ML99_mul3(x, y, z)

\(x * y * z\)

Examples

#include <metalang99/nat.h>

// 24
ML99_mul3(v(2), v(3), v(4))
ML99_div3(x, y, z)

\(\frac{(\frac{x}{y})}{z}\)

Examples

#include <metalang99/nat.h>

// 5
ML99_div(v(30), v(3), v(2))

Note

A compile-time error if \(\frac{(\frac{x}{y})}{z}\) is not a natural number.

ML99_min(x, y)

\(min(x, y)\)

Examples

#include <metalang99/nat.h>

// 5
ML99_min(v(5), v(7))
ML99_max(x, y)

\(max(x, y)\)

Examples

#include <metalang99/nat.h>

// 7
ML99_max(v(5), v(7))
ML99_assertIsNat(x)

Emits a fatal error if x is not a natural number, otherwise results in emptiness.

Examples

#include <metalang99/nat.h>

#define F_IMPL(x) ML99_TERMS(ML99_assertIsNat(v(x)), ML99_inc(v(x)))

// 6
ML99_call(F, v(5))

// A compile-time number mismatch error.
ML99_call(F, v(blah))
ML99_INC(x)
ML99_DEC(x)
ML99_NAT_EQ(x, y)
ML99_NAT_NEQ(x, y)
ML99_DIV_CHECKED(x, y)
ML99_NAT_MAX

The maximum value of a natural number, currently 255.