Skip to content

Feature/general literals - #1004

Merged
nicolas-abril merged 21 commits into
bendlang:mainfrom
MattCozendey:feature/general-literals
Sep 24, 2026
Merged

nicolas-abril merged 21 commits into
bendlang:mainfrom
MattCozendey:feature/general-literals

Conversation

@MattCozendey

@MattCozendey MattCozendey commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

A literal carries its Base type: one Lit for Nat, String, U32, F32 and Char

This PR (#1004) generalizes the compact literal. It is an internal change: no
syntax, no program meaning, no printed output and no emitted code changes.

Where this comes from

  1. Fix/string literal term #907 made a String literal one Lit node that unfolds one character at a
    time where it is read.
  2. Feature/nat literal term #924 did the same for Nat literals.
  3. Feature/u32-and-f32-to-literals #936 extended Lit to U32 and F32 with an optional tag k. Each reader
    (the step, the full unfolding, check-lit, the printer, nat_from_term, the
    compiler) got a k !== undefined branch next to its typeof v branch.
  4. A literal is one node per kind: Lit{k, v} steps through lit_step, and U32 and F32 literals are kinds #1003 (Nico Abril) is an alternative to Feature/u32-and-f32-to-literals #936: every literal carries its
    kind, and lit_step is the one place that knows a kind.

This PR starts from #936 (its commits and its nine tests are included) and
takes the same direction as #1003. It adds the compiler and checker work that
removes the cost of the step, and it deletes the code that the tag makes
unnecessary.

The design

A literal is one node that holds a closed value of its Base type:

| { $: "Lit"; k: "Nat" | "U32" | "F32"; v: number }   // 3n, 7, 1.5 (its bits)
| { $: "Lit"; k: "String"; v: string }                // "text"
  1. The union ties each kind to its payload, so narrowing on k types v, and
    Lit("Nat", "x") does not compile.
  2. lit_step unfolds one constructor. Its fields can be literals again: a
    String's step and a char literal 'c' hold the code as Chr{Lit(U32)}, one
    node, not a 65-node Word chain. Char needs no kind of its own.
  3. Check-lit is one condition: Lit(k, v) checks at Base's type k. Base
    loads with the empty namespace, and each head constructor of Nat, String,
    U32 and F32 is declared by that type alone, so this accepts exactly the
    terms that the old ctrs_find rule accepted, with no step built.
  4. The printer prints a literal as its first step, so the existing sugar
    prints every kind (this idea is from A literal is one node per kind: Lit{k, v} steps through lit_step, and U32 and F32 literals are kinds #1003).
  5. The compiler steps a literal one constructor at a time, as the checker
    does. lit_full is gone.
  6. term_const in the compiler answers "constant" for a literal at once. It
    unfolded each U32 literal into 65 nodes to find every node constant: 75%
    of the compiler's U32 unfolds in pure_hvm5_mini came from there.
    lit_call is the one place for the exception, a Nat past the cap.
  7. A step is a closed tree of Ctr and Lit nodes, the same in both body
    representations, so the checker and the compiler use it without a
    term_higher copy.

Removed: lit_full, u32_to_term, lit_chain, every typeof v and
k === undefined test, and the per-kind printer branch. The per-kind code is
lit_step and the parser.

Correctness

bend2/bend.lean models literals as their constructor trees ("Literals are
base.bend constructors, not calculus"), so the proof holds while these hold:

  1. Meaning: each Lit means one closed tree of Base constructors, the tree
    that main parses.
  2. Typing: the fast check accepts Lit(k, v) only at Base's type k.
  3. Equality: two literals are equal only when k and v are equal.
  4. Reading: each reader of a term head unfolds a literal first (term_compare,
    term_descend, the match frame, parse_patt, infer-err, the compiler's
    term_force); u32_from_term reads a U32 literal's value directly.

Nothing mutates a step's nodes, so sharing a step is safe; main shares its
parsed U32 trees the same way.

Measurements

WSL (Ubuntu 24.04, 20 threads). Each cell is CPU time (user + sys) in seconds
and peak memory in MB, the median of 5 rounds that rotate the three trees.
All three trees emit identical C for every program measured. "Run" is
bend file.bend (check and interpreter). The three literal programs are
generated scratch files, not part of the PR.

Case main #1003 #1004 (this)
5000 Char literals, check 2.17 / 418 0.50 / 97 0.45 / 95
5000 Char literals, run 2.53 / 480 1.01 / 132 0.65 / 122
5000 Char literals, -o main.c 3.27 / 629 2.00 / 240 0.97 / 144
500 Strings + 200 String proofs, check 0.56 / 92 0.27 / 78 0.27 / 78
500 Strings + 200 String proofs, run 1.38 / 134 1.19 / 125 0.58 / 118
500 Strings + 200 String proofs, -o main.c 3.12 / 610 2.93 / 481 1.07 / 176
2500 U32 + 1000 F32 literals, check 1.74 / 318 0.34 / 88 0.31 / 84
2500 U32 + 1000 F32 literals, run 1.97 / 379 0.77 / 129 0.51 / 103
2500 U32 + 1000 F32 literals, -o main.c 2.47 / 490 1.42 / 193 0.68 / 112
app_slash_boss_3d check 2.11 / 432 0.89 / 142 0.83 / 146
app_slash_boss_3d/PROOF check 6.91 / 505 7.50 / 193 7.07 / 194
pure_hvm5_mini -o main.c 2.43 / 229 2.23 / 180 2.05 / 182
pure_par_sort check 0.82 / 88 0.80 / 85 0.71 / 84
bench/runtime/raytrace -o main.c 0.91 / 134 0.65 / 102 0.62 / 103

Summary of the full table below (87 cases), time / memory:

Group #1004 vs main (median, range) #1004 vs #1003 (median)
Checker benches (5) −5% (−9% to −4%) / −1% −2% / −2%
Runtime benches, -o main.c (16) −23% (−32% to −11%) / −12% −2% / 0%
Demo checks (32) −17% (−61% to +2%) / −4% −3% / 0%
Demo -o main.c (7) −14% (−23% to −8%) / −5% −4% / 0%
  1. On the literal programs, this PR is 52% to 82% faster than main and uses
    12% to 77% less memory.
  2. On ordinary programs, A literal is one node per kind: Lit{k, v} steps through lit_step, and U32 and F32 literals are kinds #1003 and Feature/general literals #1004 are within a few percent of each
    other. Most of their gain over main comes from the U32 and F32 literals
    in base.bend, which main builds as full chains at parse time.
  3. The PROOF check of app_slash_boss_3d is within noise of main in time
    (+2% here, −3% and +2% in two earlier runs) and uses 62% less memory.
  4. A separate 8-round run compiled app_slash_boss_3d (-o main.c) in 9.28 s
    on main, 7.66 s on A literal is one node per kind: Lit{k, v} steps through lit_step, and U32 and F32 literals are kinds #1003 and 6.54 s on Feature/general literals #1004.

Against #1003, the difference is in the steps that #1003 still builds: its
compiler unfolds each literal completely (lit_full), so a String program
compiles almost as slowly as on main; its check-lit builds a U32's word to
read the head constructor; and its u32_from_term builds the word to read a
value the literal holds.

Full table: every checker bench, runtime bench and demo, and the literal programs

The LAWS.bend files exit 1 on all three trees: they are laws without proofs.

Case main #1003 #1004 #1004 vs main
5000 Char literals, check 2.17 / 418 0.50 / 97 0.45 / 95 −79% / −77%
5000 Char literals, run 2.53 / 480 1.01 / 132 0.65 / 122 −74% / −75%
5000 Char literals, -o main.c 3.27 / 629 2.00 / 240 0.97 / 144 −70% / −77%
500 Strings + 200 proofs, check 0.56 / 92 0.27 / 78 0.27 / 78 −52% / −14%
500 Strings + 200 proofs, run 1.38 / 134 1.19 / 125 0.58 / 118 −58% / −12%
500 Strings + 200 proofs, -o main.c 3.12 / 610 2.93 / 481 1.07 / 176 −66% / −71%
2500 U32 + 1000 F32, check 1.74 / 318 0.34 / 88 0.31 / 84 −82% / −74%
2500 U32 + 1000 F32, run 1.97 / 379 0.77 / 129 0.51 / 103 −74% / −73%
2500 U32 + 1000 F32, -o main.c 2.47 / 490 1.42 / 193 0.68 / 112 −72% / −77%
bench/checker/compute_1600 1.97 / 159 1.91 / 151 1.88 / 148 −5% / −7%
bench/checker/defs_12800 1.78 / 299 1.68 / 304 1.68 / 295 −6% / −1%
bench/checker/generics_3200 1.88 / 348 1.79 / 346 1.72 / 347 −9% / 0%
bench/checker/proofs_3200 4.10 / 766 4.12 / 782 3.92 / 760 −4% / −1%
bench/checker/trees_400 2.90 / 80 2.81 / 80 2.76 / 80 −5% / −1%
bench/runtime/bfs -o main.c 0.47 / 91 0.37 / 83 0.36 / 80 −23% / −12%
bench/runtime/editdist -o main.c 0.82 / 121 0.62 / 105 0.63 / 106 −23% / −13%
bench/runtime/gameoflife -o main.c 0.52 / 98 0.36 / 81 0.37 / 82 −29% / −16%
bench/runtime/hashmap -o main.c 0.45 / 85 0.34 / 78 0.33 / 79 −27% / −7%
bench/runtime/kmeans -o main.c 0.59 / 103 0.45 / 87 0.43 / 87 −27% / −15%
bench/runtime/lexer -o main.c 0.55 / 99 0.41 / 81 0.38 / 83 −31% / −16%
bench/runtime/mandelbrot -o main.c 0.53 / 97 0.42 / 82 0.41 / 84 −23% / −14%
bench/runtime/merkle -o main.c 0.82 / 128 0.63 / 100 0.62 / 98 −24% / −23%
bench/runtime/nbody -o main.c 0.81 / 131 0.58 / 102 0.58 / 99 −28% / −25%
bench/runtime/queens -o main.c 0.51 / 96 0.44 / 87 0.43 / 85 −16% / −12%
bench/runtime/raytrace -o main.c 0.91 / 134 0.65 / 102 0.62 / 103 −32% / −23%
bench/runtime/symreg -o main.c 0.38 / 85 0.36 / 79 0.33 / 79 −13% / −7%
bench/runtime/terrain -o main.c 0.63 / 101 0.49 / 90 0.49 / 94 −22% / −7%
bench/runtime/tree-bitonic -o main.c 0.35 / 82 0.31 / 78 0.31 / 77 −11% / −6%
bench/runtime/tree-matmul -o main.c 0.47 / 90 0.39 / 81 0.36 / 81 −23% / −10%
bench/runtime/tree-radix -o main.c 0.36 / 82 0.31 / 77 0.31 / 78 −14% / −5%
app_pong_game_2d/LAWS check 0.33 / 79 0.29 / 78 0.29 / 78 −12% / −2%
app_pong_game_2d/PROOF check 0.42 / 95 0.32 / 80 0.33 / 80 −21% / −16%
app_pong_game_2d/main check 0.42 / 95 0.32 / 80 0.30 / 81 −29% / −14%
app_ray_tracer_3d/LAWS check 0.48 / 97 0.34 / 77 0.33 / 78 −31% / −19%
app_ray_tracer_3d/PROOF check 0.57 / 112 0.41 / 88 0.40 / 89 −30% / −21%
app_ray_tracer_3d/main check 0.63 / 116 0.39 / 90 0.40 / 91 −37% / −22%
app_slash_boss_3d/LAWS check 1.71 / 308 0.79 / 125 0.76 / 124 −56% / −60%
app_slash_boss_3d/PROOF check 6.91 / 505 7.50 / 193 7.07 / 194 +2% / −62%
app_slash_boss_3d/bend3d check 0.74 / 135 0.54 / 103 0.57 / 108 −23% / −21%
app_slash_boss_3d/main check 2.11 / 432 0.89 / 142 0.83 / 146 −61% / −66%
app_triangle_2d/LAWS check 0.25 / 77 0.21 / 71 0.20 / 71 −20% / −8%
app_triangle_2d/PROOF check 0.38 / 92 0.26 / 77 0.25 / 76 −34% / −17%
app_triangle_2d/main check 0.31 / 83 0.24 / 75 0.23 / 73 −26% / −12%
app_win_is_bug_2d/LAWS check 0.24 / 73 0.21 / 71 0.20 / 70 −17% / −4%
app_win_is_bug_2d/PROOF check 1.42 / 106 1.42 / 97 1.44 / 100 +1% / −6%
app_win_is_bug_2d/main check 0.24 / 76 0.21 / 71 0.21 / 71 −13% / −6%
io_hello_world/LAWS check 0.25 / 73 0.19 / 69 0.20 / 70 −20% / −4%
io_hello_world/PROOF check 0.24 / 73 0.20 / 69 0.20 / 69 −17% / −5%
io_hello_world/main check 0.22 / 71 0.20 / 69 0.20 / 70 −9% / −2%
io_hello_world -o main.c 0.25 / 74 0.21 / 72 0.20 / 71 −20% / −5%
io_http_fetch/LAWS check 0.23 / 73 0.20 / 70 0.19 / 70 −17% / −5%
io_http_fetch/PROOF check 0.29 / 77 0.25 / 74 0.24 / 74 −17% / −3%
io_http_fetch/main check 0.25 / 75 0.21 / 72 0.20 / 72 −20% / −4%
io_http_server/LAWS check 0.23 / 72 0.20 / 69 0.20 / 70 −13% / −3%
io_http_server/PROOF check 0.29 / 79 0.21 / 69 0.20 / 69 −31% / −12%
io_http_server/main check 0.23 / 72 0.21 / 70 0.21 / 70 −9% / −3%
io_rollback_netcode/netcode check 0.36 / 88 0.32 / 82 0.29 / 79 −19% / −10%
io_rollback_netcode/server check 0.36 / 86 0.29 / 79 0.28 / 78 −22% / −9%
io_rollback_netcode/walkers_demo check 0.49 / 103 0.33 / 83 0.34 / 88 −31% / −15%
io_rollback_netcode/walkers_test check 0.52 / 104 0.37 / 87 0.36 / 84 −31% / −20%
io_tcp_echos/LAWS check 0.24 / 72 0.20 / 70 0.19 / 69 −21% / −4%
io_tcp_echos/PROOF check 0.24 / 72 0.21 / 69 0.20 / 69 −17% / −4%
io_tcp_echos/main check 0.24 / 73 0.22 / 70 0.21 / 70 −12% / −4%
proof_insertion_sort/LAWS check 0.22 / 73 0.20 / 70 0.20 / 70 −9% / −4%
proof_insertion_sort/PROOF check 0.25 / 74 0.21 / 72 0.22 / 72 −12% / −3%
proof_insertion_sort/main check 0.23 / 72 0.20 / 70 0.21 / 69 −9% / −4%
proof_insertion_sort -o main.c 0.28 / 76 0.23 / 73 0.24 / 72 −14% / −5%
proof_numerics/LAWS check 0.22 / 73 0.20 / 70 0.20 / 69 −9% / −4%
proof_numerics/PROOF check 0.26 / 75 0.24 / 74 0.25 / 74 −4% / −1%
proof_numerics/main check 0.23 / 72 0.21 / 70 0.19 / 70 −17% / −2%
proof_numerics -o main.c 0.26 / 75 0.23 / 72 0.23 / 72 −12% / −4%
proof_typed_eval/LAWS check 0.23 / 72 0.21 / 69 0.20 / 70 −13% / −3%
proof_typed_eval/PROOF check 0.26 / 77 0.23 / 74 0.24 / 74 −8% / −3%
proof_typed_eval/main check 0.24 / 74 0.21 / 70 0.21 / 71 −12% / −3%
proof_typed_eval -o main.c 0.31 / 77 0.26 / 73 0.27 / 73 −13% / −5%
pure_hvm5_mini/LAWS check 0.47 / 95 0.35 / 78 0.34 / 79 −28% / −17%
pure_hvm5_mini/PROOF check 0.69 / 113 0.61 / 103 0.59 / 105 −14% / −7%
pure_hvm5_mini/main check 0.53 / 109 0.39 / 89 0.38 / 87 −28% / −20%
pure_hvm5_mini -o main.c 2.43 / 229 2.23 / 180 2.05 / 182 −16% / −20%
pure_par_sort/LAWS check 0.77 / 84 0.76 / 84 0.71 / 88 −8% / +5%
pure_par_sort/PROOF check 0.82 / 85 0.78 / 83 0.69 / 86 −16% / +1%
pure_par_sort/main check 0.82 / 88 0.80 / 85 0.71 / 84 −13% / −5%
pure_par_sort -o main.c 2.21 / 109 2.15 / 103 2.03 / 103 −8% / −5%
pure_par_sum/LAWS check 0.23 / 72 0.20 / 70 0.19 / 70 −17% / −3%
pure_par_sum/PROOF check 0.25 / 74 0.21 / 70 0.20 / 71 −20% / −4%
pure_par_sum/main check 0.24 / 73 0.21 / 70 0.19 / 70 −21% / −4%
pure_par_sum -o main.c 0.30 / 75 0.24 / 73 0.23 / 72 −23% / −4%

Size

File main #1004
bend2/bend.ts (ttok) 43431 43259 (−172)
bend2/bend.ts (lines) 3976 3952
bend2/comp.ts (ttok) 62163 62207 (+44, lit_call)

Code diff: 67 lines added, 87 removed. bend.ts stays under the 44000 cap.

Tests

  1. The nine tests of Feature/u32-and-f32-to-literals #936 are included and pass.
  2. 303 tests in the literal, char, string, escape, pattern, printer, parse,
    eval and halt namespaces give the same result as main on the check,
    interpreter, JS and C lanes, judged as gates/test.ts judges them, and
    their emitted JS and C are byte-identical to main.
  3. All 240 show and check tests give the same result as before the
    printer change.
  4. Every demo's check output, and the run output and emitted JS and C of the
    demos that run on the CPU, are byte-identical to main.
  5. tsc reports four errors, all in code that this PR does not touch.

Not run: the cluster gates and the full 1436-test suite. #1003 reports a
full byte comparison against main; this PR has not done that.

Considered and rejected

  1. A compact Word literal (U32{Lit(Word, v, 32)}): the checker reads all
    32 bits where a U32 unfolds (term_compare against computed values, the
    Word.* functions), so a lazy word adds nodes; it also needs a width field,
    a Nat-index test in check-lit and a Word case in u32_from_term.
  2. A per-literal cache of word steps in the checker: measured on top of this
    PR, it gained nothing.
  3. User-overloadable literals (T.literal): a language feature, not this
    change.

🤖 Generated with Claude Code

@nicolas-abril

Copy link
Copy Markdown
Collaborator

One gap in the new check-lit rule (bend.ts#L3637): the fast path ignores the ADT's residual r.

if (t_wnf.$ === "ADT" && t_wnf.k === tm.k && book.tlds[tm.k]?.b === true) {

A type like Nat - Zero{} or U32 - U32{} has k === "Nat" / "U32", so this accepts 0n : Nat - Zero{} and 7 : U32 - U32{}. The second type is empty. The old rule went through book_adt (L992), which filters out the removed constructors, so it rejected both. That means the description's claim that the new rule "accepts exactly the terms that the old ctrs_find rule accepted" doesn't hold for residual types.

I couldn't reach this from source today. Residuals are only created as the binder type of a match's default arm (L3704), and there is no syntax to write one and no metavariable that could carry one to a literal. So it's latent, not a live soundness hole. It is still a break in property 2 of the correctness list ("the fast check accepts Lit(k, v) only at Base's type k"). It would become reachable as soon as anything lets a residual type flow into a checking position.

Suggested fix: keep the fast path for the plain type only, and let residual types take the step path, which is the old behaviour:

if (t_wnf.$ === "ADT" && t_wnf.k === tm.k && t_wnf.r.length === 0 && book.tlds[tm.k]?.b === true) {

The step then checks Zero{} / Succ{Lit(n-1)} / U32{word} against book_adt's filtered constructors, so 3n : Nat - Zero{} still passes and 0n : Nat - Zero{} is rejected by check-ctr (expected Nat - Zero{}, observed Nat).

A smaller, unrelated nit: u32_from_term's new Lit branch returns w0.v as U32 whenever w0.k === k, so a call with k = "String" would return a string. No caller does that today. Narrowing the parameter to k: "U32" | "F32" would make that impossible.

Everything else I checked matches main:

  • Local test suite (gates/test.ts run on one Mac instead of the cluster): the PR passes 1434/1436 and main passes 1425/1427. The only failures are gfx_clicks and gfx_window on both, because the machine has a display.
  • Emitted code: all 662 C files and 681 JS files (demos, runtime benches, tests) are byte-identical to main.
  • Demo checks: all 50 demo files give identical output.
  • Edge-case probes: about 35 small programs covering equality, wrong-type errors, patterns, printing and holes, templates, and surrogate code points all give output identical to main.

Note: this comment was written by an AI after it reviewed the PR and reported the issue to me. If anything here is wrong, reply and I will review it myself.

@MattCozendey

Copy link
Copy Markdown
Contributor Author

Thanks, both confirmed and fixed in 29233a3. Check-lit's fast path now requires t_wnf.r.length === 0, so a residual type takes the step path as on main, and the description's "accepts exactly what the old rule accepted" now holds. u32_from_term takes k: "U32" | "F32", which also removes its cast; its one caller in comp.ts tests adt.k directly. Emitted code, the check and show tests and the demos are unchanged.

MattCozendey and others added 21 commits September 23, 2026 21:38
…erentiate (v was already a value string | number)
…check for k === undefined to ensure that optimization is exclusive to nat
Reset bend.ts, comp.ts, GUIDE.md and literal_reads.bend to aa823c0,
then keep two changes from 29a75ae: lit_full is gone (parse_patt and
the compiler's term_force unfold one step through lit_step), and
term_unapply loops over App nodes. Literal tests pass 61/61; every
demo's check, run output, emitted JS and emitted C match aa823c0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every Lit names its type in k, and the type ties v to its host value
(a count or bits for Nat, U32 and F32, text for String). lit_step, the
printer and each reader select by k, and check-lit accepts a literal
at Base's type k, which is the rule's old condition: each head
constructor of Nat, String, U32 and F32 is declared by that type alone.
A module's own Nat, U32 or F32 still checks the literal's unfolding.
Literal tests 61/61; demos, a literal-heavy program and the literal
tests emit byte-identical JS and C to aa823c0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ma2csvkz5EeH5N2MiF8JPL
'a', each character a String literal unfolds and a Nat literal past the
cap build Chr{Lit}, SCon{Chr{Lit}, ..} and U32.to_nat(Lit): the code
stays one node that unfolds to the same U32{Word} where a head is read,
and checks at Base's U32 without its 65-node chain. 303 tests (literal,
char, string, pattern, printer, parse, eval, halt) pass as on aa823c0
with byte-identical JS and C; the demos and a literal-heavy program too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ma2csvkz5EeH5N2MiF8JPL
Size, the correctness checks, the memory and time measurements (5
interleaved rounds; C emit alone over 15), the thermo-nuclear review
and the complexity table. The Word study waits for the user.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ma2csvkz5EeH5N2MiF8JPL
lit_chain had one caller and folds into lit_of; the lit_step comment
restated its code; the comments that listed the literal types (and
would grow with each one) now name the rule instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ma2csvkz5EeH5N2MiF8JPL
Finished steps, the pre-edit checklist, the first attempt's history and
the superseded measurements go; the design, the correctness contract,
the result, the Word study and the local method stay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ma2csvkz5EeH5N2MiF8JPL
term_const unfolded each U32 literal into its 65-node Word to find every
node constant; a Lit is a closed tree by construction, so it answers at
once. lit_call names the one exception, a Nat past the cap, for
term_force and term_const alike. Emitted C and JS are byte-identical on
303 tests, the demos and 8 programs; a Char-heavy program emits in
-65% time and -54% memory, the rest within noise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ma2csvkz5EeH5N2MiF8JPL
lit_step and word_to_term build closed trees of Ctr and Lit only, which
are the same in both body representations, so the checker and the
compiler take the step as it is instead of copying it node by node
(65 nodes per word). Nothing mutates those nodes, so sharing is safe;
main shared its parsed U32 trees the same way. Emitted C and JS are
byte-identical (303 tests, demos). The slash_boss PROOF check falls
from +4.6% to -2.8% CPU time against main; a String-heavy program runs
-19% and emits -15% faster than before. A per-literal word cache was
measured on top and gained nothing, so there is none.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ma2csvkz5EeH5N2MiF8JPL
The Nat, U32, F32 and String sugar already prints each step, so the
per-kind ternary goes and a new kind prints with no printer code; the
idea is from bendlang#1003. 303 representative tests, all show and check tests
(240) and the demos print as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ma2csvkz5EeH5N2MiF8JPL
A residual type (Nat - Zero{}, U32 - U32{}) shares its name with Base's,
so the fast path accepted 0n and 7 there; book_adt filters the removed
constructors, and the old rule went through it. A residual now takes the
step path, the old behaviour. Residuals only arise as a match default
arm's binder type, so no source program reached this. u32_from_term
takes "U32" or "F32", so its literal branch reads a number with no cast.
303 tests, the 240 check and show tests and the demos are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ma2csvkz5EeH5N2MiF8JPL
@MattCozendey
MattCozendey force-pushed the feature/general-literals branch from 29233a3 to 2ab44d6 Compare September 24, 2026 00:39
@nicolas-abril nicolas-abril self-assigned this Sep 24, 2026
@nicolas-abril
nicolas-abril merged commit 2f50df1 into bendlang:main Sep 24, 2026
@MattCozendey
MattCozendey deleted the feature/general-literals branch September 24, 2026 12:24
@Lorenzobattistela Lorenzobattistela mentioned this pull request Sep 25, 2026
VictorTaelin added a commit that referenced this pull request Sep 27, 2026
… kernel

`bend <file> --safe` runs bend2's checker, then elaborates the checked book
to a .bendtt file (bend2/safe.ts) and checks it with the BendTT kernel's CLI
(bend2/bendtt.lean, bend5 main 8284f4a8: one Lean file with the kernel, its
claims and their proofs).

- safe.ts (19,924 ttok): per-def scope; convoys through matches and
  refutations; eta-long at binder or kind mismatch; bodiless defs go out as
  opaque defs at a model built from their type; items specialize at every
  parameter a kind depends on; templates specialize at their ~ arguments
  (or check at bend2's instances when an opaque constant has no model); a
  law-declared def and the helpers that call it back go out as one group
  def with a selector; a linear pair used twice is matched once and rebuilt
  at each use; it reads main's typed literals (#1004): a Nat literal past
  4096 goes out as Nat.add(Nat.mul(q, 4096), r), a U32 or F32 literal as one
  def per word.
- base.bend: Array.get.go, Array.swap.go, Map.get, Map.put, Map.ins,
  Map.seek, Map.has and Map.pop recurse on their own pieces; the forward
  helpers (.bit/.deep/.if) and five laws go. tests/flatten/array_statement
  expects Array.get.go.
- gates/safe.ts (+ safe_node.ts, safe_diag.ts): bend2's verdict against
  BendTT's per file, on the minis, for tests/ or a BendHub store. Last run:
  tests 979 agree, 1 partial (base's @unsafe Array.join), 24 user @unsafe,
  481 bend2 rejects, 0 false rejects, 0 timeouts; hub 715 agree, 11
  vacuous, 2 partial (F32 LtOk, no model for the native), 78 user @unsafe,
  213 bend2 rejects, 0 false rejects, 0 timeouts. Since the old base, bend2
  itself rejects 155 more hub files: 101 name clashes and 52 digit words
  (#1042), 2 calls to the gone Nat.read.fit; every other file that agreed
  still agrees.
- main.ts takes --safe; repo.ts and AGENTS.md list safe.ts and bendtt.lean;
  .gitignore skips the *.bendtt files --safe writes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants