Skip to content

selfhost: lower algebraic effects in flowc (#996, #675) - #1014

Merged
godofecht merged 5 commits into
mainfrom
selfhost/lower-effects
Sep 29, 2026
Merged

godofecht merged 5 commits into
mainfrom
selfhost/lower-effects

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Part of #996 and #675. Addresses the handler half of #991.

flowc now parses, checks and lowers algebraic effects the way the Python host does: effect and capability declarations, handle E1, E2 with H1, H2 { }, effect rows (-> T with E), capability E parameter types, E.op(args), and the unhandled-effect policy.

This branch contains #1001 and #1003, which are still open. Merge those first; the diff then shrinks to the effect work.

What changed

  • compiler/src/effects.flow (new): an effect table (effects, operation signatures, capabilities, with rows) that rides in the bundle signature table, so a module sees what its imports declare. This is what the log package needs: export effect Log in one module, handle Log with ConsoleLog in another.
  • parser: effect and capability declarations become AST_EFFECT / AST_CAPABILITY (methods are AST_CAP_METHOD, so the whole-arena function scans never see Console.info as a function info). capability E marks its AST_TYPE. handle and effect rows are no longer parse-only.
  • typecheck: the Python host's effect diagnostics with the same text: unknown effect, unknown operation, arity, literal argument types, unqualified operation inside a handler, capability used as a value, unknown effect in a handle or a with row. With FLOWC_STRICT_EFFECTS=1 (the Python host's --strict-effects) it also reports unhandled performs and calls whose row no handler covers. Diagnostics print as path:line:col: error: <message>.
  • cgen: the Python host's runtime shape. Per effect a handler vtable type, a fiber-local or thread-local handler slot and a dispatch function per operation; per capability its method functions and one vtable per handled effect; handle saves, installs and restores the slots; implicit capability arguments inside handle; the FiberAsync main wrapper. An unhandled operation returns zero, or aborts with FLOW_STRICT_EFFECTS=1 at run time (hard-wired by FLOWC_STRICT_EFFECTS=1 at compile time).
  • fmt and jsgen keep refusing effect programs so they never drop a handler silently.
  • Source buffers grow from 256 KiB to 512 KiB (cgen.flow passed 256 KiB). The per-module self-host scripts build and link effects.flow.
  • Small general fixes the corpus needed: printf, fprintf, sprintf, scanf, sscanf are typecheck builtins as on the Python host; putenv, setenv, unsetenv are libc; the legacy array_f32 type spelling lowers to float*.

Parity gate

