Patterns – a formal grammar that compiles natural language text into RL agents
The core idea: every sentence is a lossy projection of a high-dimensional cognitive state onto a 1-D token string. Patterns is the inverse map — a small formal grammar that parses natural language into expressions over eight typed terminals (the Jungian cognitive functions), then compiles those expressions into executable reinforcement-learning agents whose loss landscapes are meant to mirror the speaker’s internal dynamics.
Pipeline:
natural language → algebraic expression → math schedule → PyTorch agent
Example:
“I explore impulsively but feel held back by past regrets.”
→ 7Se oo 3Si -> Ni
→ adversarial schedule (entropy vs. centroid clustering, with drag into trajectory alignment)
→ AlgebraAgent with time-varying objective weights
The grammar is deliberately tiny: 8 terminals, 5 operators, 2 numeric attributes (mass = intensity, acceleration = frequency). But the operators compose:
• ~ orbit — judgment structures perception (sin/cos weight modulation)
• oo opposition — same-domain clash; winner drags to opposite domain
• → drag — exponential transfer between objectives
• | switching — cross-domain alternation
• + conjunction — linear sum
Type rules reject ill-formed states (e.g. Se ~ Si is illegal — same domain, can’t orbit). Every well-typed expression has a canonical mathematical image.
Three layers, each an LLM call constrained by explicit production rules:
-
Algebraic Analyst — NL → grammar string
-
Harmonic Composer — grammar → JSON schedule (objectives + dynamics)
-
Mechanic — schedule → runnable AlgebraAgent code
Each terminal maps to a concrete RL objective:
Se → maximize policy entropy
Si → cluster around centroid
Ne → seek novel states
Ni → follow imagined trajectory
Te → maximize value
Ti → maximize discrimination
Fe → balance entropy and value
Fi → temporal consistency
You can run it locally:
pip install -r requirements.txt
python -m patterns.app# Gradio UI, three panes
Or use the AI studio demo.
Why I think this is interesting beyond psychology cosplay:
-
It’s a compiler, not a classifier. Output is executable code with typed semantics, not a label.
-
Compositionality. Nested motivation/conflict/rationalization is just nested parentheses — same parser at every depth.
-
LLM introspection. Drop a chain-of-thought trace in, get a grammar expression out. Read the model’s cognitive state like a spectrogram reads a sound.
-
AGI criterion (speculative). If a model’s distribution over grammar expressions matches human reasoning traces under KL divergence, it’s manipulating the same functional basis — a completeness test independent of benchmarks.
What it’s NOT (being honest upfront):
• Not validated against clinical psychology or MBTI literature
• Layer 1–3 quality depends heavily on the LLM; smaller local models struggle with JSON in Layer 2
• The capo PPO base class is referenced but out-of-tree — you get the agent skeleton, not a full training loop
• “Jungian functions as RL objectives” will sound wild to some; the claim is structural (typed grammar → typed objectives), not that Jung was right about cognition
I’d love feedback on:
— Whether the type system is actually doing work vs. being LLM theater
— Alternative terminal sets (Big Five? plain P/J × S/N?)
— Making Layer 2 deterministic (rule-based JSON emission instead of LLM)
Repo: https://github.com/iblameandrew/patterns
README has the full BNF, worked examples, and the four-dimensional functional space formalism.
Happy to answer questions.
submitted by /u/causality-ai
[link] [comments]