Metalang99 1.13.3
Full-blown preprocessor metaprogramming
Loading...
Searching...
No Matches
nat.h File Reference

Natural numbers: [0; 255]. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ML99_inc(x)   ML99_call(ML99_inc, x)
 \(x + 1\)
 
#define ML99_dec(x)   ML99_call(ML99_dec, x)
 \(x - 1\)
 
#define ML99_natMatch(x, matcher)   ML99_call(ML99_natMatch, x, matcher)
 Matches x against the two cases: if it is zero or positive.
 
#define ML99_natMatchWithArgs(x, matcher, ...)    ML99_call(ML99_natMatchWithArgs, x, matcher, __VA_ARGS__)
 The same as ML99_natMatch but provides additional arguments to all branches.
 
#define ML99_natEq(x, y)   ML99_call(ML99_natEq, x, y)
 \(x = y\)
 
#define ML99_natNeq(x, y)   ML99_call(ML99_natNeq, x, y)
 \(x \neq y\)
 
#define ML99_greater(x, y)   ML99_call(ML99_greater, x, y)
 \(x > y\)
 
#define ML99_greaterEq(x, y)   ML99_call(ML99_greaterEq, x, y)
 \(x \geq y\)
 
#define ML99_lesser(x, y)   ML99_call(ML99_lesser, x, y)
 \(x < y\)
 
#define ML99_lesserEq(x, y)   ML99_call(ML99_lesserEq, x, y)
 \(x \leq y\)
 
#define ML99_add(x, y)   ML99_call(ML99_add, x, y)
 \(x + y\)
 
#define ML99_sub(x, y)   ML99_call(ML99_sub, x, y)
 \(x - y\)
 
#define ML99_mul(x, y)   ML99_call(ML99_mul, x, y)
 \(x * y\)
 
#define ML99_div(x, y)   ML99_call(ML99_div, x, y)
 \(\frac{x}{y}\)
 
#define ML99_divChecked(x, y)   ML99_call(ML99_divChecked, x, y)
 Like ML99_div but returns ML99_nothing() is x is not divisible by y, otherwise ML99_just(result).
 
#define ML99_mod(x, y)   ML99_call(ML99_mod, x, y)
 Computes the remainder of division.
 
#define ML99_add3(x, y, z)   ML99_call(ML99_add3, x, y, z)
 \(x + y + z\)
 
#define ML99_sub3(x, y, z)   ML99_call(ML99_sub3, x, y, z)
 \(x - y - z\)
 
#define ML99_mul3(x, y, z)   ML99_call(ML99_mul3, x, y, z)
 \(x * y * z\)
 
#define ML99_div3(x, y, z)   ML99_call(ML99_div3, x, y, z)
 \(\frac{(\frac{x}{y})}{z}\)
 
#define ML99_min(x, y)   ML99_call(ML99_min, x, y)
 \(min(x, y)\)
 
#define ML99_max(x, y)   ML99_call(ML99_max, x, y)
 \(max(x, y)\)
 
#define ML99_assertIsNat(x)   ML99_call(ML99_assertIsNat, x)
 Emits a fatal error if x is not a natural number, otherwise results in emptiness.
 
#define ML99_INC(x)   ML99_PRIV_INC(x)
 
#define ML99_DEC(x)   ML99_PRIV_DEC(x)
 
#define ML99_NAT_EQ(x, y)   ML99_PRIV_NAT_EQ(x, y)
 
#define ML99_NAT_NEQ(x, y)   ML99_PRIV_NOT(ML99_NAT_EQ(x, y))
 
#define ML99_DIV_CHECKED(x, y)   ML99_PRIV_DIV_CHECKED(x, y)
 
#define ML99_NAT_MAX   255
 The maximum value of a natural number, currently 255.
 

Detailed Description

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.

Macro Definition Documentation

◆ ML99_add

#define ML99_add (   x,
 
)    ML99_call(ML99_add, x, y)

\(x + y\)

Examples

#include <metalang99/nat.h>
// 11
ML99_add(v(5), v(6))
#define v(...)
A value that is pasted as-is; no evaluation occurs on provided arguments.
Definition lang.h:145
Natural numbers: [0; 255].
#define ML99_add(x, y)
Definition nat.h:211

◆ ML99_add3

#define ML99_add3 (   x,
  y,
 
)    ML99_call(ML99_add3, x, y, z)

\(x + y + z\)

Examples

#include <metalang99/nat.h>
// 15
ML99_add3(v(1), v(6), v(8))
#define ML99_add3(x, y, z)
Definition nat.h:306

◆ ML99_assertIsNat

#define ML99_assertIsNat (   x)    ML99_call(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))
#define ML99_call(op,...)
Invokes a metafunction with arguments.
Definition lang.h:33

◆ ML99_dec

#define ML99_dec (   x)    ML99_call(ML99_dec, x)

\(x - 1\)

Examples

#include <metalang99/nat.h>
// 4
#define ML99_dec(x)
Definition nat.h:51
Note
If x is 0, the result is ML99_NAT_MAX.

