Stage1 analyzer parity (#11)

* compiler: self-hosted analyzer compiles set literals (#{…})

Stage 1 Task 1. analyzer.clj punted set literals to the interpreter
((form-set? form) (uncompilable "set literal")); now it builds the set-node IR
(already defined in ir.clj) from (form-set-items form), and backend.janet emits
(make-phs e1 e2 …) — each element evaluated then the persistent set built,
mirroring compiler.janet's emit-set-expr and the interpreter's :jolt/set path.

Closes a self-hosted-analyzer vs bootstrap-compiler parity gap: #{…} no longer
forces interpreter fallback on the compile path.

Gate: conformance 262x3 (+4 set-literal cases incl computed elements / empty /
in-let), fixpoint, self-host, sci, suite 3981/66, specs+unit green; core-bench
neutral (A/B). set?/disj-as-fns remain deliberately interpreted (in-sync across
all three lists) — adjudicated in Task 2.

* test: fallback-zero harness — assert non-stateful forms compile (not interpret)

Stage 1 Task 3. self-host-test checks results but not which path ran. This runs
the portable analyzer (backend/analyze-form) on a corpus of non-stateful forms
and asserts NONE raise :jolt/uncompilable — i.e. the self-hosted analyzer
compiled them, not the interpreter fallback. Inverse sanity list confirms a few
intentional-interpret forms (ns/defmacro/require/set?/letfn) still punt, so the
harness can't pass by compiling everything.

29 must-compile (incl set literals from Task 1) + 5 must-punt, 0 failures. As
Stage 1 parity grows, forms move from the punt list into must-compile; when the
fallback set equals the frozen intentional stateful set, the bootstrap is
retireable.

* core: migrate 7 lazy seq fns from the Janet seed to the Clojure overlay (40-lazy)

Finishes a port the prior team started and reverted (bb4a3e0): the 40-lazy.clj
tier moved lazy seq fns Janet→Clojure but regressed the suite to 849 because
lazy-seq's expansion leaked as data in compile mode — that was jolt-r81, since
root-fixed (lazy-seq/lazy-cat moved to 00-syntax). With the wall gone, the port
works. This shrinks the Janet seed toward the north star (self-hosted
clojure-in-clojure on a minimal host bootstrap).

Moved to core/40-lazy.clj (wired as a loaded tier after 30-macros):
  distinct keep keep-indexed map-indexed cycle repeat iterate
40-lazy.clj completed to full parity: distinct gains its transducer arity;
keep/keep-indexed/map-indexed already had both arities.

Removed from the Janet seed (core.janet): the 7 core-* fns + their core-bindings
entries, the now-dead td-keep/td-map-indexed transducer helpers (the CLJ versions
carry their own), and the already-dead core-partition-by/core-xml-seq (shadowed by
10-seq/20-coll). Net: core.janet −131 lines.

Deferred (kept in Janet, separate follow-ups): partition-all (a CLJ port via
take/drop realizes a non-minimal element count, tripping the §6.3 laziness
counters + a suite file) and repeatedly (canonical CLJ doesn't validate args, so
the repeatedly.cljc throw cases regress). Both need behavior-matching first.

Gate: conformance 262x3, lazy-infinite 44/44, clojure-test-suite 4004/66 (UP from
3981 — the CLJ versions add coverage, e.g. distinct value-equality), fixpoint,
self-host, sci 422/0, fallback-zero, specs+unit, core-bench all green.

* test: raise clojure-test-suite baseline 3981 -> 4004 (lazy-fn migration coverage)

* core: migrate partition-all to the Clojure overlay (minimal realization)

Resolves the deferred partition-all port (jolt-yo3). The earlier CLJ attempt via
lazy take/drop over-realized vs the Janet pstep, tripping the §6.3 laziness
counter. The collection arities now realize EXACTLY n per chunk with a first/rest
loop and continue from the advanced cursor (no re-drop), so (take 3 (partition-all
2 (map counting (range)))) realizes exactly 6 — matching minimal realization in
both interpret and compile modes. Keeps transducer + [n coll] + [n step coll]
arities. letfn-bound recursion sidesteps the compile-mode multi-arity closure bug
(jolt-zxw), like keep-indexed/map-indexed.

Removed from the Janet seed: core-partition-all + its binding + the now-dead
td-partition-all helper (the CLJ version carries its own transducer arity).

Gate: conformance 262x3, lazy-infinite 44/44 (incl the §6.3 partition-all
counter), clojure-test-suite 4004/66, fixpoint, self-host, specs+unit green.

* core: migrate repeatedly to Clojure + fix char-not-callable / take count validation

Resolves the deferred repeatedly port (jolt-8qx). The blockers were two jolt
leniencies vs Clojure, now fixed (and correct beyond repeatedly):

- A char (a :jolt/type-tagged struct) fell into the struct-as-map branch of both
  jolt-call (compile path) and the interpreter's apply dispatch, so (\a) returned
  nil instead of throwing. Now only an UNtagged struct (a map literal) — or a
  record — is callable as a key lookup; tagged structs fall through to "Cannot
  call … as a function". Symbols are still handled (keyword-style get).
- core-take didn't validate its count, letting Janet's >= silently compare an int
  to a char/string. It now rejects a non-number n like Clojure.

With those, the canonical CLJ repeatedly matches: (first (repeatedly non-fn)) and
(repeatedly non-number f) throw. Moved repeatedly to core/40-lazy.clj; removed
core-repeatedly + its binding from the seed.

These correctness fixes help broadly: repeatedly.cljc goes clean (19/10 -> 29/0),
and the suite rises 4004 -> 4034 pass / 66 -> 67 clean. Baseline raised.

Gate: conformance 262x3, lazy-infinite 44/44, clojure-test-suite 4034/67,
fixpoint, self-host, sci, fallback-zero, specs+unit green.

* test: document the 4004 -> 4034 baseline raise (partition-all/repeatedly + char/take fixes)

* docs: partition-all letfn is for minimal realization, not jolt-zxw

jolt-zxw (multi-arity arity-param mis-capture in a nested lazy-seq under :compile?)
is no longer reproducible: an arity-direct partition-all now compiles correctly in
the overlay. The original failure was an artifact of the CLJ multi-arity version
coexisting with the Janet core-partition-all ([n & rest]) during migration — the
shadowing confused multi-arity dispatch; removing the Janet version resolved it.
The letfn in partition-all stays purely for minimal realization (jolt-yo3).

* compiler: compile set?/disj as plain fns (close the last Stage-1 fallback gap)

Stage 1 jolt-g3h. set? and disj were special-cased in all three "can't compile"
lists (host_iface special-names, compiler.janet uncompilable-heads, evaluator
special-symbol? + handlers) — but they're pure value-production with callable
core vars (core-set?/core-disj), and those vars are byte-for-byte equivalent to
the evaluator handlers. Removed them from all three lists + dropped the now-dead
evaluator handler arms, so they're ordinary clojure.core fns everywhere: the
analyzer compiles (set? x)/(disj s x) as normal var calls instead of punting to
the interpreter.

Verified identical results in default AND JOLT_MUTABLE builds (no representation
sensitivity — sets are phs in both, unlike vector?/list? which collapse).

With this, the self-hosted analyzer's compile-path fallback set equals the frozen
intentional stateful set (Task 2) — it's now a strict superset of the bootstrap
compiler's compilable surface, so the Janet bootstrap is retireable (Stage 2).

fallback-zero: set?/disj moved to must-compile (31 now), set! into must-punt.
Gate: conformance 267x3 (+5 set?/disj cases), lazy-infinite 44/44, suite 4034/67,
fixpoint, self-host, sci, specs+unit green.
This commit is contained in:
Dmitri Sotnikov 2026-06-09 12:24:37 +08:00 committed by GitHub
parent fdf2c742c4
commit 1ded89b47b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 179 additions and 200 deletions

View file

@ -53,7 +53,8 @@
{:ns "clojure.core.00-kernel" :kernel true}
{:ns "clojure.core.10-seq" :kernel false}
{:ns "clojure.core.20-coll" :kernel false}
{:ns "clojure.core.30-macros" :kernel false}])
{:ns "clojure.core.30-macros" :kernel false}
{:ns "clojure.core.40-lazy" :kernel false}])
(defn- eval-overlay-source [ctx src]
(var s src)

View file

@ -230,6 +230,12 @@
(array/push args (emit ctx (in p 1))))
(tuple/slice args))
# A set literal: build (make-phs e1 e2 …) so each element is evaluated at runtime
# then the persistent set is constructed — mirrors compiler.janet's emit-set-expr.
(defn- emit-set [ctx node]
(def items (map |(emit ctx $) (vview (node :items))))
(tuple/slice (array/concat @[phm/make-phs] items)))
(set emit
(fn emit [ctx raw]
(def node (norm-node raw))
@ -259,6 +265,7 @@
:invoke (emit-invoke ctx node)
:vector (emit-vector ctx node)
:map (emit-map ctx node)
:set (emit-set ctx node)
:quote ['quote (node :form)]
(error (string "backend: unhandled op " (node :op))))))

