Record a withdrawal
You need knowledge of double-entry bookkeeping and a Stack project prepared as described in Installation and modules. The program on this page records one journal entry in exchange algebra, separates its debits and credits, and checks that their totals agree. You can adapt it to your own entries without reading the chapters on customization.
Transaction and journal entry
On January 10, a business withdraws 3,000 yen from its savings account at Bank A and keeps it as cash. Table 1 shows the journal entry.
| Date | Description | Debit | Amount | Credit | Amount |
|---|---|---|---|---|---|
| January 10 | Savings account at Bank A | Cash | 3,000 | Deposits | 3,000 |
Table 1. The journal entry for the withdrawal, in yen.
Run the program
Save the following program as Main.hs in your project directory:
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
module Main where
import ExchangeAlgebra hiding (Yen, Dollar)
-- | The unit of an amount.
data Unit = Yen | Dollar | Unit
deriving (Eq, Ord, Show, Generic, Hashable)
instance Element Unit where
wildcard = Unit
-- | Read and replace the account title in a base.
instance ExBaseClass (HatBase (AccountTitles, Unit)) where
getAccountTitle (_ :< (account, _)) = account
setAccountTitle (direction :< (_, unit)) account = direction :< (account, unit)
-- | January 10: withdraw 3,000 yen from a savings account at Bank A.
withdrawal :: Alg MoneyDecimal (HatBase (AccountTitles, Unit))
withdrawal = 3000 .@ Not :< (Cash, Yen)
.+ 3000 .@ Hat :< (Deposits, Yen)
-- | Print the entry, its debit terms, its credit terms, and the balance check.
main :: IO ()
main = do
print withdrawal
print (decL withdrawal)
print (decR withdrawal)
print (balance withdrawal)
Run these commands from the project directory:
stack exec -- ghc -Wall -Werror Main.hs -o withdrawal
./withdrawal
The program prints:
3000:@Not:<(Cash,Yen) .+ 3000:@Hat:<(Deposits,Yen)
3000:@Not:<(Cash,Yen)
3000:@Hat:<(Deposits,Yen)
True
The following sections explain the program in the order in which its type is built: elements, a base and hat base, a redundant algebra, and an exchange algebra. Each section shows again the part of the program that it explains.
Elements: AccountTitles and Unit
An element is one piece of the information that identifies what changes. This entry uses two kinds of elements:
AccountTitlesis the built-in type of account titles.CashandDepositsare two of its values, andDepositsdenotes savings deposits.Unitis the type that the program declares for units.YenandDollarare units. The valueUnitis the wildcard: it means “any unit” in a query and “no applicable unit” in a recorded term.
The program imports the library and declares Unit:
import ExchangeAlgebra hiding (Yen, Dollar)
-- | The unit of an amount.
data Unit = Yen | Dollar | Unit
deriving (Eq, Ord, Show, Generic, Hashable)
instance Element Unit where
wildcard = Unit
The deriving clause and the instance Element Unit make the new type usable as an element type. The import hides the library’s Yen and Dollar because their names collide with the new values.
The program does not use the date or the bank as an element, so its balances do not distinguish dates or banks.
Base and hat base: HatBase (AccountTitles, Unit)
A base is a tuple of elements. (Cash, Yen) is the base for cash in yen, and (Deposits, Yen) is the base for savings deposits in yen. A hat base pairs a base with a direction through :<. The definition of withdrawal contains two hat bases, Not :< (Cash, Yen) and Hat :< (Deposits, Yen):
-- | January 10: withdraw 3,000 yen from a savings account at Bank A.
withdrawal :: Alg MoneyDecimal (HatBase (AccountTitles, Unit))
withdrawal = 3000 .@ Not :< (Cash, Yen)
.+ 3000 .@ Hat :< (Deposits, Yen)
The direction Not represents an increase, and Hat represents a decrease. For revenue and expenses, they represent recognition and reversal, respectively. The amount is nonnegative in both directions. The direction and the account’s division together determine the debit or credit side, as Table 2 shows.
| 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 2. The meaning of Not and Hat and the resulting side, by account division.
Cash and deposits are assets. Thus Not :< (Cash, Yen) is an increase in cash on the debit side, and Hat :< (Deposits, Yen) is a decrease in deposits on the credit side.
For a contra account, read the direction as a change in that account itself. Not on AccumulatedDepreciation is an increase in accumulated depreciation and falls on the credit side.
Note: The side that increases
CashOverShort,IncomeSummary, orSuspenseAccountdepends on the context. The library gives them the same sides as assets:Notis a debit andHatis a credit.
Redundant algebra: Alg
Alg MoneyDecimal (HatBase (AccountTitles, Unit)) is the type of a redundant algebra. Its first argument is the amount type, and its second is the hat base type. MoneyDecimal stores decimal amounts without binary rounding.
In the definition of withdrawal, .@ attaches an amount to a hat base to make a term, and .+ adds the two terms:
-- | January 10: withdraw 3,000 yen from a savings account at Bank A.
withdrawal :: Alg MoneyDecimal (HatBase (AccountTitles, Unit))
withdrawal = 3000 .@ Not :< (Cash, Yen)
.+ 3000 .@ Hat :< (Deposits, Yen)
The first line of the output is this sum. Addition keeps every term, even on the same base. To net the terms on each base, apply bar explicitly.
Exchange algebra: ExBaseClass
The instance of ExBaseClass defines where the account title is in the base. getAccountTitle reads the title, and setAccountTitle replaces it while keeping the direction and the unit. FlexibleInstances permits this instance declaration.
-- | Read and replace the account title in a base.
instance ExBaseClass (HatBase (AccountTitles, Unit)) where
getAccountTitle (_ :< (account, _)) = account
setAccountTitle (direction :< (_, unit)) account = direction :< (account, unit)
With this instance, the redundant algebra is an exchange algebra, and three more functions are available. main prints their results after the sum:
-- | Print the entry, its debit terms, its credit terms, and the balance check.
main :: IO ()
main = do
print withdrawal
print (decL withdrawal)
print (decR withdrawal)
print (balance withdrawal)
decLselects the debit (left) terms, which form the second line of the output.decRselects the credit (right) terms, which form the third line.balancecompares the debit and credit totals. The last line,True, means that they agree.
balance compares amounts only. It does not detect a wrong account title or convert between units, so apply it to terms in a single unit.
Use the program for your entry
To record another entry in yen, change three parts of each term in withdrawal:
- Replace the account title with one from Built-in account titles.
- Select
NotorHatfrom Table 2. - Replace
3000with a nonnegative amount..@stops with an error for a negative amount.
If the entry has more than two terms, join the additional terms with .+. Run the program again, and check that the last line is True.
If this form is sufficient, continue with Transfers. To customize it, read the chapter for the component that you want to change:
- To declare or add an element, read Elements.
- To change the combination of elements, read Bases.
- To change the amount type or use other operations, read Redundant and exchange algebras.
Tested with exchangealgebra-0.6.1.1 on 2026-11-22.