Skip to content

The != and where sugar mean the Empty and Exists of the file they are written in, not the entry's - #1577

Merged
nicolas-abril merged 7 commits into
bendlang:mainfrom
HamedShams:fix/sugar-names-resolve
Oct 27, 2026
Merged

nicolas-abril merged 7 commits into
bendlang:mainfrom
HamedShams:fix/sugar-names-resolve

Conversation

@HamedShams

@HamedShams HamedShams commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1331.

The !=, exs and where sugar wrote Empty and Exists as bare names that never went through parse_reso. With Base loaded that is harmless, since the bare key is Base's and parse_fresh keeps anyone from redeclaring it. Without Base, the bare key is whatever the entry file declares, so a library's != meant the entry's Empty, and its where the entry's Exists. The issue's PROOF.bend certifies a false law that way, and a LAWS.bend that declares its own Empty checks alone but loses it the moment it is imported.

The sugar now takes its name from parse_own, in the file where it is written: the file's own Empty/Exists if the file declares one, else the bare name, which is Base's when Base is in scope; a user alias is not consulted, and when neither exists the usual "a defined name" error points at the sugar. The four sites write the call as plain Apps around Ref(parse_own(p, …)), so against main the diff is mostly Ref("X") → Ref(parse_own(p, "X")). Nothing changes for a book that imports Base. A local binder still cannot capture the codomain, since the sugar yields a top-level Ref, never a Var; tests/check/neq_qualified_capture.bend is unchanged.

@nicolas-abril's commits on this branch put the rest of the sugar through the same rule: Array.new, xs[i] and xs[i] <- (Array.get/Array.set and the U32 index), the infix operators and <>, and the &&/||/++ check. parse_own looks in one table: sugar that names a def or type (!=, &, |, &x: A -> B, the operators) looks in tlds, sugar that builds a constructor (<>) in ctrs, so a file's constructor named Empty, Pair, Or or Exists no longer takes over !=, &, | or &x: A -> B, and a def named Con no longer takes over <>. List and tuple literals ([a, b], (a, b)) now build the file's own Con/Nil/Tuple, as <> already did. Char, string and number literals (Zero/Succ) stay bare, as on main, like the Lit nodes they unfold from: a file with its own Nat gets a type error rather than its own constructors, which also avoids a stack overflow on large literals. The one name the literal path still wrote bare was the Nat.add a big literal (300n+x) lowers to; the fifth commit sends it through parse_own as well, since a Base-free LAWS file's 300n+Zero{} otherwise reaches for the proof file's Nat.add and a law that 300 is zero certifies (tests/import/big_lit_add pins the pair, refused now). The literals themselves stay plain.

Of the two directions the issue lists, this is the second. Binding the sugar to Base only would refuse != and where in a Base-free book outright; resolving in place keeps a Base-free module's oWhat I ran: every file under tests/ through bend <file> on main and on each commit of this branch, byte-identical output on the shared files (1,641 files at the time). Rebased on main (757180e); gates on Mac: repo 54/54 (bend.ts at 44,493 of 48,000, main is 44,345), test 1768/1768, perf 124/124, ping 80/81 (the BendHub lic_bom--name check, which fails on main too). Not run: safe (needs a bendtt build), the cluster gates.

@nicolas-abril nicolas-abril self-assigned this Oct 15, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

This comment and commit ba7e90a were written by an AI agent (Unii) working for @nicolas-abril, without line-by-line human review.

This PR fixed !=, exs and where, but #1331 also covers other shortcuts that wrote bare names, so a module's meaning still depended on the file that imports it. ba7e90a applies the same idea everywhere the parser builds names for you: each one now goes through parse_reso / parse_call when it's parsed.

  • operators: ||, &&, ++, &, |, <> and the other non-dotted infix ops
  • list, tuple, char and number literals, through a new parse_lit, plus Nat.add for big literals
  • number and list patterns (case 0n:, case 1n+k:, case []:)
  • array shortcuts (^d, [ix], <-) and the U32 index

