ocaml: phase 6 Hashtbl.keys/values/bindings/remove/clear (+4 tests, 545 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s

Two new host primitives:
  _hashtbl_remove t k   -> dissoc the key from the underlying dict
  _hashtbl_clear  t     -> reset the cell to {}

Eight new OCaml-syntax helpers in runtime.sx Hashtbl module:
  bindings t            = _hashtbl_to_list t
  keys t                = List.map (fun (k, _) -> k) (...)
  values t              = List.map (fun (_, v) -> v) (...)
  to_seq t              = bindings t
  to_seq_keys / to_seq_values
  remove / clear / reset

The keys/values implementations use a 'fun pair -> match pair with
(k, _) -> k' indirection because parse-fun does not currently allow
tuple patterns directly on parameters. Same restriction we worked
around in iteration 98's let-pattern desugaring.

Also: a detour attempting to add top-level 'let (a, b) = expr'
support was started but reverted — parse-decl-let in the outer
ocaml-parse-program scope does not have access to parse-pattern
(which is local to ocaml-parse). Will need a slice + re-parse trick
later.
This commit is contained in:
2026-05-09 03:59:20 +00:00
parent dab8718289
commit 41190c6d23
4 changed files with 58 additions and 0 deletions

View File

@@ -1350,6 +1350,16 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 5092)
(eval "(ocaml-run \"let pair = (5, 7) in let (x, y) = pair in x * y\")")
;; ── Hashtbl.keys / values / remove / clear ────────────────────
(epoch 5100)
(eval "(ocaml-run \"let t = Hashtbl.create 4 in Hashtbl.add t \\\"a\\\" 1; Hashtbl.add t \\\"b\\\" 2; List.length (Hashtbl.keys t)\")")
(epoch 5101)
(eval "(ocaml-run \"let t = Hashtbl.create 4 in Hashtbl.add t \\\"a\\\" 5; Hashtbl.add t \\\"b\\\" 7; List.fold_left (+) 0 (Hashtbl.values t)\")")
(epoch 5102)
(eval "(ocaml-run \"let t = Hashtbl.create 4 in Hashtbl.add t \\\"x\\\" 1; Hashtbl.add t \\\"y\\\" 2; Hashtbl.remove t \\\"x\\\"; Hashtbl.length t\")")
(epoch 5103)
(eval "(ocaml-run \"let t = Hashtbl.create 4 in Hashtbl.add t \\\"a\\\" 1; Hashtbl.clear t; Hashtbl.length t\")")
EPOCHS
OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -2145,6 +2155,12 @@ check 5090 "let (a, b) = (1,2)" '3'
check 5091 "let (a, b, c) = (10,20,30)" '60'
check 5092 "let pair; let (x, y) = pair" '35'
# ── Hashtbl.keys/values/remove/clear ────────────────────────────
check 5100 "Hashtbl.keys length" '2'
check 5101 "Hashtbl.values sum" '12'
check 5102 "Hashtbl.remove + length" '1'
check 5103 "Hashtbl.clear + length" '0'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"