Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
Full HM inference in lib/haskell/infer.sx: unification, substitution, occurs check, instantiation, generalisation, let-polymorphism. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
70 lines
3.5 KiB
Plaintext
70 lines
3.5 KiB
Plaintext
;; infer.sx tests — Algorithm W: literals, vars, lambdas, application, let,
|
|
;; if, operators, tuples, lists, let-polymorphism.
|
|
|
|
(define hk-t (fn (src expected)
|
|
(hk-test (str "infer: " src) (hk-infer-type src) expected)))
|
|
|
|
;; ─── Literals ────────────────────────────────────────────────────────────────
|
|
(hk-t "1" "Int")
|
|
(hk-t "3.14" "Float")
|
|
(hk-t "\"hello\"" "String")
|
|
(hk-t "'x'" "Char")
|
|
(hk-t "True" "Bool")
|
|
(hk-t "False" "Bool")
|
|
|
|
;; ─── Arithmetic and boolean operators ────────────────────────────────────────
|
|
(hk-t "1 + 2" "Int")
|
|
(hk-t "3 * 4" "Int")
|
|
(hk-t "10 - 3" "Int")
|
|
(hk-t "True && False" "Bool")
|
|
(hk-t "True || False" "Bool")
|
|
(hk-t "not True" "Bool")
|
|
(hk-t "1 == 1" "Bool")
|
|
(hk-t "1 < 2" "Bool")
|
|
|
|
;; ─── Lambda ───────────────────────────────────────────────────────────────────
|
|
;; \x -> x (identity) should get t1 -> t1
|
|
(hk-test "infer: identity lambda" (hk-infer-type "\\x -> x") "t1 -> t1")
|
|
|
|
;; \x -> x + 1 : Int -> Int
|
|
(hk-test "infer: lambda add" (hk-infer-type "\\x -> x + 1") "Int -> Int")
|
|
|
|
;; \x -> not x : Bool -> Bool
|
|
(hk-test "infer: lambda not" (hk-infer-type "\\x -> not x") "Bool -> Bool")
|
|
|
|
;; \x y -> x + y : Int -> Int -> Int
|
|
(hk-test "infer: two-arg lambda" (hk-infer-type "\\x -> \\y -> x + y") "Int -> Int -> Int")
|
|
|
|
;; ─── Application ─────────────────────────────────────────────────────────────
|
|
(hk-t "not True" "Bool")
|
|
(hk-t "negate 1" "Int")
|
|
|
|
;; ─── If-then-else ─────────────────────────────────────────────────────────────
|
|
(hk-t "if True then 1 else 2" "Int")
|
|
(hk-t "if 1 == 2 then True else False" "Bool")
|
|
|
|
;; ─── Let bindings ─────────────────────────────────────────────────────────────
|
|
;; let x = 1 in x + 2
|
|
(hk-t "let x = 1 in x + 2" "Int")
|
|
|
|
;; let f x = x + 1 in f 5
|
|
(hk-t "let f x = x + 1 in f 5" "Int")
|
|
|
|
;; let-polymorphism: let id x = x in id 1
|
|
(hk-t "let id x = x in id 1" "Int")
|
|
|
|
;; ─── Tuples ───────────────────────────────────────────────────────────────────
|
|
(hk-t "(1, True)" "(Int, Bool)")
|
|
(hk-t "(1, 2, 3)" "(Int, Int, Int)")
|
|
|
|
;; ─── Lists ───────────────────────────────────────────────────────────────────
|
|
(hk-t "[1, 2, 3]" "[Int]")
|
|
(hk-t "[True, False]" "[Bool]")
|
|
|
|
;; ─── Polymorphic list functions ───────────────────────────────────────────────
|
|
(hk-t "length [1, 2, 3]" "Int")
|
|
(hk-t "null []" "Bool")
|
|
(hk-t "head [1, 2, 3]" "Int")
|
|
|
|
{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}
|