Skip to content

The root file gets its own namespace, so sugar's bare names can only mean Base's - #1585

Open
nicolas-abril wants to merge 3 commits into
mainfrom
ai/root-ns
Open

nicolas-abril wants to merge 3 commits into
mainfrom
ai/root-ns

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 Written by an AI agent for @nicolas-abril; not yet reviewed by a human.

The root file and Base shared the empty namespace. Because of that, a bare name written by sugar (Empty for !=, Exists for where, Nat/Succ for literals, Con/Nil for lists, Tuple for pairs, Type.op for operators) resolved to whatever the root file declared. One result is that a module's != law could take its Empty from the entry that imported it (#1331).

With this PR the root file loads under its own namespace, the one an import of it would get (its file name). Base keeps the empty one. Sugar itself doesn't change: it still writes Base's bare names. In a file without Base those names are undefined, and a literal never means a file's own Nat or List. This matches the guide: "Operators, literals and brackets are sugar for names in Base."

Code (bend.ts +136 tokens, main.ts +110):

  • book_read loads the root under basename(file). tlds["main"] points at the root's main.
  • name_key prints the root's own names bare, so output is unchanged.
  • cli_verdict decides whether a def belongs to the root by checking the root's namespace.
  • Fixed a bug that already existed in imported modules: a forward reference to a dotted type applied to <..> was qualified twice (lib:lib:Vec.Con).
  • parse_qual now builds each ns:name only once, and parse_reso checks the far name only when the own one is missing. Without these two changes, the longer root keys made generics_3200 about 7% slower and pushed it past the perf gate's 1.15x limit.

Dropping the TLD b flag (dc7c14d). Once the root file has its own namespace, a name with no : belongs to Base. The b ("from Base") field on top-level definitions doesn't tell you anything the name doesn't, so this commit removes it:

  • Bend.is_base(k) (the name has no : and no ~) replaces every read of b in bend.ts, comp.ts, main.ts and safe.ts.
  • b also marked a template's stand-in constants (f~T) while def_check runs. That job now has its own flag, o.
  • Removed the checks that refused a root file for declaring a name the compiler encodes itself (OWNED in comp.ts, plus the b guards in intr_of, io_base, literals and foreign IO). The root's Nat is now file:Nat, so it can't be taken for Base's. A Base-free file that declares its own Nat now builds, where main refused it with "Nat is a name the compiler encodes itself".
  • book_load refuses the empty namespace for any file except Base. gates/test.ts (state_run) loads each file under its own namespace, the way the CLI does.
  • Tokens against the previous head: bend.ts +59, comp.ts -111, safe.ts -5, main.ts -1.

bend-lint (22764c3). bend-lint found the root file by the empty namespace, so on this PR its self-check threw DriftError on every file. tools/bend-lint/src/seam.ts now finds the root by its namespace and shows rules the root's own names bare: fact names, owners, the defs/names filters, body(name) and the self-check. Rules and their configs don't change. The format guard also deletes the main alias when it removes ns:main. Without that, every format fix failed with "duplicate declaration: main". The tests that read bend's internal data now expect ns:name keys. bend-lint: 126/126 pass, the same as main. The gates don't run bend-lint.

Behaviour change. These Base-free tests declare their own Nat/String/List/U32 constructor in the root and relied on literals or printing using them. Their expected output changed:

Relation to #1577. #1577 fixes #1331 from the module's side, making sugar use the writing file's own name. This PR fixes it from the entry's side. Whichever merges second will need its tests/import expectations re-pinned.