New tests in tests/import:

  • or_own_bool: the || attack from the issue. It's refused.
  • lit_own_nat, list_own, pat_own: a Base-free module with its own Nat/List. These used to fail ("unknown: Zero", or a mismatch with the entry's constructors) and now run with the module's own constructors.

Checks (macOS M4): repo 54/54, ping 57/57, test 1643/1643, perf 124/124; safe.ts 1654 files, 0 false rejects. Linux and CUDA were not run.

Not covered: a Nat/String/U32 literal whose type is Base's (or the entry's) stays a Lit and is expanded later by the checker. That's safe, because those types' constructors are the plain names. bend.ts grows by about 160 tokens.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

🤖 Unsupervised AI comment (posted on behalf of @nicolas-abril, not reviewed by a human).

Pushed 3819102, which replaces ba7e90a's approach:

  • Literals use bare names again, as before this PR (Nat, U32, String, Con/Nil, Succ, …). A file with its own Nat gets a type error, not its own constructors. This removes the stack overflow on large own-Nat literals and the namespace threading through lit_of/lit_step/word_to_term.
  • The other sugar goes through one helper, parse_own (the file's own name if it has one, else the bare one): != → Empty, where/exists → Exists, Array.new, xs[i] / xs[i] <- (Array.get/set, U32 index), the infix operators and <>, the &&/||/++ check, and the xs[i] <- read-back. This covers review findings 2, 3, 5 and 6. Finding 5 (the String alias shadowing String.append) is fixed by not going through the alias expansion.
  • Dropped the 8 tests/import/*_own* files that expected literals to build the file's own constructors.

bend.ts is +98 tokens over bf80636. Gates: repo 54/54, ping 57/57, test 1641/1641, perf 124/124; safe.ts 1654 files, 0 false rejects. Not run: Linux, CUDA, hub.

A follow-up PR will give the root file its own namespace, the way an imported file already has one. Bare sugar names then can only mean Base's, which is the underlying cause of #1331.

@HamedShams

Copy link
Copy Markdown
Contributor Author

@nicolas-abril Thanks for carrying it through the rest of the sugar; I left the infix table out on purpose, so good to see it go through the same helper. Keeping literals bare makes sense too, a type error on a file’s own Nat beats a stack overflow. Pulled 3819102 locally and the sweep here agrees with your gates.

Also tried #1585 on top of this branch to see how the two sit together: one small conflict, both rewrite the tail of parse_reso and with that resolved every file in tests/import still matches, root_ns_empty included. So whichever lands second shouldn’t have much to re-pin.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

I pushed 9a795af on top of your branch. It drops parse_sugar and writes the sugar calls as plain Apps around Ref(parse_own(p, …)).

Why: the only new rule here is the name rule. Sugar takes the file's own name if it has one, else the bare one, and user aliases don't apply. parse_own is that rule, and it's needed on its own anyway, for the <> constructor, the operator head, and the checks in parse_term_ns and term_write. parse_sugar added a second call builder that was almost a copy of parse_call with a different resolver. Its one fixed span also didn't fit the where site (Ref on ws, Apps on s), which is why that site had App(App(parse_sugar(p, "Exists", [], ws), A, s), …). With plain Apps, the diff against main is mostly Ref("X") → Ref(parse_own(p, "X")).

It's also smaller: bend2/bend.ts goes from 43,878 to 43,865 ttok (main is 43,767). I tried a generic term_call(f, xs, s) helper too, but it came to 43,884, because most of these sites take two arguments and two Apps are shorter.

Checked: all 1,654 tests/**/*.bend files through bun bend2/main.ts give byte-identical output before and after this commit, and bun gates/repo.ts passes.

The PR description still says the four sites use parse_call, which no longer matches the code. Could you update it? Also, Ref("Nat.add") in the n + x literal case (bend2/bend.ts:2237) is still a bare name. That's fine to leave, since literals are out of scope here, but it has the same problem.

@HamedShams

Copy link
Copy Markdown
Contributor Author

@nicolas-abril Thanks for the cleanup; pulled it here and the sweep agrees. Description updated to match.

On Nat.add: I took the hint and sent it through parse_own too, as its own commit on top of yours, since it was the last name the sugar still wrote bare. It closes the case Lorenzo mentioned on #1585: a Base-free LAWS file with 300n+Zero{} and a proof file that supplies its own Nat.add certified "300 is zero" on this branch. tests/import/big_lit_add pins the pair. Literals themselves stay plain, as you set them. If you'd rather keep that out of this PR, dropping the last commit is all it takes.

Also merged the branch onto today's main locally: clean, gate green, every test file identical.

HamedShams and others added 7 commits October 27, 2026 20:32
… written in, not the entry's

The !=, exs and where sugar wrote Empty and Exists as bare names that
never went through parse_reso. Without Base in scope the bare key is
whatever the entry file declares, so a library's != negated into the
entry's Empty and its where ranged over the entry's Exists: a false law
certified, and a module that declared its own Empty lost it once
imported.

The four sites now build the reference with parse_call, through
parse_reso in the file where the sugar is written. With Base loaded
nothing changes, and a local binder still cannot capture the codomain,
since the sugar yields a top-level Ref.

Closes bendlang#1331.
…list, tuple, char and number literals, number and list patterns and array shortcuts resolve through parse_reso at parse time, so an importing file can't change what they mean
…n, which takes the file's own name if it has one, else the bare one

Covers != (Empty), where/exists (Exists), Array.new, xs[i] and xs[i] <- (Array.get/set, U32 index), the infix operators and <>, the &&/||/++ check, and the xs[i] <- statement read back. Literals keep the bare names, as before this PR: a file with its own Nat gets a type error, not its own constructors.
… is gone, since the name rule is the only new thing

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…tten

`300n+x` lowers to Nat.add applied to the literal, and that Nat.add was
the one bare name the sugar still wrote, so a Base-free module's big
literal reached for whatever Nat.add the entry declared. A LAWS file
with its own Nat and the law "300n+Zero{} is zero" certifies when the
proof file supplies a Nat.add that returns its second argument. The
reference now goes through parse_own like the rest of the sugar: the
file's own Nat.add if it declares one, else the bare name. Literals
themselves are untouched; `Lit`, `Zero` and `Succ` stay the plain
names. tests/import/big_lit_add pins the pair: the module alone is
missing Nat.add as before, and the entry's proof is refused instead of
certified.
… tlds, sugar that builds a constructor in ctrs; list and tuple literals go through it too

A file's constructor named like Base's type (Empty, Pair, Or, Exists) no longer takes over !=, &, | or &x: A -> B, and a def named Con no longer takes over <>. [a, b] and (a, b) now build the file's own Con/Nil/Tuple, as <> already did. Char, string and number literals stay bare, like the Lit nodes they unfold from.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril
nicolas-abril force-pushed the fix/sugar-names-resolve branch from 255c954 to 1f1ff5a Compare October 27, 2026 19:33
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Two commits on top of the branch (dd78ff78, 255c954e) from review feedback:

parse_own looks in one table. It used to treat a name as the file's own if it was in either tlds or ctrs. That let a constructor take over sugar that names a type, and the reverse. In a library that imports Base:

  • a constructor named Empty, Pair, Or or Exists took over !=, &, | or &x: A -> B, and the check failed with "a defined name";
  • a def Con took over <>, which then built a constructor that doesn't exist.

Now sugar that names a def or type looks only in tlds, and sugar that builds a constructor (<>, list and tuple literals) looks only in ctrs. parse_reso is unchanged and still looks in both.

List and tuple literals go through parse_own. [a, b] and (a, b) used to write bare Con/Nil/Tuple while <> used the file's own Con, so x <> [] mixed the two. An entry file could also still supply a library's list and tuple constructors. Char, string and number literals stay bare, since they unfold from Lit nodes into bare constructors.

Also dropped parse_own's header comment, to match the other parse_* functions.

Tests: new tests/import/sugar_ctr_tld* (constructors and defs named like Base's types and constructors, with Base) and tests/import/sugar_list_own* (a Base-free library's own List and Tuple, under an entry that declares its own). Both fail on the previous head and pass now. Every file under tests/ gives the same output as the previous head, apart from the new tests and proof/app_main (an endless loop that hit the 120 s timeout in both). bun gates/repo.ts passes. Cluster gates not run.

Performance: the parse_own calls on the parser path don't measurably cost anything. Medians against the branch's merge base: the 5 checker benches are within -0.5% to +1.2% (about one stddev), and compile time (-o main.c) on the 17 runtime benches has a geomean of -0.3%. The emitted C is byte-identical.

🤖 Generated with Claude Code

@nicolas-abril
nicolas-abril merged commit 7d7ce1c into bendlang:main Oct 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants