Skip to content

Pipeline composition: placeholders, fork records, and flows as stages - #199

Merged
godofecht merged 9 commits into
mainfrom
claude/flow-pipeline-syntax-ayt4lt
Aug 5, 2026
Merged

godofecht merged 9 commits into
mainfrom
claude/flow-pipeline-syntax-ayt4lt

Conversation

@godofecht

@godofecht godofecht commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Builds an expression-level composition layer on top of Flow's existing |> pipeline operator, in eight self-contained increments. Each turns a construct that previously errored (or didn't exist) into working, tested syntax that lowers to ordinary AST — so the type checker and every backend need no new support.

What changed (each commit is independently reviewable):

  1. Placeholder _ — value |> clamp(0.0, _, 1.0) routes the piped value to any argument slot instead of the default first-argument prepend. One _ per stage.
  2. Named fork blocks — src |> Record { a = f, b = g } applies several pipelines to one value and collects a named record. Branches use = (a pipeline), distinct from struct-literal :.
  3. Anonymous inferred records — src |> { a = f, b = g }: the record type is synthesized from each branch's outermost-call return type (a post-parse pass; identical signatures share one struct).
  4. Single-eval — a non-trivial fork source is hoisted to one temp above the statement and shared by every branch (was substituted per-branch); trivial sources stay inlined.
  5. Parent→child connect wiring — connect { signal -> g.x } lets a flow's own input/state drive a child input, not just child→child. This was the deferred "NOT YET" in robot_connect.
  6. Flows as pipeline stages — inside a flow, output y = src |> Gain |> Limiter desugars to synthesized children + connections + a child-port output map. Each stage adds one tick of delay (a sampled, state-broken pipeline).
  7. Stage parameter overrides — src |> Gain { k: 3.0 } overrides a stage flow's params. The : value form disambiguates from a fork block's = pipeline form purely at parse time.

