Multiple accounting entities
The January 10 withdrawal appears in two accounting records. The business receives 3,000 yen in cash and reduces its deposit asset. Bank A reduces the deposit liability it owes to the business and pays out 3,000 yen in cash. Add an element that identifies the accounting entity to the base to keep both records in one algebra while retaining whose accounts each term describes.
Use exchangealgebra-0.6.1.1 in the project prepared in Add the package to a Stack project. The example extends Record the withdrawal from HatBase (AccountTitles, Unit) to HatBase (AccountTitles, Owner, Unit). The same algebra operations apply to bases that also identify the accounting entity.
Add an element type for accounting entities
Save the code blocks in their displayed order as Main.hs. Retain the currency declaration:
Common setup
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
module Main where
import ExchangeAlgebra hiding (Yen, Dollar)
-- | Currency elements for the withdrawal and exchange records.
data Unit
= Yen -- ^ Japanese yen.
| Dollar -- ^ US dollars.
| Unit -- ^ Inapplicable in an entry; unrestricted in a query.
deriving (Eq, Ord, Show, Generic, Hashable)
instance Element Unit where
wildcard = Unit
Declare the accounting entities and account access for the expanded base:
-- | Owners of the two accounting records.
data Owner
= Business -- ^ The business withdrawing its own deposit.
| BankA -- ^ The bank paying out the withdrawal.
| Owner -- ^ Inapplicable in an entry; unrestricted in a query.
deriving (Eq, Ord, Show, Generic, Hashable)
instance Element Owner where
wildcard = Owner
-- | Account access for bases with an owner and a currency.
instance ExBaseClass (HatBase (AccountTitles, Owner, Unit)) where
getAccountTitle (_ :< (account, _, _)) = account
setAccountTitle (direction :< (_, owner, unit)) account = direction :< (account, owner, unit)
Business and BankA identify the accounting entities. Owner is the wildcard element, used in queries to select both entities. getAccountTitle reads the account element, and setAccountTitle changes only that element. This custom base shape needs its own ExBaseClass instance; the Alg exchange operations are unchanged.
Record both sides of the withdrawal
Construct each entity’s balanced record, then add the records:
-- | The business receives cash and reduces its deposit asset.
businessRecord :: Alg MoneyDecimal (HatBase (AccountTitles, Owner, Unit))
businessRecord = 3000 .@ Not :< (Cash, Business, Yen)
.+ 3000 .@ Hat :< (Deposits, Business, Yen)
-- | Bank A reduces its deposit liability and pays out cash.
bankRecord :: Alg MoneyDecimal (HatBase (AccountTitles, Owner, Unit))
bankRecord = 3000 .@ Hat :< (DepositPayable, BankA, Yen)
.+ 3000 .@ Hat :< (Cash, BankA, Yen)
-- | Retain both owners' records in one algebra without canceling between owners.
combined :: Alg MoneyDecimal (HatBase (AccountTitles, Owner, Unit))
combined = businessRecord .+ bankRecord
The business debits Cash and credits Deposits. Bank A debits DepositPayable, an ordinary liability account for deposits accepted, and credits Cash. The bank’s liability decrease has direction Hat but is a debit because an ordinary liability’s home side is credit. Both bank terms therefore use Hat, yet they fall on opposite debit and credit sides.
DepositPayable describes a bank deposit liability. DepositsReceived describes other deposits held, so the bank record uses DepositPayable. Neither entity records revenue or expense for this withdrawal. Every amount remains nonnegative.
Select entities and corresponding terms
Restrict the accounting entity element to read one entity’s record. Select the deposit asset and liability with separate patterns:
-- | Select one owner's complete accounting record.
recordOf :: Owner -> Alg MoneyDecimal (HatBase (AccountTitles, Owner, Unit))
recordOf owner = proj [HatNot :< ((.#), owner, (.#))] combined
-- | Select the business deposit asset and the bank's corresponding liability.
depositPair :: Alg MoneyDecimal (HatBase (AccountTitles, Owner, Unit))
depositPair = proj [ HatNot :< (Deposits, Business, Yen)
, HatNot :< (DepositPayable, BankA, Yen) ] combined
recordOf Business contains both business terms, and recordOf BankA contains both bank terms. The cash increase of the business corresponds to the bank’s cash decrease. The business’s deposit asset decrease corresponds to the bank’s deposit liability decrease. These correspondences concern the same transaction, but the full bases differ in entity or account, so the terms do not cancel automatically.
Check each entity’s record
Print the entity selections, balance checks, and corresponding movements:
-- | Print a result with the question or operation that produced it.
printNamed :: Show a => String -> a -> IO ()
printNamed label value = putStrLn (label ++ ": " ++ show value)
-- | Inspect each owner and the corresponding cash and deposit movements.
main :: IO ()
main = do
printNamed "Business record" (recordOf Business)
printNamed "Bank A record" (recordOf BankA)
printNamed "Business record is balanced" (balance (recordOf Business))
printNamed "Bank A record is balanced" (balance (recordOf BankA))
printNamed "Combined record is balanced" (balance combined)
printNamed "Bank liability decrease side" (whichSide (Hat :< (DepositPayable, BankA, Yen)))
printNamed "Corresponding deposit changes" depositPair
printNamed "Cash changes with owners retained" (proj [HatNot :< (Cash, (.#), Yen)] combined)
printNamed "Bar retains separate owners" (bar combined)
Run stack exec -- ghc -Wall -Werror Main.hs -o agents-example, then run ./agents-example in your project directory. The program prints:
Business record: 3000:@Not:<(Cash,Business,Yen) .+ 3000:@Hat:<(Deposits,Business,Yen)
Bank A record: 3000:@Hat:<(Cash,BankA,Yen) .+ 3000:@Hat:<(DepositPayable,BankA,Yen)
Business record is balanced: True
Bank A record is balanced: True
Combined record is balanced: True
Bank liability decrease side: Debit
Corresponding deposit changes: 3000:@Hat:<(Deposits,Business,Yen) .+ 3000:@Hat:<(DepositPayable,BankA,Yen)
Cash changes with owners retained: 3000:@Not:<(Cash,Business,Yen) .+ 3000:@Hat:<(Cash,BankA,Yen)
Bar retains separate owners: 3000:@Not:<(Cash,Business,Yen) .+ 3000:@Hat:<(Cash,BankA,Yen) .+ 3000:@Hat:<(Deposits,Business,Yen) .+ 3000:@Hat:<(DepositPayable,BankA,Yen)
The business, bank, and combined records are balanced in yen. Check each entity individually: a combined balance alone could conceal an imbalance in one entity offset by another. Bar retains separate owners still contains both entities’ cash terms because bar cancels only within a full base, including the accounting entity element.
The accounting entity element identifies the entity whose record contains the term. Counterparty identity is separate. With only one business and one bank the correspondence is specified by the example. To identify particular pairs in a larger set of transactions, add the counterparty or a shared transaction identifier to the note type, as described in Declare a note type.
Tested with exchangealgebra-0.6.1.1 on 2026-11-20.
Reference
| Construction or operation | Inputs and result |
|---|---|
HatBase (AccountTitles, Owner, Unit) | Identify the account, the accounting entity, and the currency. Direction is separate from the tuple. |
Element Owner | Supply a wildcard element and derive consistent equality, ordering, and hashing. |
ExBaseClass instance | Read and replace the account while preserving entity, currency, and direction. |
proj [HatNot :< ((.#), owner, (.#))] | Select one entity across accounts, directions, and currencies. |
proj [HatNot :< (Cash, (.#), Yen)] | Select yen cash movements across entities, retaining the accounting entity elements. |
balance (recordOf owner) | Compare debit and credit sums for the selected entity. The example uses only yen. |
bar combined | Net within each full base. Keep different entities separate. |
The accounting entity element set is an ordinary element set for a base under Definitions 1–3. The additional element of the base changes neither redundant addition nor the debit and credit decomposition. It changes which terms share a full base and which entities can be selected in a query.