◆ ML99_div

#define ML99_div (   x,
 
)    ML99_call(ML99_div, x, y)

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

Examples

#include <metalang99/nat.h>
// 3
ML99_div(v(12), v(4))
#define ML99_div(x, y)
Definition nat.h:255
Note
A compile-time error if \(\frac{x}{y}\) is not a natural number.

◆ ML99_div3

#define ML99_div3 (   x,
  y,
 
)    ML99_call(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_divChecked

#define ML99_divChecked (   x,
 
)    ML99_call(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_nothing()
// ML99_nothing()
#define ML99_divChecked(x, y)
Like ML99_div but returns ML99_nothing() is x is not divisible by y, otherwise ML99_just(result).
Definition nat.h:276

◆ ML99_greater

#define ML99_greater (   x,
 
)    ML99_call(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))
#define ML99_greater(x, y)
Definition nat.h:146

◆ ML99_greaterEq

#define ML99_greaterEq (   x,
 
)    ML99_call(ML99_greaterEq, x, y)

\(x \geq y\)

Examples

#include <metalang99/nat.h>
// 1
// 0
#define ML99_greaterEq(x, y)
Definition nat.h:163

◆ ML99_inc

#define ML99_inc (   x)    ML99_call(ML99_inc, x)

\(x + 1\)

Examples

#include <metalang99/nat.h>
// 6
#define ML99_inc(x)
Definition nat.h:35
Note
If x is ML99_NAT_MAX, the result is 0.

◆ ML99_lesser

#define ML99_lesser (   x,
 
)    ML99_call(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))
#define ML99_lesser(x, y)
Definition nat.h:180

◆ ML99_lesserEq

#define ML99_lesserEq (   x,
 
)    ML99_call(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))
#define ML99_lesserEq(x, y)
Definition nat.h:197

◆ ML99_max

#define ML99_max (   x,
 
)    ML99_call(ML99_max, x, y)

\(max(x, y)\)

Examples

#include <metalang99/nat.h>
// 7
ML99_max(v(5), v(7))
#define ML99_max(x, y)
Definition nat.h:378

◆ ML99_min

#define ML99_min (   x,
 
)    ML99_call(ML99_min, x, y)

\(min(x, y)\)

Examples

#include <metalang99/nat.h>
// 5
ML99_min(v(5), v(7))
#define ML99_min(x, y)
Definition nat.h:364

◆ ML99_mod

#define ML99_mod (   x,
 
)    ML99_call(ML99_mod, x, y)

Computes the remainder of division.

Examples

#include <metalang99/nat.h>
// 2
ML99_mod(v(8), v(3))
#define ML99_mod(x, y)
Computes the remainder of division.
Definition nat.h:292
Note
A compile-time error if y is 0.

◆ ML99_mul

#define ML99_mul (   x,
 
)    ML99_call(ML99_mul, x, y)

\(x * y\)

Examples

#include <metalang99/nat.h>
// 12
ML99_mul(v(3), v(4))
#define ML99_mul(x, y)
Definition nat.h:239

◆ ML99_mul3

#define ML99_mul3 (   x,
  y,
 
)    ML99_call(ML99_mul3, x, y, z)

\(x * y * z\)

Examples

#include <metalang99/nat.h>
// 24
ML99_mul3(v(2), v(3), v(4))
#define ML99_mul3(x, y, z)
Definition nat.h:334

◆ ML99_natEq

#define ML99_natEq (   x,
 
)    ML99_call(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))
#define ML99_natEq(x, y)
Definition nat.h:112

◆ ML99_natMatch

#define ML99_natMatch (   x,
  matcher 
)    ML99_call(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_))
#define ML99_natMatch(x, matcher)
Matches x against the two cases: if it is zero or positive.
Definition nat.h:74
Note
This function calls f with ML99_call, so no partial application occurs, and so arity specifiers are not needed.

◆ ML99_natMatchWithArgs

#define ML99_natMatchWithArgs (   x,
  matcher,
  ... 
)     ML99_call(ML99_natMatchWithArgs, x, matcher, __VA_ARGS__)

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))
#define ML99_natMatchWithArgs(x, matcher,...)
The same as ML99_natMatch but provides additional arguments to all branches.
Definition nat.h:94

◆ ML99_natNeq

#define ML99_natNeq (   x,
 
)    ML99_call(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))
#define ML99_natNeq(x, y)
Definition nat.h:129

◆ ML99_sub

#define ML99_sub (   x,
 
)    ML99_call(ML99_sub, x, y)

\(x - y\)

Examples

#include <metalang99/nat.h>
// 6
ML99_sub(v(11), v(5))
#define ML99_sub(x, y)
Definition nat.h:225

◆ ML99_sub3

#define ML99_sub3 (   x,
  y,
 
)    ML99_call(ML99_sub3, x, y, z)

\(x - y - z\)

Examples

#include <metalang99/nat.h>
// 3
ML99_sub3(v(8), v(2), v(3))
#define ML99_sub3(x, y, z)
Definition nat.h:320