jolt/test/integration/predicate-fold-test.janet
Dmitri Sotnikov f0293fb4ee
Parallelize the test gate; cache cold-init tests, drop the benchmark from it (#139)
run-tests.janet runs the same file set as `jpm test` across a pool of worker
processes (one `janet FILE` each, ev-based). The full gate goes from ~790s
serial to ~98s here (8x), and more on CI where the heavy files don't thrash on
swap. CI and the docs point at it; `jpm test` still works serially.

Three things dominated the wall:

- Nine integration tests cold-built a compile ctx (~8s each); switch them to
  api/init-cached so they share the prebuilt image. The cache key already
  fingerprints the ctx-shaping env vars, so the direct-link ones share one DL
  image and the rest share the plain one.
- core-bench's main ran on every gate (~35s of benchmark loops that assert
  nothing); gate it behind JOLT_BENCH=1.
- cli-test spawned `janet src/jolt/main.janet` ~20 times at ~8s cold each
  (340s under parallel load, and it was the whole wall); prefer build/jolt
  (~20ms baked ctx) when present, fall back to from-source for an unbuilt tree.

type-check-test stays on cold init: a snapshot-loaded ctx loses the success
checker's op/msg detail (jolt-vley). jolt-pria tracks caching from-source
startup generally, which would let cli-test drop the build/jolt preference.

Co-authored-by: Yogthos <yogthos@gmail.com>
2026-06-16 14:23:02 +00:00

61 lines
3.5 KiB
Text

# Predicate folding from inference (jolt-wcw): when the collection-type
# inference PROVES the argument's type, a type predicate (number?/string?/
# keyword?/nil?/some?/record?) folds to a compile-time boolean constant, which
# the trailing const-fold then propagates — collapsing any `if` it gates to the
# taken branch. Sound: only a provable answer folds, and only when the argument
# is side-effect-free (a local or const), so dropping its evaluation is a no-op.
# Mirrors type-infer-test.janet's harness (count a marker in the emitted IR to
# prove the optimization fired, then evaluate to prove it stayed correct).
(import ../../src/jolt/api :as api)
(import ../../src/jolt/backend :as backend)
(import ../../src/jolt/reader :as reader)
(print "Predicate folding (jolt-wcw)...")
(os/setenv "JOLT_DIRECT_LINK" "1")
(def ctx (api/init-cached {:compile? true}))
(api/eval-string ctx "(ns pf)")
(defn code [src]
(string/format "%p" (backend/emit-ir ctx (backend/analyze-form ctx (reader/parse-string src)))))
(defn occurs [src needle] (length (string/find-all needle (code src))))
(defn ev [src] (api/eval-string ctx src))
# --- the predicate call is gone where the type is proven --------------------
(assert (= 0 (occurs "(fn [] (let [x (+ 1 2)] (number? x)))" "number?"))
"number? on proven :num -> folded, call eliminated")
(assert (= 0 (occurs "(fn [] (let [x \"hi\"] (string? x)))" "string?"))
"string? on proven :str -> folded")
(assert (= 0 (occurs "(fn [] (let [x :k] (keyword? x)))" "keyword?"))
"keyword? on proven :kw -> folded")
(assert (= 0 (occurs "(fn [] (let [x (+ 1 2)] (nil? x)))" "nil?"))
"nil? on a provably non-nil value -> folded")
# --- the folded constant collapses an if it gates --------------------------
# the dead branch's literal (200) must be gone after dead-branch removal
(assert (= 0 (occurs "(fn [] (let [x (+ 1 2)] (if (number? x) 100 200)))" "200"))
"true predicate folds the if to its then-branch (dead 200 dropped)")
(assert (= 0 (occurs "(fn [] (let [x (+ 1 2)] (if (string? x) 100 200)))" "100"))
"false predicate folds the if to its else-branch (dead 100 dropped)")
# --- sound fallback: unknown type or impure arg keeps the call -------------
# a param is :any (Phase 0 doesn't type it) -> no fold
(assert (= 1 (occurs "(fn [m] (number? m))" "number?"))
"unknown-type arg keeps the predicate call")
# arg type is proven :num but the arg has side effects (a call) -> must NOT
# drop its evaluation, so the predicate is left in place
(assert (>= (occurs "(fn [g] (number? (+ (g) 1)))" "number?") 1)
"impure arg (even with proven type) keeps the predicate call")
# --- correctness: folded path evaluates to the dispatched path -------------
(assert (= true (ev "((fn [] (let [x (+ 1 2)] (number? x))))")) "number? true value")
(assert (= false (ev "((fn [] (let [x \"hi\"] (number? x))))")) "number? false value")
(assert (= true (ev "((fn [] (let [x :k] (keyword? x))))")) "keyword? true value")
(assert (= false (ev "((fn [] (let [x 5] (nil? x))))")) "nil? false value")
(assert (= true (ev "((fn [] (let [x 5] (some? x))))")) "some? true value")
(assert (= :yes (ev "((fn [] (let [x 5] (if (number? x) :yes :no))))")) "gated if takes proven branch")
(assert (= :no (ev "((fn [] (let [x 5] (if (string? x) :yes :no))))")) "gated if drops false branch")
# impure arg still runs its side effect and returns the right answer
(assert (= 6 (ev "((fn [g] (if (number? (+ (g) 1)) (+ (g) 5) 0)) (fn [] 1))")) "impure-arg predicate stays correct")
(print "Predicate folding (jolt-wcw) passed!")