luna-generic

Algebraic traits and default numeric instances that define the generic foundation for LunaFlow math packages.

math
algebra
interface
moon add Luna-Flow/luna-generic@0.3.3
Download zip
Author
Version
0.3.3
License
Apache-2.0
Last updated
2 months ago
Downloads
5K
README

#Luna-Generic

General algebraic traits and default numeric instances for Luna projects.

#Exports

  • Traits: AddMonoid, MulMonoid, AddGroup, MulGroup, Semiring, Ring, Field, Integral, Nat, Num
  • Operations: One, Zero, Inverse, Conjugate
  • Default numeric types: Int, Int16, Int64, UInt, UInt16, UInt64, Float, Double

#Integer Families

  • Nat covers pure unsigned integer types: UInt, UInt16, UInt64
  • Integral covers signed and unsigned integers: Int, Int16, Int64, UInt, UInt16, UInt64
  • Byte is intentionally excluded from both traits

Unsigned integer instances stop at Semiring. They do not implement AddGroup, Ring, or Num.

#Floating-Point Embeddings

Integral::to_float and Integral::to_double provide embeddings into floating-point types.

  • Float preserves integer values and semiring structure only within its exact integer range, i.e. absolute values up to 2^24
  • Double preserves integer values and semiring structure only within its exact integer range, i.e. absolute values up to 2^53

#Conversion Safety

Here "absolutely safe" means every value of the source integral type is represented exactly in the target floating-point type, so small algebraic laws over integer values remain intact after conversion.

#Integral::to_float

Absolutely safe:

  • Int16 -> Float
  • UInt16 -> Float

Risky, user must pay attention:

  • Int -> Float
  • UInt -> Float
  • Int64 -> Float
  • UInt64 -> Float

For these risky conversions, values outside the exact integer range [-2^24, 2^24] may be rounded. The result is still finite for these integral types, but it may no longer preserve exact integer equality, addition, or multiplication.

#Integral::to_double

Absolutely safe:

  • Int16 -> Double
  • UInt16 -> Double
  • Int -> Double
  • UInt -> Double

Risky, user must pay attention:

  • Int64 -> Double
  • UInt64 -> Double

For these risky conversions, values outside the exact integer range [-2^53, 2^53] may be rounded. The result is still finite for these integral types, but it may no longer preserve exact integer equality, addition, or multiplication.

If exactness matters, keep computations in the integral domain until the final step, or explicitly restrict inputs to the exact range of the target floating-point type.

#Testing

Run moon check to validate trait impls and package metadata.

#
AddGroup

pub(open) trait AddGroup : AddMonoid + Neg + Sub {
}

impl AddGroup for Int
impl AddGroup for Int16
impl AddGroup for Int64
impl AddGroup for Float
impl AddGroup for Double

#
AddMonoid

pub(open) trait AddMonoid : Add + Zero {
}

impl AddMonoid for Int
impl AddMonoid for Int16
impl AddMonoid for Int64
impl AddMonoid for UInt
impl AddMonoid for UInt16
impl AddMonoid for UInt64
impl AddMonoid for Float
impl AddMonoid for Double

#
Conjugate

pub(open) trait Conjugate {
fn conjugate(Self) -> Self
}

#
Field

pub(open) trait Field : Ring + Inverse + Div {
}

impl Field for Float
impl Field for Double

#
Integral

pub(open) trait Integral : Semiring {
fn to_float(Self) -> Float
fn to_double(Self) -> Double
}

impl Integral for Int
impl Integral for Int16
impl Integral for Int64
impl Integral for UInt
impl Integral for UInt16
impl Integral for UInt64

#
Inverse

pub(open) trait Inverse {
fn inv(Self) -> Self
}

impl Inverse for Float
impl Inverse for Double

#
MulGroup

pub(open) trait MulGroup : MulMonoid + Inverse + Div {
}

impl MulGroup for Float
impl MulGroup for Double

#
MulMonoid

pub(open) trait MulMonoid : Mul + One {
}

impl MulMonoid for Int
impl MulMonoid for Int16
impl MulMonoid for Int64
impl MulMonoid for UInt
impl MulMonoid for UInt16
impl MulMonoid for UInt64
impl MulMonoid for Float
impl MulMonoid for Double

#
Nat

pub(open) trait Nat : Integral {
}

impl Nat for UInt
impl Nat for UInt16
impl Nat for UInt64

#
Num

pub(open) trait Num : Ring {
fn abs(Self) -> Self
fn signum(Self) -> Self
}

impl Num for Int
impl Num for Int16
impl Num for Int64
impl Num for Float
impl Num for Double

#
One

pub(open) trait One {
fn one() -> Self
}

impl One for Int
impl One for Int16
impl One for Int64
impl One for UInt
impl One for UInt16
impl One for UInt64
impl One for Float
impl One for Double

#
Ring

pub(open) trait Ring : Semiring + Neg + Sub {
}

impl Ring for Int
impl Ring for Int16
impl Ring for Int64
impl Ring for Float
impl Ring for Double

#
Semiring

pub(open) trait Semiring : AddMonoid + MulMonoid {
}

impl Semiring for Int
impl Semiring for Int16
impl Semiring for Int64
impl Semiring for UInt
impl Semiring for UInt16
impl Semiring for UInt64
impl Semiring for Float
impl Semiring for Double

#
Zero

pub(open) trait Zero {
fn zero() -> Self
}

impl Zero for Int
impl Zero for Int16
impl Zero for Int64
impl Zero for UInt
impl Zero for UInt16
impl Zero for UInt64
impl Zero for Float
impl Zero for Double

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io