ocaml: phase 5 HM pattern-match inference (+5 tests, 344 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s

ocaml-infer-pat covers :pwild, :pvar, :plit, :pcons, :plist, :ptuple,
:pas. Returns {:type T :env ENV2 :subst S} where ENV2 has the pattern's
bound names threaded through.

ocaml-infer-match unifies each clause's pattern type with the scrutinee,
runs the body in the env extended with pattern bindings, and unifies
all body types via a fresh result tv.

Examples:
  fun lst -> match lst with | [] -> 0 | h :: _ -> h : Int list -> Int
  match (1, 2) with | (a, b) -> a + b              : Int

Constructor patterns (:pcon) fall through to a fresh tv for now —
proper handling needs a ctor type registry from 'type' declarations.
This commit is contained in:
2026-05-08 13:02:15 +00:00
parent 202ea9cf5f
commit d2bf0c0d00
3 changed files with 135 additions and 0 deletions

View File

@@ -844,6 +844,18 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 1706)
(eval "(ocaml-run \"List.sort compare [\\\"b\\\"; \\\"a\\\"; \\\"c\\\"]\")")
;; ── HM pattern-match inference ─────────────────────────────────
(epoch 1800)
(eval "(ocaml-type-of \"match 1 with | n -> n + 1\")")
(epoch 1801)
(eval "(ocaml-type-of \"match [1;2] with | [] -> 0 | h :: t -> h\")")
(epoch 1802)
(eval "(ocaml-type-of \"match (1, 2) with | (a, b) -> a + b\")")
(epoch 1803)
(eval "(ocaml-type-of \"fun x -> match x with | 0 -> 0 | n -> n + 1\")")
(epoch 1804)
(eval "(ocaml-type-of \"fun lst -> match lst with | [] -> 0 | h :: _ -> h\")")
EPOCHS
OUTPUT=$(timeout 180 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -1335,6 +1347,13 @@ check 1704 "List.sort descending" '(4 3 1)'
check 1705 "List.sort empty" '()'
check 1706 "List.sort strings" '("a" "b" "c")'
# ── HM match inference ──────────────────────────────────────────
check 1800 "match int" '"Int"'
check 1801 "match list" '"Int"'
check 1802 "match tuple" '"Int"'
check 1803 "fn match int -> int" '"Int -> Int"'
check 1804 "fn list -> elem" '"Int list -> Int"'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"