Skip to content
Back to skills

Cardinality Reduction

ASecurity

Reduce Total Program Cardinality (TPC) in this repository using the sbt-cardinality analyzer. Use when asked to 'reduce cardinality', 'shrink types', 'find cardinality candidates', 'run the cardinality report', or before/after refactors that change data or method signatures. Encodes the stage hierarchy from Rodolfo Hansen's Type System Savings / Hidden Powers of TPC articles, ranked by impact.

  • 27 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added November 30, 2026
testingbashapi

Works with

  • api

Security analysis

A100/100

Scanned November 30, 2026

npx -y skills add Constructive-Programming/eo --skill cardinality-reduction --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Cardinality Reduction?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Cardinality Reduction
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/constructive-programming-cardinality-reduction/badge)](https://www.skillsdirectory.com/skills/constructive-programming-cardinality-reduction)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cardinality-reduction
description: "Reduce Total Program Cardinality (TPC) in this repository using the sbt-cardinality analyzer. Use when asked to 'reduce cardinality', 'shrink types', 'find cardinality candidates', 'run the cardinality report', or before/after refactors that change data or method signatures. Encodes the stage hierarchy from Rodolfo Hansen's Type System Savings / Hidden Powers of TPC articles, ranked by impact."
---

<!-- Generated by sbt-cardinality 0.0.6 via cardinalitySeedSkill; re-run it to
     refresh this file to the current template, delete it to take ownership of the copy. -->

# Cardinality reduction in eo

This repository measures itself with `sbt-cardinality`; the report is one command away.

## 1. Measure first

```bash
sbt cardinalityReport   # per module; also written to cardinalityReportFile (default: target/cardinality/report.txt)
```

- Reports are **per module**: every project the plugin applies to reports its own sources.
  Read the summary and the rows per module, never as one merged number.
- Narrow a single signature with `sbt 'cardinalityReportSelected <qualified.Name>'`.
- Measure sources this build does not compile — a dependency's published sources jar, another
  checkout — with `sbt 'cardinalityReportOf <path-or-jar>'`.
- Keep a checked-in baseline: point `cardinalityReportFile` at a path under version control
  (for example `docs/baselines/cardinality-<date>.txt`) or copy the report there, recording the
  date, the command and the plugin version in its header. Diff a new run against it; refresh the
  baseline only when a change lands.

### Reading rows

| Size | Meaning |
| --- | --- |
| `2^n`, `8`, `3` | finite upper bound (constructor inputs only) |
| `ω` | countably infinite value space (recursive fixed point, lazy hole, `String`-like base) |
| `ε₀`, `aω` | beyond countable / coefficient of countable alternatives |
| `?` | unresolved — the trailing `unresolved:` reasons say what stopped the bound |
| `—` | definition with no cardinality of its own (abstract) |

The signature section counts *implementation space* of methods/constructors; the stored-value
section counts *state space* of data definitions. Never read `?` as infinity.

## 2. The hierarchy: where the value is

From Hansen's ["Type System Savings"](https://medium.com/swlh/type-system-saving-55db2ca062ff)
and ["The Hidden Powers of Total Program Cardinality" 1](https://medium.com/@rodolfohansen/the-hidden-powers-of-total-program-cardinality-9351ba774d4d)
/ [2](https://medium.com/@rodolfohansen/the-hidden-powers-of-total-program-cardinality-2-of-2-ceb0a3af13dd).
Four stages, **ranked by impact on the TPC score** — always try the lowest-numbered applicable
stage first; the bulk of the returns are reaped by stage 3, and stage 4 is the craftsman's tier.

### Stage 1 — Replace products with sums (biggest win)

`|A| + |B|` vs `(1+|A|) × (1+|B|)`: a nullable product of two engines has 54 states where the
sum has 13; the article's validation function drops from `1.8×10¹⁶` possible implementations.
Make illegal states unrepresentable: split "either A or B" fields into a sealed trait/enum,
kill nullable pairs, model `Option` in the type, not in two independent fields.

### Stage 2 — Narrow types (functional domain design / domain driven design)

Any type broader than the domain needs carries states the code must never see. The article's
primitive case — `String`/`Int` → enum or refined type (`Int Refined Positive` exposed the
booking bug) — is one instance of the general rule: wherever a broader type is used than the
situation admits, narrow it. A closed vocabulary carried in `String` becomes an enum; a count
that is always positive becomes a refined number; a `List` whose elements never repeat or
reorder becomes a `Set` or a sized structure; an `Either[E, A]` with exactly one possible
error becomes `Option` or a concrete type; a supertype accepted where one subtype is ever
supplied narrows to that subtype. Every unnecessary breadth removed deletes a family of
invalid states — and shows up as a smaller report row.

### Stage 3 — Take sums out of exponents (functions)

`(aᵇ + aᶜ) < a^(b+c)`: an enum/Option/interface as a parameter multiplies the implementation
space by the case count. Distribute instead: one method per enum value, extension methods,
type classes, overloads, pattern matching. Avoid `Option` as a parameter — map over it and
pass the simpler value. This is also the quantitative reading of SOLID
(`aᵇ < (a+1)ᵇ` for single responsibility; `(aᶜ + bᶜ) < (a+b)ᶜ` for open–closed).

### Stage 4 — Generics (theorems for free)

Parametricity shrinks implementation space to a unique inhabitant: `[A] A => A` has exactly
one implementation, `fold :: (a -> c) (b -> c) Either[a,b] -> c` likewise. Smallest absolute
shifts, but highest reuse — the marker of stage-4 code is 3–5 line bodies.

## 3. Workflow for a reduction pass

1. Run the report (§1). List stored-value rows ordered by size: `2^320` beats `ω` beats `ε₀`
   beats finite. Ignore `?` signature rows — those are analyzer resolution limits, not
   reduction targets; the `unresolved:` reasons name what would resolve them.
2. For each top row, classify which stage applies and name the smallest refactor. The report
   shows sizes, not fields — audit the construction sites (there are few) before classifying:
   - Numeric-field products (a limits/budget class whose fields are independent numbers):
     stage 1/2 — check whether the fields are genuinely independent knobs enforced on separate
     paths; if a test or doc pins the independence, leave it.
   - String-factor products (an `ω` row with generic-sounding fields — `budget`, `frontier`,
     `message`): two checks at the construction sites. First, closed vocabulary: a field
     assembled from a handful of string literals is a hidden enum (stage 2). Second,
     correlated fields: if one field's meaning depends on another field's value — an
     operation label for some values, a source path for others — the product admits
     combinations no value can produce; split it into a sum of cases (stage 1). Worked
     example from the plugin's own codebase: `Exhausted(budget: String, frontier: String)`
     became `Fuel(index | target, operation)`, `ResolutionDepth(operation)`,
     `SyntaxDepth(path)`, each case rendering the exact prior message string.
   - Genuinely open Strings (identifiers, source text, arbitrary type syntax embedded in a
     message): leave them.
   - Method rows with enum/`Option` parameters: stage 3 — split per case. The report only
     flags the parameter; finding the case splits themselves is a source-level pass — §4.
3. Propose, then re-measure: behavior preserved is the hard gate — the project's test suite
   first, scripted/ integration tests for anything a build plugin does. The report is the
   scoreboard with one blind spot: products are deliberately coarse, so a class with any
   `ω`-factor (`String`, `List`) reads `ω` no matter how the other factors shrink. Narrowing
   a finite factor beside an `ω` factor (a hidden enum inside such a product) shows no
   change — groundwork, not a win. Splitting the product into a sum does move the row:
   `ω` → `3ω`, the case count surfaced as an explicit coefficient. Count that as a win even
   though the tier is unchanged, and record the before/after reading in the baseline note
   per the ratchet.
4. Ratchet: refresh the baseline and never land a run that moves an existing row **up a tier**
   (`ω` → `ε₀`, `2^n` growing) without an explicit justification. A same-tier coefficient
   appearing (`ω` → `3ω`) is precision gained by a sum split, not a regression — justify it
   in the baseline note.

## 4. Stage-3 pass: sums in exponents (source-level audit)

The report cannot show this class of problem: a parameter's size reads the same whether the
caller dispatches the sum or branches around it, and stored-value rows never move when only
method bodies change. Run it as a separate pass over the sources.

1. **Inventory the build's own sums**: `enum`, `sealed trait`, `sealed abstract class` in the
   production sources. Third-party sealed hierarchies (AST libraries, framework types) are
   visitor territory — splitting those per case is not a win; leave them.
2. **Find the external case analysis**: `= x match` / `isInstanceOf[` / `== caseObject`
   where `x` is one of those sums — directly, or as a field of a product parameter — and the
   site lives outside the sum's own methods.
3. **Classify each site before touching it**:
   - *Case analysis outside the sum* — an `exists(_.isInstanceOf[Case])` or a `match`
     duplicating a question the sum should answer: move it into the sum as a `def` on `this`.
     The sum owns its cases; callers can no longer reach into them.
   - *Switchboard* — a few cases, each branch a one-line delegation onto a dedicated
     function: the article endorses pattern matching that isolates the sum's value before the
     body is read; keep.
   - *Companion renderer or ADT interpreter*: the canonical exceptions; keep.
   - *Equality predicates on case objects*: fine on their own; extract into the sum only when
     the same predicate repeats across sites.
4. **Expect no report movement, and accept it**: moving dispatch into a sum adds signature
   rows (the new `def`s) and drops none. That is the article's own prescription — dynamic
   dispatch is the language feature that addresses sums-in-exponents — so this pass is gated
   by behavior tests, not by the ratchet's row diff. Record the classification table and the
   keep/refactor verdicts in the baseline note so the next pass starts from it.

## 5. Stage-4 pass: generics (theorems for free)

Parametricity narrows implementation space to (near-)unique inhabitants: `[A] A => A` has
exactly one implementation, and a function that receives its operation as a parameter cannot
do anything but structure. The report cannot show this — `?`-tier signature rows hide
implementation space — so run it as a source-level pass. Grep first and read only hit
regions; whole-file reads waste the budget.

Three buckets, in order of value:

1. **Type-duplicated helpers** — near-identical functions differing only in the operated
   type: one generic function replaces them, taking the varying operation as a parameter.
2. **Hand-rolled recursions re-implementing existing machinery** — manual folds or loops that
   restate a traversal library the project already depends on (recursion schemes, cats
   combinators, standard `Traverse`/`Foldable` operations). Swap, preserving semantics
   exactly.
3. **Concrete-typed purely structural functions** — containers/primitives in and out, body
   free of domain logic: a type parameter would make the implementation parametrically
   unique. Require a second use or an already-forced body; generalizing a single-caller
   helper is speculation, not a win.

**Verify library semantics before swapping.** Do not guess failure order, error accumulation,
or strictness from a combinator's name — look the type up in the published artifact (an API
browser such as cellar, or the library's source) and confirm the semantics match the hand-
rolled code. The swap is only behavior-preserving if they do. Worked example: a hand-rolled
`foldRight` sequence over `Either` short-circuits with the leftmost `Left`, and cats'
`Traverse.sequence` for `Either` matches, which is why that swap is safe.

**Gates**: behavior tests are the hard gate; expect no report movement (parametric bodies do
not move signature or stored-value rows). Record what was swapped and which semantics were
checked in the baseline note. Reject domain-coupled candidates: there the implementation
space is pinned by invariants, not types, and generalization is type shuffling.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in testing →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.