Checks (macOS M-series, at 22764c3): repo 54/54, ping 57/57, test 1637/1637, perf 124/124; bend-lint 126/126. safe.ts on 1650 files: 0 false rejects (run with dc7c14d; 22764c3 only touches bend-lint and doesn't change bend2/). No new tsc errors in bend2/. Not run: bend-lint's tsc (its @types/bun dev dependency isn't installed here), Linux, CUDA, hub corpus.

…mean Base's

The root file and Base shared the empty namespace, so a bare name written by
sugar (Empty for !=, Exists for where, Nat and Succ for literals, Con and Nil
for lists, Tuple for pairs, the operators' Type.op) resolved to whatever the
root file declared. A module's != law could take its Empty from the entry
that imported it (#1331). The root now loads under the namespace an import
of it would get (its file name); Base keeps the empty one. Sugar is
unchanged and still writes Base's bare names: in a file without Base they
are undefined, and a file's own Nat or List is never what a literal means.

- tlds["main"] points at the root's main, for the runner and the compiler.
- name_key prints the root's own names bare, as before.
- cli_verdict's "own def" test reads the root's namespace.
- A forward reference to a dotted type applied to <..> was qualified twice
  (lib:lib:Vec.Con); it already happened in imported modules.
- parse_qual builds each ns:name once, and parse_reso tests the far name
  only when the own one is missing: the longer root keys cost the checker
  benchmarks about 7% otherwise.

Tests: the Base-free tests whose root declares its own Nat, String, List or
a U32 constructor now expect the refusal (nat_literal_*, string_literal_own_scon,
safe_box_word_def, safe_parameterized_box_word) or print the constructors
(cop_bare_trio_000, flatten type_param_match_000); beta_ann_match and
grade accept_data spell their constructors; show cons_chain_readback uses
Base's List. The checker benchmarks spell Tuple{..} and Pair(..) for their
own Sigma. New: import/root_ns_empty (#1331 from the entry's side).
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

This fixes #1331: every capture repro we tried is refused, including one #1577 misses (a Base-free LAWS with 300n+Zero{}, where the entry defines its own Nat.add). Gates pass on the PR merged with main d2aeefc (repo 54/54, test 1637/1637, ping 59/59, safe 0 false rejects, perf 124/124). We found some problems the gates don't catch:

  1. Concurrent loads from JS export the wrong names. ROOT.ns is one global that name_key reads. book_read sets it and then awaits, so a second .bend file loading at the same time overwrites it before the first is compiled. With alpha.bend (inc, big) and beta.bend (dbl):

    import A from "./alpha.bend"
    import B from "./beta.bend"
    console.log(Object.keys(A), Object.keys(B), A.inc?.(1))

    bun --preload bend2/main.ts st.mjs prints [ "inc", "big" ] [ "dbl" ] 2 on main and [ "alpha.inc", "alpha.big" ] [ "dbl" ] undefined on the PR. A single import is fine.

  2. tests/check/safe_unit_model.bend goes from agree to out of scope. safe.ts:572 looks up tlds["Unit"], but the root's own Unit is now safe_unit_model:Unit, so --verdict fails with "no model for idt~A" and the .bendtt loses opaque idt_7e_A = Unit. The test gate still passes because it only checks the output Mk{}.

  3. The root's namespace can clash with an import. The namespace comes from path.basename(file) (main.ts:931). A root file named util (no extension) that imports ./util.bend as U can call one() bare, and it resolves to U.one (main refuses it). If both define the same name, the PR fails with "duplicate declaration" where main runs. Same through a symlink whose name matches an import.

  4. A root named 0x….bend is dropped from --publish. The ns.startsWith("0x") filter at main.ts:757 was meant for hub namespaces and now matches the root: 0xab.bend importing lib.bend packs ["lib.bend"] with entry lib.bend. With no imports files is empty. Line 761 drops the root's foreign effect files the same way (from reading only).

  5. Missing file error. bend nope.bend used to print no such file: nope.bend; now fs.realpathSync in book_read throws the raw ENOENT: … lstat 'nope.bend'.

  6. Refusal messages are misleading in Base-free files. Since the root's names print bare, a file that declares its own Empty gets expected : a defined name / observed : Empty, and own-type literals get a declared constructor (Nat declares Zero, Succ) / observed : 4097n or (List declares Nil, Con) / observed : [A{}]. It should say the sugar or literal needs import Base, as Without import Base, != and where/exs are parsed as bare Empty/Exists, so PROOF.bend decides what an imported LAWS.bend's != and where mean, even when LAWS.bend declares its own and checks alone #1331 suggested.

  7. Literal behavior changes. Base-free files can no longer use literals at their own Nat/String/List. That reverses A literal trusts a user-defined Nat/String constructor name, forges its field type, and admits Empty #941, fix(verdict): preserve Base-free large Nat literals #1337 and fix(verdict): respect Unit provenance and word constructor families #1338 (GUIDE:573 does support the new rule). If that's intended, GUIDE should say so. The != and where sugar mean the Empty and Exists of the file they are written in, not the entry's #1577 keeps own Empty/!=/where working in Base-free files and conflicts only in parse_reso; merged together, every repro is still refused. So merging The != and where sugar mean the Empty and Exists of the file they are written in, not the entry's #1577 first and rebasing this on it may be the way to go.

Smaller: C ids now depend on the file name (CID_Mk -> CID_app_Mk), and gates/test.ts:218 still loads files with an empty namespace.

Separately, a heads up: bend-lint (tools/bend-lint) stops working with this change. seam.ts:676 and :687 find the root by ns === "", so its self-check throws DriftError on every file (tests 127 -> 0, and the CLI exits 0 with no findings). It's community-maintained, so this isn't a blocker here, just something its maintainers will need to update.

@HamedShams

Copy link
Copy Markdown
Contributor

One more for the list, from #1514. This fixes the <..> head site there (the issue’s entry prints 6 now), but the \{..} arm @viridale found still resolves its name twice, and since the root file now has a namespace, that case breaks in a root file too, not only in an import. His forward_dotted_lib.bend from that thread prints 7 on main and is refused here at 22764c3:

- expected : a constructor of Cell.At (missing, or already matched)
- observed : \{forward_dotted_lib: (v => v)}

Either his three-line guard at the top of parse_reso (return a key that already has a :) or the same Ref check you used for <..> fixes it. With the guard on this branch, both his files pass and the import cases still print 6.

@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

🤖 Written by an AI agent for @nicolas-abril; not yet reviewed by a human.

I pushed two commits on top of bccd6d1. The PR description now covers both.

  • dc7c14d removes the TLD b flag. After this PR a name with no : is Base's, so Bend.is_base(k) replaces it. Template stand-ins (f~T) get their own flag, o. The OWNED / "a name the compiler encodes itself" checks are removed because the root's own Nat is now file:Nat. This commit also covers @Lorenzobattistela's smaller note: gates/test.ts now loads each file under its own namespace.
  • 22764c3 fixes bend-lint, the heads-up at the end of the review above. seam.ts now finds the root by its namespace instead of "" and shows rules the root's names bare. bend-lint passes 126/126, the same as main.

At 22764c3: repo 54/54, ping 57/57, test 1637/1637, perf 124/124. safe.ts has 0 false rejects on tests/. Not run: Linux, CUDA, hub.

This doesn't address review items 1–7 or @HamedShams's \{..} case yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants