ocaml: phase 4 'fun (a, b) -> body' tuple-param destructuring (+4 tests, 553 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s

parse-fun's collect-params now detects '(IDENT, ...)' as a
tuple-pattern parameter (lookahead at peek-tok-at 1/2 distinguishes
from '(x : T)' and '()' cases that try-consume-param! already
handles). For each tuple param it:

  1. parse-pattern to get the full pattern AST
  2. generate a synthetic __pat_N name as the actual fun parameter
  3. push (synth_name, pattern) onto tuple-binds

After parsing the body, wraps it innermost-first with one
'match __pat_N with PAT -> ...' per tuple-param. The user-visible
result is a (:fun (params...) body) where params are all simple
names but the body destructures.

Also retroactively simplifies Hashtbl.keys/values from
'fun pair -> match pair with (k, _) -> k' to plain
'fun (k, _) -> k', closing the iteration-99 workaround.

  (fun (a, b) -> a + b) (3, 7)              = 10
  List.map (fun (a, b) -> a * b)
           [(1, 2); (3, 4); (5, 6)]         = [2; 12; 30]
  List.map (fun (k, _) -> k)
           [("a", 1); ("b", 2)]              = ["a"; "b"]
  (fun a (b, c) d -> a + b + c + d) 1 (2, 3) 4 = 10
This commit is contained in:
2026-05-09 04:25:18 +00:00
parent 8ca3ef342d
commit 64f4f10c32
4 changed files with 73 additions and 12 deletions

View File

@@ -1370,6 +1370,16 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 5113)
(eval "(ocaml-run \"Random.init 7; Random.bool ()\")")
;; ── fun (a, b) -> body — tuple param destructure ──────────────
(epoch 5120)
(eval "(ocaml-run \"(fun (a, b) -> a + b) (3, 7)\")")
(epoch 5121)
(eval "(ocaml-run \"List.map (fun (a, b) -> a * b) [(1, 2); (3, 4); (5, 6)]\")")
(epoch 5122)
(eval "(ocaml-run \"List.map (fun (k, _) -> k) [(\\\"a\\\", 1); (\\\"b\\\", 2)]\")")
(epoch 5123)
(eval "(ocaml-run \"(fun a (b, c) d -> a + b + c + d) 1 (2, 3) 4\")")
EPOCHS
OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -2177,6 +2187,12 @@ check 5111 "Random.int x3 seed=42 sum" '152'
check 5112 "Random.int 10 seed=1" '0'
check 5113 "Random.bool seed=7" 'true'
# ── fun (a, b) -> body tuple param ──────────────────────────────
check 5120 "fun (a, b) -> a + b" '10'
check 5121 "List.map fun (a, b)" '(2 12 30)'
check 5122 "List.map fun (k, _)" '("a" "b")'
check 5123 "fun a (b, c) d mixed" '10'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"