Skip to content

Unbalanced Array: the checker proves size 4, JS returns 3, native fail-stops #808

Closed as not planned

Description

@jasisz

What you did

Ran this file from the current main checkout:

import Base

def tree() -> Array<U32>:
  ANode{ANode{ALeaf{10}, ALeaf{20}}, ALeaf{30}}

def observed() -> U32:
  Pair.snd(Array<U32>, U32, Array.size(U32, tree()))

def theorem() -> {observed() == 4 : U32}:
  {==}

def main() -> IO(Unit):
  IO.print(U32.show(observed()))
bun bend2/main.ts array_soundness.bend
bun bend2/main.ts array_soundness.bend -o array_soundness.js
node array_soundness.js
bun bend2/main.ts array_soundness.bend -o array_soundness
./array_soundness

What happened

The checker accepts theorem, so it proves that observed() is 4.

The JS lane and emitted JS both print:

3

The native binary exits with code 1:

bend: runtime fail-stop

Array<T> is an unrestricted ALeaf | ANode(Array<T>, Array<T>), so the unbalanced tree above is well-typed. The source Array.size follows the left child and doubles its size, which gives 4.

The JS representation flattens ANode with $0.concat($1), reconstructs children by splitting at $0.length >> 1, and implements Array.size as $0.length. It loses the original boundary and returns the number of leaves, 3. The native representation fail-stops on the same value.

The runtime paper calls Array<T> a perfect binary tree, but neither the type nor the checker enforces that. Its constructors are public, while the backends assume both children have equal depth. This uses no @unsafe, FFI, open law, or ?TODO.

This is different from #804. That issue is about the allocation cost of split/join on regular arrays. Here a checked equation becomes false at runtime.

Bend version

bend 2.0.5
main @ 0b7e2b11

Operating system

Darwin arm64

C compiler version

Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

  1. VictorTaelin commented on Sep 18, 2026

    @VictorTaelin
    Contributor

    (deleted the AI comment)

    I asked the AI so many times to ensure the array is depth-indexed. I have no idea how I let this slip through. sighs

    I'm tired

    will fix soon, thanks for the report

    I hate AI

  2. VictorTaelin commented on Sep 18, 2026

    @VictorTaelin
    Contributor

    Decision: this stays a runtime limit, recorded in WONTFIX.txt under a new RUNTIME section (things the theory allows and the runtime does not run yet; we remove these over time).

    We implemented a depth-indexed Array(T, d) and it worked, but Bend does no inference, so every Array.* call and the index sugar would have to name the depth: xs[i^d] instead of xs[i]. We chose not to pay that. An unbalanced tree checks and fail-stops at runtime on every lane ("runtime fail-stop"), like a Nat past 2^48-1 or an array past block class 31.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions