Named freeze scopes for serializable reactive state
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled

Replace raw CEK state serialization with named freeze scopes.
A freeze scope collects signals registered within it. On freeze,
signal values are serialized to SX. On thaw, values are restored.

- freeze-scope: scoped effect delimiter for signal collection
- freeze-signal: register a signal with a name in the current scope
- cek-freeze-scope / cek-thaw-scope: freeze/thaw by scope name
- freeze-to-sx / thaw-from-sx: full SX text round-trip
- cek-freeze-all / cek-thaw-all: batch operations

Also: register boolean?, symbol?, keyword? predicates in both
Python and JS platforms with proper var aliases.

Demo: counter + name input with Freeze/Thaw buttons.
Frozen SX: {:name "demo" :signals {:count 5 :name "world"}}

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-14 22:42:21 +00:00
parent a759f4da3b
commit a390f5f304
4 changed files with 280 additions and 375 deletions

View File

@@ -542,79 +542,90 @@
(defisland ~geography/cek/freeze-demo ()
(let ((source (signal "(+ 1 (* 2 3))"))
(state-sig (signal nil))
(step-count (signal 0))
(frozen-sx (signal ""))
(thaw-result (signal "")))
(letrec
((do-parse (fn ()
(reset! frozen-sx "")
(reset! thaw-result "")
(reset! step-count 0)
(let ((parsed (sx-parse (deref source))))
(when (not (empty? parsed))
(reset! state-sig (make-cek-state (first parsed) (make-env) (list)))))))
(do-step (fn ()
(when (and (deref state-sig) (not (cek-terminal? (deref state-sig))))
(reset! state-sig (cek-step (deref state-sig)))
(swap! step-count inc))))
(do-freeze (fn ()
(when (deref state-sig)
(let ((frozen (cek-freeze (deref state-sig))))
(reset! frozen-sx (sx-serialize frozen))))))
(do-thaw (fn ()
(when (not (empty? (deref frozen-sx)))
(let ((parsed (sx-parse (deref frozen-sx))))
(when (not (empty? parsed))
(let ((thawed (cek-thaw (first parsed))))
(reset! thaw-result
(str "Resumed from step " (deref step-count) ": "
(cek-run thawed)))))))))
(do-run (fn ()
(when (deref state-sig)
(let run-loop ()
(when (not (cek-terminal? (deref state-sig)))
(do-step)
(run-loop)))))))
;; Auto-parse
(effect (fn () (do-parse)))
(div :class "space-y-4"
;; Input
(div :class "flex gap-2 items-end"
(div :class "flex-1"
(label :class "text-xs text-stone-400 block mb-1" "Expression")
(input :type "text" :bind source
:class "w-full px-3 py-1.5 rounded border border-stone-300 font-mono text-sm focus:outline-none focus:border-violet-400"
:on-change (fn (e) (do-parse))))
(let ((bg (signal "violet"))
(size (signal "text-2xl"))
(weight (signal "font-bold"))
(text (signal "the joy of sx"))
(frozen-text (signal ""))
(saved (signal (list))))
;; Register in freeze scope
(freeze-scope "widget" (fn ()
(freeze-signal "bg" bg)
(freeze-signal "size" size)
(freeze-signal "weight" weight)
(freeze-signal "text" text)))
(div :class "space-y-4"
;; Live preview
(div :class "p-6 rounded-lg text-center transition-all border"
:style (str "background:" (get {:violet "#f5f3ff" :rose "#fff1f2" :emerald "#ecfdf5" :amber "#fffbeb" :sky "#f0f9ff" :stone "#fafaf9"} (deref bg))
";border-color:" (get {:violet "#ddd6fe" :rose "#fecdd3" :emerald "#a7f3d0" :amber "#fde68a" :sky "#bae6fd" :stone "#e7e5e4"} (deref bg)))
(span :class (str (deref size) " " (deref weight))
:style (str "color:" (get {:violet "#6d28d9" :rose "#be123c" :emerald "#047857" :amber "#b45309" :sky "#0369a1" :stone "#44403c"} (deref bg)))
(deref text)))
;; Controls
(div :class "grid grid-cols-2 gap-3"
(div
(label :class "text-xs text-stone-400 block mb-1" "Colour")
(div :class "flex gap-1"
(button :on-click (fn (e) (do-step))
:class "px-3 py-1.5 rounded bg-violet-500 text-white text-sm hover:bg-violet-600"
"Step")
(button :on-click (fn (e) (do-run))
:class "px-3 py-1.5 rounded bg-violet-700 text-white text-sm hover:bg-violet-800"
"Run")))
;; Step count + freeze button
(div :class "flex items-center gap-3"
(span :class "text-sm text-stone-500 font-mono"
"Step " (deref step-count))
(when (deref state-sig)
(button :on-click (fn (e) (do-freeze))
:class "px-3 py-1.5 rounded bg-amber-500 text-white text-sm hover:bg-amber-600"
"Freeze")))
;; Frozen SX output
(when (not (empty? (deref frozen-sx)))
(div :class "space-y-2"
(label :class "text-xs text-stone-400 block" "Frozen CEK state")
(pre :class "text-xs font-mono bg-stone-50 rounded p-3 overflow-x-auto whitespace-pre-wrap text-stone-700"
(deref frozen-sx))
(button :on-click (fn (e) (do-thaw))
:class "px-3 py-1.5 rounded bg-emerald-600 text-white text-sm hover:bg-emerald-700"
"Thaw \u2192 Resume")))
;; Thaw result
(when (not (empty? (deref thaw-result)))
(div :class "rounded bg-emerald-50 border border-emerald-200 p-3 text-emerald-800 font-mono text-sm"
(deref thaw-result)))))))
(map (fn (pair)
(let ((c (first pair))
(hex (nth pair 1)))
(button :on-click (fn (e) (reset! bg c))
:class (str "w-8 h-8 rounded-full"
(if (= (deref bg) c) " ring-2 ring-offset-1 ring-stone-400" ""))
:style (str "background:" hex)
"")))
(list
(list "violet" "#a78bfa")
(list "rose" "#fb7185")
(list "emerald" "#34d399")
(list "amber" "#fbbf24")
(list "sky" "#38bdf8")
(list "stone" "#a8a29e")))))
(div
(label :class "text-xs text-stone-400 block mb-1" "Size")
(div :class "flex gap-1"
(map (fn (s)
(button :on-click (fn (e) (reset! size s))
:class (str "px-2 py-1 rounded text-xs "
(if (= (deref size) s)
"bg-stone-700 text-white" "bg-stone-100 text-stone-600"))
s))
(list "text-sm" "text-lg" "text-2xl" "text-4xl"))))
(div
(label :class "text-xs text-stone-400 block mb-1" "Weight")
(div :class "flex gap-1"
(map (fn (w)
(button :on-click (fn (e) (reset! weight w))
:class (str "px-2 py-1 rounded text-xs "
(if (= (deref weight) w)
"bg-stone-700 text-white" "bg-stone-100 text-stone-600"))
w))
(list "font-normal" "font-bold" "font-semibold"))))
(div
(label :class "text-xs text-stone-400 block mb-1" "Text")
(input :type "text" :bind text
:class "w-full px-2 py-1 rounded border border-stone-300 text-sm")))
;; Freeze / Thaw
(div :class "flex gap-2 items-center"
(button :on-click (fn (e)
(let ((sx (freeze-to-sx "widget")))
(reset! frozen-text sx)
(swap! saved (fn (l) (append l (list sx))))))
:class "px-3 py-1.5 rounded bg-amber-500 text-white text-sm hover:bg-amber-600"
"Save config")
(span :class "text-xs text-stone-400"
(str (len (deref saved)) " saved")))
;; Saved configs
(when (not (empty? (deref saved)))
(div :class "space-y-1"
(label :class "text-xs text-stone-400 block" "Saved configs")
(map-indexed (fn (i sx)
(button :on-click (fn (e) (thaw-from-sx sx))
:class "block w-full text-left px-2 py-1 rounded bg-stone-50 hover:bg-stone-100 font-mono text-xs text-stone-600 truncate"
(str "Config " (+ i 1))))
(deref saved))))
)))
;; ---------------------------------------------------------------------------