Skip to content

A product over an indexed type family dies with a memory fault from depth 2 unless a second recursion over the same types is reachable #901

Closed

Description

@Lorenzobattistela

What you did

bend repro.bend -o r && ./r 1 && ./r 2

What happened

bend 2.0.20 (release binary; 2.0.16 and 2.0.18 behave the same), Darwin
arm64 (Apple M4 Pro), Apple clang 21.0.0, macOS 26.5. The checker
accepts the file and the interpreter prints the right answer at every
depth.

The file

Mat(r, c) is a family of 2^r x 2^c floats: a leaf holds one float, a node is split in two by rows, in two by columns, or in four. Mat.matmul is the product over three depths (rows of a, the shared side, columns of b), eight cases, its operands shared with +. main reads the depth d from the command line and prints the sum of ones times ones at 2^d x 2^d, which is 8^d: 8, 64, 512, 4096 for d = 1, 2, 3, 4.

Full file:

import Base

# Mat(r, c): 2^r rows and 2^c columns of F32, a leaf holding one float,
# a node split in two by rows, in two by columns, or in four
law Mat:
  for r: Nat
  for c: Nat
  Data

type Mat.Leaf is Data:
  MatB{x: F32}

type Mat.Rows<-p: Nat, -c: Nat> is Data:
  MatR{top: Mat(p, c), bot: Mat(p, c)}

type Mat.Cols<-r: Nat, -q: Nat> is Data:
  MatC{left: Mat(r, q), right: Mat(r, q)}

type Mat.Quad<-p: Nat, -q: Nat> is Data:
  MatQ{tl: Mat(p, q), tr: Mat(p, q), bl: Mat(p, q), br: Mat(p, q)}

def Mat(r, c):
  match r c:
    case 0n 0n:
      Mat.Leaf
    case 1n+p 0n:
      Mat.Rows<p, 0n>
    case 0n 1n+q:
      Mat.Cols<0n, q>
    case 1n+p 1n+q:
      Mat.Quad<p, q>

def Mat.full(+r: Nat, +c: Nat, +x: F32) -> Mat(r, c):
  match r c:
    case 0n 0n:
      MatB{x}
    case 1n+p 0n:
      t b = Mat.full(p, 0n, x) Mat.full(p, 0n, x)
      MatR{t, b}
    case 0n 1n+q:
      l r2 = Mat.full(0n, q, x) Mat.full(0n, q, x)
      MatC{l, r2}
    case 1n+p 1n+q:
      a b c2 d = Mat.full(p, q, x) Mat.full(p, q, x) Mat.full(p, q, x) Mat.full(p, q, x)
      MatQ{a, b, c2, d}

def Mat.add(+r: Nat, +c: Nat, m: Mat(r, c), n: Mat(r, c)) -> Mat(r, c):
  match r c:
    case 0n 0n:
      MatB{x} = m
      MatB{y} = n
      MatB{(x + y : F32)}
    case 1n+p 0n:
      MatR{mt, mb} = m
      MatR{nt, nb} = n
      x y = Mat.add(p, 0n, mt, nt) Mat.add(p, 0n, mb, nb)
      MatR{x, y}
    case 0n 1n+q:
      MatC{ml, mr} = m
      MatC{nl, nr} = n
      x y = Mat.add(0n, q, ml, nl) Mat.add(0n, q, mr, nr)
      MatC{x, y}
    case 1n+p 1n+q:
      MatQ{ma, mb, mc, md} = m
      MatQ{na, nb, nc, nd} = n
      w x y z = Mat.add(p, q, ma, na) Mat.add(p, q, mb, nb) Mat.add(p, q, mc, nc) Mat.add(p, q, md, nd)
      MatQ{w, x, y, z}

def Mat.add4(+p: Nat, +q: Nat, a0: Mat(p, q), b0: Mat(p, q), a1: Mat(p, q), b1: Mat(p, q),
             a2: Mat(p, q), b2: Mat(p, q), a3: Mat(p, q), b3: Mat(p, q)) -> Mat(1n+p, 1n+q):
  w x y z = Mat.add(p, q, a0, b0) Mat.add(p, q, a1, b1) Mat.add(p, q, a2, b2) Mat.add(p, q, a3, b3)
  MatQ{w, x, y, z}

def Mat.add2r(+p: Nat, a0: Mat(p, 0n), b0: Mat(p, 0n), a1: Mat(p, 0n), b1: Mat(p, 0n)) -> Mat(1n+p, 0n):
  x y = Mat.add(p, 0n, a0, b0) Mat.add(p, 0n, a1, b1)
  MatR{x, y}

def Mat.add2c(+q: Nat, a0: Mat(0n, q), b0: Mat(0n, q), a1: Mat(0n, q), b1: Mat(0n, q)) -> Mat(0n, 1n+q):
  x y = Mat.add(0n, q, a0, b0) Mat.add(0n, q, a1, b1)
  MatC{x, y}

