core design

Design goal

luna-generic gives LunaFlow a shared algebraic vocabulary. Packages such as arithmetic, luna-complex, linear-algebra, and luna-poly should be able to describe their requirements in terms of reusable traits instead of inventing their own incompatible capability layers.

Main design decisions

  • The operation capability graph and the exact law layer are separate. Bundles such as RingOps and FieldOps compose primitive operations; exact structures return opaque Law[Theory, Self] certificates.
  • Structural traits such as Ring, Field, Integral, and Nat do not inherit exact laws through a supertrait chain. Each law has a distinct theory tag and an explicit certificate method.
  • Conversions are split into two halves that meet at ℤ, represented by BigInt. The target side is an operation FromInteger; it does not by itself claim to be a homomorphism. The source side is Integral::normalize, which picks a representative. lift_to composes the operations and promises nothing about preservation.
  • The halves are single-parameter traits because every conversion factors through ℤ, the initial ring: no trait has to relate two types.
  • Integral combines semiring operations and FromInteger, and returns an explicit IntegralTh certificate for from_integer(normalize(x)) == x. This makes an integral type a quotient of ℤ with chosen representatives. FromInteger takes a BigInt rather than a generic integral source, so the traits do not refer to each other.
  • Unsigned types stop before additive inverse, so the abstraction stays mathematically honest.
  • Field means a commutative field. FieldTh is a contract returned by the implementor; neither the compiler nor finite tests prove its equations.

Mathematical background

Operation bundles state which operations are available. Exact structure certificates state which equations are assumed. With x,y,zx, y, z ranging over the carrier:

Law traitTheory tagEquations represented
AddMonoidAddMonoidTh(x+y)+z=x+(y+z)(x+y)+z = x+(y+z), 0+x=x+0=x0+x = x+0 = x
MulMonoidMulMonoidTh(xy)z=x(yz)(xy)z = x(yz), 1x=x1=x1x = x1 = x
AddGroupAddGroupThx+(−x)=(−x)+x=0x + (-x) = (-x) + x = 0
MulGroupMulGroupThinverse laws for nonzero elements
SemiringSemiringThdistributivity, additive commutativity, and zero absorption
RingRingThring laws, including additive inverses
CommRingCommRingThring laws and commutative multiplication
IntegralDomainIntegralDomainThcommutative ring laws and no zero divisors
FieldFieldTh0≠10 \ne 1, xy=yxxy = yx, inverse and division laws
OrderedFieldOrderedFieldThordered-field laws

The operation bundle graph describes available methods only. Kernel-owned weakening functions expose proven implications between law tags, such as FieldTh -> RingTh and RingTh -> SemiringTh; there is no RingTh -> CommRingTh rule. A generic bound on operations does not imply any exact equations.

A homomorphism φ:A→B\varphi : A \to B of such structures preserves every operation of the signature:

φ(0)=0,φ(1)=1,φ(x+y)=φ(x)+φ(y),φ(xy)=φ(x)φ(y).\varphi(0) = 0, \quad \varphi(1) = 1, \quad \varphi(x + y) = \varphi(x) + \varphi(y), \quad \varphi(xy) = \varphi(x)\varphi(y).

Integers as quotients of ℤ

This section gives the mathematics behind FromNat, FromInteger, Integral and lift_to. The core tutorial shows how to use them without it.

The canonical map is unique

For every ring R there is exactly one ring homomorphism ℤ -> R. A ring homomorphism φ must send 1 to 1, and additivity then forces φ(n) = 1 + ... + 1 (n times) for n > 0 and φ(-n) = -φ(n). Conversely, n ↦ n·1 preserves + and * by distributivity. The same argument gives exactly one semiring homomorphism ℕ -> R for every semiring R. In categorical terms ℕ and ℤ are initial objects.

The derivation in full, first for ℕ. Let RR be a semiring and define φ:N→R\varphi : \mathbb{N} \to R by recursion:

φ(0)=0,φ(n+1)=φ(n)+1.\varphi(0) = 0, \qquad \varphi(n + 1) = \varphi(n) + 1 .

