moonlight-core: Mathematical basis for Pale Meridian.

[ library, math, mit ] [ Propose Tags ] [ Report a vulnerability ]

A total vocabulary of numeric classes, structural identity, orders, patterns, fixpoints, union-find, finite registries, and host-neutral e-graph programs.


[Skip to Readme]

library moonlight-core

Modules

[Index] [Quick Jump]

library moonlight-core:moonlight-core-automata

Modules

[Index] [Quick Jump]

  • Moonlight
    • Automata
      • Pure
        • Moonlight.Automata.Pure.Algebra
        • Moonlight.Automata.Pure.Coalgebra
        • Moonlight.Automata.Pure.Core
        • Moonlight.Automata.Pure.Transducer
    • Core
      • Pattern
        • Moonlight.Core.Pattern.Automata
        • Moonlight.Core.Pattern.Kernel

library moonlight-core:moonlight-core-syntax

Modules

[Index] [Quick Jump]

  • Moonlight
    • Core
      • Fix
        • Moonlight.Core.Fix.Order
      • Moonlight.Core.Guidance
      • Moonlight.Core.Language
      • Moonlight.Core.Pattern
        • Moonlight.Core.Pattern.AntiUnify
      • Site
        • Moonlight.Core.Site.Program
      • Moonlight.Core.Substitution
      • Moonlight.Core.Theory

library moonlight-core:moonlight-core-egraph-program

Modules

