cardinality

cardinality

Build development cycles that reduce complexity.

A type is a state space. Objects and values fit into types, with simple rules.

Unit 1 · Boolean 2 · Option[Boolean] 3

Long 264 · String ω · (String, String) ω2

Map[Char, Boolean] 3216 · Char => Boolean 2216.¹

Sums add, products multiply, functions are exponentials. Every value a type admits is a state the program must handle — or can wrongly produce. The calculator puts a number on that space inside your build, so each change can be judged by whether it shrank the space or grew it.

The loop

Measure

Add the plugin to project/plugins.sbt:

addSbtPlugin("dev.constructive" % "sbt-cardinality" % "0.0.6")

and run it:

sbt cardinalityReport
scala-cardinality — 2 sources, 11 definitions
  stored-value estimates: constructor inputs only; a finite capacity prints as the bound ≤2^b, marked (lossy) when it holds floating point
  ?: no single number here — the reason after the row says what would give one
  1 instantiation-dependent · 8 with more than one value · 2 with one value
Stored-value estimates (constructor inputs; `?` = unresolved)

?   example.Holder[A]  class     Light.scala:11  depends on its instantiation (A)
ε₀  example.Reducer    class     Light.scala:20
ω   example.Cursor     class     Light.scala:16
ω   example.Nat        abstract  Natural.scala:5
ω   example.Succ       class     Natural.scala:7
ω   example.Timeline   class     Light.scala:18
3   example.Light      abstract  Light.scala:3
2   example.Custom     class     Light.scala:5
2   example.Mode       enum      Light.scala:7
1   example.Red        object    Light.scala:4
1   example.Zero       object    Natural.scala:6

Every row is a definition, its number, and the reason when the number is not exact. The same report is written to target/cardinality/report.txt, so a build can keep it, diff it, or post it as an artifact.

Narrow

Replace the type, not the test:

The number falls, and the compiler keeps it fallen: a state the type cannot represent cannot be built, tested, or hallucinated.

Enforce

Diff report.txt in review and CI. A change that widens a type shows the new number next to the old one; a change that narrows it is the improvement, in one line. The reading a report page has the row anatomy and the moves that lower a number.

What the loop buys

Ergonomics

Without a build plugin, the library reads sources beside their compiled classes:

Report.of(paths, Some(CompiledClasses(output, classpath))).render

cardinality.reporting.Report holds every definition's value space and every method's implementation count; cardinality.tasty.CompiledClasses is the sources' compiled output and the dependency classpath it is read against. See counting methods for the other number a report gives: the implementations a signature admits.

Current release: 0.0.6. These pages describe main, which reads compiled classes; that ships in the next release (getting started says how to try it now).

Further Reading


¹ The report prints finite capacities as bounds (≤265536) and keeps products above the finite tier coarse ((String, String) prints ω). The arithmetic above is the target; a bound is not a licence to stop looking.