(defcomp ()
  (~docs/page :title "Evaluation Rules"
    (p (~tw :tokens "text-stone-500 text-sm italic mb-8")
      "Machine-readable SX semantics — a non-circular reference for how the language evaluates expressions.")

    (~docs/section :title "Why rules as data" :id "why"
      (p "The SX spec is self-hosting: SX defines SX. This is elegant for bootstrapping but circular for understanding — you need to know SX to read the spec that defines SX.")
      (p "The evaluation rules break this circularity. Each rule is a data structure describing one dispatch case in the CEK evaluator: name, pattern, semantics, effects, and examples. Tools and LLMs can query these rules without reading the evaluator source.")
      (~docs/code :src (str ";; Ask the MCP server to explain a form\nsx_explain name=\"let\"\n\nlet\n  Category: special-form\n  Pattern:  (let ((name val) ...) body ...)\n  Effects:  (none)\n\nCreate new scope. Evaluate each val sequentially,\nbind name. Last body form is in tail position.\n\nExamples:\n  (let ((x 1) (y 2)) (+ x y)) → 3")))

    (~docs/section :title "Rule categories" :id "categories"
      (table (~tw :tokens "min-w-full text-sm mb-6")
        (thead
          (tr
            (th (~tw :tokens "text-left pr-4 pb-2 font-semibold text-stone-700") "Category")
            (th (~tw :tokens "text-left pr-4 pb-2 font-semibold text-stone-700") "Count")
            (th (~tw :tokens "text-left pb-2 font-semibold text-stone-700") "What it covers")))
        (tbody (~tw :tokens "text-stone-600")
          (tr (td (~tw :tokens "pr-4 py-1 font-mono text-xs") "literal") (td (~tw :tokens "pr-4 py-1") "6") (td (~tw :tokens "py-1") "Numbers, strings, booleans, nil, keywords, dicts"))
          (tr (td (~tw :tokens "pr-4 py-1 font-mono text-xs") "lookup") (td (~tw :tokens "pr-4 py-1") "1") (td (~tw :tokens "py-1") "Symbol resolution: env → primitives → error"))
          (tr (td (~tw :tokens "pr-4 py-1 font-mono text-xs") "special-form") (td (~tw :tokens "pr-4 py-1") "13") (td (~tw :tokens "py-1") "if, when, cond, case, let, letrec, lambda, define, set!, begin, quote, quasiquote, ->"))
          (tr (td (~tw :tokens "pr-4 py-1 font-mono text-xs") "definition") (td (~tw :tokens "pr-4 py-1") "3") (td (~tw :tokens "py-1") "defcomp, defisland, defmacro"))
          (tr (td (~tw :tokens "pr-4 py-1 font-mono text-xs") "higher-order") (td (~tw :tokens "pr-4 py-1") "6") (td (~tw :tokens "py-1") "map, filter, reduce, some, every?, for-each"))
          (tr (td (~tw :tokens "pr-4 py-1 font-mono text-xs") "scope") (td (~tw :tokens "pr-4 py-1") "5") (td (~tw :tokens "py-1") "scope, provide, context, emit!, emitted"))
          (tr (td (~tw :tokens "pr-4 py-1 font-mono text-xs") "continuation") (td (~tw :tokens "pr-4 py-1") "2") (td (~tw :tokens "py-1") "reset, shift (delimited continuations)"))
          (tr (td (~tw :tokens "pr-4 py-1 font-mono text-xs") "reactive") (td (~tw :tokens "pr-4 py-1") "1") (td (~tw :tokens "py-1") "deref (signal reading)"))
          (tr (td (~tw :tokens "pr-4 py-1 font-mono text-xs") "call") (td (~tw :tokens "pr-4 py-1") "1") (td (~tw :tokens "py-1") "General function call dispatch")))))

    (~docs/section :title "The sx_explain tool" :id "tool"
      (p "Query rules by name or category:")
      (~docs/code :src (str ";; Explain a specific form\nsx_explain name=\"if\"\n\n;; List all forms in a category\nsx_explain name=\"higher-order\"\n\n;; The rules live in spec/eval-rules.sx as SX data"))
      (p "The rules file is loaded by the MCP server at startup. It's plain SX — you can extend it with new rules for custom forms."))

    (~docs/section :title "Rule structure" :id "structure"
      (~docs/code :src (str ";; Each rule is a dict with:\n{:name \"let\"               ;; form name\n :category \"special-form\"  ;; dispatch category\n :pattern \"(let ...)\"       ;; syntax pattern\n :rule \"description\"       ;; evaluation semantics\n :effects ()               ;; required capabilities\n :examples \"...\"}          ;; input → output"))
      (p "The " (code ":effects") " field connects rules to the capability system. A rule with " (code ":effects \"mutation\"") " (like " (code "set!") ") can only be evaluated in contexts that provide the " (code "mutation") " capability."))))