Additivity, by induction on nn (associativity of ++ in RR):

φ(m+0)=φ(m)=φ(m)+φ(0),φ(m+(n+1))=φ((m+n)+1)=φ(m+n)+1=(φ(m)+φ(n))+1=φ(m)+φ(n+1).\begin{aligned} \varphi(m + 0) &= \varphi(m) = \varphi(m) + \varphi(0), \\ \varphi(m + (n+1)) &= \varphi((m + n) + 1) = \varphi(m + n) + 1 \\ &= (\varphi(m) + \varphi(n)) + 1 = \varphi(m) + \varphi(n + 1). \end{aligned}

Multiplicativity, by induction on nn (absorption x⋅0=0x \cdot 0 = 0, additivity, distributivity):

φ(m⋅0)=0=φ(m)⋅0=φ(m)φ(0),φ(m(n+1))=φ(mn+m)=φ(m)φ(n)+φ(m)=φ(m)(φ(n)+1)=φ(m)φ(n+1).\begin{aligned} \varphi(m \cdot 0) &= 0 = \varphi(m) \cdot 0 = \varphi(m)\varphi(0), \\ \varphi(m(n+1)) &= \varphi(mn + m) = \varphi(m)\varphi(n) + \varphi(m) \\ &= \varphi(m)(\varphi(n) + 1) = \varphi(m)\varphi(n + 1). \end{aligned}

Uniqueness: any semiring homomorphism ψ\psi satisfies ψ(0)=0\psi(0) = 0 and ψ(n+1)=ψ(n)+ψ(1)=ψ(n)+1\psi(n + 1) = \psi(n) + \psi(1) = \psi(n) + 1, the same recursion, so ψ=φ\psi = \varphi by induction.

For ℤ, let RR be a ring. Every integer is a difference a−ba - b of naturals; put φ^(a−b)=φ(a)−φ(b)\hat\varphi(a - b) = \varphi(a) - \varphi(b). This is well defined, because a−b=c−da - b = c - d means a+d=b+ca + d = b + c in ℕ, hence φ(a)+φ(d)=φ(b)+φ(c)\varphi(a) + \varphi(d) = \varphi(b) + \varphi(c) in RR, and adding −φ(b)−φ(d)-\varphi(b) - \varphi(d) to both sides (with ++ commutative) gives φ(a)−φ(b)=φ(c)−φ(d)\varphi(a) - \varphi(b) = \varphi(c) - \varphi(d). It is additive term by term, and multiplicative by distributivity:

φ^((a−b)(c−d))=φ^((ac+bd)−(ad+bc))=φ(a)φ(c)+φ(b)φ(d)−φ(a)φ(d)−φ(b)φ(c)=(φ(a)−φ(b))(φ(c)−φ(d)).\begin{aligned} \hat\varphi((a - b)(c - d)) &= \hat\varphi((ac + bd) - (ad + bc)) \\ &= \varphi(a)\varphi(c) + \varphi(b)\varphi(d) - \varphi(a)\varphi(d) - \varphi(b)\varphi(c) \\ &= (\varphi(a) - \varphi(b))(\varphi(c) - \varphi(d)). \end{aligned}

A ring homomorphism must also satisfy ψ(−n)=−ψ(n)\psi(-n) = -\psi(n), so it agrees with φ^\hat\varphi on negatives too, and φ^\hat\varphi is unique.11 In the language of category theory, ℕ is the initial object of the category of semirings and ℤ that of rings. ℤ is the Grothendieck group of the additive monoid ℕ, which is the construction used above.

Because the mathematical map depends only on R, its operation can use a single-parameter trait: FromNat::from_natural and FromInteger::from_integer. These conversion operations do not themselves carry the exact homomorphism law; a strict certificate needs an explicit target-bound law premise.

Fixed-width integers