def Mat.sum(+r: Nat, +c: Nat, m: Mat(r, c)) -> F32:
  match r c:
    case 0n 0n:
      MatB{x} = m
      x
    case 1n+p 0n:
      MatR{t, b} = m
      x y = Mat.sum(p, 0n, t) Mat.sum(p, 0n, b)
      (x + y : F32)
    case 0n 1n+q:
      MatC{l, r2} = m
      x y = Mat.sum(0n, q, l) Mat.sum(0n, q, r2)
      (x + y : F32)
    case 1n+p 1n+q:
      MatQ{a, b, c2, d} = m
      w x y z = Mat.sum(p, q, a) Mat.sum(p, q, b) Mat.sum(p, q, c2) Mat.sum(p, q, d)
      ((w + x) + (y + z) : F32)

# a * b over three depths: r rows of a, k the shared side, c columns of b
def Mat.matmul(+r: Nat, +k: Nat, +c: Nat, +a: Mat(r, k), +b: Mat(k, c)) -> Mat(r, c):
  match r k c:
    case 0n 0n 0n:
      MatB{x} = a
      MatB{y} = b
      MatB{(x * y : F32)}
    case 1n+p 0n 0n:
      MatR{at, ab} = a
      x y = Mat.matmul(p, 0n, 0n, at, b) Mat.matmul(p, 0n, 0n, ab, b)
      MatR{x, y}
    case 0n 0n 1n+s:
      MatC{bl, br} = b
      x y = Mat.matmul(0n, 0n, s, a, bl) Mat.matmul(0n, 0n, s, a, br)
      MatC{x, y}
    case 0n 1n+q 0n:
      MatC{al, ar} = a
      MatR{bt, bb} = b
      x y = Mat.matmul(0n, q, 0n, al, bt) Mat.matmul(0n, q, 0n, ar, bb)
      Mat.add(0n, 0n, x, y)
    case 1n+p 1n+q 0n:
      MatQ{a0, a1, a2, a3} = a
      MatR{bt, bb} = b
      p0 q0 p1 q1 = Mat.matmul(p, q, 0n, a0, bt) Mat.matmul(p, q, 0n, a1, bb) Mat.matmul(p, q, 0n, a2, bt) Mat.matmul(p, q, 0n, a3, bb)
      Mat.add2r(p, p0, q0, p1, q1)
    case 0n 1n+q 1n+s:
      MatC{al, ar} = a
      MatQ{b0, b1, b2, b3} = b
      p0 q0 p1 q1 = Mat.matmul(0n, q, s, al, b0) Mat.matmul(0n, q, s, ar, b2) Mat.matmul(0n, q, s, al, b1) Mat.matmul(0n, q, s, ar, b3)
      Mat.add2c(s, p0, q0, p1, q1)
    case 1n+p 0n 1n+s:
      MatR{at, ab} = a
      MatC{bl, br} = b
      w x y z = Mat.matmul(p, 0n, s, at, bl) Mat.matmul(p, 0n, s, at, br) Mat.matmul(p, 0n, s, ab, bl) Mat.matmul(p, 0n, s, ab, br)
      MatQ{w, x, y, z}
    case 1n+p 1n+q 1n+s:
      MatQ{a0, a1, a2, a3} = a
      MatQ{b0, b1, b2, b3} = b
      p0 q0 p1 q1 p2 q2 p3 q3 = Mat.matmul(p, q, s, a0, b0) Mat.matmul(p, q, s, a1, b2) Mat.matmul(p, q, s, a0, b1) Mat.matmul(p, q, s, a1, b3) Mat.matmul(p, q, s, a2, b0) Mat.matmul(p, q, s, a3, b2) Mat.matmul(p, q, s, a2, b1) Mat.matmul(p, q, s, a3, b3)
      Mat.add4(p, s, p0, q0, p1, q1, p2, q2, p3, q3)

def depth.of(m: Maybe<&2, U32>) -> Nat:
  match m:
    case None{}:
      2n
    case Some{k}:
      U32.to_nat(k)

def depth(args: List<String>) -> Nat:
  match args:
    case []:
      2n
    case a <> rest:
      depth.of(U32.read(a))

# the sum of ones * ones at 2^d x 2^d: 8^d
def run(+d: Nat) -> F32:
  Mat.sum(d, d, Mat.matmul(d, d, d, Mat.full(d, d, 1.0), Mat.full(d, d, 1.0)))

def main() -> IO(Unit):
  do IO<Unit>:
    args : List<String> <- IO.args()
    IO.print(F32.show(run(depth(args))))

Output:

$ bend repro.bend -o r
$ ./r 1
8
$ ./r 2
bend: memory fault (machine stack overflow?)
$ ./r 3
bend: memory fault (machine stack overflow?)
$ ./r 4
bend: memory fault (machine stack overflow?)
$ bend repro.bend 3 # the interpreter
512

