feat: complete evidence threading in C backend

C backend now fully threads evidence through effectful function calls:

- Track effectful functions via effectful_functions HashSet
- Add has_evidence flag to track context during code generation
- Add LuxEvidence* ev parameter to effectful function signatures
- Transform effect operations to use ev->console->print() when evidence available
- Update function calls to pass evidence (ev or &default_evidence)
- Update main entry point to pass &default_evidence

Generated code now uses zero-cost evidence passing:
  void greet_lux(LuxEvidence* ev) {
      ev->console->print(ev->console->env, "Hello!");
  }

This completes the evidence passing implementation for both interpreter
(O(1) HashMap lookup) and C backend (direct function pointer calls).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 11:58:26 -05:00
parent ce4ab45651
commit 52cb38805a
3 changed files with 160 additions and 48 deletions

View File

@@ -251,13 +251,23 @@ Koka also compiles to C with algebraic effects. Key differences:
## Current Progress
### Evidence Passing (Zero-Cost Effects) - PARTIALLY COMPLETE
### Evidence Passing (Zero-Cost Effects) COMPLETE
**Interpreter:** ✅ Complete - O(1) HashMap lookup instead of O(n) stack search.
**C Backend Infrastructure:** ✅ Complete - Handler structs and evidence types generated.
**C Backend:** ✅ Complete - Full evidence threading through function calls.
**C Backend Threading:** 🔜 Planned - Passing evidence through function calls.
**Generated code example:**
```c
void greet_lux(LuxEvidence* ev) {
ev->console->print(ev->console->env, "Hello!");
}
int main(int argc, char** argv) {
greet_lux(&default_evidence);
return 0;
}
```
See [docs/EVIDENCE_PASSING.md](EVIDENCE_PASSING.md) for details.
@@ -265,28 +275,6 @@ See [docs/EVIDENCE_PASSING.md](EVIDENCE_PASSING.md) for details.
## Future Roadmap
### Phase 1: Complete Evidence Passing in C Backend
**Goal:** Thread evidence through all effectful function calls.
**Current state:** Handler types (`LuxConsoleHandler`, etc.) and `LuxEvidence` struct
are generated, but not yet used. Effect calls still use hardcoded `lux_console_print()`.
**Required changes:**
```c
// Current (hardcoded):
void greet_lux(LuxString name) {
lux_console_print(name);
}
// Target (evidence passing):
void greet_lux(LuxEvidence* ev, LuxString name) {
ev->console->print(ev->console->env, name);
}
```
**Expected speedup:** 10-20x for effect-heavy code (based on Koka benchmarks).
### Phase 2: Perceus Reference Counting
**Goal:** Deterministic memory management without GC pauses.