Repository navigation
Description
capability already compiles on BEAM — it lowers as the ordinary module it is, and a program that declares one and calls it runs correctly with -R. Only with is missing:
error: 'with Clock = ...' is not supported on the BEAM backend yet; capabilities
run on the interpreter. The capability itself compiles — only replacing it does not.
(That message replaced IR lower: unimplemented expr node N3kex3ast8WithExprE.)
This is a design decision, not a gap in typing, and the two candidates differ in more than effort.
Option A — process dictionary
with does erlang:put({kex_cap, Name}, Value) and restores on exit; every capability-qualified call site checks erlang:get before falling through to the real module. This mirrors what the walker does today (a per-process stack saved and restored around each resume()).
Two real problems:
- No
after. The IR hasTryCatchbut no finally, so an error escaping awithwould leave the binding installed. Usually the process dies and takes its dictionary with it, but if atryinghigher up in the same process catches the error, the binding leaks — a divergence from the walker, whose restore is a destructor and therefore unconditional. - Spawn inheritance is not free. The process dictionary is not inherited, so
spawnwould have to copy a snapshot explicitly to matchspec/capabilities.kex, where a child spawned inside a region sees the replacement and one spawned outside does not.
It also puts a runtime check on every capability call.
Option B — thread it, as the design argues
The counter-proposal on #143 is that a capability is an implicit parameter, threaded statically. The requirement inference needed for that now exists: the compiler knows which functions require which capabilities and why. Lowering would pass the capability to exactly those functions.
This is the designed semantics, and it removes every problem above: exception safety is trivial because there is no state to restore, spawn inheritance is ordinary closure capture, and there is no per-call check. The cost is arity changes on capability-requiring functions and their call sites, and it would make the BEAM implementation structurally different from the walker's.
Recommendation
B, because A knowingly ships a restore hole and a hot-path check to imitate an implementation the design already rejected. But B is the larger change, and the walker would eventually want the same treatment so the two backends do not diverge in mechanism.
Until this lands, spec/capabilities.kex carries # kex: skip-beam.
Activity
akoskovacs commentedon Aug 22, 2026
Static threading needs a precise call graph first, and today's is not
Agreed on threading over the process dictionary. Implementing it turned up a prerequisite that has to land first, and it is 10000 worth recording because it is not obvious.
The call graph and the requirement map are keyed by bare function name (
m_callGraph[def.name],analyzer.cxx). Two same-named functions in different modules share one entry:For foulness this is safe, because the effect bit only ever widens — attributing A's foulness to B costs precision, never correctness. For threading it is not safe, because the parameter list is the ABI:
ticks the parameter, which is consistent but coarse — and the coarseness spreads, since common names (read,get,run) collide readily across a unit.with. A test that reads as mocked would quietly exercise production — the exact failure this whole feature exists to remove.So the honest ordering is: give the analyzer per-definition identity (key by
FunctionDef*, resolve call edges through the resolved-call targets that already exist for method calls), then thread.What landed meanwhile
Two defects in the requirement inference, both found while sizing this:
computeTransitiveEffectswas a drifted copy ofprocessFnthat never set the collection target, so inference only ever worked for top-level functions. The duplicate is gone; module members now go throughprocessFn.The rest of the plan, unchanged
Plumbing is confirmed available:
lowerModules/lowerProgramTieredalready take optional semantic results and the Analyzer is in scope at the compile site, so requirements can be passed rather than recomputed — recomputing a second fixpoint in lowering is the divergencedocs/ufcs-dispatch-plan.mdwarns about.Then: append a hidden parameter per required capability to locally-defined functions; pass the current value at local call sites; lower
withto a binding of that value; and at a capability call site dispatch on it, falling back to the real module when it holds the default sentinel. One atom comparison per capability call, no process state, exception-safe, andspawninherits by ordinary closure capture.akoskovacs commentedon Aug 22, 2026
Revised: one context parameter, not one per capability
Better than what I proposed above, and it removes the blocker rather than working around it.
Instead of a hidden parameter per required capability, every foul function defined in the unit takes one context — a map from capability name to implementation.
Why this dissolves the precision problem. The per-capability version needed to know exactly which functions require which capabilities, which is why the bare-name call graph was fatal. With one uniform context the ABI rule is flat — every foul function takes it — so nothing about correctness depends on the call graph at all. Requirement inference drops back to advisory, for hover and target checking, which is all it should ever have been load-bearing for.
Everything else follows:
afterhole — a context is a value.spawncaptures it in the closure, as does any block. That is why pure combinators never need it even when the block they run is foul: the block closed overCtxwhere it was written, somapstays untouched.Pure functions genuinely never need it, since a pure function cannot call a capability method — they are
foul— and a foul block created in a foul function already carries the context.Cost. Every foul function gains one parameter whether it uses it or not: 108
foulagainst 938letin the stdlib, so about a tenth of functions, and the pure core where the hot loops live is untouched.The precedent already exists
Trait dictionaries thread hidden arguments through definitions and call sites today —
def.dictionaryTraitsanddictionaryAritywhen lowering a definition,fnTraitParamsandmakeTraitDictionaryat call sites. A capability context is the same shape, so this is following an established path inlower.cxxrather than inventing one.Decision to take before implementing
This changes the ABI of every foul function in the prelude, so prelude artifacts rebuild. Fine in-tree, and nothing sealed is stranded now that
.kexois dropped — but it does mean capability sets eventually belong in the KexI chunk, or an external caller will pass the wrong arity. Worth deciding whether to land the KexI field in the same change or accept that cross-module foul calls are unsupported until the module system is finished.akoskovacs commentedon Aug 22, 2026
Implementation attempt: what works, and the one thing that stops it
Tried the context threading. Two pieces are done and correct; a third is bigger than it looks, so I backed the change out rather than leave a half-threaded ABI in the tree. Recording the findings so the next pass is quick.
The gate works, and it matters
Nothing is emitted unless the unit declares a capability (
capabilityModulesnon-empty). Verified: a program with no capability compiles byte-identically, so the prelude and every existing spec are untouched. That bounds the blast radius to capability-using programs, which isspec/capabilities.kexand nothing else — and it means the prelude ABI churn I worried about above does not happen at all until the stdlib is converted.Definitions: done, and the insertion point is unambiguous
The context is a trailing parameter after any trait dictionaries, with
def.arityadjusted to match. Purity is uniform across an overload group (the UFCS plan requires it), sogroup[0]->isFouldecides for the whole group.currentContextVaris set for the body and restored after, so calls inside pick it up.Call sites: the blocker
lower.cxxemits a local call from several branches, not one. I patched thefnTraitParamspath — the same one the trait dictionaries use — and it was not enough:maincalls a zero-arg function through thezeroArgFns/ thunk branch, which never reaches the site I patched. Constants, constructors, method calls, and the generated dispatchers are further paths, and each needs the same append to stay arity-consistent. Threading is therefore an enumeration problem across every local-call emission site, and missing one shows up asundefat runtime rather than a compile error.The suite will not catch mistakes here. No spec in the tree declares a capability except
spec/capabilities.kex, which is# kex: skip-beam— so ordinary runs stay green while capability programs break. Whoever picks this up should drive it from capability programs run under-R, not frommake test-all.One correctness point found along the way
Functions defined inside a capability module must not take a context. They are the default implementation, and giving them one turns
'Clock.now'/0into/1, which breaks the very fallback the dispatch relies on:Remaining work, in order
withtoCtx2 = Ctx#{Name => Impl}over the body.maps:get(Name, Ctx, undefined)with the real module as the miss case.# kex: skip-beamfromspec/capabilities.kex.akoskovacs commentedon Aug 22, 2026
Landed:
withnow lowers on BEAM, threaded as one contextspec/capabilities.kexno longer carries# kex: skip-beam— it runs on both backends with identical output, and spec-beam went 258 → 259 matching.The process semantics came out free
This is the part worth recording. The walker needed a per-process binding stack saved and restored around every
resume(), plus an explicit snapshot atspawn, to stop one process changing what another process's code means. On BEAM none of that was written: the context is a value, so a spawned closure captures it and a process spawned outside the region simply never received it. The identicalspawned inside: 7/spawned outside: 1000result falls out of ordinary closure capture.That is the concrete payoff of threading over the process dictionary, and it is why the
after/restore hole never arose — there is nothing to restore.How it is built
withlowers tomaps:put(Name, Impl, Ctx)with the body lowered against the extended map.maps:get(Name, Ctx, undefined)with the capability's own body as the miss case.'Clock.now'/0into/1and breaks the fallback.The call-site enumeration, resolved
As predicted, this was the work. Local calls are emitted from many branches; they now all route through one
localCallhelper so the context is appended in a single place. The sites that mattered were the plain call path, the zero-arg/thunk paths, the module-member path, and the UFCS method path — each found by running a capability program under-Rand reading the arity mismatch, sincemake test-allcannot see these.Remaining gap: nested capabilities
FS2.File.read(p)lowers as a UFCS call on the result ofFS2.Filerather than as a module-qualified call, so the capability dispatch never sees it. Split intospec/capabilities_nested.kex, which keeps# kex: skip-beam.This matters for the stdlib conversion, since every realistic capability there is nested (
FS.File,FS.Directory). Fixing it means making the outer call's candidate resolution recognise a dotted capability receiver.akoskovacs commentedon Aug 22, 2026
Correction: the nested-capability diagnosis above was wrong
I said
FS2.File.read(p)"lowers as a UFCS call on the result ofFS2.File". The Core Erlang shows something more specific, and it changes what the fix is.A nested module becomes its own BEAM module, so the capability's
readis'Kex.FS2.File':read/1— plain nested modules confirm this works:Change
module Innertocapability Innerand it breaks:The call reaches the member through the bare-name UFCS path and appends the capability context there, producing
greet/2against agreet/1. The exclusion that keeps a capability's own members at their declared arity is keyed on the enclosing module, which the bare-name path never consults.So the fix is not about candidate resolution. It is that "this name belongs to a capability's own implementation" has to be knowable from the bare name at a UFCS call site — and doing that naively is unsafe, because a same-named ordinary function would then be excluded too and lose the context its definition expects.
Where this leaves things
Top-level capabilities are landed and verified on both backends;
spec/capabilities.kexruns under-R(spec-beam 258 → 259). Nested capabilities are inspec/capabilities_nested.kex, walker-only, with the corrected diagnosis in its header.This is the last thing standing between the current state and converting the stdlib, since
FS.FileandFS.Directoryare both nested.akoskovacs commentedon Aug 22, 2026
Nested capabilities: the actual cause, third time lucky
Tested the two hypotheses above. Both were wrong, and the real cause is one line of my own dispatch.
Not the capability-member exclusion. Removing it — letting a capability's own members take a context like any other foul function — changes nothing: top-level still works, nested still fails identically. So the arity of the capability's members is not the problem.
It is the miss-branch. The dispatch I emit is:
That first branch goes through
localCall, which always buildsCall{"", name, ...}. For a top-level capability the implementation is a local function in the same emitted module, so that is right. For a nested capability it is not: a nested Kex module becomes its own BEAM module, so the body lives in'Kex.Outer.Inner':greet/Nand the call has to be module-qualified. Emitting it locally producesundefined function greet/2 in use/2.Plain nested modules work precisely because the ordinary path knows to emit the external call; my branch bypasses that path and hand-builds a local one.
The fix
The miss-branch must emit whatever the ordinary module-call path would have emitted, rather than assuming local. The awkward part is structural: the normal path consumes the argument expressions and returns, while the dispatch needs those same arguments twice (once per branch) — which is why I hand-built the call in the first place. Binding the arguments to variables up front and then asking the ordinary path to build a call over those variables is the shape that works.
This is the last blocker for converting the stdlib, since
FS.FileandFS.Directoryare both nested.akoskovacs commentedon Aug 22, 2026
Nested capabilities: the evidence, and why my earlier three diagnoses were wrong
Read the emitted Core instead of guessing. The cause is a UFCS-versus-module-path precedence problem that capabilities expose but do not cause.
Setup. A nested capability with an implementation:
What is emitted for
use:The receiver
Outer.Inneris evaluated as a value, fails, andgreetis applied UFCS-style to the result. The capability dispatch never fires — instrumenting it shows it is not reached at all.Why the make block matters. Without any implementation, the dispatch does fire and emits a correct miss branch (
call 'Kex.Outer.Inner':'greet'(...)); it fails only because the override branch references a local receiver dispatcher that does not exist when nothing implements the capability. Add an implementation and the behaviour changes completely: a make-block method namedgreetmakesOuter.Inner.greet(n)resolve through the receiver/UFCS machinery instead of the module path.Why top-level capabilities are unaffected.
Clock.now()has a bareUpperIdentifierreceiver, which resolves as a module path before UFCS gets a chance. A dotted receiver does not, and a capability always has both a module member and a same-named make method — so this shape is guaranteed to collide.Corrections to what I posted earlier in this thread: it is not "UFCS on the result of
FS2.File" in the sense I meant, it is not the capability-member arity exclusion (removing it changes nothing), and it is not the miss-branch being local (that branch is correct — the emitter already maps a dotted name to'Kex.Outer.Inner').What the fix has to do
Make a dotted module path win over UFCS receiver dispatch when the path names a module in the unit — or, narrower and safer, when it names a capability. This is a precedence question in
lowerMethodCall, not something the capability dispatch can fix from inside, since it never runs.Worth noting the same precedence issue should be reachable without capabilities: a nested module function whose name collides with any make-block method in the unit. Only the capability case is covered by a spec today.
akoskovacs commentedon Aug 22, 2026
Root cause is not capability-specific: filed as #182.
The same failure reproduces with a plain nested module and no capability anywhere, as long as some
makeblock defines a method with the matching name. The walker is right, BEAM lowers the dotted module path as a value. Capabilities merely guarantee the collision, since a capability always has both a module member and a same-namedmakemethod implementing it.So nested capabilities on BEAM are blocked on #182 rather than on anything in this design.
spec/capabilities_nested.kexstays walker-only until that lands.akoskovacs commentedon Aug 22, 2026
Nested capabilities work on BEAM now — the blocker was #182, which is fixed.
spec/capabilities_nested.kexno longer carries# kex: skip-beam, so every capability spec runs on both backends (spec-beam 259 -> 261):That clears the last thing standing between this and converting the stdlib, since
FS.FileandFS.Directoryare both nested.akoskovacs commentedon Aug 22, 2026
The last BEAM blocker: cross-unit override dispatch
Recording the design, since this is the only thing between the current state and converting the stdlib.
The problem. The capability dispatch emits
The override branch needs a compile-time symbol. That is fine when the unit being compiled also implements the capability, and wrong otherwise — and "otherwise" is the normal case, because the call site lives in the stdlib while the implementation replacing it lives in the test.
erlcthen rejects a module over a branch that is never reached:What it needs instead. A runtime receiver dispatch: given the replacement value and a method name, find the module that owns that method for that value's type and apply it. The walker does this naturally because its dispatch is dynamic.
Why it is new infrastructure. The runtime has only two registries,
kex_display_recordsandkex_display_variants, both about rendering — nothing maps a record tag to the module owning its methods. The closest existing hook iskex_io:register_display/2, which each compiled module already calls at startup with the record layouts it owns; it knows both the tag set and its own module atom, so extending it to record tag → module is a contained change rather than a new startup path.Sketch:
register_display/2(or a sibling) also records{Tag, Module}in a persistent term.kex_capability:dispatch(Impl, MethodBin, Args)— reads the tag, looks up the owning module, anderlang:applys. It should raise the same "Undefined method: M for T" message the rest of the runtime uses when the lookup fails, so the two backends stay identical.Caveat worth checking first: a make block need not live in the same module as the record it targets, so tag → module is an approximation.
KexiMethodOwnershipalready exists in the KexI chunk and may carry the precise answer; if so, populate the registry from it rather than from the record layouts.Once this lands,
spec/capabilities.kexand the stdlib conversion both work on BEAM, andMock.*can go.akoskovacs commentedon Aug 22, 2026
Cross-unit override dispatch: landed. The remaining blocker is the gate.
The override branch no longer names a local symbol.
kex_io:dispatch_method/3resolves the implementation at runtime from the value's tag, and each compiled unit reports which module owns its records' methods through a newregister_method_owners/1, emitted beside the existingregister_display/2— so this reuses the startup path rather than adding one.Two details that mattered:
foulby construction, so their implementations take the context too; dispatching[Receiver | Args]alone gaveundefuntil the context was appended, exactly as a direct call would.undefined_methodhelper the rest of the runtime uses, so the backends stay identical when a lookup fails.This fixed the four REPL/filesystem failures that the first stdlib attempt caused —
ctestwent back to 19/19 withFS.Filestill marked as a capability.What still blocks the stdlib conversion
One spec fails with the conversion applied, and it is the ABI question from earlier in this thread rather than a new bug:
The prelude compiles as one unit. Once any stdlib module is a capability, every foul prelude function takes a context — but a consumer program that declares no capability of its own does not thread, so every cross-unit call into the prelude mismatches by one argument.
The gate that made threading safe ("emit nothing unless this unit declares a capability") is precisely what breaks cross-unit consistency. Two ways out:
foulagainst 938letin the stdlib, with pure code untouched. It gives up the current "byte-identical when no capability is declared" property.ExternalModules, notImportedInterfaces, so the flag would have to be carried across.(1) is simpler and matches what this thread already agreed — one context on every foul function. (2) preserves a nicety that stops mattering the moment the stdlib is converted, since the prelude will then always have a capability.
src/stdlib/fs.kexis reverted again; everything else here is kept and the tree is green.akoskovacs commentedon Aug 22, 2026
Tested unconditional threading. It needs one more thing, and I had the reason backwards.
Tried option (1) — drop the gate, every foul function takes a context. It fails, and not for the reason I gave above.
I said the problem was a consumer failing to thread while the prelude did. It is the mirror image: with threading unconditional the prelude's own foul functions become
start/2,double/2and so on, but a consumer has no way to know that an imported function is foul, so it still emitsTask.start/1. Local calls agree; imported ones do not.So neither the gated nor the ungated form is complete on its own:
The good news is that the information already exists:
KexiExport.isFoulis in the chunk today, andImportedFunction/ImportedModuleInterfacecarry it into semantic analysis. What is missing is that lowering receivesExternalModules, not the imported interfaces — so the call site cannot see it. Threading that through (a set of foul export keys, populated from KexI alongsideexportArity) is the concrete next step, and it is the same shape as theisCapabilityplumbing that already landed.Order of work, then:
ExternalModules.FS.Filea capability and delete theMock.FSmachinery.Everything else from this session is committed and green: runtime override dispatch, the KexI capability flag, imported-capability registration, and the local-call routing.
akoskovacs commentedon Aug 22, 2026
Step 1 of the remaining plan is in: imported foulness reaches lowering
ExternalModules.foulExportsnow carries which imported exports arefoul, populated fromKexiExport.isFoulinkexi_registry.cxxbesideexportArity, and merged in the override path inmain.cxx. Inert on its own — nothing reads it yet — and the suite is green with it.What remains, precisely
Step 2: append the context on calls to imported foul functions. This is the same enumeration the local calls needed, on the external side. The emission points are the
externalModules->exportToBeamFnlookups inlower.cxx(currently four: around lines 1540, 1666, 2617, 2732), each building acallE(Atom, BeamFn, ...). They should route through one helper that appendscurrentContext()whenfoulExportsholds the qualified key, mirroringlocalCall.The same caution applies as before:
make test-allcannot see mistakes here. A missed site shows up asundefat runtime in a capability program, not as a compile error. Drive it from a capability program under-R, and from the prelude build, which exercises every shape at once.Step 3: drop the gate (
threadsCapabilities()returns true). Verified failing today only because of step 2 — with the gate off, the prelude's ownTask.startbecomes start/2 while consumers still emit start/1.Step 4: mark
FS.Filea capability and delete theMock.FSmachinery. Everything else for this is already in place: the KexI capability flag, imported-capability registration with trait synthesis, runtime cross-unit override dispatch, and the nested-module fix from #182. The conversion itself is one keyword.Verified along the way
With the conversion applied and cross-unit dispatch in place,
ctestis 19/19 and the only failure isspec/process_task.kex, which is exactly the step-2 gap — a prelude call whose arity changed. So the sequence above is not speculative; each step has a failing test that proves it is needed and will show when it is done.akoskovacs commentedon Aug 22, 2026
Tested the combined change: gate off + imported-foul append together
This is the key result, so recording it precisely.
They must land together, and together they work for capabilities. With the gate dropped and the imported append enabled, both capability specs pass on BEAM. Each half alone fails:
Task.start/2while the prelude, declaring no capability, still exportsstart/1. Both capability specs die withundef(spec-beam 261 → 259).start/2while consumers still emitstart/1.spec/process_task.kexdies withundef.What the combined change leaves. spec-beam goes to 258/3, and the three are not capability specs — they are call shapes that universal threading has not covered yet:
currying.kexbadargcrossfile_using.kexbadargoptionparser_commands.kexundefThe two
badargs are the interesting ones and probably one fix:~foulFn(and anyfun F/Nreference) must account for the context, either by binding it —fun(A1..An) -> f(A1..An, Ctx) end— or by carrying the widened arity through the reference machinery, includingFun.aritybookkeeping in the runtime.Two arity bugs found and fixed along the way
Both were mine, and both were the same mistake: recomputing a call's arity as
args.size()where an explicit value was authoritative —arat the external-call sites,resolved->second.backendArityon the resolved path. They do not always agree, and getting it wrong silently changed calls that had nothing to do with capabilities.State
Reverted to the gated form, which is green (spec-beam 261/0).
foulExportsand theexternalCallExprrouting stay, so enabling the pair is a two-line flip. The next piece of work is the function-reference shape, and it has a failing spec to prove it:spec/currying.kexunder-R.akoskovacs commentedon Aug 22, 2026
A note on approach, having done it the slow way
I found the call sites by trial: apply a capability, run under
-R, read the arity mismatch, patch that site, repeat. That found the plain-call path, the zero-arg/thunk paths, the module-member path, the UFCS path, the sibling-modulecallE("", ...)paths, the resolved-call path, and finally the function-reference shape. Each round costs a full rebuild plus a suite run, and a missed site is invisible until a capability program runs —make test-allcannot see it.The systematic alternative is to centralize at
callE. Every emitted call is built there, so if the append happened in that one place — driven by a table of known-foul targets rather than by whichever branch is being edited — every path would be covered by construction, including ones nobody has enumerated.What it needs:
callEreceives a module ATOM and a backend function name, whilefoulFnsis keyed by local source name andfoulExportsbyShort.name. Building{atom, beamFn} -> foulonce fromExternalModulesplus the local definitions closes that gap.erlang:,maps:andkex_io:are not in either table.It does not cover function references.
fun F/Nis a different node, not acallE, which is exactly whycurrying.kexandcrossfile_using.kexstill fail withbadargafter the call sites agree. That shape needs its own answer — binding the context into the reference (fun(A1..An) -> f(A1..An, Ctx) end) is the obvious one, and it also has to keep the runtime'sFun.aritybookkeeping honest, sincekex_intrinsic_funreads it.So the remaining work is two pieces, not N: centralize the call append, then handle references. That is a much smaller statement than the site-by-site list above, and it is what I would start from rather than continuing the trial loop.
akoskovacs commentedon Aug 22, 2026
Centralizing worked, and the last failure is now exact
Tried the centralized form described above: the append moved into
callE, driven by a{atom, beamFn} -> foultable built once fromExternalModules, with the gate off.It reproduces the per-site result exactly — same three failing specs, no more, no fewer. That is the useful confirmation: the call-site problem is solved, one append covers every emission path, and the per-site enumeration was unnecessary. Runtime BIFs exclude themselves by not being in the table.
The remaining defect, precisely
spec/currying.kex:64:The prelude now exports three arities for that one function:
The extra ones are the context-widened variants. The curry/reference path derives the lambda's parameter count from the callee's backend arity, so
~IO.printLinebuilds a lambda one parameter too wide;eachthen applies it with a single argument and Erlang raisesbadarg.The body is fine — it goes through
buildCall, which appends the context correctly. Only the parameter count is wrong.Fix: the reference and curry paths must use the source parameter count, never the backend arity, because the context is added during lowering and is not a source parameter.
exportParamNamesalready carries the source names, so it is likely a matter of ensuring that is what the arity is taken from on every branch of that path — including the fallback when it is missing, which is where the widenedexportAritycan leak in.crossfile_using.kexfails identically (badarg, same shape across files).optionparser_commands.kexfails withundefand has not been diagnosed.State
Reverted to the gated form; the tree is green (spec-beam 261/0) with
foulExportsand the routing kept. Enabling the pair is a two-line change once the reference arity is fixed, andspec/currying.kexunder-Ris the proving test.akoskovacs commentedon Aug 22, 2026
The blocker is not a missed call site — the context collides with trait dictionaries
Instrumented instead of guessing, and the previous explanation (curry taking a widened arity) is wrong: the probe shows the curry arity is 1, which is correct, and the emitted Core is correct too:
The
badargis raised insideprintLine/2, and that arity is not what I assumed:printLine(msg: Showable)emitsprintLine/1plus a dictionary variantprintLine/2. Threading makes the genuine targetprintLine/3. Appending the context produced/2, which lands on the dictionary wrapper and hands it the context map where aShowabledictionary belongs.Why this matters beyond one function
A positional context can collide with an existing arity of the same name. Trait dictionaries already occupy trailing positions, so "append one more argument" is not a safe ABI extension on its own — the arity alone no longer identifies the target. This is not a missed call site and cannot be fixed by finding more of them.
Whatever lands has to make the context unambiguous. Options, roughly in order of how much they disturb what exists:
printLineandprintLine$ctxnever collide, and dictionaries keep their slots.(1) looks cleanest: it keeps dictionaries untouched and makes "threaded" visible in the symbol, which also makes a mismatch a link error rather than a silent misinterpretation of an argument.
Where this leaves things
The gated form is committed and green (spec-beam 261/0). The three specs that fail with threading on —
currying,crossfile_using,optionparser_commands— are all this same collision, not three separate gaps, which is why fixing call sites never converged.Also worth noting for whoever picks it up:
each { |x| IO.printLine(x) }works whileeach(~IO.printLine)does not, because the direct call resolves the dictionary properly and the reference does not.akoskovacs commentedon Aug 22, 2026
BEAM
withis done, and the stdlib conversion worksThreading is now universal — every foul function takes the context, in every unit — and the whole suite is green with it: ctest 19/19, spec 277/0, spec-beam 262/0, prelude 462/0 and 453/0, stdlib 59/0 on both backends.
FS.Fileis a realcapabilityinsrc/stdlib/fs.kex, replaceable from user code on both backends with no global mock state.spec/capability_stdlib_fs.kexcovers it.Confirming option C — the "collision" was an ordering bug
My earlier framing (positional context fundamentally collides with trait-dictionary arities) was wrong. The wrapper was:
makeDictionaryWrapperderives its own parameter count fromarity - dictionaryTraits.size(), which swallowed the context slot — so it forwarded(msg, Ctx)and appended the dictionary last, while the definition emits(msg, Dict, Ctx). The real function then calledshowValueon the context map.FunDef::hasCapabilityContextnow tells the wrapper the slot exists; it takes the context as its own trailing parameter and forwards it after the dictionaries.That one fix took spec-beam from 258/3 to 260/1 —
curryingandcrossfile_usingboth.The other two gaps it uncovered
cli.run(...)resolves throughExternalModules.receiverFunctions, a separate table fromexportToBeamFn, so receiver calls never got the context.ReceiverFunction::isFoulfixes it — that was the last failing spec.Still open, and why
Converting prelude capabilities (
ENV,IO, …) needs one more dispatch site.FSis opt-in, sousing FSmerges its source and its calls resolve throughmoduleFunctions, where the dispatch lives. A prelude module reaches a consumer only through the external tables, andENV.get(...)lowers through a third path — the qualifiedFunctionCallroute — which has no dispatch. The symptom is the dangerous one:with ENV = FakeEnv { … }compiles and runs, and silently uses the real environment.The three dispatch sites should be collapsed into one before adding a fourth. The
callEcentralisation worked for the append; the dispatch is harder because it replaces the call rather than augmenting it, but the same argument applies — enumerating sites is how this stayed broken.Deleting
Mock.*waits on that, sinceMock.ENV,Mock.IOandMock.Httpall cover prelude modules.Keep IO mock working, closing #181, #174