On 2.0.18 the depth-2 run sometimes printed nan or out of memory
instead; on 2.0.20 it is the memory fault every time.

What makes it run

Add a second recursion over the same constructors and call it once.
withsq.bend is repro.bend with these lines inserted before
depth.of:

# the square product over one depth, walking the same nodes
def Mat.matmul.sq(+d: Nat, +a: Mat(d, d), +b: Mat(d, d)) -> Mat(d, d):
  match d:
    case 0n:
      MatB{x} = a
      MatB{y} = b
      MatB{(x * y : F32)}
    case 1n+p:
      MatQ{a0, a1, a2, a3} = a
      MatQ{b0, b1, b2, b3} = b
      p0 q0 p1 q1 p2 q2 p3 q3 = Mat.matmul.sq(p, a0, b0) Mat.matmul.sq(p, a1, b2) Mat.matmul.sq(p, a0, b1) Mat.matmul.sq(p, a1, b3) Mat.matmul.sq(p, a2, b0) Mat.matmul.sq(p, a3, b2) Mat.matmul.sq(p, a2, b1) Mat.matmul.sq(p, a3, b3)
      Mat.add4(p, p, p0, q0, p1, q1, p2, q2, p3, q3)

def run.sq(+d: Nat) -> F32:
  Mat.sum(d, d, Mat.matmul.sq(d, Mat.full(d, d, 1.0), Mat.full(d, d, 1.0)))

and this main instead of the one above, printing the square product's
sum before the general one's:

def main() -> IO(Unit):
  do IO<Unit>:
    args : List<String> <- IO.args()
    +d : Nat = depth(args)
    IO.print(F32.show(run.sq(d)))
    IO.print(F32.show(run(d)))

Both products are now right at every depth, the general one included:

$ bend withsq.bend -o s
$ ./s 1 ; ./s 2 ; ./s 3 ; ./s 4
8
8
64
64
512
512
4096
4096

Mat.matmul.sq walks the same MatQ nodes through the same Mat.add4
as the general product's last case. Making a different def reachable
instead (a transpose over the same types) does not help. With a literal
depth in main (run(2n) in place of run(depth(args))) the general
product is right even without the square one, so the failure needs a
depth the compiler cannot see.

Guess / possible directions

The ownership analysis of Mat.matmul (three depths, eight arms, +
operands opened by several arms) comes out different when the same
constructors are also opened by another recursive def, and the first
outcome is wrong. In the emitted C the arms open their operands with
ctr_take in the failing build and term_peek in the working one. Might be related to #853

bend --version

2.0.20

uname -sm

Darwin arm64

clang --version (the first line)

Apple clang 21.0.0

