Linear Resource Types (Capital<T>, Account<K, T> & Move Semantics)
The heart of Fin# is its Linear Flow Analyzer (LinearFlowAnalyzer in FinSharp.Compiler). Inspired by substructural linear logic (Girard, 1987) and quantum no-cloning / no-deleting theorems in Q#, Fin# splits data types into two worlds:
- Ordinary Unrestricted Value Types (
Int64,String,Date,Bool,DecimalQuantity): Can be copied, inspected, and dropped freely—just like values in C# or F#. - Linear Monetary Resource Types (
Capital<TCurrency>): Represent conserved units of purchasing power or commodity obligation. EveryCapital<TCurrency>binding must be consumed exactly once along every control-flow path!
Rules of Capital<TCurrency>
A binding of type Capital<USD> obeys three compile-time substructural laws:
- No Implicit
Copy: Passingpaymenttodebit RentExpense <- payment;moves ownership into the posting and markspaymentas Consumed. Referencingpaymenta second time fails compilation! - No Silent
Drop(FIN0005): Allowing aCapital<T>variable—including anif/elsebranch or a0.01_USDsplit remainder—to go out of scope unconsumed fails compilation withFIN0005(UnconsumedLinearCapitalError). - No Unpaired Literal Minting (
FIN0001): Outside atransactioncredit leg or an explicitopening_balance(...)boundary declaration, code cannot conjureCapital<USD>out of a bare integer or string.
transaction SplitRentThreeWays [audit: Z_SOX_302, date: "2015-09-01"] {
// Source $1,000.00 linearly from Checking:
let totalRent : Capital<USD> = credit OperatingChecking -> 1_000.00_USD;
// $1,000.00 / 3 = $333.33 * 3 + $0.01 remainder!
// FloorWithRemainder returns BOTH the 3 equal shares AND the 1-cent linear remainder:
let (shares, remainder) = capital.splitEqual(totalRent, 3, RoundingPolicy.FloorWithRemainder);
debit RoommateShareA <- shares[0]; // $333.33
debit RoommateShareB <- shares[1]; // $333.33
debit RoommateShareC <- shares[2]; // $333.33
// REQUIRED: If we forget this line, finsc halts with FIN0005 because $0.01 was dropped!
debit RoundingAdjustmentPool <- remainder; // $0.01 -> Total Debits == $1,000.00!
}
Strongly Typed T-Accounts (Account<TKind, TCurrency>)
Every ledger account carries two compile-time type parameters:
TKind Marker Type | Normal Balance | Accounting Equation Side | Increases On | Decreases On |
|---|---|---|---|---|
Asset | Debit | Left (Assets + Expenses) | debit | credit |
Expense | Debit | Left (Assets + Expenses) | debit | credit |
Liability | Credit | Right (Liabilities + Equity + Revenue) | credit | debit |
Equity | Credit | Right (Liabilities + Equity + Revenue) | credit | debit |
Revenue | Credit | Right (Liabilities + Equity + Revenue) | credit | debit |
Because TKind is part of the static type signature, a helper function expecting an Account<Expense, USD> will never accidentally post into an Account<Liability, USD>!
128-Bit Scaled Fixed-Point Arithmetic (FIN0003)
In binary IEEE 754 floating-point (float / double), 0.1 + 0.2 evaluates to 0.30000000000000004. Across millions of ledger rows, binary rounding drift corrupts trial balances.
- Compile-Time Float Ban (
FIN0003): Attempting to multiply, divide, or compareCapital<T>,Account<K, T>, orRate<A, B>withfloatordoubleis a hard compile error (FIN0003). - Exact
Int128Fixed-Point Representation: Internally,FinSharp.Runtimerepresents monetary amounts as signed 128-bit integers (System.Int128on.NET 10 / 11) scaled by10⁸(100,000,000atomic ticks per major currency unit—supporting sub-basis-point interest and FX math while guaranteeing exact cent reconciliation at posting boundaries). - Explicit
RoundingPolicyEnum: RoundingPolicy.HalfEven/RoundingPolicy.Bankers— Round half to even, returning(roundedShare, adjustmentDelta)inside settlement combinators.RoundingPolicy.FloorWithRemainder— Deterministic floor division returning(shares, exactRemainder)as linearCapital<T>values.
ISO 4217 Dimension-Typed Currencies (FIN0004)
Just as an aerospace compiler prevents adding meters to foot-pounds, Fin# treats every ISO 4217 currency (USD, EUR, GBP, CAD, JPY, SGD) and custom commodity unit (MWh, TreasuryBill) as a distinct phantom type dimension:
// COMPILE ERROR FIN0004: Cannot combine Capital<USD> and Capital<EUR> without fx.convert!
// let invalidTotal = usdPayment + eurPayment;
To settle across currencies, see fx.convert(...) in Ledgers & Transactions.