Skip to content

Language: intended encoding for evolving state across an online loop #2150

Open

Description

@adeerkhan

Environment: Bend 2.0.38. Context: porting an online greedy decoder for the two weight-tied layers of a Mamba-3 SISO LM. The work is done and gated (tokens equal the full-prefix golden exactly at 16 tokens); this issue is a question about the intended encoding, not a bug report.

The shape that works today:

  • per-layer state as a record of a 2048-float Array<F32> (the SSM), a List<&2, F32> (angle, 16 floats) and a pending previous-row record (x 128, roped k 64, DT 4, trap 4) — records with Array fields work as parameters, matched at the top of their def;
  • the whole decoder state carried unmatched through the single self-recursive def's parameter (so nothing needs +), and every intermediate record (LOUT, SR, SA) consumed by a parameter-match helper;
  • arrays threaded by value, never +-bound.

Two spellings the compiler rejects today (both verified on this lane):

def use(+a: Array<F32>) -> F32:
  0.0

bend → expected : Data, observed : Type (a can be used many times, so its type must be Data).

A record carrying an Array<F32> as a parameter matches fine (type St is Type: St{arr: Array<F32>, step: Nat}), so the record+Array combination is expressible — the missing part is any way to share it or to look through a def boundary at the values it contains without a helper per access.

The question: is unmatched threading of state records through one recursive def the intended encoding for an online loop (streaming decode, incremental scan), or is there a blessed pattern (explicit mutable state, a Scan-like law, a State-ish helper in Base) that we should target instead? A pointer to the intended direction is enough; no implementation ask.

Related asks already filed: #2146 (record field projection / local-binder match / Data-shared records), #2147 (forward references, mutual recursion, unrolling loops).

Activity

  1. oxura commented on Nov 24, 2026

    @oxura
    Contributor

    I got this working on current main without sharing the array: pass the state record unchanged into the loop, and unpack/update it in an affine feed helper. Put the structurally decreasing argument before the state. run(feed(s, x), xs) failed the termination check here; run(xs, feed(s, x)) passed.

    This example writes three samples and reads the last one. Both the interpreter and a native build printed 13.

    import Base
    
    type State is Type:
      State{buf: Array<U32>, pos: U32}
    
    def feed(s: State, x: U32) -> State:
      match s:
        case State{buf, +pos}:
          State{Array.set(U32, buf, pos, x), (pos + 1 : U32)}
    
    def run(input: List<U32>, s: State) -> State:
      match input:
        case Nil{}:
          s
        case x <> xs:
          run(xs, feed(s, x))
    
    def finish(s: State) -> U32:
      match s:
        case State{buf, pos}:
          Pair.snd(Array<U32>, U32, Array.get(U32, buf, (pos - 1 : U32)))
    
    def main() -> U32:
      finish(run([3, 8, 13], State{[0 : U32*8n], 0}))
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