Skip to content

Language: no forward references and no mutual recursion #2147

Open

Description

@adeerkhan

Environment: Bend 2.0.38.

While writing the training loop for a tiny ten-step optimization in the same Mamba-3 port, two definitions that call each other fail:

import Base
def f(+n: Nat) -> Nat:
  match n:
    case 0n: 0n
    case 1n+p: g(p)
def g(+n: Nat) -> Nat:
  f(n)
def main() -> IO(Unit):
  do IO<Unit>:
    IO.print(Nat.show(f(3n)))
    return Unit{}

bend → an unfilled law is a dead claim: live code cannot use it (and a def calling a later def fails the same way).

Workaround applied locally: the loop was unrolled into rest0..rest10 helpers plus a chain of single-purpose defs, and every match scrutinee was made a parameter. This works but is easy to get wrong at scale.

Question: would a two-pass compilation (collect all def names before checking bodies), or a sanctioned iterative/loop encoding, be feasible? It seems adjacent to the existing filled-law discipline without changing what is proved.

Activity

  1. adeerkhan commented on Nov 22, 2026

    @adeerkhan
    ContributorAuthor

    Re-confirming on Bend 2.0.38, now from the HiT diffusion-port work in bend-lab (we filed this issue during the Mamba-3 training loop).

    I re-ran .local/mamba3-repro/mutual.bend today against the pinned binary. It still fails with:

    expected : a filled definition (an unfilled law is a dead claim: live code cannot use it) / observed : g

    The new port runs into the same limitation in a different place: HiT's recursive architecture is naturally a nested encode → group → compact → ungroup → decode flow, and any attempt to express the repeated grouping levels as mutually recursive defs is refused. The local workaround is again unrolling into single-purpose defs plus parameter-threaded match scrutinees, which is mechanical but easy to get wrong at scale.

    A two-pass name collection, or a sanctioned iterative encoding that keeps the current no-forward-reference discipline for proofs, would help both the training loop and the hierarchical model. Repro: bend-lab/.local/mamba3-repro/mutual.bend; the error text is recorded in bend-lab/docs/NOTES.md.

  2. oxura commented on Nov 24, 2026

    @oxura
    Contributor

    For the bounded training loop, a single recursive def can alternate between phases without unrolling. Put the decreasing counter first, and pass the phase and state after it. In this sketch the counter counts phase transitions, so one encode/decode pair uses two.

    The interpreter and a native build both printed 33 on current main. This is a workaround for a bounded loop, not a change to forward references or mutual recursion.

    import Base
    
    type Phase is Data:
      Encode{}
      Decode{}
    
    def step(fuel: Nat, phase: Phase, value: U32) -> U32:
      match fuel:
        case 0n:
          value
        case 1n+rest:
          match phase:
            case Encode{}:
              step(rest, Decode{}, (value + 2 : U32))
            case Decode{}:
              step(rest, Encode{}, (value * 3 : U32))
    
    def main() -> U32:
      step(4n, Encode{}, 1)
    
  3. adeerkhan commented on Dec 2, 2026

    @adeerkhan
    ContributorAuthor

    Re-ran this issue's repro on the pinned 2.0.38 and current 2.0.40, and ran oxura's phase-tagged loop on both.

    The original repro still fails on both, with the message moving between versions:

    def f(+n: Nat) -> Nat:
      match n:
        case 0n: 0n
        case 1n+p: g(p)
    def g(+n: Nat) -> Nat:
      f(n)
    
    • 2.0.38: - expected : a filled definition (an unfilled law is a dead claim: live code cannot use it) - observed : g
    • 2.0.40: - expected : a def whose body is implemented before the def is used (defs are checked in file order, and an unfilled law has none)

    oxura's workaround is confirmed, and it runs. A single recursive def that carries a Phase tag and alternates on the decreasing counter compiles and prints 33 on both versions:

    def step(fuel: Nat, phase: Phase, value: U32) -> U32:
      match fuel:
        case 0n: value
        case 1n+rest:
          match phase:
            case Encode{}: step(rest, Decode{}, (value + 2 : U32))
            case Decode{}: step(rest, Encode{}, (value * 3 : U32))
    

    That covers a bounded two-phase loop without unrolling, which is what the training loop needed. It is a workaround, not a change to the language: any two defs that call each other are still refused, so a general mutually recursive encode/decode (not tag-and-counter) remains unavailable.

    Net: the limitation is real on both versions; for the bounded-loop case the phase-tagged recursion removes the need to unroll, but the forward-reference/mutual-recursion restriction itself stands.

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