haskell: thunks + force, app args become lazy (+6 tests, 333/333)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 23:22:21 +00:00
parent fba92c2b69
commit 0e53e88b02
4 changed files with 222 additions and 94 deletions

View File

@@ -75,9 +75,9 @@ Key mappings:
- [x] 30+ eval tests in `lib/haskell/tests/eval.sx`
### Phase 3 — laziness + classic programs
- [ ] Transpile to thunk-wrapped SX: every application arg becomes `(make-thunk (lambda () <arg>))`
- [ ] `force` = SX eval-thunk-to-WHNF primitive
- [ ] Pattern match forces scrutinee before matching
- [x] Transpile to thunk-wrapped SX: every application arg becomes `(make-thunk (lambda () <arg>))`
- [x] `force` = SX eval-thunk-to-WHNF primitive
- [x] Pattern match forces scrutinee before matching
- [ ] Infinite structures: `repeat x`, `iterate f x`, `[1..]`, Fibonacci stream, sieve of Eratosthenes
- [ ] `seq`, `deepseq` from Prelude
- [ ] Do-notation for a stub `IO` monad (just threading, no real side effects yet)
@@ -114,6 +114,32 @@ Key mappings:
_Newest first._
- **2026-04-24** — Phase 3 laziness foundation. Added a thunk type to
`lib/haskell/eval.sx` (`hk-mk-thunk` / `hk-is-thunk?`) backed by a
one-shot memoizing `hk-force` that evaluates the deferred AST, then
flips a `forced` flag and caches the value on the thunk dict; the
shared `hk-deep-force` walks the result tree at the test/output
boundary. Three single-line wiring changes in the evaluator make
every application argument lazy: `:app` now wraps its argument in
`hk-mk-thunk` rather than evaluating it. To preserve correctness
where values must be inspected, `hk-apply`, `hk-eval-op`,
`hk-eval-if`, `hk-eval-case`, and `hk-eval` for `:neg` now force
their operand. `hk-apply-builtin` forces every collected arg
before invoking the underlying SX fn so built-ins (`error`, `not`,
`id`) stay strict. The pattern matcher in `match.sx` now forces
the scrutinee just-in-time only for patterns that need to inspect
shape — `p-wild`, `p-var`, `p-as`, and `p-lazy` are no-force
paths, so the value flows through as a thunk and binding
preserves laziness. `hk-match-list-pat` forces at every cons-spine
step. 6 new lazy-specific tests in `lib/haskell/tests/eval.sx`
verify that `(\x y -> x) 1 (error …)` and `(\x y -> y) (error …) 99`
return without diverging, that `case Just (error …) of Just _ -> 7`
short-circuits, that `const` drops its second arg, that
`myHead (1 : error … : [])` returns 1 without touching the tail,
and that `Just (error …)` survives a wildcard-arm `case`. 333/333
green, all prior eval tests preserved by deep-forcing the result
in `hk-eval-expr-source` and `hk-prog-val`.
- **2026-04-24** — Phase 2 evaluator (`lib/haskell/eval.sx`) — ties
the whole pipeline together. Strict semantics throughout (laziness
is Phase 3). Function values are tagged dicts: `closure`,