View file

@ -163,7 +163,6 @@
"find-ns" "all-ns" "the-ns" "find-var" "intern" "resolve"
"ns-resolve" "ns-aliases" "ns-imports" "ns-interns"
"alter-var-root" "alter-meta!" "reset-meta!" "locking" "new"
"disj" "set?"
# Definitional/host macros that mutate context or build runtime
# values the emitter doesn't model.
"defrecord" "defprotocol" "definterface" "reify" "proxy"

View file

@ -508,7 +508,10 @@
(if (and (number? k) (= k (math/floor k)) (>= k 0) (< k (length f)))
(in f k)
(error (string "Index " k " out of bounds for vector of length " (length f)))))
(or (struct? f) (and (table? f) (get f :jolt/deftype)))
# Map literal (struct with no :jolt/type marker) or a record: callable as a
# key lookup. A TAGGED struct (char/etc.) is NOT a fn — symbols are handled
# above; everything else with a :jolt/type falls through to the error.
(or (and (struct? f) (nil? (get f :jolt/type))) (and (table? f) (get f :jolt/deftype)))
(let [v (get f (get args 0) :jolt/not-found)]
(if (= v :jolt/not-found) (get args 1) v))
(error (string "Cannot call " (type f) " as a function"))))
@ -804,9 +807,7 @@
(fn [rf] (fn [& a] (case (length a) 0 (rf) 1 (rf (a 0))
(if (truthy? (pred (a 1))) (rf (a 0) (a 1)) (a 0))))))
(defn td-remove [pred] (td-filter (fn [x] (not (pred x)))))
(defn td-keep [f]
(fn [rf] (fn [& a] (case (length a) 0 (rf) 1 (rf (a 0))
(let [v (f (a 1))] (if (nil? v) (a 0) (rf (a 0) v)))))))
# td-keep removed: keep (incl its transducer arity) lives in core/40-lazy.clj.
(defn td-take [n]
(fn [rf]
(var left n)
@ -829,31 +830,11 @@
(fn [& a] (case (length a) 0 (rf) 1 (rf (a 0))
(do (when (and dropping (not (truthy? (pred (a 1))))) (set dropping false))
(if dropping (a 0) (rf (a 0) (a 1))))))))
(defn td-map-indexed [f]
(fn [rf]
(var i -1)
(fn [& a] (case (length a) 0 (rf) 1 (rf (a 0)) (do (++ i) (rf (a 0) (f i (a 1))))))))
# td-map-indexed removed: map-indexed (incl transducer arity) lives in core/40-lazy.clj.
# Stateful windowing transducers. The 1-arg (completion) arity flushes a partial
# trailing window before delegating to rf's completion; matches Clojure.
(defn td-partition-all [n]
(fn [rf]
(var buf @[])
(fn [& a]
(case (length a)
0 (rf)
1 (let [result (if (= 0 (length buf)) (a 0)
(let [v (tuple/slice (tuple ;buf))]
(set buf @[])
(core-unreduced (rf (a 0) v))))]
(rf result))
(do
(array/push buf (a 1))
(if (= n (length buf))
(let [v (tuple/slice (tuple ;buf))]
(set buf @[])
(rf (a 0) v))
(a 0)))))))
# td-partition-all removed: partition-all (incl transducer arity) lives in core/40-lazy.clj.
# partition-by's transducer arity lives with its (lazy) collection arity in the
# overlay (10-seq tier), written in Clojure with volatiles.
@ -1113,6 +1094,9 @@
(error "Wrong number of args passed to: reduce"))))
(defn core-take [n & rest]
# n is a count — reject non-numbers (e.g. a char/string) like Clojure, rather
# than letting Janet's >= silently compare mixed types.
(unless (number? n) (error (string "take: n must be a number, got " (type n))))
(if (= 0 (length rest)) (td-take n)
(let [coll (in rest 0)]
# Option A: lazy take (returns a seq, not a vector, even over a vector).
@ -1342,22 +1326,7 @@
(sort-by keyfn arr))
(tuple/slice (tuple ;arr))))))
(defn core-distinct [coll]
# Option A: always lazy. seen-set is captured once and shared across the chain.
(let [seen @{}]
(defn dstep [c]
(fn []
(var cur c) (var found false) (var result nil)
(while (and (not found) (not (seq-done? cur)))
(let [x (core-first cur)]
(set cur (core-rest cur))
(when (nil? (seen x))
(put seen x true)
(set found true)
(set result x))))
(if found @[result (dstep cur)] nil)))
(make-lazy-seq (dstep (lazy-from coll)))))
# distinct now lives in the Clojure lazy tier (core/40-lazy.clj).
# group-by / frequencies now live in the Clojure collection tier
# (core/20-coll.clj).
@ -1384,72 +1353,13 @@
nil)))))
(make-lazy-seq (pstep (lazy-from coll)))))
(defn core-partition-by [f coll]
(def f (as-fn f))
(var result @[])
(var part @[])
(var last-k nil)
(each x (realize-for-iteration coll)
(let [k (f x)]
(if (and last-k (deep= k last-k))
(array/push part x)
(do
(if (> (length part) 0) (array/push result (tuple/slice (tuple ;part))))
(set part @[x])
(set last-k k)))))
(if (> (length part) 0) (array/push result (tuple/slice (tuple ;part))))
result)
# partition-by now lives in the Clojure seq tier (core/10-seq.clj).
(defn core-partition-all [n & rest]
(if (= 0 (length rest)) (td-partition-all n)
(let [coll (in rest 0)]
# Option A: always lazy.
(defn pstep [c]
(fn []
(if (seq-done? c) nil
(do
(var part @[]) (var cur c) (var i 0)
(while (and (< i n) (not (seq-done? cur)))
(array/push part (core-first cur))
(set cur (core-rest cur))
(++ i))
@[(tuple/slice (tuple ;part)) (pstep cur)]))))
(make-lazy-seq (pstep (lazy-from coll))))))
# partition-all now lives in the Clojure lazy tier (core/40-lazy.clj).
(defn core-keep-indexed [f coll]
(def f (as-fn f))
# Option A: always lazy.
(defn kstep [c i]
(fn []
(var cur c) (var idx i) (var found false) (var result nil)
(while (and (not found) (not (seq-done? cur)))
(let [v (f idx (core-first cur))]
(++ idx)
(set cur (core-rest cur))
(when (not (nil? v))
(set found true)
(set result v))))
(if found @[result (kstep cur idx)] nil)))
(make-lazy-seq (kstep (lazy-from coll) 0)))
(defn core-map-indexed [f & rest]
(if (= 0 (length rest)) (td-map-indexed f)
(let [coll (in rest 0)]
# Option A: always lazy.
(defn mstep [c i]
(fn []
(if (seq-done? c) nil
@[(f i (core-first c)) (mstep (core-rest c) (+ i 1))])))
(make-lazy-seq (mstep (lazy-from coll) 0)))))
(defn core-cycle [coll]
(let [c (realize-for-iteration coll)]
(if (= 0 (length c))
(make-lazy-seq (fn [] nil))
(do
(defn cstep [i] (fn [] @[(in c (% i (length c))) (cstep (+ i 1))]))
(make-lazy-seq (cstep 0))))))
# keep-indexed / map-indexed / cycle now live in the Clojure lazy tier
# (core/40-lazy.clj).
# reduce-kv now lives in the Clojure collection tier (core/20-coll.clj).
@ -1495,30 +1405,9 @@
(+= i step))
(tuple/slice (tuple ;result))))))
(defn core-repeat
"(repeat x) -> infinite lazy seq of x; (repeat n x) -> n copies of x."
[a & rest]
(if (= 0 (length rest))
(do (defn rstep [] (fn [] @[a (rstep)])) (make-lazy-seq (rstep)))
(let [n a x (in rest 0)]
(var result @[]) (var i 0)
(while (< i n) (array/push result x) (++ i))
result)))
# repeat / iterate now live in the Clojure lazy tier (core/40-lazy.clj).
(defn core-iterate [f x]
"Lazy infinite sequence x, (f x), (f (f x)), ..."
(defn istep [v] (fn [] @[v (istep (f v))]))
(make-lazy-seq (istep x)))
(defn core-repeatedly
"(repeatedly f) -> infinite lazy seq of (f) calls; (repeatedly n f) -> n calls."
[a & rest]
(if (= 0 (length rest))
(do (defn rstep [] (fn [] @[(a) (rstep)])) (make-lazy-seq (rstep)))
(let [n a f (in rest 0)]
(var result @[]) (var i 0)
(while (< i n) (array/push result (f)) (++ i))
result)))
# repeatedly now lives in the Clojure lazy tier (core/40-lazy.clj).
# ============================================================
# Higher-order functions
@ -2408,26 +2297,7 @@
@[(core-first c) (istep (core-rest c) true)]))))
(make-lazy-seq (istep (lazy-from coll) false)))))
(defn core-keep
"(keep f coll) — (f x) for each x, dropping nils. (keep f) is a transducer."
[f & rest]
(def f (as-fn f))
(if (= 0 (length rest))
(td-keep f)
(let [coll (in rest 0)]
# Option A: always lazy.
(defn kstep [c]
(fn []
(var cur c) (var found false) (var result nil)
(while (and (not found) (not (seq-done? cur)))
(let [v (f (core-first cur))]
(set cur (core-rest cur))
(when (not (nil? v))
(set found true)
(set result v))))
(if found @[result (kstep cur)] nil)))
(make-lazy-seq (kstep (lazy-from coll))))))
# keep now lives in the Clojure lazy tier (core/40-lazy.clj).
(defn core-empty [coll]
(cond
@ -2517,14 +2387,7 @@
# Iterator/enumeration seqs — Jolt has no Java iterators, so adapt to plain seq.
(defn core-enumeration-seq [x] (core-seq x))
(defn core-iterator-seq [x] (core-seq x))
(defn core-xml-seq [root]
(def out @[])
(defn walk [n]
(array/push out n)
(when (and (core-map? n) (core-contains? n :content))
(each c (realize-for-iteration (core-get n :content)) (walk c))))
(walk root)
(tuple ;out))
# xml-seq now lives in the Clojure collection tier (core/20-coll.clj).
(defn core-line-seq [rdr]
(if (string? rdr) (core-seq (string/split "\n" rdr)) nil))
(defn core-re-matcher [re s] @{:jolt/type :jolt/matcher :re re :s s :pos 0})
@ -2866,10 +2729,6 @@
"get-in" core-get-in
"contains?" core-contains?
"count" core-count
"partition-all" core-partition-all
"keep-indexed" core-keep-indexed
"map-indexed" core-map-indexed
"cycle" core-cycle
"pop" core-pop
"trampoline" core-trampoline
"format" core-format
@ -2952,7 +2811,6 @@
"random-uuid" core-random-uuid
"interpose" core-interpose
"mapcat" core-mapcat
"keep" core-keep
"find" core-find
"transduce" core-transduce
"sequence" core-sequence
@ -2990,13 +2848,8 @@
"nth" core-nth
"sort" core-sort
"sort-by" core-sort-by
"distinct" core-distinct
"partition" core-partition
"partition-by" core-partition-by
"range" core-range
"repeat" core-repeat
"iterate" core-iterate
"repeatedly" core-repeatedly
"identity" core-identity
"constantly" core-constantly
"complement" core-complement
@ -3103,7 +2956,6 @@
"test" core-test
"enumeration-seq" core-enumeration-seq
"iterator-seq" core-iterator-seq
"xml-seq" core-xml-seq
"line-seq" core-line-seq
"re-matcher" core-re-matcher
"bean" core-bean

View file

@ -26,7 +26,6 @@
(= name "var-get") (= name "var-set") (= name "var?")
(= name "alter-var-root") (= name "find-var") (= name "intern")
(= name "alter-meta!") (= name "reset-meta!")
(= name "disj") (= name "set?")
(= name "satisfies?")
(= name "protocol-dispatch") (= name "register-method") (= name "make-reified")
(= name "prefer-method") (= name "remove-method") (= name "remove-all-methods")
@ -116,7 +115,9 @@
(if (and (number? k) (= k (math/floor k)) (>= k 0) (< k (length f)))
(in f k)
(error (string "Index " k " out of bounds for vector of length " (length f)))))
(struct? f)
# Map literal only (struct with no :jolt/type). A tagged struct (char/etc.)
# is not callable — symbols are handled above; chars fall through to the error.
(and (struct? f) (nil? (get f :jolt/type)))
(let [v (get f (get args 0) :jolt/not-found)]
(if (= v :jolt/not-found) (get args 1) v))
(and (table? f) (get f :jolt/deftype))
@ -1140,12 +1141,8 @@
val (eval-form ctx bindings (in form 3))
ns (ctx-find-ns ctx (if (struct? ns-name) (ns-name :name) ns-name))]
(ns-intern ns (if (struct? sym-name) (sym-name :name) sym-name) val))
"disj" (let [s (eval-form ctx bindings (in form 1))
ks (map |(eval-form ctx bindings $) (tuple/slice form 2))]
(if (set? s)
(apply phs-disj s ks)
(error "disj expects a set")))
"set?" (set? (eval-form ctx bindings (in form 1)))
# set?/disj are plain clojure.core fns now (core-set?/core-disj) — no longer
# special-cased here, the analyzer, or compiler.janet (jolt-g3h).
"protocol-dispatch" (let [proto-sym (in form 1)
method-sym (in form 2)
obj (eval-form ctx bindings (in form 3))

View file

@ -74,7 +74,7 @@
"defmacro" "fn*" "let*" "loop*" "recur" "throw" "try" "set!" "var"
"locking" "eval" "instance?" "defmulti" "defmethod" "deftype" "new"
"." "var-get" "var-set" "var?" "alter-var-root" "find-var" "intern"
"alter-meta!" "reset-meta!" "disj" "set?" "satisfies?"
"alter-meta!" "reset-meta!" "satisfies?"
"protocol-dispatch" "register-method" "make-reified" "prefer-method"
"remove-method" "remove-all-methods" "get-method" "methods"
# ns-management forms dispatched by the interpreter (not core vars)