Skip to content

A literal is one node per kind: Lit{k, v} steps through lit_step, and U32 and F32 literals are kinds - #1003

Closed
nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:lit-kinds
Closed

nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:lit-kinds

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Draft: this needs a decision on bend2/bend.ts's ttok cap before it can land (see Size below). It is an alternative to #936, which adds U32 and F32 literals by tagging Lit with an optional k.

What this changes

Since #907 and #924, a string or nat literal is one Lit node that unfolds one constructor at a time where it is read. Its kind was implicit in typeof v, and each reader (the step, the full unfolding, check-lit, the printer, nat_from_term) spelled out Nat and String by hand. #936 extends that to U32 and F32 with a k !== undefined branch at each of those sites.

Here every literal carries its kind, and the kind decides the payload's type:

export type Lit =
  | { k: "Nat"; v: number }
  | { k: "String"; v: string }
  | { k: "U32"; v: U32 }
  | { k: "F32"; v: U32 };

lit_step is the only code that knows a kind: one case per kind, returning the constructor it unfolds to. Everything else reads a literal through it:

  • the termination check, the match frame, term_compare (a mixed Lit/Ctr pair steps the literal) and infer-err, as before;
  • check-lit takes the head constructor from the step instead of a Zero/Succ/SNil/SCon ternary;
  • the printer prints a literal as its first step, so the existing Nat, String, U32 and F32 sugar does the work;
  • lit_full (patterns, and the compiler's term_force) is one worklist that steps any literal it finds in a step's fields;
  • two literals are equal when their kind and payload are.

So a new kind is a Lit variant, its case, and its parse rule. Kinds nest: a string's step holds its character as Chr{U32 literal} rather than a 33-word chain, and a char literal 'c' parses to the same 2-node shape. Ctr and u32_to_term return the Ctr variant, so lit_step needs no cast, and narrowing on k types v, so the readers need none either.

In comp.ts only the big-Nat case of term_force changes (s.k === "Nat").

Measurements

Apple M4, min of 5, seconds / peak MB:

main #936 this
2000 defs, each a U32 literal, --check-only 0.28 / 334 0.06 / 107 0.06 / 120
2000 defs, each an F32 literal, --check-only 0.28 / 339 0.06 / 108 0.06 / 119
a 200-case U32 match, --check-only 1.11 / 1167 0.19 / 236 0.21 / 242
the same, -o main.c 1.82 / 1389 0.42 / 358 0.43 / 360
demos/app_slash_boss_3d --check-only 0.49 / 526 0.17 / 199 0.18 / 201
demos/app_slash_boss_3d -o main.c 2.01 / 2238 1.44 / 1814 1.49 / 1692

The extra ~12 MB against #936 on the literal-per-def files is check-lit building a U32's first step (its word) where #936 reads the head from the tag. String-heavy programs (2000 string defs, a 100-case string match) are unchanged from main on all three.

Tests

  • The C, JS and stderr output of every test, bench and demo with a main (2411 files) is byte-identical to main.
  • The interpreter lane (bend t.bend) over all 1429 tests under tests/ prints the same as main.
  • Feature/u32-and-f32-to-literals #936's nine tests (check/, compile/, parse/, printer/ and proof/ *_literal_*, parse/nat_plus_u32) are included and pass; the two compile tests print right on C and JS and emit the same code as main.
  • The cluster gates have not been run.

Size

bend.ts 42979 → 43217 ttok, over the 43000 cap in gates/repo.ts, so bun gates/repo.ts fails. #936 costs about the same and raises the cap to 43200. The choice is to raise the cap to ~43300 or to find that much elsewhere in the file.

The u32/f32 tests are @MattCozendey's, from #936.

🤖 Generated with Claude Code

… String, U32, F32), and lit_step steps it one constructor out, so U32 and F32 literals are one node too

Checking, comparing, printing, patterns and the compiler all read a
literal through lit_step, so a new kind is a Lit variant, its case and a
parse rule. A char literal and a string's step hold the character as
Chr{U32 literal}. Ctr and u32_to_term return the Ctr variant. The u32/f32
tests are from bendlang#936.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Sep 23, 2026
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
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Sep 24, 2026
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
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.

1 participant