haskell: dict-passing elaborator — runtime dispatch via hk-mk-lazy-builtin (+3 tests, 506/506)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s

This commit is contained in:
2026-05-06 08:56:39 +00:00
parent 41a69ecca7
commit 60a8eb24e0
2 changed files with 97 additions and 7 deletions

View File

@@ -702,10 +702,42 @@ negate x = 0 - x
((or (= (first d) "bind") (= (first d) "pat-bind"))
(append! pat-binds d))
((= (first d) "class-decl")
(dict-set!
env
(str "__class__" (nth d 1))
(list "class" (nth d 1) (nth d 2))))
(let
((cls (nth d 1))
(tvar (nth d 2))
(method-decls (nth d 3)))
(dict-set! env (str "__class__" cls) (list "class" cls tvar))
(for-each
(fn
(m)
(when
(= (first m) "type-sig")
(for-each
(fn
(mname)
(dict-set!
env
mname
(hk-mk-lazy-builtin
mname
(fn
(x)
(let
((tv (hk-force x)))
(let
((key (str "dict" cls "_" (hk-runtime-type tv))))
(if
(has-key? env key)
(hk-apply (get (get env key) mname) x)
(raise
(str
"No instance "
cls
" for "
(hk-runtime-type tv)))))))
1)))
(nth m 1))))
method-decls)))
((= (first d) "instance-decl")
(let
((cls (nth d 1))
@@ -713,8 +745,7 @@ negate x = 0 - x
(method-decls (nth d 3)))
(let
((inst-dict (dict))
(inst-key
(str "dict" cls "_" (hk-type-ast-str inst-type))))
(type-str (hk-type-ast-str inst-type)))
(for-each
(fn
(m)
@@ -732,7 +763,11 @@ negate x = 0 - x
(hk-eval body env)
(hk-eval (list "lambda" pats body) env))))))
method-decls)
(dict-set! env inst-key inst-dict))))
(dict-set! env (str "dict" cls "_" type-str) inst-dict)
(dict-set!
env
(str "dict" cls "_" (hk-type-to-runtime-key type-str))
inst-dict))))
(:else nil)))
decls)
(let
@@ -829,6 +864,36 @@ negate x = 0 - x
(hk-type-ast-str (nth ast 2))))
(:else "?"))))
(define
hk-runtime-type
(fn
(val)
(let
((t (type-of val)))
(cond
((= t "number") "number")
((= t "boolean") "boolean")
((= t "string") "string")
((and (= t "list") (not (empty? val)))
(let
((tag (str (first val))))
(cond
((or (= tag "True") (= tag "False")) "Bool")
(:else tag))))
(:else t)))))
(define
hk-type-to-runtime-key
(fn
(ts)
(cond
((= ts "Int") "number")
((= ts "Float") "number")
((= ts "Bool") "Bool")
((= ts "String") "string")
((= ts "Char") "string")
(:else ts))))
(define
hk-typecheck
(fn