Arithmetic capability traits for Luna projects.
This documentation tracks the current v0.2.1 package baseline.
luna-genericexpresses algebraic structure such asRing,Field, andNum.arithmeticexpresses analytic and elementary-function capabilities such assqrt,exp,log, and trigonometric functions.- The package intentionally describes capabilities rather than declaring “real number” abstractions.
- Packages such as
floatingand future packages such ascomputable-realshould implement these traits where the semantics make sense, rather than inheriting a premature bundledRealnotion from this package. - The floating-point instances directly call
Kaida-Amethyst/math.
- Atomic traits:
SqrtCbrtRadicalExponentialLogarithmicPowerTrigonometricInverseTrigonometricHyperbolicInverseHyperbolicConstants
- Checked/contextual shared types:
FpClassRoundingModeArithmeticContextArithmeticErrorKindArithmeticError
- Checked/contextual traits:
SqrtCheckedDivCheckedCompareCheckedPowNatCheckedPowIntCheckedParseChecked
- Enclosure traits:
ContainsOverlapsDefinitelyLtDefinitelyLeMaybeEq
- Default instances in the root package:
FloatDoubleBigIntIntInt16Int64UIntUInt16UInt64
- The package documents callable capabilities, not a unified semantic model for all numeric domains.
- Each trait preserves the concrete
Selftype instead of introducing widening or wrapper return types. - Domain validity remains part of the caller contract unless a specific instance documents stronger guarantees.
- Floating-point instances inherit special-value behavior, branch choices, and principal-value conventions from
Kaida-Amethyst/math. - Integer-family instances are intentionally narrower: this package only implements capabilities that stay meaningfully closed on those concrete types.
- Unchecked/context-free elementary traits remain available for native scalars and other backends where direct IEEE-style behavior is acceptable.
- Checked/contextual traits use
Result[Self, ArithmeticError]plusArithmeticContextto express validated, precision-aware, or error-aware arithmetic boundaries. - Enclosure traits describe containment/overlap style relations without pretending enclosure values are totally ordered scalars.
src/impl_float.mbtandsrc/impl_double.mbtdirectly bind the trait surface toKaida-Amethyst/math.src/impl_bigint.mbt,src/impl_signed_ints.mbt, andsrc/impl_unsigned_ints.mbtprovide exact integer-family impls only for the capabilities that stay closed on those types.- Algebraic structure still lives in
luna-generic; this package only binds function capabilities to concrete numeric backends. Constantsonly states that the givenSelfexposes the standard scalar constants used by the package.
Power::powuses one shared signature across floating and integer families.- For
FloatandDouble,powfollows the backend floating-point semantics fromKaida-Amethyst/math. - For
BigInt,Int,Int16, andInt64, the exponent must be non-negative. - Supplying a negative exponent to those signed integer-family instances aborts at runtime, because the result would not remain closed in the same integer type.
UInt,UInt16, andUInt64are already non-negative by construction, so theirpowsurface has no extra signed-exponent check.- This package exposes the capability surface and its preconditions; selecting arguments that satisfy the mathematical domain is still partly the caller's responsibility.
PowNatCheckedandPowIntCheckedsplit integer-exponent power from general uncheckedpow(Self, Self).- In this first checked-power layer,
x^0returns one, including0^0. PowIntCheckedrequires reciprocal/division semantics for negative exponents and must not silently accept zero base with a negative exponent.
- Use the narrowest trait that matches an algorithm.
- Prefer direct capability constraints such as
Sqrt,Trigonometric, orLogarithmicinstead of semantic bundle traits. Powerremains intentionally undivided in this release, even though integer and floating use cases may be split more carefully later.- This package does not define “realness”; it defines callable capability surfaces.
- When an algorithm may produce out-of-domain arguments, validate them at the application layer before calling the trait method.
- For high-precision, validated, or context-sensitive numeric types, prefer the checked/contextual traits over the unchecked elementary traits.
arithmeticis a capability-boundary package; it does not add calculus, matrices, complex numbers, symbolic algebra, or special-function layers.- Packages such as
floatingimplement these capability boundaries for concrete numeric representations. floatingnow targets the published checked capability boundary from this package rather than carrying its own ecosystem-level arithmetic error/context types.
using @lg { trait Add, trait Mul }
fn hypot2[T : Add + Mul + Sqrt](x : T, y : T) -> T {
Sqrt::sqrt(x * x + y * y)
}
fn polar_x[T : Mul + Trigonometric](r : T, theta : T) -> T {
r * Trigonometric::cos(theta)
}
inspect(hypot2(3.0, 4.0), content="5")
inspect(
polar_x(2.0, Constants::pi() / 3.0),
content="1.0000000000000002",
)We provide README-level documentation in multiple languages:
- English: doc/en_US/README.md
- Simplified Chinese: doc/zh_CN/README.md
- Japanese: doc/ja_JP/README.md
Useful local commands:
moon check
moon test