Repository navigation
Pipeline composition: placeholders, fork records, and flows as stages - #199
Merged
Merged
Conversation
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
_ placeholder to the |> pipeline operator…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
_—value |> clamp(0.0, _, 1.0)routes the piped value to any argument slot instead of the default first-argument prepend. One_per stage.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:.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).connectwiring —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" inrobot_connect.output y = src |> Gain |> Limiterdesugars to synthesized children + connections + a child-port output map. Each stage adds one tick of delay (a sampled, state-broken pipeline).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, andconnectwires), and everything desugars to existing nodes — new syntax, no new core semantics, one deliberate exception (the parent→child copy, wired into the existingconnectstep 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-existingpdflatex/numpy-env failures unrelated to this change).pipeline_placeholder,pipeline_fork,pipeline_fork_inferred(basics);parent_input_connect,flow_pipeline_stages(evolution, the latter converging tok·signal).docs/language/syntax.md(Pipelines: operator, placeholders, forks) anddocs/vision/north-star.md§8 (parent-sourceconnect, 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 thanQuestions.md; happy to move it if preferred.Compatibility
|> Name { … },|> { … },|> obj(_, …), bareconnectsources).Checklist
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):
@gpuon a stage,differentiate … with respect tosugar,choose mode { … }, andevolve pipeline { … }topology search.