haskell: reject untypeable programs — hk-typecheck + hk-run-typed (+9 tests, 464/464)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 46s

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-05 22:32:18 +00:00
parent 68124adc3b
commit 2606b83920
5 changed files with 121 additions and 38 deletions

View File

@@ -0,0 +1,44 @@
;; typecheck.sx — tests for hk-typecheck / hk-run-typed.
;; Verifies that untypeable programs are rejected and well-typed programs pass.
(define hk-str-has? (fn (s sub) (>= (index-of s sub) 0)))
;; Helper: expect a type error containing `sub`
(define
hk-tc-err
(fn
(label src sub)
(hk-test
label
(guard
(e (#t (hk-str-has? e sub)))
(begin (hk-run-typed src) false))
true)))
;; ─── Valid programs pass through ─────────────────────────────────────────────
(hk-test "typed ok: simple arithmetic" (hk-run-typed "main = 1 + 2") 3)
(hk-test "typed ok: boolean" (hk-run-typed "main = True") (list "True"))
(hk-test "typed ok: let binding" (hk-run-typed "main = let x = 1 in x + 2") 3)
(hk-test
"typed ok: two independent fns"
(hk-run-typed "f x = x + 1\nmain = f 5")
6)
;; ─── Untypeable programs are rejected ────────────────────────────────────────
;; Adding Int and Bool is a unification failure.
(hk-tc-err "reject: Int + Bool mentions Int" "main = 1 + True" "Int")
(hk-tc-err "reject: Int + Bool mentions Bool" "main = 1 + True" "Bool")
;; Condition of if must be Bool.
(hk-tc-err "reject: if non-bool condition" "main = if 1 then 2 else 3" "Bool")
;; Unbound variable.
(hk-tc-err "reject: unbound variable" "main = unknownVar + 1" "unknownVar")
;; Function body type error: applying non-function.
(hk-tc-err "reject: apply non-function" "f x = 1 x" "Int")
{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}