Bases
Change the base in Record a withdrawal to include the bank element. The base type determines which elements identify each term and which elements a query can match.
Add an element to the base
Use the triple (AccountTitles, Unit, Bank) as the underlying base and pair it with a direction using (:<). These two types implement Definition 2 and Definition 3.
-- | Cash increases with no applicable bank.
cashIncrease :: HatBase (AccountTitles, Unit, Bank)
cashIncrease = Not :< (Cash, Yen, Bank)
-- | Deposits at Bank A decrease.
depositDecrease :: HatBase (AccountTitles, Unit, Bank)
depositDecrease = Hat :< (Deposits, Yen, BankA)
The library supplies BaseClass for this triple because all three types have Element instances. It also supplies HatBaseClass for the resulting HatBase.
Declare the base instance
Update ExBaseClass to read and replace the first element of the triple. Keep the direction, unit, and bank when replacing the account title.
-- | Read or replace the title in a base that includes a bank.
instance ExBaseClass (HatBase (AccountTitles, Unit, Bank)) where
getAccountTitle (_ :< (account, _, _)) = account
setAccountTitle (direction :< (_, unit, bank)) account = direction :< (account, unit, bank)
FlexibleInstances permits this instance head. Implement getAccountTitle and setAccountTitle for a custom exchange base. The other methods have defaults based on the account title and direction, and the Exchange instance for Alg uses them.
Built-in exchange bases
In 0.6.1.1, the following forms of HatBase a have built-in ExBaseClass instances.
a in HatBase a |
|---|
AccountTitles |
(AccountTitles, Day) |
(AccountTitles, Name) |
(CountUnit, AccountTitles) |
(AccountTitles, Name, CountUnit) |
(AccountTitles, Name, CountUnit, Subject) |
(AccountTitles, Name, CountUnit, Subject, Day) |
(AccountTitles, Name, CountUnit, Subject, Day, TimeOfDay) |
(AccountTitles, Wild Day) |
(AccountTitles, Name, CountUnit, Subject, Wild Day) |
(AccountTitles, Name, CountUnit, Subject, Wild Day, Wild TimeOfDay) |
Use a single unit as a base
To use Unit itself as the underlying base, declare BaseClass Unit. The empty instance uses the default compareBase implementation. The resulting HatBase Unit can identify an amount in yen without an account title.
-- | Permit Unit itself, as well as tuples containing it, to be a base.
instance BaseClass Unit where
-- | A base with one element, used to compare the instance requirements.
yenOnly :: HatBase Unit
yenOnly = Not :< Yen
Inspect the withdrawal bases
Use base and hat to read the elements and direction. revHat reverses the cash direction and leaves (Cash, Yen, Bank) intact. merge reconstructs the deposit hat base from the direction and elements. Replacing Deposits with Cash through setAccountTitle preserves Hat, Yen, and BankA in the example.
-- | Inspect the direction, account, unit, and bank of the example bases.
main :: IO ()
main = do
printNamed "Cash increase" cashIncrease
printNamed "Deposit decrease" depositDecrease
printNamed "Elements of the cash increase" (base cashIncrease)
printNamed "Direction of the deposit change" (hat depositDecrease)
printNamed "Reverse the cash direction" (revHat cashIncrease)
printNamed "Rebuild the deposit decrease"
(merge (hat depositDecrease) (base depositDecrease) :: HatBase (AccountTitles, Unit, Bank))
printNamed "Replace the account title" (setAccountTitle depositDecrease Cash)
printNamed "Single-element yen base" yenOnly
Save the complete program as Main.hs, then use the commands in Run the program.
Complete program
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
module Main where
import ExchangeAlgebra hiding (Yen, Dollar)
data Unit = Yen | Dollar | Unit
deriving (Eq, Ord, Show, Generic, Hashable)
instance Element Unit where
wildcard = Unit
-- | The bank where a deposit is held.
data Bank = BankA | BankB | Bank
deriving (Eq, Ord, Show, Generic, Hashable)
instance Element Bank where
wildcard = Bank
-- | Cash increases with no applicable bank.
cashIncrease :: HatBase (AccountTitles, Unit, Bank)
cashIncrease = Not :< (Cash, Yen, Bank)
-- | Deposits at Bank A decrease.
depositDecrease :: HatBase (AccountTitles, Unit, Bank)
depositDecrease = Hat :< (Deposits, Yen, BankA)
-- | Read or replace the title in a base that includes a bank.
instance ExBaseClass (HatBase (AccountTitles, Unit, Bank)) where
getAccountTitle (_ :< (account, _, _)) = account
setAccountTitle (direction :< (_, unit, bank)) account = direction :< (account, unit, bank)
-- | Permit Unit itself, as well as tuples containing it, to be a base.
instance BaseClass Unit where
-- | A base with one element, used to compare the instance requirements.
yenOnly :: HatBase Unit
yenOnly = Not :< Yen
-- | 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 the direction, account, unit, and bank of the example bases.
main :: IO ()
main = do
printNamed "Cash increase" cashIncrease
printNamed "Deposit decrease" depositDecrease
printNamed "Elements of the cash increase" (base cashIncrease)
printNamed "Direction of the deposit change" (hat depositDecrease)
printNamed "Reverse the cash direction" (revHat cashIncrease)
printNamed "Rebuild the deposit decrease"
(merge (hat depositDecrease) (base depositDecrease) :: HatBase (AccountTitles, Unit, Bank))
printNamed "Replace the account title" (setAccountTitle depositDecrease Cash)
printNamed "Single-element yen base" yenOnly
Cash increase: Not:<(Cash,Yen,Bank)
Deposit decrease: Hat:<(Deposits,Yen,BankA)
Elements of the cash increase: (Cash,Yen,Bank)
Direction of the deposit change: Hat
Reverse the cash direction: Hat:<(Cash,Yen,Bank)
Rebuild the deposit decrease: Hat:<(Deposits,Yen,BankA)
Replace the account title: Hat:<(Cash,Yen,BankA)
Single-element yen base: Not:<Yen
Caution:
Hatrepresents a direction on a nonnegative amount. The account and direction together determine the debit or credit side. UseHatNotin query patterns. Some direction tests and reversal methods fail onHatNot.
Tested with exchangealgebra-0.6.1.1 on 2026-11-23.
Reference
BaseClass
BaseClass a requires Element a and provides compareBase :: a -> a -> Ordering, defaulting to compareElement. Built-in base types with a single element include AccountTitles, CountUnit, Text (also called Name), Day, TimeOfDay, and Wild a when a has Ord, Show, Hashable, and Typeable instances. Tuples of 2 through 7 element types have BaseClass instances even when individual element types have no separate BaseClass instance.
HatBaseClass
HatBaseClass requires BaseClass for both the whole hat base and its BasePart, and AxisDecompose for BasePart. AxisDecompose supplies a key for each element of the base. Its generic and tuple instances cover the bases used in the withdrawal.
| Associated type or method | Purpose |
|---|---|
BasePart a | The base type after removing the direction. For HatBase a, this is a. |
base, hat | Extract the underlying base and direction. |
merge | Build a hat base from a direction and a base part. |
toHat, toNot | Set the direction while retaining the base. |
revHat | Exchange Hat and Not. |
isHat, isNot | Test the direction. |
compareHatBase | Compare hat bases; its default is compareBase. |
A custom representation must implement BasePart, base, hat, merge, toHat, toNot, revHat, isHat, and isNot. Reconstruction must preserve the underlying base and direction. Reversing twice must restore a concrete base. HatBase a already implements these operations. The bare Hat type has an instance with the single-value BaseForSingleHat as its base part.