soma.cell

Progressive income tax over marginal brackets, plus effective rate and marginal rate lookup

Iterates a list of {upto, rate} bracket records, taxing each slice. Records, min(), accumulation.

A Soma program from the finance part of the verified corpus: 70 lines, cells TaxBrackets, TaxTests. It passes soma check with soma 3.2.4.

How it is built

The cell TaxBrackets declares 4 signals in its face: brackets() -> List, tax_owed(income: Int) -> Float, effective_rate(income: Int) -> Float and marginal_rate(income: Int) -> Float. Handlers: brackets (line 14), tax_owed (line 23), effective_rate (line 39) and marginal_rate (line 44).

The file also holds 1 test cell (TaxTests) with 5 assertions, run by soma test.

Generated from soma describe on this file.

The source

tax_brackets.cell

// Progressive income tax over marginal brackets, plus effective rate and
// marginal rate lookup. Iterates a list of {upto, rate} bracket records,
// taxing each slice. Records, min(), accumulation.

cell TaxBrackets {
    face {
        signal brackets() -> List
        signal tax_owed(income: Int) -> Float
        signal effective_rate(income: Int) -> Float
        signal marginal_rate(income: Int) -> Float
    }

    // simplified marginal brackets (upper bound, marginal rate on the slice)
    on brackets() {
        return list(
            Bracket { upto: 10000,  rate: 0.10 },
            Bracket { upto: 40000,  rate: 0.20 },
            Bracket { upto: 100000, rate: 0.30 },
            Bracket { upto: -1,     rate: 0.40 }   // -1 = no upper bound
        )
    }

    on tax_owed(income: Int) {
        let owed = 0.0
        let lower = 0
        for b in brackets() {
            let cap = b.upto
            if cap == -1 { cap = income }
            let top = min(income, cap)
            if top > lower {
                owed = owed + (top - lower) * b.rate
            }
            lower = b.upto
            if b.upto != -1 && income <= b.upto { break }
        }
        return owed
    }

    on effective_rate(income: Int) {
        if income == 0 { return 0.0 }
        return tax_owed(income) / income
    }

    on marginal_rate(income: Int) {
        let rate = 0.0
        for b in brackets() {
            if b.upto == -1 || income <= b.upto {
                rate = b.rate
                break
            }
        }
        return rate
    }
}

cell test TaxTests {
    rules {
        // 10k taxed at 10% = 1000
        assert abs(tax_owed(10000) - 1000.0) < 0.0001
        // 40k: 10k*.1 + 30k*.2 = 1000 + 6000 = 7000
        assert abs(tax_owed(40000) - 7000.0) < 0.0001
        // 50k: 7000 + 10k*.3 = 10000
        assert abs(tax_owed(50000) - 10000.0) < 0.0001
        // effective < marginal for a 50k earner
        assert effective_rate(50000) < marginal_rate(50000)
        // top bracket applies above 100k
        assert abs(marginal_rate(200000) - 0.40) < 0.0001
    }
}

What the toolchain says

Run on a fresh copy of the program at build time; the outputs are pasted unedited.

$ soma check tax_brackets.cell
✓ All checks passed.
$ soma test tax_brackets.cell
test TaxTests ...
  ✓ assert abs(tax_owed(10000) - 1000.0) < 0.0001
  ✓ assert abs(tax_owed(40000) - 7000.0) < 0.0001
  ✓ assert abs(tax_owed(50000) - 10000.0) < 0.0001
  ✓ assert effective_rate(50000) < marginal_rate(50000)
  ✓ assert abs(marginal_rate(200000) - 0.40) < 0.0001

5 tests: 5 passed, 0 failed
$ soma verify tax_brackets.cell
Verifying tax_brackets.cell...
No state machine in this program: nothing to prove beyond `soma check` (invariants and lifecycles are what verify proves).
VERIFY OK (vacuous — no state machine)

Builtins used

abs, list, min

Source file: /corpus/finance/tax_brackets.cell · examples/corpus/finance/tax_brackets.cell