(defcomp
  ()
  (~docs/page
    :title "Freeze / Thaw"
    (p
      (~tw :tokens "text-stone-500 text-sm italic mb-8")
      "A computation is a value. Freeze it to an s-expression. "
      "Store it, transmit it, content-address it. Thaw and resume anywhere.")
    (~docs/section
      :title "The idea"
      :id "idea"
      (p
        "The CEK machine makes evaluation explicit: every step is a pure function from state to state. "
        "The state is a dict with four fields:")
      (ul
        (~tw :tokens "list-disc pl-6 mb-4 space-y-1 text-stone-600")
        (li (code "control") " — the expression being evaluated")
        (li (code "env") " — the bindings in scope")
        (li
          (code "kont")
          " — the continuation (what to do with the result)")
        (li (code "phase") " — eval or continue"))
      (p
        "Since the state is data, it can be serialized. "
        (code "cek-freeze")
        " converts a live CEK state to pure s-expressions. "
        (code "cek-thaw")
        " reconstructs a live state from frozen SX. "
        (code "cek-run")
        " resumes from where it left off."))
    (~docs/section
      :title "Freeze"
      :id "freeze"
      (p "Take a computation mid-flight and freeze it:")
      (~docs/code
        :src (highlight
          "(let ((expr (sx-parse \"(+ 1 (* 2 3))\"))\n      (state (make-cek-state (first expr) (make-env) (list))))\n  ;; Step 4 times\n  (set! state (cek-step (cek-step (cek-step (cek-step state)))))\n  ;; Freeze to SX\n  (cek-freeze state))"
          "lisp"))
      (p "The frozen state is pure SX:")
      (~docs/code
        :src (highlight
          "{:phase \"continue\"\n :control nil\n :value 1\n :env {}\n :kont ({:type \"arg\"\n         :f (primitive \"+\")\n         :evaled ()\n         :remaining ((* 2 3))\n         :env {}})}"
          "lisp"))
      (p
        "Everything is data. The continuation frame says: “I was adding 1 to something, "
        "and I still need to evaluate "
        (code "(* 2 3)")
        ".”"))
    (~docs/section
      :title "Thaw and resume"
      :id "thaw"
      (p "Parse the frozen SX back. Thaw it. Resume:")
      (~docs/code
        :src (highlight
          "(let ((frozen (sx-parse frozen-text))\n      (state (cek-thaw (first frozen))))\n  (cek-run state))\n;; => 7"
          "lisp"))
      (p
        "Native functions like "
        (code "+")
        " serialize as "
        (code "(primitive \"+\")")
        " and are looked up in the primitive registry on thaw. "
        "Lambdas serialize as their source AST — "
        (code "(lambda (x) (* x 2))")
        " — and reconstruct as callable functions."))
    (~docs/section
      :title "Live demo"
      :id "demo"
      (p
        "Type an expression, step to any point, freeze the state. "
        "The frozen SX appears below. Click Thaw to resume from the frozen state.")
      (~geography/cek/freeze-demo))
    (~docs/section
      :title "Content addressing"
      :id "content-addressing"
      (p
        "Hash the frozen SX "
        (code "→")
        " content identifier. "
        "Same state always produces the same CID. Store by CID, retrieve by CID, verify by CID.")
      (~docs/code
        :src (highlight
          "(freeze-to-cid \"widget\")\n;; => \"d9eea67b\"\n\n(thaw-from-cid \"d9eea67b\")\n;; Signals restored. Same CID = same state."
          "lisp"))
      (p
        "Try it: change the values, click Content-address. Copy the CID. "
        "Change the values again. Paste the CID and Restore.")
      (~geography/cek/content-address-demo))
    (~docs/section
      :title "What this enables"
      :id "enables"
      (ul
        (~tw :tokens "list-disc pl-6 mb-4 space-y-2 text-stone-600")
        (li
          (strong "Persistence")
          " — save reactive island state to localStorage, "
          "resume on page reload")
        (li
          (strong "Migration")
          " — freeze a computation on one machine, "
          "thaw on another. Same result, deterministically.")
        (li
          (strong "Content addressing")
          " — hash the frozen SX → CID. "
          "A pointer to a computation in progress, not just a value.")
        (li
          (strong "Time travel")
          " — freeze at each step, store the history. "
          "Jump to any point. Undo. Branch.")
        (li
          (strong "Verification")
          " — re-run from a frozen state, "
          "check the result matches. Reproducible computation."))
      (p
        "The Platonic argument made concrete: a computation IS a value. "
        "The Form persists. The instance resumes."))))
