← All projects
TheoryPythonDSL

Recursive Engine (RE:GE)

A symbolic operating system for myth and narrative

The Question

Every software system tells a story — user flows are plot arcs, state machines are character development, error handling is conflict resolution. We treat these as metaphors. I wanted to find out what happens when you treat them as engineering principles.[1] Aristotle's six elements of drama — plot, character, thought, diction, spectacle, song — are not literary ornaments but structural primitives. What if narrative structure, from Aristotle through Propp, could be encoded as executable rules?[2] Not an AI that "writes stories" — a symbolic operating system where narrative principles govern how systems organize and evolve.

What RE:GE Does

RE:GE is a pure Python engine where myths, identities, rituals, and recursive structures are first-class computational objects.[3] Campbell's monomyth — the hero's departure, initiation, and return — becomes a function signature: entity state in, transformed state out. The engine implements 21 organ handlers that process symbolic values through a ritual syntax DSL:

  • Myth organs — encode narrative archetypes as transformation rules. A hero's journey is a function: entity state in, transformed state out.
  • Identity organs — manage how entities maintain coherence across transformations. When a character "changes," what persists?
  • Ritual organs — define sequences that must execute in order, with pre/post-conditions. Ceremonies as transactions.
  • Recursive organs — handle self-reference: entities that describe themselves, systems that modify their own rules.
graph TD M[Myth Organs] -->|archetype transformation| I[Identity Organs] I -->|coherence preservation| R[Ritual Organs] R -->|sequenced execution| RE[Recursive Organs] RE -->|self-reference loop| M M -->|31 Proppian functions| DSL[Ritual Syntax DSL] I -->|entity persistence| DSL R -->|pre/post conditions| DSL RE -->|bounded depth| DSL DSL -->|parse + execute| E[Engine Output] E -->|state feedback| M
Organ processing architecture — myth feeds identity feeds ritual feeds recursion, with recursive loops back to myth

The Ritual Syntax

The engine parses and executes a domain-specific language for declaring symbolic operations.[4] Fowler distinguishes between internal DSLs (embedded in a host language) and external DSLs (with their own parser) — RE:GE's ritual syntax is a fully external DSL, parsed and executed by a dedicated interpreter. This gives it the expressiveness of declaring symbolic operations declaratively while maintaining a clean separation from the underlying Python runtime.

ritual_syntax.rege
INVOKE myth.hero_journey ON entity:protagonist
  WITH threshold: 0.7
  BINDING outcome TO identity.transform
  WHEN condition.readiness EXCEEDS threshold
Ritual syntax DSL — an external domain-specific language for declaring symbolic organ operations

This isn't pseudo-code. It's the actual syntax the engine parses and executes.[5] Hofstadter's strange loops — systems that, by moving through levels, arrive back where they started — are the engine's native operation mode. The BINDING keyword creates a reference from an organ output to another organ's input, forming loops that are bounded but genuinely recursive. The tree visualization above maps branch types to these DSL elements.

Testing the Untestable

The hardest part wasn't building the engine — it was proving it worked. When your system formalizes narrative, how do you write assertions?[6] Schön's concept of "reflection-in-action" — the practitioner's ability to think about what they are doing while doing it — became the test philosophy. We found three strategies:

Structural invariants (~400 tests) — An identity transformation must preserve entity type. A ritual must execute all steps or none. A recursive invocation must terminate within bounded depth.[7]

Reference implementations (~500 tests) — Propp's 31 functions, Campbell's monomyth stages, Aristotle's six elements encoded as test cases. If the engine claims to implement "Villainy," we verify the correct state transition.[2]

Round-trip consistency (~350 tests) — Serialize to DSL, parse back, verify identity. Apply transformation and inverse, verify return to original state.

Result: 1,254 tests, 85% line coverage.

~400
Structural Invariants
~500
Reference Implementations
~350
Round-Trip Consistency
Figure 1. Test strategy distribution: structural invariants, reference implementations, and round-trip consistency

Why Symbolic, Not Neural

