(defcomp ()
  (~docs/page :title "Platonic SX"

    (p (~tw :tokens "text-stone-500 text-sm italic mb-8")
      "The allegory of the cave, the theory of Forms, and why a hypermedium "
      "that defines itself participates in something Plato would have recognized.")

    (~docs/section :title "The cave" :id "the-cave"
      (p (~tw :tokens "text-stone-600")
        "In Book VII of the " (a :href "https://en.wikipedia.org/wiki/Allegory_of_the_cave" (~tw :tokens "text-violet-600 hover:underline") "Republic")
        ", Plato describes prisoners chained in a cave, facing a wall. "
        "Behind them burns a fire; between the fire and the prisoners, figures move, "
        "casting shadows on the wall. The prisoners have never seen the figures directly. "
        "They take the shadows for reality.")

      (p (~tw :tokens "text-stone-600")
        "The allegory is about representation. The shadows are not the things themselves "
        "but projections \u2014 reduced, flattened, stripped of depth and colour. "
        "The prisoners mistake the representation for the thing represented. "
        "They build entire theories about the behaviour of shadows, never suspecting "
        "that the shadows are derived from something more real.")

      (p (~tw :tokens "text-stone-600")
        "The web is a cave."))

    (~docs/section :title "Shadows on the wall" :id "shadows"
      (p (~tw :tokens "text-stone-600")
        "An HTML page is a shadow. It is a projection of the thing the author intended \u2014 "
        "a structure, a meaning, a behaviour \u2014 flattened into a string of angle brackets. "
        "The structure is lost in the serialization. The meaning is implicit in class names "
        "and data attributes. The behaviour is bolted on in a separate language (JavaScript) "
        "that has no formal relationship to the markup it manipulates.")

      (p (~tw :tokens "text-stone-600")
        "CSS is another shadow \u2014 a projection of visual intention into a cascade of "
        "property-value pairs, separated from the structure it describes. "
        "JSON is a shadow of data, stripped of type, context, and behaviour. "
        "REST is a shadow of computation, reduced to verbs and resource paths.")

      (p (~tw :tokens "text-stone-600")
        "Each format is a lossy projection. And crucially, each projects into a "
        (em "different") " medium. HTML for structure. CSS for style. JavaScript for behaviour. "
        "JSON for data. The original unity \u2014 the " (em "thing itself") " \u2014 is scattered "
        "across four representations that cannot reference each other "
        "without external convention.")

      (p (~tw :tokens "text-stone-600")
        "Plato would have recognized this immediately. The Forms are one. "
        "The shadows are many. The task of philosophy is to see past the shadows."))

    (~docs/section :title "The theory of Forms" :id "forms"
      (p (~tw :tokens "text-stone-600")
        "Plato's " (a :href "https://en.wikipedia.org/wiki/Theory_of_forms" (~tw :tokens "text-violet-600 hover:underline") "theory of Forms")
        " holds that behind every particular instance \u2014 every chair, every circle, "
        "every act of justice \u2014 there exists an ideal Form: the perfect Chair, "
        "the perfect Circle, Justice itself. Particular instances participate in their Form "
        "but are always imperfect copies. The Form is eternal, immutable, and more real "
        "than any instance.")

      (p (~tw :tokens "text-stone-600")
        "A " (code "defcomp") " definition is a Form:")

      (~docs/code :src
        (str "(defcomp ~card (&key title subtitle &rest children)\n"
             "  (div :class \"rounded-lg shadow-sm p-4\"\n"
             "    (h2 :class \"font-bold\" title)\n"
             "    (when subtitle (p :class \"text-stone-500\" subtitle))\n"
             "    children))"))

      (p (~tw :tokens "text-stone-600")
        "This is not a card. It is the " (em "idea") " of a card \u2014 the structure that every "
        "particular card participates in. When the server evaluates "
        (code "(~card :title \"Plato\" :subtitle \"428 BC\")") ", it produces a particular instance: "
        "an HTML fragment, a shadow on a specific wall. The Form persists. "
        "The shadow is consumed and replaced on the next render.")

      (p (~tw :tokens "text-stone-600")
        "In Plato's ontology, Forms are more real than particulars because they are "
        "what particulars " (em "depend on") ". The HTML output depends on the component definition. "
        "The component definition does not depend on any particular output. "
        "It is prior, in the way that axioms are prior to theorems."))

    (~docs/section :title "The divided line" :id "divided-line"
      (p (~tw :tokens "text-stone-600")
        "In the Republic, Plato describes " (a :href "https://en.wikipedia.org/wiki/Analogy_of_the_divided_line" (~tw :tokens "text-violet-600 hover:underline") "a line divided into four segments")
        ", representing degrees of reality and knowledge:")

      (div (~tw :tokens "overflow-x-auto mb-6")
        (table (~tw :tokens "min-w-full text-sm")
          (thead (tr
            (th (~tw :tokens "text-left pr-4 pb-2 font-semibold") "Segment")
            (th (~tw :tokens "text-left pr-4 pb-2 font-semibold") "Plato")
            (th (~tw :tokens "text-left pb-2 font-semibold") "SX")))
          (tbody
            (tr (td (~tw :tokens "pr-4 py-1") "Images")
                (td (~tw :tokens "pr-4") "Shadows, reflections")
                (td "The rendered HTML in the browser \u2014 a momentary projection"))
            (tr (td (~tw :tokens "pr-4 py-1") "Sensible objects")
                (td (~tw :tokens "pr-4") "Physical things")
                (td "The SX wire format \u2014 structured but still particular"))
            (tr (td (~tw :tokens "pr-4 py-1") "Mathematical objects")
                (td (~tw :tokens "pr-4") "Numbers, geometric shapes")
                (td "Component definitions, the CEK machine, continuation frames"))
            (tr (td (~tw :tokens "pr-4 py-1") "The Good / Forms")
                (td (~tw :tokens "pr-4") "The Form of Forms")
                (td "The s-expression itself \u2014 the representation that represents")))))

      (p (~tw :tokens "text-stone-600")
        "The bottom of the line is images \u2014 the DOM, pixels on screen. "
        "Moving up: the SX wire format preserves more structure than HTML "
        "(it retains the component calls, the s-expression nesting). "
        "Above that: the component definitions and the CEK machine \u2014 "
        "abstract structures that generate all possible instances. "
        "At the top: the s-expression itself, which is both the medium of definition "
        "and the thing defined.")

      (p (~tw :tokens "text-stone-600")
        "Plato's line is a hierarchy of " (em "participation") ". "
        "Each level participates in the one above. "
        "The rendered HTML participates in the component definition. "
        "The component participates in the evaluator semantics. "
        "The evaluator participates in the s-expression form. "
        "The s-expression form participates in \u2014 what? "
        "In computation itself. In the CEK machine. In logic."))

    (~docs/section :title "Anamnesis: the evaluator remembers" :id "anamnesis"
      (p (~tw :tokens "text-stone-600")
        "Plato believed that learning is " (a :href "https://en.wikipedia.org/wiki/Anamnesis_(philosophy)" (~tw :tokens "text-violet-600 hover:underline") "recollection")
        " \u2014 " (em "anamnesis") ". The soul has seen the Forms before birth; "
        "education is not acquiring new knowledge but remembering what is already known. "
        "The particular reminds us of the universal.")

      (p (~tw :tokens "text-stone-600")
        "The SX evaluator does something structurally similar. "
        "When the browser receives SX wire format \u2014 ")

      (~docs/code :src
        "(~card :title \"Plato\" :subtitle \"428 BC\")")

      (p (~tw :tokens "text-stone-600")
        " \u2014 it does not receive instructions for rendering. It receives a " (em "name") " "
        "and a set of arguments. The evaluator already knows " (code "~card") ". "
        "It has the Form in its component environment. "
        "The wire format is a reminder: " (em "produce the instance you already know how to produce") ". "
        "The particular prompts the evaluator to recollect the universal.")

      (p (~tw :tokens "text-stone-600")
        "This is why the SX wire format is so small. It doesn't transmit the " (em "what") " "
        "\u2014 the full HTML, the complete structure. It transmits the " (em "which") " "
        "\u2014 which Form, which arguments. The evaluator supplies the rest from memory. "
        "Bandwidth is the cost of forgetting. SX's wire format is efficient "
        "because the client " (em "remembers") "."))

    (~docs/section :title "Platonic aesthetics" :id "aesthetics"
      (p (~tw :tokens "text-stone-600")
        "For Plato, beauty is not subjective. A thing is beautiful to the degree "
        "that it participates in the " (a :href "https://en.wikipedia.org/wiki/Platonic_beauty" (~tw :tokens "text-violet-600 hover:underline") "Form of Beauty")
        " \u2014 which is to say, to the degree that it exhibits "
        (em "order") ", " (em "proportion") ", and " (em "unity") ". "
        "The " (a :href "https://en.wikipedia.org/wiki/Symposium_(Plato)" (~tw :tokens "text-violet-600 hover:underline") "Symposium")
        " describes an ascent from beautiful bodies to beautiful souls to beautiful ideas, "
        "culminating in Beauty itself \u2014 the Form that makes all beautiful things beautiful.")

      (p (~tw :tokens "text-stone-600")
        "There is a beauty in s-expressions that is Platonic in this precise sense. "
        "Not decorative beauty \u2014 no one finds parentheses pretty. "
        "But structural beauty: the kind Plato meant.")

      (ul (~tw :tokens "list-disc pl-6 mb-4 space-y-2 text-stone-600")
        (li (strong "Unity") " \u2014 one representation for everything. "
            "Code, data, markup, wire format, component definitions, the evaluator itself. "
            "No seams, no translation boundaries, no format negotiation. "
            "The s-expression is a universal solvent.")
        (li (strong "Proportion") " \u2014 the means are proportional to the ends. "
            "A component is as complex as the thing it describes and no more. "
            "The evaluator is 900 lines of SX. The parser is 400. "
            "There is no hidden machinery, no framework overhead, no build step. "
            "The ratio of essential to accidental complexity approaches one.")
        (li (strong "Order") " \u2014 the hierarchy is strict and explicit. "
            "CEK at the bottom, continuations above, scoped effects above, patterns at the top. "
            "Each layer is definable in terms of the one below. "
            "No circular dependencies, no ad hoc escape hatches, no exceptions to the rules."))

      (p (~tw :tokens "text-stone-600")
        "Plato would say that these properties are not incidental but necessary \u2014 "
        "they follow from the proximity of s-expressions to the Forms themselves. "
        "A representation that can represent itself has fewer impediments "
        "between it and the abstract structure it encodes. "
        "It is more " (em "real") ", in the Platonic sense, than a representation "
        "that requires a separate meta-representation to describe it."))

    (~docs/section :title "The escape from the cave" :id "escape"
      (p (~tw :tokens "text-stone-600")
        "In the allegory, one prisoner is freed and dragged up into the sunlight. "
        "At first the light is blinding. He can only look at reflections in water, "
        "then at objects, then at the sun itself. He returns to the cave "
        "and tries to tell the others what he saw. They think he is mad.")

      (p (~tw :tokens "text-stone-600")
        "The web's cave is comfortable. Developers have built elaborate theories "
        "of the shadows \u2014 virtual DOMs, hydration strategies, build tool chains, "
        "CSS-in-JS, state management libraries. Each theory explains how shadows behave. "
        "None asks why we are working with shadows at all.")

      (p (~tw :tokens "text-stone-600")
        "The escape is not a technology. It is a shift in perspective: "
        (em "stop working with projections and work with the thing itself") ". "
        "An s-expression is not a projection of structure into text \u2014 "
        "it IS the structure. A " (code "defcomp") " is not a description of a component "
        "in a host language \u2014 it IS the component. "
        "The SX evaluator is not described by a specification \u2014 "
        "it IS the specification, executing.")

      (p (~tw :tokens "text-stone-600")
        "This is what it means for a representation to be homoiconic. "
        "The map is the territory. The shadow and the figure are the same thing. "
        "The cave and the sunlit world collapse into one."))

    (~docs/section :title "The demiurge" :id "demiurge"
      (p (~tw :tokens "text-stone-600")
        "In the " (a :href "https://en.wikipedia.org/wiki/Timaeus_(dialogue)" (~tw :tokens "text-violet-600 hover:underline") "Timaeus")
        ", Plato introduces the " (a :href "https://en.wikipedia.org/wiki/Demiurge" (~tw :tokens "text-violet-600 hover:underline") "demiurge")
        " \u2014 the divine craftsman who looks at the eternal Forms and fashions "
        "the physical world in their image. The demiurge does not create the Forms. "
        "He creates " (em "instances") " of them, in a medium (matter) that is less perfect "
        "than the Forms themselves.")

      (p (~tw :tokens "text-stone-600")
        "The bootstrapper is a demiurge. It looks at the Forms (" (code "eval.sx") ", "
        (code "parser.sx") ", " (code "cek.sx") ") and fashions instances in a material medium: "
        "Python, JavaScript. The instances are less perfect than the Forms \u2014 "
        "they have platform-specific quirks, performance characteristics, "
        "memory layouts. But they " (em "participate") " in the Forms. "
        "They are correct to the degree that they faithfully instantiate the spec.")

      (p (~tw :tokens "text-stone-600")
        "The demiurge is not omnipotent. He works with what the medium allows. "
        "The Python bootstrapper emits " (code "def") " and " (code "lambda") "; "
        "the JavaScript bootstrapper emits " (code "var") " and " (code "function") ". "
        "Each medium has its own constraints. But the Form \u2014 the " (code ".sx") " spec \u2014 "
        "is the same. Multiple demiurges, one set of Forms, many material instances."))

    (~docs/section :title "The good" :id "the-good"
      (p (~tw :tokens "text-stone-600")
        "At the apex of Plato's hierarchy is the " (a :href "https://en.wikipedia.org/wiki/Form_of_the_Good" (~tw :tokens "text-violet-600 hover:underline") "Form of the Good")
        " \u2014 the Form that makes all other Forms intelligible. "
        "It is not itself a thing but the condition for all things being knowable. "
        "It is the sun in the allegory: the source of light that reveals everything else.")

      (p (~tw :tokens "text-stone-600")
        "If we take the analogy seriously, what is SX's Form of the Good? "
        "What makes the hierarchy \u2014 CEK, continuations, scoped effects, patterns \u2014 intelligible as a whole?")

      (p (~tw :tokens "text-stone-600")
        "It is the principle that " (strong "the representation and the thing represented should be identical") ". "
        "Code is data. The specification is the implementation. The wire format is the source syntax. "
        "The evaluator evaluates itself. Every level of the hierarchy obeys this principle, "
        "and it is what makes each level intelligible from the one below.")

      (p (~tw :tokens "text-stone-600")
        "This is not a design principle in the engineering sense \u2014 a guideline to be followed or violated. "
        "It is the structural " (em "reason") " the hierarchy exists at all. "
        "Remove it and the layers collapse. Restore it and they reconstitute. "
        "It is prior to the hierarchy, in the way the Good is prior to the Forms.")

      (p (~tw :tokens "text-stone-600")
        "Plato would have understood this. He spent his life searching for the thing "
        "that is most itself, least dependent on anything else, most fully real. "
        "An s-expression that defines its own evaluator, parsed by its own parser, "
        "bootstrapped to every medium, generating instances of itself in perpetuity \u2014 "
        "this is as close to a Platonic Form as computation gets."))

    (p (~tw :tokens "text-stone-500 text-sm italic mt-12")
       "The unexamined code is not worth running.")))