Int adds and multiplies modulo 2^32, so as a ring it is ℤ/2^32. Its from_integer is the reduction ℤ -> ℤ/2^32, which is surjective. normalize goes the other way and picks one integer in every residue class, the one in [-2^31, 2^31). The law from_integer(normalize(x)) == x says exactly that normalize is a section of the reduction:

  • normalize is injective, because it has a left inverse.
  • Its image contains exactly one element of every class: at most one because it is injective, and at least one because of the law.
  • It is not a homomorphism: normalize(2147483647 + 1) = -2^31, while normalize(2147483647) + normalize(1) = 2^31.

In symbols, with m=2km = 2^k and π:Z→Z/m\pi : \mathbb{Z} \to \mathbb{Z}/m the reduction, whose kernel is mZm\mathbb{Z}, the shipped instances use

ssigned(π(n))=((n+2k−1) mod 2k)−2k−1,sunsigned(π(n))=n mod 2k,s_{\text{signed}}(\pi(n)) = \bigl((n + 2^{k-1}) \bmod 2^k\bigr) - 2^{k-1}, \qquad s_{\text{unsigned}}(\pi(n)) = n \bmod 2^k,

with  mod \bmod taking values in [0,2k)[0, 2^k). Both are well defined, since nn and n+jmn + jm give the same value, and both satisfy π(s(x))=x\pi(s(x)) = x, since each differs from nn by a multiple of mm. The two injectivity steps above, written out:

s(x)=s(y)  ⟹  x=π(s(x))=π(s(y))=y.s(x) = s(y) \;\Longrightarrow\; x = \pi(s(x)) = \pi(s(y)) = y .

The failure of additivity is a multiple of the modulus:

s(x)+s(y)−s(x+y)∈mZ,becauseπ(s(x)+s(y))=x+y=π(s(x+y)).s(x) + s(y) - s(x + y) \in m\mathbb{Z}, \qquad\text{because}\qquad \pi\bigl(s(x) + s(y)\bigr) = x + y = \pi\bigl(s(x + y)\bigr).

For x=231−1x = 2^{31} - 1 and y=1y = 1 on Int the difference is 2322^{32}, so no choice of representatives can repair it: ss is a homomorphism only when m=0m = 0, that is for BigInt.

The hom design shows what a section does preserve.

When a conversion is a homomorphism

lift_to : S -> R is R::from_integer composed with S::normalize. When S is ℤ/m (with m = 0 for BigInt), a ring homomorphism ℤ/m -> R exists if and only if m·1 = 0 in R:

  • If ψ is one, then 0 = ψ(0) = ψ(m·1) = m·1 in R.
  • If m·1 = 0, the canonical map ℤ -> R sends mℤ to 0 and so factors through ℤ/m. It is unique because ℤ -> ℤ/m is surjective.

In that case lift_to is this homomorphism: write the canonical map ℤ -> R as ψ ∘ π with π : ℤ -> ℤ/m; then lift_to = ψ ∘ π ∘ normalize = ψ. So:

  • Int64 -> Int is a homomorphism, because 2^64 ≡ 0 (mod 2^32).
  • Int -> Int64 is not, because 2^32 ≢ 0 (mod 2^64).
  • No fixed-width integer maps homomorphically into BigInt, Float or Double, where m·1 ≠ 0 for every m > 0.

The same argument as one chain, with ιR:Z→R\iota_R : \mathbb{Z} \to R the canonical map and ιR=ψ∘π\iota_R = \psi \circ \pi when m⋅1R=0m \cdot 1_R = 0:

lift_to=ιR∘s=ψ∘π∘s=ψ∘idZ/m=ψ.\texttt{lift\_to} = \iota_R \circ s = \psi \circ \pi \circ s = \psi \circ \mathrm{id}_{\mathbb{Z}/m} = \psi .

For the two modulus examples, 264⋅1=232⋅(232⋅1)=232⋅0=02^{64} \cdot 1 = 2^{32} \cdot (2^{32} \cdot 1) = 2^{32} \cdot 0 = 0 in ℤ/2^32, while 232⋅1≠02^{32} \cdot 1 \neq 0 in ℤ/2^64 because 0<232<2640 < 2^{32} < 2^{64}.

Why unsigned types stop at Semiring

