Definitions and axioms
Note: The mathematical definitions are optional background for using the library. If you prefer to skip the mathematics, go to Installation and modules.
Element structure
Definition 1 (Element structure). Each element set $E$ has a distinguished wildcard element $\# \in E$ and a total order $\leq$. For a query element $p$ and a stored element $e$ in $E$, one-way matching is defined by
$$ p \trianglerighteq e \quad\Longleftrightarrow\quad (p=\#)\lor(p=e). $$
A stored $\#$ indicates that the corresponding element set does not apply. A query for a specific firm therefore skips a term whose firm element is $\#$. The wildcard is an ordinary member of the total order; matching does not define that order.
Definition 1 of the preprint gives a symmetric comparison. The query relation used here treats only a query-side $\#$ as a pattern.
Usage: Elements.
Bases
Definition 2. A base is a tuple of elements, one from each element set. For element sets $E_1,\ldots,E_n$, the base space is $E_1\times\cdots\times E_n$. An account and a firm, for example, form a base with two elements. A query tuple matches a stored base exactly when each query element matches the corresponding stored element.
Here, “element” refers to a member of an element set that supplies a component of a base, while a “member of the redundant algebra” is a sum of terms.
Usage: Bases.
Hat bases and terms
Definition 3. A hat base pairs a base with one of two directions: Not (increase) or Hat (decrease). A query can select both directions with HatNot. Each term has a direction of Not or Hat.
A term pairs a hat base with a nonnegative real amount from the value domain $V\subseteq\mathbb{R}_{\geq 0}$. We write $a:e$ for an increase and $a:\widehat{e}$ for a decrease at the base $e$. The redundant algebra is generated by its terms. In Haskell, Posting is the type that represents a term.
Usage: Bases and Amount types.
Redundant algebra and linear combinations
Definition 4. A redundant algebra consists of finite collections of terms, with addition, scalar multiplication by nonnegative values, hat, bar, and a norm into the value domain.
Usage: Operations and Amount types.
Definition 5. At each underlying base $e_i$, a member of the redundant algebra records two finite sequences of amounts. Write $f_i(e_i)=\langle A_i,B_i\rangle$, with $A_i$ the increase sequence and $B_i$ the decrease sequence. Two increases of 3 and 2 at the same base remain separate terms until an operation explicitly combines them. A member of the redundant algebra $x=\langle f_1,\ldots,f_n\rangle$ has finite support over the bases. An empty sequence pair contributes zero.
Usage: Add the month’s transactions.
Operations
Definition 6. The operations on the paired sequences are as follows.
| Operation | Effect |
|---|---|
| Hat | Swap the two sequences at each base, preserving every amount. |
| Bar | Total each sequence at each base and cancel the two totals. Keep the excess on the larger side and drop a zero remainder. |
| Sum | Concatenate the corresponding sequences. |
| Scalar multiplication | Multiply every amount by the same nonnegative scalar. |
| Norm | Sum all amounts on both sides over all bases. |
The library adds compress, which sums amounts separately for each base and each direction (Hat or Not).
For bar, write $[z]_+=\max(0,z)$. If $A_i=(a_{i1},\ldots,a_{ip_i})$ and $B_i=(b_{i1},\ldots,b_{iq_i})$, the remaining increase and decrease are
$$ D_i=\left[\sum_j a_{ij}-\sum_k b_{ik}\right]_+, \qquad R_i=\left[\sum_k b_{ik}-\sum_j a_{ij}\right]_+. $$
For example, two increases of 3 and 2 and a decrease of 4 on the same base have norm 9. Applying compress gives an increase of 5 and a decrease of 4. Bar leaves an increase of 1. In general, the contribution to the norm is
$$ \operatorname{norm}(f_i(e_i)) = \sum_{j=1}^{p_i} a_{ij} + \sum_{k=1}^{q_i} b_{ik}. $$
Usage: Select an operation.
Five axioms
Let $x=\langle f_1,\ldots,f_n\rangle$ and let $a$ be a nonnegative scalar.
-
Hat is an involution. Applying hat twice restores the original terms.
$$ \widehat{\widehat{f_i}}(e_i) = f_i(e_i). $$
-
Scaling at one base. The scalar acts on each amount in each sequence.
$$ a \ast f_i(e_i) = \langle a \ast A_i, a \ast B_i\rangle. $$
-
Scaling across bases. The scalar acts on every component.
$$ a \ast \langle f_1,\ldots,f_n\rangle = \langle a \ast f_1,\ldots,a \ast f_n\rangle. $$
-
Norm homogeneity. Scaling the terms scales their total.
$$ \operatorname{norm}(a \ast x) = a \cdot \operatorname{norm}(x), \qquad a \ge 0. $$
-
Norm additivity. Adding terms adds their totals.
$$ \operatorname{norm}(f_i(e_i)+g_i(e_i)) = \operatorname{norm}(f_i(e_i)) + \operatorname{norm}(g_i(e_i)). $$
The preprint lists the defining sum of the norm under the name “norm homogeneity.” The homogeneity law above follows from that sum and the scalar multiplication.
Axioms 4 and 5 hold exactly with exact decimal arithmetic when the amounts, intermediate sums, and products are representable. With binary floating-point arithmetic, summing stored amounts exactly and rounding once still permits the two sides to differ in the last digit: scaling can round individual products, and adding two norms can use two already rounded totals.
Usage: Choose an amount type.
Account classes and sides
Definition 7. An exchange base carries an account title with an account division and a home side. Its debit or credit side combines that home side with the direction. In the library, whatPIMO classifies the account as plus stock, input, minus stock, or output, and fixedCurrent classifies it as current, fixed, or other. Table 1 shows how the directions map to sides for ordinary accounts.
| Division | Not | Hat |
|---|---|---|
| Assets | Increase (debit) | Decrease (credit) |
| Liabilities, equity | Increase (credit) | Decrease (debit) |
| Revenue | Recognition (credit) | Reversal (debit) |
| Expenses | Recognition (debit) | Reversal (credit) |
Table 1. The meaning of Not and Hat and the resulting side, by account division.
This table also appears as Table 2 in Record a withdrawal.
A contra account reverses the home side of its division before the direction is applied.
Usage: Base and hat base and Classify an account and check its side.
Debit and credit decomposition
Definition 8. An exchange algebra extends the redundant algebra with four decompositions. decL selects debit terms (left), and decR selects credit terms (right). The other two decompositions select by direction. decP selects terms with Hat, and decM selects terms with Not. For example, a decrease in cash is a credit, and an increase in an ordinary expense is a debit. A sum of terms is balanced when its exact debit and credit totals agree.
Usage: Split an entry into debits and credits and Read the debit and credit difference.
Transfers
Definition 9. A transfer table is an ordered collection of triples $(b_i,a_i,f_i)$, where $b_i$ is a source base, $a_i$ is a destination base, and $f_i:V\to V$ transforms a nonnegative amount. Applying the table moves the selected amounts from their source bases to their destination bases according to those transformations. The source base, destination base, and value transformation are separate parts of each rule.
A source wildcard matches any stored element. A destination wildcard retains the corresponding source element. For an accounting reclassification, the amount removed from the source equals the amount added at the destination.
The rules of the algebra layer are sorted by source pattern, as in the preprint. The accounting transfer table preserves written row order and permits a destination to match a later source, but rejects a match with the source of the same or an earlier row; see Transfers.
Usage: Transfers.
Notes
Definition 10. A note set $N$ is a totally ordered set with equality and a distinguished empty element $\mathrm{plank}\in N$. A note can contain a date, a description, or a transaction identifier.
Usage: Declare a note type.
Finite journals
Definition 11. A journal is a finite-support function $J:N\to E$, where $E$ is the exchange algebra. A note outside the support has value zero. Equivalently, the journal is the finite collection of its nonzero note-algebra pairs $(n,J(n))$. Equal notes identify the same algebra component.
Usage: Attach notes to the month.
Journal operations
Definition 12. Hat and scalar multiplication act separately on each note’s algebra, preserving its note. Addition combines the algebras at each equal note by redundant addition:
$$ (J_1+J_2)(n)=J_1(n)+J_2(n). $$
Aggregation combines the note components under the empty note. Its algebra is $\sum_{n\in N}J(n)$; applying bar to that combined algebra gives the journal bar. The norm counts all amounts across notes:
$$ \operatorname{norm}(J)=\operatorname{norm}\left(\sum_{n\in N}J(n)\right). $$
A note projection selects note keys; a base projection selects terms within the selected notes. A joint projection restricts both. Neither projection requires aggregation or cancellation.
Usage: Select by note, base, and date and Total amounts and combine notes.
Definitions 1–12 follow Appendix A of Accounting State Space as a Foundation for Economic Agent-Based Modeling: An Input–Output Benchmark and Inventory Extension, with the query matching and norm clarification stated in Definitions 1 and 6.