Skip to content

fix(base): stop list searches when the result is known - #1548

Merged
Lorenzobattistela merged 2 commits into
bendlang:mainfrom
sebastian-griego:fix/list-search-short-circuit
Oct 13, 2026
Merged

Lorenzobattistela merged 2 commits into
bendlang:mainfrom
sebastian-griego:fix/list-search-short-circuit

Conversation

@sebastian-griego

@sebastian-griego sebastian-griego commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

On the C and JavaScript backends, List.any, List.all, List.find and List.contains evaluate the recursive search as a strict argument. Because of that they:

  • scan the whole list after the result is known,
  • run List.find's predicate from the end of the list, and
  • overflow the stack on long lists (interpreter and JavaScript, 50,000 elements).

This PR has each function call a tail-recursive .go helper that carries the previous result. The helper stops when any or contains finds a match, when all sees a false, or when find reaches its first match. The signatures, the empty-list results and support for affine elements stay the same.

tests/base/list_search.bend covers:

  • empty lists
  • that find returns the first match
  • affine closures
  • 50,000-element lists with a match last or no match

On main, the long-list cases overflow on the interpreter and JavaScript lanes.

Checks (macOS arm64, merged with main):

  • repo 54/54, test 1622/1622, perf 124/124
  • interpreter, C and JS give the same answers as main on small lists, with matches at the head, middle and tail, several matches, and none
  • an early match with a costly predicate after it: C 7.5s -> 0.5s, JS 45s -> 0.5s
  • 1,000,000 elements: main overflows on the interpreter and JS; this branch passes on all three
  • a ! call runs the same with --gpu on and off (Metal)

Not run: CUDA, Linux, Windows.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 13, 2026
@Lorenzobattistela
Lorenzobattistela merged commit 32d23b4 into bendlang:main Oct 13, 2026
1 check passed
MuhDur added a commit to bendlib/bendlib that referenced this pull request Oct 16, 2026
…ugh bridge lemmas

bend main after 2.0.37 redefines these Base functions (bendlang/bend#1548, #1459), and proofs that
unfold them stop checking (PLAN F39). Proofs now reach their cons cases through internal_ bridge
lemmas whose bodies are {==} on 2.0.37; research/candidates/base-next-bridges.bend holds the proofs
for the new Base, checked at bend 234cb9b. string.bend checks on bend main with them; list.bend is
partly converted. Seven frozen list lemmas have no proof on the new Base at all (bendlang/bend#1616).
All statements are unchanged; the full gate and --verdict of list and string pass on 2.0.37.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NJjQ47v3mvnKmc8XojZitg
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