haskell: Phase 14 — record creation Foo { f = e, … } (parser + desugar)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m0s

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-07 16:11:23 +00:00
parent b89e321007
commit 9307437679
4 changed files with 137 additions and 40 deletions

View File

@@ -12,12 +12,7 @@
(define
hk-register-con!
(fn
(cname arity type-name)
(dict-set!
hk-constructors
cname
{:arity arity :type type-name})))
(fn (cname arity type-name) (dict-set! hk-constructors cname {:arity arity :type type-name})))
(define hk-is-con? (fn (name) (has-key? hk-constructors name)))
@@ -48,26 +43,15 @@
(fn
(data-node)
(let
((type-name (nth data-node 1))
(cons-list (nth data-node 3)))
((type-name (nth data-node 1)) (cons-list (nth data-node 3)))
(for-each
(fn
(cd)
(hk-register-con!
(nth cd 1)
(len (nth cd 2))
type-name))
(fn (cd) (hk-register-con! (nth cd 1) (len (nth cd 2)) type-name))
cons-list))))
;; (:newtype NAME TVARS CNAME FIELD)
(define
hk-register-newtype!
(fn
(nt-node)
(hk-register-con!
(nth nt-node 3)
1
(nth nt-node 1))))
(fn (nt-node) (hk-register-con! (nth nt-node 3) 1 (nth nt-node 1))))
;; Walk a decls list, registering every `data` / `newtype` decl.
(define
@@ -78,15 +62,9 @@
(fn
(d)
(cond
((and
(list? d)
(not (empty? d))
(= (first d) "data"))
((and (list? d) (not (empty? d)) (= (first d) "data"))
(hk-register-data! d))
((and
(list? d)
(not (empty? d))
(= (first d) "newtype"))
((and (list? d) (not (empty? d)) (= (first d) "newtype"))
(hk-register-newtype! d))
(:else nil)))
decls)))
@@ -99,16 +77,12 @@
((nil? ast) nil)
((not (list? ast)) nil)
((empty? ast) nil)
((= (first ast) "program")
(hk-register-decls! (nth ast 1)))
((= (first ast) "module")
(hk-register-decls! (nth ast 4)))
((= (first ast) "program") (hk-register-decls! (nth ast 1)))
((= (first ast) "module") (hk-register-decls! (nth ast 4)))
(:else nil))))
;; Convenience: source → AST → desugar → register.
(define
hk-load-source!
(fn (src) (hk-register-program! (hk-core src))))
(define hk-load-source! (fn (src) (hk-register-program! (hk-core src))))
;; ── Built-in constructors pre-registered ─────────────────────
;; Bool — used implicitly by `if`, comparison operators.
@@ -122,9 +96,9 @@
;; Standard Prelude types — pre-registered so expression-level
;; programs can use them without a `data` decl.
(hk-register-con! "Nothing" 0 "Maybe")
(hk-register-con! "Just" 1 "Maybe")
(hk-register-con! "Left" 1 "Either")
(hk-register-con! "Right" 1 "Either")
(hk-register-con! "Just" 1 "Maybe")
(hk-register-con! "Left" 1 "Either")
(hk-register-con! "Right" 1 "Either")
(hk-register-con! "LT" 0 "Ordering")
(hk-register-con! "EQ" 0 "Ordering")
(hk-register-con! "GT" 0 "Ordering")