UInt, UInt16 and UInt64 are read as natural numbers that wrap: Nat promises non-negative representatives, and code over them treats values as counts and sizes. ℕ has no additive inverses, so it is a semiring and not a ring:

1+x=0 in N  ⟹  0=1+x≥1,1 + x = 0 \text{ in } \mathbb{N} \;\Longrightarrow\; 0 = 1 + x \geq 1,

a contradiction. As an abstract ring the unsigned type is ℤ/2^k, which does have negatives, −x=2k−x-x = 2^k - x. Exposing them would make -1 silently mean 2k−12^k - 1 in generic code written for rings, which is the confusion the Nat reading avoids. The core library also provides no Neg for unsigned types, and MoonBit does not let this package add an instance of a foreign trait to a foreign type, so AddGroup could not be implemented without a wrapper type anyway. The canonical map is still total: from_integer(-1) on UInt is 232−12^{32} - 1, the image of −1-1 in ℤ/2^32.

Why the traits do not refer to each other

Every conversion factors through ℤ: S -> ℤ -> R. The first half depends only on S (Integral), the second only on R (FromInteger), so neither trait has to relate two types. FromInteger takes a BigInt rather than a generic integral source, which lets Integral extend it without a cycle.

Fields and division rings

Field is Ring + Inverse + Div with commutative multiplication. The commutativity is part of the contract even though no method states it.

Definitions

A division ring is a ring with 1≠01 \neq 0 in which every a≠0a \neq 0 has a two-sided inverse, aa−1=a−1a=1a a^{-1} = a^{-1} a = 1. A field is a division ring whose multiplication commutes, ab=baab = ba. The two differ only in that law, and the method signatures of Ring + Inverse + Div cannot tell them apart.

The standard example of a division ring that is not a field is Hamilton’s quaternions ℍ, with basis 1,i,j,k1, i, j, k and i2=j2=k2=ijk=−1i^2 = j^2 = k^2 = ijk = -1. From ijk=−1ijk = -1, multiplying on the right by kk gives ij⋅k2=−kij \cdot k^2 = -k, so ij=kij = k; similarly ji=−kji = -k:

ij=k,ji=−k,ij≠ji.ij = k, \qquad ji = -k, \qquad ij \neq ji .

What breaks without commutativity

In any division ring the inverse of a product reverses the order:

(ab)(b−1a−1)=a(bb−1)a−1=aa−1=1,so(ab)−1=b−1a−1.(ab)(b^{-1}a^{-1}) = a(bb^{-1})a^{-1} = a a^{-1} = 1, \qquad\text{so}\qquad (ab)^{-1} = b^{-1}a^{-1}.

The other order is the inverse of the other product, a−1b−1=(ba)−1a^{-1}b^{-1} = (ba)^{-1}, and inversion is injective, so

(ab)−1=a−1b−1  ⟺  (ab)−1=(ba)−1  ⟺  ab=ba.(ab)^{-1} = a^{-1}b^{-1} \iff (ab)^{-1} = (ba)^{-1} \iff ab = ba .

In ℍ, (ij)−1=k−1=−k(ij)^{-1} = k^{-1} = -k, while i−1j−1=(−i)(−j)=ij=ki^{-1} j^{-1} = (-i)(-j) = ij = k. Division is ambiguous in the same way: ab−1a b^{-1} and b−1ab^{-1} a are different elements in general, and Div provides only one of them.

Generic code bounded by F : Field may rely on ab=baab = ba: rewrite (ab)−1(ab)^{-1} as a−1b−1a^{-1}b^{-1}, compute a/b⋅ca/b \cdot c as ac/bac/b, or reorder products to save work. A non-commutative type that implemented Field would compile and then get wrong answers from such code. That is why the trait states commutativity, and why a division ring that is not a field must not implement it. Generic code that also works for division rings asks for Ring + Inverse + Div and keeps the order of its factors.

Every finite division ring is commutative,22 Wedderburn’s little theorem (1905): a finite division ring is a field. Over the reals, Frobenius’ theorem (1877) adds that the only finite-dimensional associative division algebras are ℝ, ℂ and ℍ. so the distinction only arises for infinite types.

