Printer: core definitions → text in a profile. Source:
print.rs.Sources: SPEC-005 Rendering Profiles §3 to §7. code:
print.rs
- Binder names: display names where they are usable. Otherwise
x, and_when unused. Names are freshened against the scope, the items being printed and the built-ins. - References: printed by name only after checking that the name resolves back. Otherwise as
#hexor##hex. - Members of the printed recursive group are printed by their names, and so are drafts after a parse.
- Infix for the built-in operators, with minimal parentheses.
letandmatchprint as indented blocks in the profile’s block style (2026-10-21):let x = v…endwithblocks = end;let x = v {…}andmatch s {…}with braces;let x = v:andmatch s:with indentation (display only). Before, they always ended inend, so a Rust-profile text mixed both styles (found by Moonkale’s desktop check). Negative literals are parenthesised, so a block body never starts with-.- Profile settings honoured:
kw.function,blocks(end, braces, indent),type.annotation,operators.
Not yet: type.show, implicit.show, generic.call, index.base (no arrays yet), and comments inside bodies (SP-001).
Any depth (2026-10-24, SP-015). term_level and occurs go through deep::grow. Three costs that grew faster than depth, found at 4 000 levels and fixed: (Superseded the same day: every pass now uses explicit stacks and deep::grow is gone; see below.)
- whether a binder is used was an
occurswalk per binder (quadratic); nowbinder_usescomputes it for a whole item in one pass; freshscanned the scope for each candidate suffix (cubic for a chain of same-named binders); nowScopecounts names and keeps, per base name, the smallest suffix that might be free (same names as before: 1 000 generated definitions print identically in both profiles);- indentation grew 4 columns per nested block, so text was quadratic in depth; it now stops growing at 64 levels (
MAX_INDENT), keeping the line breaks. Measured (release,tests/deep.rsprobe): at 16 000 levels a sum chain prints in 56 ms, a lambda chain in 29 ms, a let chain in 5.0 s for 8.5 MB. Still superlinear: each level copies its children’s text into its own string, which dominates for nestedlets; the fix is a printer that writes into one buffer or builds a document tree (SP-015 step 1b).
An explicit-stack printer (2026-10-24, SP-015 step 1b). Terms are no longer printed recursively. Printer::doc runs a machine of steps: Eval a child at a minimum level, Push / Pop / Truncate scope names, Indent, Combine the top children’s text with literal pieces, and Con for constructors. Each node is planned when its Eval step runs, so scope and indentation are exactly what the recursive printer saw, and binder names are chosen the same way. Text is collected as a tree of pieces (Doc) and joined once (flatten), so no parent copies its children’s text. binder_uses and occurs use explicit stacks too.
- Same output: the 1 000 generated definitions and the 18 sample definitions print byte-identically in all four profiles (compared with the previous build); 20 000 random round trips pass.
- Linear: at depth 100 000 (release) a sum chain prints in 0.31 s, a lambda chain in 0.06 s, a let chain in 0.20 s for 53 MB; before, a let chain took 5.0 s at 16 000 levels.
- Any depth on wasm: no recursion is left in printing (
sophia-interface/tests/wasm_deep.rs).