compiler/scripts/parity_effects.sh, modelled on parity_field_dsl.sh. It needs only cc for the goldens, and --python <rev> adds a live comparison with the Python host from that revision (extracted with git archive).

  • 15 fixtures in compiler/fixtures/effects/: every diagnostic above, strict and non-strict, runtime zero defaults and nested handler restore, the FLOW_STRICT_EFFECTS=1 abort (stderr and exit code 134), multi-effect handle, capability method printing, and the log registry package demo with the package beside it. Goldens recorded from the Python host at 860b7dd.
  • Corpus: every tracked .flow file that uses effects, compiled the way flow run does, linked against runtime/*.c, run with a 10 s limit.

Results with the regenerated bootstrap:

fixtures: pass=15 fail=0
live fixtures python@860b7dd7: pass=15 fail=0
corpus python@860b7dd7: files=46 same_run=16 same_reject=29 (different stage: 5) known_gaps=1 fail=0
parity_effects: PASS

Diagnostics match as text. Two Python messages use an em dash and an ellipsis as separators; flowc writes : and ..., and the gate maps the Python spelling before comparing.

Before and after on flowc

Of the 17 corpus programs that build and run on the Python host:

  • before: 2 ran on flowc (neither declares an effect); the other 15 stopped at unsupported in Stage-A: handle or effect row
  • after: 16 run with identical output and exit code. The 17th, tests/runtime/test_flow_way_examples.flow, stops on the pipeline placeholder _ (Stage-A: pipeline placeholder argument x |> f(a, _, b) #1005).

./flow run examples/effects/effect_rows.flow now runs on the default flowc host.

The 29 programs both hosts reject are mostly the concurrency, GPU and stdlib files that need the Flow-side runtime in lib/runtime or GPU libraries at link time. Five reject at a different stage (Python at link, flowc at C compile); the flowc errors are general cgen gaps unrelated to effects, filed as #1009. lib/stdlib/audio/notation.flow is one of them.

Python removed

  • tests/unit/test_effect_rows.py, test_unqualified_effect_op.py, test_capability_not_a_value.py, test_capability_method_params.py: their cases are now gate fixtures.
  • The effect checks in tests/unit/test_strict_effects.py (the file keeps its one debug-info test).
  • python_ratchet.sh --update: limit 97003 lines.

The effect lowering in src/flow/c_generator.py and type_checker.py stays while the Python host is still selectable with FLOW_HOST=python.

Verification

  • Fixed point: Python-host emit of main.flow equals the rebuilt bootstrap's emit.
  • bootstrap_from_c.sh --verify, self_host_full.sh, roundtrip.sh, parse_coverage.sh --check (2139 ≥ 2082), parity_field_dsl.sh, parity_effects.sh --python 860b7dd7: all pass.
  • Bootstrap suite (tests/lang through Stage-A, compile only): 147 pass, 11 fail. origin/main's bootstrap: 133 pass, 25 fail. Every remaining failure also fails on main; test_effects is one of the new passes.
  • python_ratchet.sh: PASS.

Not done

@godofecht
godofecht force-pushed the selfhost/lower-effects branch from f5c7df0 to 0510995 Compare September 29, 2026 08:27
godofecht added a commit that referenced this pull request Sep 29, 2026
…s lowered

test_parser_python_forms asserted that handle blocks, or-patterns, effect
rows and fork blocks stay parse-only. #1012 lowers or-patterns and forks
and #1014 lowers handle and effect rows, so the test exited 7 (6 with
both). It now checks that each form parses to its node and is lowered, and
exits 0 on flowc and the Python host alike. The parity_lowering golden
follows.
flowc now parses, checks and lowers effect declarations, capabilities,
handle blocks, effect rows and capability parameter types, the same way the
Python host does:

- parser: effect and capability declarations become AST_EFFECT and
  AST_CAPABILITY (methods are AST_CAP_METHOD); `capability E` types carry
  AST_TYPE_CAPABILITY. handle and effect rows are no longer parse-only.
- effects.flow: an effect table that rides in the bundle signature table so
  imported effects, capabilities and rows are known in later modules.
- typecheck: the Python host's effect diagnostics (unknown effect or
  operation, arity, literal argument types, unqualified operations,
  capability used as a value, unknown effects in rows and handle), plus the
  --strict-effects row checks under FLOWC_STRICT_EFFECTS=1.
- cgen: handler vtables, fiber-local handler slots, dispatch functions,
  capability vtables, handle install and restore, E.op calls, implicit
  capability arguments, the FiberAsync main wrapper, and the unhandled
  policy (zero default, FLOW_STRICT_EFFECTS=1 aborts).
- fmt and jsgen still refuse effect programs instead of dropping them.
- Source buffers grow to 512 KiB: cgen.flow passed 256 KiB.

Corpus (45 tracked .flow files using effects), flowc vs Python host at
860b7dd: every program that builds and runs on the Python host (17) now
builds and runs on flowc with identical output and exit code, except
tests/runtime/test_flow_way_examples.flow, which stops on the pipeline
placeholder `_`. Before: 2 of those 17 ran on flowc.

Bootstrap C is regenerated in a later commit.
compiler/scripts/parity_effects.sh holds flowc to the Python host on
algebraic effects. 15 fixtures in compiler/fixtures/effects (14 programs and
the log registry package demo from #991, built with the package beside it)
carry goldens recorded from the Python host at 860b7dd: the effect
diagnostics of a rejected program, or the run output and exit code of an
accepted one. --python <rev> also runs the Python host live over the
fixtures and over every tracked .flow file that uses effects.

Results against 860b7dd:
- fixtures: 15/15 match the goldens, 15/15 match the live Python host
- corpus: 46 files, 16 run with identical output and exit code, 29 are
  rejected by both hosts (5 at a different stage, all non-effect cgen gaps),
  1 known gap (#1005, pipeline placeholder), 0 failures

flowc also treats putenv, setenv and unsetenv as libc (the log package
declares putenv) and maps the legacy array_f32 type spelling to a pointer.

Python removed: tests/unit/test_effect_rows.py,
test_unqualified_effect_op.py, test_capability_not_a_value.py and
test_capability_method_params.py (their cases are fixtures now), and the
effect checks of test_strict_effects.py. python_ratchet --update: limit
104478 lines.
roundtrip.sh, stage_a_self_emit.sh and stage_a_self_emit_g2.sh compile each
compiler module on its own and link the objects. They now emit effects.flow,
include its header in cgen, typecheck and resolve, and link it into the
frontend object. The C host driver reads sources up to 512 KiB, matching
flowc.
…s lowered

test_parser_python_forms asserted that handle blocks, or-patterns, effect
rows and fork blocks stay parse-only. #1012 lowers or-patterns and forks
and #1014 lowers handle and effect rows, so the test exited 7 (6 with
both). It now checks that each form parses to its node and is lowered, and
exits 0 on flowc and the Python host alike. The parity_lowering golden
follows.
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.

1 participant