Activity

  1. added
    bug
    on Sep 20, 2026
  2. jasisz commented on Sep 20, 2026

    @jasisz
    Contributor

    I found a stronger closed form of this on current main (6018e28e, Bend 2.0.21). It removes F32, command-line input and IO from the failure, and puts the expected result in a checked theorem in the same file.

    Starting from the file in the issue, replace every F32 with U32, replace both 1.0 literals with 1, remove depth.of, depth and the old main, then use:

    def run(+d: Nat) -> U32:
      Mat.sum(d, d, Mat.matmul(d, d, d, Mat.full(d, d, 1), Mat.full(d, d, 1)))
    
    def hold(n: Nat) -> Nat:
      n
    
    def opaque_two(u: Unit) -> Nat:
      a b = hold(1n) hold(0n)
      1n+a
    
    def opaque_two_ok() -> {opaque_two(Unit{}) == 2n : Nat}:
      {==}
    
    def runtime_tag() -> Bool:
      U32.is_eq(run(opaque_two(Unit{})), 64)
    
    def runtime_tag_ok() -> {runtime_tag() == True{} : Bool}:
      {==}
    
    def main() -> Bool:
      runtime_tag()
    

    The parallel let keeps the closed 2n opaque to the compiler while remaining definitionally equal to 2n for the checker. Results:

    $ bun bend2/main.ts closed.bend --check-only
    All terms check.
    $ bun bend2/main.ts closed.bend
    True{}
    $ bun bend2/main.ts closed.bend -o closed.js && bun closed.js
    True{}
    $ bun bend2/main.ts closed.bend -o closed && ./closed
    False{}
    

    Repeated runs of the exact same C binary alternate between False{} and bend: memory fault (machine stack overflow?) on my M4 Pro. So this is reproducible with a fully closed program, integer leaves and a checked proposition whose runtime value the C backend flips. There are no templates, @unsafe, FFI, holes or open user laws involved.

  3. jasisz commented on Sep 20, 2026

    @jasisz
    Contributor

    I reduced this to a much smaller closed repro and found the specific missed fact. This is the same root cause as the matrix failure.

    $ bun bend2/main.ts nested_family.bend --check-only
    All terms check.
    $ bun bend2/main.ts nested_family.bend
    True{}
    $ bun bend2/main.ts nested_family.bend -o out.js && bun out.js
    True{}
    $ bun bend2/main.ts nested_family.bend -o out && ./out
    False{}
    

    The result is deterministic on both the 2.0.21 tag and current main (6018e28e). The file is closed and pure. It has no templates, @unsafe, FFI, holes or open user laws. result_ok proves result() == 29, and runtime_tag_ok proves the True{} that native C changes to False{}.

    import Base
    
    type RT is Data:
      RT{a: U32, b: U32, c: U32}
    
    law F:
      for t: Nat
      for n: Nat
      Data
    
    def F(t, n):
      match t n:
        case 0n 0n:
          U32
        case 0n 1n+p:
          U32
        case 1n+q 0n:
          RT
        case 1n+q 1n+p:
          RT
    
    type Carrier<-n: Nat, -t: Nat> is Data:
      Carrier{payload: F(t, n), blob: String, tail: Maybe<&2, Carrier<n, t>>}
    
    def hold(n: Nat) -> Nat:
      n
    
    def opaque_zero() -> Nat:
      a b = hold(0n) hold(1n)
      a
    
    def make_rt(+n: Nat, +x: U32) -> F(1n, n):
      match n:
        case 0n:
          RT{x, U32.add(x, 1), U32.add(x, 7)}
        case 1n+p:
          RT{x, U32.add(x, 1), U32.add(x, 7)}
    
    def pack(+n: Nat, r: F(1n, n), s: String) -> Carrier<n, 1n>:
      Carrier{r, s, None{}}
    
    def take_blob(c: Carrier<0n, 0n>) -> String:
      Carrier{payload, blob, tail} = c
      blob
    
    def heat_carrier(+c: Carrier<0n, 0n>) -> String:
      String.append(take_blob(c), take_blob(c))
    
    def use(+n: Nat, c: Carrier<n, 1n>) -> U32:
      match n:
        case 0n:
          Carrier{r, blob, tail} = c
          RT{a, b, c2} = r
          U32.add(a, c2)
        case 1n+p:
          Carrier{r, blob, tail} = c
          RT{a, b, c2} = r
          U32.add(a, c2)
    
    def result() -> U32:
      +n = opaque_zero()
      U32.add(U32.from_nat(String.length(heat_carrier(Carrier{0, U32.show(U32.add(U32.from_nat(n), 7)), None{}}))), use(n, pack(n, make_rt(n, 10), U32.show(U32.from_nat(n)))))
    
    def result_ok() -> {result() == 29 : U32}:
      {==}
    
    def runtime_tag() -> Bool:
      U32.is_eq(result(), 29)
    
    def runtime_tag_ok() -> {runtime_tag() == True{} : Bool}:
      {==}
    
    def main() -> Bool:
      runtime_tag()
    

    Carrier<0n, 0n> is shared only to make its recursive constructor hot at an instantiation whose payload is U32. pack then builds the same hot constructor at the open type Carrier<n, 1n>, whose payload is F(1n, n). RT must become hot before that payload is sealed.

    The missed fact is in facts_fam. For F(1n, n), it calls mat_arms only on the outer match over t. Those arm bodies are still matches over n:

    Zero -> match n { Zero -> U32; Succ -> U32 }
    Succ -> match n { Zero -> RT;  Succ -> RT  }
    

    They reach facts_hot as Mat terms, and the walk stops one level above RT. CID_CARRIER is therefore hot while CID_RT stays cold. The hot build seals the RT payload; use takes it as a cold raw three-word record and reads the one-word seal cell as its fields.

    A semantic control splits the inner match into G(n). Then facts_fam sees Zero -> RT and Succ -> RT, CID_RT becomes hot, and C prints True{}. In a temporary copy of the compiler, recursively walking a forced Mat inside facts_hot also makes this repro print True{}; the same change makes the closed U32 matrix repro from my previous comment print True{} in 8/8 runs instead of alternating between False{} and a memory fault. That ties this directly to #901 rather than a second backend bug.

  4. VictorTaelin commented on Sep 20, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.22 (bend update). The C emitter decides per bind whether a shared node is read through ctr_take or read bare and freed; to heat a family's constructors it walks the family's match, and a family with two indices is a match whose arms are matches, which fell into the unknown-type fallback, so MatQ never became hot and Mat.matmul freed a shared pointer as an owned node. Mat.matmul.sq "fixed" it only because its rebind reduces to the ADT and heats it by accident. Now facts_hot walks a match arm's arms. Your program prints 8 / 64 / 512 at every depth; pinned as tests/compile/family_two_indices.bend.

    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

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions