haskell: type error reporting — hk-expr->brief + hk-infer-decl/prog (+21 tests, 455/455)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-05 21:40:16 +00:00
parent 8f3b0d9301
commit 68124adc3b
4 changed files with 192 additions and 7 deletions

View File

@@ -431,6 +431,10 @@
elems)
(list s-acc (hk-t-list (hk-subst-apply s-acc tv))))))))
;; Location annotation: just delegate — position is for outer context.
((= tag "loc")
(hk-w env (nth expr 3)))
(:else
(raise (str "hk-w: unhandled tag: " tag)))))))
@@ -472,6 +476,105 @@
(dict-set! env "abs" (hk-tarr hk-t-int hk-t-int))
env)))
;; ─── Expression brief printer ────────────────────────────────────────────────
;; Produces a short human-readable label for an AST node used in error messages.
(define
hk-expr->brief
(fn
(expr)
(cond
((not (list? expr)) (str expr))
((empty? expr) "()")
(:else
(let ((tag (first expr)))
(cond
((= tag "var") (nth expr 1))
((= tag "con") (nth expr 1))
((= tag "int") (str (nth expr 1)))
((= tag "float") (str (nth expr 1)))
((= tag "string") (str "\"" (nth expr 1) "\""))
((= tag "char") (str "'" (nth expr 1) "'"))
((= tag "neg") (str "(-" (hk-expr->brief (nth expr 1)) ")"))
((= tag "app")
(str "(" (hk-expr->brief (nth expr 1))
" " (hk-expr->brief (nth expr 2)) ")"))
((= tag "op")
(str "(" (hk-expr->brief (nth expr 2))
" " (nth expr 1)
" " (hk-expr->brief (nth expr 3)) ")"))
((= tag "lambda") "(\\ ...)")
((= tag "let") "(let ...)")
((= tag "if") "(if ...)")
((= tag "tuple") "(tuple ...)")
((= tag "list") "[...]")
((= tag "loc") (hk-expr->brief (nth expr 3)))
(:else (str "(" tag " ..."))))))))
;; ─── Loc-annotated inference ──────────────────────────────────────────────────
;; ("loc" LINE COL INNER) node: hk-w catches any error and re-raises with
;; "at LINE:COL: " prepended. Emitted by the parser or test scaffolding.
;; Extended hk-w handles "loc" — handled inline in the cond below.
;; ─── Program-level inference ─────────────────────────────────────────────────
;; hk-infer-decl : env × decl → ("ok" name type-str) | ("err" msg) | nil
;; Uses tagged results so callers don't need re-raise.
(define
hk-infer-decl
(fn
(env decl)
(let ((tag (first decl)))
(cond
((= tag "fun-clause")
(let ((name (nth decl 1))
(pats (nth decl 2))
(body (nth decl 3)))
(let ((rhs (if (empty? pats) body (list "lambda" pats body))))
(guard
(e (#t (list "err" (str "in '" name "': " e))))
(begin
(hk-reset-fresh)
(let ((r (hk-w env rhs)))
(list "ok" name
(hk-type->str (hk-subst-apply (first r) (nth r 1))))))))))
((or (= tag "bind") (= tag "pat-bind"))
(let ((pat (nth decl 1))
(body (nth decl 2)))
(let ((label (if (and (list? pat) (= (first pat) "p-var"))
(nth pat 1)
"<binding>")))
(guard
(e (#t (list "err" (str "in '" label "': " e))))
(begin
(hk-reset-fresh)
(let ((r (hk-w env body)))
(list "ok" label
(hk-type->str (hk-subst-apply (first r) (nth r 1))))))))))
(:else nil)))))
;; hk-infer-prog : program-ast × env → list of ("ok" name type) | ("err" msg)
(define
hk-infer-prog
(fn
(prog env)
(let ((decls (cond
((and (list? prog) (= (first prog) "program"))
(nth prog 1))
((and (list? prog) (= (first prog) "module"))
(nth prog 3))
(:else (list))))
(results (list)))
(for-each
(fn (d)
(let ((r (hk-infer-decl env d)))
(when (not (nil? r))
(append! results r))))
decls)
results)))
;; ─── Convenience ─────────────────────────────────────────────────────────────
;; hk-infer-type : Haskell expression source → inferred type string

View File

@@ -14,7 +14,7 @@ cd "$(git rev-parse --show-toplevel)"
SX_SERVER="hosts/ocaml/_build/default/bin/sx_server.exe"
if [ ! -x "$SX_SERVER" ]; then
# Fall back to the main-repo build if we're in a worktree.
MAIN_ROOT=$(git worktree list | head -1 | awk '{print $1}')
MAIN_ROOT=$(git worktree list | awk 'NR==1{print $1}')
if [ -x "$MAIN_ROOT/$SX_SERVER" ]; then
SX_SERVER="$MAIN_ROOT/$SX_SERVER"
else
@@ -42,6 +42,9 @@ FAILED_FILES=()
for FILE in "${FILES[@]}"; do
[ -f "$FILE" ] || { echo "skip $FILE (not found)"; continue; }
# Load infer.sx only for infer test files (it adds ~6s overhead).
INFER_LOAD=""
case "$FILE" in *infer*) INFER_LOAD='(load "lib/haskell/infer.sx")' ;; esac
TMPFILE=$(mktemp)
cat > "$TMPFILE" <<EPOCHS
(epoch 1)
@@ -52,7 +55,7 @@ for FILE in "${FILES[@]}"; do
(load "lib/haskell/runtime.sx")
(load "lib/haskell/match.sx")
(load "lib/haskell/eval.sx")
(load "lib/haskell/infer.sx")
$INFER_LOAD
(load "lib/haskell/testlib.sx")
(epoch 2)
(load "$FILE")
@@ -60,14 +63,14 @@ for FILE in "${FILES[@]}"; do
(eval "(list hk-test-pass hk-test-fail)")
EPOCHS
OUTPUT=$(timeout 60 "$SX_SERVER" < "$TMPFILE" 2>&1 || true)
OUTPUT=$(timeout 240 "$SX_SERVER" < "$TMPFILE" 2>&1 || true)
rm -f "$TMPFILE"
# Output format: either "(ok 3 (P F))" on one line (short result) or
# "(ok-len 3 N)\n(P F)" where the value appears on the following line.
LINE=$(echo "$OUTPUT" | awk '/^\(ok-len 3 / {getline; print; exit}')
if [ -z "$LINE" ]; then
LINE=$(echo "$OUTPUT" | grep -E '^\(ok 3 \([0-9]+ [0-9]+\)\)' | tail -1 \
LINE=$(echo "$OUTPUT" | { grep -E '^\(ok 3 \([0-9]+ [0-9]+\)\)' || true; } | tail -1 \
| sed -E 's/^\(ok 3 //; s/\)$//')
fi
if [ -z "$LINE" ]; then
@@ -95,14 +98,14 @@ EPOCHS
(load "lib/haskell/runtime.sx")
(load "lib/haskell/match.sx")
(load "lib/haskell/eval.sx")
(load "lib/haskell/infer.sx")
$INFER_LOAD
(load "lib/haskell/testlib.sx")
(epoch 2)
(load "$FILE")
(epoch 3)
(eval "(map (fn (f) (get f \"name\")) hk-test-fails)")
EPOCHS
FAILS=$(timeout 60 "$SX_SERVER" < "$TMPFILE2" 2>&1 | grep -E '^\(ok 3 ' || true)
FAILS=$(timeout 240 "$SX_SERVER" < "$TMPFILE2" 2>&1 | grep -E '^\(ok 3 ' || true)
rm -f "$TMPFILE2"
echo " $FAILS"
elif [ "$VERBOSE" = "1" ]; then

View File

@@ -66,4 +66,72 @@
(hk-t "null []" "Bool")
(hk-t "head [1, 2, 3]" "Int")
;; ─── hk-expr->brief ──────────────────────────────────────────────────────────
(hk-test "brief var" (hk-expr->brief (list "var" "x")) "x")
(hk-test "brief con" (hk-expr->brief (list "con" "Just")) "Just")
(hk-test "brief int" (hk-expr->brief (list "int" 42)) "42")
(hk-test "brief app" (hk-expr->brief (list "app" (list "var" "f") (list "var" "x"))) "(f x)")
(hk-test "brief op" (hk-expr->brief (list "op" "+" (list "int" 1) (list "int" 2))) "(1 + 2)")
(hk-test "brief lambda" (hk-expr->brief (list "lambda" (list) (list "var" "x"))) "(\\ ...)")
(hk-test "brief loc" (hk-expr->brief (list "loc" 3 7 (list "var" "x"))) "x")
;; ─── Type error messages ─────────────────────────────────────────────────────
;; Helper: catch the error and check it contains a substring.
(define hk-str-has? (fn (s sub) (>= (index-of s sub) 0)))
(define hk-te
(fn (label src sub)
(hk-test label
(guard (e (#t (hk-str-has? e sub)))
(begin (hk-infer-type src) false))
true)))
;; Unbound variable error includes the variable name.
(hk-te "error unbound name" "foo + 1" "foo")
(hk-te "error unbound unk" "unknown" "unknown")
;; Unification error mentions the conflicting types.
(hk-te "error unify int-bool-1" "1 + True" "Int")
(hk-te "error unify int-bool-2" "1 + True" "Bool")
;; ─── Loc node: passes through to inner (position decorates outer context) ────
(define hk-loc-err-msg
(fn ()
(guard (e (#t e))
(begin
(hk-reset-fresh)
(hk-w (hk-type-env0) (list "loc" 5 10 (list "var" "mystery")))
"no-error"))))
(hk-test "loc passes through to var error"
(hk-str-has? (hk-loc-err-msg) "mystery")
true)
;; ─── hk-infer-decl ───────────────────────────────────────────────────────────
;; Returns ("ok" name type) | ("err" msg)
(define hk-env0-t (hk-type-env0))
(define prog1 (hk-core "f x = x + 1"))
(define decl1 (first (nth prog1 1)))
(define res1 (hk-infer-decl hk-env0-t decl1))
(hk-test "decl result tag" (first res1) "ok")
(hk-test "decl result name" (nth res1 1) "f")
(hk-test "decl result type" (nth res1 2) "Int -> Int")
;; Error decl: result is ("err" "in 'g': ...")
(define prog2 (hk-core "g x = x + True"))
(define decl2 (first (nth prog2 1)))
(define res2 (hk-infer-decl hk-env0-t decl2))
(hk-test "decl error tag" (first res2) "err")
(hk-test "decl error has g" (hk-str-has? (nth res2 1) "g") true)
(hk-test "decl error has msg" (hk-str-has? (nth res2 1) "unify") true)
;; ─── hk-infer-prog ───────────────────────────────────────────────────────────
;; Returns list of ("ok"/"err" ...) tagged results.
(define prog3 (hk-core "double x = x + x\ntwice f x = f (f x)"))
(define results3 (hk-infer-prog prog3 hk-env0-t))
;; results3 = (("ok" "double" "Int -> Int") ("ok" "twice" "..."))
(hk-test "infer-prog count" (len results3) 2)
(hk-test "infer-prog double" (nth (nth results3 0) 2) "Int -> Int")
(hk-test "infer-prog twice" (nth (nth results3 1) 2) "(t3 -> t3) -> t3 -> t3")
{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}