Journals
The January 10 withdrawal has a date and a description, “Withdrawal from Bank A.” Keep those facts in a journal note so they remain available for selecting transactions without including them as elements in the base used for account balances. A Journal n v b associates each note of type n with an Alg v b.
Use exchangealgebra-0.6.1.1 in the project prepared in Add the package to a Stack project. The example attaches notes to the month’s transactions from Add the month’s transactions, adds a February purchase, then selects entries by note, base, and date range. The note set, finite journal, and operations correspond to Definitions 10–12.
Declare a note type
Save the code blocks in their displayed order as Main.hs. Use the qualified journal import for constructors and queries, and import (.|) separately to attach notes.
Common imports and language extensions
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
module Main where
import ExchangeAlgebra hiding (Yen, Dollar)
import ExchangeAlgebra.Journal ((.|))
import qualified ExchangeAlgebra.Journal as Journal
Common declarations
-- | 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
-- | Account access for the custom account-and-currency base.
instance ExBaseClass (HatBase (AccountTitles, Unit)) where
getAccountTitle (_ :< (account, _)) = account
setAccountTitle (direction :< (_, unit)) account = direction :< (account, unit)
Declare a note containing the date and description:
-- | A year, month, and day, compared in calendar order for the valid dates used here.
-- This alias does not validate dates or distinguish them from other integer tuples.
type EntryDate = (Integer, Int, Int)
-- | Date and description shared by all terms in one journal entry.
data EntryNote = EntryNote
{ entryDate :: EntryDate -- ^ Calendar date of the transaction.
, entryDescription :: String -- ^ Human-readable description.
} deriving (Eq, Ord, Show, Generic, Hashable)
instance Journal.Note EntryNote where
plank = EntryNote (0, 0, 0) ""
-- | Metadata for the withdrawal; the blank note is reserved for aggregation.
withdrawalNote :: EntryNote
withdrawalNote = EntryNote (2026, 1, 10) "Withdrawal from Bank A"
EntryDate is a tuple ordered by year, month, and day. The example supplies valid dates directly; the alias performs no calendar validation. EntryNote derives equality, total ordering, display, and hashing. Typeable, another superclass of Note, is supplied automatically by GHC. Define the distinguished empty note with plank; the remaining methods have defaults.
The value EntryNote (0, 0, 0) "" is reserved for the empty note and is not a transaction date. plank is also the all-notes wildcard for projWithNote. A note identifies a group of terms, not necessarily a unique transaction: two entries with exactly the same date and description share a key and their terms are added. Include a transaction identifier in a note if those entries must remain separately selectable.
Attach notes to the month
Construct each transaction as an algebra, then attach its note once with .|:
Withdrawal definition
-- | January 10: withdraw 3,000 yen from Bank A's savings account into cash.
withdrawal :: Alg MoneyDecimal (HatBase (AccountTitles, Unit))
withdrawal = 3000 .@ Not :< (Cash, Yen)
.+ 3000 .@ Hat :< (Deposits, Yen)
-- | January 15: withdraw another 2,000 yen.
secondWithdrawal :: Alg MoneyDecimal (HatBase (AccountTitles, Unit))
secondWithdrawal = 2000 .@ Not :< (Cash, Yen)
.+ 2000 .@ Hat :< (Deposits, Yen)
-- | January 20: buy goods for 500 yen in cash.
cashPayment :: Alg MoneyDecimal (HatBase (AccountTitles, Unit))
cashPayment = 500 .@ Hat :< (Cash, Yen)
.+ 500 .@ Not :< (Purchases, Yen)
-- | January 25: exchange 1,500 yen for 10 dollars without a fee.
currencyExchange :: Alg MoneyDecimal (HatBase (AccountTitles, Unit))
currencyExchange = 1500 .@ Hat :< (Cash, Yen)
.+ 10 .@ Not :< (Cash, Dollar)
-- | February 1: buy goods for 400 yen in cash.
februaryPurchase :: Alg MoneyDecimal (HatBase (AccountTitles, Unit))
februaryPurchase = 400 .@ Not :< (Purchases, Yen)
.+ 400 .@ Hat :< (Cash, Yen)
-- | Metadata for the February purchase, excluded by the January date range.
februaryNote :: EntryNote
februaryNote = EntryNote (2026, 2, 1) "Purchase of goods"
-- | January transactions and one February purchase, each with a date and description.
transactions :: Journal.Journal EntryNote MoneyDecimal (HatBase (AccountTitles, Unit))
transactions = (withdrawal .| withdrawalNote)
.+ (secondWithdrawal .| EntryNote (2026, 1, 15) "Withdrawal from Bank A")
.+ (cashPayment .| EntryNote (2026, 1, 20) "Purchase of goods")
.+ (currencyExchange .| EntryNote (2026, 1, 25) "Yen-dollar exchange")
.+ (februaryPurchase .| februaryNote)
The two terms of the January 10 withdrawal share withdrawalNote. The January 15 withdrawal has a different date, so it remains separately selectable even though its description is the same. Adding journals with .+ keeps the notes and concatenates the term sequences at a shared note; it does not net the terms.
The January 20 purchase debits Purchases and credits Cash. The January 25 exchange reduces yen cash by 1,500 yen and increases dollar cash by 10 dollars at 150 yen per dollar, without a fee. Keep currencies separate when totaling amounts; the raw yen and dollar numbers are not a common-unit debit-credit comparison.
On February 1, the business buys another 400 yen of goods for cash. Record a debit to Purchases and a credit to Cash, both for 400 yen. transactions contains the four January entries and this February entry.
Select by note, base, and date
For a date range, test the date field with filterWithNote. Use an inclusive lower bound and an exclusive upper bound:
-- | Select entries with an inclusive lower and exclusive upper date.
isInJanuary :: EntryNote -> Alg MoneyDecimal (HatBase (AccountTitles, Unit)) -> Bool
isInJanuary note _ = (2026, 1, 1) <= entryDate note && entryDate note < (2026, 2, 1)
-- | Keep the notes while selecting the dates in January.
january :: Journal.Journal EntryNote MoneyDecimal (HatBase (AccountTitles, Unit))
january = Journal.filterWithNote isInJanuary transactions
january contains the four January entries and excludes the February 1 purchase. The predicate selects by date even when the descriptions differ. projWithNote [withdrawalNote] instead selects an exact note, including both its date and description. projWithBase selects terms while retaining their notes, and projWithNoteBase combines both restrictions.
Total amounts and combine notes
Print the February entry before and after date selection, the selected January records, the gross cash amount, and the cash balances:
-- | Print a result with the question or operation that produced it.
printNamed :: Show a => String -> a -> IO ()
printNamed label value = putStrLn (label ++ ": " ++ show value)
-- | Ask questions by note, base, and date, then combine notes for net balances.
main :: IO ()
main = do
printNamed "February 1 entry before date selection"
(Journal.projWithNote [februaryNote] transactions)
printNamed "February 1 entry after January selection"
(Journal.projWithNote [februaryNote] january)
printNamed "January 10 entry" (Journal.projWithNote [withdrawalNote] transactions)
printNamed "January 10 cash terms"
(Journal.projWithNoteBase [withdrawalNote] [HatNot :< (Cash, Yen)] transactions)
printNamed "January yen cash terms"
(Journal.projWithBase [HatNot :< (Cash, Yen)] january)
printNamed "January gross yen cash amount"
(norm (Journal.projWithBase [HatNot :< (Cash, Yen)] january))
printNamed "January cash balances by currency"
(proj [HatNot :< (Cash, (.#))] (bar (Journal.toAlg january)))
printNamed "Gathered January record" (Journal.gather Journal.plank january)
printNamed "Bar across notes at plank" (bar january)
Run stack exec -- ghc -Wall -Werror Main.hs -o journal-example, then run ./journal-example in your project directory. The program prints:
February 1 entry before date selection: 400:@Not:<(Purchases,Yen) .+ 400:@Hat:<(Cash,Yen) .| EntryNote {entryDate = (2026,2,1), entryDescription = "Purchase of goods"}
February 1 entry after January selection: 0
January 10 entry: 3000:@Hat:<(Deposits,Yen) .+ 3000:@Not:<(Cash,Yen) .| EntryNote {entryDate = (2026,1,10), entryDescription = "Withdrawal from Bank A"}
January 10 cash terms: 3000:@Not:<(Cash,Yen) .| EntryNote {entryDate = (2026,1,10), entryDescription = "Withdrawal from Bank A"}
January yen cash terms: (500:@Hat:<(Cash,Yen) .| EntryNote {entryDate = (2026,1,20), entryDescription = "Purchase of goods"}) .+ (3000:@Not:<(Cash,Yen) .| EntryNote {entryDate = (2026,1,10), entryDescription = "Withdrawal from Bank A"}) .+ (2000:@Not:<(Cash,Yen) .| EntryNote {entryDate = (2026,1,15), entryDescription = "Withdrawal from Bank A"}) .+ (1500:@Hat:<(Cash,Yen) .| EntryNote {entryDate = (2026,1,25), entryDescription = "Yen-dollar exchange"})
January gross yen cash amount: 7000
January cash balances by currency: 3000:@Not:<(Cash,Yen) .+ 10:@Not:<(Cash,Dollar)
Gathered January record: 500:@Hat:<(Cash,Yen) .+ 1500:@Hat:<(Cash,Yen) .+ 2000:@Not:<(Cash,Yen) .+ 3000:@Not:<(Cash,Yen) .+ 10:@Not:<(Cash,Dollar) .+ 2000:@Hat:<(Deposits,Yen) .+ 3000:@Hat:<(Deposits,Yen) .+ 500:@Not:<(Purchases,Yen)
Bar across notes at plank: 3000:@Not:<(Cash,Yen) .+ 10:@Not:<(Cash,Dollar) .+ 5000:@Hat:<(Deposits,Yen) .+ 500:@Not:<(Purchases,Yen)
February 1 entry before date selection contains the 400-yen purchase with its February date. February 1 entry after January selection is 0, so the purchase is absent from the January selection.
January gross yen cash amount is 7,000 yen: increases of 3,000 and 2,000 plus decreases of 500 and 1,500. norm counts all selected amounts in both directions. For January cash balances by currency, toAlg removes the note keys without netting, and bar then cancels within each account-and-currency base. The resulting cash changes are 3,000 yen and 10 dollars.
Journal.gather Journal.plank january combines all entries under the empty note without canceling opposing terms. bar january combines the notes under plank and nets the combined algebra. It loses the original dates and descriptions. A component under plank is printed without a note suffix, but the value remains a Journal. To net separately within each note, use Journal.map bar january; retain january itself when the original term detail is required.
Tested with exchangealgebra-0.6.1.1 on 2026-11-20.
Reference
| Type or operation | Inputs and result |
|---|---|
Note n | Requires Show, Eq, Ord, Hashable, and Typeable. Define plank; isPlank defaults to equality with plank. |
algebra .| note | Attach one note to all terms of an algebra. A zero algebra produces an empty journal. |
Journal.fromList entries | Sum a list of journals, combining equal note keys with redundant addition. |
Journal.sigma items f | Sum the journals produced by applying f to the items. |
Journal.projWithNote notes journal | Select exact note keys. A list containing plank selects all notes; an empty list selects none. |
Journal.projWithBase bases journal | Select matching terms at every note. Drop a note whose selected algebra is zero. |
Journal.projWithNoteBase notes bases journal | Restrict notes and bases. An empty note list or a list containing plank imposes no note restriction. An empty base list selects nothing. |
Journal.filterWithNote predicate journal | Keep note-algebra pairs satisfying the predicate. |
Journal.toAlg journal | Remove note keys and add their algebras, retaining term amounts without netting. |
Journal.gather note journal | Put the combined algebra under the specified note, without netting. |
bar journal | Gather under plank, then net the combined algebra. |
Journal.map f journal | Apply an algebra transformation separately at each note. |
Hat and scalar multiplication preserve note keys. compress aggregates within each note, while norm totals all amounts across notes. An account projection of the raw journal retains opposing amounts in different notes. Combine the notes before reading a balance that must cancel an original entry against an adjustment recorded under a different note.