[Index] [Quick Jump]

  • Moonlight
    • Core
      • EGraph
        • Moonlight.Core.EGraph.Program

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Change log CHANGELOG.md
Dependencies base (>=4.22 && <5), bytestring (>=0.11 && <0.13), comonad (>=5 && <6), containers (>=0.8 && <0.9), data-fix (>=0.3 && <0.4), deepseq (>=1.4 && <1.6), free (>=5.2 && <6), memory (>=0.18 && <0.19), moonlight-core, primitive (>=0.7 && <0.10), recursion-schemes (>=5.2 && <6), text (>=2.0 && <2.2), these (>=1.2 && <1.3), transformers (>=0.6 && <1), vector (>=0.13 && <0.14) [details]
Tested with ghc ==9.14.1
License MIT
Author Blue Rose
Maintainer rosaliafialkova@gmail.com
Uploaded by bluerose at 2026-07-20T11:03:46Z
Category Math
Home page https://github.com/PaleRoses/moonlight
Bug tracker https://github.com/PaleRoses/moonlight/issues
Source repo head: git clone https://github.com/PaleRoses/moonlight.git(moonlight-core)
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 2 total (2 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user [build log]
All reported builds failed as of 2026-07-20 [all 2 reports]

Readme for moonlight-core-0.1.0.0

[back to package description]

moonlight-core

Part of Moonlight, the sheaf-theoretic computation layer beneath Melusine and Pale Meridian.

moonlight-core is Moonlight's foundation tier. It is the total vocabulary every layer above shares: numeric classes, structural identity, orders, patterns, fixpoints, persistent union-find, finite registries, a term database, and a host-neutral e-graph program algebra.

Main idea

Every name reachable from the one import, Moonlight.Core, is total. Failure is a value in the return type: tryInv returns Maybe, makeSet returns Either UnionFindAllocationError, fixpointBounded returns Either (FixpointDivergence a), mkTotalRegistry returns Either [missingKey]. Operations with no failure case (magnitude, neg, union) return bare values.

Moonlight.Core.Unsound holds the unchecked constructors: importing it by name asserts a refinement the checker cannot see. Every other constructor reachable through the umbrella validates at construction.

Use

import Moonlight.Core

reciprocalOfThree :: Maybe Double
reciprocalOfThree = tryInv 3

size :: Double
size = magnitude (neg (2.5 :: Double))

Cookbook

Each recipe shows the main idea in practice: the total path returns a value, the failure path returns a typed sum.

Persistent union-find

makeSet mints fresh classes through a checked allocation boundary, union merges them, and the structure is persistent; older versions stay valid after a merge.

import Moonlight.Core

partition :: Either UnionFindAllocationError (Bool, Bool)
partition = do
  (a, uf1) <- makeSet emptyUnionFind
  (b, uf2) <- makeSet uf1
  (c, uf3) <- makeSet uf2
  let uf = union a b uf3
  pure (equivalent a b uf, equivalent a c uf)   -- Right (True, False)

Bounded fixpoints

fixpointBounded iterates a step to a fixed point under an explicit fuel bound. Non-convergence returns a typed Left.

import Moonlight.Core

converge :: Either (FixpointDivergence Int) Int
converge = fixpointBounded 100 (\n -> n `div` 2) 37   -- Right 0

diverges :: Either (FixpointDivergence Int) Int
diverges = fixpointBounded 5 (+1) 0                   -- Left (out of fuel)

Checked total maps

mkTotalRegistry demands every key of a finite universe; a gap is a typed Left [missingKeys]. In exchange, lookupTotal returns a bare value once the registry is constructed.

import Moonlight.Core
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.Map.Strict as Map

data Slot = Alpha | Beta | Gamma
  deriving (Eq, Ord, Show)

instance FiniteUniverse Slot where
  finiteUniverse = Alpha :| [Beta, Gamma]

palette :: Either [Slot] (TotalRegistry Slot String)
palette = mkTotalRegistry (Map.fromList [(Alpha, "a"), (Beta, "b"), (Gamma, "c")])

labelOfBeta :: Either [Slot] String
labelOfBeta = fmap (\reg -> lookupTotal reg Beta) palette   -- Right "b"
-- mkTotalRegistry (Map.fromList [(Alpha, "a")])  ==  Left [Beta, Gamma]

The matching seam

ZipMatch is one-layer structural matching: zipMatch succeeds iff two nodes share a shape, pairing their children positionally. It is the seam the e-graph and rewriting layers are built on.

import Moonlight.Core

data ExprF a = Lit Int | Add a a
  deriving (Functor, Foldable, Traversable, Eq, Ord, Show)

instance ZipMatch ExprF where
  zipMatch (Lit m)     (Lit n)     | m == n = Just (Lit m)
  zipMatch (Add a1 b1) (Add a2 b2)          = Just (Add (a1, a2) (b1, b2))
  zipMatch _           _                    = Nothing

aligned :: Maybe (ExprF (Int, Int))
aligned = zipMatch (Add 1 2) (Add 10 20)   -- Just (Add (1,10) (2,20))

Surface & boundaries

Most downstream code imports the umbrella. Lower-level packages may depend on one of the public sublibraries.

Cabal dependency Import What you get
moonlight-core Moonlight.Core The ordinary total vocabulary. This is the default.
moonlight-core:moonlight-core-syntax Moonlight.Core.Pattern.AntiUnify Patterns and term anti-unification without the full umbrella.
moonlight-core:moonlight-core-automata Moonlight.Core.Pattern.Automata or .Kernel The bottom-up automata substrate and compiled matcher. Depend on syntax too when naming its types directly.
moonlight-core:moonlight-core-egraph-program Moonlight.Core.EGraph.Program The host-neutral e-graph program algebra without the rest of the foundation.
moonlight-core Moonlight.Core.Unsound The explicit trust boundary; see Main idea.

The private implementation slices remain basis, numeric, solver, and term. Syntax, automata, and e-graph programs are public because other foundation packages consume those exact owners. The umbrella re-exports the syntax and e-graph-program vocabulary; automata are imported only through the sublibrary. Haddock carries the signatures; the laws are named and machine-checked in the law suites.

Test

cabal test moonlight-core:moonlight-core-test

Benchmarks

cabal bench moonlight-core:moonlight-core-bench --benchmark-options='--timeout=2s --csv=comparison.csv'

hackage: rows measure a package-level baseline (containers, memory, scientific, equivalence) on the same fixture; world: rows are source-equivalent baselines where no package owner exists. Fixtures assert result agreement with the baseline before timing. Dated receipts live in docs/BENCHMARKS-m4-pro.md.

License

MIT. See LICENSE.