Repository navigation
Feature/general literals - #1004
Conversation
|
One gap in the new check-lit rule (bend.ts#L3637): the fast path ignores the ADT's residual if (t_wnf.$ === "ADT" && t_wnf.k === tm.k && book.tlds[tm.k]?.b === true) {A type like 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 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 A smaller, unrelated nit: Everything else I checked matches
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. |
|
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. |
…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
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
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
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
29233a3 to
2ab44d6
Compare
… 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]>
A literal carries its Base type: one
Litfor Nat, String, U32, F32 and CharThis 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
Litnode that unfolds one character at atime where it is read.
Litto U32 and F32 with an optional tagk. Each reader(the step, the full unfolding, check-lit, the printer,
nat_from_term, thecompiler) got a
k !== undefinedbranch next to itstypeof vbranch.kind, and
lit_stepis 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:
ktypesv, andLit("Nat", "x")does not compile.lit_stepunfolds one constructor. Its fields can be literals again: aString's step and a char literal
'c'hold the code asChr{Lit(U32)}, onenode, not a 65-node Word chain. Char needs no kind of its own.
Lit(k, v)checks at Base's typek. Baseloads 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_findrule accepted, with no step built.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).
does.
lit_fullis gone.term_constin the compiler answers "constant" for a literal at once. Itunfolded each U32 literal into 65 nodes to find every node constant: 75%
of the compiler's U32 unfolds in
pure_hvm5_minicame from there.lit_callis the one place for the exception, a Nat past the cap.CtrandLitnodes, the same in both bodyrepresentations, so the checker and the compiler use it without a
term_highercopy.Removed:
lit_full,u32_to_term,lit_chain, everytypeof vandk === undefinedtest, and the per-kind printer branch. The per-kind code islit_stepand the parser.Correctness
bend2/bend.leanmodels literals as their constructor trees ("Literals arebase.bend constructors, not calculus"), so the proof holds while these hold:
Litmeans one closed tree of Base constructors, the treethat
mainparses.Lit(k, v)only at Base's typek.kandvare equal.term_compare,term_descend, the match frame,parse_patt, infer-err, the compiler'sterm_force);u32_from_termreads a U32 literal's value directly.Nothing mutates a step's nodes, so sharing a step is safe;
mainshares itsparsed 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 aregenerated scratch files, not part of the PR.
-o main.c-o main.c-o main.capp_slash_boss_3dcheckapp_slash_boss_3d/PROOFcheckpure_hvm5_mini-o main.cpure_par_sortcheckbench/runtime/raytrace-o main.cSummary of the full table below (87 cases), time / memory:
-o main.c(16)-o main.c(7)mainand uses12% to 77% less memory.
other. Most of their gain over
maincomes from the U32 and F32 literalsin
base.bend, whichmainbuilds as full chains at parse time.PROOFcheck ofapp_slash_boss_3dis within noise ofmainin time(+2% here, −3% and +2% in two earlier runs) and uses 62% less memory.
app_slash_boss_3d(-o main.c) in 9.28 son
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 programcompiles almost as slowly as on
main; its check-lit builds a U32's word toread the head constructor; and its
u32_from_termbuilds the word to read avalue the literal holds.
Full table: every checker bench, runtime bench and demo, and the literal programs
The
LAWS.bendfiles exit 1 on all three trees: they are laws without proofs.-o main.c-o main.c-o main.cbench/checker/compute_1600bench/checker/defs_12800bench/checker/generics_3200bench/checker/proofs_3200bench/checker/trees_400bench/runtime/bfs-o main.cbench/runtime/editdist-o main.cbench/runtime/gameoflife-o main.cbench/runtime/hashmap-o main.cbench/runtime/kmeans-o main.cbench/runtime/lexer-o main.cbench/runtime/mandelbrot-o main.cbench/runtime/merkle-o main.cbench/runtime/nbody-o main.cbench/runtime/queens-o main.cbench/runtime/raytrace-o main.cbench/runtime/symreg-o main.cbench/runtime/terrain-o main.cbench/runtime/tree-bitonic-o main.cbench/runtime/tree-matmul-o main.cbench/runtime/tree-radix-o main.capp_pong_game_2d/LAWScheckapp_pong_game_2d/PROOFcheckapp_pong_game_2d/maincheckapp_ray_tracer_3d/LAWScheckapp_ray_tracer_3d/PROOFcheckapp_ray_tracer_3d/maincheckapp_slash_boss_3d/LAWScheckapp_slash_boss_3d/PROOFcheckapp_slash_boss_3d/bend3dcheckapp_slash_boss_3d/maincheckapp_triangle_2d/LAWScheckapp_triangle_2d/PROOFcheckapp_triangle_2d/maincheckapp_win_is_bug_2d/LAWScheckapp_win_is_bug_2d/PROOFcheckapp_win_is_bug_2d/maincheckio_hello_world/LAWScheckio_hello_world/PROOFcheckio_hello_world/maincheckio_hello_world-o main.cio_http_fetch/LAWScheckio_http_fetch/PROOFcheckio_http_fetch/maincheckio_http_server/LAWScheckio_http_server/PROOFcheckio_http_server/maincheckio_rollback_netcode/netcodecheckio_rollback_netcode/servercheckio_rollback_netcode/walkers_democheckio_rollback_netcode/walkers_testcheckio_tcp_echos/LAWScheckio_tcp_echos/PROOFcheckio_tcp_echos/maincheckproof_insertion_sort/LAWScheckproof_insertion_sort/PROOFcheckproof_insertion_sort/maincheckproof_insertion_sort-o main.cproof_numerics/LAWScheckproof_numerics/PROOFcheckproof_numerics/maincheckproof_numerics-o main.cproof_typed_eval/LAWScheckproof_typed_eval/PROOFcheckproof_typed_eval/maincheckproof_typed_eval-o main.cpure_hvm5_mini/LAWScheckpure_hvm5_mini/PROOFcheckpure_hvm5_mini/maincheckpure_hvm5_mini-o main.cpure_par_sort/LAWScheckpure_par_sort/PROOFcheckpure_par_sort/maincheckpure_par_sort-o main.cpure_par_sum/LAWScheckpure_par_sum/PROOFcheckpure_par_sum/maincheckpure_par_sum-o main.cSize
bend2/bend.ts(ttok)bend2/bend.ts(lines)bend2/comp.ts(ttok)lit_call)Code diff: 67 lines added, 87 removed.
bend.tsstays under the 44000 cap.Tests
eval and halt namespaces give the same result as
mainon the check,interpreter, JS and C lanes, judged as
gates/test.tsjudges them, andtheir emitted JS and C are byte-identical to
main.showandchecktests give the same result as before theprinter change.
demos that run on the CPU, are byte-identical to
main.tscreports 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
Wordliteral (U32{Lit(Word, v, 32)}): the checker reads all32 bits where a U32 unfolds (
term_compareagainst computed values, theWord.*functions), so a lazy word adds nodes; it also needs a width field,a Nat-index test in check-lit and a
Wordcase inu32_from_term.PR, it gained nothing.
T.literal): a language feature, not thischange.
🤖 Generated with Claude Code