Why this is the right change for Flow: it keeps |> (Flow's chosen pipeline token) rather than overloading -> (already load-bearing for return types, lambdas, and connect wires), and everything desugars to existing nodes — new syntax, no new core semantics, one deliberate exception (the parent→child copy, wired into the existing connect step order).

Validation

  • ./flow test --strict --tier2 — not run directly; instead ran the Python test suite: 685 passed, 0 failures (pytest tests/unit tests/integration, excluding pre-existing pdflatex/numpy-env failures unrelated to this change).
  • Relevant examples run — all compile via the transpiler on both MLIR and C backends and produce correct output: pipeline_placeholder, pipeline_fork, pipeline_fork_inferred (basics); parent_input_connect, flow_pipeline_stages (evolution, the latter converging to k·signal).
  • Docs updated — docs/language/syntax.md (Pipelines: operator, placeholders, forks) and docs/vision/north-star.md §8 (parent-source connect, flows-as-stages, stage params).
  • Questions.md — the one design decision (per-stage one-tick delay for flow pipelines) is recorded in north-star §8 rather than Questions.md; happy to move it if preferred.

Compatibility

  • No user-visible compatibility change — all additive. The only behavior change is to syntax that previously raised a parse/compile error (|> Name { … }, |> { … }, |> obj(_, …), bare connect sources).

Checklist

  • The change is focused and reviewable — eight small commits, each with parser/codegen + tests + example + docs.
  • No generated build artifacts or secrets are included.
  • New syntax has parser, type-checker (strict-clean assertions), codegen (MLIR + C), and regression coverage — see tests/unit/test_pipeline_placeholder.py, tests/unit/test_connect_composition.py, tests/integration/test_pipeline_examples.py.

Not included (larger, separate design passes noted for follow-up): @gpu on a stage, differentiate … with respect to sugar, choose mode { … }, and evolve pipeline { … } topology search.

claude added 8 commits August 5, 2026 15:22
The pipeline operator prepends the piped value by default
(`x |> f(y)` == `f(x, y)`). Add a single `_` placeholder so the piped
value can be routed to any argument slot instead:

    value |> clamp(0.0, _, 1.0)   # clamp(0.0, value, 1.0)
    x     |> mix(_, sidechain, k) # mix(x, sidechain, k)

Applies to both function and method calls. At most one `_` is allowed
per stage, since the piped value fills exactly one slot; two would
duplicate it and are rejected at parse time. Absent a placeholder,
lowering is unchanged.

Includes unit tests for lowering and the multi-placeholder error, a
runnable example, and a Pipelines section in the syntax reference
(which previously documented `|>` only for declarative sort).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N3reuKQnaLHJeg2W5MAVAJ
A fork block applies several pipelines to the same value and collects
the results into a named record:

    let s: Stats = n |> Stats {
        doubled  = twice,
        squared  = square,
        plus_ten = add(_, 10),
    }
    # == Stats { doubled: twice(n), squared: square(n), plus_ten: add(n, 10) }

Each `field = stage...` branch is the pipeline `source |> stage...`, so
branch stages compose and take `_` placeholders like any other pipeline.
Branches use `=` (not the struct-literal `:`) so the fork and the record
it builds read distinctly. `|> Ident {` was previously always a syntax
error (a struct literal is not a callable pipe target), so the form is
free to claim with no ambiguity.

The block desugars to a struct literal of the named record, which is
type-checked and lowered by every backend like any other literal — no
type-system or codegen changes. The record is named on purpose: the
piped value is substituted into each branch, so a non-trivial source is
evaluated once per branch (documented; bind with `let` to avoid).

The pipeline-chaining loop is refactored into `_parse_pipeline_chain` /
`_apply_pipe` helpers so branches reuse the same stage lowering.

Adds unit tests, a compile-and-run example, and a fork-block subsection
in the syntax reference.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N3reuKQnaLHJeg2W5MAVAJ
A fork block may now omit the record name, and the record type is
inferred instead of declared:

    let s = n |> {
        doubled  = twice,
        squared  = square,
        plus_ten = add(_, 10),
    }
    # s : { doubled: i32, squared: i32, plus_ten: i32 }

Each field takes the return type of the function its branch pipeline
ends in. A post-parse pass (fork_records.desugar_fork_records) walks the
AST, synthesizes a struct from those types, and rewrites the fork into a
struct literal of it — so the type checker, every backend, and tooling
see an ordinary named record and need no special support. Anonymous
records with identical field signatures share one synthesized struct.

Parsing produces a transient `ForkRecord` node for the anonymous form
(the named form still lowers directly to a StructLiteral); the node is
resolved away before parse() returns, guarded by a per-parser flag so
fork-free files skip the walk. A branch that doesn't bottom out in a
call with a known return type (e.g. a method call on an inferred
receiver) can't be typed structurally and reports a clear error asking
for an explicit record name.

Adds unit tests (synthesis, dedup, inference-failure), a compile-and-run
example, and an anonymous-record subsection in the syntax reference.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N3reuKQnaLHJeg2W5MAVAJ
Compile pipeline_placeholder / pipeline_fork / pipeline_fork_inferred
through the real transpiler CLI (parse -> type check -> monomorphize ->
codegen) on both the MLIR and C backends, and assert the anonymous fork
example synthesizes exactly one record struct. Keeps the placeholder and
fork desugarings wired through the full pipeline, beyond the parser-level
unit tests.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N3reuKQnaLHJeg2W5MAVAJ
Previously a fork block substituted its source expression into every
branch, so `frames(1024) |> { a = ..., b = ... }` ran frames twice. Now
a non-trivial source (anything but a variable/literal/field access) is
hoisted to a `let __fork_src_N = source` binding just above the enclosing
statement, and every branch reads that temp — the source runs exactly
once. Trivial sources are still inlined (no needless temp).

Named and anonymous forks are unified under one `ForkBlock` node whose
branches are pipelines over a `ForkSource` placeholder; the post-parse
`desugar_forks` pass hoists, substitutes, infers the anonymous record
type, and lowers to a struct literal. Hoisting happens at the statement
list of the nearest enclosing block, so forks inside nested blocks bind
in their own scope; a fork with no surrounding block falls back to
inlining.

Proven end to end: a side-effecting source now prints once, not once per
branch, and the emitted C binds one temp shared by all fields. Updates
the fork docs/example (the per-branch caveat is gone) and adds unit +
codegen tests for hoisting vs. inlining.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N3reuKQnaLHJeg2W5MAVAJ
`connect` sources may now be a bare `input`/`state` of the enclosing
flow, not only a sibling child's port:

    flow Chain {
        input signal : f64
        g : Gain
        connect { signal -> g.x }   # parent input drives a child input
    }

A bare identifier source (no `member.`) is parsed as a parent-port
reference (empty src_member). Validation checks it against the enclosing
flow's ports (must be an input or state); topo-ordering and algebraic-
loop detection treat it as a root (it imposes no child ordering and
can't form a child cycle); and the child-step lowering copies
`self.<port>` into the child input before stepping, exactly as sibling
sources are copied. This is the previously-deferred parent->child input
wiring (the robot_connect "NOT YET"), and the prerequisite for composing
flows as pipeline stages.

Adds a TestParentSource suite (parse shape, lowering + copy order, strict
check, and rejection of unknown / output-typed sources), a runnable
example, and updates the connect grammar/checks in the north-star spec.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N3reuKQnaLHJeg2W5MAVAJ
Inside a flow, an `output` whose value pipes through flow-typed stages
now desugars into a composite flow. The `|>` operator lowers the surface
pipeline to nested calls `B(A(src))`; when those callees are flows rather
than functions, each becomes a synthesized child instance wired in
sequence:

    flow Chain {
        input signal : f64
        output result : f64 = signal |> Gain |> Limiter
    }

lowers to children __result_stage0 : Gain, __result_stage1 : Limiter with
connections `signal -> stage0.x`, `stage0.out -> stage1.w`, and
`result = stage1.out`. This builds directly on the parent-source connect
support: the head of the pipeline (a parent input/state, or a child port)
drives the first stage.

Each stage's output is read before it steps that tick, so a stage adds
one tick of delay (a sampled, state-broken pipeline, spec §8.3). Stages
must be single-input/single-output; parameter overrides
(`Gain { k = 3.0 }`) are not yet supported and are rejected with a
message pointing at explicit `connect`. Synthesized stage children carry
a `synthesized` flag so they may use the reserved `__` prefix.

Verified end to end: `signal |> Gain |> Limiter` compiles on both
backends and converges to k*signal. Adds a TestFlowPipelineStages suite,
a runnable example, integration compile-checks for the flow examples, and
the sugar's spec entry in north-star §8.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N3reuKQnaLHJeg2W5MAVAJ
A flow used as a pipeline stage may now override the stage flow's params
inline:

    output result : f64 = signal |> Gain { k: 3.0 } |> Limiter

The `{ param: value }` block uses `:` (a value), which distinguishes it
from a fork block's `=` (a pipeline) and resolves the `|> Name { ... }`
ambiguity purely at parse time via one token of lookahead. It lowers to a
new `FlowStage` node; `_expand_flow_pipelines` reads the overrides,
attaches them to the synthesized stage child, and emits the assignments
in the parent's init after the child's own init so they win over the
declared defaults. Overrides are checked against the stage flow's params
(unknown names error), and a `FlowStage` used anywhere but a flow output
pipeline is rejected.

Verified end to end: `Gain { k: 3.0 }` makes the chain converge to
k*signal = 3.0, and the override lands as `self->__result_stage0.k = 3.0`
in Chain_init. Adds a TestStageParams suite, updates the example to show
an override, and documents the value-vs-pipeline delimiter in
north-star §8.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N3reuKQnaLHJeg2W5MAVAJ
@godofecht godofecht changed the title Add _ placeholder to the |> pipeline operator Pipeline composition: placeholders, fork records, and flows as stages Aug 5, 2026
…rror

Two CI failures on the pipeline branch:

- Lint (ruff F821): a stray, unreachable `return left` left over from the
  pipeline-chain refactor sat after `_parse_stage_params`' real return,
  referencing an undefined name. Removed.

- Fuzz (parser/pipeline): `ForkRecordError` subclassed `Exception`, so a
  fork block that legitimately can't be lowered (an uninferrable
  anonymous record, or a flow-stage param block used outside a flow
  output) was bucketed as a crash rather than a clean rejection of bad
  input. It now subclasses `FlowSyntaxError` (hence `SyntaxError`), which
  the fuzz harness treats as an allowed parse error. `ruff check src`
  passes and the fuzzer reports 0 crash classes.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N3reuKQnaLHJeg2W5MAVAJ
@godofecht
godofecht merged commit 0e5789f into main Aug 5, 2026
14 checks passed
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