Category theory-based prompt engineering with mathematical rigor and practical power
A production-ready Claude Code plugin implementing categorical structures (functors, monads, comonads, natural transformations) for rigorous, composable prompt engineering with quality visualization and error handling.
✨ 6 Categorical Structures
- F (Functor): Task → Prompt transformation
- M (Monad): Iterative refinement with quality convergence
- W (Comonad): Context extraction and meta-observation
- α (Natural Transformation): Strategy switching between functors
- E (Exception Monad): Error handling with monad semantics
- [0,1]-Enriched: Quality tracking and visualization
✨ 4 Composition Operators
→Sequential (Kleisli composition)||Parallel (concurrent execution)⊗Tensor (quality-degrading combination)>=>Kleisli refinement (monadic with quality gates)
✨ Advanced Features
- Quality flow visualization (Phase 5)
- Error handling and recovery (Phase 3)
- Multi-dimensional quality assessment
- Categorical law verification
- Progressive disclosure (Layer 1-3)
git clone https://github.com/categorical-meta-prompting/plugin.git
cd plugin
./INSTALL.shRestart Claude Code to activate the plugin.
# Simple meta-prompting
/meta "implement rate limiter"
# Quality-gated iteration
/rmp @quality:0.85 "optimize algorithm"
# Command composition
/chain [/meta→/review] "build feature"| Structure | Symbol | Command | Description |
|---|---|---|---|
| Functor | F | /meta |
Task → Prompt (structure-preserving) |
| Monad | M | /rmp |
Prompt →^n Prompt (iterative refinement) |
| Comonad | W | /context |
History → Context (extraction) |
| Nat. Trans. | α | /transform |
F ⇒ G (strategy switching) |
| Exception | E | @catch:, @fallback: |
Error handling |
| Enriched | [0,1] | @quality:, @quality:visualize |
Quality tracking |
- meta-self: Master reference for unified categorical syntax. Contains all modifiers, operators, composition patterns, and execution protocols.
| Command | Description | Key Syntax |
|---|---|---|
/meta |
Categorical meta-prompting | @mode:, @tier:, @template: |
/rmp |
Recursive meta-prompting loop | @quality:, @max_iterations: |
/chain |
Command composition | [/cmd1→/cmd2→/cmd3] |
/context |
Comonad operations | @mode:, @focus:, @depth: |
/transform |
Natural transformations | @from:, @to:, @verify: |
Transform tasks to prompts while preserving structure.
/meta "implement authentication"
# With explicit tier
/meta @tier:L5 "design microservices architecture"
# With template components
/meta @template:{context:expert}+{mode:cot}+{format:code} "build API"Categorical Laws:
F(id) = id(Identity)F(g ∘ f) = F(g) ∘ F(f)(Composition)
Refine prompts until quality threshold met.
/rmp @quality:0.85 "optimize database query"
# With iteration limit
/rmp @quality:0.9 @max_iterations:3 "build robust system"Categorical Laws:
return >=> f = f(Left Identity)f >=> return = f(Right Identity)(f >=> g) >=> h = f >=> (g >=> h)(Associativity)
Extract context from execution history.
# Extract recent context
/context @mode:extract @focus:recent @depth:5 "summarize progress"
# Create meta-observation
/context @mode:duplicate "why did this approach fail?"
# Context-aware transformation
/context @mode:extend @transform:summarize "create summary"Categorical Laws:
extract ∘ duplicate = id(Left Identity)duplicate ∘ duplicate = fmap duplicate ∘ duplicate(Associativity)
Switch between prompting strategies.
# Transform to chain-of-thought
/transform @from:zero-shot @to:chain-of-thought "explain algorithm"
# Compare strategies
/transform @mode:compare @from:few-shot @to:chain-of-thought "task"
# Analyze optimal strategy
/transform @mode:analyze "complex reasoning problem"Categorical Law:
α_B ∘ F(f) = G(f) ∘ α_A(Naturality condition)
Handle errors with monad semantics.
# Retry on failure
/chain @catch:retry:3 [/api-call→/process] "fetch data"
# Skip failed stages
/chain @catch:skip [/cache→/compute→/save] "expensive query"
# Fallback to alternative
/chain [/primary@catch:substitute:/backup→/process] "critical task"
# Best-effort with fallback
/rmp @fallback:return-best @quality:0.9 "complex analysis"Error Handling Laws:
catch(Right(a), h) = Right(a)(Catch Identity)catch(Left(e), h) = h(e)(Catch Error)
Track and visualize quality flow.
# Basic visualization
/chain @quality:visualize [/analyze→/design→/implement] "build feature"
# Flow diagram
/chain @quality:visualize:flow [/research→/prototype→/refine] "feature"
# Detailed breakdown
/chain @quality:visualize:detailed [/cmd1→/cmd2→/cmd3] "complex task"Categorical Foundation:
Hom_Q(A, B) = [0,1](Enriched hom-sets)q1 ⊗ q2 = min(q1, q2)(Quality tensor product)
Output of A becomes input of B.
/chain [/debug→/review→/test] "error in auth.py"Quality: quality(A → B) ≤ min(quality(A), quality(B))
Execute concurrently, aggregate results.
/chain [/review-security || /review-performance] "audit code"Quality: quality(A || B) = mean(quality(A), quality(B))
Combine capabilities, quality degrades to minimum.
@skills:compose(api-testing⊗validation) "create tests"Quality: quality(A ⊗ B) = min(quality(A), quality(B))
Monadic refinement with quality gates.
/rmp @quality:0.85 [analyze>=>design>=>implement] "build feature"Quality: Improves iteratively until threshold
| Modifier | Values | Default | Description |
|---|---|---|---|
@mode: |
active, iterative, dry-run, spec | active | Execution mode |
# Preview execution plan
/meta @mode:dry-run "complex task"
# Generate YAML specification
/meta @mode:spec "task"| Modifier | Values | Default | Description |
|---|---|---|---|
@quality: |
0.0-1.0 | 0.8 | Quality threshold |
@max_iterations: |
integer | 5 | Max RMP iterations |
/rmp @quality:0.9 @max_iterations:3 "optimize code"| Modifier | Values | Default | Description |
|---|---|---|---|
@tier: |
L1-L7 | auto | Complexity classification |
| Tier | Tokens | Pattern | Strategy |
|---|---|---|---|
| L1 | 600-1200 | Single op | DIRECT |
| L2 | 1500-3000 | A → B | DIRECT |
| L3 | 2500-4500 | A → B → C | MULTI_APPROACH |
| L4 | 3000-6000 | A || B || C | MULTI_APPROACH |
| L5 | 5500-9000 | Hierarchical | AUTONOMOUS_EVOLUTION |
| L6 | 8000-12000 | Iterative | AUTONOMOUS_EVOLUTION |
| L7 | 12000-22000 | Ensemble | AUTONOMOUS_EVOLUTION |
| Modifier | Values | Default | Description |
|---|---|---|---|
@catch: |
halt, log, retry:N, skip, substitute:/cmd | halt | Error behavior |
@fallback: |
return-best, return-last, use-default:[val], empty | - | Recovery strategy |
# Retry 3 times on failure
/chain @catch:retry:3 [/api→/process] "fetch data"
# Return best result if error
/rmp @fallback:return-best @quality:0.9 "task"| Modifier | Values | Default | Description |
|---|---|---|---|
@quality:visualize |
bar, flow, detailed, compact | bar | Visualization format |
# Bar chart (default)
/chain @quality:visualize [/A→/B→/C] "task"
# Flow diagram
/chain @quality:visualize:flow [/A→/B→/C] "task"
# Detailed breakdown
/chain @quality:visualize:detailed [/A→/B→/C] "task"
# Compact summary
/chain @quality:visualize:compact [/A→/B→/C] "task"| Modifier | Values | Default | Description |
|---|---|---|---|
@budget: |
integer, [array], auto | auto | Token budget |
@variance: |
percentage | 20% | Acceptable variance |
/chain @budget:20000 @variance:15% [/research→/design] "feature"| Modifier | Values | Default | Description |
|---|---|---|---|
@template: |
{context}+{mode}+{format} | auto | Template assembly |
Components:
{context:expert\|teacher\|reviewer\|debugger}{mode:direct\|cot\|multi\|iterative}{format:prose\|structured\|code\|checklist}
/meta @template:{context:expert}+{mode:cot}+{format:code} "implement feature"/meta "implement rate limiter"What happens:
- Auto-detects domain, tier, template
- Executes single-pass
- Returns result
/rmp @quality:0.85 @max_iterations:5 "optimize algorithm"What happens:
- Generate initial solution
- Assess quality (4 dimensions)
- If quality < 0.85: refine and repeat
- Stop when quality ≥ 0.85 or 5 iterations
- Return best result
/chain [/analyze→/design→/implement→/test] "build authentication"What happens:
/analyzereceives task, produces analysis/designreceives analysis, produces design/implementreceives design, produces code/testreceives code, produces test results- Return final result
/chain [/review-security || /review-performance || /review-style] "audit api.py"What happens:
- All three reviews execute concurrently
- Results aggregated/compared
- Return combined assessment
/chain @catch:retry:3 [/fetch-data→/process→/save] "user data"What happens:
/fetch-datatries, if fails → retry up to 3 times- If all retries fail → chain halts with error
- If any retry succeeds → continue to
/process /processand/saveexecute normally
/chain @quality:visualize [/analyze→/design→/implement] "build feature"Output includes:
┌────────────────────────────────────────────────────────────┐
│ QUALITY FLOW VISUALIZATION │
├────────────────────────────────────────────────────────────┤
│ │
│ /analyze 0.75 ███████████████░░░░░░░ 75% │
│ ↓ │
│ /design 0.82 ████████████████░░░░░░ 82% (+7%) │
│ ↓ │
│ /implement 0.68 █████████████░░░░░░░░░ 68% (-14%) │
│ │
│ Final Quality: 0.68 (Acceptable) │
└────────────────────────────────────────────────────────────┘
/meta @mode:iterative @tier:L5 @quality:0.9 @template:{context:expert}+{mode:cot}+{format:code} "build production auth system"What happens:
- Mode: iterative (enable RMP loop)
- Tier: L5 (hierarchical complexity)
- Quality: 0.9 (high threshold)
- Template: expert context + chain-of-thought + code format
- Iterate until quality ≥ 0.9
| Dimension | Weight | Question |
|---|---|---|
| Correctness | 40% | Does it solve the problem correctly? |
| Clarity | 25% | Is it clear and understandable? |
| Completeness | 20% | Are edge cases and requirements covered? |
| Efficiency | 15% | Is the solution well-designed? |
aggregate = 0.40 × correctness + 0.25 × clarity + 0.20 × completeness + 0.15 × efficiency
| Score | Status | Action |
|---|---|---|
| ≥0.9 | Excellent | Stop, success |
| 0.8-0.9 | Good | Stop, success |
| 0.7-0.8 | Acceptable | Continue if iterative |
| 0.6-0.7 | Poor | Refine if iterations remain |
| <0.6 | Failed | Abort or restructure |
The framework rigorously enforces 11 categorical laws:
- Functor Identity:
F(id) = id - Functor Composition:
F(g ∘ f) = F(g) ∘ F(f) - Monad Left Identity:
return >=> f = f - Monad Right Identity:
f >=> return = f - Monad Associativity:
(f >=> g) >=> h = f >=> (g >=> h) - Comonad Left Identity:
extract ∘ duplicate = id - Comonad Associativity:
duplicate ∘ duplicate = fmap duplicate ∘ duplicate - Naturality Condition:
α_B ∘ F(f) = G(f) ∘ α_A - Exception Catch Identity:
catch(Right(a), h) = Right(a) - Exception Catch Error:
catch(Left(e), h) = h(e) - Quality Monotonicity:
quality(A ⊗ B) ≤ min(quality(A), quality(B))
All commands produce standardized checkpoints:
CHECKPOINT_[type]_[n]:
command: /[command]
iteration: [n]
quality:
correctness: [0-1]
clarity: [0-1]
completeness: [0-1]
efficiency: [0-1]
aggregate: [0-1]
quality_delta: [+/- from previous]
budget:
used: [tokens]
remaining: [tokens]
variance: [%]
status: [CONTINUE | CONVERGED | MAX_ITERATIONS | HALT]- Claude Code: v1.0 or higher
- MCP Servers: None (self-contained)
- Platform: macOS, Linux
- Shell: Bash 4.0+
git clone https://github.com/categorical-meta-prompting/plugin.git
cd plugin
./INSTALL.sh# Copy commands
cp commands/*.md ~/.claude/commands/
# Copy skills
cp -r skills/meta-self ~/.claude/skills/
# Restart Claude CodeRemove installed files:
# Remove commands
rm ~/.claude/commands/chain.md
rm ~/.claude/commands/meta.md
rm ~/.claude/commands/rmp.md
rm ~/.claude/commands/context.md
rm ~/.claude/commands/transform.md
# Remove skills
rm -rf ~/.claude/skills/meta-self
# Restart Claude Code- Unified Syntax:
~/.claude/skills/meta-self/SKILL.md - Quick Start:
docs/QUICK-START.md - Examples:
docs/EXAMPLES.md - Changelog:
docs/CHANGELOG.md
# Preview execution plan
/meta @mode:dry-run "your task"
# Generate specification
/meta @mode:spec "your task"
# Read skill reference
cat ~/.claude/skills/meta-self/SKILL.md# Check quality trend in checkpoints
# If PLATEAU: Fixed-point reached, accept result
# If DEGRADING: Restructure approach# Reduce tier or adjust budget
/meta @tier:L4 @budget:15000 "simpler approach"# Consult meta-self skill for valid modifiers
cat ~/.claude/skills/meta-self/SKILL.mdContributions welcome! Please follow these guidelines:
- Categorical Rigor: All changes must preserve categorical laws
- Unified Syntax: Follow modifier naming conventions
- Quality Standards: Maintain quality ≥ 0.85
- Documentation: Update README and SKILL.md
MIT License - see LICENSE for details
Categorical Meta-Prompting Team
Inspired by:
- Gavranović et al. (ICML 2024): Categorical Deep Learning
- de Wynter et al. (2025): Category Theory for Meta-Prompting
- Bradley (2021): Enriched Category Theory of Language
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Plugin Wiki
Version: 2.4.0 Status: Production Ready Foundation: Category Theory (F, M, W, α, E, [0,1]-enriched) Compatibility: 100% backward compatible