jolt/host/chez/natives-seq.ss
Yogthos 739b219d0e Chez Phase 1 (increment 3n): seq-native shims + reduced
The dominant prelude-parity crash bucket was 'apply non-procedure jolt-nil':
core fns calling seed-native seq fns (core_coll.janet) that have no Chez RT
shim, so var-deref returns jolt-nil. A static scan of the assembled prelude
turned up 52 referenced-but-undefined clojure.core names.

host/chez/natives-seq.ss shims the safe seq fns over the existing seq layer:
mapcat, take-while, drop-while, partition (collection arities only — the 1-arg
transducer forms are jolt-kxsr), and sort (compare default; a comparator may
return a 3-way number or a boolean less-than). reduced/reduced? is a jolt-reduced
record in seq.ss that reduce short-circuits on and deref unwraps, so unreduced
works. identical? = jolt= (the seed's definition).

Deferred list?: a Chez lazy seq and a list are both cseq, so it can't be told
apart without a distinct list type — a real divergence risk.

Parity 1407 -> 1467/2497, 0 new divergences. emit-test 263/263.
2026-06-17 23:16:26 -04:00

99 lines
5 KiB
Scheme

;; seq-native shims (jolt-y6mv) — seed-native seq fns the overlay assumes are
;; clojure.core natives but which live in the Janet seed (src/jolt/core_coll.janet),
;; so they have no def-var! in the assembled prelude and resolve to jolt-nil on
;; Chez. This was the dominant prelude-parity crash bucket ('apply jolt-nil').
;; Each is a pure fn over the existing seq layer (seq.ss) — collection arities
;; only; the 1-arg transducer arities are jolt-kxsr. Loaded last (after
;; converters.ss for jolt-compare and seq.ss for the reduced record).
;; reduced / reduced? — the box itself is the jolt-reduced record from seq.ss
;; (so the reduce machinery there can see it); these just expose the constructor
;; and predicate. (deref a-reduced) is handled in atoms.ss.
(define (jolt-reduced-new x) (make-jolt-reduced x))
(define (jolt-reduced-pred x) (jolt-reduced? x))
;; mapcat: (mapcat f coll & colls) — map f across the colls (stops at shortest),
;; then concat the results. Collection arity only.
(define (jolt-mapcat f . colls)
(apply jolt-concat (seq->list (apply jolt-map f colls))))
;; take-while / drop-while over the seq layer.
(define (take-while-seq pred s)
(if (jolt-nil? s) jolt-empty-list
(let ((x (seq-first s)))
(if (jolt-truthy? (jolt-invoke pred x))
(cseq-lazy x (lambda () (take-while-seq pred (jolt-seq (seq-more s)))))
jolt-empty-list))))
(define (jolt-take-while pred coll) (take-while-seq pred (jolt-seq coll)))
(define (jolt-drop-while pred coll)
(let loop ((s (jolt-seq coll)))
(if (and (not (jolt-nil? s)) (jolt-truthy? (jolt-invoke pred (seq-first s))))
(loop (jolt-seq (seq-more s)))
(if (jolt-nil? s) jolt-empty-list s))))
;; partition: (partition n coll), (partition n step coll), or
;; (partition n step pad coll). Only complete partitions of size n are kept;
;; with pad, a short final partition is padded from pad (and may be < n if pad
;; runs out). Each partition is a seq; the whole result is a lazy seq of seqs.
(define jolt-partition
(case-lambda
((n coll) (partition* (->idx n) (->idx n) #f #f coll))
((n step coll) (partition* (->idx n) (->idx step) #f #f coll))
((n step pad coll) (partition* (->idx n) (->idx step) #t pad coll))))
(define (take-n n s) ; -> (values list-of-first-n remaining-seq taken-count)
(let loop ((n n) (s s) (acc '()))
(if (or (fx<=? n 0) (jolt-nil? s))
(values (reverse acc) s (length acc))
(loop (fx- n 1) (jolt-seq (seq-more s)) (cons (seq-first s) acc)))))
(define (partition* n step has-pad pad coll)
(let loop ((s (jolt-seq coll)))
(if (jolt-nil? s) jolt-empty-list
(let-values (((part rest taken) (take-n n s)))
(cond
;; full partition: emit it, advance `step` from its START
((fx=? taken n)
(cseq-lazy (list->cseq part)
(lambda () (loop (jolt-seq (advance-by step s))))))
;; short final partition with pad: top up to n from pad, then stop
((and has-pad (fx>? taken 0))
(let ((padded (append part (take-list (- n taken) (jolt-seq pad)))))
(cseq-lazy (list->cseq padded) (lambda () jolt-empty-list))))
;; short final partition, no pad: dropped (Clojure keeps only full ones)
(else jolt-empty-list))))))
(define (advance-by step s) ; drop `step` elements from s (seq), returns a seq
(let loop ((step step) (s s))
(if (or (fx<=? step 0) (jolt-nil? s)) s
(loop (fx- step 1) (jolt-seq (seq-more s))))))
(define (take-list n s) ; up to n elements of seq s as a Scheme list
(let loop ((n n) (s s) (acc '()))
(if (or (fx<=? n 0) (jolt-nil? s)) (reverse acc)
(loop (fx- n 1) (jolt-seq (seq-more s)) (cons (seq-first s) acc)))))
;; sort: (sort coll) uses compare; (sort cmp coll) uses cmp, whose result may be
;; a 3-way number (<0 / 0 / >0) OR a boolean (a Clojure-style less-than pred).
(define (cmp->less cmp)
(lambda (a b)
(let ((r (jolt-invoke cmp a b)))
(if (number? r) (< r 0) (jolt-truthy? r)))))
(define jolt-sort
(case-lambda
((coll) (jolt-sort* (cmp->less jolt-compare) coll))
((cmp coll) (jolt-sort* (cmp->less cmp) coll))))
(define (jolt-sort* less? coll)
(let ((s (jolt-seq coll)))
(if (jolt-nil? s) jolt-empty-list
(list->cseq (list-sort less? (seq->list s))))))
;; identical?: jolt reference identity. The seed defines it as (= a b) over its
;; value model (core_types.janet core-identical?), where interned keywords/small
;; values compare equal — so jolt= is the faithful port.
(define (jolt-identical? a b) (jolt= a b))
(def-var! "clojure.core" "reduced" jolt-reduced-new)
(def-var! "clojure.core" "reduced?" jolt-reduced-pred)
(def-var! "clojure.core" "mapcat" jolt-mapcat)
(def-var! "clojure.core" "take-while" jolt-take-while)
(def-var! "clojure.core" "drop-while" jolt-drop-while)
(def-var! "clojure.core" "partition" jolt-partition)
(def-var! "clojure.core" "sort" jolt-sort)
(def-var! "clojure.core" "identical?" jolt-identical?)