symbolic algebra
SICP §2.5's generic arithmetic dispatches add, sub, and mul across a
tower of scheme-numbers, rationals, and polynomials through apply-generic:
a table keyed by operation and argument type. The table below is not a
case statement rendered as a picture – it is the actual runtime state the
trace runs against. Click a cell to remove it and re-run the trace: watch
apply-generic fail with "no method" or fall back to coercion, then click
it again to reinstall the entry and watch the same operation succeed.
Toggle coercion off and a mixed-type operation (a number plus a polynomial)
fails outright instead of raising the number up the tower one level at a
time (ex. 2.84) – num -> rational -> polynomial, never skipping a level.
Toggle representation between sparse and dense term lists (ex. 2.89/2.90)
to see add-terms and mul-terms run genuinely different algorithms over
the same polynomials; the cost-comparison table re-runs the same operation
both ways purely to compare call counts, without changing which
representation drives the step-by-step trace above it. Toggle drop
(ex. 2.85) to watch a result simplify to the lowest type that still
represents the same value – a polynomial with no terms drops to the number
- Different-variable polynomials (ex. 2.92) either nest the
alphabetically-later variable inside the earlier one, or fail, depending on that toggle.
The pure dispatch engine lives in wal-sh.tools.symbolic-algebra.core
(host-neutral .cljc, tested on the JVM with test.check): coercion raises
exactly one tower level per retry, sparse and dense term lists agree on
every result even though their internal operation counts differ, drop
never changes the value a term represents, and a missing dispatch entry
always surfaces as a reported error, never a silent wrong answer or an
unhandled exception. The browser adapter is
wal-sh.tools.symbolic-algebra.browser. Build: gmake tools-cljs; debug in
isolation: gmake dev-tool TOOL=symbolic-algebra.