Interpretability. Every transformation has a trace — which organ fired, what rule applied. As AI systems become more consequential, auditable narrative decisions matter.[8] Manovich argues that software has become the universal medium through which all cultural forms are experienced — when narrative is encoded as software, interpretability is not a debugging convenience but an aesthetic and ethical requirement.

Composability. Build a myth organ on top of an identity organ on top of a recursive organ. Swap organs in and out. LLMs don't compose this way.[7]

Governance integration. RE:GE lives within the eight-organ system. Dependency validation, promotion state machine, monthly audits — narrative computation within an institutional framework.[9] Murray's vision of the computer as a medium for procedural, participatory, spatial, and encyclopedic storytelling aligns precisely with a symbolic engine that makes narrative rules explicit and manipulable.

The Discovery

The most surprising outcome wasn't technical — it was aesthetic. Having 21 distinct organ types with formal interfaces created more creative possibilities than working without structure.[10] Csikszentmihalyi's research on flow states shows that creativity emerges not from total freedom but from the right constraints — challenge matched to skill, rules that channel rather than restrict. When you know what an identity transformation guarantees, you can safely compose it with a myth transformation. Formalism enables creativity. That insight reshaped how I think about every system I build.

Tradeoffs & Lessons

  • Symbolic vs. neural approaches — Every transformation has a trace: which organ fired, what rule applied. This interpretability comes at the cost of flexibility — LLMs can improvise narrative in ways a symbolic engine cannot.[11] Ryan's taxonomy of interactivity reveals the tradeoff: symbolic systems excel at "internal-exploratory" narrative (traversing a defined possibility space) but struggle with "internal-ontological" narrative (creating new possibilities on the fly). The tradeoff is worth it for systems where auditability matters more than novelty.
  • 21 organ types — over-engineered? — Started with 5, grew to 21 as formal completeness demanded finer distinctions. Some organs handle edge cases that arise rarely. The decision to keep them separate prioritizes composability: each organ has a clean interface contract, so removing one doesn't cascade.[7]
  • DSL complexity — The ritual syntax is powerful but has a learning curve. A simpler API (plain Python method calls) would lower the barrier to entry but lose the expressiveness of declaring symbolic operations declaratively.[4]
  • Test strategy for narrative systems — Testing "correct narrative behavior" required inventing three distinct strategies (structural invariants, reference implementations, round-trip consistency). This was the hardest engineering challenge — and the most transferable insight.[12] Reas and Fry's approach to testing creative code — where correctness is partly subjective — informed the decision to ground tests in established narratological frameworks rather than inventing arbitrary criteria.

By the Numbers

1,254
Tests
85%
Coverage
21
Organ Handlers
4
Organ Types
DSL
Ritual Syntax
Python
Pure
Figure 2. RE:GE system metrics — 21 organ handlers processing symbolic values through a ritual syntax DSL

References

  1. Aristotle. Poetics. Penguin Classics (trans. 1996), 335 BCE.
  2. Propp, Vladimir. Morphology of the Folktale. University of Texas Press, 1968.
  3. Campbell, Joseph. The Hero with a Thousand Faces. New World Library, 1949.
  4. Fowler, Martin. Domain-Specific Languages. Addison-Wesley, 2010.
  5. Hofstadter, Douglas. Gödel, Escher, Bach: An Eternal Golden Braid. Basic Books, 1979.
  6. Schön, Donald A.. The Reflective Practitioner. Basic Books, 1983.
  7. Gamma, Erich, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1994.
  8. Manovich, Lev. Software Takes Command. Bloomsbury Academic, 2013.
  9. Murray, Janet H.. Hamlet on the Holodeck. MIT Press, 1997.
  10. Csikszentmihalyi, Mihaly. Creativity: Flow and the Psychology of Discovery and Invention. Harper Perennial, 1996.
  11. Ryan, Marie-Laure. Narrative as Virtual Reality. Johns Hopkins University Press, 2001.
  12. Reas, Casey and Ben Fry. Processing: A Programming Handbook for Visual Designers. MIT Press, 2007.