haskell: Phase 16 — exception handling (catch/try/throwIO/evaluate/handle/throw)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 44s

hk-bind-exceptions! in eval.sx registers throwIO, throw, evaluate, catch,
try, handle, displayException. SomeException constructor pre-registered
in runtime.sx (arity 1, type SomeException).

throwIO and the existing error primitive both raise via SX `raise` with a
uniform "hk-error: msg" string. catch/try/handle parse it back into a
SomeException via hk-exception-of, which strips nested
'Unhandled exception: "..."' host wraps (CEK's host_error formatter) and
the "hk-error: " prefix.

catch and handle evaluate the handler outside the guard scope (build an
"ok"/"exn" outcome tag inside guard, then dispatch outside) so that a
re-throw from the handler propagates past this catch — matching Haskell
semantics rather than infinite-looping in the same guard.

14 unit tests in tests/exceptions.sx (catch success, catch error, try
Right/Left, handle, throwIO + catch/try, evaluate, nested catch, do-bind
through catch, branch on try result, IORef-mutating handler).

Conformance: safediv.hs (8/8) and trycatch.hs (8/8). Scoreboard now
285/285 tests, 36/36 programs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-08 00:17:46 +00:00
parent 544e79f533
commit e83c01cdcc
9 changed files with 455 additions and 14 deletions

View File

@@ -292,21 +292,21 @@ No OCaml changes are needed. The view type is fully representable as an SX dict.
### Phase 16 — Exception handling
- [ ] `SomeException` type: `data SomeException = SomeException String`.
- [x] `SomeException` type: `data SomeException = SomeException String`.
`IOException = SomeException`.
- [ ] `throwIO :: Exception e => e -> IO a` — raises `("hk-exception" e)`.
- [ ] `evaluate :: a -> IO a` — forces arg strictly; any embedded `hk-error`
- [x] `throwIO :: Exception e => e -> IO a` — raises `("hk-exception" e)`.
- [x] `evaluate :: a -> IO a` — forces arg strictly; any embedded `hk-error`
surfaces as a catchable `SomeException`.
- [ ] `catch :: Exception e => IO a -> (e -> IO a) -> IO a` — wraps action in
- [x] `catch :: Exception e => IO a -> (e -> IO a) -> IO a` — wraps action in
SX `guard`; on `hk-error` or `hk-exception`, calls the handler with a
`SomeException` value.
- [ ] `try :: Exception e => IO a -> IO (Either e a)` — returns `Right v` on
- [x] `try :: Exception e => IO a -> IO (Either e a)` — returns `Right v` on
success, `Left e` on any exception.
- [ ] `handle = flip catch`.
- [ ] Tests in `lib/haskell/tests/exceptions.sx` (≥ 10 tests: catch success,
- [x] `handle = flip catch`.
- [x] Tests in `lib/haskell/tests/exceptions.sx` (≥ 10 tests: catch success,
catch error, try Right, try Left, nested catch, evaluate surfaces error,
throwIO propagates, handle alias).
- [ ] Conformance programs:
- [x] Conformance programs:
- `safediv.hs` — safe division using `catch`; divide-by-zero raises,
handler returns 0.
- `trycatch.hs` — `try` pattern: run an action, branch on Left/Right.
@@ -315,6 +315,19 @@ No OCaml changes are needed. The view type is fully representable as an SX dict.
_Newest first._
**2026-05-08** — Phase 16 Exception handling complete (6 ops + module wiring +
14 unit tests + 2 conformance programs). `hk-bind-exceptions!` in `eval.sx`
registers `throwIO`, `throw`, `evaluate`, `catch`, `try`, `handle`, and
`displayException`. `SomeException` constructor pre-registered in
`runtime.sx`. `throwIO` and the `error` primitive both raise via SX `raise`
with a uniform `"hk-error: msg"` string; catch/try/handle parse this string
back into a `SomeException` via `hk-exception-of` (which strips nested
`Unhandled exception: "..."` host wraps and the `hk-error: ` prefix). catch
and handle evaluate the handler outside the guard scope, so a re-throw from
the handler propagates past this catch (matching Haskell semantics, not an
infinite loop). Phase 16 phase complete: scoreboard now 285/285 tests,
36/36 programs.
**2026-05-07** — Fix string ↔ `[Char]` equality. `reverse`/`length`/`head`/etc.
on a string transparently coerce to a cons-list of char codes via `hk-str-head`
+ `hk-str-tail`, but `(==)` then compared the original raw string against the