Fin# (Financial Sharp) v0.1.0
Browse Table of Contents (6 chapters)
Getting Started (2)
Language Specification (2)
Compiler & Standard Library (1)
Diagnostics & Proofs (1)
Master Symbol Index
3 min read Module: raggiesoft.finsharp F1: IDH_FIN_LINEAR_TYPES (0x3003)

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:

  1. Ordinary Unrestricted Value Types (Int64, String, Date, Bool, DecimalQuantity): Can be copied, inspected, and dropped freely—just like values in C# or F#.
  2. Linear Monetary Resource Types (Capital<TCurrency>): Represent conserved units of purchasing power or commodity obligation. Every Capital<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:

  1. No Implicit Copy: Passing payment to debit RentExpense <- payment; moves ownership into the posting and marks payment as Consumed. Referencing payment a second time fails compilation!
  2. No Silent Drop (FIN0005): Allowing a Capital<T> variable—including an if/else branch or a 0.01_USD split remainder—to go out of scope unconsumed fails compilation with FIN0005 (UnconsumedLinearCapitalError).
  3. No Unpaired Literal Minting (FIN0001): Outside a transaction credit leg or an explicit opening_balance(...) boundary declaration, code cannot conjure Capital<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 TypeNormal BalanceAccounting Equation SideIncreases OnDecreases On
AssetDebitLeft (Assets + Expenses)debitcredit
ExpenseDebitLeft (Assets + Expenses)debitcredit
LiabilityCreditRight (Liabilities + Equity + Revenue)creditdebit
EquityCreditRight (Liabilities + Equity + Revenue)creditdebit
RevenueCreditRight (Liabilities + Equity + Revenue)creditdebit

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.

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.

Indexed API Calls & Symbols on This Page (4):