Floating-point operation instances

Float and Double implement field operations and the RoundedArithmetic marker. They have no exact FieldTh certificate: associativity and distributivity hold only approximately, and 0 has no inverse. Their inv operation aborts on zero.

Rounding ℤ into Float and Double

A binary floating-point format with precision pp and maximum exponent emax⁡e_{\max} has the positive finite values

q⋅2e−p+1,2p−1≤q<2p,e≤emax⁡,q \cdot 2^{e - p + 1}, \qquad 2^{p-1} \le q < 2^p, \quad e \le e_{\max},

plus subnormals, which no non-zero integer needs because e≥0e \ge 0 for ∣n∣≥1|n| \ge 1. Double has p=53p = 53, emax⁡=1023e_{\max} = 1023; Float has p=24p = 24, emax⁡=127e_{\max} = 127. No map ℤ → Double is a ring homomorphism, since 253+12^{53} + 1 is not representable, so from_integer is the next best thing: the IEEE 754-2019 conversion convertFromInt (§5.4.1) with the default rounding attribute roundTiesToEven (§4.3.1). Write RNp\mathrm{RN}_p for it. It is odd, RNp(−n)=−RNp(n)\mathrm{RN}_p(-n) = -\mathrm{RN}_p(n), monotone, exact for ∣n∣≤2p|n| \le 2^p, and its relative error is at most 2−p2^{-p} while the result is finite.

The rounding step. Let m=∣n∣>0m = |n| > 0 with 2nb−1≤m<2nb2^{n_b - 1} \le m < 2^{n_b}, so nbn_b is the bit length of mm, and set s=nb−p−1s = n_b - p - 1. When s≥0s \ge 0, divide with remainder,

m=t⋅2s+r,0≤r<2s;m = t \cdot 2^s + r, \qquad 0 \le r < 2^s;

when s<0s < 0, put t=m⋅2−st = m \cdot 2^{-s} and r=0r = 0. Either way 2p≤t<2p+12^p \le t < 2^{p+1}: tt holds exactly the top p+1p + 1 bits of mm. Split off its last bit, t=2q+bt = 2q + b with b∈{0,1}b \in \{0, 1\}, and

m=(q+f)⋅2s+1,f=b2+r2s+1,0≤f<1.m = (q + f) \cdot 2^{s+1}, \qquad f = \frac{b}{2} + \frac{r}{2^{s+1}}, \qquad 0 \le f < 1 .

The two neighbours of mm with pp significant bits are q⋅2s+1q \cdot 2^{s+1} and (q+1)⋅2s+1(q + 1) \cdot 2^{s+1}, and round-to-nearest-even takes the upper one exactly when f>12f > \tfrac12, or f=12f = \tfrac12 and qq is odd. Because r/2s+1<12r / 2^{s+1} < \tfrac12,

f>12  ⟺  b=1∧r≠0,f=12  ⟺  b=1∧r=0,f > \tfrac12 \iff b = 1 \wedge r \neq 0, \qquad f = \tfrac12 \iff b = 1 \wedge r = 0,

so the rule needs only the round bit bb, the sticky bit [r≠0][r \neq 0] and the parity of qq:

