exchangealgebra-0.7.0.1: src/ExchangeAlgebra/Algebra/Core/Representation.hs
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
-- | Generic exchange algebra representation and operations.
module ExchangeAlgebra.Algebra.Core.Representation
( Redundant(..)
, Pair( HatOne
, NotOne
, PairSeq
, Pair
, _hatSide
, _notSide
)
, Alg(..)
, pattern (:@)
, _val
, _hatBase
, AxisPosting
, DMap
, mkPair
, pairView
, pairNull
, foldPair
, mapPair
, hatPair
, filterPair
, pairSums
, pairStates
, comparePairSides
, nullPair
, pairAppend
, pairUnion
, foldlHashMapByHash
, foldrHashMapByHash
, emptyAxisPosting
, insertAxisPosting
, queryAxisPosting
, linerFromMap
, isZero
, singleton
, (.@)
, (<@)
, union
, insert
, unions
, mergeAlgMap
, mergeAlgMapIfNonZero
, unionsMerge
, foldPairs
, netPairState
, hasNonfiniteState
, postNetStates
, vals
, bases
, fromList
, sigma
, sigma2When
, sigmaFromMap
, toList
, foldEntries
, toASCList
, map
, mapPosting
, mapMaybePosting
, filter
, mapBasePart
, extendBy
, proj
, choosePairByHat
, projExactMap
, projWildMap
, mkAlgFromMap
, singlePairToAlg
, projNetNorm
, projectedNetState
, foldProjectedNorm
, foldProjectedState
, barNormPair
, balanceBy
, balanceMapBy
, netPairMapBy
, foldEntriesToMap
, decBy
, postFromNetBy
) where
import ExchangeAlgebra.Algebra.Base.Representation
import ExchangeAlgebra.Algebra.Element.Representation (Element(..), HasElementKeys(..), ElementKey(..), CountUnit(..), ( .# ), matchesQuery)
import ExchangeAlgebra.Algebra.Value (MoneyDouble, MoneyDecimal)
import ExchangeAlgebra.Algebra.Value.Class
import qualified Data.List as L (foldl', sort, sortOn, any)
import Prelude hiding (map, head, filter, tail, traverse, mapM)
import Data.Hashable (Hashable, hash)
import qualified Data.HashMap.Strict as Map
import qualified Data.IntMap.Strict as IntMap
import qualified Data.IntSet as IntSet
import qualified Data.Map.Strict as M
import qualified Data.Foldable as Foldable (foldl', foldr)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import qualified Data.Maybe as Maybe
import Control.DeepSeq
import GHC.Stack (HasCallStack)
import GHC.Exts (oneShot)
import qualified Data.Binary as Binary
-- | State of a collision check in hash map traversal order.
data HashScan
= NoHash
| PreviousHash {-# UNPACK #-} !Int
| CollisionFound
-- | Check adjacent hashes before choosing the fold.
-- Keys with the same hash are adjacent in 'Map.HashMap' traversal order.
{-# INLINE hasHashCollision #-}
hasHashCollision :: Hashable k => Map.HashMap k v -> Bool
hasHashCollision entries = case Map.foldlWithKey' step NoHash entries of
CollisionFound -> True
_ -> False
where
step CollisionFound _ _ = CollisionFound
step NoHash key _ = PreviousHash (hash key)
step (PreviousHash previous) key _
| current == previous = CollisionFound
| otherwise = PreviousHash current
where
current = hash key
-- | The pending entry and any other entries with the same hash.
data HashRun k v
= EmptyRun -- ^ No entry has been read yet.
| HashRun !Int !k v [(k, v)] -- ^ The list is allocated only for collisions.
-- | Fold in 'Map.HashMap' traversal order, sorting keys with the same hash
-- by 'Ord'. Equal maps therefore yield the same sequence of entries. Hashes
-- can vary across @hashable@ versions and machines, so the order among keys
-- with different hashes can vary across those environments.
-- After checking adjacent hashes, maps with distinct hashes use
-- 'Map.foldrWithKey'. Maps with a collision sort each run of equal-hash keys.
{-# INLINE foldrHashMapByHash #-}
foldrHashMapByHash :: (Hashable k, Ord k) => (k -> v -> a -> a) -> a -> Map.HashMap k v -> a
foldrHashMapByHash combine ~initial entries
| hasHashCollision entries = foldrHashMapCollisions combine initial entries
| otherwise = Map.foldrWithKey combine initial entries
-- | Buffer and sort equal-hash runs in a map with collisions.
{-# INLINE foldrHashMapCollisions #-}
foldrHashMapCollisions :: (Hashable k, Ord k) => (k -> v -> a -> a) -> a -> Map.HashMap k v -> a
foldrHashMapCollisions combine ~initial entries = Map.foldrWithKey step finish entries EmptyRun
where
step key value ~next = oneShot $ \pending ->
let !keyHash = hash key
in case pending of
EmptyRun -> next (HashRun keyHash key value [])
HashRun previousHash firstKey firstValue collisions
| keyHash == previousHash ->
next (HashRun previousHash firstKey firstValue ((key, value) : collisions))
| otherwise ->
emit firstKey firstValue collisions (next (HashRun keyHash key value []))
finish EmptyRun = initial
finish (HashRun _ firstKey firstValue collisions) =
emit firstKey firstValue collisions initial
emit key value [] ~rest = combine key value rest
emit key value collisions ~rest =
foldr (\(entryKey, entryValue) -> combine entryKey entryValue) rest
(L.sortOn fst ((key, value) : collisions))
-- | Strict left fold in the traversal order of 'foldrHashMapByHash'.
-- Maps with distinct hashes use @Map.foldlWithKey'@ after the collision check.
{-# INLINE foldlHashMapByHash #-}
foldlHashMapByHash :: (Hashable k, Ord k) => (a -> k -> v -> a) -> a -> Map.HashMap k v -> a
foldlHashMapByHash combine initial entries
| hasHashCollision entries = foldrHashMapCollisions step id entries initial
| otherwise = Map.foldlWithKey' combine initial entries
where
step key value ~next = oneShot $ \ !acc -> next $! combine acc key value
-- * Algebra
------------------------------------------------------------
------------------------------------------------------------------
-- ** Definition of Redundancy (subclassing this makes a redundant algebra)
------------------------------------------------------------------
-- | Type class for Redundant Algebra.
-- Provides fundamental exchange algebra operations: hat, bar, norm, scalar multiplication, and compress.
--
-- Redundant ⊃ Exchange
--
-- hat calculation
-- >>> (.^) (10.@Not:<Yen .+ 10.@Hat:<Dollar)
-- 10.00:@Hat:<Yen .+ 10.00:@Not:<Dollar
--
-- bar calculation
-- >>> x = 10.@Not:<Yen .+ 10.@Hat:<Dollar
-- >>> y = 5.@Hat:<Yen .+ 5.@Not:<Dollar
-- >>> (.-) $ x .+ y
-- 5.00:@Not:<Yen .+ 5.00:@Hat:<Dollar
--
-- norm calculation
-- >>> norm $ 10.@Not:<Yen .+ 10.@Hat:<Dollar
-- 20.0
--
-- (.*) calculation
-- >>> (.*) 5 $ 10.@Not:<Yen .+ 10.@Hat:<Dollar
-- 50.00:@Not:<Yen .+ 50.00:@Hat:<Dollar
--
-- compress calculation
-- >>> compress $ 10.@Not:<Yen .+ 5.@Hat:<Yen .+ 3.@Not:<Yen
-- 5.00:@Hat:<Yen .+ 13.00:@Not:<Yen
--
-- == Redundant-algebra axioms (Akagi 2026, Appendix A, Definition 6)
--
-- The operations above satisfy the following /axioms/ (the paper states five;
-- they are verified as QuickCheck properties in @test\/Spec.hs@, see
-- @axiomProperties@). Axioms 4 and 5 require exact arithmetic, including
-- intermediate products and sums; floating-point readouts can round differently:
--
-- 1. Hat involution: @(.^) ((.^) x) = x@
-- 2. Scalar multiplication of a term: @a '.*' (v ':@' b) = (a*v) ':@' b@
-- 3. Scalar distribution: @a '.*' (x '.+' y) = (a '.*' x) '.+' (a '.*' y)@
-- 4. Norm homogeneity: @'norm' (a '.*' x) = a * 'norm' x@ (for @a >= 0@)
-- 5. Norm additivity: @'norm' (x '.+' y) = 'norm' x + 'norm' y@
--
-- For axiom 4 with Double, take @a = 10@ and two Not postings of 0.1 and
-- 0.2 on one base: @norm (a .* x)@ is 3, but @a * norm x@ is
-- 3.0000000000000004. For axiom 5, let @T = 2^53@, let @x@ contain
-- Not T and Not 1 on one base, and let @y@ contain Not 1 on another:
-- @norm (x .+ y)@ is T+2, but @norm x + norm y@ is T.
--
-- Derived lemmas (also property-tested): @'bar'@ idempotence
-- (@'bar' ('bar' x) = 'bar' x@), 'Zero' identity, and associativity of @('.+')@.
-- For each base and each side, @('.+')@ retains the terms in left-to-right
-- order. 'fromList', 'sigma', and 'unionsMerge' follow the input order.
-- 'Eq', @Binary@, and the sequential sums in 'barWithin' can observe that
-- order. 'Show' displays sorted terms. For finite terms, 'norm', 'bar', and
-- balance are order-independent.
class (HatVal n, HatBaseClass b, Monoid (a n b)) => Redundant a n b where
-- | Hat operation. Flips Hat/Not on all terms.
-- Complexity: O(1) for singleton, O(n) for Liner (n is the number of base keys)
(.^) :: a n b -> a n b
-- | Bar operation. For each full base, sums the Hat side and the Not side
-- exactly in the value type's 'Accum', rounds the exact difference once
-- (to nearest, ties to even) and keeps it on the winning side; the base
-- disappears only when the exact sums are equal. The result does not depend
-- on construction order. A base holding a non-finite posting is kept as is.
-- (0.6.0.0: the 0.5.x sequential sum with a tolerance is 'barWithin'.)
-- Complexity: O(n) (n is the number of base keys)
(.-) :: a n b -> a n b
-- | Alias for bar operation. Identical to @(.-)@.
-- On a ledger that retains complete bases, @norm . bar@ cancels only within each full
-- base. Use 'balanceMapBy' or 'netPairMapBy' for net amounts by group.
bar :: a n b -> a n b
bar = (.-)
-- | The 0.5.x 'bar': sequential side totals, and the two sides cancel when
-- their difference is within the tolerance. Bits may differ from 0.5.x
-- because the summation order follows the internal representation.
-- @barWithin defaultTolerance@ reproduces the 0.5.x values.
-- Complexity: O(n)
barWithin :: Tolerance n -> a n b -> a n b
-- | Aggregates values on the same base. Sums while preserving the Hat/Not distinction.
-- Each side's exact sum is rounded once; the sides do not cancel. Because each
-- side is rounded separately, the net amount of a base may change and cannot
-- be recovered afterwards: with @T = 2^53@, @Not T, Not 1, Hat T@ has exact
-- net 1, but 'compress' rounds the Not side to @T@, so a later 'bar' is zero.
-- Complexity: O(n) (n is the number of base keys)
compress :: a n b -> a n b
-- | Addition in the redundant algebra. Alias for the Monoid @<>@ operation.
-- Complexity: O(union cost)
(.+) :: a n b -> a n b -> a n b
-- | Scalar multiplication. Multiplies the value of every term by a scalar.
-- Fixity @infixr 7@: it binds more tightly than the sums '(.+)' and '(<>)',
-- so @2 .* x .+ y@ and @2 .* x <> y@ scale only @x@, and @2 .* 3 .* x@ is
-- @2 .* (3 .* x)@. It also binds more tightly than '(.@)', so write
-- @2 .* (10 .@ b)@.
-- Complexity: O(1) for singleton, O(n) for Liner
(.*) :: n -> a n b -> a n b
-- | Norm. Sum of the values of all terms (both Hat and Not sides), i.e. the
-- homomorphism from the algebra into the value domain @n@ (Akagi 2026,
-- Appendix A, Definition 6). On exact values it is /additive/:
-- @norm (x '.+' y) = norm x + norm y@ (axiom 5), and /homogeneous/:
-- @norm (a '.*' x) = a * norm x@ for @a >= 0@ (axiom 4). The implementation
-- sums the postings exactly and rounds once, so for floating-point types the
-- two sides of these equations can differ in the last bit (the right-hand
-- side adds two rounded values). Because it sums both sides it does not cancel Hat
-- against Not. Before rounding, @norm ('bar' x) <= norm x@ because bar
-- discards the canceled part. This inequality also holds for exact value
-- types. It need not hold after floating-point rounding: with T = 2^53,
-- Not T and Not 3 on one base and Not 2 on another give norm x = T+4, whereas
-- norm (bar x) = T+6.
-- On a ledger that retains complete bases, @norm ('bar' x)@ does not cancel across
-- different bases. Use 'balanceMapBy' or 'netPairMapBy' to read each
-- group's net amount.
--
-- >>> norm (100.@Not:<Yen .+ 50.@Not:<Euro :: Alg Double (HatBase CountUnit))
-- 150.0
--
-- Complexity: O(n) (n is the number of base keys)
norm :: a n b -> n
-- | Addition in an Applicative context.
-- Complexity: O(union cost)
{-# INLINE (<+) #-}
(<+) :: (Applicative f) => f (a n b) -> f (a n b) -> f (a n b)
(<+) x y = (.+) <$> x <*> y
infixr 7 .^
infixr 7 .*
infixr 2 .-
infixr 3 .+
infixr 3 <+
------------------------------------------------------------
-- | Sequences of values on the Hat and Not sides of one base.
data Pair v
= HatOne ~v
| NotOne ~v
| PairSeq !(Seq v) !(Seq v)
deriving (Eq)
-- | Normalize the representation without evaluating the values.
-- Invariant: 'PairSeq' never holds exactly one posting in total.
mkPair :: Seq v -> Seq v -> Pair v
{-# INLINE mkPair #-}
mkPair hs ns = case (Seq.length hs, Seq.length ns) of
(1, 0) -> case Seq.viewl hs of
h Seq.:< _ -> HatOne h
_ -> PairSeq hs ns
(0, 1) -> case Seq.viewl ns of
n Seq.:< _ -> NotOne n
_ -> PairSeq hs ns
_ -> PairSeq hs ns
-- | Compatibility view. Singleton sides allocate a sequence only on this path.
pairView :: Pair v -> (Seq v, Seq v)
{-# INLINE pairView #-}
pairView (HatOne v) = (Seq.singleton v, Seq.empty)
pairView (NotOne v) = (Seq.empty, Seq.singleton v)
pairView (PairSeq hs ns) = (hs, ns)
-- | Construct, match, select, or update the two ordered sides of a pair.
pattern Pair :: Seq v -> Seq v -> Pair v
pattern Pair {_hatSide, _notSide} <- (pairView -> (_hatSide, _notSide))
where Pair hs ns = mkPair hs ns
{-# COMPLETE Pair #-}
-- | Test for an empty pair without allocating singleton sequences.
pairNull :: Pair v -> Bool
{-# INLINE pairNull #-}
pairNull (HatOne _) = False
pairNull (NotOne _) = False
pairNull (PairSeq hs ns) = Seq.null hs && Seq.null ns
-- | Fold Hat entries followed by Not entries, preserving each side's order.
foldPair :: (a -> v -> a)
-> (a -> v -> a)
-> a
-> Pair v
-> a
{-# INLINE foldPair #-}
foldPair fh _ z (HatOne v) = fh z v
foldPair _ fn z (NotOne v) = fn z v
foldPair fh fn z (PairSeq hs ns) = Foldable.foldl' fn (Foldable.foldl' fh z hs) ns
-- | Map values without evaluating singleton payloads or changing their order.
mapPair :: (v -> w) -> Pair v -> Pair w
{-# INLINE mapPair #-}
mapPair f (HatOne v) = HatOne (f v)
mapPair f (NotOne v) = NotOne (f v)
mapPair f (PairSeq hs ns) = mkPair (fmap f hs) (fmap f ns)
-- | Exchange the two sides without changing the order within either side.
hatPair :: Pair v -> Pair v
{-# INLINE hatPair #-}
hatPair (HatOne v) = NotOne v
hatPair (NotOne v) = HatOne v
hatPair (PairSeq hs ns) = mkPair ns hs
-- | Keep selected entries, preserving side order and normalizing the shape.
filterPair :: (v -> Bool) -> (v -> Bool) -> Pair v -> Pair v
{-# INLINE filterPair #-}
filterPair fh _ p@(HatOne v) = if fh v then p else nullPair
filterPair _ fn p@(NotOne v) = if fn v then p else nullPair
filterPair fh fn (PairSeq hs ns) = mkPair (Seq.filter fh hs) (Seq.filter fn ns)
-- | Sequential side totals, including the initial addition to zero.
pairSums :: Num v => Pair v -> (v, v)
{-# INLINE pairSums #-}
pairSums (HatOne v) = (0 + v, 0)
pairSums (NotOne v) = (0, 0 + v)
pairSums (PairSeq hs ns) = (Foldable.foldl' (+) 0 hs, Foldable.foldl' (+) 0 ns)
-- | Exact side totals, kept separate until the caller chooses a readout.
pairStates :: HatVal v => Pair v -> (Accum v, Accum v)
{-# INLINE pairStates #-}
pairStates (HatOne v) = (addAccum v emptyAccum, emptyAccum)
pairStates (NotOne v) = (emptyAccum, addAccum v emptyAccum)
pairStates (PairSeq hs ns) =
( Foldable.foldl' (flip addAccum) emptyAccum hs
, Foldable.foldl' (flip addAccum) emptyAccum ns
)
instance (Binary.Binary v) => Binary.Binary (Pair v) where
{-# INLINABLE put #-}
{-# INLINABLE get #-}
put (HatOne v) = Binary.put (1 :: Int) >> Binary.put v >> Binary.put (0 :: Int)
put (NotOne v) = Binary.put (0 :: Int) >> Binary.put (1 :: Int) >> Binary.put v
put (PairSeq hs ns) = do
Binary.put (Seq.length hs :: Int)
Foldable.foldr (\x k -> Binary.put x >> k) (pure ()) hs
Binary.put (Seq.length ns :: Int)
Foldable.foldr (\x k -> Binary.put x >> k) (pure ()) ns
get = do
hsLen <- Binary.get :: Binary.Get Int
case hsLen of
1 -> do
h <- Binary.get
nsLen <- Binary.get :: Binary.Get Int
if nsLen <= 0
then pure (HatOne h)
else mkPair (Seq.singleton h) <$> go nsLen Seq.empty
_ | hsLen <= 0 -> do
nsLen <- Binary.get :: Binary.Get Int
if nsLen == 1
then do
n <- Binary.get
pure (NotOne n)
else mkPair Seq.empty <$> go nsLen Seq.empty
| otherwise -> do
hs <- go hsLen Seq.empty
nsLen <- Binary.get :: Binary.Get Int
ns <- go nsLen Seq.empty
pure (mkPair hs ns)
where
go :: Binary.Binary a => Int -> Seq a -> Binary.Get (Seq a)
go n !acc
| n <= 0 = pure acc
| otherwise = do
x <- Binary.get
go (n - 1) (acc Seq.|> x)
instance (HatVal v) => Ord (Pair v) where
{-# INLINE compare #-}
compare first second =
compare (key first) (key second) <> comparePairSides first second
where
-- Hat minus Not; classification uses the state, never rounded overflow.
key pair = case exactAccum state of
Just value -> (1 :: Int, value)
Nothing -> case signAccum state of
Just LT -> (0, 0)
Just GT -> (2, 0)
_ -> (3, 0)
where
state = negateAccum (netPairState pair)
(<) x y | compare x y == LT = True
| otherwise = False
(>) x y | compare x y == GT = True
| otherwise = False
(<=) x y | compare x y == LT = True
| compare x y == EQ = True
| otherwise = False
(>=) x y | compare x y == GT = True
| compare x y == EQ = True
| otherwise = False
max x y | x >= y = x
| otherwise = y
min x y | x <= y = x
| otherwise = y
-- | Compare Hat sequences first and Not sequences second, as in the old record.
comparePairSides :: forall v. Ord v => Pair v -> Pair v -> Ordering
{-# INLINE comparePairSides #-}
comparePairSides x y = compareHat x y <> compareNot x y
where
compareHat :: Pair v -> Pair v -> Ordering
compareHat (HatOne a) (HatOne b) = compare a b
compareHat (HatOne _) (NotOne _) = GT
compareHat (NotOne _) (HatOne _) = LT
compareHat (NotOne _) (NotOne _) = EQ
compareHat (HatOne a) (PairSeq hs _) = oneSeq a hs
compareHat (PairSeq hs _) (HatOne b) = seqOne hs b
compareHat (NotOne _) (PairSeq hs _) = if Seq.null hs then EQ else LT
compareHat (PairSeq hs _) (NotOne _) = if Seq.null hs then EQ else GT
compareHat (PairSeq hs _) (PairSeq ks _) = compare hs ks
compareNot :: Pair v -> Pair v -> Ordering
compareNot (NotOne a) (NotOne b) = compare a b
compareNot (NotOne _) (HatOne _) = GT
compareNot (HatOne _) (NotOne _) = LT
compareNot (HatOne _) (HatOne _) = EQ
compareNot (NotOne a) (PairSeq _ ns) = oneSeq a ns
compareNot (PairSeq _ ns) (NotOne b) = seqOne ns b
compareNot (HatOne _) (PairSeq _ ns) = if Seq.null ns then EQ else LT
compareNot (PairSeq _ ns) (HatOne _) = if Seq.null ns then EQ else GT
compareNot (PairSeq _ ns) (PairSeq _ ms) = compare ns ms
oneSeq :: v -> Seq v -> Ordering
oneSeq a xs = case Seq.viewl xs of
Seq.EmptyL -> GT
b Seq.:< rest -> compare a b <> if Seq.null rest then EQ else LT
seqOne :: Seq v -> v -> Ordering
seqOne xs b = case Seq.viewl xs of
Seq.EmptyL -> LT
a Seq.:< rest -> compare a b <> if Seq.null rest then EQ else GT
{-# INLINE nullPair #-}
-- | Complexity: O(1)
nullPair :: Pair v
nullPair = mkPair Seq.empty Seq.empty
{-# INLINE pairAppend #-}
-- | Complexity: O(log(min(h1,h2)) + log(min(n1,n2)))
-- where h1/h2 and n1/n2 are the lengths of the appended 'Seq's on each side.
pairAppend :: Pair v -> Pair v -> Pair v
pairAppend (HatOne a) (HatOne b) = mkPair (Seq.fromList [a, b]) Seq.empty
pairAppend (NotOne a) (NotOne b) = mkPair Seq.empty (Seq.fromList [a, b])
pairAppend (HatOne a) (NotOne b) = mkPair (Seq.singleton a) (Seq.singleton b)
pairAppend (NotOne a) (HatOne b) = mkPair (Seq.singleton b) (Seq.singleton a)
pairAppend (HatOne a) (PairSeq hs ns) = mkPair (a Seq.<| hs) ns
pairAppend (NotOne a) (PairSeq hs ns) = mkPair hs (a Seq.<| ns)
pairAppend (PairSeq hs ns) (HatOne a) = mkPair (hs Seq.|> a) ns
pairAppend (PairSeq hs ns) (NotOne a) = mkPair hs (ns Seq.|> a)
pairAppend (PairSeq hs ns) (PairSeq ks ms) = mkPair (hs Seq.>< ks) (ns Seq.>< ms)
{-# INLINE pairUnion #-}
-- | Set-style merge of two single-base projection results.
--
-- Used by the multi-pattern 'proj'/'projNetNorm' paths where a query list is
-- treated as a /set/: when several queries select the same posting (duplicate
-- bases, or an exact base overlapping a wildcard query), the selected sides
-- come from the /same/ underlying t'Pair' in @_realg@, so the per-side
-- sequences are identical. Taking each side from whichever operand supplies it
-- (and keeping a single copy) therefore unions the selections without double
-- counting. Contrast @pairAppend@, which concatenates and would duplicate.
--
-- Each operand is a side-restricted projection ('choosePairByHat'), so at most
-- one of the two contributes a non-empty hat side and at most one a non-empty
-- not side for a given base; @pairUnion@ keeps the non-empty one per side.
--
-- Complexity: O(1).
pairUnion :: Pair v -> Pair v -> Pair v
pairUnion p@(HatOne _) (HatOne _) = p
pairUnion p@(NotOne _) (NotOne _) = p
pairUnion (HatOne a) (NotOne b) = mkPair (Seq.singleton a) (Seq.singleton b)
pairUnion (NotOne a) (HatOne b) = mkPair (Seq.singleton b) (Seq.singleton a)
pairUnion p@(HatOne a) (PairSeq _ ns)
| Seq.null ns = p
| otherwise = mkPair (Seq.singleton a) ns
pairUnion p@(NotOne a) (PairSeq hs _)
| Seq.null hs = p
| otherwise = mkPair hs (Seq.singleton a)
pairUnion p@(PairSeq hs ns) (HatOne a)
| Seq.null hs = mkPair (Seq.singleton a) ns
| otherwise = p
pairUnion p@(PairSeq hs ns) (NotOne a)
| Seq.null ns = mkPair hs (Seq.singleton a)
| otherwise = p
pairUnion (PairSeq hs ns) (PairSeq ks ms) =
mkPair (if Seq.null hs then ks else hs) (if Seq.null ns then ms else ns)
-- | A member of the redundant algebra. 'Zero' represents zero, @(:@)@
-- represents one term with a value and hat base, and 'Liner' stores multiple
-- terms in a HashMap-based representation.
--
-- __Invariants (do not hand-construct @Liner@).__ No posting has a zero value,
-- and no @Pair@ has both sides empty. The @Liner@ constructor carries
-- internal cache\/index fields (@_axisPosting@, @_idToBp@, @_allBpIds@) that are
-- /derived from/ @_realg@ and must stay consistent with it: @_axisPosting@,
-- @_idToBp@ and @_allBpIds@ must be exactly the component index, id↔base map and id set
-- built from the keys of @_realg@ (as the internal @linerFromMap@ does). The
-- wildcard projection path (@projWildMap@, @filterByNoteField@) reads those indices and
-- will return wrong answers (silently, not an exception) if they disagree with
-- @_realg@. The fields @_bpToId@ and @_nextBpId@ are currently unmaintained
-- /poison/ (reserved for a dormant scheme) — forcing them throws. Always build
-- values via 'fromList' or the smart constructor '(.@)' rather than applying
-- @Liner@ (or @Single@) directly.
--
-- __Construction boundary (0.6).__ The singleton constructor is the internal
-- 'Single'. Public modules export the one-way pattern synonym '(:@)' instead,
-- so outside "ExchangeAlgebra.Algebra.Internal" a singleton can be matched
-- (@v :@ b@, and the selectors '_val' and '_hatBase') but not built or
-- record-updated: build with '(.@)', which checks the value and the side.
data Alg v b where
Zero :: Alg v b
Single :: !v -> !b -> Alg v b
Liner :: { _realg :: !(Map.HashMap (BasePart b) (Pair v))
, _axisPosting :: ~AxisPosting
, _bpToId :: ~(Map.HashMap (BasePart b) Int)
, _idToBp :: ~(IntMap.IntMap (BasePart b))
, _nextBpId :: ~Int
, _allBpIds :: ~IntSet.IntSet
} -> Alg v b
-- | Serialize an algebra in the 0.5 wire layout (tag 0 = 'Zero', tag 1 =
-- one posting, tag 2 = the per-base map with each t'Pair' in sequence order).
-- Tag 2 writes blocks in 'Map.HashMap' traversal order, with equal-hash keys
-- sorted by 'Ord'. Within one environment, equal algebras with the same
-- tag have identical byte sequences when their component encoders agree
-- on equal values. Equal algebras can have different byte sequences across
-- versions of @hashable@ or when one is a singleton and the other a one-term
-- map. Compare decoded values for equality.
--
-- Decoding is a construction boundary, like '.@': it fails on a value for
-- which 'valueError' is 'Just' (negative or non-finite), on a tag-1 posting
-- whose base side is 'HatNot', and on a repeated base key in tag 2. The checks
-- run before zero values are dropped, and a dropped key still counts as a
-- repeat. Zero values are then dropped as '.@' and '.+' drop them: a tag-1
-- zero decodes to 'Zero', zero entries leave their t'Pair' sequence with the
-- remaining order unchanged, an empty side or base disappears, and an empty
-- tag 2 decodes to 'Zero'. A standalone base value is not checked here, so a
-- 'HatNot' pattern still decodes. For every algebra built from Hat or Not
-- bases with valid values through the public constructors,
-- @decode . encode@ is the identity under structural equality, including the
-- order of each base's sequence.
instance forall v b. ( HatBaseClass b
, HatVal v
, Binary.Binary v
, Binary.Binary b
, Binary.Binary (BasePart b)
) => Binary.Binary (Alg v b) where
{-# INLINABLE put #-}
{-# INLINABLE get #-}
put Zero = Binary.put (0 :: Int)
put (v :@ b) = do
Binary.put (1 :: Int)
Binary.put v
Binary.put b
put (Liner m _ _ _ _ _) = do
Binary.put (2 :: Int)
Binary.put (Map.size m :: Int)
foldrHashMapByHash
(\bp p k -> Binary.put bp >> Binary.put p >> k)
(pure ())
m
get = do
tag <- Binary.get :: Binary.Get Int
case tag of
0 -> pure Zero
1 -> do
value <- Binary.get
basis <- Binary.get
case valueError value of
Just message -> fail ("Alg: " ++ show message)
Nothing
| hat basis == HatNot -> fail "Alg: HatNot posting"
| isZeroValue value -> pure Zero
| otherwise -> pure (value `Single` basis)
2 -> do
count <- Binary.get :: Binary.Get Int
pairs <- go count Map.empty
let nonempty = Map.filter
(not . pairNull) pairs
pure $ if Map.null nonempty then Zero else linerFromMap nonempty
_ -> fail ("Alg: unknown tag " ++ show tag)
where
go :: Int
-> Map.HashMap (BasePart b) (Pair v)
-> Binary.Get (Map.HashMap (BasePart b) (Pair v))
go remaining !acc
| remaining <= 0 = pure acc
| otherwise = do
basis <- Binary.get
decoded <- Binary.get
if Map.member basis acc
then fail "Alg: repeated base key"
else do
let check :: v -> Binary.Get ()
check value = case valueError value of
Just message -> fail ("Alg: " ++ show message)
Nothing -> pure ()
case decoded of
HatOne value -> check value
NotOne value -> check value
PairSeq hs ns -> mapM_ check hs >> mapM_ check ns
let pair = filterPair (not . isZeroValue) (not . isZeroValue) decoded
go (remaining - 1) (Map.insert basis pair acc)
-- | An index from component positions and element keys to sets of base IDs.
type AxisPosting = IntMap.IntMap (Map.HashMap ElementKey SizedIds)
-- | A set of base IDs and its cardinality in the first field.
-- Invariant: insertion evaluates the cardinality before storing the pair.
type SizedIds = (Int, IntSet.IntSet)
{-# INLINE emptyAxisPosting #-}
-- | Complexity: O(1)
emptyAxisPosting :: AxisPosting
emptyAxisPosting = IntMap.empty
{-# INLINE insertAxisPosting #-}
-- | Insert a base ID once for each component position and element key.
-- For d components, this performs O(d) map updates and up to d set membership
-- checks and insertions.
insertAxisPosting :: [ElementKey] -> Int -> AxisPosting -> AxisPosting
insertAxisPosting !keys !bpId !idx =
snd $ L.foldl' step (0 :: Int, idx) keys
where
step (!axis, !acc) !k =
let !axisMap = IntMap.findWithDefault Map.empty axis acc
!axisMap' = Map.alter addId k axisMap
!acc' = IntMap.insert axis axisMap' acc
in (axis + 1, acc')
addId Nothing = Just (1, IntSet.singleton bpId)
addId (Just sized@(count, ids))
| IntSet.member bpId ids = Just sized
| otherwise =
let !count' = count + 1
!ids' = IntSet.insert bpId ids
in Just (count', ids')
{-# INLINE queryAxisPosting #-}
-- | Find candidate base IDs for the concrete components of a query.
-- With d components and k matched sets, this performs up to d index lookups,
-- O(k log k) comparisons of cached cardinalities, and k - 1 intersections
-- (zero intersections when k is zero).
queryAxisPosting :: [ElementKey] -> AxisPosting -> IntSet.IntSet -> IntSet.IntSet
queryAxisPosting !keys !idx !allIds =
case matchedSets of
Left () -> IntSet.empty
Right sets -> case L.sortOn fst sets of
[] -> allIds
((_, ids) : rest) -> L.foldl' intersectIds ids rest
where
intersectIds acc (_, ids) = IntSet.intersection acc ids
matchedSets =
L.foldl' collect (Right []) (zip [0 :: Int ..] keys)
collect (Left ()) _ = Left ()
collect (Right acc) (!axis, !k)
| axisHasWildcard k = Right acc
| otherwise =
case IntMap.lookup axis idx of
Nothing -> Left ()
Just axisMap -> case Map.lookup k axisMap of
Nothing -> Left ()
Just ids -> Right (ids : acc)
-- A component with a wildcard inside it (e.g. @("a", wildcard)@) must
-- not be looked up as an exact key; the final 'matchesQuery' check selects.
axisHasWildcard (ElementKey coordinate) = haveWildcard coordinate
{-# INLINE axisHasWildcard #-}
{-# INLINE linerFromMap #-}
-- | Build a 'Liner' with a lazy index. For n distinct bases with d components,
-- forcing the index performs O(n * d) map and set operations.
linerFromMap :: (HatBaseClass b)
=> Map.HashMap (BasePart b) (Pair v)
-> Alg v b
-- | @_bpToId@ and @_nextBpId@ are reserved for the (currently dormant) P1a
-- incremental-id scheme and are not consumed by any read path. We therefore skip
-- building them and leave them as lazy poison: nothing forces them in normal
-- operation (the wildcard 'proj' path uses @idx@/@idToBp@/@allIds@ only). Forcing
-- either field is a bug, surfaced loudly here instead of silently returning a
-- stale value. Guarded by a poison-field regression test.
--
-- NOTE: the @error@ thunks are written inline in the constructor application (the
-- @Liner@ fields are lazy @~@) rather than as @where@-bindings, because this
-- module is compiled @{-\# LANGUAGE Strict \#-}@, under which a @where@-bound thunk
-- would be forced when 'linerFromMap' is evaluated.
linerFromMap m =
Liner m idx
(error "Liner internal: _bpToId is not maintained (reserved for P1a); do not force")
idToBp
(error "Liner internal: _nextBpId is not maintained (reserved for P1a); do not force")
allIds
where
~(_, idx, idToBp, allIds) =
Map.foldlWithKey'
(\(!bpId, !idxAcc, !idToBpAcc, !allIdsAcc) bp _ ->
let !nextId = bpId + 1
!idx' = insertAxisPosting (toElementKeys bp) bpId idxAcc
!idToBp' = IntMap.insert bpId bp idToBpAcc
!allIds' = IntSet.insert bpId allIdsAcc
in (nextId, idx', idToBp', allIds'))
(0, emptyAxisPosting, IntMap.empty, IntSet.empty)
m
-- | Tests whether the member of the redundant algebra is zero (empty).
--
-- Complexity: O(1)
isZero :: Alg v b -> Bool
isZero Zero = True
isZero _ = False
{-# INLINE singleton #-}
-- | The checked singleton behind '(.@)'; see its contract.
--
-- Complexity: O(1)
singleton :: (HatVal v, HatBaseClass b) => v -> b -> Alg v b
singleton v b | hat b == HatNot = error $ "errorValue at (.@): HatNot side val: "
++ show v ++ show ":@" ++ show b
| isZeroValue v = Zero
| Just reason <- valueError v = error $ "errorValue at (.@): "
++ show reason ++ " val: " ++ show v
++ show ":@"
++ show b
| otherwise = v `Single` b
{-# INLINE (.@) #-}
-- | Smart constructor that builds a term from a value and a hat base.
-- This is the public construction boundary for a posting.
--
-- Contract (0.6): the side of @b@ must be v'Hat' or 'Not'. A base whose side is
-- the wildcard 'HatNot' is rejected with 'error', checked before the zero
-- short-cut (so @0 .@ HatNot :< Cash@ also fails): 'HatNot' is a query
-- pattern, not a side a posting can have. Then a zero value gives 'Zero', and
-- a value for which 'valueError' is 'Just' (negative or non-finite) is
-- rejected with 'error'. Otherwise the result is the singleton @v :@ b@.
-- Values supplied by callbacks that rebuild postings (for example
-- 'mapMaybePosting') are subject to the same precondition.
--
-- Complexity: O(1)
(.@) :: (HatVal n, HatBaseClass b) => n -> b -> Alg n b
(.@) v b = singleton v b
-- | Constructs a term in an Applicative context. Lifted version of @(.@)@.
--
-- Complexity: O(1) + Applicative effects
(<@) :: (HatVal n, Applicative f, HatBaseClass b)
=> f n -> b -> f (Alg n b)
(<@) v b = (.@) <$> v <*> (pure b)
-- | Match a singleton: @v :@ b@ is the posting of value @v@ on base @b@.
-- One-way: it matches and gives the record selectors '_val' and '_hatBase'
-- (partial, as before: they fail on 'Zero' and on a multi-posting value), but
-- it does not build. Build with '(.@)'. Library code that needs the unchecked
-- constructor uses the internal 'Single'.
pattern (:@) :: v -> b -> Alg v b
pattern (:@) {_val, _hatBase} <- Single _val _hatBase
{-# COMPLETE Zero, (:@), Liner #-}
infixr 6 :@
infixr 6 `Single`
infixr 6 .@
infixr 6 <@
-- NOTE: the former top-level @showV@ (which hard-wired @RealFloat@ via
-- @fromFloatDigits@) has been replaced by the per-instance 'showValue' method of
-- 'HatVal', so that exact value types can render themselves. The 'Show' instance
-- of 'Alg' below now calls 'showValue'. The @Double@/@MoneyDouble@ 'showValue'
-- implementations reproduce the old formatting byte-for-byte.
-- | Compare the members of the redundant algebra. A 'Single' and a one-term
-- 'Liner' are equal when their hat bases and values match. Equality also
-- compares the order of values on each Hat and Not side of a base.
instance (HatVal v, HatBaseClass b) => Eq (Alg v b) where
(==) Zero Zero = True
(==) Zero _ = False
(==) _ Zero = False
(==) (v1:@b1) (v2:@b2) = (v1 == v2) && (b1 == b2)
(==) (Liner m1 _ _ _ _ _) (Liner m2 _ _ _ _ _) = m1 == m2
(==) single@(_ :@ _) (Liner entries _ _ _ _ _) =
maybe False (single ==) (singleFromMap entries)
(==) (Liner entries _ _ _ _ _) single@(_ :@ _) =
maybe False (== single) (singleFromMap entries)
(/=) x y = not (x == y)
-- | Order 'Zero' first, one-term members next, and members with multiple
-- terms last. Compare one-term members by hat base and then by value.
-- Compare the remaining members by their maps, including the per-side
-- order of values. For lawful bases and values, @compare x y == EQ@
-- exactly when @x == y@.
instance forall v b. (HatVal v, HatBaseClass b) => Ord (Alg v b) where
{-# INLINE compare #-}
compare Zero Zero = EQ
compare Zero _ = LT
compare _ Zero = GT
compare left right = case (asSingle left, asSingle right) of
(v1 :@ b1, v2 :@ b2)
| b1 == b2 -> compare v1 v2
| b1 > b2 -> GT
| otherwise -> LT
(_ :@ _, Liner {}) -> LT
(Liner {}, _ :@ _) -> GT
(Liner m1 _ _ _ _ _, Liner m2 _ _ _ _ _) -> compare m1 m2
(Zero, Zero) -> EQ
(Zero, _) -> LT
(_, Zero) -> GT
where
asSingle :: Alg v b -> Alg v b
asSingle algebra@(Liner entries _ _ _ _ _) =
Maybe.fromMaybe algebra (singleFromMap entries)
asSingle algebra = algebra
(<) x y | compare x y == LT = True
| otherwise = False
(>) x y | compare x y == GT = True
| otherwise = False
(<=) x y | compare x y == LT = True
| compare x y == EQ = True
| otherwise = False
(>=) x y | compare x y == GT = True
| compare x y == EQ = True
| otherwise = False
max x y | x >= y = x
| otherwise = y
min x y | x <= y = x
| otherwise = y
-- | Read a one-term map as a singleton by inspecting at most two map entries.
{-# INLINE singleFromMap #-}
singleFromMap :: (HatVal v, HatBaseClass b)
=> Map.HashMap (BasePart b) (Pair v)
-> Maybe (Alg v b)
singleFromMap entries = case Map.toList entries of
[(basePart, pair)] -> singlePairToAlg basePart pair
_ -> Nothing
instance (HatVal v, HatBaseClass b) => Show (Alg v b) where
show Zero = "0"
show (v:@b) = (showValue v) ++ ":@" ++ show b
show xs = let ls = toASCList xs
in go ls
where
go [] = "0"
go [y] = show y
go (y:ys) = show y ++ " .+ " ++ go ys
instance NFData (Alg v b) where
rnf Zero = Zero `seq` ()
rnf (v:@b) = v `seq` b `seq` ()
rnf (Liner m _ _ _ _ _) = Map.foldrWithKey (\k v acc -> k `seq` v `seq` acc) () m
------------------------------------------------------------------
-- Semigroup
------------------------------------------------------------------
instance (HatVal n, HatBaseClass b) => Semigroup (Alg n b) where
{-# INLINE (<>) #-}
-- | Associative law ;convert to right join
(<>) = union
-- | Combine two algebras, appending the right-hand values on each base and side.
--
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> x = 1.@Hat:<Yen .+ 1.@Not:<Amount :: Test
-- >>> y = 2.@Hat:<Yen .+ 2.@Not:<Amount :: Test
-- >>> union x y
-- 1.00:@Hat:<Yen .+ 2.00:@Hat:<Yen .+ 1.00:@Not:<Amount .+ 2.00:@Not:<Amount
{-# INLINE union #-}
-- | Complexity:
-- - singleton/singleton and singleton/liner cases: O(n * d * index-build)
-- - liner/liner case: O(n + m) for map union plus O((n+m) * d * index-build)
-- where n and m are distinct key counts on each side.
union :: (HatVal n, HatBaseClass b) => Alg n b -> Alg n b -> Alg n b
union Zero x = x
union x Zero = x
-- singletons
-- NOTE: a zero-valued singleton contributes nothing, so the result must keep the
-- /nonzero/ value on its OWN base. Earlier code returned @v2:@b1@ / @v1:@b2@,
-- relabeling the surviving value onto the zero posting's base — this preserved
-- 'norm' but silently moved the value to the wrong base, corrupting per-base
-- projection and making construction order observable (raw @(:@)@ on a sparsified
-- zero coefficient builds an explicit @0:@base@ singleton). Keep @v2:@b2@ / @v1:@b1@.
union (v1:@b1) (v2:@b2)
| isZeroValue v1 = case isZeroValue v2 of
True -> Zero
False -> v2`Single`b2
| isZeroValue v2 = v1`Single`b1
| otherwise = insert b2 v2 (v1`Single`b1)
-- If one side is a singleton
union x@(Liner m _ _ _ _ _) (v:@b)
| isZeroValue v = x
| otherwise = insertLiner (flip pairAppend) b v m
union (v:@b) x = insert b v x
-- In the case of multiple terms
union (Liner m1 _ _ _ _ _) (Liner m2 _ _ _ _ _) = linerFromMap (Map.unionWith pairAppend m1 m2)
{-# INLINE insert #-}
-- | Insert one term. Into a 'Liner', the term goes before the existing terms
-- for its base and side. Into a single term @w :\@ c@, the existing term
-- stays first, as in @(w :\@ c) .+ (v :\@ b)@.
--
-- Complexity:
-- - into Zero or singleton: O(1) to O(d * index-build)
-- - into Liner: O(n * d * index-build) due to rebuilding 'linerFromMap'
-- where n is the number of distinct base keys after insertion.
insert :: (HatVal v,HatBaseClass b) => b -> v -> Alg v b -> Alg v b
insert _ v x | isZeroValue v = x
insert !b !v Zero = v .@ b
insert !b1 !v1 (v2:@b2) = case isHat b1 of
True -> insert b2 v2
$ linerFromMap
$ Map.singleton (base b1)
$ HatOne v1
False -> insert b2 v2
$ linerFromMap
$ Map.singleton (base b1)
$ NotOne v1
insert !b !v (Liner m _ _ _ _ _) = insertLiner pairAppend b v m
{-# INLINE insertLiner #-}
insertLiner :: HatBaseClass b
=> (Pair v -> Pair v -> Pair v)
-> b -> v -> Map.HashMap (BasePart b) (Pair v) -> Alg v b
insertLiner combine b v m =
linerFromMap (Map.insertWith combine (base b) pairToInsert m)
where
pairToInsert = if isHat b then HatOne v else NotOne v
------------------------------------------------------------------
-- Monoid
------------------------------------------------------------------
instance (HatVal n, HatBaseClass b) => Monoid (Alg n b) where
-- Identity element
mempty = Zero
mappend = (<>)
mconcat = unions
{-# INLINE unions #-}
-- | Complexity: O(sum of HashMap union costs over the fold)
-- For a long list this is typically the dominant construction cost.
unions :: (HatVal n, Foldable f, HatBaseClass b) => f (Alg n b) -> Alg n b
unions ts = Foldable.foldl' union Zero ts
{-# INLINE mergeAlgMap #-}
mergeAlgMap :: (HatVal n, HatBaseClass b)
=> Map.HashMap (BasePart b) (Pair n)
-> Alg n b
-> Map.HashMap (BasePart b) (Pair n)
mergeAlgMap !acc Zero = acc
mergeAlgMap !acc (v :@ b)
| isZeroValue v = acc
| otherwise =
let !p = if isHat b
then HatOne v
else NotOne v
in Map.insertWith (flip pairAppend) (base b) p acc
mergeAlgMap !acc (Liner m _ _ _ _ _)
| Map.null m = acc
| otherwise = Map.unionWith pairAppend acc m
{-# INLINE mergeAlgMapIfNonZero #-}
mergeAlgMapIfNonZero :: (HatVal n, HatBaseClass b)
=> Map.HashMap (BasePart b) (Pair n)
-> Alg n b
-> Map.HashMap (BasePart b) (Pair n)
mergeAlgMapIfNonZero !acc Zero = acc
mergeAlgMapIfNonZero !acc alg@(v :@ _)
| isZeroValue v = acc
| otherwise = mergeAlgMap acc alg
mergeAlgMapIfNonZero !acc alg = mergeAlgMap acc alg
{-# INLINE unionsMerge #-}
-- | Merge multiple Algs by directly combining their internal HashMaps,
-- building the AxisPosting index only once at the end.
--
-- The result preserves the input traversal order on each base and side,
-- as 'fromList' does for a list.
unionsMerge :: (HatVal n, Foldable f, HatBaseClass b) => f (Alg n b) -> Alg n b
unionsMerge ts =
let !m = Foldable.foldl' mergeAlgMap Map.empty ts
in mkAlgFromMap m
------------------------------------------------------------------
-- Redundant
------------------------------------------------------------------
instance (HatVal n, HatBaseClass b) => Redundant Alg n b where
{-# SPECIALIZE instance HatBaseClass b => Redundant Alg Double b #-}
{-# SPECIALIZE instance HatBaseClass b => Redundant Alg MoneyDouble b #-}
{-# SPECIALIZE instance HatBaseClass b => Redundant Alg MoneyDecimal b #-}
(.^) Zero = Zero
(.^) (n:@ b) = n `Single` (revHat b)
(.^) (Liner ms idx bpToId idToBp nextBpId allIds) = Liner
(Map.map hatPair ms)
idx
bpToId
idToBp
nextBpId
allIds
(.+) = mappend
_ .* Zero = Zero
0 .* _ = Zero
-- The algebra is over non-negative values: reject a negative / non-finite
-- scalar instead of silently producing out-of-domain (negative) postings.
-- Products that underflow to zero are dropped without aggregating postings.
x .* _ | isErrorValue x =
error ("(.*): non-negative finite scalar required, got " ++ show x)
x .* (v:@b)
| isZeroValue productValue = Zero
| otherwise = productValue `Single` b
where
productValue = x * v
x .* (Liner ms idx bpToId idToBp nextBpId allIds)
| any hasZero scaledPairs = filter (not . isZeroValue . _val) scaled
| otherwise = scaled
where
scaledPairs = Map.map (mapPair (x *)) ms
scaled = Liner scaledPairs idx bpToId idToBp nextBpId allIds
hasZero (HatOne value) = isZeroValue value
hasZero (NotOne value) = isZeroValue value
hasZero (PairSeq hs ns) = any isZeroValue hs || any isZeroValue ns
norm Zero = 0
norm (v:@_) = roundAccum (addAccum v emptyAccum)
norm (Liner ms _ _ _ _ _) = roundAccum $
Map.foldl' (foldPair (flip addAccum) (flip addAccum)) emptyAccum ms
{-# INLINE (.-) #-}
(.-) Zero = Zero
(.-) scalar@(v :@ _)
| isZeroValue v = Zero
| otherwise = scalar
(.-) (Liner pairs ~idx ~bpToId ~idToBp ~nextBpId ~allIds)
| Map.null reduced = Zero
| Map.size reduced == Map.size pairs =
Liner reduced idx bpToId idToBp nextBpId allIds
| otherwise = linerFromMap reduced
where
reduced = Map.mapMaybe reduce pairs
reduce pair = case signAccum state of
Just EQ -> Nothing
Just LT -> settle Hat (negateAccum state)
_ -> settle Not state
where
state = netPairState pair
settle side magnitude =
let value = roundAccum magnitude
in if isErrorValue value && hasNonfiniteState state
then Just pair
else finish side value
finish side value
| isZeroValue value = Nothing
| side == Hat = Just (HatOne value)
| otherwise = Just (NotOne value)
{-# INLINE barWithin #-}
barWithin _ Zero = Zero
barWithin _ (v:@b) = v`Single`b
barWithin tolerance (Liner m _ _ _ _ _) = let !res = Map.mapMaybe f m
in case null res of
True -> Zero
False -> linerFromMap res
where
{-# INLINE f #-}
f p =
let (!h, !n) = pairSums p
in case (not (isErrorValue h || isErrorValue n)
&& abs (h - n) <= absTol tolerance
+ relTol tolerance * max (abs h) (abs n)) of -- scale-aware tolerance (WI-11)
True -> Nothing
False -> case p of
HatOne _ | h > n -> Just p
NotOne _ | n > h -> Just p
_ -> case compare h n of
GT -> Just (HatOne (h - n))
_ -> Just (NotOne (n - h))
{-# INLINE compress #-}
compress Zero = Zero
compress scalar@(v :@ _)
| isZeroValue v = Zero
| otherwise = scalar
compress (Liner pairs ~idx ~bpToId ~idToBp ~nextBpId ~allIds)
| Map.null reduced = Zero
| Map.size reduced == Map.size pairs =
Liner reduced idx bpToId idToBp nextBpId allIds
| otherwise = linerFromMap reduced
where
reduced = Map.mapMaybe compressPair pairs
compressPair pair =
let (hatState, notState) = pairStates pair
h = roundAccum hatState
n = roundAccum notState
in case (isZeroValue h, isZeroValue n) of
(True, True) -> Nothing
(False, True) -> Just (HatOne h)
(True, False) -> Just (NotOne n)
(False, False) -> Just (mkPair (Seq.singleton h) (Seq.singleton n))
-- | Visit full-base pairs without rebuilding every posting's base.
-- The fold visits keys with the same hash in 'Ord' order.
foldPairs :: (HatVal n, HatBaseClass b)
=> (a -> BasePart b -> Pair n -> a) -> a -> Alg n b -> a
{-# INLINE foldPairs #-}
foldPairs = foldPairsWith foldlHashMapByHash
-- | Visit each full-base pair in the order provided by the supplied map fold.
{-# INLINE foldPairsWith #-}
foldPairsWith :: (HatVal n, HatBaseClass b)
=> ((a -> BasePart b -> Pair n -> a) -> a -> Map.HashMap (BasePart b) (Pair n) -> a)
-> (a -> BasePart b -> Pair n -> a)
-> a
-> Alg n b
-> a
foldPairsWith _ _ initial Zero = initial
foldPairsWith _ collect initial (value :@ postingBase)
| isZeroValue value = initial
| isHat postingBase = collect initial (base postingBase) (HatOne value)
| otherwise = collect initial (base postingBase) (NotOne value)
foldPairsWith foldMapPairs collect initial (Liner pairs _ _ _ _ _) =
foldMapPairs collect initial pairs
-- | Exact Not-minus-Hat residual for one complete base.
netPairState :: HatVal n => Pair n -> Accum n
{-# INLINE netPairState #-}
netPairState pair = mergeAccum nots (negateAccum hats)
where
(hats, nots) = pairStates pair
-- | Distinguish nonfinite inputs from finite overflow of the rounded output.
hasNonfiniteState :: HatVal n => Accum n -> Bool
{-# INLINE hasNonfiniteState #-}
hasNonfiniteState state = signAccum (mergeAccum state (negateAccum state)) /= Just EQ
-- | Group unrounded residuals for Alg and Journal callbacks. Nonfinite bases
-- are returned separately, unchanged, and never classified by the callback key.
postNetStates :: (HatVal n, HatBaseClass b, Ord k)
=> (b -> Maybe k) -> Alg n b -> (Alg n b, M.Map k (Accum n))
{-# INLINE postNetStates #-}
postNetStates keyOf algebra = (mkAlgFromMap retained, totals)
where
(!retained, !totals) = foldPairsWith Map.foldlWithKey' collect (Map.empty, M.empty) algebra
collect (!unchanged, !amounts) basePart pair =
let state = netPairState pair
select side magnitude = case keyOf (merge side basePart) of
Nothing -> (unchanged, amounts)
Just key -> let !updated = M.insertWith mergeAccum key magnitude amounts
in (unchanged, updated)
in if isErrorValue (abs (roundAccum state)) && hasNonfiniteState state
then let !updated = Map.insert basePart pair unchanged in (updated, amounts)
else case signAccum state of
Just EQ -> (unchanged, amounts)
Just LT -> select Hat (negateAccum state)
_ -> select Not state
-- * Basic functions
------------------------------------------------------------------
-- | Returns the value of each term, as a list.
-- The traversal visits keys with the same hash in 'Ord' order.
-- Complexity: linear in the number of terms, plus sorting within collisions.
vals :: (HatVal v, HatBaseClass b) => Alg v b -> [v]
vals Zero = []
vals (v:@_) = [v]
vals (Liner m _ _ _ _ _) =
reverse $
foldlHashMapByHash
(\values _ -> foldPair (flip (:)) (flip (:)) values)
[]
m
-- | Returns the hat base of each term, as a list.
-- The traversal visits keys with the same hash in 'Ord' order before
-- prepending terms to the result.
-- Complexity: linear in the number of terms, plus sorting within collisions.
bases :: (HatVal v, HatBaseClass b) => Alg v b -> [b]
bases Zero = []
bases (_:@b) = [b]
bases (Liner m _ _ _ _ _) = foldlHashMapByHash f [] m
where
f :: (HatVal v, HatBaseClass b) => [b] -> BasePart b -> Pair v -> [b]
f xs b = foldPair (g Hat b) (g Not b) xs
g :: (HatVal v, HatBaseClass b) => Hat -> BasePart b -> [b] -> v -> [b]
g h b ys _ = (merge h b):ys
{-# INLINE fromList #-}
-- | convert List to Alg n b
-- Complexity: O(sum of HashMap union costs), because this is implemented via 'mconcat'.
--
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> xs = [1.@Hat:<Yen,1.@Not:<Dollar, 2.@Hat:<Yen, 2.@Not:<Dollar] :: [Test]
-- >>> fromList xs
-- 1.00:@Hat:<Yen .+ 2.00:@Hat:<Yen .+ 1.00:@Not:<Dollar .+ 2.00:@Not:<Dollar
--
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> x = 1.@Hat:<Yen .+ 1.@Not:<Amount :: Test
-- >>> y = 2.@Hat:<Yen .+ 2.@Not:<Amount :: Test
-- >>> fromList [x,y]
-- 1.00:@Hat:<Yen .+ 2.00:@Hat:<Yen .+ 1.00:@Not:<Amount .+ 2.00:@Not:<Amount
fromList ::(HatVal v, HatBaseClass b ) => [Alg v b] -> Alg v b
fromList = mconcat
-- | Summation function that applies a function to each element of a list and sums the results.
-- Complexity: O(sum of HashMap union costs over produced members of the redundant algebra).
--
-- Each base and side retains terms in list order, as in 'unionsMerge'.
--
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> vals (sigma [1,2,3] (\x -> x .@ Hat :< Yen) :: Test)
-- [1.0,2.0,3.0]
{-# INLINE sigma #-}
sigma :: (HatVal v, HatBaseClass b) => [a] -> (a -> Alg v b) -> Alg v b
sigma xs f = mkAlgFromMap $ L.foldl' step Map.empty xs
where
step !acc !x = mergeAlgMapIfNonZero acc (f x)
-- | Conditional summation over a double loop. For all combinations of two lists,
-- applies the function only to pairs that satisfy the condition and sums the results.
--
-- Complexity: O(|xs| * |ys| * union cost)
{-# INLINE sigma2When #-}
sigma2When :: (HatVal v, HatBaseClass b)
=> [a]
-> [c]
-> (a -> c -> Bool)
-> (a -> c -> Alg v b)
-> Alg v b
sigma2When xs ys cond f =
mkAlgFromMap $ L.foldl' outer Map.empty xs
where
outer !acc !x = L.foldl' (inner x) acc ys
inner !x !acc !y
| cond x y = mergeAlgMapIfNonZero acc (f x y)
| otherwise = acc
-- | Summation using keys and values from a Map. Skips entries with zero values.
--
-- Complexity: O(|map| * union cost)
{-# INLINE sigmaFromMap #-}
sigmaFromMap :: (HatVal v, HatBaseClass b, Ord k)
=> M.Map k v
-> (k -> v -> Alg v b)
-> Alg v b
sigmaFromMap kvs f =
mkAlgFromMap $ M.foldlWithKey' step Map.empty kvs
where
step !acc !k !v
| isZeroValue v = acc
| otherwise = mergeAlgMapIfNonZero acc (f k v)
-- | Returns the list of terms.
-- The traversal visits keys with the same hash in 'Ord' order before
-- prepending terms to the result. Equal algebras produce equal lists.
-- Complexity: linear in the number of terms, plus sorting within collisions.
--
-- >>> toList (10.@Hat:<(Yen) .+ 10.@Hat:<(Dollar) .+ Zero :: Alg Double (HatBase CountUnit))
-- [10.00:@Hat:<Dollar,10.00:@Hat:<Yen]
--
-- you need define type variables to use this for Zero
-- >>> toList Zero :: [Alg Double (HatBase CountUnit)]
-- []
toList :: (HatVal v, HatBaseClass b) => Alg v b -> [Alg v b]
toList = toListWith foldlHashMapByHash
-- | Collect terms by prepending each term in the supplied map traversal.
{-# INLINE toListWith #-}
toListWith :: (HatVal v, HatBaseClass b)
=> (([Alg v b] -> BasePart b -> Pair v -> [Alg v b])
-> [Alg v b] -> Map.HashMap (BasePart b) (Pair v) -> [Alg v b])
-> Alg v b
-> [Alg v b]
toListWith _ Zero = []
toListWith _ (v :@ b) = [v `Single` b]
toListWith foldMapPairs (Liner m _ _ _ _ _) = foldMapPairs f [] m
where
f :: (HatVal v, HatBaseClass b) => [Alg v b] -> BasePart b -> Pair v -> [Alg v b]
f xs b (HatOne v) = g Hat b xs v
f xs b (NotOne v) = g Not b xs v
f xs b (PairSeq hs ns) =
Foldable.foldl' (g Hat b) (Foldable.foldl' (g Not b) xs ns) hs
g :: (HatVal v, HatBaseClass b) => Hat -> BasePart b -> [Alg v b] -> v -> [Alg v b]
g h b ys v
| isZeroValue v = ys
| otherwise = (v `Single` (merge h b)):ys
{-# INLINE foldEntries #-}
-- | Strict left fold over scalar entries, sorting only runs of base keys
-- with the same hash by 'Ord'. For lawful values and bases, @x == y@ implies
-- @foldEntries f initial x == foldEntries f initial y@ for the same step
-- and initial accumulator, whenever the step respects equality.
--
-- This is the implementation vehicle for the universal extension from the free
-- commutative monoid of entries when each step acts through an associative,
-- commutative accumulator operation (equivalently, entry updates commute).
-- Under that condition the result is independent of both the internal
-- @HashMap@ traversal and each side's sequence order. For a non-commutative
-- accumulator, such as list append, this is only an ordinary left fold: its
-- result records the actual traversal order and can distinguish different
-- sequence orders of the same entry multiset.
foldEntries :: forall v b acc. (HatVal v, HatBaseClass b)
=> (acc -> v -> b -> acc)
-> acc
-> Alg v b
-> acc
foldEntries _ !acc Zero = acc
foldEntries f !acc (v :@ b)
| isZeroValue v = acc
| otherwise = f acc v b
foldEntries f !acc (Liner m _ _ _ _ _) =
foldlHashMapByHash step acc m
where
step :: acc -> BasePart b -> Pair v -> acc
step !acc0 !bp pair =
let !hatBase = merge Hat bp
!notBase = merge Not bp
in foldPair (entry hatBase) (entry notBase) acc0 pair
entry :: b -> acc -> v -> acc
entry b a v = if isZeroValue v then a else f a v b
{-# INLINE toASCList #-}
-- | Complexity: O(s log s), dominated by sorting the list representation.
toASCList :: (HatVal v, HatBaseClass b) => Alg v b -> [Alg v b]
toASCList = L.sort . toListWith Map.foldlWithKey'
-- | Transform each term with a function that returns one term.
-- The traversal orders base keys with the same hash by 'Ord', so equal
-- inputs give equal results with the same equality-preserving function.
-- Complexity: O(s + c), plus sorting within collisions, where s is the
-- number of input terms and c is the number of output terms.
-- Typed alternatives: 'mapPosting' and 'mapMaybePosting'.
--
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> x = 1.@Hat:<Yen .+ 1.@Not:<Amount :: Test
-- >>> y = 2.@Not:<Yen .+ 2.@Hat:<Amount :: Test
-- >>> map (\ (x:@hb) -> x.@(toHat hb)) $ x .+ y
-- 1.00:@Hat:<Yen .+ 2.00:@Hat:<Yen .+ 1.00:@Hat:<Amount .+ 2.00:@Hat:<Amount
--
-- >>> type Test = Alg Double Hat
-- >>> x = 1.@Hat .+ 1.@Not :: Test
-- >>> y = 2.@Not .+ 2.@Hat :: Test
-- >>> map (\ (x:@hb) -> (2 * x).@hb) $ x .+ y
-- 2.00:@Hat .+ 4.00:@Hat .+ 2.00:@Not .+ 4.00:@Not
-- NB. 'map' applies its function to one singleton posting at a time and requires
-- the result to itself be a singleton @(v2 :@ b2)@ (or 'Zero', handled via the
-- 'isZeroValue' check / the 'r' helper below). The @let v2:@b2 = ...@ and the
-- @case fn (...)@ in 'r' therefore intentionally match only the @(:@)@ shape; a
-- 'Zero'\/'Liner' result is outside this contract, so the non-exhaustive patterns
-- are by design (audited). Adding catch-all arms would silently change behavior.
map :: (HasCallStack,HatVal v, HatBaseClass b)
=> (Alg v b -> Alg v b) -> Alg v b -> Alg v b
map _ Zero = Zero
map f (v:@b) = let v2:@b2 = f (v`Single`b)
in case isZeroValue v2 of
True -> Zero
False -> (v2 `Single` b2)
map f (Liner m _ _ _ _ _) = mkAlgFromMap $ foldrHashMapByHash (p f) dnilMap m Map.empty
where
{-# INLINE dnilMap #-}
dnilMap = id
{-# INLINE dappendMap #-}
dappendMap = (.)
{-# INLINE dsingleMap #-}
dsingleMap (bp, p') = Map.insertWith pairAppend bp p'
{-# INLINE p #-}
p :: (HatVal v, HatBaseClass b)
=> (Alg v b -> Alg v b)
-> BasePart b
-> Pair v
-> DMap (BasePart b) (Pair v)
-> DMap (BasePart b) (Pair v)
p fn b (HatOne v) accDList = pOne fn Hat b v accDList
p fn b (NotOne v) accDList = pOne fn Not b v accDList
p fn b (PairSeq hs ns) accDList =
let (dl1, hs2) = q fn Hat b hs
(dl2, ns2) = q fn Not b ns
prefix = dappendMap dl1 dl2
in case (Seq.null hs2, Seq.null ns2) of
(True,True) -> dappendMap prefix accDList
(True,False) -> dappendMap prefix
. dappendMap (dsingleMap (b, mkPair Seq.empty ns2))
$ accDList
(False,True) -> dappendMap prefix
. dappendMap (dsingleMap (b, mkPair hs2 Seq.empty))
$ accDList
(False,False) -> dappendMap prefix
. dappendMap (dsingleMap (b, mkPair hs2 ns2))
$ accDList
pOne :: (HatVal v, HatBaseClass b)
=> (Alg v b -> Alg v b)
-> Hat
-> BasePart b
-> v
-> DMap (BasePart b) (Pair v)
-> DMap (BasePart b) (Pair v)
pOne fn h bp v rest = case fn (v `Single` merge h bp) of
Zero -> rest
v2 :@ b2
| isZeroValue v2 -> rest
| b2 == (merge h bp) -> Map.insertWith pairAppend bp
(if isHat h then HatOne v2 else NotOne v2) . rest
| isHat (hat b2) -> Map.insertWith pairAppend (base b2) (HatOne v2) . rest
| otherwise -> Map.insertWith pairAppend (base b2) (NotOne v2) . rest
{-# INLINE q #-}
q :: (HatVal v, HatBaseClass b)
=> (Alg v b -> Alg v b)
-> Hat
-> BasePart b
-> Seq v
-> (DMap (BasePart b) (Pair v), Seq v)
q fn h b vs = Foldable.foldl' (r fn h b) (dnilMap, Seq.empty) vs
{-# INLINE r #-}
r :: (HatVal v, HatBaseClass b)
=> (Alg v b -> Alg v b)
-> Hat
-> BasePart b
-> (DMap (BasePart b) (Pair v), Seq v)
-> v
-> (DMap (BasePart b) (Pair v), Seq v)
r fn h b (dlAcc,vsAcc) v = case fn (v`Single`(merge h b)) of
Zero -> (dlAcc, vsAcc)
------------------------------------------------------------------
v2:@b2
| isZeroValue v2 -> (dlAcc, vsAcc)
-- Structural equality: a wildcard in a ledger base is
-- a value, so rewriting it to (or from) a concrete
-- element changes the base.
| b2 == (merge h b) -> (dlAcc, v2 Seq.<| vsAcc)
| isHat (hat b2) -> (dappendMap dlAcc (dsingleMap ( base b2
,HatOne v2))
,vsAcc )
| otherwise -> (dappendMap dlAcc (dsingleMap ( base b2
,NotOne v2 ))
,vsAcc )
-- | Map every posting to exactly one posting. The typed form of 'map'.
mapPosting :: (HatVal v, HatVal v2, HatBaseClass b, HatBaseClass b2)
=> (v -> b -> (v2, b2)) -> Alg v b -> Alg v2 b2
mapPosting f = mapMaybePosting (\v b -> Just (f v b))
-- | Map each term to at most one term. 'Nothing' drops the term, and '(.@)'
-- normalizes a zero value. The traversal orders base keys with the same hash
-- by 'Ord', so equal inputs give equal results with the same
-- equality-preserving function.
mapMaybePosting :: (HatVal v, HatVal v2, HatBaseClass b, HatBaseClass b2)
=> (v -> b -> Maybe (v2, b2)) -> Alg v b -> Alg v2 b2
mapMaybePosting _ Zero = Zero
mapMaybePosting f (v :@ b) = case f v b of
Nothing -> Zero
Just (v2, b2) -> v2 .@ b2
mapMaybePosting f (Liner m _ _ _ _ _) =
mkAlgFromMap $ foldrHashMapByHash addPair id m Map.empty
where
{-# INLINE addPair #-}
addPair bp (HatOne v) accDMap = mapOne (merge Hat bp) id v . accDMap
addPair bp (NotOne v) accDMap = mapOne (merge Not bp) id v . accDMap
addPair bp (PairSeq hs ns) accDMap =
mapSide Hat bp hs . mapSide Not bp ns . accDMap
{-# INLINE mapSide #-}
mapSide h bp = Foldable.foldl' (mapOne (merge h bp)) id
{-# INLINE mapOne #-}
mapOne b dlAcc v = case f v b of
Nothing -> dlAcc
Just (v2, b2) -> case v2 .@ b2 of
Zero -> dlAcc
v3 :@ b3 -> dlAcc . Map.insertWith pairAppend (base b3) (postingPair v3 b3)
Liner {} -> dlAcc
{-# INLINE postingPair #-}
postingPair v b
| isHat (hat b) = HatOne v
| otherwise = NotOne v
-- Difference-map (endo) used by 'map' to accumulate Liner rebuilds in O(1).
-- NB. The plain difference-list helpers (dnil/dappend/dsingle/dToList/dFromList)
-- and the unused 'DList' type alias were removed as dead code: only the DMap
-- variants (dnilMap/dappendMap/dsingleMap, defined locally in 'map') are used.
type DMap k v = Map.HashMap k v -> Map.HashMap k v
{-# INLINE filter #-}
-- | filter
-- Complexity: O(s), where s is total number of scalar entries.
--
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> x = 1.@Hat:<Yen .+ 1.@Not:<Amount :: Test
-- >>> y = 2.@Not:<Yen .+ 2.@Hat:<Amount :: Test
-- >>> filter (isHat . _hatBase) $ x .+ y
-- 1.00:@Hat:<Yen .+ 2.00:@Hat:<Amount
--
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> x = 1.@Hat:<Yen .+ 1.@Not:<Amount :: Test
-- >>> y = 2.@Not:<Yen .+ 2.@Hat:<Amount :: Test
-- >>> filter ((1 <). _val) $ x .+ y
-- 2.00:@Not:<Yen .+ 2.00:@Hat:<Amount
filter :: forall v b. (HatVal v, HatBaseClass b) => (Alg v b -> Bool) -> Alg v b -> Alg v b
filter _ Zero = Zero
filter f (v:@b) | f (v`Single`b) = v`Single`b
| otherwise = Zero
filter f (Liner m _ _ _ _ _) =
let selected = Map.mapMaybeWithKey select m
in if Map.null selected then Zero else linerFromMap selected
where
select :: BasePart b -> Pair v -> Maybe (Pair v)
select bp pair =
let kept = filterPair (accept Hat bp) (accept Not bp) pair
in if pairNull kept then Nothing else Just kept
accept :: Hat -> BasePart b -> v -> Bool
accept h bp val = f (val `Single` merge h bp)
------------------------------------------------------------
-- | Relabel the /base part/ of every term, preserving the Hat\/Not structure
-- and the redundancy (the ordered Hat- and Not-side sequences). Only the
-- 'BasePart' is rewritten by @f@; the Hat\/Not side and the values are untouched.
-- When @f@ maps two distinct base parts onto the same target, their sequences are
-- concatenated (pair-append), so no value is lost — hence @'norm'@ is preserved.
-- Preserving wildcards (@(.#)@) is the caller's responsibility (in @f@).
--
-- == Laws and their layer of validity
--
-- On the full subcategory of 'HatBaseClass' bases, this relabelling is a
-- functor only after observing an algebra through ℘, where ℘ forgets sequence
-- order but retains, for every full Hat\/Not base, the multiset of values.
-- The functor and additive laws at this layer are:
--
-- * For valid algebras, @mapBasePart id x == x@. When rebuilding a one-term
-- 'Liner' as a 'Single', 'Eq' compares the same value and hat base.
-- * @mapBasePart (g . f) x@ and
-- @mapBasePart g (mapBasePart f x)@ are equal through ℘, but need not be raw
-- equal.
-- * @mapBasePart f (x .+ y)@ and
-- @mapBasePart f x .+ mapBasePart f y@ are equal through ℘, but need not be
-- raw equal.
-- * @norm (mapBasePart f x) == norm x@ for exact additive value types. For
-- floating-point values, regrouping after collisions has the usual rounding
-- caveat.
-- * @mapBasePart f ((.^) x) == (.^) (mapBasePart f x)@ (raw).
-- * Algebraically, @bar (mapBasePart f (bar x))@ and
-- @bar (mapBasePart f x)@ are equal through ℘ whenever the implementation's
-- rounding of a source-base residual does not change the cancellation after
-- relabelling (always on exact values).
--
-- In particular, @mapBasePart f (bar x) == bar (mapBasePart f x)@ is false in
-- general: distinct source bases may merge after relabeling. The traversal
-- preserves every value and visits source keys with the same hash in 'Ord'
-- order. For lawful values and bases and an equality-preserving @f@,
-- @x == y@ implies @mapBasePart f x == mapBasePart f y@.
--
-- Complexity: linear in the number of base keys, plus sorting within
-- collisions. The posting index is rebuilt once.
--
-- >>> type T = Alg Double (HatBase CountUnit)
-- >>> mapBasePart id (10.@Hat:<Yen :: T) :: T
-- 10.00:@Hat:<Yen
--
-- >>> norm (mapBasePart (const Amount) (10.@Not:<Yen .+ 5.@Not:<Dollar :: T) :: T)
-- 15.0
mapBasePart :: (HatVal v, HatBaseClass b, HatBaseClass b')
=> (BasePart b -> BasePart b') -> Alg v b -> Alg v b'
mapBasePart _ Zero = Zero
mapBasePart f (v :@ b) = singleton v (merge (hat b) (f (base b)))
mapBasePart f (Liner m _ _ _ _ _) =
mkAlgFromMap $
foldlHashMapByHash
(\acc bp p -> Map.insertWith pairAppend (f bp) p acc)
Map.empty
m
------------------------------------------------------------
-- | Free extension: substitute every scalar entry @v :\@ b@ by the algebra
-- @h v b@ and merge the results with @('.+')@.
--
-- Through ℘ (see 'mapBasePart'), 'Alg' is the free commutative monoid on its
-- entries, and @extendBy h@ is the unique ℘-homomorphism that agrees with @h@
-- on the generators. It acts entry by entry in the /redundant/ layer, whereas
-- 'postFromNetBy' first nets through exact, unrounded base residuals and
-- extends from that quotient, as described in its rounding contract.
-- 'mapBasePart' is the special case that keeps the value and the Hat\/Not side
-- and only relabels the base part.
--
-- == Laws and their layer of validity
--
-- ℘ observes, for every full Hat\/Not base, the multiset of values and forgets
-- the per-side sequence order. Through ℘:
--
-- * @extendBy h (x .+ y)@ equals @extendBy h x .+ extendBy h y@.
-- * For valid algebras, @extendBy (.\@) x == x@. The substitution rebuilds
-- each @v :\@ b@ term and the fold preserves the order of values on each
-- Hat and Not side of a base.
-- * @mapBasePart f x@ equals
-- @extendBy (\\v b -> v :\@ merge (hat b) (f (base b))) x@.
-- * @norm (extendBy h x)@ equals the sum of @norm (h v b)@ over the entries of
-- @x@ (exact additive value types; floating-point values carry the usual
-- rounding caveat). In particular, for exact additive value types, @norm@
-- is preserved whenever every @h v b@ has norm @v@. Rounded norms alone
-- do not suffice: with T = 2^53, replace a Not T posting by Not T and Not 1,
-- and preserve a separate Not 1 posting. Each replacement has the original
-- rounded norm, but the total norm changes from T to T+2.
--
-- Zero-valued entries are skipped, as in 'foldEntries'. Well-formedness of the
-- algebras returned by @h@ (non-negative, finite values) is the caller's
-- responsibility: build them with @(.\@)@ rather than the raw constructor.
-- The traversal visits base keys with the same hash in 'Ord' order.
-- For lawful values and bases and an equality-preserving @h@,
-- @x == y@ implies @extendBy h x == extendBy h y@.
--
-- Complexity: O(n · cost(h)) plus the cost of the @('.+')@ merges and
-- sorting within collisions, in one pass over the entries.
--
-- >>> type T = Alg Double (HatBase CountUnit)
-- >>> let split v b = (v / 2) .@ b .+ (v / 2) .@ b :: T
-- >>> norm (extendBy split (10.@Not:<Yen .+ 4.@Hat:<Dollar :: T))
-- 14.0
--
-- >>> extendBy (\v b -> v .@ b .+ v .@ b) (3.@Not:<Yen :: T) == (3.@Not:<Yen .+ 3.@Not:<Yen :: T)
-- True
{-# INLINE extendBy #-}
extendBy :: (HatVal v, HatBaseClass b, HatBaseClass b')
=> (v -> b -> Alg v b') -> Alg v b -> Alg v b'
extendBy h = foldEntries (\acc v b -> acc .+ h v b) Zero
------------------------------------------------------------
-- | proj
--
-- Projects an 'Alg' onto the bases matching a query list. The query list is
-- treated as a __set__: duplicate queries, or an exact base that also matches a
-- wildcard query in the same list, select each underlying posting __at most
-- once__ (no double counting). The result is the union of the selected
-- @(base, side)@ cells.
--
-- Wildcards match __one way__: only a wildcard in the /query/ is a pattern
-- (\"any value on this component\"). A wildcard stored in a ledger base is an
-- ordinary value (\"this component does not apply\"): a wildcard query component selects
-- it, a concrete query component does not. The result is the same whether the
-- ledger is a single term or a 'Liner'.
--
-- >>> type Test = Alg Double (HatBase (CountUnit, CountUnit))
-- >>> proj [Not:<(Yen,Yen)] (10.@Not:<(Yen,(.#)) :: Test)
-- 0
--
-- >>> type Test = Alg Double (HatBase (CountUnit, CountUnit))
-- >>> proj [Not:<(Yen,(.#))] (10.@Not:<(Yen,(.#)) :: Test)
-- 10.00:@Not:<(Yen,CountUnit)
--
-- Complexity:
-- exact single-key path: expected O(1)
-- wildcard single-key path: index lookup, sorting by cached cardinality, set
-- intersections, and one lookup and verification step per candidate base.
-- The first wildcard query also builds the index.
-- multi-pattern path: O(sum pattern costs + union costs)
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> x = 1.@Hat:<Yen .+ 1.@Not:<Amount :: Test
-- >>> y = 2.@Not:<Yen .+ 2.@Hat:<Amount :: Test
-- >>> proj [Hat:<Yen] $ x .+ y
-- 1.00:@Hat:<Yen
--
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> x = 1.@Hat:<Yen .+ 1.@Not:<Amount :: Test
-- >>> y = 2.@Not:<Yen .+ 2.@Hat:<Amount :: Test
-- >>> proj [HatNot:<Amount] $ x .+ y
-- 2.00:@Hat:<Amount .+ 1.00:@Not:<Amount
--
-- >>> type Test = Alg Double (HatBase (CountUnit, CountUnit))
-- >>> x = 1.@Hat:<(Yen,Yen) .+ 1.@Not:<(Amount,Amount) :: Test
-- >>> y = 2.@Not:<(Yen,Yen) .+ 2.@Hat:<(Dollar,Yen) :: Test
-- >>> proj [Hat:<((.#),Yen)] $ x .+ y
-- 1.00:@Hat:<(Yen,Yen) .+ 2.00:@Hat:<(Dollar,Yen)
--
-- >>> type Test = HatBase CountUnit
-- >>> compareHatBase (Not:<(.#) :: Test) (Not:<Yen :: Test)
-- EQ
--
-- >>> type Test = Alg Double (HatBase CountUnit)
-- >>> x = 1.@Hat:<Yen .+ 1.@Not:<Amount :: Test
-- >>> y = 2.@Not:<Yen .+ 2.@Hat:<Amount :: Test
-- >>> proj [Not:<(.#)] $ x .+ y
-- 2.00:@Not:<Yen .+ 1.00:@Not:<Amount
--
------------------------------------------------------------
proj :: (HatVal v, HatBaseClass b) => [b] -> Alg v b -> Alg v b
proj [] _ = Zero
proj _ Zero = Zero
proj [b] (v:@b2)
| matchesQuery b b2 = v`Single`b2
| otherwise = Zero
-- Index fields bound lazily (@~@) so a concrete (non-wildcard) base never forces
-- the component index (the module is @Strict@; see 'projExactMap').
proj [b] (Liner m ~idx _ ~idToBp _ ~allIds) =
mkAlgFromMap $
if haveWildcard (base b)
then projWildMap b m idx idToBp allIds
else projExactMap b m
proj (b:bs) (v:@b2)
| matchesQuery b b2 = v`Single`b2
| otherwise = proj bs (v`Single`b2)
-- Multi-pattern path: the query list is treated as a /set/. Overlapping or
-- duplicate queries (e.g. a duplicated base, or an exact base subsumed by a
-- wildcard) select the same posting only once. Per-base results are merged
-- with 'pairUnion' (set union of the selected sides) rather than @pairAppend@
-- (concatenation), so no posting is double counted.
proj (b:bs) (Liner m ~idx _ ~idToBp _ ~allIds) =
mkAlgFromMap $
L.foldl'
(\acc q -> Map.unionWith pairUnion acc
(if haveWildcard (base q)
then projWildMap q m idx idToBp allIds
else projExactMap q m))
Map.empty
(b:bs)
{-# INLINE choosePairByHat #-}
-- | Complexity: O(1)
choosePairByHat :: Hat -> Pair v -> Pair v
choosePairByHat HatNot p = p
choosePairByHat Hat p@(HatOne _) = p
choosePairByHat Not p@(NotOne _) = p
choosePairByHat Hat (NotOne _) = nullPair
choosePairByHat Not (HatOne _) = nullPair
choosePairByHat Hat (PairSeq hs _) = mkPair hs Seq.empty
choosePairByHat Not (PairSeq _ ns) = mkPair Seq.empty ns
{-# INLINE projExactMap #-}
-- | Exact (non-wildcard) single-base projection: a direct 'Map.lookup' on the
-- @_realg@ map. It does NOT reference the component index, so the concrete projection
-- path can keep that lazy index unforced.
--
-- This matters because the module is compiled @{-\# LANGUAGE Strict \#-}@: handing
-- the (lazy) index to a helper that takes it as a strict argument would force its
-- whole construction even for a concrete lookup that never needs it. Callers
-- therefore dispatch on 'haveWildcard' BEFORE touching the index, binding the
-- index fields lazily (@~@) and only mentioning them on the wildcard branch.
-- Guarded by the poison-index regression test in the test suite.
--
-- Complexity: expected O(1).
projExactMap
:: (HatBaseClass b)
=> b
-> Map.HashMap (BasePart b) (Pair v)
-> Map.HashMap (BasePart b) (Pair v)
projExactMap b m = case Map.lookup bp m of
Nothing -> Map.empty
Just p -> case choosePairByHat h p of
selected | pairNull selected -> Map.empty
| otherwise -> Map.singleton bp selected
where
!bp = base b
!h = hat b
{-# INLINE projWildMap #-}
-- | Wildcard single-base projection: resolves candidates through the component index
-- ('queryAxisPosting'), so it necessarily forces the index. Only invoked when
-- 'haveWildcard' holds.
--
-- The query uses cached set cardinalities to order intersections, then looks up
-- and verifies each candidate base. The index is built on first use.
projWildMap
:: (HatBaseClass b)
=> b
-> Map.HashMap (BasePart b) (Pair v)
-> AxisPosting
-> IntMap.IntMap (BasePart b)
-> IntSet.IntSet
-> Map.HashMap (BasePart b) (Pair v)
projWildMap b m idx idToBp allIds =
let !ids = queryAxisPosting (toElementKeys bp) idx allIds
in IntSet.foldl'
(\acc bpId -> case IntMap.lookup bpId idToBp of
Nothing -> acc
Just bp0 -> case Map.lookup bp0 m of
Nothing -> acc
Just p
| matchesQuery bp bp0 -> case choosePairByHat h p of
selected | pairNull selected -> acc
| otherwise -> Map.insert bp0 selected acc
| otherwise -> acc)
Map.empty
ids
where
!bp = base b
!h = hat b
{-# INLINE mkAlgFromMap #-}
-- | Complexity: O(n) to inspect shape and possibly rebuild index.
mkAlgFromMap :: (HatVal v, HatBaseClass b) => Map.HashMap (BasePart b) (Pair v) -> Alg v b
mkAlgFromMap m
| Map.null m = Zero
| otherwise = case Map.toList m of
[(b, p)] -> Maybe.fromMaybe (linerFromMap $ Map.singleton b p) (singlePairToAlg b p)
_ -> linerFromMap m
{-# INLINE singlePairToAlg #-}
-- | Complexity: O(1)
singlePairToAlg :: (HatVal v, HatBaseClass b) => BasePart b -> Pair v -> Maybe (Alg v b)
singlePairToAlg b (HatOne v) = Just (v `Single` merge Hat b)
singlePairToAlg b (NotOne v) = Just (v `Single` merge Not b)
singlePairToAlg _ (PairSeq _ _) = Nothing
------------------------------------------------------------------
-- | Bar-netted norm of a projection. The query list is treated as a __set__
-- (see 'proj'): overlapping or duplicate queries do not double count.
--
-- Note the semantics include the @bar@ netting: each projected base is reduced
-- to the non-negative net of its hat and not sides. On exact values
--
-- @projNetNorm bs x == norm (bar (proj bs x))@
--
-- which is /not/ the same as @norm (proj bs x)@ when a base carries both sides.
-- The implementation adds the unrounded per-base residuals and rounds once,
-- whereas @norm (bar (proj bs x))@ rounds each base first; for floating-point
-- types the two can differ (residuals @2^53 + 1@ and @1@ give @2^53 + 2@ here
-- and @2^53@ there).
--
-- Wildcards match one way, as in 'proj': a concrete query component does not match
-- a wildcard stored in a ledger base.
-- On a ledger that retains complete bases, this nets only within each projected full
-- base; it does not cancel across different bases. Use 'balanceMapBy' or
-- 'netPairMapBy' for net amounts by group.
-- Sums are exact over the given postings and the output is rounded once; sides
-- cancel only when they are exactly equal.
--
-- Complexity: O(cost(proj) + cost(bar) + cost(norm)).
{-# SPECIALIZE projNetNorm :: HatBaseClass b => [b] -> Alg Double b -> Double #-}
{-# SPECIALIZE projNetNorm :: HatBaseClass b => [b] -> Alg MoneyDouble b -> MoneyDouble #-}
{-# SPECIALIZE projNetNorm :: HatBaseClass b => [b] -> Alg MoneyDecimal b -> MoneyDecimal #-}
projNetNorm :: (HatVal n, HatBaseClass b) => [b] -> Alg n b -> n
projNetNorm queries = roundAccum . projectedNetState queries
-- | Indexed projection followed by exact base-local netting, without rounding.
projectedNetState :: (HatVal n, HatBaseClass b) => [b] -> Alg n b -> Accum n
{-# INLINE projectedNetState #-}
projectedNetState [] _ = emptyAccum
projectedNetState _ Zero = emptyAccum
projectedNetState bs (v :@ b)
| L.any (`matchesQuery` b) bs = addAccum v emptyAccum
| otherwise = emptyAccum
-- Index fields bound lazily (@~@); a concrete base uses 'projExactMap' (a plain
-- 'Map.lookup') and never forces the component index. See 'projExactMap'.
projectedNetState [b] (Liner m ~idx _ ~idToBp _ ~allIds) =
foldProjectedState $
if haveWildcard (base b)
then projWildMap b m idx idToBp allIds
else projExactMap b m
-- Multi-pattern path: the query list is a /set/ (see 'proj'). Per-base results
-- are merged with 'pairUnion' so overlapping/duplicate queries do not double
-- count. Note 'projectedNetState' returns a bar-netted norm: each base contributes its
-- unrounded residual, and the residuals are rounded once at the end.
projectedNetState bs (Liner m ~idx _ ~idToBp _ ~allIds) =
foldProjectedState $
L.foldl'
(\acc q -> Map.unionWith pairUnion acc
(if haveWildcard (base q)
then projWildMap q m idx idToBp allIds
else projExactMap q m))
Map.empty
bs
{-# INLINE foldProjectedNorm #-}
-- | Complexity: O(k), where k is the number of projected base keys.
foldProjectedNorm :: (HatVal n) => Map.HashMap k (Pair n) -> n
foldProjectedNorm = roundAccum . foldProjectedState
-- | Merge projected base residuals before rounding the requested output.
foldProjectedState :: HatVal n => Map.HashMap k (Pair n) -> Accum n
{-# INLINE foldProjectedState #-}
foldProjectedState = Map.foldl' (\total pair ->
let state = netPairState pair
in mergeAccum total (case signAccum state of
Just LT -> negateAccum state
_ -> state)) emptyAccum
{-# INLINE barNormPair #-}
-- | Round the exact magnitude of one base's residual.
barNormPair :: HatVal n => Pair n -> n
barNormPair pair = case signAccum state of
Just LT -> roundAccum (negateAccum state)
_ -> roundAccum state
where
state = netPairState pair
-- | Compute the net balance as the difference of two projections.
-- @balanceBy plusBases minusBases alg@ computes
-- @projNetNorm plusBases alg - projNetNorm minusBases alg@ on exact values.
-- The implementation takes the exact difference of the two unrounded projected
-- residuals and rounds once, so it is not the difference of two rounded
-- 'projNetNorm' results.
--
-- Useful for calculating stock quantities, profits, etc.
--
-- >>> type T = Alg Double (HatBase CountUnit)
-- >>> let alg = 100 .@ Not:<Yen .+ 30 .@ Hat:<Yen :: T
-- >>> balanceBy [Not:<Yen] [Hat:<Yen] alg
-- 70.0
--
-- >>> balanceBy [Hat:<Yen] [Not:<Yen] alg
-- -70.0
{-# SPECIALIZE balanceBy :: HatBaseClass b => [b] -> [b] -> Alg Double b -> Double #-}
{-# SPECIALIZE balanceBy :: HatBaseClass b => [b] -> [b] -> Alg MoneyDouble b -> MoneyDouble #-}
{-# SPECIALIZE balanceBy :: HatBaseClass b => [b] -> [b] -> Alg MoneyDecimal b -> MoneyDecimal #-}
balanceBy :: (HatVal n, HatBaseClass b) => [b] -> [b] -> Alg n b -> n
balanceBy plusBases minusBases alg =
roundAccum (mergeAccum (projectedNetState plusBases alg)
(negateAccum (projectedNetState minusBases alg)))
-- | Aggregate the net balance of an 'Alg' by a key, in a single pass.
--
-- @balanceMapBy keyOf@ is the bucketed form of 'balanceBy': for every entry it
-- projects the (side-stripped) 'BasePart' to a bucket key with @keyOf@ ('Nothing'
-- drops the entry), and nets each bucket using the Hat\/Not convention (Not adds,
-- Hat subtracts) — on exact values, @projNetNorm [Not:<k] - projNetNorm [Hat:<k]@
-- per key. The implementation takes the exact signed sum and rounds once, so with
-- floating-point values it is not the difference of two rounded 'projNetNorm'
-- results.
-- @keyOf@ sees only the 'BasePart', not the Hat\/Not side, so it cannot split one
-- key across sides.
--
-- This replaces @[ (k, balanceBy [Not:<k] [Hat:<k] alg) | k <- keys ]@ — one
-- wildcard projection per key — with a single fold; both readouts sum exactly
-- before rounding. For per-key reporting over many keys this is the
-- difference between @O(keys * entries)@ and @O(entries)@.
--
-- The values are /signed/ net balances and may be negative, so use a signed value
-- type (e.g. 'Double', @MoneyDouble@, @MoneyDecimal@); a non-negative-only type
-- such as @Number.NonNegative.Double@ is unsuitable here. Keys whose net is zero
-- are kept (like 'foldEntriesToMap'); filter afterwards if undesired.
-- Each key's signed sum (Not minus Hat) is exact over the given postings and
-- rounded once.
--
-- Complexity: O(total number of entries) — a single fold, no per-key projection.
--
-- >>> type T = Alg Double (HatBase CountUnit)
-- >>> let alg = 100 .@ Not:<Yen .+ 30 .@ Hat:<Yen .+ 50 .@ Not:<Dollar :: T
-- >>> balanceMapBy Just alg
-- fromList [(Yen,70.0),(Dollar,50.0)]
{-# INLINE balanceMapBy #-}
{-# SPECIALIZE balanceMapBy :: (HatBaseClass b, Ord k) => (BasePart b -> Maybe k) -> Alg Double b -> M.Map k Double #-}
{-# SPECIALIZE balanceMapBy :: (HatBaseClass b, Ord k) => (BasePart b -> Maybe k) -> Alg MoneyDouble b -> M.Map k MoneyDouble #-}
{-# SPECIALIZE balanceMapBy :: (HatBaseClass b, Ord k) => (BasePart b -> Maybe k) -> Alg MoneyDecimal b -> M.Map k MoneyDecimal #-}
balanceMapBy :: (HatVal v, HatBaseClass b, Ord k)
=> (BasePart b -> Maybe k) -> Alg v b -> M.Map k v
balanceMapBy keyOf = M.map roundAccum . foldPairsWith Map.foldlWithKey' collect M.empty
where
collect totals basePart pair = case keyOf basePart of
Nothing -> totals
Just key -> M.insertWith mergeAccum key (netPairState pair) totals
-- | Aggregate the per-base /netted/ balance of an 'Alg' by a key, keeping the
-- winning side, in a single pass.
--
-- This is the implementation counterpart of the paper's @def:class-net@
-- (\(\nu_\kappa\)), the __pair__ read-out of the class-net operator:
-- Before rounding, the exact Not-minus-Hat total of each key equals the
-- difference of its two unrounded residual totals. Read a key absent from
-- netPairMapBy as (0,0): balanceMapBy retains zero keys after full cancellation.
-- The equality also holds for
-- the returned values when rounding is the identity on sums and subtraction is
-- exact (MoneyDecimal within its scale limit, or a Rational-backed value type).
-- It does not hold for Double or MoneyDouble: let T = 2^53 and group two
-- distinct bases containing Not T, Not 1 and Hat T under one key. The signed
-- readout is 1, but the pair is (T,T), whose rounded-component difference is 0.
--
-- @netPairMapBy keyOf@ projects each entry's (side-stripped) 'BasePart' to a
-- bucket key with @keyOf@ ('Nothing' drops the entry), and for every key
-- returns a pair @(notTotal, hatTotal)@ built as follows: for each base \(b\)
-- it first nets the two sides (@bar@-like cancellation of the redundant
-- sequences), keeping only the residual on the side that wins, then sums the
-- residuals across all bases mapping to the key —
--
-- \[ \Big( \textstyle\sum_{n_b > h_b} (n_b - h_b),\ \sum_{h_b > n_b} (h_b - n_b) \Big). \]
--
-- This per-base netting is named and documented here — it is /not/ an implicit
-- 'bar'; the function performs exactly the standard-form reduction the name
-- promises, mirroring how 'balanceMapBy' reports a netted read-out.
--
-- __Both components are non-negative__ (consistent with the value domain
-- \(V \subseteq \mathbb{R}_{\ge 0}\)): a base contributes to at most one side,
-- whichever residual is larger. Bases whose two sides are exactly equal
-- contribute nothing. Because of the non-negativity,
-- @netPairMapBy@ is well behaved for non-negative-only value types, whereas the
-- @n - h@ identity on returned values requires a signed value type with exact
-- sums and subtraction, as specified above; a signed floating-point type is
-- insufficient.
-- Each base is netted exactly; the unrounded residuals are merged per key and
-- winning side, and each component is rounded once. Both sides of distinct
-- bases within a key do not cancel.
--
-- Complexity: O(total number of entries) — a single fold over the entries,
-- followed by one collapse over the distinct bases.
--
-- >>> type T = Alg Double (HatBase CountUnit)
-- >>> let alg = 100 .@ Not:<Yen .+ 30 .@ Hat:<Yen .+ 50 .@ Not:<Dollar :: T
-- >>> netPairMapBy Just alg
-- fromList [(Yen,(70.0,0.0)),(Dollar,(50.0,0.0))]
{-# INLINE netPairMapBy #-}
{-# SPECIALIZE netPairMapBy :: (HatBaseClass b, Ord k) => (BasePart b -> Maybe k) -> Alg Double b -> M.Map k (Double, Double) #-}
{-# SPECIALIZE netPairMapBy :: (HatBaseClass b, Ord k) => (BasePart b -> Maybe k) -> Alg MoneyDouble b -> M.Map k (MoneyDouble, MoneyDouble) #-}
{-# SPECIALIZE netPairMapBy :: (HatBaseClass b, Ord k) => (BasePart b -> Maybe k) -> Alg MoneyDecimal b -> M.Map k (MoneyDecimal, MoneyDecimal) #-}
netPairMapBy :: (HatVal v, HatBaseClass b, Ord k)
=> (BasePart b -> Maybe k) -> Alg v b -> M.Map k (v, v)
netPairMapBy keyOf = M.map finish . foldPairsWith Map.foldlWithKey' collect M.empty
where
collect totals basePart pair = case keyOf basePart of
Nothing -> totals
Just key ->
let state = netPairState pair
in case signAccum state of
Just EQ -> totals
Just GT -> M.insertWith combine key (state, emptyAccum) totals
Just LT -> M.insertWith combine key (emptyAccum, negateAccum state) totals
Nothing -> M.insertWith combine key (state, state) totals
combine (!first, !second) (!left, !right) =
let !notTotal = mergeAccum first left
!hatTotal = mergeAccum second right
in (notTotal, hatTotal)
finish (!first, !second) =
let !notTotal = roundAccum first
!hatTotal = roundAccum second
in (notTotal, hatTotal)
-- | Fold algebra entries into a @Map@, combining values with @(+)@.
--
-- The selector function examines each entry @(v, b)@ and optionally returns
-- a @(key, value)@ pair. Values for duplicate keys are summed.
--
-- >>> type T = Alg Double (HatBase CountUnit)
-- >>> let alg = 10 .@ Hat:<Yen .+ 20 .@ Hat:<Dollar .+ 5 .@ Hat:<Yen :: T
-- >>> let f v (Hat :< a) = Just (a, v); f _ _ = Nothing
-- >>> foldEntriesToMap f alg
-- fromList [(Yen,15.0),(Dollar,20.0)]
foldEntriesToMap :: (HatVal v, HatBaseClass b, Ord k)
=> (v -> b -> Maybe (k, v))
-> Alg v b
-> M.Map k v
foldEntriesToMap f = foldEntries step M.empty
where
step acc v b = case f v b of
Just (k, v') -> M.insertWith (+) k v' acc
Nothing -> acc
-- | Quotient decomposition (dec_κ): partition an algebra along the equivalence
-- classes induced by a classifier on the full 'HatBase', in a single pass.
--
-- For each entry, @keyOf@ maps its base (Hat\/Not state included) to a class
-- key; @Nothing@ drops the entry as residual. Each class is returned as an
-- 'Alg' that is exactly the restriction of the input to that class:
-- __redundancy (the per-base value sequences) is fully preserved__ — no 'bar',
-- no 'norm', no aggregation. The pieces reconstruct the input:
-- @mconcat (M.elems (decBy keyOf x)) .+ residual == x@ (up to per-base
-- sequence order).
--
-- This generalizes the decomposition operators of Deguchi & Nakano (1986)
-- ('ExchangeAlgebra.Accounting.Exchange.decR', 'ExchangeAlgebra.Accounting.Exchange.decL',
-- 'ExchangeAlgebra.Accounting.Exchange.decP', and 'ExchangeAlgebra.Accounting.Exchange.decM'
-- are two-class special cases) and replaces
-- per-class projection loops: one pass over the entries instead of one
-- projection query per class.
--
-- Choosing between the per-key family:
--
-- +--------------------+----------------------------------+---------------------+
-- | function | returns | redundancy |
-- +====================+==================================+=====================+
-- | 'decBy' | @Map k (Alg v b)@ (structure) | preserved |
-- +--------------------+----------------------------------+---------------------+
-- | 'balanceMapBy' | @Map k v@ (signed net per key) | lost (bar-like) |
-- +--------------------+----------------------------------+---------------------+
-- | 'netPairMapBy' | @Map k (v,v)@ (non-neg net pair) | lost (bar-like) |
-- +--------------------+----------------------------------+---------------------+
-- | 'foldEntriesToMap' | @Map k v@ (custom collection) | lost |
-- +--------------------+----------------------------------+---------------------+
-- | 'mapBasePart' | @Alg v b'@ (base coarsening π_κ) | preserved |
-- +--------------------+----------------------------------+---------------------+
--
-- Note on 'bar': @bar@ commutes with 'decBy' componentwise iff @keyOf@ does not
-- distinguish Hat\/Not (i.e. factors through 'base'). Side-sensitive classifiers
-- (such as the ones underlying
-- 'ExchangeAlgebra.Accounting.Exchange.decP'\/'ExchangeAlgebra.Accounting.Exchange.decM' or
-- 'ExchangeAlgebra.Accounting.Exchange.decL'\/'ExchangeAlgebra.Accounting.Exchange.decR') do not
-- commute
-- with @bar@ — netting before or after such a split is a semantic choice.
--
-- Complexity: O(m) over distinct bases (single fold; per-class insert costs
-- O(log k) in the result 'M.Map').
--
-- >>> type T = Alg Double (HatBase CountUnit)
-- >>> let alg = 100 .@ Not:<Yen .+ 30 .@ Hat:<Yen .+ 50 .@ Not:<Dollar :: T
-- >>> M.toList (M.map norm (decBy (\(_ :< a) -> Just a) alg))
-- [(Yen,130.0),(Dollar,50.0)]
--
-- >>> M.toList (M.map norm (decBy (\b -> if isHat b then Just () else Nothing) alg))
-- [((),30.0)]
{-# INLINE decBy #-}
decBy :: (HatVal v, HatBaseClass b, Ord k)
=> (b -> Maybe k)
-> Alg v b
-> M.Map k (Alg v b)
decBy _ Zero = M.empty
decBy kf a@(v :@ b)
| isZeroValue v = M.empty
| otherwise = case kf b of
Nothing -> M.empty
Just k -> M.singleton k a
decBy kf (Liner m _ _ _ _ _) =
M.map mkAlgFromMap (Map.foldlWithKey' step M.empty m)
where
step !acc !bp p@(HatOne _) = insertSide (merge Hat bp) bp p acc
step !acc !bp p@(NotOne _) = insertSide (merge Not bp) bp p acc
step !acc !bp (PairSeq hs ns) =
let !acc1 = if Seq.null hs
then acc
else insertSide (merge Hat bp) bp (mkPair hs Seq.empty) acc
in if Seq.null ns
then acc1
else insertSide (merge Not bp) bp (mkPair Seq.empty ns) acc1
{-# INLINE insertSide #-}
insertSide hb bp p acc = case kf hb of
Nothing -> acc
Just k -> M.insertWith (Map.unionWith (flip pairAppend)) k (Map.singleton bp p) acc
-- | Classify-net-post, fused: net the algebra per base ('bar' — explicit in the
-- name), classify each netted entry with @keyOf@ (class totals are summed with
-- @(+)@), then generate postings per class and bulk-merge them.
--
-- On exact values,
-- @postFromNetBy keyOf post x == sigmaFromMap (foldEntriesToMap collect (bar x)) post@
-- where @collect@ pairs each netted entry with its class. With floating-point
-- values the two differ: the left side merges the unrounded residuals of a key
-- and rounds once, the right side rounds each base in 'bar' first (residuals
-- @2^53 + 1@ and @1@ in one key give @2^53 + 2@ and @2^53@). The common
-- \"shortage detection → purchase postings\" pattern becomes a single call:
--
-- @
-- purchases = postFromNetBy shortageKey purchasePosting termAlg
-- @
--
-- and runs in one pass over the netted entries — no per-pair projection loop
-- (the naive all-pairs formulation costs O(N²) queries; this costs O(m)).
--
-- __This function applies unrounded exact bar internally__ (per-base netting, the standard
-- positive-part normalization). Redundancy of the input is not preserved in the
-- intermediate; the output is whatever @post@ builds. If you need the
-- redundancy-preserving split itself, use 'decBy'.
-- Thus it factors through exact base residuals before rounding, not through
-- the returned 'bar'. With T = 2^53, two bases with residuals (T+1,1) and (T,1)
-- have identical rounded bar outputs. Grouping them under one key gives callback
-- arguments T+2 and T, respectively. It is not the free
-- extension that acts independently on entries in the redundant layer (that
-- one is 'extendBy').
-- Each base is netted exactly to choose the winning side; the unrounded
-- residuals are merged per key, and the value passed to the callback is rounded
-- once. The guarantee ends at the callback's argument. Bases containing
-- nonfinite inputs retain their original postings and are not passed to the
-- callback. Finite accumulation overflow is rounded only at the callback
-- boundary, which may receive infinity.
--
-- Complexity: O(m + Σ cost(post)).
--
-- >>> type T = Alg Double (HatBase CountUnit)
-- >>> let stock = 100 .@ Not:<Amount .+ 130 .@ Hat:<Amount .+ 20 .@ Not:<Yen :: T
-- >>> let shortageKey b = case b of { Hat :< Amount -> Just () ; _ -> Nothing }
-- >>> norm (postFromNetBy shortageKey (\_ v -> v .@ Not:<Amount .+ v .@ Hat:<Yen) stock)
-- 60.0
{-# INLINE postFromNetBy #-}
{-# SPECIALIZE postFromNetBy :: (HatBaseClass b, Ord k) => (b -> Maybe k) -> (k -> Double -> Alg Double b) -> Alg Double b -> Alg Double b #-}
{-# SPECIALIZE postFromNetBy :: (HatBaseClass b, Ord k) => (b -> Maybe k) -> (k -> MoneyDouble -> Alg MoneyDouble b) -> Alg MoneyDouble b -> Alg MoneyDouble b #-}
{-# SPECIALIZE postFromNetBy :: (HatBaseClass b, Ord k) => (b -> Maybe k) -> (k -> MoneyDecimal -> Alg MoneyDecimal b) -> Alg MoneyDecimal b -> Alg MoneyDecimal b #-}
postFromNetBy :: (HatVal v, HatBaseClass b, Ord k)
=> (b -> Maybe k)
-> (k -> v -> Alg v b)
-> Alg v b
-> Alg v b
postFromNetBy keyOf post algebra =
retained .+ sigmaFromMap (M.map roundAccum amounts) post
where
(!retained, !amounts) = postNetStates keyOf algebra