Elements
Add or change the element types in the program from Record a withdrawal. This chapter adds a bank element and shows how to query it.
Add a bank element
Add a Bank type to distinguish deposits held at different banks. Its wildcard follows the Element structure of Definition 1.
-- | The bank where a deposit is held.
data Bank = BankA | BankB | Bank
deriving (Eq, Ord, Show, Generic, Hashable)
instance Element Bank where
wildcard = Bank
BankA and BankB identify banks. The Bank constructor is the wildcard. The derived instances supply equality, ordering, display, and hashing. GHC supplies Typeable. For this new element type, implement wildcard.
Add Bank as the third type in (AccountTitles, Unit, Bank). Tuples of two through seven element types have Element instances. The cash term has no applicable bank, so its base stores Bank. The deposit term stores BankA.
-- | Cash has no applicable bank.
cashBase :: (AccountTitles, Unit, Bank)
cashBase = (Cash, Yen, Bank)
-- | The deposit is held at Bank A.
depositBase :: (AccountTitles, Unit, Bank)
depositBase = (Deposits, Yen, BankA)
Use these bases in the two terms of the withdrawal.
-- | The withdrawal using bases with a bank element.
bankWithdrawal :: Alg MoneyDecimal (HatBase (AccountTitles, Unit, Bank))
bankWithdrawal = 3000 .@ Not :< cashBase
.+ 3000 .@ Hat :< depositBase
Query bases by their elements
In a query, (.#) stands for the wildcard of the inferred element type. Use it to leave the bank unrestricted. Import matchesQuery to compare a query directly with a stored base.
import ExchangeAlgebra.Algebra.Element (matchesQuery)
-- | Select terms on accounts at Bank A.
bankATerms :: Alg MoneyDecimal (HatBase (AccountTitles, Unit, Bank))
bankATerms = proj [HatNot :< ((.#), (.#), BankA)] bankWithdrawal
-- | Check the cash base with an unrestricted or a specific bank query.
cashMatchesAnyBank, cashMatchesBankA :: Bool
cashMatchesAnyBank = matchesQuery (Cash, Yen, (.#)) cashBase
cashMatchesBankA = matchesQuery (Cash, Yen, BankA) cashBase
The projection selects the deposit term at Bank A. The cash base matches the unrestricted bank query. A query for the specific value BankA skips that cash base because its bank element is the stored wildcard.
Save the complete program as Main.hs, then use the commands in Run the program.
Complete program
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
module Main where
import ExchangeAlgebra hiding (Yen, Dollar)
import ExchangeAlgebra.Algebra.Element (matchesQuery)
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 has no applicable bank.
cashBase :: (AccountTitles, Unit, Bank)
cashBase = (Cash, Yen, Bank)
-- | The deposit is held at Bank A.
depositBase :: (AccountTitles, Unit, Bank)
depositBase = (Deposits, Yen, BankA)
-- | The withdrawal using bases with a bank element.
bankWithdrawal :: Alg MoneyDecimal (HatBase (AccountTitles, Unit, Bank))
bankWithdrawal = 3000 .@ Not :< cashBase
.+ 3000 .@ Hat :< depositBase
-- | Select terms on accounts at Bank A.
bankATerms :: Alg MoneyDecimal (HatBase (AccountTitles, Unit, Bank))
bankATerms = proj [HatNot :< ((.#), (.#), BankA)] bankWithdrawal
-- | Check the cash base with an unrestricted or a specific bank query.
cashMatchesAnyBank, cashMatchesBankA :: Bool
cashMatchesAnyBank = matchesQuery (Cash, Yen, (.#)) cashBase
cashMatchesBankA = matchesQuery (Cash, Yen, BankA) cashBase
-- | Print a result with the question or operation that produced it.
printNamed :: Show a => String -> a -> IO ()
printNamed label value = putStrLn (label ++ ": " ++ show value)
-- | Print the deposit terms selected by bank and the cash base checks.
main :: IO ()
main = do
printNamed "Bank A terms" bankATerms
printNamed "Any-bank cash query matches cash without a bank" cashMatchesAnyBank
printNamed "Bank A cash query matches cash without a bank" cashMatchesBankA
The program prints the results below.
Bank A terms: 3000:@Hat:<(Deposits,Yen,BankA)
Any-bank cash query matches cash without a bank: True
Bank A cash query matches cash without a bank: False
Tested with exchangealgebra-0.6.1.1 on 2026-11-23.
Query a date stored as an ordinary element
Wild a adds the wildcard Wild to the element type a. Wrap an ordinary element x in Exactly x.
Wrap the date 1858-11-17 in Exactly to store it in a cash term.
-- | Record a cash increase on 1858-11-17.
record :: Alg MoneyDecimal (HatBase (AccountTitles, Wild Day))
record = 100 .@ Not :< (Cash, Exactly (ModifiedJulianDay 0))
main :: IO ()
main = do
putStrLn $ "Recorded term: " ++ show record
putStrLn $ "Any date: " ++ show (norm (proj [HatNot :< (Cash, Wild)] record))
putStrLn $ "1858-11-17: "
++ show (norm (proj [HatNot :< (Cash, Exactly (ModifiedJulianDay 0))] record))
putStrLn $ "1858-11-18: "
++ show (norm (proj [HatNot :< (Cash, Exactly (ModifiedJulianDay 1))] record))
Save the complete program as Main.hs.
Complete program
module Main where
import Data.Time.Calendar (Day (ModifiedJulianDay))
import ExchangeAlgebra
-- | Record a cash increase on 1858-11-17.
record :: Alg MoneyDecimal (HatBase (AccountTitles, Wild Day))
record = 100 .@ Not :< (Cash, Exactly (ModifiedJulianDay 0))
main :: IO ()
main = do
putStrLn $ "Recorded term: " ++ show record
putStrLn $ "Any date: " ++ show (norm (proj [HatNot :< (Cash, Wild)] record))
putStrLn $ "1858-11-17: "
++ show (norm (proj [HatNot :< (Cash, Exactly (ModifiedJulianDay 0))] record))
putStrLn $ "1858-11-18: "
++ show (norm (proj [HatNot :< (Cash, Exactly (ModifiedJulianDay 1))] record))
Add time to your project’s dependencies. From the project directory, run stack exec -- ghc -Wall -Werror -package time Main.hs -o wild-day, then run ./wild-day.
Recorded term: 100:@Not:<(Cash,Exactly 1858-11-17)
Any date: 100
1858-11-17: 100
1858-11-18: 0
The Wild query selects the term. An Exactly query selects the term only when its date matches.
Tested with exchangealgebra-0.6.1.1 on 2026-11-20.
Reference
Element methods
Element requires Eq, Ord, Show, Hashable, and Typeable. Equality and ordering must agree, and equal values must have equal hashes. Identity and ordering treat the wildcard as an ordinary value. The derived order for Bank is BankA < BankB < Bank.
| Method or function | Meaning |
|---|---|
wildcard, or the alias (.#) | The distinguished wildcard value of the inferred type. |
isWildcard x | Whether x equals the whole wildcard value. |
haveWildcard x | Whether x contains a wildcard element, including inside a tuple. |
ignoreWildcard before after | Use each element of after unless it is a wildcard, in which case keep the corresponding element of before. |
equal, (.==), (./=) | Symmetric wildcard-aware comparison and its negation. (.==) also handles partially wildcarded tuples. |
compareElement | Return EQ for a wildcard match, otherwise compare with the ordinary order. Compare tuples element by element. |
(.<), (.>), (.<=), (.>=), maxElement, minElement | Comparisons and selection using compareElement. |
matchesQuery query entry | One-way query matching. Import it from ExchangeAlgebra.Algebra.Element. |
wildcard is the only required method for a simple element type. Tuples implement wildcard substitution and matching component by component. In a transfer target, a wildcard preserves the source element.
Caution:
(.==)is symmetric. UsematchesQueryto match queries against stored elements or bases. Use ordinary==andcomparefor identity and canonical ordering; wildcard-aware comparison is not a total order on stored values.
Built-in instances
| Type | Wildcard |
|---|---|
AccountTitles | AccountTitle. Import the account type from ExchangeAlgebra or ExchangeAlgebra.Accounting.Account. |
CountUnit | CountUnit; other constructors are Yen, Dollar, Euro, CNY, Amount. |
Text | The empty text. Name and Subject are aliases for Text. You can use Subject to identify which accounting entity a record belongs to. |
Day | ModifiedJulianDay 0 (1858-11-17). To use that date as an ordinary element, use Wild Day. |
TimeOfDay | midnight (00:00). To use 00:00 as an ordinary element, use Wild TimeOfDay. |
Wild a | Wild. Each value x of a is the ordinary element Exactly x. Added in 0.6.1.0. |
| Tuples of 2 through 7 element types | A tuple in which every element is a wildcard. |
Hat | HatNot, for direction patterns. |
HatBase a, for BaseClass a | HatNot :< wildcard. |
BaseForSingleHat | Its sole constructor, BaseForSingleHat, used by the bare Hat base. |
Int, Integer, Double, and String require a wrapper to serve as element types in 0.6.1.1. Use Wild to give each type a wildcard.
Wild a has Element and BaseClass instances when a has Ord, Show, Hashable, and Typeable instances. It derives Eq, Ord, Show, and Generic, and has NFData, Hashable, and Binary instances when a has the corresponding instance.
For example, Wild Int can identify branches by number. Int has no Element instance, so a base that contains Int without Wild does not compile. A query for Exactly 1 selects branch 1, and a query for Wild selects every branch:
-- | Cash at branches 1 and 2.
branches :: Alg MoneyDecimal (HatBase (AccountTitles, Wild Int))
branches = 100 .@ Not :< (Cash, Exactly 1)
.+ 200 .@ Not :< (Cash, Exactly 2)
-- | Total the cash at branch 1 and at any branch.
branchQueries :: IO ()
branchQueries = do
putStrLn $ "Branch 1: " ++ show (norm (proj [HatNot :< (Cash, Exactly 1)] branches))
putStrLn $ "Any branch: " ++ show (norm (proj [HatNot :< (Cash, Wild)] branches))
Branch 1: 100
Any branch: 300
The Element instance for TimeOfDay uses 00:00 as its wildcard. A query for 00:00 therefore matches every time of day. A term stored at 00:00 means that no time of day applies, so a query for a specific time, such as 13:00, does not match it.
The following record receives cash at 00:00 and at 13:00 with TimeOfDay. The query for 00:00 selects both terms, and the query for 13:00 selects only the term at 13:00:
-- | Cash received at 00:00 and at 13:00, with the time as TimeOfDay.
byTime :: Alg MoneyDecimal (HatBase (AccountTitles, TimeOfDay))
byTime = 100 .@ Not :< (Cash, midnight)
.+ 200 .@ Not :< (Cash, TimeOfDay 13 0 0)
-- | Total the cash selected by a query for 00:00 and by a query for 13:00.
timeQueries :: IO ()
timeQueries = do
putStrLn $ "TimeOfDay, query 00:00: " ++ show (norm (proj [HatNot :< (Cash, midnight)] byTime))
putStrLn $ "TimeOfDay, query 13:00: "
++ show (norm (proj [HatNot :< (Cash, TimeOfDay 13 0 0)] byTime))
TimeOfDay, query 00:00: 300
TimeOfDay, query 13:00: 200
To record 00:00 as an ordinary time, use Wild TimeOfDay, added in 0.6.1.0. Store Exactly midnight, and query Exactly midnight to select only 00:00 or Wild to select any time. Day and Wild Day treat 1858-11-17 in the same way. The built-in base (AccountTitles, Name, CountUnit, Subject, Wild Day, Wild TimeOfDay) has an ExBaseClass instance; see Built-in exchange bases.
With Wild TimeOfDay, the same receipts are distinguished by time:
-- | The same receipts, with the time as Wild TimeOfDay.
byWildTime :: Alg MoneyDecimal (HatBase (AccountTitles, Wild TimeOfDay))
byWildTime = 100 .@ Not :< (Cash, Exactly midnight)
.+ 200 .@ Not :< (Cash, Exactly (TimeOfDay 13 0 0))
-- | Total the cash at 00:00, at 13:00, and at any time.
wildTimeQueries :: IO ()
wildTimeQueries = do
putStrLn $ "Wild TimeOfDay, query Exactly 00:00: "
++ show (norm (proj [HatNot :< (Cash, Exactly midnight)] byWildTime))
putStrLn $ "Wild TimeOfDay, query Exactly 13:00: "
++ show (norm (proj [HatNot :< (Cash, Exactly (TimeOfDay 13 0 0))] byWildTime))
putStrLn $ "Wild TimeOfDay, query Wild: "
++ show (norm (proj [HatNot :< (Cash, Wild)] byWildTime))
Wild TimeOfDay, query Exactly 00:00: 100
Wild TimeOfDay, query Exactly 13:00: 200
Wild TimeOfDay, query Wild: 300
The query for Exactly midnight selects only the term at 00:00. For dates, Query a date stored as an ordinary element shows the same use of Wild Day.
These excerpts come from one program. To run it, save the complete program below as Main.hs and add time to your project’s dependencies. From the project directory, run stack exec -- ghc -Wall -Werror -package time Main.hs -o wild-elements, then run ./wild-elements.
Complete program
module Main where
import Data.Time.LocalTime (TimeOfDay (TimeOfDay), midnight)
import ExchangeAlgebra
-- | Cash at branches 1 and 2.
branches :: Alg MoneyDecimal (HatBase (AccountTitles, Wild Int))
branches = 100 .@ Not :< (Cash, Exactly 1)
.+ 200 .@ Not :< (Cash, Exactly 2)
-- | Total the cash at branch 1 and at any branch.
branchQueries :: IO ()
branchQueries = do
putStrLn $ "Branch 1: " ++ show (norm (proj [HatNot :< (Cash, Exactly 1)] branches))
putStrLn $ "Any branch: " ++ show (norm (proj [HatNot :< (Cash, Wild)] branches))
-- | Cash received at 00:00 and at 13:00, with the time as TimeOfDay.
byTime :: Alg MoneyDecimal (HatBase (AccountTitles, TimeOfDay))
byTime = 100 .@ Not :< (Cash, midnight)
.+ 200 .@ Not :< (Cash, TimeOfDay 13 0 0)
-- | Total the cash selected by a query for 00:00 and by a query for 13:00.
timeQueries :: IO ()
timeQueries = do
putStrLn $ "TimeOfDay, query 00:00: " ++ show (norm (proj [HatNot :< (Cash, midnight)] byTime))
putStrLn $ "TimeOfDay, query 13:00: "
++ show (norm (proj [HatNot :< (Cash, TimeOfDay 13 0 0)] byTime))
-- | The same receipts, with the time as Wild TimeOfDay.
byWildTime :: Alg MoneyDecimal (HatBase (AccountTitles, Wild TimeOfDay))
byWildTime = 100 .@ Not :< (Cash, Exactly midnight)
.+ 200 .@ Not :< (Cash, Exactly (TimeOfDay 13 0 0))
-- | Total the cash at 00:00, at 13:00, and at any time.
wildTimeQueries :: IO ()
wildTimeQueries = do
putStrLn $ "Wild TimeOfDay, query Exactly 00:00: "
++ show (norm (proj [HatNot :< (Cash, Exactly midnight)] byWildTime))
putStrLn $ "Wild TimeOfDay, query Exactly 13:00: "
++ show (norm (proj [HatNot :< (Cash, Exactly (TimeOfDay 13 0 0))] byWildTime))
putStrLn $ "Wild TimeOfDay, query Wild: "
++ show (norm (proj [HatNot :< (Cash, Wild)] byWildTime))
main :: IO ()
main = do
branchQueries
timeQueries
wildTimeQueries
Tested with exchangealgebra-0.6.1.1 on 2026-11-23.