RNp(m)={(q+1)⋅2s+1if b=1 and (r≠0 or q odd),q⋅2s+1otherwise.\mathrm{RN}_p(m) = \begin{cases} (q + 1) \cdot 2^{s+1} & \text{if } b = 1 \text{ and } (r \ne 0 \text{ or } q \text{ odd}), \\ q \cdot 2^{s+1} & \text{otherwise.} \end{cases}

The exponent is e=(p−1)+(s+1)=nb−1e = (p - 1) + (s + 1) = n_b - 1. If rounding up carries, q+1=2pq + 1 = 2^p, the value is 2p−1⋅2s+22^{p-1} \cdot 2^{s+2} and e=nbe = n_b. The implementation computes tt as m >> s, the sticky bit as (t << s) != m, and assembles the result from its bit pattern: sign, biased exponent e+emax⁡e + e_{\max}, and qq without its leading bit. It uses only bit_length, shifts, to_uint64 and comparisons of BigInt, which behave identically on every backend, and it runs in time linear in the length of nn.

Overflow. The largest finite value is

Ω=(2p−1)⋅2emax⁡−p+1=2emax⁡+1−2emax⁡−p+1.\Omega = (2^p - 1) \cdot 2^{e_{\max} - p + 1} = 2^{e_{\max} + 1} - 2^{e_{\max} - p + 1}.

Its significand 2p−12^p - 1 is odd, so the midpoint between Ω\Omega and 2emax⁡+12^{e_{\max}+1}, namely 2emax⁡+1−2emax⁡−p2^{e_{\max}+1} - 2^{e_{\max}-p}, rounds up to 2emax⁡+12^{e_{\max}+1}, which does not fit. Following §7.4, an overflowing result under roundTiesToEven is ±∞\pm\infty with the sign of nn:

RNp(n)=±∞  ⟺  ∣n∣≥2emax⁡+1−2emax⁡−p,\mathrm{RN}_p(n) = \pm\infty \iff |n| \ge 2^{e_{\max}+1} - 2^{e_{\max}-p},

that is ∣n∣≥21024−2970|n| \ge 2^{1024} - 2^{970} for Double and ∣n∣≥2128−2103|n| \ge 2^{128} - 2^{103} for Float. The integer just below each threshold still rounds to Ω\Omega.

Why Float is rounded once. Rounding to Double and then to Float is not RN24\mathrm{RN}_{24}. Take n=253+229+1n = 2^{53} + 2^{29} + 1. At 53 bits the spacing is 22, so nn is halfway between 253+2292^{53} + 2^{29} and 253+229+22^{53} + 2^{29} + 2, and the tie goes to the even significand, 253+2292^{53} + 2^{29}. That value is exactly halfway between the Float neighbours 2532^{53} and 253+2302^{53} + 2^{30}, and the second rounding goes to the even one, 2532^{53}. But nn itself lies above that midpoint, so RN24(n)=253+230\mathrm{RN}_{24}(n) = 2^{53} + 2^{30}. Float therefore runs the same rounding step with p=24p = 24 directly.

Why not through a decimal string. Earlier versions printed the BigInt in decimal and parsed the result as a Double. That is correctly rounded in range, but the parser rejects values at or above the overflow threshold, so the conversion aborted instead of returning ±∞\pm\infty; the decimal conversion costs time quadratic in the length on some backends; and Float was rounded twice.

Alternatives rejected

  • A two-parameter conversion trait Into[S, R]: MoonBit traits have only Self, and the factorization through ℤ makes it unnecessary.
  • One broad “number” trait: it would hide the difference between ℤ, ℤ/2^k, approximate reals and fields, which are exactly the differences generic code has to respect.
  • NatHomomorphism and IntegralHomomorphism as the conversion interface: they promised a homomorphism that a fixed-width source cannot give. They remain only as deprecated traits.
  • A separate division-ring trait: no shipped type needs it, and code that must work without commutativity can ask for Ring + Inverse + Div.

Boundaries

  • This package does not define matrices, complex numbers, polynomials, parsing, or numerical algorithms.
  • It does not erase the semantic difference between exact and approximate number systems.
  • It does not check laws at compile time. The laws of every structural trait, including the commutativity of Field, are contracts on the implementor, tested with the tools of the hom API.
  • It does not model an arbitrary-precision ℕ: such a type is not a quotient of ℤ, so it cannot be Integral.

Footnotes

  1. In the language of category theory, ℕ is the initial object of the category of semirings and ℤ that of rings. ℤ is the Grothendieck group of the additive monoid ℕ, which is the construction used above. ↩

  2. Wedderburn’s little theorem (1905): a finite division ring is a field. Over the reals, Frobenius’ theorem (1877) adds that the only finite-dimensional associative division algebras are ℝ, ℂ and ℍ. ↩